package com.hfkj.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.channel.tianque.service.TianQueFundManageService;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.DateUtil;
import com.hfkj.common.utils.RedisUtil;
import com.hfkj.dao.BsTransferAccountsMapper;
import com.hfkj.entity.BsMerPlatformNo;
import com.hfkj.entity.BsTransferAccounts;
import com.hfkj.entity.BsTransferAccountsExample;
import com.hfkj.openapi.v1.utils.SignatureUtil;
import com.hfkj.service.BsMerKeyService;
import com.hfkj.service.BsMerPlatformNoService;
import com.hfkj.service.BsTransferAccountsService;
import com.hfkj.sysenum.MerPlatformNoStatusEnum;
import com.hfkj.sysenum.PlatformTypeEnum;
import com.hfkj.sysenum.TransferAccountsCreateType;
import com.hfkj.sysenum.TransferAccountsStatus;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className: BsTransferAccountsServiceImpl
 * @author: HuRui
 * @date: 2023/10/23
 **/
@Service("transferAccountsService")
public class BsTransferAccountsServiceImpl implements BsTransferAccountsService {

    @Resource
    private BsTransferAccountsMapper transferAccountsMapper;
    @Autowired
    private RedisUtil redisUtil;

    @Resource
    private TianQueFundManageService tianQueFundManageService;
    @Resource
    private BsTransferAccountsService transferAccountsService;
    @Resource
    private BsMerPlatformNoService merPlatformNoService;

    // 缓存目录
    private final static String TRANSFER_ACCOUNTS = "TRANSFER_ACCOUNTS:";
    // 缓存时间  1小时
    private final static long cacheTime = 60 * 60;
    @Override
    public void editData(BsTransferAccounts transferAccounts) {
        if (transferAccounts.getId() == null) {
            transferAccounts.setCreateTime(new Date());
            transferAccounts.setUpdateTime(new Date());
            transferAccountsMapper.insert(transferAccounts);
        } else {
            transferAccounts.setUpdateTime(new Date());
            transferAccountsMapper.updateByPrimaryKey(transferAccounts);
        }
        // 缓存
        redisUtil.set(TRANSFER_ACCOUNTS + transferAccounts.getOrderNo(), transferAccounts, cacheTime);
    }

    @Override
    public BsTransferAccounts transfer(BsTransferAccounts transferAccounts) {
            // 查询渠道
            BsMerPlatformNo platform = merPlatformNoService.getPlatformNo(transferAccounts.getMerId(), PlatformTypeEnum.type5);
            if (platform == null) {
                throw ErrorHelp.genException(SysCode.OpenApi, ErrorCode.OPEN_API_COMMON, "未开通权限");
            }
            if (!platform.getStatus().equals(MerPlatformNoStatusEnum.status1.getNumber())) {
                throw ErrorHelp.genException(SysCode.OpenApi, ErrorCode.OPEN_API_COMMON, "未开通权限");
            }
            transferAccounts.setStatus(TransferAccountsStatus.status1.getNumber());
            transferAccounts.setCreateType(TransferAccountsCreateType.type2.getNumber());
            transferAccounts.setReceiverPlatformType(platform.getPlatformType());
            transferAccounts.setReceiverPlatformNo(platform.getPlatformNo());
            transferAccountsService.editData(transferAccounts);

            // 调用渠道转账
            JSONObject orgTransfer = tianQueFundManageService.orgTransfer(
                    transferAccounts.getReceiverPlatformNo(),
                    transferAccounts.getOrderNo(),
                    transferAccounts.getAmount(),
                    transferAccounts.getOrderNo(),
                    "01"
            );
            transferAccounts.setReceiverPlatformNo(orgTransfer.getString("transactionNo"));
            transferAccounts.setSettleDate(DateUtil.format(orgTransfer.getString("clrDt"), "yyyyMMdd"));
            transferAccounts.setTransferTime(DateUtil.format(orgTransfer.getString("transactionTime"), "yyyy-MM-dd HH:mm:ss"));
            transferAccountsService.editData(transferAccounts);
            return transferAccounts;
    }

    @Override
    public List<Map<String, Object>> getTradeRebate(Date startTime, Date endTime) {
        return transferAccountsMapper.queryTradeRebate(
                DateUtil.format(startTime, DateUtil.Y_M_D_HMS),
                DateUtil.format(endTime, DateUtil.Y_M_D_HMS)
        );
    }

    @Override
    public BsTransferAccounts getDetailByOrderNo(String orderNo) {
        Object obj = redisUtil.get(TRANSFER_ACCOUNTS + orderNo);
        if (obj != null) {
            return (BsTransferAccounts) obj;
        }
        BsTransferAccountsExample example = new BsTransferAccountsExample();
        example.createCriteria()
                .andOrderNoEqualTo(orderNo)
                .andStatusNotEqualTo(TransferAccountsStatus.status0.getNumber());
        List<BsTransferAccounts> list = transferAccountsMapper.selectByExample(example);
        if (!list.isEmpty()) {
            BsTransferAccounts transferAccounts = list.get(0);
            // 缓存
            redisUtil.set(TRANSFER_ACCOUNTS + transferAccounts.getOrderNo(), transferAccounts, cacheTime);
            return transferAccounts;
        }
        return null;
    }

    @Override
    public List<BsTransferAccounts> getList(Map<String, Object> param) {
        BsTransferAccountsExample example = new BsTransferAccountsExample();
        example.createCriteria().andStatusNotEqualTo(TransferAccountsStatus.status0.getNumber());

        if (MapUtils.getInteger(param, "status") != null) {
            example.createCriteria().andStatusEqualTo(MapUtils.getInteger(param, "status"));
        }

        example.setOrderByClause("create_time desc");
        return transferAccountsMapper.selectByExample(example);
    }

}
