package com.friday.custody.service.basicService.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.friday.custody.dao.DataDictionaryOpenStateDao;
import com.friday.custody.entity.DataDictionaryOpenStateDetailsEntity;
import com.friday.custody.entity.DataDictionaryOpenStateEntity;
import com.friday.custody.enums.DataDictionaryTypeEnum;
import com.friday.custody.service.basicService.DataDictionaryOpenStateDetailsService;
import com.friday.custody.service.basicService.DataDictionaryOpenStateService;
import com.friday.custody.service.logicService.DataDictionaryBasicService;
import com.friday.custody.utils.PageUtils;
import com.friday.custody.utils.Query;
import com.friday.custody.utils.R;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


@Service("dataDictionaryOpenStateService")
public class DataDictionaryOpenStateServiceImpl extends ServiceImpl<DataDictionaryOpenStateDao, DataDictionaryOpenStateEntity>
        implements DataDictionaryOpenStateService , DataDictionaryBasicService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DataDictionaryOpenStateEntity> page = this.page(
                new Query<DataDictionaryOpenStateEntity>().getPage(params),
                new QueryWrapper<DataDictionaryOpenStateEntity>()
        );

        return new PageUtils(page);
    }

    @Autowired
    private DataDictionaryOpenStateDetailsService detailsService;

    @Override
    public Map<String, Map<String, String>> queryOpenStateWithDetail(String trustNo) {
        HashMap<String, Map<String, String>> result = new HashMap<>();
        //查询出所有开放状态
        List<DataDictionaryOpenStateEntity> allDatas = baseMapper.selectList(new QueryWrapper<>());
        if (!CollectionUtils.isEmpty(allDatas)) {
            //通过有trustNo查询出 此托管的数据字典
            List<DataDictionaryOpenStateDetailsEntity> detailsEntities = detailsService.selectVByTrustNo(trustNo);
            if (!CollectionUtils.isEmpty(detailsEntities)) {
                //key dicid   value originValues
                Map<Integer, List<String>> openStateMap = new HashMap<>();
                detailsEntities.forEach(item->{
                    //托管数据字典details中的dicId作为key，托管原数值作为value
                    List<String> originValues = openStateMap.get(item.getDicId());
                    if (CollectionUtils.isEmpty(originValues)) {
                        originValues = new ArrayList<>();
                        openStateMap.put(item.getDicId(),originValues);
                    }
                    originValues.add(item.getOriginValue());
                });
                //查询全部开放状态数据字典，id(details表中的dicId)作为key
                Map<Integer, DataDictionaryOpenStateEntity> idEntityMap = allDatas.stream().collect(Collectors.toMap(DataDictionaryOpenStateEntity::getId, x -> x));
                openStateMap.forEach((id, originValues) -> {
                    DataDictionaryOpenStateEntity entity = idEntityMap.get(id);
                    if (!Objects.isNull(entity)) {
                        //type作为主键
                        Map<String, String> childMap = result.get(entity.getTypeKey());
                        //如果当前type没有值,则添加 type作为主键, Map<托管原始值, 对应数据字典中的key>作为值
                        if (Objects.isNull(childMap)) {
                            childMap = new HashMap<>();
                            result.put(entity.getTypeKey(), childMap);
                        }
                        for (String originValue : originValues) {
                            childMap.put(originValue, entity.getKeyStr());
                        }
                    }
                });
            } else {
                log.error("DataDictionaryOpenStateDetailsEntity 查询失败！");
            }
        } else {
            log.error("DataDictionaryOpenStateDetailsEntity 查询失败！");
        }
        return result;
    }

    @Override
    public Map<String, String> queryWithDetail(DataDictionaryTypeEnum dicType, String trustNo) {
        return null;
    }

    @Override
    public Map<String, String> queryWithDetail(String trustNo) {
        return null;
    }


    @Override
    public R queryData(String type) {
        List<DataDictionaryOpenStateEntity> entities = baseMapper.selectList(new QueryWrapper<>());
        if (!CollectionUtils.isEmpty(entities)) {
            Map<String, Map<String, String>> result = new HashMap<>();
            entities.forEach(entity -> {
                Map<String, String> stringStringMap = result.get(entity.getTypeKey());
                if (stringStringMap == null) {
                    stringStringMap = new HashMap<>();
                    result.put(entity.getTypeKey(), stringStringMap);
                }
                stringStringMap.put(entity.getKeyStr(), entity.getValue());
            });
            return R.ok("查询字典信息成功！").put("data", result);
        }
        return R.ok("字典信息为空，请联系管理员配置！");
    }
}