package com.jwipc.contract.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.contract.constant.ContractConst;
import com.jwipc.contract.entity.Contract;
import com.jwipc.contract.service.IContractService;
import com.jwipc.exception.DaoException;
import com.jwipc.log.annotation.ApiOperateLog;
import com.jwipc.log.constant.ApiDirection;
import com.jwipc.log.constant.ThirdSysType;
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("ContractMroCallBack")
@Slf4j
public class ContractMroCallBackImpl implements OaCallBackLogic {

    @Autowired
    IContractService contractService;


    @Override
//    @ApiOperateLog(code = "contractManageMroCallBack", name = "采购类合同评审审批结果回传", 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:废弃
        Contract contract = contractService.getOne(
                new LambdaQueryWrapper<Contract>().eq(Contract::getOaProcessId, workflowId));
        if (Objects.isNull(contract)) {
            throw new DaoException("未找到匹配流程!");
        }
        if (status.getStatus() == OaStatus.PASS.getStatus()) {
            contract.setContractState(3);
        }
        if (status.getStatus() == OaStatus.REFUSE.getStatus()) {
            contract.setContractState(4);
        }
        if (status.getStatus() == OaStatus.DISCARDED.getStatus()) {
            contract.setContractState(6);
        }

        if (status.getStatus() == OaStatus.RUNNING.getStatus()) {
            //需要json转换
            final JSONArray jsonArray = JSON.parseArray(data2);
            for (Object o : jsonArray) {
                JSONObject jsonObject = (JSONObject) o;
                if (Objects.nonNull(jsonObject.get("upFlag"))){
                    final String upFlag = jsonObject.getString("upFlag");
                    if ("1".equals(upFlag)){
                        contract.setContractSignFileStatue(ContractConst.MANUALING);
                    }
                }
            }

        }
        contractService.updateById(contract);
        log.info("{}", "contractManageMroCallBack");
        log.info("{}", workflowId);
        return ApiResult.builder().code(RestCode.SAP_SUCCESS.getCode()).build();
    }

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

}
