package com.hzs.system.config.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.domain.system.config.BdAwards;
import com.hzs.common.domain.system.config.BdGrade;
import com.hzs.common.domain.system.config.BdTradeConfig;
import com.hzs.system.config.mapper.BdTradeConfigMapper;
import com.hzs.system.config.service.IBdAwardsService;
import com.hzs.system.config.service.IBdGradeService;
import com.hzs.system.config.service.IBdTradeConfigService;
import com.hzs.system.config.vo.BdTradeConfigDetailVO;
import com.hzs.system.config.vo.BdTradeConfigVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 交易配置表 服务实现类
 */
@Slf4j
@Service
public class BdTradeConfigServiceImpl extends ServiceImpl<BdTradeConfigMapper, BdTradeConfig> implements IBdTradeConfigService {

    @Autowired
    IBdAwardsService iBdAwardsService;
    @Autowired
    IBdGradeService iBdGradeService;

    @Override
    public Integer addTradeConfig(BdTradeConfigVO tradeConfigVO) {
        BdTradeConfig bdTradeConfig = conversionTradeConfig(tradeConfigVO);
        bdTradeConfig.setPkCreator(tradeConfigVO.getPkMember());
        return baseMapper.insert(bdTradeConfig);
    }

    @Override
    public Integer updateTradeConfig(BdTradeConfigVO tradeConfigVO) {
        BdTradeConfig bdTradeConfig = conversionTradeConfig(tradeConfigVO);
        bdTradeConfig.setPkModified(tradeConfigVO.getPkMember());
        bdTradeConfig.setModifiedTime(new Date());
        return baseMapper.updateById(bdTradeConfig);
    }

    private BdTradeConfig conversionTradeConfig(BdTradeConfigVO tradeConfigVO) {
        List<Integer> pkAwardsList = tradeConfigVO.getPkAwardsList();
        List<Integer> pkGradeList = tradeConfigVO.getPkGradeList();
        BdTradeConfig bdTradeConfig = BeanUtil.copyProperties(tradeConfigVO, BdTradeConfig.class);
        //转化等级、奖衔
        String pkAwards = StringUtils.join(pkAwardsList, ",");
        bdTradeConfig.setPkAwards(pkAwards);

        if (CollUtil.isNotEmpty(pkGradeList)) {
            String pkGrade = StringUtils.join(pkGradeList, ",");
            bdTradeConfig.setPkGrade(pkGrade);
        }
        return bdTradeConfig;
    }

    @Override
    public Boolean deleteTradeConfig(Integer pkId, Long pkMember) {
        LambdaUpdateWrapper<BdTradeConfig> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(BdTradeConfig::getDelFlag, EYesNo.NO.getIntValue());
        updateWrapper.set(BdTradeConfig::getPkModified, pkMember);
        updateWrapper.set(BdTradeConfig::getModifiedTime, new Date());
        updateWrapper.eq(BdTradeConfig::getPkId, pkId);
        return update(updateWrapper);
    }

    @Override
    public List<BdTradeConfigVO> showTradeConfigList(BdTradeConfigVO tradeConfigVO) {
        List<BdTradeConfigVO> configVOList = new ArrayList<>();
        List<BdTradeConfig> bdTradeConfigs = baseMapper.selectTradeConfigList(tradeConfigVO);
        if (CollUtil.isNotEmpty(bdTradeConfigs)) {
            //根据国家查询配置的奖衔
            List<BdAwards> awardsList = iBdAwardsService.queryAwards(tradeConfigVO.getPkCountry());
            Map<Integer, BdAwards> map = awardsList.stream().collect(Collectors.toMap(BdAwards::getPkId, o -> o));

            //根据国家查询配置的等级
            List<BdGrade> bdGradeList = iBdGradeService.selectGradeAll(tradeConfigVO.getPkCountry());
            Map<Integer, BdGrade> gradeMap = bdGradeList.stream().collect(Collectors.toMap(BdGrade::getPkId, o -> o));

            for (BdTradeConfig traderConfig : bdTradeConfigs) {
                BdTradeConfigVO configVO = BeanUtil.copyProperties(traderConfig, BdTradeConfigVO.class);
                //转化奖衔翻译
                String pkAwardsStr = traderConfig.getPkAwards();
                configVO.setAwardsDetailList(awardsTransaction(pkAwardsStr, map));
                //转化等级翻译
                String pkGradeStr = traderConfig.getPkGrade();
                configVO.setGradeDetailList(gradeTransaction(pkGradeStr, gradeMap));
                configVOList.add(configVO);
            }
        }
        return configVOList;
    }

