package com.jwipc.contract.logic.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jwipc.api.OaCallBackLogic;
import com.jwipc.constant.OaProcessType;
import com.jwipc.api.constant.OaStatus;
import com.jwipc.api.constant.RestCode;
import com.jwipc.api.dto.ApiResult;
import com.jwipc.contract.entity.ContractPay;
import com.jwipc.contract.service.IContractPayService;
import com.jwipc.exception.DaoException;
import com.jwipc.log.annotation.ApiOperateLog;
import com.jwipc.log.constant.ApiDirection;
import com.jwipc.log.constant.ThirdSysType;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Objects;

/**
 * 功能描述:
 *
 * @Author: zx
 * @Date: 2024/3/21 0021 16:18
 */
@RequiredArgsConstructor
@Service("ContractPayCallBack")
@Slf4j
public class ContractPayCallBackImpl implements OaCallBackLogic {

    @Autowired
    IContractPayService payService;


    @Override
//    @ApiOperateLog(code = "contractPayCallBack", name = "合同付款申请推送审批结果回传", direction = ApiDirection.OTHER_TO_SRM,
//            thirdSysType = ThirdSysType.OA)
    public ApiResult processor(String workflowId, OaStatus status, String remark, String processCode, String data1,
                               String data2)
    {

        //0：同意 1：拒绝  2:审批中  3:废弃
        ContractPay pay = payService.getOne(
                new LambdaQueryWrapper<ContractPay>().eq(ContractPay::getOaProcessId, workflowId));
        if (Objects.isNull(pay)) {
            throw new DaoException("未找到匹配流程!");
        }
        // 0 待提交  1审批中 2 通过 3 驳回
        if (status.getStatus() == OaStatus.PASS.getStatus()) {
            pay.setPayState(2);
            pay.setOaFinishTime(LocalDate.now());
        }
        if (status.getStatus() == OaStatus.REFUSE.getStatus()) {
            pay.setPayState(3);
        }
        if (status.getStatus() == OaStatus.DISCARDED.getStatus()) {
            pay.setPayState(4);
        }
        payService.updateById(pay);
        log.info("{}", "contractPayCallBack");
        log.info("{}", workflowId);
        return ApiResult.builder().code(RestCode.SAP_SUCCESS.getCode()).build();
    }

    @Override
    public OaProcessType getProcessType() {
        return OaProcessType.CONTRACT_PAY_CHANGE;
    }

}
