package com.jwipc.tender.logic.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jwipc.api.OaCallBackLogic;
import com.jwipc.api.constant.OaStatus;
import com.jwipc.api.constant.RestCode;
import com.jwipc.api.dto.ApiResult;
import com.jwipc.constant.OaProcessType;
import com.jwipc.tender.entity.QuotaManagement;
import com.jwipc.tender.service.IQuotaManagementService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;


/**
 * @author shenyang.ou
 * @date 2024/4/1 14:39
 * @desc 扣款审批回调
 */
@Service("QuotaPriceCallBack")
@RequiredArgsConstructor
public class QuotaPriceCallBack implements OaCallBackLogic {
    private final IQuotaManagementService quotaManagementService;


    /**
     * oa回调接口
     *
     * @param workflowId  流程id
     * @param status      状态
     * @param remark      审批意见
     * @param processCode 流程编号
     * @param data1       回传的数据1
     * @param data2       回传数据2
     * @return
     */
    @Override
    @Transactional
    public ApiResult processor(String workflowId, OaStatus status, String remark, String processCode, String data1, String data2) {
        List<QuotaManagement> quotaManagementList = quotaManagementService.list(Wrappers.lambdaQuery(QuotaManagement.class).eq(QuotaManagement::getProcessId,workflowId));

        if (CollectionUtil.isEmpty(quotaManagementList)) {
            return ApiResult.builder().code(RestCode.SAP_ERROR.getCode()).message("未找到匹配的流程数据!").build();
        }
        if (quotaManagementList.get(0).getSyncStatus()== 2){
            return ApiResult.builder().code(RestCode.SAP_ERROR.getCode()).message("该流程数据已处理，不可重复提交!").build();
        }
        RestCode code = RestCode.SAP_SUCCESS;
        String mesg = "";

        try {
            for (QuotaManagement quotaManagement:quotaManagementList){
                if (status.equals(OaStatus.REFUSE)) {
                    quotaManagement.setSyncStatus(3);
                } else if (status.equals(OaStatus.PASS)){
                    quotaManagement.setSyncStatus(2);
                }
                else if (status.equals(OaStatus.DISCARDED)){
                    quotaManagement.setSyncStatus(4);
                } else if (status.equals(OaStatus.RUNNING)){
                    quotaManagement.setSyncStatus(1);
                }
                quotaManagementService.updateById(quotaManagement);
            }

        } catch (Exception e) {
            code = RestCode.SAP_ERROR;
            mesg = e.getMessage();
            e.printStackTrace();
        }
        return ApiResult.builder().code(code.getCode()).message(mesg).build();
    }

    /**
     * 当前服务是属于哪个oa流程
     *
     * @return
     */
    @Override
    public OaProcessType getProcessType() {
        return OaProcessType.QUOTA_PRICE;
    }
}
