package com.crm.pay.controller.pay;

import com.crm.pay.controller.base.BasePayCloseFunctionController;
import com.crm.pay.entity.callback.InoutworkCallbackEntity;
import com.crm.pay.service.InoutworkService;
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 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;

/**
 * @program: cwg-my-base
 * @description:
 * @author: houn
 * @create: 2023-01-04 18:58
 */
@RestController
@RequestMapping("/inoutwork")
public class InoutworkController extends BasePayCloseFunctionController {

    @Autowired
    private InoutworkService service;

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

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

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

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

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

        FinanceDepositAddEntity balanceSaveEntity = getFinanceDepositSaveEntity(login, amount, bankCode,
                PayConstants.INOUTWORK_PAY_KEY, activity, entity, request);

        String url = service.pay(balanceSaveEntity);

        PayDto payDto = new PayDto(1, url);
        return BaseResultDto.success(payDto);
    }

    /**
     * 异步回调
     *
     * @param callbackEntity 用于获取回调数据
     * @return 返回成功给第三方系统
     * @throws Exception
     */

    @RequestMapping(value = "/callback", produces = "text/html;charset=UTF-8")
    public String callback(@RequestBody InoutworkCallbackEntity callbackEntity) throws Exception {
        callbackEntity.setBill_status(PayConstants.INOUTWORK_SUCCESS_STATUS);

        service.callback(callbackEntity);

        return PayConstants.INOUTWORK_RESULT_SUCCESS;
    }

    @RequestMapping(value = "/cancel/callback", produces = "text/html;charset=UTF-8")
    public String cancelCallback(@RequestBody InoutworkCallbackEntity callbackEntity) throws Exception {

        service.callback(callbackEntity);

        return PayConstants.INOUTWORK_RESULT_SUCCESS;
    }

    /**
     * 同步页面 成功
     *
     * @param response 用于重定向页面
     * @throws IOException
     */
    @RequestMapping(value = "/pickup", produces = "text/html;charset=UTF-8")
    public String pickup(HttpServletResponse response) throws Exception {
        sendRedirect(response);
        return PayConstants.INOUTWORK_RESULT_SUCCESS;
    }

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

        sendRedirect(response);
    }
}
