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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.*;
import com.hzs.common.core.enums.EAccount;
import com.hzs.common.core.enums.EAccountProperty;
import com.hzs.common.core.enums.EShowStatus;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.service.RedisService;
import com.hzs.common.core.utils.DateUtils;
import com.hzs.common.core.utils.StringUtils;
import com.hzs.common.domain.system.config.BdAccount;
import com.hzs.common.domain.system.config.ext.BdAccountExt;
import com.hzs.common.domain.system.ext.AreaCurrencyExt;
import com.hzs.system.config.mapper.BdAccountMapper;
import com.hzs.system.config.service.IBdAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * 币种配置表 服务实现类
 *
 * @author zhangjing
 * @since 2022-08-25
 */
@Service
public class BdAccountServiceImpl extends ServiceImpl<BdAccountMapper, BdAccount> implements IBdAccountService {

    @Autowired
    private RedisService redisService;

    /**
     * 根据条件查找对应的币种配置
     *
     * @param specialArea 所属专区
     * @param pkCountry   国家
     * @return: List<AreaCurrencyExt>
     * @Author: sui q
     * @Date: 2022/9/6 10:37
     */
    @Override
    public List<AreaCurrencyExt> queryAreaCurrencyByCondition(Integer specialArea, Integer pkCountry) {
        String key = CacheConstants.BD_AREA_CURRENCY + pkCountry + specialArea;
        List<AreaCurrencyExt> areaCurrencyList = redisService.getCacheList(key);
        if (areaCurrencyList == null || areaCurrencyList.size() == 0) {
            areaCurrencyList = baseMapper.queryAreaCurrencyByCondition(specialArea, pkCountry);
            if (areaCurrencyList != null && areaCurrencyList.size() > 0) {
                if (redisService.hasKey(key)) {
                    redisService.deleteObject(key);
                }
                redisService.setCacheList(key, areaCurrencyList);
                redisService.expire(key, Constants.LANG_TIMEOUT, TimeUnit.DAYS);
            }
        }
        return areaCurrencyList;
    }

    /**
     * @description: 根据币种属性查询币种列表（账户类型）
     * @author: zhang jing
     * @date: 2022/9/22 10:00
     * @param: [accountProperty, pkCountry]
     * @return: java.util.List<com.hzs.common.domain.system.config.BdAccount>
     **/
    @Override
    public List<BdAccount> queryAccountByType(Integer accountProperty, Integer pkCountry) {
        LambdaQueryWrapper<BdAccount> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotNull(accountProperty) && accountProperty == EAccountProperty.IS_RECHARGE.getValue()) {
            queryWrapper.eq(BdAccount::getIsRecharge, EYesNo.YES.getIntValue());
        }
        if (StringUtils.isNotNull(accountProperty) && accountProperty == EAccountProperty.IS_TRANSFER.getValue()) {
            queryWrapper.eq(BdAccount::getIsTransfer, EYesNo.YES.getIntValue());
        }
        if (StringUtils.isNotNull(accountProperty) && accountProperty == EAccountProperty.IS_WITHDRAWAL.getValue()) {
            queryWrapper.eq(BdAccount::getIsWithdrawal, EYesNo.YES.getIntValue());
        }
        if (StringUtils.isNotNull(accountProperty) && accountProperty == EAccountProperty.IS_ON_LINE_RECHARGE.getValue()) {
            queryWrapper.eq(BdAccount::getOnLineRecharge, EYesNo.YES.getIntValue());
        }
        queryWrapper.eq(BdAccount::getPkCountry, pkCountry);
        queryWrapper.le(BdAccount::getEffectiveDate, new Date());
        queryWrapper.orderByDesc(BdAccount::getEffectiveDate);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<BdAccount> queryAccountByType(Integer accountType, Collection<Integer> pkCountryList) {
        LambdaQueryWrapper<BdAccount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdAccount::getFieldValue, accountType);
        queryWrapper.in(BdAccount::getPkCountry, pkCountryList);
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * @description: 会员端账户查询
     * @author: zhang jing
     * @date: 2023/4/7 13:44
     * @param: [accountProperty, pkCountry]
     * @return: java.util.List<com.hzs.common.domain.system.config.BdAccount>
     **/
    @Override
    public List<BdAccount> memberAccountByType(Integer pkCountry) {
        LambdaQueryWrapper<BdAccount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdAccount::getShowStatus, EShowStatus.SHOW.getValue());
        queryWrapper.eq(BdAccount::getPkCountry, pkCountry);
        queryWrapper.le(BdAccount::getEffectiveDate, new Date());
        queryWrapper.orderByDesc(BdAccount::getEffectiveDate);
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * @description: 根据pkid查询币种信息
     * @author: zhang jing
     * @date: 2022/10/11 17:49
     * @param: [pkId]
     * @return: com.hzs.common.domain.system.config.BdAccount
     **/
    @Override
    public BdAccount getBdAccount(Integer pkId) {
        BdAccount account = redisService.getCacheObject(CacheConstants.BD_ACCOUNT + pkId);
        if (account == null) {
            LambdaQueryWrapper<BdAccount> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(BdAccount::getPkId, pkId);
            queryWrapper.le(BdAccount::getEffectiveDate, DateUtils.currentDateTime());
            account = baseMapper.selectOne(queryWrapper);
            if (account != null) {
                redisService.setCacheObject(CacheConstants.BD_ACCOUNT + pkId, account);
            }
        }
        return account;
    }

