package com.jwipc.shopping.logic.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
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.shopping.constant.ShoppingPayBpmState;
import com.jwipc.shopping.entity.ShoppingPay;
import com.jwipc.shopping.logic.ShoppingPayLogic;
import com.jwipc.shopping.service.IShoppingPayService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @Author: swq
 * @Date: 2024/4/15 9:33
 */
@Service("ShoppingPayCallBack")
@RequiredArgsConstructor
public class ShoppingPayCallBack implements OaCallBackLogic {
    private final ShoppingPayLogic shoppingPayLogic;
    private final IShoppingPayService shoppingPayService;

    @Override
    public ApiResult processor(String workflowId, OaStatus status, String remark, String processCode, String data1,
                               String data2)
    {
        final ShoppingPay shoppingPay = shoppingPayService.getOne(
                new LambdaQueryWrapper<ShoppingPay>().eq(ShoppingPay::getOaProcessId, workflowId));


        if (Objects.isNull(shoppingPay)) {
            return ApiResult.builder().code(RestCode.SAP_ERROR.getCode()).message("未找到匹配的流程数据!").build();
        }
        RestCode code = RestCode.SAP_SUCCESS;
        String mesg = "";
        try {
            if (status.equals(OaStatus.PASS)) {
                shoppingPayLogic.oaExamine(shoppingPay.getId(), ShoppingPayBpmState.PASS.getVal());
            } else if(status.equals(OaStatus.DISCARDED)){
                shoppingPayLogic.oaExamine(shoppingPay.getId(), ShoppingPayBpmState.OBSOLETE.getVal());
            } else {
                shoppingPayLogic.oaExamine(shoppingPay.getId(), ShoppingPayBpmState.REJECT.getVal());

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

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

}
