package com.yem.or.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yem.em.utils.SubRelationUtils;
import com.yem.or.utils.OrderUtils;
import com.yem.wm.utils.DynamicObjectUtil;
import com.yem.wm.utils.YEM;
import kd.bos.algo.DataSet;
import kd.bos.algo.Row;
import kd.bos.algo.sql.tree.OrderBy;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.entity.OrmLocaleValue;
import kd.bos.openapi.common.custom.annotation.ApiController;
import kd.bos.openapi.common.custom.annotation.ApiParam;
import kd.bos.openapi.common.custom.annotation.ApiPostMapping;
import kd.bos.openapi.common.result.CustomApiResult;
import kd.bos.orm.ORM;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author zhouc
 * @date 2024/3/21 16:52
 * @className AssepcDynamic
 * @description 查询图册查询
 */
@ApiController(value = "AssepcDynamic", desc = "获取资源号数结构部件")
public class AssepcDynamic {

    /**
     * 获取一级部件
     *
     * @param number
     * @param Type
     * @return
     * @throws IOException
     */
    @ApiPostMapping("/assepcdynamic/treeview")
    public CustomApiResult<Object> quetreeview(@NotNull @ApiParam("参数") String number, @NotNull @ApiParam("参数") String Type) throws IOException {
        String msg = "";
        JSONObject jsonObject = OrderUtils.QueryTreeview(number, Type);
        try {
            String status = jsonObject.getString("status");
            if (status.equals("error")) {
                msg = jsonObject.getString("mag");
                CustomApiResult<Object> fail = CustomApiResult.fail("501", msg);
                return fail;
            }
        } catch (Exception e) {
            CustomApiResult<Object> fail = CustomApiResult.fail("501", e.getMessage());
            return fail;
        }
        CustomApiResult<Object> ces = CustomApiResult.success(jsonObject);
        return ces;
    }

    /**
     * 获取下级节点
     *
     * @param number 部件编码
     * @param Type   类型
     * @return
     * @throws IOException
     */
    @ApiPostMapping("/assepcdynamic/queatlas")
    public CustomApiResult<Object> queatlas(@NotNull @ApiParam("部件编码") String number,
                                            @NotNull @ApiParam("查询类型：整机（comp）、资源（assets）") String Type,
                                            @ApiParam("整机编码") String framenumber) throws IOException {
        String msg = "";
        JSONObject jsonObject = OrderUtils.QueryGetsub(number, Type, framenumber);
        try {
            String status = jsonObject.getString("status");
            if (status.equals("error")) {
                msg = jsonObject.getString("mag");
                CustomApiResult<Object> fail = CustomApiResult.fail("501", msg);
                return fail;
            }
        } catch (Exception e) {
            return CustomApiResult.fail("501", e.getMessage());
        }
        return CustomApiResult.success(jsonObject);
    }

