package com.sunxung.factoring.web.salescontractmanagement;

import com.sunxung.factoring.component.annotation.OperationLog;
import com.sunxung.factoring.component.enums.OperationTypeEnum;
import com.sunxung.factoring.component.util.ConstantConversionUtil;
import com.sunxung.factoring.component.util.ConstantUtil;
import com.sunxung.factoring.component.util.ThreadLocalUtil;
import com.sunxung.factoring.component.util.ValidatorUtil;
import com.sunxung.factoring.entity.ResponseJson;
import com.sunxung.factoring.entity.ValidatorEntity;
import com.sunxung.factoring.entity.salescontractmanagement.*;
import com.sunxung.factoring.service.salescontractmanagement.*;
import com.sunxung.factoring.service.sys.flowable.FlowableService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Description : 销售合同变更控制器
 * @Author : daxian
 * @Date: 2022/3/24
 */
@RestController
@RequestMapping("/salesContract/change")
public class SalesContractChangeController {


    @Autowired
    private ISalesContractSupplementTemplateApprovalService supplementTemplateApprovalService;

    @Autowired
    private ISalesContractSupplementApprovalService salesContractSupplementApprovalService;

    @Autowired
    private ISalesContractSupplementTemplateFirstApprovalInfoService salesContractSupplementTemplateFirstApprovalInfoService;

    @Autowired
    private ICSalesSupplementSignConfirmationService salesSupplementSignConfirmationService;

    @Autowired
    private ISalesSupplementSignConfirmationApprovalService salesSupplementSignConfirmationApprovalService;

    @Autowired
    FlowableService flowableService;


    /**
     * 模板初审数据初始化
     *
     * @return
     */
    @GetMapping("/firstTemplate/approval")
    public ResponseJson approval() {
        return new ResponseJson(salesContractSupplementTemplateFirstApprovalInfoService.add());
    }

    /**
     * 模板初审提交接口
     *
     * @param firstApprovalInfo
     * @return
     */
    @PostMapping("/firstTemplate/doApproval")
    public ResponseJson doApproval(@RequestBody @Validated SalesContractSupplementTemplateFirstApprovalInfo firstApprovalInfo) {
        flowableService.claimTask(firstApprovalInfo.getTaskId(), ThreadLocalUtil.getUserId(), ConstantUtil.LEGAL_SPECIALIST);
        salesContractSupplementTemplateFirstApprovalInfoService.doAdd(firstApprovalInfo);
        return new ResponseJson("操作成功！");
    }

    /**
     * 模板复审 提交
     * @param supplementTemplateApproval
     * @return
     */
    @PostMapping("/templateApproval")
    public ResponseJson templateApproval(@RequestBody @Validated SalesContractSupplementTemplateApproval supplementTemplateApproval) {
        flowableService.claimTask(supplementTemplateApproval.getTaskId(), ThreadLocalUtil.getUserId(), ConstantUtil.LEGAL_SPECIALIST);
        supplementTemplateApprovalService.doApproval(supplementTemplateApproval);
        return new ResponseJson("操作成功！");
    }

    /**
     * 审批详情
     * @param id
     * @return
     */
    @GetMapping("/approval")
    public ResponseJson audit(Long id){
        return new ResponseJson(salesContractSupplementApprovalService.approval(id));
    }


    /**
     * 销售合同变更法务审核提交
     *
     * @param salesContractApproval 销售合同审核信息
     * @return
     */
    @PostMapping("/doAudit")
    @OperationLog(operationModule = "销售合同变更", operationType = OperationTypeEnum.INSERT, description = "销售合同变更法务审核提交")
    public Object doAudit(@RequestBody @Validated SalesContractSupplementApproval salesContractApproval) {
        flowableService.claimTask(salesContractApproval.getTaskId(), ThreadLocalUtil.getUserId(), ConstantUtil.LEGAL_SPECIALIST);
        salesContractSupplementApprovalService.doApproval(salesContractApproval);
        return new ResponseJson("审核完成");
    }

    /**
     * 销售合同变更签署审核-详情
     * @param id
     * @return
     */
    @GetMapping("/sign/approval")
    public ResponseJson signApproval(Long id){
        return new ResponseJson(salesSupplementSignConfirmationApprovalService.approval(id));
    }

    /**
     * 销售合同变更签署审核-提交
     * @param salesSignConfirmationApproval
     * @return
     */
    @PostMapping("/sign/doApproval")
    public ResponseJson signDoApproval(@RequestBody SalesSupplementSignConfirmationApproval salesSignConfirmationApproval){
        flowableService.claimTask(salesSignConfirmationApproval.getTaskId(), ThreadLocalUtil.getUserId(), ConstantUtil.LEGAL_SPECIALIST);
        salesSupplementSignConfirmationApprovalService.doApproval(salesSignConfirmationApproval);
        return new ResponseJson("操作成功！");
    }

}
