package com.hfkj.openapi.v1.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.entity.BsMerPlatformNo;
import com.hfkj.entity.BsTransferAccounts;
import com.hfkj.openapi.v1.model.request.RequestTransferAccountsModel;
import com.hfkj.openapi.v1.model.response.ResponseTransferAccountsModel;
import com.hfkj.openapi.v1.service.OpenApiTransferAccountsService;
import com.hfkj.openapi.v1.utils.SignatureUtil;
import com.hfkj.service.BsMerKeyService;
import com.hfkj.service.BsMerPlatformNoService;
import com.hfkj.service.BsMerService;
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.springframework.stereotype.Service;

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

/**
 * @className: OpenApiTransferAccountsServiceImpl
 * @author: HuRui
 * @date: 2023/10/23
 **/
@Service("openApiTransferAccountsService")
public class OpenApiTransferAccountsServiceImpl implements OpenApiTransferAccountsService {

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

    @Override
    public Map<String, Object> transfer(BsTransferAccounts transferAccounts) throws Exception {
        // 查询渠道
        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);

        Map<String,Object> map = new HashMap<>();
        map.put("orderNo", transferAccounts.getOrderNo());
        map.put("sign", SignatureUtil.createSign(map, merKeyService.getKeyByMerNo(transferAccounts.getMerNo())));
        return map;
    }

    @Override
    public ResponseTransferAccountsModel queryTransferInfo(String orderNo) throws Exception {
        // 查询转账记录
        BsTransferAccounts transferInfo = transferAccountsService.getDetailByOrderNo(orderNo);
        if (transferInfo == null) {
            throw ErrorHelp.genException(SysCode.OpenApi, ErrorCode.OPEN_API_COMMON, "未找到转账记录");
        }
        ResponseTransferAccountsModel response = new ResponseTransferAccountsModel();
        response.setMerchantNo(transferInfo.getMerNo());
        response.setOrderNo(transferInfo.getOrderNo());
        response.setAmount(transferInfo.getAmount());
        response.setShdFeeAmt(transferInfo.getShdFeeAmt());
        response.setSettleDate(transferInfo.getSettleDate());
        response.setContent(transferInfo.getContent());
        response.setTransferTime(transferInfo.getTransferTime());
        response.setSign(SignatureUtil.createSign(response, merKeyService.getKeyByMerNo(transferInfo.getMerNo())));
        return response;
    }

}
