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

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.ArrayList;
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, EnableStatus.ENABLE.getValue());
    }

    @Override
    public BdAwards getBdAwardsOne(Integer pkId) {
        LambdaQueryWrapper<BdAwards> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdAwards::getPkId, pkId);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public List<BdAwards> getBdAwards(Integer pkCountry, Integer enableState) {
        LambdaQueryWrapper<BdAwards> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdAwards::getPkCountry, pkCountry);
        if (null != enableState) {
            queryWrapper.eq(BdAwards::getEnableState, enableState);
        }
        queryWrapper.orderByAsc(BdAwards::getAwardsValue);
        return baseMapper.selectList(queryWrapper);
    }

    @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());
            LambdaQueryWrapper<BdAwards> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(BdAwards::getAwardsValue, 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() {
        LambdaQueryWrapper<BdAwards> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdAwards::getSystemType, 0);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<BdAwards> queryRetailAwards() {
        LambdaQueryWrapper<BdAwards> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdAwards::getSystemType, 2);
        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) {
        LambdaQueryWrapper<BdAwards> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdAwards::getPkCountry, pkCountry);
        queryWrapper.eq(BdAwards::getEnableState, EnableStatus.ENABLE.getValue());
        queryWrapper.orderByAsc(BdAwards::getAwardsValue);
        return baseMapper.selectList(queryWrapper);
    }
}
