package com.jwipc.contract.logic.impl;

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.ShareStatus;
import com.jwipc.api.dto.ApiResult;
import com.jwipc.constant.ShareProcessType;
import com.jwipc.contract.entity.ContractDelivery;
import com.jwipc.contract.entity.ContractDeliveryDetail;
import com.jwipc.contract.service.IContractDeliveryDetailService;
import com.jwipc.contract.service.IContractDeliveryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 302：收货单资产验收状态反写
 */
@RequiredArgsConstructor
@Service("ContractAcceptanceCallBack")
@Slf4j
public class ContractAcceptanceCallBackImpl implements ShareCallBackLogic {
    @Autowired
    IContractDeliveryService contractDeliveryService;
    @Autowired
    IContractDeliveryDetailService contractDeliveryDetailService;

    @Override
    public ApiResult processor(String workflowId, ShareStatus status, String remark,
                               String processCode, String data1, String jsonData, String data,String data2)
    {
        //收货单资产验收反写
        JSONObject jsonObject = JSONObject.parseObject(data2);
        JSONArray details = jsonObject.getJSONArray("details");
        List<String> itemNoList = new ArrayList<>();
        List<String> itemLineNoList = new ArrayList<>();


        // 遍历JSONArray并提取属性
        for (int i = 0; i < details.size(); i++) {
            JSONObject obj = details.getJSONObject(i);
            itemNoList.add(obj.getString("itemNo"));
            itemLineNoList.add(obj.getString("itemLineNo"));
        }
        List<ContractDeliveryDetail> list = contractDeliveryDetailService.list(new LambdaQueryWrapper<ContractDeliveryDetail>().in(ContractDeliveryDetail::getDeliveryNo, itemNoList).in(ContractDeliveryDetail::getReceiptLineNo, itemLineNoList).eq(ContractDeliveryDetail::getDeleteFlag, 0));
        if(CollectionUtils.isEmpty(list)){
            return ApiResult.builder().code(RestCode.SAP_ERROR.getCode()).message("未找到匹配流程!收货单号:"+itemNoList +"，行号："+ itemLineNoList).build();
        }
        for (ContractDeliveryDetail detail : list) {
            for (int i = 0; i < details.size(); i++) {
                JSONObject obj = details.getJSONObject(i);
                if(detail.getDeliveryNo().equals(obj.getString("itemNo")) &&  detail.getReceiptLineNo().equals(obj.getString("itemLineNo"))){
                    detail.setAcceptanceStatus(obj.getString("itemLineStatus"));//验收单状态
                    detail.setAcceptanceNo(obj.getString("checkNo"));
                }
            }
        }
        contractDeliveryDetailService.updateBatchById(list);
        return ApiResult.builder().code(RestCode.SAP_SUCCESS.getCode()).build();
    }

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

}
