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.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.core.constant.CacheConstants;
import com.hzs.common.core.enums.EDelFlag;
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.domain.system.config.BdWithdrawal;
import com.hzs.common.domain.system.config.BdWithdrawalAuto;
import com.hzs.common.domain.system.config.ext.BdWithdrawalAutoExt;
import com.hzs.common.domain.system.config.ext.BdWithdrawalExt;
import com.hzs.common.security.utils.SecurityUtils;
import com.hzs.system.config.mapper.BdWithdrawalMapper;
import com.hzs.system.config.service.IBdWithdrawalAutoService;
import com.hzs.system.config.service.IBdWithdrawalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 * 提现设置主表 服务实现类
 * </p>
 *
 * @author zhangjing
 * @since 2022-08-29
 */
@Service
public class BdWithdrawalServiceImpl extends ServiceImpl<BdWithdrawalMapper, BdWithdrawal> implements IBdWithdrawalService {

    private RedisService redisService;

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




    /**
     * @description: 根据国家查询提现配置列表
     * @author: zhang jing
     * @date: 2022/9/27 15:04
     * @param: [pkCountry]
     * @return: java.util.List<com.hzs.common.domain.system.config.BdWithdrawal>
     **/
    @Override
    public BdWithdrawal queryWithdrawal(Integer pkCountry,Integer pkAccount) {
        BdWithdrawal withdrawal = new BdWithdrawal();
            List<BdWithdrawal> withdrawallist = baseMapper.queryWithdrawal(pkCountry,pkAccount);
            if(CollectionUtil.isNotEmpty(withdrawallist)){
                withdrawal= withdrawallist.get(0);
            }
        return withdrawal;
    }

    /**
     * @description: 查询自动提现的提现周期
     * @author: zhang jing
     * @date: 2022/11/1 19:24
     * @param: [pkCountry]
     * @return: com.hzs.common.domain.system.config.BdWithdrawal
     **/
    @Override
    public List<BdWithdrawal>  queryWithdrawalByIsAuto() {
            QueryWrapper<BdWithdrawal> queryWrapper = new QueryWrapper();
            queryWrapper.eq("IS_AUTO", EYesNo.YES.getIntValue());
            queryWrapper.le("EFFECTIVE_DATE", DateUtils.currentDateTime());
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * @description: 查询会员提价表是否有数据
     * @author: zhang jing
     * @date: 2023/3/16 11:06
     * @param: [withdrawalAutoExt]
     * @return: java.lang.Integer
     **/
    @Override
    public Integer checkWithdraw(BdWithdrawalAutoExt withdrawalAutoExt) {
        return baseMapper.checkWithdraw(withdrawalAutoExt);
    }

    /**
     * @description: 查询提现配置列表
     * @author: zhang jing
     * @date: 2024/6/17 16:27
     * @param: [withdrawalExt]
     * @return: java.util.List<com.hzs.common.domain.system.config.ext.BdWithdrawalExt>
     **/
    @Override
    public List<BdWithdrawalExt> listExt(BdWithdrawalExt withdrawalExt) {
        return baseMapper.listExt(withdrawalExt);
    }


    /**
     * @description: 新增提现配置
     * @author: zhang jing
     * @date: 2022/10/31 15:05
     * @param: [withdrawalExt]
     * @return: boolean
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveWithdrawal(BdWithdrawalExt withdrawalExt) {
        baseMapper.insert(withdrawalExt);
        List<BdWithdrawalAutoExt> withdrawalAutoList=withdrawalExt.getWithdrawalAutoList();
        for(BdWithdrawalAuto wao :withdrawalAutoList){
            wao.setPkWithdrawal(withdrawalExt.getPkId());
            wao.setPkCountry(SecurityUtils.getPkCountry());
            wao.setPkCreator(SecurityUtils.getUserId());
            wao.setCreationTime(new Date());
            withdrawalAutoService.save(wao);
        }
        redisService.deleteObjectByKey(CacheConstants.BD_WITHDRAWAL + SecurityUtils.getPkCountry());
        redisService.deleteObjectByKey(CacheConstants.BD_WITHDRAWAL_IS_AUTO + SecurityUtils.getPkCountry());
        redisService.deleteObjectByKey(CacheConstants.BD_WITHDRAWAL_AUTO + SecurityUtils.getPkCountry());
        return true;
    }

    /**
     * @description: 修改提现配置
     * @author: zhang jing
     * @date: 2022/10/31 15:10
     * @param: [withdrawalExt]
     * @return: boolean
     **/
    @Override
    public boolean updateWithdrawal(BdWithdrawalExt withdrawalExt) {
        baseMapper.updateById(withdrawalExt);
        UpdateWrapper<BdWithdrawalAuto> updateWrapper=new UpdateWrapper();
        updateWrapper.eq("PK_WITHDRAWAL",withdrawalExt.getPkId());
        updateWrapper.eq("DEL_FLAG",EDelFlag.UN_DELETE.getValue());
        updateWrapper.set("DEL_FLAG", EDelFlag.DELETE.getValue());
        withdrawalAutoService.update(updateWrapper);
        List<BdWithdrawalAutoExt> withdrawalAutoList=withdrawalExt.getWithdrawalAutoList();
        for(BdWithdrawalAuto wao :withdrawalAutoList){
            wao.setPkWithdrawal(withdrawalExt.getPkId());
            wao.setPkCountry(SecurityUtils.getPkCountry());
            wao.setPkCreator(SecurityUtils.getUserId());
            wao.setCreationTime(new Date());
            withdrawalAutoService.save(wao);
        }
        redisService.deleteObjectByKey(CacheConstants.BD_WITHDRAWAL + SecurityUtils.getPkCountry());
        redisService.deleteObjectByKey(CacheConstants.BD_WITHDRAWAL_IS_AUTO + SecurityUtils.getPkCountry());
        redisService.deleteObjectByKey(CacheConstants.BD_WITHDRAWAL_AUTO + SecurityUtils.getPkCountry());
        return true;
    }

    /**
     * @description: 查询自动提现明细
     * @author: zhang jing
     * @date: 2022/10/31 14:56
     * @param: [withdrawalAutoExt]
     * @return: java.util.List<com.hzs.common.domain.system.config.ext.BdWithdrawalAutoExt>
     **/
    @Override
    public List<BdWithdrawalAutoExt> getWithdrawalAutoList(BdWithdrawalAutoExt withdrawalAutoExt) {
        return baseMapper.getWithdrawalAutoList(withdrawalAutoExt);
    }


}
