package com.jwipc.finance.logic.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jwipc.api.OaCallBackLogic;
import com.jwipc.api.constant.DeductionStatus;
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.finance.entity.Deduction;
import com.jwipc.finance.service.IDeductionService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * @author shenyang.ou
 * @date 2024/4/1 14:39
 * @desc 扣款审批回调
 */
@Service("DeductionChangeCallBack")
@RequiredArgsConstructor
public class DeductionChangeCallBack implements OaCallBackLogic {
    private final IDeductionService deductionService;
    /**
     * oa回调接口
     *
     * @param workflowId  流程id
     * @param status      状态
     * @param remark      审批意见
     * @param processCode 流程编号
     * @param data1       回传的数据1
     * @param data2       回传数据2
     * @return
     */
    @Override
    public ApiResult processor(String workflowId, OaStatus status, String remark, String processCode, String data1, String data2) {
        final Deduction deduction = deductionService.getOne(
                new LambdaQueryWrapper<Deduction>().eq(Deduction::getOaCode, workflowId));


        if (Objects.isNull(deduction)) {
            return ApiResult.builder().code(RestCode.SAP_ERROR.getCode()).message("未找到匹配的流程数据!").build();
        }
        RestCode code = RestCode.SAP_SUCCESS;
        String mesg = "";
        try {
            if (status.equals(OaStatus.REFUSE)) {
                deduction.setStatus(DeductionStatus.OA_REFUSE.getStatus());
            } else if (status.equals(OaStatus.PASS)){
                deduction.setStatus(DeductionStatus.COMPLATE.getStatus());
                // 使用Jackson库解析JSON字符串
                ObjectMapper mapper = new ObjectMapper();
                JsonNode listNode = mapper.readTree(data2);
                String orderCode = null;
                BigDecimal bd = null;
                // 遍历列表中的每个对象，取出orderCode属性的值
                for (JsonNode node : listNode) {
                     orderCode = node.get("orderCode").asText();
                     bd = BigDecimal.valueOf(Double.parseDouble(node.get("actualAmount").toString()));
                }

                deduction.setErpSyncNo(orderCode);
                deduction.setDntAmount(bd);
            } else if (status.equals(OaStatus.DISCARDED)){
                deduction.setStatus(DeductionStatus.REFUSE.getStatus());
            } else if (status.equals(OaStatus.RUNNING)){
                deduction.setStatus(DeductionStatus.OA_AUDIT.getStatus());
                //审批中也要回传订单号给srm 2024-09-10 需求变更增加，邓京术
                // 使用Jackson库解析JSON字符串
                ObjectMapper mapper = new ObjectMapper();
                JsonNode listNode = mapper.readTree(data2);
                String orderCode = null;
                // 遍历列表中的每个对象，取出orderCode属性的值
                for (JsonNode node : listNode) {
                    orderCode = node.get("orderCode").asText();
                }
                deduction.setErpSyncNo(orderCode);
            }
            deductionService.updateById(deduction);
        } 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.DEDUCTION;
    }
}
