package com.cool.store.dao.tp;

import cn.hutool.core.collection.CollStreamUtil;
import com.cool.store.entity.tp.TpRuleDO;
import com.cool.store.mapper.tp.TpRuleMapper;
import com.cool.store.request.tp.TpRuleQueryRequest;
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.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 十二分制-奖惩规则DAO
 * </p>
 *
 * @author wangff
 * @since 2025/11/4
 */
@Repository
@RequiredArgsConstructor
public class TpRuleDAO {
    private final TpRuleMapper tpRuleMapper;

    /**
     * 新增
     */
    public boolean insertSelective(TpRuleDO tpRuleDO) {
        return tpRuleMapper.insertSelective(tpRuleDO) > 0;
    }

    /**
     * 编辑
     */
    public boolean updateSelective(TpRuleDO tpRuleDO) {
        return tpRuleMapper.updateByPrimaryKeySelective(tpRuleDO) > 0;
    }

    /**
     * 删除
     */
    public boolean deleteByIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        Example example = new Example(TpRuleDO.class);
        example.createCriteria().andIn("id", ids);
        return tpRuleMapper.updateByExampleSelective(TpRuleDO.builder().deleted(1).build(), example) > 0;
    }

    /**
     * 根据id查询
     */
    public TpRuleDO getById(Long id) {
        return tpRuleMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据规则编码查询启用的规则
     */
    public TpRuleDO getEnableByCode(String ruleNo) {
        return tpRuleMapper.selectOne(TpRuleDO.builder().ruleNo(ruleNo).status(1).deleted(0).build());
    }

    public List<TpRuleDO> getEffectiveList(TpRuleQueryRequest request) {
        Example example = new Example(TpRuleDO.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(request.getProblemClassification())) {
            criteria.andEqualTo("problemClassification", request.getProblemClassification());
        }
        if (StringUtils.isNotBlank(request.getProjectCategory())) {
            criteria.andEqualTo("projectCategory", request.getProjectCategory());
        }
        if (StringUtils.isNotBlank(request.getProjectName())) {
            criteria.andLike("projectName", "%" + request.getProjectName() + "%");
        }
        if (Objects.nonNull(request.getStatus())) {
            criteria.andEqualTo("status", request.getStatus());
        }
        if (Objects.nonNull(request.getType())) {
            criteria.andEqualTo("type", request.getType());
        }
        criteria.andEqualTo("deleted", 0);
        example.setOrderByClause("create_time DESC");
        return tpRuleMapper.selectByExample(example);
    }

    /**
     * 修改启用状态
     * @param ids id列表
     * @param status 启用状态
     * @return boolean
     */
    public boolean updateStatus(List<Long> ids, Integer status) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        Example example = new Example(TpRuleDO.class);
        example.createCriteria()
                .andIn("id", ids);
        return tpRuleMapper.updateByExampleSelective(TpRuleDO.builder().status(status).build(), example) > 0;
    }
}