    /**
     * 根据客户查询整机合同号
     *
     * @param number 客户编码
     * @return
     * @throws IOException
     */
    @ApiPostMapping("/assepcdynamic/qurtbillno")
    public CustomApiResult<Object> qurtbillno(@NotNull @ApiParam("客户编码") String number) throws IOException {
        JSONObject object = new JSONObject();
        try {
            JSONArray jsonArray = new JSONArray();
            QFilter qFilter = new QFilter("createorg.number", QCP.equals, "110");
            qFilter.and("status", QCP.equals, "C");
            qFilter.and("yem_customer.number", QCP.equals, number);
//            qFilter.and("yem_billtype.number", QCP.equals, "yem_es_salesorder_ZJ");
            String selectfields = DynamicObjectUtil.getSelectfields("yem_em_devicefiles", false);
            DynamicObjectCollection dynamicObjects = QueryServiceHelper.query("yem_em_devicefiles", selectfields
                    , new QFilter[]{qFilter});
            if (dynamicObjects.size() > 0) {
                List<String> list = new ArrayList<>();
                for (DynamicObject dynamicObject : dynamicObjects) {
                    String billno = dynamicObject.getString("yem_salesorderno");
                    list.add(billno);
                }
                String[] stringArray = list.toArray(new String[0]);
                Set<String> strings = removeDuplicates(stringArray);
                for (String s : strings) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("billno", s);
                    jsonArray.add(jsonObject);
                }
            }
            object.put("status", "success");
            object.put("mag", "");
            object.put("list", jsonArray);
        } catch (Exception e) {
            CustomApiResult<Object> fail = CustomApiResult.fail("501", e.getMessage());
            return fail;
        }
        CustomApiResult<Object> ces = CustomApiResult.success(object);
        return ces;
    }

    public static Set<String> removeDuplicates(String[] strings) {
        Set<String> set = new LinkedHashSet<>();
        for (String s : strings) {
            set.add(s); // 如果字符串不在集合中，它将被添加；如果已存在，则不会添加
        }
        return set;
    }

    /**
     * 资源信息查询
     *
     * @param number
     * @param searchap
     * @return
     * @throws IOException
     */
    @ApiPostMapping("/assepcdynamic/modelquery")
    public CustomApiResult<Object> modelquery(
            @NotNull @ApiParam("订单号(number)、产品机型(number)") String number,
            @NotNull @ApiParam("searchap3（订单号）,searchap4（产品机型）") String searchap,
            @ApiParam("客户编码，查询产品机型必传") String custom) throws IOException {
        JSONObject object = new JSONObject();
        try {
            JSONArray jsonArray = new JSONArray();
            if (searchap.equals("searchap3")) {//订单号
                List<QFilter> qFilters = new ArrayList<>();
                String algoKey = this.getClass().getName();
                qFilters.add(new QFilter("yem_salesorderno", QCP.equals, number));
                extracted(algoKey, qFilters, jsonArray);
            } else if (searchap.equals("searchap4")) {//产品机型
                List<QFilter> qFilters = new ArrayList<>();
                String algoKey = this.getClass().getName();
                //查询零件明细表 根据零件编码过滤n
                qFilters.add(new QFilter("yem_model.yem_products.number", QCP.equals, number));
                if (YEM.isNotEmpty(custom)) {
                    qFilters.add(new QFilter("yem_customer.number", QCP.equals, custom));
                } else {
                    return CustomApiResult.fail("501", "客户编码必传");
                }
                extracted(algoKey, qFilters, jsonArray);
                fillMissInfo(algoKey, number, jsonArray);
            }
            object.put("status", "success");
            object.put("mag", "");
            object.put("list", jsonArray);
        } catch (Exception e) {
            return CustomApiResult.fail("501", e.getMessage());
        }
        return CustomApiResult.success(object);
    }

    /**
     * 查询产品资源明细表
     *
     * @param number
     * @param jsonArray
     */
    private void fillMissInfo(String algoKey, String number, JSONArray jsonArray) {
        QFilter qFilter = new QFilter("yem_brandmodel.number", QCP.equals, number);
        String selectField1 = "number, yem_brandmodel.number yem_brandmodel, yem_eqctype.number yem_eqctype";

        DataSet yemEmDevicefiles = ORM.create().queryDataSet(algoKey, "yem_em_assequipment", selectField1, qFilter.toArray());
        DataSet dataSetss = yemEmDevicefiles.distinct();


        if (dataSetss != null) {
            for (Row data : dataSetss) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("yem_salesorderno", "");
                jsonObject.put("yem_dateofpurchase", "");
                jsonObject.put("yem_modelno", data.getString("number")); // 资源号
                jsonObject.put("yem_products", data.getString("yem_brandmodel")); // 产品机型
                jsonObject.put("yem_productsgroup", data.getString("yem_eqctype")); // 产品类型
                jsonArray.add(jsonObject);
            }
        }
    }

    /**
     * 产品机型查询
     *
     * @param algoKey
     * @param qFilters
     * @param jsonArray
     */
    private static void extracted(String algoKey, List<QFilter> qFilters, JSONArray jsonArray) {
        String selectField1 = "yem_model.number yem_modelno,yem_model.yem_products.number yem_products" +
                ",yem_model.yem_productsgroup.name  yem_productsgroup,yem_dateofpurchase,yem_salesorderno";
        DataSet yemEmDevicefiles = ORM.create().queryDataSet(algoKey, "yem_em_devicefiles", selectField1, qFilters.toArray(new QFilter[]{}));
        DataSet dataSetss = yemEmDevicefiles.distinct();

        if (dataSetss != null) {
            for (Row data : dataSetss) {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("yem_salesorderno", data.getString("yem_salesorderno"));
                jsonObject.put("yem_dateofpurchase", data.getDate("yem_dateofpurchase"));
                jsonObject.put("yem_modelno", data.getString("yem_modelno"));
                jsonObject.put("yem_products", data.getString("yem_products"));
                jsonObject.put("yem_productsgroup", data.getString("yem_productsgroup"));
                jsonArray.add(jsonObject);
            }
        }
    }

    /**
     * 整机信息查询
     *
     * @param salesorderno
     * @param modelnumber
     * @return
     * @throws IOException
     */
    @ApiPostMapping("/assepcdynamic/querymachine")
    public CustomApiResult<Object> querymachine(@NotNull @ApiParam("订单号") String salesorderno, @NotNull @ApiParam("资源号（编码）") String modelnumber) throws IOException {
        JSONObject object = new JSONObject();
        try {
            JSONArray jsonArray = new JSONArray();
            QFilter qFilters = new QFilter("yem_salesorderno", QCP.equals, salesorderno);
            qFilters.and(new QFilter("yem_model.number", QCP.equals, modelnumber));
            DynamicObjectCollection yem_em_devicefiles = QueryServiceHelper.query("yem_em_devicefiles",
                    "id,number,name,yem_framenumber,yem_enginecode,yem_enginemodel,yem_dateofmanufacture"
                    , new QFilter[]{qFilters});
            if (!yem_em_devicefiles.isEmpty()) {
                for (DynamicObject devicefiles : yem_em_devicefiles) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("yem_framenumber", devicefiles.getString("yem_framenumber"));
                    jsonObject.put("yem_enginecode", devicefiles.getString("yem_enginecode"));
                    jsonObject.put("yem_enginemodel", devicefiles.getString("yem_enginemodel"));
                    if (devicefiles.getString("yem_dateofmanufacture") != null) {
                        SimpleDateFormat dt = new SimpleDateFormat("yyyy-MM-dd");
                        jsonObject.put("yem_dateofmanufacture", dt.parse(devicefiles.getString("yem_dateofmanufacture")));
                    } else {
                        jsonObject.put("yem_dateofmanufacture", "");
                    }
                    jsonArray.add(jsonObject);
                }
            }
            object.put("status", "success");
            object.put("mag", "");
            object.put("list", jsonArray);
        } catch (Exception e) {
            return CustomApiResult.fail("501", e.getMessage());
        }
        return CustomApiResult.success(object);
    }

    /**
     * 查询零件部件
     *
     * @param modelnumber
     * @return
     * @throws IOException
     */
    @ApiPostMapping("/assepcdynamic/queryassemblies")
    public CustomApiResult<Object> queryassemblies(@NotNull @ApiParam("部件编码") String modelnumber, @NotNull @ApiParam("客户编码") String clientnumber) throws IOException {
        JSONObject object = new JSONObject();
        try {
            List<QFilter> qFilters = new ArrayList<>();
            String algoKey = this.getClass().getName();
            JSONArray jsonArray = new JSONArray();
            DynamicObject object1 = SubRelationUtils.getfirstNumber(modelnumber);
            DynamicObject getfirstid = SubRelationUtils.getfirstid(modelnumber);
            if (getfirstid != null) {
                qFilters.add(new QFilter("yem_entryentity.yem_asscomponent.number", QCP.equals, getfirstid.getString("number")));
                DataSet dataSet3 = ORM.create().queryDataSet(algoKey, "yem_em_cmmptable", "number", qFilters.toArray(new QFilter[]{}));

                //查询设备档案  与 查询零件明细表DataSet通过资源号编码进行左关联
                String selectField1 = "yem_model.number yem_modelno,yem_model.yem_products.number yem_products" +
                        ",yem_model.yem_productsgroup.name  yem_productsgroup,yem_dateofpurchase,yem_salesorderno,group";
//                DataSet dataSet1 = ORM.create().queryDataSet(algoKey, "yem_em_devicefiles", selectField1, null);
                DataSet dataSet1 = ORM.create().queryDataSet(algoKey, "yem_em_devicefiles", selectField1, new QFilter[]{new QFilter("yem_customer.number", QCP.equals, clientnumber)});
                DataSet dataSetdi11 = dataSet3.leftJoin(dataSet1).on("number", "yem_modelno")
                        .select(new String[]{"number"},
                                new String[]{"yem_modelno", "yem_products", "yem_productsgroup", "yem_dateofpurchase", "yem_salesorderno", "group"})
                        .finish().where("yem_salesorderno is not null");
                DataSet dataSetdis = dataSetdi11.distinct();

                if (dataSetdis != null) {
                    for (Row data : dataSetdis) {
                        JSONObject jsonObject = new JSONObject();
                        DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle("yem_em_electronicgroup", "id,number,yem_model,yem_modelname,yem_modelenname"
                                , new QFilter[]{new QFilter("id", QCP.equals, data.getLong("group"))});
                        if (data.get("yem_salesorderno") != null) {
                            jsonObject.put("yem_salesorderno", data.get("yem_salesorderno"));
                            jsonObject.put("yem_resourceno", data.getString("yem_modelno"));
                            jsonObject.put("yem_products", data.getString("yem_products"));
                            jsonObject.put("yem_productsgroup", data.getString("yem_productsgroup"));
                            if (object1 != null) {
                                jsonObject.put("yem_parentpartcode", object1.getString("number"));
                            } else {
                                jsonObject.put("yem_parentpartcode", "");
                            }
                            jsonObject.put("yem_firstNumber", getfirstid.getString("number"));
                            jsonObject.put("yem_partcode", modelnumber);
                            jsonObject.put("producttypeno", dynamicObject.getString("yem_model"));
                            jsonObject.put("producttypename", dynamicObject.getString("yem_modelname"));
                            jsonObject.put("producttypeenname", dynamicObject.getString("yem_modelenname"));
                            if (data.getString("yem_dateofpurchase") != null) {
                                Date yem_dateofpurchase = formatDate(data.getString("yem_dateofpurchase"), "yyyy-MM-dd");
                                jsonObject.put("yem_dateofpurchase", yem_dateofpurchase);
                            }
                            jsonArray.add(jsonObject);
                        }
                    }
                }
            }
            object.put("status", "success");
            object.put("mag", "");
            object.put("list", jsonArray);
        } catch (
                Exception e) {
            return CustomApiResult.

                    fail("501", e.getMessage());
        }
        return CustomApiResult.

                success(object);
    }

    public Date formatDate(String date, String format) {
        SimpleDateFormat dt = new SimpleDateFormat(format);
        try {
            return dt.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 查询颜色
     *
     * @param type     类型
     * @param model    资源号
     * @param complete 整机编码
     * @param pnumber  零件编码
     * @return
     * @throws IOException
     */
    @ApiPostMapping("/assepcdynamic/color")
    public CustomApiResult<Object> querycolor(@NotNull @ApiParam("类型：整机（comp）、资源（assets）") String type,
                                              @NotNull @ApiParam("资源号") String model,
                                              @NotNull @ApiParam("整机编码") String complete,
                                              @NotNull @ApiParam("零件编码") List pnumber) throws IOException {

        JSONObject jsonObject = OrderUtils.getColor(type, model, complete, pnumber);
        String msg = "";
        try {
            String status = jsonObject.getString("status");
            if (status.equals("error")) {
                msg = jsonObject.getString("mag");
                CustomApiResult<Object> fail = CustomApiResult.fail("501", msg);
                return fail;
            }
        } catch (Exception e) {
            return CustomApiResult.fail("501", e.getMessage());
        }
        return CustomApiResult.success(jsonObject);
    }

    /**
     * 查询配件价格库
     *
     * @param product 产品类型
     * @param pnumber 零件编码
     * @return
     * @throws IOException
     */
    @ApiPostMapping("/assepcdynamic/getprice")
    public CustomApiResult<Object> querygetprice(@NotNull @ApiParam("产品类型：装载机（ZZJ）、叉车（CC）、拖拉机（TLJ）") String product,
                                                 @NotNull @ApiParam("零件编码") String pnumber) throws IOException {

        JSONObject jsonObject = OrderUtils.getprice(product, pnumber);
        String msg = "";
        try {
            String status = jsonObject.getString("status");
            if (status.equals("error")) {
                msg = jsonObject.getString("mag");
                CustomApiResult<Object> fail = CustomApiResult.fail("501", msg);
                return fail;
            }
        } catch (Exception e) {
            return CustomApiResult.fail("501", e.getMessage());
        }
        return CustomApiResult.success(jsonObject);
    }

    @ApiPostMapping("/assepcdynamic/electronic_group")
    public CustomApiResult<Object> queryElectronicGroup(@NotNull @ApiParam("客户.编码(116021200012)") String number) {
        List<Map<String, Object>> result = new ArrayList<>();
        QFilter qFilter = new QFilter("yem_customer.number", QCP.equals, number);
        qFilter.and("status", QCP.equals, "C");
        ORM orm = ORM.create();
        DynamicObjectCollection load = orm.query("yem_em_devicefiles", qFilter.toArray());
        for (DynamicObject dynamicObject : load) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", dynamicObject.getPkValue());
            map.put("yem_salesorderno", dynamicObject.getString("yem_salesorderno"));
            map.put("yem_shippingdetailno", dynamicObject.getString("yem_shippingdetailno"));
            DynamicObject createorg = dynamicObject.getDynamicObject("createorg");
            if (createorg != null) {
                map.put("createorg", createorg.getString("name"));
            } else {
                map.put("createorg", "");
            }
            DynamicObject group = dynamicObject.getDynamicObject("group");
            if (group != null) {
                map.put("yem_products_number", group.getString("number"));
                DynamicObject single = BusinessDataServiceHelper.loadSingle(group.getLong("id"), "yem_em_electronicgroup");
                map.put("productnumber", single.getString("yem_model"));
                map.put("productname", single.getString("yem_modelname"));
                map.put("productenname", single.getString("yem_modelenname"));

            } else {
                map.put("productnumber", "");
                map.put("productname", "");
                map.put("productenname", "");
            }
            map.put("number", dynamicObject.getString("number"));
            DynamicObject model = dynamicObject.getDynamicObject("yem_model");
            if (model != null) {
                DynamicObject modelObj = BusinessDataServiceHelper.loadSingle(model.getPkValue(), "yem_bd_basemodelib");
                map.put("yem_modelno", modelObj.getString("number"));
                String zhdescription = modelObj.getString("yem_zhdescription");
                map.put("zhdescription", zhdescription);
                String endescription = modelObj.getString("yem_endescription");
                map.put("endescription", endescription);
            } else {
                map.put("yem_modelno", "");
                map.put("zhdescription", "");
                map.put("endescription", "");
            }
            String enginecode = dynamicObject.getString("yem_enginecode");
            map.put("enginecode", enginecode);
            String enginemodel = dynamicObject.getString("yem_enginemodel");
            map.put("enginemodel", enginemodel);
            String compcode = dynamicObject.getString("yem_compcode");
            map.put("compcode", compcode);
            String compname = dynamicObject.getString("yem_compname");
            map.put("yem_compname", compname);
            Date dateofpurchase = dynamicObject.getDate("yem_dateofpurchase");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            if (dateofpurchase != null) {
                map.put("yem_dateofpurchase", sdf.format(dateofpurchase));
            } else {
                map.put("yem_dateofpurchase", "");
            }
            BigDecimal warrantytime = dynamicObject.getBigDecimal("yem_warrantytime");
            map.put("warrantytime", warrantytime);
            Date warrantyenddate = dynamicObject.getDate("yem_warrantyenddate");
            if (warrantyenddate != null) {
                map.put("warrantyenddate", sdf.format(warrantyenddate));
            } else {
                map.put("warrantyenddate", "");
            }

            result.add(map);
        }
        return CustomApiResult.success(result);
    }

    /**
     * 模糊查询零件信息
     *
     * @param modnumber 模糊数据
     * @return
     */
    @ApiPostMapping("/assepcdynamic/quelikemod")
    public CustomApiResult<Object> quelikemod(@NotNull @ApiParam("零件编码") String modnumber) {
        String algoKey = this.getClass().getName();
        JSONObject object = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        QFilter qFilter = new QFilter("yem_entryentity.yem_partnumber", QCP.like, "%" + modnumber + "%");//
        qFilter.and(new QFilter("status", QCP.equals, "C"));
//        String selectfield = DynamicObjectUtil.getSelectfields("yem_em_assatlasdet", false);
//        String selectfields = DynamicObjectUtil.getEntrySelectfields(selectfield, "yem_em_assatlasdet", "yem_entryentity", false);
        DataSet dataSet1 = ORM.create().queryDataSet(algoKey, "yem_em_assatlasdet", "yem_entryentity.yem_partnumber", new QFilter[]{qFilter});
//        DynamicObjectCollection yem_em_replace = QueryServiceHelper.query("yem_em_assatlasdet",
//                "yem_entryentity.yem_partnumber", new QFilter[]{qFilter}, null);
        DataSet dataSetdis = dataSet1.distinct();
        for (Row data : dataSetdis) {
            JSONObject jsonObject = new JSONObject();
            String yem_partnumber = data.getString("yem_entryentity.yem_partnumber");
            jsonObject.put("yem_partnumber", yem_partnumber);
            jsonArray.add(jsonObject);
        }
        object.put("status", "success");
        object.put("mag", "");
        object.put("line", jsonArray.size());
        object.put("list", jsonArray);
        return CustomApiResult.success(object);
    }

    @ApiPostMapping("/assepcdynamic/colorlibrary")
    public CustomApiResult<Object> colorlibrary(@NotNull @ApiParam("产品类型") String modnumber,
                                                @NotNull @ApiParam("物料编码") String material,
                                                @NotNull @ApiParam("页码") int page,
                                                @NotNull @ApiParam("每一页个数") int number) {
        material = material.trim();
        JSONArray jsonArray = new JSONArray();
        QFilter qFilter = new QFilter("yem_productclass.number", QCP.equals, modnumber + "PJ");
        qFilter.and(new QFilter("yem_accessory.yem_material.number", QCP.equals, material));
        String selectfield = DynamicObjectUtil.getSelectfields("yem_pricelibrary", false);
        String selectfields = DynamicObjectUtil.getEntrySelectfields(selectfield, "yem_pricelibrary", "yem_accessory", false);
        DynamicObject[] loaded = BusinessDataServiceHelper.load("yem_pricelibrary", selectfields, new QFilter[]{qFilter}, "auditdate desc");
        JSONObject object = new JSONObject();
        Map<String, Date> map = new HashMap<>();
        List<String> stringList = new ArrayList<>();
        int i = 0;
        if (loaded.length > 0) {
            for (DynamicObject dynamicObject : loaded) {
                Date yemBizdate = dynamicObject.getDate("auditdate");
                DynamicObjectCollection dynamicObjectCollection = dynamicObject.getDynamicObjectCollection("yem_accessory");
                if (dynamicObjectCollection.size() > 0) {
                    for (DynamicObject collection : dynamicObjectCollection) {
                        DynamicObject yemMaterial = collection.getDynamicObject("yem_material");
                        if (yemMaterial != null) {
                            String yemMaterialString = yemMaterial.getString("number");
                            if (map.get(yemMaterialString) == null) {
                                map.put(yemMaterialString, yemBizdate);
                                JSONObject jsonObject = new JSONObject();
                                int i1 = number * (page - 1);
                                int i2 = number * page;
                                stringList.add(yemMaterialString);
                                String string = "";
                                for (String s : stringList) {
                                    if (s.equals(material)) {
                                        string = s;
                                    }
                                }
                                if (string.equals(yemMaterialString)) {
                                    if (i >= i1 && i < i2) {
                                        jsonObject.put("material", collection.getString("yem_material.number"));
                                        jsonObject.put("materialename", collection.getString("yem_materialename"));
                                        jsonObject.put("markup", collection.getString("yem_markup"));
                                        jsonObject.put("standsalesprice", collection.getBigDecimal("yem_standsalesprice"));
                                        jsonObject.put("remark_a", collection.getString("yem_remark_a"));
                                        jsonObject.put("easentryid", collection.getString("yem_easentryid"));
                                        jsonArray.add(jsonObject);
                                    }
                                    i++;
                                }
//                            jsonObjectS.put("total", i);//总数
                            }
                        }
                    }
                }
            }
//            jsonArray.add(jsonObjectS);
        }
        int i3 = i / number + 1;
        if (i3 == page) {
            object.put("isLastPage", true);
        } else {
            object.put("isLastPage", false);
        }
        object.put("status", "success");
        object.put("mag", "");
        object.put("total", i);//总数
        fillWithMaterialInfo(jsonArray);
        object.put("current", jsonArray.size());
        object.put("list", jsonArray);
        return CustomApiResult.success(object);
    }

    /**
     * 补充物料信息
     *
     * @param jsonArray
     */
    private void fillWithMaterialInfo(JSONArray jsonArray) {
        Set<String> materialNumber = new LinkedHashSet<>();
        for (Object object : jsonArray) {
            JSONObject jsonObject = (JSONObject) object;
            String number = jsonObject.getString("material");
            if (YEM.isNotEmpty(number)) materialNumber.add(number);
        }

        DynamicObjectCollection materials = QueryServiceHelper.query("bd_material", "number, name, yem_nameen, modelnum", new QFilter[]{new QFilter("number", QCP.in, materialNumber)});
        for (Object object : jsonArray) {
            JSONObject jsonObject = (JSONObject) object;
            String material = jsonObject.getString("material");
            for (DynamicObject exist : materials) {
                String number = exist.getString("number");
                if (YEM.isNotEmpty(material) && material.equals(number)) {
                    jsonObject.put("name", exist.get("name")); // 名称
                    jsonObject.put("enname", exist.getString("yem_nameen")); // 英文名称
                    jsonObject.put("specifications", exist.get("modelnum")); // 规格型号
                }
            }
        }
    }
}
