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.DateUtil;
import com.hfkj.common.utils.IDGenerator;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.BsMer;
import com.hfkj.entity.BsMerLedger;
import com.hfkj.entity.BsTradeOrder;
import com.hfkj.entity.BsTradeOrderProfitSharing;
import com.hfkj.model.ResponseData;
import com.hfkj.openapi.v1.model.request.RequestSeparateModel;
import com.hfkj.openapi.v1.model.request.RequestTradePreorderModel;
import com.hfkj.openapi.v1.service.OpenApiTradeOrderService;
import com.hfkj.openapi.v1.utils.SignatureUtil;
import com.hfkj.service.BsMerKeyService;
import com.hfkj.service.BsMerLedgerService;
import com.hfkj.service.BsMerService;
import com.hfkj.service.BsTradeOrderProfitSharingService;
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.*;

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

/**
 * @className: LedgerController
 * @author: HuRui
 * @date: 2023/5/26
 **/
@Controller
@Api(value = "交易订单")
@RequestMapping(value = "/v1/ledger")
public class LedgerController {

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

    @Resource
    private BsMerKeyService merKeyService;

    @RequestMapping(value="/applyBind",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "分账关系申请")
    public ResponseData applyBind(@Validated @RequestBody RequestTradePreorderModel body) {
        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, "");
            }

            log.info("返回参数：" + JSONObject.toJSONString(null));
            return ResponseMsgUtil.success(null);

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

    @RequestMapping(value="/applyUnBind",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "分账关系解绑申请")
    public ResponseData applyUnBind(@Validated @RequestBody RequestTradePreorderModel body) {
        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, "");
            }

            log.info("返回参数：" + JSONObject.toJSONString(null));
            return ResponseMsgUtil.success(null);

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



}
