package com.starry.admin.modules.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.starry.admin.modules.system.mapper.SysDictDataMapper;
import com.starry.admin.modules.system.module.entity.SysDictDataEntity;
import com.starry.admin.modules.system.service.ISysDictDataService;
import com.starry.admin.modules.system.vo.SysDictDataVo;
import com.starry.admin.utils.DictUtils;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 字典数据表Service业务层处理
 *
 * @author admin
 */
@Service
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictDataEntity>
        implements
            ISysDictDataService {
    @Resource
    private SysDictDataMapper sysDictDataMapper;

    /**
     * 查询字典数据表
     *
     * @param dictDataId
     *            字典数据表主键
     * @return 字典数据表
     */
    @Override
    public SysDictDataEntity selectXlDictDataByDictDataId(Long dictDataId) {
        return sysDictDataMapper.selectXlDictDataByDictDataId(dictDataId);
    }

    /**
     * 查询字典数据表列表
     *
     * @param sysDictDataEntity
     *            字典数据表
     * @return 字典数据表
     */
    @Override
    public IPage<SysDictDataEntity> selectXlDictDataList(SysDictDataVo sysDictDataEntity) {
        return sysDictDataMapper.selectXlDictDataList(
                new Page<>(sysDictDataEntity.getPageNum(), sysDictDataEntity.getPageSize()), sysDictDataEntity);
    }

    /**
     * 新增字典数据表
     *
     * @param sysDictDataEntity
     *            字典数据表
     * @return 结果
     */
    @Override
    public boolean create(SysDictDataEntity sysDictDataEntity) {
        return save(sysDictDataEntity);
    }

    /**
     * 修改字典数据表
     *
     * @param sysDictDataEntity
     *            字典数据表
     * @return 结果
     */
    @Override
    public boolean update(SysDictDataEntity sysDictDataEntity) {
        return updateById(sysDictDataEntity);
    }

    /**
     * 批量删除字典数据表
     *
     * @param dictDataIds
     *            需要删除的字典数据表主键
     * @return 结果
     */
    @Override
    public int deleteXlDictDataByDictDataIds(Long[] dictDataIds) {
        return sysDictDataMapper.deleteBatchIds(Arrays.asList(dictDataIds));
    }

    /**
     * 删除字典数据表信息
     *
     * @param dictDataId
     *            字典数据表主键
     * @return 结果
     */
    @Override
    public int deleteXlDictDataByDictDataId(Long dictDataId) {
        return sysDictDataMapper.deleteById(dictDataId);
    }

    /**
     * 根据字典类型查询字典数据
     *
     * @param dictType
     *            字典类型
     * @return 字典数据集合信息
     */
    @Override
    public List<SysDictDataEntity> selectDictDataByType(String dictType) {
        List<SysDictDataEntity> dictDataList = DictUtils.getDictCache(dictType);
        if (CollectionUtil.isNotEmpty(dictDataList)) {
            return dictDataList;
        }
        dictDataList = sysDictDataMapper.selectDictDataByType(dictType);
        if (CollectionUtil.isNotEmpty(dictDataList)) {
            DictUtils.setDictCache(dictType, dictDataList);
            return dictDataList;
        }
        return null;
    }
}
