package com.cool.store.dao.config;

import com.cool.store.entity.config.ConfigItemCategoryDO;
import com.cool.store.mapper.config.ConfigItemCategoryMapper;
import com.cool.store.vo.config.ConfigItemBaseInfoVO;
import com.cool.store.vo.config.ConfigItemCategoryBaseInfoVO;
import com.cool.store.vo.config.ConfigItemCategoryVO;
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.*;
import java.util.stream.Collectors;

@Repository
@RequiredArgsConstructor
public class ConfigItemCategoryDAO {

    private final ConfigItemCategoryMapper configItemCategoryMapper;

    public boolean insertSelective(ConfigItemCategoryDO entity) {
        return configItemCategoryMapper.insertSelective(entity) > 0;
    }

    public boolean updateSelective(ConfigItemCategoryDO entity) {
        return configItemCategoryMapper.updateByPrimaryKeySelective(entity) > 0;
    }

    public ConfigItemCategoryDO getById(Long id) {
        return configItemCategoryMapper.selectByPrimaryKey(id);
    }

    public List<ConfigItemCategoryVO> queryByCondition(String categoryName, Integer status) {
        return configItemCategoryMapper.queryByCondition(categoryName, status);
    }

    public List<ConfigItemCategoryBaseInfoVO> queryAllList() {
        return configItemCategoryMapper.queryAllActiveList();
    }

    public Map<String,String> queryAllListMap() {
        List<ConfigItemCategoryBaseInfoVO> configItemBaseInfoVOS = configItemCategoryMapper.queryAllActiveList();
        if (CollectionUtils.isEmpty(configItemBaseInfoVOS)){
            return new HashMap<>();
        }
        return configItemCategoryMapper.queryAllActiveList().stream()
                .filter(v -> StringUtils.isNotBlank(v.getCategoryCode()))
                .collect(Collectors.toMap(ConfigItemCategoryBaseInfoVO::getCategoryCode, ConfigItemCategoryBaseInfoVO::getCategoryName, (a, b) -> a));
    }

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

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


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

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