package com.jwipc.contract.logic.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jwipc.api.ShareCallBackLogic;
import com.jwipc.api.constant.RestCode;
import com.jwipc.api.constant.ShareStatus;
import com.jwipc.api.dto.ApiResult;
import com.jwipc.log.dto.ShareCallBackDTO;
import com.jwipc.constant.ShareProcessType;
import com.jwipc.contract.entity.ContractPay;
import com.jwipc.contract.service.IContractPayService;
import com.jwipc.exception.DaoException;
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: 邓京术
 * @Date: 2025/12/05 0021 16:18
 */
@RequiredArgsConstructor
@Service("ContractPayShareCallBack")
@Slf4j
public class ContractPayShareCallBackImpl implements ShareCallBackLogic {

    @Autowired
    IContractPayService payService;


    @Override
    public ApiResult processor(String workflowId, ShareStatus status, String remark, String processCode, String data1,
                               String jsonData, String data, 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() == ShareStatus.PASS.getStatus()) {
            pay.setPayState(2);
            pay.setOaFinishTime(LocalDate.now());
        }
        if (status.getStatus() == ShareStatus.REFUSE.getStatus()) {
            pay.setPayState(3);
        }
        if (status.getStatus() == ShareStatus.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 ShareProcessType getProcessType() {
        return ShareProcessType.CONTRACT_PAYABLE;
    }

}
