package cn.cslg.report.service.business;

import cn.cslg.report.common.core.base.Constants;
import cn.cslg.report.common.utils.LogExceptionUtil;
import cn.cslg.report.common.utils.Response;

import cn.cslg.report.entity.SysDictItem;

import cn.cslg.report.mapper.SystemDictItemMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Slf4j
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
@SuppressWarnings({"all"})
public class SystemDictItemService extends ServiceImpl<SystemDictItemMapper, SysDictItem> {
    /**
     * @return 成功返回客户列表信息 失败返回错误信息
     * @title 获取部分客户
     * @author 沈永艺
     */
    private final SystemDictItemMapper systemDictItemMapper;

    public String getSystemKey() {
        try {
            Map<String, Object> lst = new HashMap<>();
            //查询所有字典
            List<SysDictItem> sysDictList = this.list();
            for (SysDictItem sysDict : sysDictList) {
                //用字典的KEY值去字典项表内查询对应字典项
                LambdaQueryWrapper<SysDictItem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(SysDictItem::getDictParentKey, sysDict.getDictParentKey());
                //装填数据
                //字典格式 KEY:KEY值对应的字典项
                if (!lst.containsKey(sysDict.getDictParentKey())) {
                    lst.put(sysDict.getDictParentKey(), systemDictItemMapper.selectList(lambdaQueryWrapper));
                }
//                //数据字典格式 dataDict:查询的所有数据字典数据
//                lst.put("dataDict", dataSourceService.list());
            }
            return Response.success(lst);
        } catch (Exception ex) {
            ex.printStackTrace();
            log.error("获得字典项失败,错误信息:" + LogExceptionUtil.getMessage(ex));
            return Response.error(Constants.NET_ERROR);
        }
    }

    public List<Map<String, Object>> getCronConClusions(List<Integer> types) {
        if(types==null||types.size()==0){
            types=Arrays.asList(0,1,2,3,4,5);
        }
        List<String> keys = new ArrayList<>();
        for (Integer type : types) {
            switch (type) {
                case 0:
                    keys.add("INVALID_ASSESS");
                    break;
                case 1:
                    keys.add("THIRD_ASSESS");
                    break;
                case 2:
                    keys.add("STABILITY_ASSESS");
                    break;
                case 3:
                    keys.add("FTO_ASSESS");
                    break;
                case 4:
                    keys.add("TORT_ASSESS");
                    break;
                case 5:
                    keys.add("AVOID_ASSESS");
                    break;
            }
        }
        //根据key值查
        LambdaQueryWrapper<SysDictItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SysDictItem::getDictParentKey, keys);
        List<SysDictItem> sysDictItems = this.list(wrapper);
        List<Map<String, Object>> mapList = new ArrayList<>();
        sysDictItems.forEach(item -> {
            Map<String, Object> map = new HashMap<>();
            switch (item.getDictParentKey()) {
                case "INVALID_ASSESS":
                    map.put("rType", 0);
                    map.put("rTypeName", "无效分析");
                    break;
                case "THIRD_ASSESS":
                    map.put("rType", 1);
                    map.put("rTypeName", "第三方意见");
                    break;
                case "STABILITY_ASSESS":
                    map.put("rType", 2);
                    map.put("rTypeName", "稳定性分析");
                    break;
                case "FTO_ASSESS":
                    map.put("rType", 3);
                    map.put("rTypeName", "FTO调查");
                    break;
                case "TORT_ASSESS":
                    map.put("rType", 4);
                    map.put("rTypeName", "侵权分析");
                    break;
                case "AVOID_ASSESS":
                    map.put("rType", 5);
                    map.put("rTypeName", "回避设计");
                    break;
            }
            map.put("cTypeId", item.getId());
            map.put("cTypeName", item.getDictChildLabel());
            map.put("cType", item.getDictChildValue());
            mapList.add(map);
        });
        return mapList;
    }

}
