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

import com.friday.custody.entity.DataDictionaryTypesOfFeeDetailsEntity;
import com.friday.custody.enums.DataDictionaryTypeEnum;
import com.friday.custody.service.basicService.DataDictionaryTypesOfFeeDetailsService;
import com.friday.custody.service.logicService.DataDictionaryBasicService;
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.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.utils.PageUtils;
import com.friday.custody.utils.Query;

import com.friday.custody.dao.DataDictionaryTypesOfFeeDao;
import com.friday.custody.entity.DataDictionaryTypesOfFeeEntity;
import com.friday.custody.service.basicService.DataDictionaryTypesOfFeeService;


@Service("dataDictionaryTypesOfFeeService")
public class DataDictionaryTypesOfFeeServiceImpl extends ServiceImpl<DataDictionaryTypesOfFeeDao, DataDictionaryTypesOfFeeEntity>
        implements DataDictionaryTypesOfFeeService, DataDictionaryBasicService {

    @Autowired
    private DataDictionaryTypesOfFeeDao dataDictionaryTypesOfFeeDao;

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

        return new PageUtils(page);
    }

    @Override
    public DataDictionaryTypesOfFeeEntity queryByOriginValue(String key, String trustNo) {
        DataDictionaryTypesOfFeeEntity entity = dataDictionaryTypesOfFeeDao.queryByOriginvalueAndTrustNo(key,trustNo);
        return entity;
    }

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

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

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

}