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

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.domain.R;
import com.hzs.common.core.enums.EAwards;
import com.hzs.common.core.enums.ETransactionKey;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.enums.EnableStatus;
import com.hzs.common.core.service.RedisService;
import com.hzs.common.domain.system.config.BdAwards;
import com.hzs.common.domain.system.config.ext.BdAwardsExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.base.ITransactionServiceApi;
import com.hzs.system.config.mapper.BdAwardsMapper;
import com.hzs.system.config.service.IBdAwardsService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 奖衔配置服务实现类
 */
@Service
public class BdAwardsServiceImpl extends ServiceImpl<BdAwardsMapper, BdAwards> implements IBdAwardsService {

    @Autowired
    private RedisService redisService;

    @DubboReference
    ITransactionServiceApi iTransactionServiceApi;

    @Override
    public List<BdAwards> queryAwards(Integer pkCountry) {
        return getBdAwards(pkCountry);
    }

    @Override
    public BdAwards getBdAwardsOne(Integer pkId) {
        QueryWrapper<BdAwards> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_ID", pkId);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 根据国家查询奖衔配置
     **/
    private List<BdAwards> getBdAwards(Integer pkCountry) {
        List<BdAwards> awards = redisService.getCacheList(CacheConstants.BD_AWARDS + pkCountry);
        if (CollectionUtil.isEmpty(awards)) {
            QueryWrapper<BdAwards> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("PK_COUNTRY", pkCountry);
            queryWrapper.eq("ENABLE_STATE", EnableStatus.ENABLE.getValue());
            queryWrapper.orderByAsc("AWARDS_VALUE");
            awards = baseMapper.selectList(queryWrapper);
            if (CollectionUtil.isNotEmpty(awards)) {
                redisService.setCacheListIfPresent(CacheConstants.BD_AWARDS + pkCountry, awards);
            }
        }
        return awards;
    }

    @Override
    public boolean generate(BdAwards awards) {
        Integer pkCountry = SecurityUtils.getPkCountry();
        for (EAwards value : EAwards.values()) {
            awards.setAwardsValue(value.getValue());
            awards.setAwardsName(value.getLabel());
            //字段翻译保存到数据库
            R<Integer> pkTransaction = iTransactionServiceApi.createTransaction(pkCountry, ETransactionKey.AWARDS,
                    value.getLabel(), EYesNo.YES, EYesNo.NO);
            awards.setPkTransaction(pkTransaction.getData());
            awards.setPkCountry(pkCountry);
            awards.setPkCreator(SecurityUtils.getUserId());
            awards.setCreationTime(new Date());
            QueryWrapper<BdAwards> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("AWARDS_VALUE", value.getValue());
            List<BdAwards> list = baseMapper.selectList(queryWrapper);
            if (CollectionUtil.isEmpty(list)) {
                baseMapper.insert(awards);
            }
        }
        redisService.deleteObjectByKey(CacheConstants.BD_AWARDS + pkCountry);
        return true;
    }

    @Override
    public boolean updateAwards(BdAwardsExt awardsExt) {
        for (BdAwards awards : awardsExt.getAwardsList()) {
            baseMapper.updateById(awards);
        }
        return true;
    }

    @Override
    public boolean updateLobalIntegral(BdAwardsExt awardsExt) {
        for (BdAwards awards : awardsExt.getLobalIntegralList()) {
            baseMapper.updateById(awards);
        }
        return true;
    }

    @Override
    public boolean updateGlobalIntegral(BdAwardsExt awardsExt) {
        for (BdAwards awards : awardsExt.getCarAwardPointsList()) {
            baseMapper.updateById(awards);
        }
        return true;
    }

    @Override
    public List<BdAwards> queryAwards() {
        QueryWrapper<BdAwards> queryWrapper = new QueryWrapper<>();
        String querySql = "PK_ID,AWARDS_VALUE,COMMUNITY_CHECK,ALGEBRA,PURCHASE_CHECK,SHARE_CHECK,"
                + "GLOBAL_INTEGRAL,CAR_AWARD_POINTS,PK_CHECK_AWARDS_LEFT,"
                + "PK_CHECK_AWARDS_RIGHT,RELATION_TYPE,COACH_ALGEBRA,COACH_RATIO,ABONUS_RATIO,PK_COUNTRY,"
                + "BLO_COMMUNITY_CHECK,AWARDS_NUM";
        queryWrapper.select(querySql);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public Integer checkGradeAwards(Long pkRegisterGrade, Long pkSettleGrade, Long pkAwards) {
        return baseMapper.checkGradeAwards(pkRegisterGrade, pkSettleGrade, pkAwards);
    }

    @Override
    public List<BdAwards> queryByPkCountryAwards(Integer pkCountry) {
        QueryWrapper<BdAwards> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_COUNTRY", pkCountry);
        queryWrapper.eq("ENABLE_STATE", EnableStatus.ENABLE.getValue());
        queryWrapper.orderByAsc("AWARDS_VALUE");
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<BdAwards> queryAwardsByIdList(List<Integer> countryList) {
        QueryWrapper<BdAwards> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("PK_COUNTRY", countryList);
        return this.list(queryWrapper);
    }

}