    /**
     * @description: 根据币种值查询币种
     * @author: zhang jing
     * @date: 2023/3/17 11:07
     * @param: [fieldValue]
     * @return: com.hzs.common.domain.system.config.BdAccount
     **/
    @Override
    public BdAccount getBdAccountByFieldValue(Integer pkCountry, Integer fieldValue) {
        LambdaQueryWrapper<BdAccount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdAccount::getFieldValue, fieldValue);
        queryWrapper.eq(BdAccount::getPkCountry, pkCountry);
        queryWrapper.le(BdAccount::getEffectiveDate, DateUtils.currentDateTime());
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * @description: 查询币种配置列表
     * @author: zhang jing
     * @date: 2022/10/25 15:40
     * @param: [bdAccountExt]
     * @return: java.util.List<com.hzs.common.domain.system.config.ext.BdAccountExt>
     **/
    @Override
    public List<BdAccountExt> selectBdAccountList(BdAccountExt bdAccountExt) {
        return baseMapper.selectBdAccountList(bdAccountExt);
    }

    @Override
    public void updateAccountById(Integer pkId) {
        BdAccount bdAccount = baseMapper.selectById(pkId);
        if (bdAccount != null) {
            bdAccount.setEnableState(EYesNo.YES.getIntValue());
        }
        baseMapper.updateById(bdAccount);
    }

    /**
     * @description: 查询所有会员账户是否有值
     * @author: zhang jing
     * @date: 2023/3/13 11:04
     * @param: [bdAccountExt]
     * @return: java.lang.Integer
     **/
    @Override
    public Integer selectMemberAccountCount(BdAccountExt bdAccountExt) {
        return baseMapper.selectMemberAccountCount(bdAccountExt);
    }

    @Override
    public BdAccount queryAccountByCondition(Integer pkCountry) {
        LambdaQueryWrapper<BdAccount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdAccount::getPkCountry, pkCountry);
        queryWrapper.eq(BdAccount::getOnLineRecharge, EYesNo.YES.getIntValue());
        List<BdAccount> bdAccountList = baseMapper.selectList(queryWrapper);
        if (bdAccountList != null && bdAccountList.size() > 0) {
            return bdAccountList.get(0);
        }
        return null;
    }

    /**
     * @description: 查询币种转换
     * @author: zhang jing
     * @date: 2023/3/16 11:45
     * @param: [pkId]
     * @return: java.lang.Integer
     **/
    @Override
    public Integer getPkAccount(Integer pkId) {
        Integer pkAccount = redisService.getCacheObject(CacheConstants.BD_ACCOUNT_CHANGE + pkId);
        if (StringUtils.isNull(pkAccount)) {
            LambdaQueryWrapper<BdAccount> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(BdAccount::getPkId, pkId);
            BdAccount account = baseMapper.selectOne(queryWrapper);
            if (StringUtils.isNotNull(account) && StringUtils.isNotNull(account.getPkBdAccount())) {
                pkAccount = account.getPkBdAccount();
            } else {
                pkAccount = pkId;
            }
            redisService.setCacheObject(CacheConstants.BD_ACCOUNT_CHANGE + pkId, pkAccount);
        }
        return pkAccount;
    }

    @Override
    public BdAccount queryPostageAccount(Integer pkCountry) {
        LambdaQueryWrapper<BdAccount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdAccount::getPkCountry, pkCountry);
        queryWrapper.eq(BdAccount::getIsPostage, EYesNo.YES.getIntValue());
        List<BdAccount> bdAccountList = baseMapper.selectList(queryWrapper);
        if (bdAccountList != null && bdAccountList.size() > 0) {
            return bdAccountList.get(0);
        }
        return null;
    }

    @Override
    public List<BdAccount> queryBdAccount(Integer pkCountry) {
        LambdaQueryWrapper<BdAccount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BdAccount::getEnableState, EYesNo.YES.getIntValue());
        queryWrapper.eq(BdAccount::getPkCountry, pkCountry);
        queryWrapper.le(BdAccount::getEffectiveDate, DateUtils.currentDateTime());
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<BdAccount> queryCommissionAccount(Integer pkCountry) {
        if (CountryConstants.CHINA_COUNTRY.equals(pkCountry)) {
            // 2023.11.08 运营需求，中国手续费为可选消费账户、现金账户、奖金账户
            LambdaQueryWrapper<BdAccount> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(BdAccount::getPkCountry, pkCountry);
            queryWrapper.eq(BdAccount::getEnableState, EYesNo.YES.getIntValue());
            queryWrapper.le(BdAccount::getEffectiveDate, DateUtils.currentDateTime());
            queryWrapper.in(BdAccount::getFieldValue, Arrays.asList(EAccount.CONSUME.getValue(), EAccount.CASH.getValue(), EAccount.BONUS.getValue()));
            queryWrapper.orderByAsc(BdAccount::getFieldValue);
            return baseMapper.selectList(queryWrapper);
        }
        return new ArrayList<>();
    }

}
