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

import com.friday.custody.entity.DataDictionaryTypeCertificateCsdcDetailsEntity;
import com.friday.custody.enums.DataDictionaryTypeEnum;
import com.friday.custody.service.basicService.DataDictionaryTypeCertificateCsdcDetailsService;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

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.DataDictionaryTypeCertificateCsdcDao;
import com.friday.custody.entity.DataDictionaryTypeCertificateCsdcEntity;
import com.friday.custody.service.basicService.DataDictionaryTypeCertificateCsdcService;
import org.springframework.util.CollectionUtils;


@Service("dataDictionaryTypeCertificateCsdcService")
public class DataDictionaryTypeCertificateCsdcServiceImpl extends ServiceImpl<DataDictionaryTypeCertificateCsdcDao, DataDictionaryTypeCertificateCsdcEntity>
        implements DataDictionaryTypeCertificateCsdcService, DataDictionaryBasicService {

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

        return new PageUtils(page);
    }

    @Autowired
    private DataDictionaryTypeCertificateCsdcDetailsService detailsService;

    @Override
    public Map<String, Map<String, String>> queryCertificateWithDetail(String trustNo) {
        HashMap<String, Map<String, String>> result = new HashMap<>();
        List<DataDictionaryTypeCertificateCsdcEntity> allDatas = baseMapper.selectList(new QueryWrapper<>());
        if (!CollectionUtils.isEmpty(allDatas)) {
            List<DataDictionaryTypeCertificateCsdcDetailsEntity> detailsEntities = detailsService.selectVByTrustNo(trustNo);
            if (!CollectionUtils.isEmpty(detailsEntities)) {
                //key dicid   value originValues
                Map<Integer, List<String>> certificateMap = new HashMap<>();
                detailsEntities.forEach(item->{
                    List<String> originValues = certificateMap.get(item.getDicId());
                    if (CollectionUtils.isEmpty(originValues)) {
                        originValues = new ArrayList<>();
                        certificateMap.put(item.getDicId(),originValues);
                    }
                    originValues.add(item.getOriginValue());
                });
                Map<Integer, DataDictionaryTypeCertificateCsdcEntity> idEntityMap = allDatas.stream().collect(Collectors.toMap(DataDictionaryTypeCertificateCsdcEntity::getId, x -> x));
                certificateMap.forEach((id, originValues) -> {
                    DataDictionaryTypeCertificateCsdcEntity entity = idEntityMap.get(id);
                    if (!Objects.isNull(entity)) {
                        Map<String, String> childMap = result.get(entity.getCustomerKey());
                        if (Objects.isNull(childMap)) {
                            childMap = new HashMap<>();
                            result.put(entity.getCustomerKey(), childMap);
                        }
                        for (String originValue : originValues) {
                            childMap.put(originValue, entity.getKeyStr());
                        }
                    }
                });
            } else {
                log.error("DataDictionaryTypeCustomerCsdcDetailsEntity 查询失败！");
            }
        } else {
            log.error("DataDictionaryTypeCustomerCsdcEntity 查询失败！");
        }
        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<DataDictionaryTypeCertificateCsdcEntity> 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.getCustomerKey());
                if (stringStringMap == null) {
                    stringStringMap = new HashMap<>();
                    result.put(entity.getCustomerKey(), stringStringMap);
                }
                stringStringMap.put(entity.getKeyStr(), entity.getValue());
            });
            return R.ok("查询字典信息成功！").put("data", result);
        }
        return R.ok("字典信息为空，请联系管理员配置！");
    }
}