package com.cool.store.dao.config;

import com.cool.store.entity.config.ConfigItemCategoryDO;
import com.cool.store.entity.config.ConfigItemDO;
import com.cool.store.mapper.config.ConfigItemMapper;
import com.cool.store.utils.StringUtil;
import com.cool.store.vo.config.ConfigItemBaseInfoVO;
import com.cool.store.vo.config.ConfigItemVO;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Repository
@RequiredArgsConstructor
public class ConfigItemDAO {

    private final ConfigItemMapper configItemMapper;

    public boolean insertSelective(ConfigItemDO entity) {
        return configItemMapper.insertSelective(entity) > 0;
    }

    public boolean updateSelective(ConfigItemDO entity) {
        return configItemMapper.updateByPrimaryKeySelective(entity) > 0;
    }

    public ConfigItemDO getById(Long id) {
        return configItemMapper.selectByPrimaryKey(id);
    }

    public List<ConfigItemVO> queryByCondition(String itemName, String itemCode, Long categoryId, Integer status) {
        return configItemMapper.queryByCondition(itemName, itemCode, categoryId, status);
    }

    public Map<String, String>  queryAllActiveMap() {
        List<ConfigItemBaseInfoVO> configItemBaseInfoVOS = configItemMapper.queryAllActiveList();
        if (CollectionUtils.isEmpty(configItemBaseInfoVOS)){
            return new HashMap<>();
        }
        return configItemBaseInfoVOS.stream()
                .filter(v -> StringUtils.isNotBlank(v.getItemCode()))
                .collect(Collectors.toMap(ConfigItemBaseInfoVO::getItemCode, ConfigItemBaseInfoVO::getItemName, (a, b) -> a));
    }

    public boolean softDeleteByIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        ConfigItemDO update = new ConfigItemDO();
        update.setIsDeleted(1);
        Example example = new Example(ConfigItemDO.class);
        example.createCriteria().andIn("id", ids).andEqualTo("isDeleted", 0);
        return configItemMapper.updateByExampleSelective(update, example) > 0;
    }


    public boolean existsItemCode(String code, Long excludeId) {
        if (StringUtil.isEmpty(code)) {
            return false;
        }
        Example example = new Example(ConfigItemDO.class);
        Example.Criteria criteria = example.createCriteria().andEqualTo("sortNo", code).andEqualTo("isDeleted", 0);
        if (excludeId != null) {
            criteria.andNotEqualTo("id", excludeId);
        }
        return configItemMapper.selectCountByExample(example) > 0;
    }

    public boolean existsSortNo(Integer sortNo, Long excludeId) {
        if (sortNo == null) {
            return false;
        }
        Example example = new Example(ConfigItemDO.class);
        Example.Criteria criteria = example.createCriteria().andEqualTo("sortNo", sortNo).andEqualTo("isDeleted", 0);
        if (excludeId != null) {
            criteria.andNotEqualTo("id", excludeId);
        }
        return configItemMapper.selectCountByExample(example) > 0;
    }

    public int countByCategoryIds(List<Long> categoryIds) {
        if (CollectionUtils.isEmpty(categoryIds)) {
            return 0;
        }
        return configItemMapper.countByCategoryIds(categoryIds);
    }

    public Map<String,String> nameMap(List<String> codeList) {
        if (CollectionUtils.isEmpty(codeList)) {
            return new HashMap<>();
        }
        Example example = new Example(ConfigItemDO.class);
        example.createCriteria().andIn("itemCode", codeList).andEqualTo("isDeleted", 0);
        return configItemMapper.selectByExample( example).stream()
                .filter(v -> StringUtils.isNotBlank(v.getItemCode()))
                .collect(Collectors.toMap(ConfigItemDO::getItemCode, ConfigItemDO::getItemName, (a, b) -> a));
    }


}
