package cn.iocoder.yudao.module.system.api.dict;

import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.system.api.dict.dto.DictDataRespDTO;
import cn.iocoder.yudao.module.system.controller.admin.dict.vo.data.DictDataSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.dict.vo.type.DictTypeSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dict.DictDataDO;
import cn.iocoder.yudao.module.system.dal.dataobject.dict.DictTypeDO;
import cn.iocoder.yudao.module.system.service.dict.DictDataService;
import cn.iocoder.yudao.module.system.service.dict.DictTypeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 字典数据 API 实现类
 *
 * @author 芋道源码
 */
@Service
public class DictDataApiImpl implements DictDataApi {

    @Resource
    private DictDataService dictDataService;
    @Resource
    private DictTypeService dictTypeService;

    @Override
    public void validateDictDataList(String dictType, Collection<String> values) {
        dictDataService.validateDictDataList(dictType, values);
    }

    @Override
    public DictDataRespDTO getDictData(String dictType, String value) {
        DictDataDO dictData = dictDataService.getDictData(dictType, value);
        return BeanUtils.toBean(dictData, DictDataRespDTO.class);
    }

    @Override
    public DictDataRespDTO parseDictData(String dictType, String label) {
        DictDataDO dictData = dictDataService.parseDictData(dictType, label);
        return BeanUtils.toBean(dictData, DictDataRespDTO.class);
    }

    @Override
    public List<DictDataRespDTO> getDictDataList(String dictType) {
        List<DictDataDO> list = dictDataService.getDictDataListByDictType(dictType);
        return BeanUtils.toBean(list, DictDataRespDTO.class);
    }

    /**
     * 判断数据字典是否存在
     * @param sysDictType
     * @return
     */
    @Override
    public Boolean exist(String sysDictType) {
        DictTypeDO dictType = dictTypeService.getDictType(sysDictType);
        if (ObjectUtil.isEmpty(dictType)){
         return false;
        }
        return true;
    }

    /**
     * 创建数据字典
     * @param name
     * @param type
     */
    @Override
    public void createDictType(String name,String type) {
        DictTypeSaveReqVO createReqVO =new DictTypeSaveReqVO();
        createReqVO.setName(name);
        createReqVO.setType(type);
        createReqVO.setStatus(0);
        dictTypeService.createDictType(createReqVO);
    }

    /**
     * 增加字典数据项
     * @param dictType
     * @param label
     * @param value
     */
    @Override
    public void addDictData(String dictType,String label,String value, Integer sort) {

        DictDataSaveReqVO createReqVO =new DictDataSaveReqVO();
        createReqVO.setStatus(0);
        createReqVO.setDictType(dictType);
        createReqVO.setLabel(label);
        createReqVO.setValue(value);
        createReqVO.setSort(sort);
        dictDataService.createDictData(createReqVO);
    }

    /**
     * 删除字典数据项
     * @param ids
     */
    @Override
    public void deleteDictDataByIds(Long[] ids) {
        for (Long id : ids) {
            dictDataService.deleteDictData(id);
        }

    }

    /**
     * 修改字典数据
     * @author 小李
     * @date 9:48 2024/8/28
     * @param dictDataRespDTO 字典对象
     **/
    @Override
    public void updateDictData(DictDataRespDTO dictDataRespDTO){
        dictDataService.updateDictData(dictDataRespDTO);
    }

    /**
     * 获得包含查看id的字典数据
     * @author 小李
     * @date 15:38 2024/8/28
     * @param dataIds id集合
     **/
    @Override
    public List<DictDataRespDTO> getDictDataListById(List<Long> dataIds){
        List<DictDataDO> dictDataListById = dictDataService.getDictDataListById(dataIds);
        return dictDataListById.stream().map(item -> BeanUtils.toBean(item, DictDataRespDTO.class)).collect(Collectors.toList());
    }
}
