package com.cf.imes.module.executor.util.fileConversion.admin.api.webcad;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.cf.imes.module.system.api.application.ApplicationApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import java.util.concurrent.Future;

import static com.cf.imes.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.cf.imes.module.executor.enums.ErrorCodeConstants.*;

/**
 * api数据获取
 */
@Slf4j
@Component
public class ApiDataAchieve {

    @Resource
    private ApiDataProduction apiDataProduction;

    @Resource
    private ApplicationApi applicationApi;

    private static final int PARTS_PAGE_MAX = 100;

//    获得token数据
    public String getApiToken(Long organId) {
        JSONObject app = applicationApi.getApplicationByOrganId(organId);
        String appId = app.getString("appId");
        String appSecret = app.getString("appSecret");

        JSONObject json = apiDataProduction.getApiTokenMessage(appId, appSecret);
        if (!json.getString("err_msg").equals("success")) {
            throw exception(APP_TOKEN_ERROR);
        }
        String token = json.getJSONObject("info").getString("access_token");
        return token;
    }

//    获取板件生产数据信息
    @Async
    public Future<JSONObject> getApiPlateProData(String token, String orderNo) {
        JSONObject jsonPlatesData = new JSONObject();
        jsonPlatesData.put("order_no", orderNo);//生产单号需要传入赋值
        jsonPlatesData.put("format", "json");
        JSONObject dataPlates = apiDataProduction.getApiOrderMessage(token, jsonPlatesData);
        if (!dataPlates.getString("err_code").equals("0")) {
            throw exception(PLATE_PLAN_DATA_ERROR);
        }
        return new AsyncResult<>(dataPlates);
    }

//    获取配件信息 分页获取
    @Async
    public Future<JSONObject> getApiPartsMessage(String token, String orderNo) {
        JSONObject jsonParts = new JSONObject();
        jsonParts.put("curr_page", 1);
        jsonParts.put("page_count", PARTS_PAGE_MAX);
        jsonParts.put("order_no", orderNo);
        JSONObject parts = apiDataProduction.getApiOrderPartsMessage(token, jsonParts);
        if (!parts.getString("err_code").equals("0")) {
            throw exception(PARTS_DATA_ERROR);
        }
        JSONObject dataParts = new JSONObject();
        for (int i = 1; i <= parts.getJSONObject("value").getInteger("PageCount"); i++) {
            jsonParts.put("curr_page", i);
            JSONObject value = apiDataProduction.getApiOrderPartsMessage(token, jsonParts);
            dataParts = mergeJSONObjects(dataParts, value);
        }
        return new AsyncResult<>(dataParts);
    }

//    柜体信息转换
    @Async
    public Future<JSONObject> getApiBodyMessage(String token, String orderNo) {
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("curr_page", 1);
        jsonBody.put("page_count", PARTS_PAGE_MAX);
        jsonBody.put("order_no", orderNo);
        JSONObject body = apiDataProduction.getApiOrderBodyMessage(token, jsonBody);
        if (!body.getString("err_code").equals("0")) {
            throw exception(BODY_DATA_ERROR);
        }
        JSONObject dataBody = new JSONObject();
        for (int i = 1; i <= body.getJSONObject("value").getInteger("PageCount"); i++) {
            jsonBody.put("curr_page", i);
            JSONObject value = apiDataProduction.getApiOrderBodyMessage(token, jsonBody);
            dataBody = mergeJSONObjects(dataBody, value);
        }
        return new AsyncResult<>(dataBody);
    }

//    商品信息转换
    @Async
    public Future<JSONObject> getApiGoodsMessage(String token, String orderNo) {
        JSONObject jsonGoods = new JSONObject();
        jsonGoods.put("curr_page", 1);
        jsonGoods.put("page_count", PARTS_PAGE_MAX);
        jsonGoods.put("order_no", orderNo);
        JSONObject goods = apiDataProduction.getApiOrderGoodsMessage(token, jsonGoods);
        if (!goods.getString("err_code").equals("0")) {
            throw exception(GOODS_DATA_ERROR);
        }
        JSONObject dataGoods = new JSONObject();
        for (int i = 1; i <= goods.getJSONObject("value").getInteger("PageCount"); i++) {
            jsonGoods.put("curr_page", i);
            JSONObject value = apiDataProduction.getApiOrderGoodsMessage(token, jsonGoods);
            dataGoods = mergeJSONObjects(dataGoods, value);
        }
        return new AsyncResult<>(dataGoods);
    }

//    板材明细信息转换
    @Async
    public Future<JSONObject> getApiPlateDetailMessage(String token, String orderNo) {
        JSONObject jsonPlates = new JSONObject();
        jsonPlates.put("curr_page", 1);
        jsonPlates.put("page_count", PARTS_PAGE_MAX);
        jsonPlates.put("order_no", orderNo);
        JSONObject plates = apiDataProduction.getApiBlocksDataMessage(token, jsonPlates);
        if (!plates.getString("err_code").equals("0")) {
            throw exception(PLATE_DATA_ERROR);
        }
        JSONObject plate = new JSONObject();
        for (int i = 1; i <= plates.getJSONObject("value").getInteger("PageCount"); i++) {
            jsonPlates.put("curr_page", i);
            JSONObject value = apiDataProduction.getApiBlocksDataMessage(token, jsonPlates);
            plate = mergeJSONObjects(plate, value);
        }
        return new AsyncResult<>(plate);
    }

//    加工组信息转换
    @Async
    public Future<JSONObject> getApiGroupMessage(String token, String orderNo) {
        JSONObject jsonModule = new JSONObject();
        jsonModule.put("order_no", orderNo);
        JSONObject dataModule = apiDataProduction.getApiModuleTypeDataMessage(token, jsonModule);
        return new AsyncResult<>(dataModule);
    }

//    板材数据信息转换
    @Async
    public Future<JSONObject> getApiBlocksMessage(String token, String orderNo) {
        JSONObject jsonBlocks = new JSONObject();
        jsonBlocks.put("curr_page", 1);
        jsonBlocks.put("page_count", PARTS_PAGE_MAX);
        jsonBlocks.put("order_no", orderNo);
        JSONObject blocks = apiDataProduction.getApiBlocksMessage(token, jsonBlocks);
        if (!blocks.getString("err_code").equals("0")) {
            throw exception(PLATE_DATA_ERROR);
        }
        JSONObject block = new JSONObject();
        for (int i = 1; i <= blocks.getJSONObject("value").getInteger("PageCount"); i++) {
            jsonBlocks.put("curr_page", i);
            JSONObject value = apiDataProduction.getApiBlocksMessage(token, jsonBlocks);
            block = mergeJSONObjects(block, value);
        }
        return new AsyncResult<>(block);
    }


    public JSONObject mergeJSONObjects(JSONObject object1, JSONObject object2) {

        // 创建新对象
        JSONObject mergedObj = new JSONObject();
        // 合并 List 属性
        JSONArray mergedList = new JSONArray();
        JSONArray list1 = null;
        if (object1.getJSONObject("value") != null) {
            list1 = object1.getJSONObject("value").getJSONArray("List");
            if (list1 != null) {
                for (int i = 0; i < list1.size(); i++) {
                    mergedList.add(list1.getJSONObject(i));
                }
            }
        }
        if (object1.getJSONArray("List") != null) {
            list1 = object1.getJSONArray("List");
            if (list1 != null) {
                for (int i = 0; i < list1.size(); i++) {
                    mergedList.add(list1.getJSONObject(i));
                }
            }
        }
        JSONArray list2 = object2.getJSONObject("value").getJSONArray("List");
        if (list2 != null) {
            for (int i = 0; i < list2.size(); i++) {
                mergedList.add(list2.getJSONObject(i));
            }
        }

        mergedObj.put("List", mergedList);
//        System.out.println(mergedObj);
        return mergedObj;


    }
}
