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

import com.friday.custody.entity.DataDictionaryTypeOfFundValuationDetailsEntity;
import com.friday.custody.entity.DataDictionaryTypeOfFundValuationEntity;
import com.friday.custody.enums.DataDictionaryTypeEnum;
import com.friday.custody.service.basicService.DataDictionaryTypeOfFundValuationService;
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.HashMap;
import java.util.List;
import java.util.Map;
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.DataDictionaryTypeOfFundValuationDao;
import org.springframework.util.CollectionUtils;


@Service("dataDictionaryTypeOfFundValuationService")
public class DataDictionaryTypeOfFundValuationServiceImpl extends ServiceImpl<DataDictionaryTypeOfFundValuationDao, DataDictionaryTypeOfFundValuationEntity>
        implements DataDictionaryTypeOfFundValuationService, DataDictionaryBasicService {


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

        return new PageUtils(page);
    }

    @Override
    public Map<String, String> queryKeyValue() {
        List<DataDictionaryTypeOfFundValuationEntity> allDatas = baseMapper.selectList(new QueryWrapper<>());
        Map<String, String> result = new HashMap<>();
        if (!CollectionUtils.isEmpty(allDatas)){
            result = allDatas.stream().collect(Collectors.toMap(DataDictionaryTypeOfFundValuationEntity::getKeyStr, DataDictionaryTypeOfFundValuationEntity::getValue));
        } else {
            log.error("查询DataDictionaryTypeOfFundValuationEntity失败!");
        }
        return result;
    }

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


    @Override
    public Map<String, String> queryWithDetail(String trustNo) {
        Map<String, String> result = new HashMap<>();
        List<DataDictionaryTypeOfFundValuationEntity> allDatas = baseMapper.selectList(new QueryWrapper<>());
        if (!CollectionUtils.isEmpty(allDatas)){
            List<DataDictionaryTypeOfFundValuationDetailsEntity> detailsEntities = detailsService.selectVByTrustNo(trustNo);
            if (!CollectionUtils.isEmpty(detailsEntities)) {
                Map<String, Long> originValueKeyMap = detailsEntities.stream().collect(Collectors.toMap(DataDictionaryTypeOfFundValuationDetailsEntity::getOriginValue, DataDictionaryTypeOfFundValuationDetailsEntity::getDicId));
                Map<Long, String> idKeyMap = allDatas.stream().collect(Collectors.toMap(DataDictionaryTypeOfFundValuationEntity::getId, DataDictionaryTypeOfFundValuationEntity::getKeyStr));
                originValueKeyMap.forEach((originValue, id) -> {
                    result.put(originValue, idKeyMap.get(id));
                });
            } else {
                log.error("DataDictionaryTypeOfFundValuationDetailsEntity 查询失败！");
            }
        } else {
            log.error("DataDictionaryTypeOfFundValuationEntity数据字典查询失败!");
        }
        return result;
    }


    @Override
    public R queryData(String type) {
        List<DataDictionaryTypeOfFundValuationEntity> entities = baseMapper.selectList(new QueryWrapper<>());
        if (!CollectionUtils.isEmpty(entities)) {
            return R.ok("查询字典信息成功！")
                    .put("data", entities.stream().collect(Collectors.toMap(DataDictionaryTypeOfFundValuationEntity::getKeyStr
                            ,DataDictionaryTypeOfFundValuationEntity::getValue)));
        }
        return R.ok("字典信息为空，请联系管理员配置！");
    }
}