    /**
     * 奖衔翻译
     *
     * @param pkAwardsStr 奖衔
     * @param map         配置奖衔
     * @return List<BdTradeConfigDetailVO>
     */
    private List<BdTradeConfigDetailVO> awardsTransaction(String pkAwardsStr, Map<Integer, BdAwards> map) {
        List<BdTradeConfigDetailVO> awardsDetailList = new ArrayList<>();
        BdTradeConfigDetailVO detailVO;
        if (StringUtils.isNotEmpty(pkAwardsStr)) {
            String[] pkAwardsList = pkAwardsStr.split(",");
            for (String pkAwards : pkAwardsList) {
                detailVO = new BdTradeConfigDetailVO();
                detailVO.setPkAwardsTrans(map.get(Integer.parseInt(pkAwards)).getPkTransaction());
                awardsDetailList.add(detailVO);
            }
        }
        return awardsDetailList;
    }

    /**
     * 奖衔翻译
     *
     * @param pkGradeStr 等级
     * @param gradeMap   配置等级
     * @return List<BdTradeConfigDetailVO>
     */
    private List<BdTradeConfigDetailVO> gradeTransaction(String pkGradeStr, Map<Integer, BdGrade> gradeMap) {
        List<BdTradeConfigDetailVO> gradeDetailList = new ArrayList<>();
        BdTradeConfigDetailVO detailVO;
        if (StringUtils.isNotEmpty(pkGradeStr)) {
            String[] pkGradeList = pkGradeStr.split(",");
            for (String pkGrade : pkGradeList) {
                detailVO = new BdTradeConfigDetailVO();
                detailVO.setPkGradeTrans(gradeMap.get(Integer.parseInt(pkGrade)).getPkTransaction());
                gradeDetailList.add(detailVO);
            }
        }
        return gradeDetailList;
    }

    @Override
    public BdTradeConfigVO selectTradeConfigDetail(Integer pkId, Integer pkCountry) {
        BdTradeConfigVO bdTradeConfigVO = null;
        BdTradeConfig bdTradeConfig = getById(pkId);
        if (null != bdTradeConfig) {
            bdTradeConfigVO = BeanUtil.copyProperties(bdTradeConfig, BdTradeConfigVO.class);
            //根据国家查询配置的奖衔
            List<BdAwards> awardsList = iBdAwardsService.queryAwards(pkCountry);
            Map<Integer, BdAwards> map = awardsList.stream().collect(Collectors.toMap(BdAwards::getPkId, o -> o));

            //根据国家查询配置的等级
            List<BdGrade> bdGradeList = iBdGradeService.selectGradeAll(pkCountry);
            Map<Integer, BdGrade> gradeMap = bdGradeList.stream().collect(Collectors.toMap(BdGrade::getPkId, o -> o));

            //转化奖衔翻译
            String pkAwardsStr = bdTradeConfig.getPkAwards();
            bdTradeConfigVO.setAwardsDetailList(awardsTransaction(pkAwardsStr, map));
            if (StringUtils.isNotEmpty(pkAwardsStr)) {
                bdTradeConfigVO.setPkAwardsList(Arrays.asList(pkAwardsStr.split(",")).stream().map(Integer::valueOf).collect(Collectors.toList()));
            }
            //转化等级翻译
            String pkGradeStr = bdTradeConfig.getPkGrade();
            bdTradeConfigVO.setGradeDetailList(gradeTransaction(pkGradeStr, gradeMap));
            if (StringUtils.isNotEmpty(pkGradeStr)) {
                bdTradeConfigVO.setPkGradeList(Arrays.asList(pkGradeStr.split(",")).stream().map(Integer::valueOf).collect(Collectors.toList()));
            }
        }
        return bdTradeConfigVO;
    }

    @Override
    public BdTradeConfig selectTradeConfigByCondition(BdTradeConfigVO tradeConfigVO) {
        return baseMapper.selectTradeConfigByCondition(tradeConfigVO);
    }

}
