package com.jwipc.contract.logic.impl;

import cn.hutool.core.util.ObjectUtil;
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.ShareCallBackLogic;
import com.jwipc.api.constant.RestCode;
import com.jwipc.api.constant.ShareContractStatus;
import com.jwipc.api.constant.ShareStatus;
import com.jwipc.api.dto.ApiResult;
import com.jwipc.log.dto.ShareCallBackDTO;
import com.jwipc.constant.ShareProcessType;
import com.jwipc.contract.constant.ContractConst;
import com.jwipc.contract.entity.Contract;
import com.jwipc.contract.service.IContractService;
import com.jwipc.exception.DaoException;
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: 邓京术
 * @Date: 2025/12/05 0021 16:18
 */
@RequiredArgsConstructor
@Service("ContractMroShareCallBack")
@Slf4j
public class ContractMroShareCallBackImpl implements ShareCallBackLogic {

    @Autowired
    IContractService contractService;


    @Override
    public ApiResult processor(String workflowId, ShareStatus status, String remark, String processCode, String data1,
                               String jsonData, String data, String data2)
    {

        //1、0 | 暂存
        //2、1 | 待提交
        //3、2 | 审批中
        //4、3 | 通过
        //5、4 | 驳回
        //6、7 | 终止
        Contract contract = contractService.getOne(
                new LambdaQueryWrapper<Contract>().eq(Contract::getOaProcessId, workflowId));
        if (Objects.isNull(contract)) {
            return ApiResult.builder().code(RestCode.SAP_ERROR.getCode()).message("未找到匹配流程").build();
        }
        if (status.getStatus() == ShareContractStatus.PASS.getStatus()) {
            contract.setContractState(3);
        }
        if (status.getStatus() == ShareContractStatus.REFUSE.getStatus()) {
            contract.setContractState(4);
        }


        if (status.getStatus() == ShareContractStatus.RUNNING.getStatus()) {
            ShareCallBackDTO.ItData itData = JSONObject.parseObject(data2, ShareCallBackDTO.ItData.class);
            JSONObject jsonObj = itData.getExtension();
            Object extension1 = jsonObj.get("extension1");
            if(ObjectUtil.isEmpty(extension1)){
                return ApiResult.builder().code(RestCode.SAP_ERROR.getCode()).message("状态值不能为空!").build();
            }
            final String upFlag = (String)extension1;
            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 ShareProcessType getProcessType() {
        return ShareProcessType.CONTRACT_STAMP;
    }

}
