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


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
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.constant.SystemFieldConstants;
import com.hzs.common.core.enums.EBusinessModule;
import com.hzs.common.core.enums.EYesNo;
import com.hzs.common.core.service.RedisService;
import com.hzs.common.domain.member.base.CuMember;
import com.hzs.common.domain.system.config.BdTradeWhiteConfig;
import com.hzs.common.domain.system.config.ext.BdTradeWhiteConfigExt;
import com.hzs.member.base.IMemberServiceApi;
import com.hzs.system.config.mapper.BdTradeWhiteConfigMapper;
import com.hzs.system.config.service.IBdTradeWhiteConfigService;
import com.hzs.system.config.vo.BdTradeWhiteConfigVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 交易白名单配置表 服务实现类
 * </p>
 *
 * @author hzs
 * @since 2023-08-18
 */
@Service
public class BdTradeWhiteConfigServiceImpl extends ServiceImpl<BdTradeWhiteConfigMapper, BdTradeWhiteConfig> implements IBdTradeWhiteConfigService {

    @DubboReference
    IMemberServiceApi iMemberServiceApi;


    @Autowired
    private RedisService redisService;

    @Override
    public Integer addTradeWhiteConfig(BdTradeWhiteConfigVO tradeWhiteConfigVO) {
        BdTradeWhiteConfig bdTradeWhiteConfig = BeanUtil.copyProperties(tradeWhiteConfigVO, BdTradeWhiteConfig.class);
        return baseMapper.insert(bdTradeWhiteConfig);
    }


    @Override
    public Integer updateTradeWhiteConfig(BdTradeWhiteConfigVO tradeWhiteConfigVO) {
        BdTradeWhiteConfig bdTradeWhiteConfig = BeanUtil.copyProperties(tradeWhiteConfigVO, BdTradeWhiteConfig.class);
        bdTradeWhiteConfig.setPkModified(tradeWhiteConfigVO.getPkCreator());
        bdTradeWhiteConfig.setModifiedTime(new Date());
        return baseMapper.updateById(bdTradeWhiteConfig);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteTradeWhiteConfig(String pkId, Long pkCreator) {
        List<Integer> pkIdList = Arrays.stream(pkId.split(","))
                .map(Integer::parseInt)
                .collect(Collectors.toList());
        for(Integer id:pkIdList){
            UpdateWrapper<BdTradeWhiteConfig> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set(SystemFieldConstants.DEL_FLAG, EYesNo.NO.getIntValue());
            updateWrapper.set(SystemFieldConstants.PK_MODIFIED, pkCreator);
            updateWrapper.set(SystemFieldConstants.MODIFIED_TIME, new Date());
            updateWrapper.eq(SystemFieldConstants.PK_ID, id);
            baseMapper.update(null,updateWrapper);
        }
        return true;
    }

    @Override
    public Object[] showTradeWhiteConfigList(BdTradeWhiteConfigVO tradeWhiteConfigVO) {
        List<BdTradeWhiteConfigExt> bdTradeWhiteConfigExts = baseMapper.selectTradeWhiteConfigList(tradeWhiteConfigVO);
        List<BdTradeWhiteConfigVO> bdTradeWhiteConfigVOList = BeanUtil.copyToList(bdTradeWhiteConfigExts, BdTradeWhiteConfigVO.class);
        return new Object[]{bdTradeWhiteConfigExts, bdTradeWhiteConfigVOList};
    }


    @Override
    public BdTradeWhiteConfig selectTradeWhiteConfigByCondition(BdTradeWhiteConfigVO tradeWhiteConfigVO) {
        BdTradeWhiteConfig tradeWhiteConfig = null;
        Long pkMember = tradeWhiteConfigVO.getPkMember();
        Integer pkCountry = tradeWhiteConfigVO.getPkCountry();
        Integer withdrawalState = tradeWhiteConfigVO.getWithdrawalState();
        Integer transferState = tradeWhiteConfigVO.getTransferState();
        //提现
        if (null != pkMember && null != withdrawalState) {
            String withdrawalKey = CacheConstants.TRADE_CONFIG_WHITE + ":" + pkCountry + EBusinessModule.WITHDRAWAL.getValue() + ":" + pkMember + withdrawalState;
            tradeWhiteConfig = redisService.getCacheObject(withdrawalKey);
            if (null == tradeWhiteConfig) {
                tradeWhiteConfig = baseMapper.selectTradeWhiteConfigByCondition(tradeWhiteConfigVO);
                if (null != tradeWhiteConfig) {
                    redisService.setCacheObject(withdrawalKey, tradeWhiteConfig);
                }
            }
        }
        //转账
        if (null != pkMember && null != transferState) {
            String transferKey = CacheConstants.TRADE_CONFIG_WHITE + ":" + pkCountry + EBusinessModule.TRANSFER_ACCOUNTS.getValue() + ":" + pkMember + transferState;
            tradeWhiteConfig = redisService.getCacheObject(transferKey);
            if (null == tradeWhiteConfig) {
                tradeWhiteConfig = baseMapper.selectTradeWhiteConfigByCondition(tradeWhiteConfigVO);
                if (null != tradeWhiteConfig) {
                    redisService.setCacheObject(transferKey, tradeWhiteConfig);
                }
            }
        }
        return tradeWhiteConfig;
    }

    @Override
    public BdTradeWhiteConfig selectTradeWhiteConfigByPkMember(Long pkMember) {
        QueryWrapper<BdTradeWhiteConfig> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("PK_MEMBER", pkMember);
        return this.getOne(queryWrapper);

    }

}
