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

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.CacheConstants;
import com.hzs.common.core.constant.SystemFieldConstants;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EBonusItems;
import com.hzs.common.core.enums.ETransactionKey;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.service.RedisService;
import com.hzs.common.core.utils.ComputeUtil;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.domain.system.config.BdBonusItems;
import com.hzs.common.domain.system.config.ext.BdBonusItemsExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.base.ITransactionServiceApi;
import com.hzs.system.config.mapper.BdBonusItemsMapper;
import com.hzs.system.config.service.IBdBonusItemsService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 奖项配置 服务实现类
 * </p>
 *
 * @author zhangjing
 * @since 2022-11-09
 */
@Service
public class BdBonusItemsServiceImpl extends ServiceImpl<BdBonusItemsMapper, BdBonusItems> implements IBdBonusItemsService {

    @DubboReference
    private ITransactionServiceApi iTransactionServiceApi;

    private RedisService redisService;

    @Autowired
    public void setRedisService(RedisService redisService) {
        this.redisService = redisService;
    }

    /**
     * @description: 生成奖项配置
     * @author: zhang jing
     * @date: 2022/11/9 19:38
     * @param: [bonusItems]
     * @return: boolean
     **/
    @Override
    public boolean generate(BdBonusItems bonusItems) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        for (EBonusItems value : EBonusItems.values()) {
            bonusItems.setBonusName(value.getLabel());
            bonusItems.setBonusValue(value.getValue());
            //字段翻译保存到数据库
            R<Integer> pkTransaction = iTransactionServiceApi.createTransaction(pkCountry, ETransactionKey.BD_BONUS_ITEMS,
                    value.getLabel(), EYesNo.YES, EYesNo.NO);
            bonusItems.setPkTransaction(pkTransaction.getData());
            bonusItems.setPkCountry(pkCountry);
            bonusItems.setPkCreator(SecurityUtils.getUserId());
            bonusItems.setCreationTime(new Date());
            QueryWrapper<BdBonusItems> queryWrapper=new QueryWrapper();
            queryWrapper.eq("BONUS_VALUE",value.getValue());
            queryWrapper.eq("PK_COUNTRY",pkCountry);
            BdBonusItems items= baseMapper.selectOne(queryWrapper);
            if(StringUtils.isNull(items)){
                baseMapper.insert(bonusItems);
                redisService.deleteObject(CacheConstants.BD_BONUS_ITEMS + SecurityUtils.getPkCountry());
            }
        }
        return true;
    }

    /**
     * @description: 根据国家查询奖项配置列表
     * @author: zhang jing
     * @date: 2022/11/10 17:11
     * @param: [bonusItemsExt]
     * @return: java.util.List<com.hzs.common.domain.system.config.ext.BdBonusItemsExt>
     **/
    @Override
    public List<BdBonusItemsExt> queryBonusItems(Integer pkCountry) {
        List<BdBonusItemsExt> bonusItemsExtList = redisService.getCacheList(CacheConstants.BD_BONUS_ITEMS + pkCountry);
        List<BdBonusItemsExt> bonusItemsExtsNew = new ArrayList<>();
        if (CollectionUtil.isEmpty(bonusItemsExtList)) {
            QueryWrapper<BdBonusItems> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("PK_COUNTRY", pkCountry);
            queryWrapper.orderByAsc("BONUS_VALUE");
            List<BdBonusItems> btList = baseMapper.selectList(queryWrapper);
            if (CollectionUtil.isNotEmpty(btList)) {
                btList.forEach(item -> bonusItemsExtsNew.add(BeanUtil.copyProperties(item, BdBonusItemsExt.class)));
                redisService.setCacheList(CacheConstants.BD_BONUS_ITEMS + pkCountry, bonusItemsExtsNew);
            }
            return bonusItemsExtsNew;
        } else {
            return bonusItemsExtList;
        }
    }

    @Override
    public List<BdBonusItemsExt> queryCalBonusItems(Integer pkCountry) {
        List<BdBonusItemsExt> bonusItemsExtList = new ArrayList<>();
        QueryWrapper<BdBonusItems> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_COUNTRY", pkCountry);
        queryWrapper.orderByAsc("BONUS_VALUE");
        List<BdBonusItems> bdBonusItems = baseMapper.selectList(queryWrapper);
        bdBonusItems.forEach(item -> bonusItemsExtList.add(BeanUtil.copyProperties(item, BdBonusItemsExt.class)));
        return bonusItemsExtList;
    }

    @Override
    public List<BdBonusItems> queryBonusItemsByValue(List<Integer> bonusValueList) {
        QueryWrapper<BdBonusItems> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("BONUS_VALUE", bonusValueList);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public BdBonusItems queryBonusItems(Integer pkCountry, Integer pkBonusItems) {
        List<BdBonusItemsExt> bonusItemsExtList = redisService.getCacheList(CacheConstants.BD_BONUS_ITEMS + pkCountry);
        if (bonusItemsExtList != null && bonusItemsExtList.size() > 0) {
            Map<Integer, BdBonusItems> bdBonusItemsMap = new HashMap<>(ComputeUtil.mapInitCapacity(bonusItemsExtList.size()));
            bonusItemsExtList.forEach(bdBonusItemsExt ->
                    bdBonusItemsMap.put(bdBonusItemsExt.getPkId(), bdBonusItemsExt));
            if (bdBonusItemsMap.containsKey(pkBonusItems)) {
                return bdBonusItemsMap.get(pkBonusItems);
            } else {
                return queryBdBonusItems(pkCountry, pkBonusItems);
            }
        }
        return queryBdBonusItems(pkCountry, pkBonusItems);
    }

    @Override
    public BdBonusItems queryBonusItemsByBonusValue(Integer pkCountry, Integer bonusValue) {
        QueryWrapper<BdBonusItems> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_COUNTRY", pkCountry);
        queryWrapper.eq("BONUS_VALUE", bonusValue);
        return baseMapper.selectOne(queryWrapper);
    }

    private BdBonusItems queryBdBonusItems(Integer pkCountry, Integer pkBonusItems) {
        QueryWrapper<BdBonusItems> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_COUNTRY", pkCountry);
        queryWrapper.eq(SystemFieldConstants.PK_ID, pkBonusItems);
        return baseMapper.selectOne(queryWrapper);
    }
}
