package com.jwipc.order.logic.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
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.exception.DaoException;
import com.jwipc.log.annotation.ApiOperateLog;
import com.jwipc.log.constant.ApiDirection;
import com.jwipc.log.constant.ThirdSysType;
import com.jwipc.order.constant.PcnStatus;
import com.jwipc.order.entity.PcnChangeApplyList;
import com.jwipc.order.service.IPcnChangeApplyListService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

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

    @Autowired
    IPcnChangeApplyListService pcnChangeApplyListService;


    @Override
    @ApiOperateLog(code = "pcnManageCallBack", name = "PCN推送审批结果回传", 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:废弃
        PcnChangeApplyList applyList = pcnChangeApplyListService.getOne(
                new LambdaQueryWrapper<PcnChangeApplyList>().eq(PcnChangeApplyList::getOaProcessId, workflowId));
        if (Objects.isNull(applyList)) {
            throw new DaoException("未找到匹配流程!");
        }
        if (status.getStatus() == OaStatus.PASS.getStatus()) {
            applyList.setStatus(PcnStatus.APPROVAL_AGREED.getStatus());
        } else if (status.getStatus() == OaStatus.REFUSE.getStatus()) {
            applyList.setStatus(PcnStatus.APPROVAL_REJECTED.getStatus());
        } else if (status.getStatus() == OaStatus.DISCARDED.getStatus()) {
            applyList.setStatus(PcnStatus.Destroy.getStatus());
        } else if (status.getStatus() == OaStatus.RUNNING.getStatus()) {
            applyList.setStatus(PcnStatus.APPROVAL_IN_PROGRESS.getStatus());
        }
        JSONArray json1 = JSON.parseArray(data2);
        if (Objects.nonNull(json1)) {
            final JSONObject json = (JSONObject) json1.get(0);
            applyList.setChangeNotice(json.getString("ChangeNotice"));
            applyList.setChangeNotifier(json.getString("ChangeNotifier"));
            applyList.setNoticeDate(json.getString("NoticeDate"));
            applyList.setChangeNoticeRemarks(json.getString("ChangeNoticeRemarks"));
        }
        pcnChangeApplyListService.updateById(applyList);
        log.info("{}", "SupplierChangeCallback");
        log.info("{}", workflowId);
        return ApiResult.builder().code(RestCode.SAP_SUCCESS.getCode()).build();
    }

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

}
