package com.sunxung.factoring.web.financing;

import com.sunxung.factoring.component.util.ConstantUtil;
import com.sunxung.factoring.component.util.ThreadLocalUtil;
import com.sunxung.factoring.entity.ResponseJson;
import com.sunxung.factoring.entity.financing.payment.ExemptionApprovalBusiness;
import com.sunxung.factoring.entity.financing.payment.ExemptionApprovalRisk;
import com.sunxung.factoring.entity.financing.payment.PaymentApply;
import com.sunxung.factoring.entity.financing.payment.PaymentApproval;
import com.sunxung.factoring.service.financing.payment.IExemptionApprovalBusinessService;
import com.sunxung.factoring.service.financing.payment.IExemptionApprovalRiskService;
import com.sunxung.factoring.service.financing.payment.IPaymentApplyService;
import com.sunxung.factoring.service.sys.flowable.FlowableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description : 付款申请Controller
 * @Author : L.Jia
 * @Date: 2024-02-28 10:40
 */
@RestController
@RequestMapping("/paymentApply")
public class PaymentApplyController {

    @Autowired
    private IPaymentApplyService applyService;
    @Autowired
    private FlowableService flowableService;
    @Autowired
    private IExemptionApprovalBusinessService exemptionApprovalBusinessService;
    @Autowired
    private IExemptionApprovalRiskService exemptionApprovalRiskService;

    /**
     * 进入付款申请页面
     *
     * @return
     */
    @GetMapping("/edit")
    public Object editApply(Long id) {
        return new ResponseJson(applyService.getToEditPageById(id));
    }

    /**
     * 付款申请提交
     */
    @PostMapping("/doEdit")
    public Object doEdit(@RequestBody PaymentApply paymentApply) {
        applyService.doEdit(paymentApply);
        return new ResponseJson("提交成功！");
    }

    /**
     * 豁免审批（业务）提交
     *
     * @param exemptionApprovalBusiness
     * @return
     */
    @PostMapping("/exemptionBusiness/edit")
    public ResponseJson exemptionBusinessEdit(@RequestBody ExemptionApprovalBusiness exemptionApprovalBusiness) {
        //拾取task
        flowableService.claimTask(exemptionApprovalBusiness.getTaskId(), ThreadLocalUtil.getUserId(), ConstantUtil.PROJECT_MANAGER_OWNER);
        exemptionApprovalBusinessService.edit(exemptionApprovalBusiness);
        return new ResponseJson("审批成功");
    }

    /**
     * 豁免审批（风控）提交
     *
     * @param exemptionApprovalRisk
     * @return
     */
    @PostMapping("/exemptionRisk/edit")
    public ResponseJson exemptionRiskEdit(@RequestBody ExemptionApprovalRisk exemptionApprovalRisk) {
        //拾取task
        flowableService.claimTask(exemptionApprovalRisk.getTaskId(), ThreadLocalUtil.getUserId(), ConstantUtil.RISK_CONTROL_MANAGER);
        exemptionApprovalRiskService.edit(exemptionApprovalRisk);
        return new ResponseJson("审批成功");
    }

    /**
     * 进入付款审核页面
     *
     * @return
     */
    @GetMapping("/editApproval")
    public Object editApproval(Long id) {
        return new ResponseJson(applyService.getToApprovalPageById(id));
    }

    /**
     * 付款审核提交
     *
     * @return
     */
    @PostMapping("/doEditApproval")
    public Object doEditApproval(@RequestBody PaymentApproval paymentApproval) {
        //拾取task
        flowableService.claimTask(paymentApproval.getTaskId(), ThreadLocalUtil.getUserId(), ConstantUtil.LEGAL_SPECIALIST);
        applyService.editApproval(paymentApproval);
        return new ResponseJson("提交成功");
    }
}
