package com.api.controller.v1;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.BsMer;
import com.hfkj.entity.BsTransferAccounts;
import com.hfkj.model.ResponseData;
import com.hfkj.openapi.v1.model.request.RequestQueryTransferInfoModel;
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.BsMerService;
import com.hfkj.service.BsTransferAccountsService;
import com.hfkj.sysenum.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;

/**
 * 资金管理
 * @className: FundManageController
 * @author: HuRui
 * @date: 2023/10/23
 **/
@Controller
@Api(value = "交易订单")
@RequestMapping(value = "/v1/fundManage")
public class FundManageController {

    private static Logger log = LoggerFactory.getLogger(FundManageController.class);

    @Resource
    private BsMerService merService;
    @Resource
    private BsMerKeyService merKeyService;
    @Resource
    private BsTransferAccountsService transferAccountsService;
    @Resource
    private OpenApiTransferAccountsService openApiTransferAccountsService;

    @RequestMapping(value="/transfer",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "转账")
    public ResponseData transfer(@Validated @RequestBody RequestTransferAccountsModel body, HttpServletRequest request) {
        log.info("========= Start 转账接口 Start ===========");
        log.info("请求参数：" + JSONObject.toJSONString(body));
        try {
            // 验证签名
            if (!SignatureUtil.checkSign(body.getSign(), body, merKeyService.getKeyByMerNo(body.getMerchantNo()))) {
                throw ErrorHelp.genException(SysCode.OpenApi, ErrorCode.OPEN_API_SIGN_ERR, "");
            }
            if (transferAccountsService.getDetailByOrderNo(body.getOrderNo()) != null) {
                throw ErrorHelp.genException(SysCode.OpenApi, ErrorCode.OPEN_API_COMMON, "订单号已存在");
            }
            // 查询商户
            BsMer mer = merService.getMer(body.getMerchantNo());
            if (mer == null) {
                throw ErrorHelp.genException(SysCode.OpenApi, ErrorCode.OPEN_API_COMMON, "未知的商户号");
            }
            if (!mer.getMerStatus().equals(MerStatusEnum.status1.getNumber())) {
                throw ErrorHelp.genException(SysCode.OpenApi, ErrorCode.OPEN_API_COMMON, "商户状态异常");
            }

            BsTransferAccounts transferAccounts = new BsTransferAccounts();
            transferAccounts.setMerId(mer.getId());
            transferAccounts.setMerNo(mer.getMerNo());
            transferAccounts.setMerName(mer.getMerName());
            transferAccounts.setMerAbbreviate(mer.getMerAbbreviate());
            transferAccounts.setOrderNo(body.getOrderNo());
            transferAccounts.setAmount(new BigDecimal(body.getAmount()));
            transferAccounts.setContent(body.getContent());
            // 转账
            Map<String, Object> returnParam = openApiTransferAccountsService.transfer(transferAccounts);
            log.info("返回参数：" + JSONObject.toJSONString(returnParam));
            return ResponseMsgUtil.success(returnParam);

        } catch (Exception e) {
            log.info("出现异常：", e);
            return ResponseMsgUtil.exception(e);
        } finally {
            log.info("========= END 转账接口 END ===========");
        }
    }

    @RequestMapping(value="/queryTransferInfo",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "查询结果")
    public ResponseData queryTransferInfo(@Validated @RequestBody RequestQueryTransferInfoModel body, HttpServletRequest request) {
        log.info("========= Start 查询结果 Start ===========");
        log.info("请求参数：" + JSONObject.toJSONString(body));
        try {
            // 验证签名
            if (!SignatureUtil.checkSign(body.getSign(), body, merKeyService.getKeyByMerNo(body.getMerchantNo()))) {
                throw ErrorHelp.genException(SysCode.OpenApi, ErrorCode.OPEN_API_SIGN_ERR, "");
            }
            // 查询详情
            ResponseTransferAccountsModel transferInfo = openApiTransferAccountsService.queryTransferInfo(body.getOrderNo());
            log.info("返回参数：" + JSONObject.toJSONString(transferInfo));
            return ResponseMsgUtil.success(transferInfo);

        } catch (Exception e) {
            log.info("出现异常：", e);
            return ResponseMsgUtil.exception(e);
        } finally {
            log.info("========= END 查询结果 END ===========");
        }
    }

}
