package cn.gov.customs.wxjy.system.service.impl;

import cn.gov.customs.wxjy.common.constant.UserConstants;
import cn.gov.customs.wxjy.common.utils.DictUtils;
import cn.gov.customs.wxjy.common.utils.StringUtils;
import cn.gov.customs.wxjy.common.utils.uuid.SnowflakeIdWorker;
import cn.gov.customs.wxjy.system.dao.DictDataDao;
import cn.gov.customs.wxjy.system.pojo.DictData;
import cn.gov.customs.wxjy.system.pojo.DictDataQuery;
import cn.gov.customs.wxjy.system.service.DictDataService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 字典数据Service业务层处理
 *
 * @author xiong
 * @date 2025-11-07
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class DictDataServiceImpl implements DictDataService {
    @Autowired
    private DictDataDao dictDataMapper;

    /**
     * 根据条件分页查询字典数据
     * 字典数据信息
     * @return 字典数据集合信息
     */
    @Override
    public PageInfo<DictData> selectDictDataList(DictDataQuery query) {
        PageHelper.startPage(query.getPageIndex(), query.getPageSize());
        List<DictData> list = dictDataMapper.selectDictDataList(query.getDictType(),query.getDictLabel(),query.getStatus());
        return PageInfo.of(list);
    }

    /**
     * 根据字典类型和字典键值查询字典数据信息
     *
     * @param dictType 字典类型
     * @param dictValue 字典键值
     * @return 字典标签
     */
    @Override
    public String selectDictLabel(String dictType, String dictValue)
    {
        return dictDataMapper.selectDictLabel(dictType, dictValue);
    }

    /**
     * 根据字典数据ID查询信息
     *
     * @param dictCode 字典数据ID
     * @return 字典数据
     */
    @Override
    public DictData selectDictDataById(Long dictCode)
    {
        return dictDataMapper.selectDictDataById(dictCode);
    }

    /**
     * 批量删除字典数据信息
     *
     * @param dictCodes 需要删除的字典数据ID
     */
    @Override
    public void deleteDictDataByIds(Long[] dictCodes)
    {
        for (Long dictCode : dictCodes)
        {
            DictData data = selectDictDataById(dictCode);
            dictDataMapper.deleteDictDataById(dictCode);
            List<DictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDatas);
        }
    }

    /**
     * 新增保存字典数据信息
     *
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    public int insertDictData(DictData data)
    {
        data.setDictCode(new SnowflakeIdWorker(0, 0).nextId());
        int row = dictDataMapper.insertDictData(data);
        if (row > 0)
        {
            List<DictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDatas);
        }
        return row;
    }

    /**
     * 修改保存字典数据信息
     *
     * @param data 字典数据信息
     * @return 结果
     */
    @Override
    public int updateDictData(DictData data)
    {
        int row = dictDataMapper.updateDictData(data);
        if (row > 0)
        {
            List<DictData> dictDatas = dictDataMapper.selectDictDataByType(data.getDictType());
            DictUtils.setDictCache(data.getDictType(), dictDatas);
        }
        return row;
    }

    @Override
    public boolean checkDictDataUnique(DictData dictData) {
        Long PkDictType = StringUtils.isNull(dictData.getDictCode()) ? -1L : dictData.getDictCode();
        DictData data = dictDataMapper.checkDictDataUnique(dictData.getDictType(),dictData.getDictValue());
        if (StringUtils.isNotNull(data) && data.getDictCode().longValue() != PkDictType.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public int countDictDataByType(String dictType) {
        return dictDataMapper.countDictDataByType(dictType);
    }
}
