package com.crm.pay.controller.pay;

import com.crm.pay.controller.base.BasePayCloseFunctionController;
import com.crm.pay.entity.callback.HuaboCallbackEntity;
import com.crm.pay.service.HuaboService;
import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.model.constant.PayConstants;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.pay.dto.PayDto;
import com.crm.rely.backend.model.entity.custom.info.CustomInfoEntity;
import com.crm.rely.backend.model.entity.finance.deposit.FinanceDepositAddEntity;
import com.crm.pay.entity.FinanceDepositParameterEntity;
import com.crm.rely.backend.core.exception.PayValidatedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * @description:
 * @author: houn
 * @create: 2024-10-14 14:58
 */
@RestController
@RequestMapping("/huabo")
public class HuaboController extends BasePayCloseFunctionController {

    @Autowired
    private HuaboService service;

    /**
     * 支付
     *
     * @param login  入金账号
     * @param amount 入金金额
     * @return 错误信息
     * @throws Exception
     */
    @RequestMapping("/pay/{login}/{amount}")
    public BaseResultDto pay(@PathVariable("login") Long login,
                             @PathVariable("amount") BigDecimal amount,
                             @RequestBody FinanceDepositParameterEntity entity,
                             CustomInfoEntity infoEntity, HttpServletRequest request) throws Exception {


        return payCall(login, amount, 0, entity, infoEntity, request);
    }

    @RequestMapping("/pay/{login}/{amount}/{activity}")
    public BaseResultDto pay(@PathVariable("login") Long login,
                             @PathVariable("amount") BigDecimal amount,
                             @PathVariable("activity") int activity,
                             @RequestBody FinanceDepositParameterEntity entity,
                             CustomInfoEntity infoEntity, HttpServletRequest request) throws Exception {


        return payCall(login, amount, activity, entity, infoEntity, request);
    }


    private BaseResultDto payCall(Long login,
                                  BigDecimal amount,
                                  int activity,
                                  FinanceDepositParameterEntity entity,
                             CustomInfoEntity infoEntity, HttpServletRequest request) throws Exception {
        validated(login, amount);

        FinanceDepositAddEntity financeDepositSaveEntity = getFinanceDepositSaveEntity(login, amount,
                PayConstants.HUABO_PAY_TYPE, activity, entity, request);

        String url = service.pay(financeDepositSaveEntity);
        PayDto payDto = new PayDto(1, url);
        return BaseResultDto.success(payDto);
    }

    /**
     * 异步回调
     *
     * @return 返回成功给第三方系统
     * @throws Exception
     */
    @RequestMapping(value = "/callback", produces = "text/html;charset=UTF-8")
    public synchronized String callback(HttpServletResponse response,
                                        HttpServletRequest request) throws Exception {

//        service.callback(callbackEntity);
        service.callback(getCallback(request));
        return Constants.SUCCESS;
    }


    /**
     * 同步页面 成功
     *
     * @param response 用于重定向页面
     * @throws IOException
     */
    @GetMapping(value = "/pickup")
    public void pickup(HttpServletResponse response) throws Exception, PayValidatedException {

        sendRedirect(response);
    }

    @GetMapping(value = "/error")
    public void error(HttpServletResponse response) throws Exception {

        sendRedirect(response);
    }

    private HuaboCallbackEntity getCallback(HttpServletRequest request) {
        HuaboCallbackEntity callbackEntity = new HuaboCallbackEntity();

//        private String payOrderId;
//        private String mchId;
//        private String appId;
//        private String productId;
//        private String mchOrderNo;
//        private String amount;
//        private String income;
//        private String status;
//        private String paySuccTime;
//        private String backType;
//        private String sign;

        callbackEntity.setPayOrderId(request.getParameter("payOrderId"));
        callbackEntity.setMchId(request.getParameter("mchId"));
        callbackEntity.setAppId(request.getParameter("appId"));
        callbackEntity.setProductId(request.getParameter("productId"));
        callbackEntity.setMchOrderNo(request.getParameter("mchOrderNo"));
        callbackEntity.setAmount(request.getParameter("amount"));
        callbackEntity.setIncome(request.getParameter("income"));
        callbackEntity.setStatus(request.getParameter("status"));
        callbackEntity.setPaySuccTime(request.getParameter("paySuccTime"));
        callbackEntity.setBackType(request.getParameter("backType"));
        callbackEntity.setSign(request.getParameter("sign"));
        return callbackEntity;
    }

}
