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

import com.friday.custody.enums.DataDictionaryTypeEnum;
import com.friday.custody.service.logicService.DataDictionaryBasicService;
import com.friday.custody.utils.R;
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.DataDictionaryBankCodeDao;
import com.friday.custody.entity.DataDictionaryBankCodeEntity;
import com.friday.custody.service.basicService.DataDictionaryBankCodeService;
import org.springframework.util.CollectionUtils;


@Service("dataDictionaryBankCodeService")
public class DataDictionaryBankCodeServiceImpl extends ServiceImpl<DataDictionaryBankCodeDao, DataDictionaryBankCodeEntity>
        implements DataDictionaryBankCodeService, DataDictionaryBasicService {

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

        return new PageUtils(page);
    }
    @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<>();
        QueryWrapper<DataDictionaryBankCodeEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("origin_value", trustNo);
        List<DataDictionaryBankCodeEntity> allDatas = baseMapper.selectList(queryWrapper);
        if (!CollectionUtils.isEmpty(allDatas)) {
            result = allDatas.stream().collect(Collectors.toMap(DataDictionaryBankCodeEntity::getMapValue, DataDictionaryBankCodeEntity::getKeyStr));
        } else {
            log.error("DataDictionaryBankCodeEntity 查询失败！");
        }
        return result;
    }
    @Override
    public R queryData(String type) {
        List<DataDictionaryBankCodeEntity> entities = baseMapper.selectList(new QueryWrapper<>());
        if (!CollectionUtils.isEmpty(entities)) {
            return R.ok("查询字典信息成功！")
                    .put("data", entities.stream().collect(Collectors.toMap(DataDictionaryBankCodeEntity::getKeyStr
                            ,DataDictionaryBankCodeEntity::getMapValue)));
        }
        return R.ok("字典信息为空，请联系管理员配置！");
    }
}