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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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;

/**
 * 币种配置表 服务实现类
 */
@Service
public class BdAccountServiceImpl extends ServiceImpl<BdAccountMapper, BdAccount> implements IBdAccountService {

    @Autowired
    private RedisService redisService;

    @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;
    }

    @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.eq(BdAccount::getShowStatus, EShowStatus.SHOW.getValue());
        queryWrapper.le(BdAccount::getEffectiveDate, new Date());
        queryWrapper.orderByDesc(BdAccount::getEffectiveDate);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<BdAccount> queryAccountByType(Integer accountType, Collection<Integer> pkCountryList) {
        QueryWrapper<BdAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("field_value", accountType);
        queryWrapper.in(SystemFieldConstants.PK_COUNTRY, pkCountryList);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public List<BdAccount> memberAccountByType(Integer pkCountry) {
        QueryWrapper<BdAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("SHOW_STATUS", EShowStatus.SHOW.getValue());
        queryWrapper.eq("PK_COUNTRY", pkCountry);
        queryWrapper.le("EFFECTIVE_DATE", new Date());
        queryWrapper.orderByDesc("EFFECTIVE_DATE");
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public BdAccount getBdAccount(Integer pkId) {
        BdAccount account = redisService.getCacheObject(CacheConstants.BD_ACCOUNT + pkId);
        if (account == null) {
            QueryWrapper<BdAccount> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("PK_ID", pkId);
            queryWrapper.le("EFFECTIVE_DATE", DateUtils.currentDateTime());
            account = baseMapper.selectOne(queryWrapper);
            if (account != null) {
                redisService.setCacheObject(CacheConstants.BD_ACCOUNT + pkId, account);
            }
        }
        return account;
    }

    @Override
    public BdAccount getBdAccountByFieldValue(Integer pkCountry, Integer fieldValue) {
        QueryWrapper<BdAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("FIELD_VALUE", fieldValue);
        queryWrapper.eq(SystemFieldConstants.PK_COUNTRY, pkCountry);
        queryWrapper.le("EFFECTIVE_DATE", DateUtils.currentDateTime());
        return baseMapper.selectOne(queryWrapper);
    }

    @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);
    }

    @Override
    public Integer selectMemberAccountCount(BdAccountExt bdAccountExt) {
        return baseMapper.selectMemberAccountCount(bdAccountExt);
    }

    @Override
    public BdAccount queryAccountByCondition(Integer pkCountry) {
        QueryWrapper<BdAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SystemFieldConstants.PK_COUNTRY, pkCountry);
        queryWrapper.eq("ON_LINE_RECHARGE", EYesNo.YES.getIntValue());
        List<BdAccount> bdAccountList = baseMapper.selectList(queryWrapper);
        if (bdAccountList != null && bdAccountList.size() > 0) {
            return bdAccountList.get(0);
        }
        return null;
    }

    @Override
    public Integer getPkAccount(Integer pkId) {
        Integer pkAccount = redisService.getCacheObject(CacheConstants.BD_ACCOUNT_CHANGE + pkId);
        if (StringUtils.isNull(pkAccount)) {
            QueryWrapper<BdAccount> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("PK_ID", 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) {
        QueryWrapper<BdAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SystemFieldConstants.PK_COUNTRY, pkCountry);
        queryWrapper.eq("IS_POSTAGE", 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) {
        QueryWrapper<BdAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SystemFieldConstants.ENABLE_STATE, EYesNo.YES.getIntValue());
        queryWrapper.eq(SystemFieldConstants.PK_COUNTRY, pkCountry);
        queryWrapper.le(SystemFieldConstants.EFFECTIVE_DATE, DateUtils.currentDateTime());
        return baseMapper.selectList(queryWrapper);
    }

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

}
