package com.jwipc.contract.logic.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSON;
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.ShareStatus;
import com.jwipc.api.dto.ApiResult;
import com.jwipc.log.dto.ShareCallBackDTO;
import com.jwipc.constant.ShareProcessType;
import com.jwipc.contract.entity.Contract;
import com.jwipc.contract.service.IContractDetailService;
import com.jwipc.contract.service.IContractService;
import com.jwipc.exception.DaoException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 功能描述:
 *
 * @Author: 邓京术
 * @Date: 2025/12/05 0021 16:18
 */
@RequiredArgsConstructor
@Service("ContractMuJuShareCallBack")
@Slf4j
public class ContractMuJuShareCallBackImpl implements ShareCallBackLogic {

    @Autowired
    IContractService contractService;

    @Autowired
    IContractDetailService contractDetailService;

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


        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(StringUtils.isEmpty(data2)){
            return ApiResult.builder().code(RestCode.SAP_ERROR.getCode()).message("数据不完整").build();
        }
        ShareCallBackDTO.ItData itData = JSONObject.parseObject(data2, ShareCallBackDTO.ItData.class);
        JSONObject jsonObj = itData.getExtension();
        Object contractCode = jsonObj.get("extension1");
        if(ObjectUtil.isEmpty(contractCode)){
            return ApiResult.builder().code(RestCode.SAP_ERROR.getCode()).message("合同编号不能为空").build();
        }
        Object checkNo = jsonObj.get("extension2");
        if(ObjectUtil.isEmpty(checkNo)){
            return ApiResult.builder().code(RestCode.SAP_ERROR.getCode()).message("验收单号不能为空").build();
        }
        Object checkState = jsonObj.get("extension3");
        if(ObjectUtil.isEmpty(checkState)){
            return ApiResult.builder().code(RestCode.SAP_ERROR.getCode()).message("验收状态不能为空").build();
        }

        Object checkDate = jsonObj.get("extension4");
        if(ObjectUtil.isEmpty(checkDate)){
            return ApiResult.builder().code(RestCode.SAP_ERROR.getCode()).message("验收日期不能为空").build();
        }
        contract.setCheckNo((String)checkNo);
        contract.setCheckState((String)checkState);
        LocalDate date = LocalDate.parse((String)checkDate);
        LocalDateTime dateTime = date.atStartOfDay();
        contract.setCheckDate(dateTime);
        contractService.updateById(contract);
        log.info("{}", workflowId);
        return ApiResult.builder().code(RestCode.SAP_SUCCESS.getCode()).build();
    }

    @Override
    public ShareProcessType getProcessType() {
        return ShareProcessType.CONTRACT_MU_JU;
    }

}
