package com.cool.store.dao.config;

import com.cool.store.entity.config.ConfigOptionDO;
import com.cool.store.mapper.config.ConfigOptionMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 配置选项 DAO
 */
@Repository
@RequiredArgsConstructor
public class ConfigOptionDAO {
    private final ConfigOptionMapper configOptionMapper;

    public boolean insertSelective(ConfigOptionDO option) {
        return configOptionMapper.insertSelective(option) > 0;
    }

    public boolean updateSelective(ConfigOptionDO option) {
        return configOptionMapper.updateByPrimaryKeySelective(option) > 0;
    }

    public ConfigOptionDO getById(Long id) {
        return configOptionMapper.selectByPrimaryKey(id);
    }

    public boolean deleteByIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        Example example = new Example(ConfigOptionDO.class);
        example.createCriteria().andIn("id", ids);
        return configOptionMapper.deleteByExample(example) > 0;
    }

    public List<ConfigOptionDO> query(String categoryCode, String itemCode, Integer active, String optionName,String expenseTypeCode) {
        Example example = new Example(ConfigOptionDO.class);
        Example.Criteria criteria = example.createCriteria();
        if (Objects.nonNull(categoryCode)) {
            criteria.andEqualTo("categoryCode", categoryCode);
        }
        if (Objects.nonNull(itemCode)) {
            criteria.andEqualTo("itemCode", itemCode);
        }
        if (Objects.nonNull(active)) {
            criteria.andEqualTo("active", active);
        }
        if (Objects.nonNull(expenseTypeCode)) {
            criteria.andEqualTo("expenseTypeCode", expenseTypeCode);
        }
        if (Objects.nonNull(optionName) && !optionName.trim().isEmpty()) {
            criteria.andLike("optionName", "%" + optionName.trim() + "%");
        }
        example.setOrderByClause("sort_order ASC");
        return configOptionMapper.selectByExample(example);
    }

    public List<ConfigOptionDO> listActiveByIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return configOptionMapper.listActiveByIds(ids);
    }

    /**
     * 校验排序是否已存在（全局唯一，不区分类型/配置项）
     * @param sortOrder 排序值
     * @param excludeId 排除自身ID（新增传 null；修改传当前ID）
     */
    public boolean existsSortOrder(Integer sortOrder, Long excludeId) {
        if (sortOrder == null) {
            return false;
        }
        Example example = new Example(ConfigOptionDO.class);
        Example.Criteria criteria = example.createCriteria()
                .andEqualTo("sortOrder", sortOrder);
        criteria.andEqualTo("active", 1);
        if (excludeId != null) {
            criteria.andNotEqualTo("id", excludeId);
        }
        return configOptionMapper.selectCountByExample(example) > 0;
    }

    public boolean existsOptionName(String optionName, Long excludeId) {
        if (optionName == null || optionName.trim().isEmpty()) {
            return false;
        }
        Example example = new Example(ConfigOptionDO.class);
        Example.Criteria criteria = example.createCriteria()
                .andEqualTo("optionName", optionName.trim());
        criteria.andEqualTo("active", 1);
        if (excludeId != null) {
            criteria.andNotEqualTo("id", excludeId);
        }
        return configOptionMapper.selectCountByExample(example) > 0;
    }
}
