package com.storlead.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.storlead.system.pojo.dto.DictExtQueryParam;
import com.storlead.system.pojo.entity.SysDictDetailEntity;
import com.storlead.system.mapper.SysDictDetailMapper;
import com.storlead.system.pojo.entity.SysDictEntity;
import com.storlead.system.service.SysDictDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.storlead.system.service.SysDictService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 字典详情 服务实现类
 * </p>
 *
 * @author chenkq
 * @since 2022-09-09
 */
@Service
public class SysDictDetailServiceImpl extends ServiceImpl<SysDictDetailMapper, SysDictDetailEntity> implements SysDictDetailService {

    @Resource
    private SysDictService sysDictService;
    /**
     * 根据dict code 查询字典详情list
     */
    @Override
    public Map<String,List<SysDictDetailEntity>> getDictDetailByCodes(String... dictCode) {
        if (dictCode == null || dictCode.equals("")) {
            return null;
        }
        LambdaQueryWrapper<SysDictEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(SysDictEntity::getIsDelete,Integer.valueOf(0));
        queryWrapper.eq(SysDictEntity::getEnabled,1);
        if (dictCode.length == 1) {
            queryWrapper.eq(SysDictEntity::getCode,dictCode);
        } else {
            queryWrapper.in(SysDictEntity::getCode,Arrays.asList(dictCode));
        }
        List<SysDictEntity> vos = sysDictService.list(queryWrapper);
        if (CollectionUtils.isEmpty(vos)) {
            return Collections.EMPTY_MAP;
        }
        List<Long> dictIds = vos.stream().map(SysDictEntity::getDictId).collect(Collectors.toList());
        List<SysDictDetailEntity> vos1 = this.list(new LambdaQueryWrapper<SysDictDetailEntity>().in(SysDictDetailEntity::getDictId,dictIds));
        if (CollectionUtils.isEmpty(vos1)) {
            return Collections.EMPTY_MAP;
        }
        Map<String,List<SysDictDetailEntity>> dictMap = new HashMap<>();
        vos.stream().forEach(e -> {
            List<SysDictDetailEntity> rs = vos1.stream().filter(s ->s.getDictId().equals(e.getDictId())).collect(Collectors.toList());
            dictMap.put(e.getCode(),rs);
        });
        return dictMap;
    }

    @Override
    public Map<String,SysDictDetailEntity> getDictDetailByCode(String dictCode) {
        if (dictCode == null || dictCode.equals("")) {
            return null;
        }
        LambdaQueryWrapper<SysDictEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(SysDictEntity::getIsDelete,Integer.valueOf(0));
//        queryWrapper.eq(SysDictEntity::getEnabled,1);

        queryWrapper.eq(SysDictEntity::getCode,dictCode);
        List<SysDictEntity> vos = sysDictService.list(queryWrapper);
        if (CollectionUtils.isEmpty(vos)) {
            return Collections.EMPTY_MAP;
        }
        List<Long> dictIds = vos.stream().map(SysDictEntity::getDictId).collect(Collectors.toList());
        List<SysDictDetailEntity> vos1 = this.list(new LambdaQueryWrapper<SysDictDetailEntity>().in(SysDictDetailEntity::getDictId,dictIds));
        if (CollectionUtils.isEmpty(vos1)) {
            return Collections.EMPTY_MAP;
        }
        Map<String,SysDictDetailEntity> dictMap = vos1.stream().collect(Collectors.toMap(SysDictDetailEntity::getValue, a -> a, (u1, u2) -> u1));
        return dictMap;
    }

    @Override
    public Map<String,String> getDictMapByCode(String dictCode) {
        if (dictCode == null || dictCode.equals("")) {
            return null;
        }
        LambdaQueryWrapper<SysDictEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(SysDictEntity::getIsDelete,Integer.valueOf(0));
//        queryWrapper.eq(SysDictEntity::getEnabled,1);
        queryWrapper.eq(SysDictEntity::getCode,dictCode);
        queryWrapper.last(" limit 1");

        SysDictEntity  vo = sysDictService.getOne(queryWrapper);
        if (Objects.isNull(vo)) {
            return Collections.EMPTY_MAP;
        }
        List<SysDictDetailEntity> vos1 = this.list(new LambdaQueryWrapper<SysDictDetailEntity>().eq(SysDictDetailEntity::getDictId,vo.getDictId()));
        if (CollectionUtils.isEmpty(vos1)) {
            return Collections.EMPTY_MAP;
        }
        Map<String,String> dictMap = vos1.stream().collect(Collectors.toMap(SysDictDetailEntity::getValue, SysDictDetailEntity::getLabel));
        return dictMap;
    }

    @Override
    public String getWorkDataById(DictExtQueryParam param) {
        return this.baseMapper.selectWorkDataNameById(param);
    }
}
