package com.cool.store.dao.store;

import com.cool.store.entity.AssessmentDataDO;
import com.cool.store.entity.CloseStoreAuditRecordDO;
import com.cool.store.entity.store.StoreTypeDO;
import com.cool.store.entity.store.StoreTypeOptionDO;
import com.cool.store.enums.close.CloseStoreAuditStatusEnum;
import com.cool.store.mapper.store.StoreTypeOptionMapper;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 店型-选项 DAO
 */
@Repository
@RequiredArgsConstructor
public class StoreTypeOptionDAO {
    private final StoreTypeOptionMapper mapper;

    public boolean insertBatchSelective(List<StoreTypeOptionDO> list) {
        if (CollectionUtils.isEmpty(list)) {
            return false;
        }
        return mapper.insertBatch(list) > 0;
    }

    public boolean deleteByStoreTypeId(Long storeTypeId) {
        return mapper.delete(StoreTypeOptionDO.builder().storeTypeId(storeTypeId).build()) > 0;
    }

    public Integer softDeleteByStoreTypeId(List<Long> storeTypeIds){
        Example example = new Example(StoreTypeOptionDO.class);
        StoreTypeOptionDO update = new StoreTypeOptionDO();
        update.setVisible(1);
        example.createCriteria()
                .andIn("storeTypeId", storeTypeIds);
        return mapper.updateByExampleSelective(update,example);
    }

    public List<StoreTypeOptionDO> selectByIds(List<Long> optionIds,Long storeTypeId){
        if (storeTypeId==null||CollectionUtils.isEmpty(optionIds)){
            return new ArrayList<>();
        }
        Example example = new Example(StoreTypeOptionDO.class);
        example.createCriteria().andIn("optionId", optionIds).andEqualTo("storeTypeId",storeTypeId);
        return mapper.selectByExample(example);
    }

    public List<StoreTypeOptionDO> listByStoreTypeId(Long storeTypeId) {
        return mapper.select(StoreTypeOptionDO.builder().storeTypeId(storeTypeId).build());
    }

    public List<StoreTypeOptionDO> query(Long storeTypeId, String itemCode) {
        Example example = new Example(StoreTypeOptionDO.class);
        Example.Criteria criteria = example.createCriteria();
        if (Objects.nonNull(storeTypeId)) {
            criteria.andEqualTo("storeTypeId", storeTypeId);
        }
        if (Objects.nonNull(itemCode)) {
            criteria.andEqualTo("itemCode", itemCode);
        }
        return mapper.selectByExample(example);
    }

    public int countByOptionId(Long optionId) {
        if (optionId == null) {
            return 0;
        }
        Example example = new Example(StoreTypeOptionDO.class);
        example.createCriteria().andEqualTo("optionId", optionId).andEqualTo("visible",0);
        return mapper.selectCountByExample(example);
    }
}
