package com.yem.or.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yem.em.utils.AtlasUtils;
import com.yem.em.utils.SubRelationUtils;
import com.yem.wm.utils.DynamicObjectUtil;
import com.yem.wm.utils.RequestCmmp;
import com.yem.wm.utils.StringUtils;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.tree.TreeNode;
import kd.bos.form.IPageCache;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.sdk.util.KHttpClientUtils;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;

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

/**
 * @Description 下单工具类
 * @Date 2024/2/22 10:45
 * @Created by ZZSLL
 */

public class OrderUtils {
    public static Log log = LogFactory.getLog(OrderUtils.class);

    /**
     * 根据条件查询是否包含
     *
     * @param entryname 单据表示
     * @param filename  查询条件字段
     * @param value     查询条件值
     * @return 返沪动态对象
     */
    public static DynamicObject seelctDyn(String entryname, String filename, Object value) {
        QFilter qFilter = new QFilter(filename, QCP.equals, value);
        DynamicObject Object = BusinessDataServiceHelper.loadSingle(entryname, "id,number,name", qFilter.toArray());
        return Object;
    }

    /**
     * 查询一级部件
     *
     * @param number
     * @param Type
     * @return
     */
    public static JSONObject QueryTreeview(String number, String Type) {
        JSONObject object = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        String yem_replacement = "Par";
        Date yem_completetime = null;
        boolean isComplete = false;
        String assnumber = "";
        if (Type.equals("comp")) {
            QFilter qFilters = new QFilter("yem_framenumber", QCP.equals, number);
            DynamicObjectCollection yem_em_devicefiles = QueryServiceHelper.query("yem_em_devicefiles",
                    "id,number,name,yem_model.number,yem_analysistdate", new QFilter[]{qFilters});
            if (yem_em_devicefiles.size() > 0) {
                String yem_model = yem_em_devicefiles.get(0).getString("yem_model.number");//资源号
                Date yem_analysistdate = yem_em_devicefiles.get(0).getDate("yem_analysistdate");//料单分析时间
                if (yem_analysistdate == null) {
                    object.put("status", "error");
                    object.put("mag", "整机编号【" + number + "】领料分析时间为空，禁止查询图册！");
                    object.put("list", jsonArray);
                    return object;
                } else {
                    isComplete = true;
                    yem_completetime = yem_analysistdate;
                    assnumber = yem_model;
                }
            }
        } else {
            assnumber = number;
        }
        //根据CMMP明细表构建
        QFilter Filter = new QFilter("number", QFilter.equals, assnumber);
        Filter.and(new QFilter("status", QFilter.equals, "C"));
        Filter.and(new QFilter("yem_entryentity.yem_partisreplace", QFilter.equals, false));
        Filter.and(new QFilter("yem_entryentity.yem_assatlasdet.number", QFilter.not_equals, null));
        DynamicObjectCollection yem_em_cmmptable = QueryServiceHelper.query("yem_em_cmmptable",
                "yem_entryentity.yem_assatlasdet.number,yem_entryentity.yem_assatlasdet.name,yem_entryentity.yem_assatlasdet.yem_enname",
                new QFilter[]{Filter}, "yem_entryentity.yem_assatlasdet.number");
        Map map = new HashMap<>();
        if (yem_em_cmmptable.size() > 0) {
            for (int i = 0; i < yem_em_cmmptable.size(); i++) {
                JSONObject jsonObject = new JSONObject();
                DynamicObject dynamicObject = yem_em_cmmptable.get(i);
                String ccode = dynamicObject.getString("yem_entryentity.yem_assatlasdet.number");
                String ccodename = dynamicObject.getString("yem_entryentity.yem_assatlasdet.name");
                String yem_nameen = dynamicObject.getString("yem_entryentity.yem_assatlasdet.yem_enname");
                //查询部件替换关系
                DynamicObject yem_em_substitution = SubRelationUtils.queryAssemblyrep(assnumber, ccode, "Par", isComplete, yem_completetime);
                if (yem_em_substitution != null) {
                    String yemTnumber = yem_em_substitution.getString("yem_entryentity.yem_repnumber");
                    String yemTname = yem_em_substitution.getString("yem_entryentity.yem_repname");
                    String repenname = yem_em_substitution.getString("yem_entryentity.yem_repenname");
                    if (isassat(yemTnumber)) {
                        if (!map.containsKey(yemTnumber)) {
                            map.put(yemTnumber, yemTnumber);
                            jsonObject.put("number", yemTnumber);
                            jsonObject.put("name", yemTname);
                            jsonObject.put("enname", repenname);
                            jsonObject.put("isParent", true);
                            jsonArray.add(jsonObject);
                        }
                    } else {
                        if (!map.containsKey(yemTnumber)) {
                            map.put(yemTnumber, yemTnumber);
                            jsonObject.put("number", yemTnumber);
                            jsonObject.put("name", yemTname);
                            jsonObject.put("enname", repenname);
                            jsonObject.put("isParent", false);
                            jsonArray.add(jsonObject);
                        }
                    }
                } else {
                    //是否替换《总成间零件替换关系》
                    DynamicObject pra = SubRelationUtils.queryassemtopart(assnumber, ccode, "", "pra", yem_replacement, isComplete, yem_completetime);
                    if (pra != null) {
                        String yem_repno = pra.getString("yem_entryentity.yem_repno");
                        String yem_repnoname = pra.getString("yem_entryentity.yem_repnoname");
                        String repenname = yem_em_substitution.getString("yem_entryentity.yem_repenname");
                        if (isassat(yem_repno)) {
                            if (!map.containsKey(yem_repno)) {
                                map.put(yem_repno, yem_repno);
                                jsonObject.put("number", yem_repno);
                                jsonObject.put("name", yem_repnoname);
                                jsonObject.put("enname", repenname);
                                jsonObject.put("isParent", true);
                                jsonArray.add(jsonObject);
                            }

                        } else {
                            if (!map.containsKey(yem_repno)) {
                                map.put(yem_repno, yem_repno);
                                jsonObject.put("number", yem_repno);
                                jsonObject.put("name", yem_repnoname);
                                jsonObject.put("enname", repenname);
                                jsonObject.put("isParent", false);
                                jsonArray.add(jsonObject);
                            }
                        }
                    } else {
                        if (isassat(ccode)) {
                            if (!map.containsKey(ccode)) {
                                map.put(ccode, ccode);
                                jsonObject.put("number", ccode);
                                jsonObject.put("name", ccodename);
                                jsonObject.put("enname", yem_nameen);
                                jsonObject.put("isParent", true);
                                jsonArray.add(jsonObject);
                            }
                        } else {
                            if (!map.containsKey(ccode)) {
                                map.put(ccode, ccode);
                                jsonObject.put("number", ccode);
                                jsonObject.put("name", ccodename);
                                jsonObject.put("enname", yem_nameen);
                                jsonObject.put("isParent", false);
                                jsonArray.add(jsonObject);
                            }

                        }
                    }
                }
            }
        } else {
            object.put("status", "error");
            object.put("mag", "根据编码【" + assnumber + "】未获取到对应的资源号！");
            object.put("list", jsonArray);
            return object;
        }
        if (StringUtils.isNotEmpty(number)) {
            QFilter qFilterdev = new QFilter("yem_framenumber", QCP.equals, number);
            DynamicObject billType = BusinessDataServiceHelper.loadSingle("yem_em_devicefiles",
                    "id,yem_retrofitting,yem_albumassemblyno", qFilterdev.toArray());
            if (billType != null) {
                Map<String, DynamicObject> jzlist = new HashMap<>();
                DynamicObjectCollection yem_retrofitting = billType.getDynamicObjectCollection("yem_retrofitting");
                if (yem_retrofitting.size() > 0) {
                    for (int i = 0; i < yem_retrofitting.size(); i++) {
                        DynamicObject dynamicObject = yem_retrofitting.get(i);
                        DynamicObject yem_albumassemblyno = dynamicObject.getDynamicObject("yem_albumassemblyno");
                        if (yem_albumassemblyno != null) {
                            String number1 = yem_albumassemblyno.getString("number");
                            if (!jzlist.containsKey(number1)) {
                                jzlist.put(number1, yem_albumassemblyno);
                            }
                        }
                    }
                    if (jzlist.size() > 0) {
                        JSONObject jsonObject = new JSONObject();
                        for (String key : jzlist.keySet()) {
                            DynamicObject dynamicObject = jzlist.get(key);
                            jsonObject.put("number", key);
                            jsonObject.put("name", dynamicObject.getString("name"));
                            jsonObject.put("isParent", false);
                            jsonArray.add(jsonObject);
                        }
                    }
                    if (yem_retrofitting.size() > 0 && jzlist.size() != yem_retrofitting.size()) {
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("number", "_123245678910");
                        jsonObject.put("name", "加装明细");
                        jsonObject.put("isParent", false);
                        jsonArray.add(jsonObject);
                    }
                }
            }
        }
        object.put("status", "success");
        object.put("mag", "");
        object.put("list", jsonArray);
        return object;
    }


    public static JSONObject QueryGetsub(String number, String type, String yem_framenumber) {
        JSONObject object = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        String yem_replacement = "Par";
        Date yem_completetime = null;
        boolean isComplete = false;
        String assnumber = "";
        if (type.equals("comp")) {
            //根据整机编码获取领料分析时间
            QFilter qFilters = new QFilter("yem_framenumber", QCP.equals, yem_framenumber);
            DynamicObjectCollection yem_em_devicefiles = QueryServiceHelper.query("yem_em_devicefiles",
                    "id,number,name,yem_model.number,yem_analysistdate", new QFilter[]{qFilters});
            if (yem_em_devicefiles.size() > 0) {
                String yem_model = yem_em_devicefiles.get(0).getString("yem_model.number");//资源号
                Date yem_analysistdate = yem_em_devicefiles.get(0).getDate("yem_analysistdate");//料单分析时间
                if (yem_analysistdate == null) {
                    object.put("status", "error");
                    object.put("mag", "整机编号【" + yem_framenumber + "】领料分析时间为空，禁止查询图册！");
                    object.put("list", jsonArray);
                    return object;
                } else {
                    isComplete = true;
                    yem_completetime = yem_analysistdate;
                }
            }
        }
        QFilter qFilter = new QFilter("number", QCP.equals, number);
        qFilter.and(new QFilter("enable", QCP.equals, "1"));
        qFilter.and(new QFilter("yem_entryentity.yem_iscomp", QCP.equals, true));
        qFilter.and(new QFilter("yem_entryentity.yem_partisreplace", QCP.equals, false));
        DynamicObjectCollection yem_em_assatlasdet = QueryServiceHelper.query("yem_em_assatlasdet",
                "id,yem_entryentity.yem_partnumber,yem_entryentity.yem_partname,yem_entryentity.yem_partenname", new QFilter[]{qFilter});
        if (yem_em_assatlasdet.size() > 0) {
            for (int i = 0; i < yem_em_assatlasdet.size(); i++) {
                JSONObject jsonObject = new JSONObject();
                DynamicObject dynamicObject = yem_em_assatlasdet.get(i);
                String yem_partnumber = dynamicObject.getString("yem_entryentity.yem_partnumber");
                String yem_partname = dynamicObject.getString("yem_entryentity.yem_partname");
                String yem_partenname = dynamicObject.getString("yem_entryentity.yem_partenname");
                TreeNode node = null;
                //查询部件替换关系
                DynamicObject yem_em_substitution = SubRelationUtils.queryAssemblyrep(assnumber, yem_partnumber, "Par", isComplete, yem_completetime);
                if (yem_em_substitution != null) {
                    String yemTnumber = yem_em_substitution.getString("yem_entryentity.yem_repnumber");
                    String yemTname = yem_em_substitution.getString("yem_entryentity.yem_repname");
                    String repenname = yem_em_substitution.getString("yem_entryentity.yem_repenname");
                    if (isassat(yemTnumber)) {
                        jsonObject.put("number", yemTnumber);
                        jsonObject.put("name", yemTname);
                        jsonObject.put("enname", repenname);
                        jsonObject.put("isParent", true);
                        jsonArray.add(jsonObject);
                    } else {
                        jsonObject.put("number", yemTnumber);
                        jsonObject.put("name", yemTname);
                        jsonObject.put("enname", repenname);
                        jsonObject.put("isParent", false);
                        jsonArray.add(jsonObject);
                    }
                } else {
                    //是否替换《总成间零件替换关系》
                    DynamicObject pra = SubRelationUtils.queryassemtopart(assnumber, yem_partnumber, "", "pra", yem_replacement, isComplete, yem_completetime);
                    if (pra != null) {
                        String yem_repno = pra.getString("yem_entryentity.yem_repno");
                        String yem_repnoname = pra.getString("yem_entryentity.yem_repnoname");
                        String repenname = yem_em_substitution.getString("yem_entryentity.yem_repenname");
                        if (isassat(yem_repno)) {
                            jsonObject.put("number", yem_repno);
                            jsonObject.put("name", yem_repnoname);
                            jsonObject.put("enname", repenname);
                            jsonObject.put("isParent", true);
                            jsonArray.add(jsonObject);
                        } else {
                            jsonObject.put("number", yem_repno);
                            jsonObject.put("name", yem_repnoname);
                            jsonObject.put("enname", repenname);
                            jsonObject.put("isParent", false);
                            jsonArray.add(jsonObject);
                        }
                    } else {
                        if (isassat(yem_partnumber)) {
                            jsonObject.put("number", yem_partnumber);
                            jsonObject.put("name", yem_partname);
                            jsonObject.put("enname", yem_partenname);
                            jsonObject.put("isParent", true);
                            jsonArray.add(jsonObject);
                        } else {
                            jsonObject.put("number", yem_partnumber);
                            jsonObject.put("name", yem_partname);
                            jsonObject.put("enname", yem_partenname);
                            jsonObject.put("isParent", false);
                            jsonArray.add(jsonObject);
                        }
                    }
                }
            }
        }
        object.put("status", "success");
        object.put("mag", "");
        object.put("list", jsonArray);
        return object;
    }

    /**
     * 查询颜色公共类
     *
     * @param type     类型
     * @param model    资源号
     * @param complete 整机编码
     * @param pnumber  零件编码
     * @return 颜色结果集
     */
    public static JSONObject getColor(String type, String model, String complete, List pnumber) {
        JSONObject object = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        StringBuilder pnumbers = new StringBuilder();
        if (pnumber.size() == 0) {
            object.put("status", "error");
            object.put("mag", "请求零件编码不能为空！");
            object.put("list", jsonArray);
            return object;
        }
        for (int j = 0; j < pnumber.size(); j++) {
            Object number = pnumber.get(j);
            pnumbers.append("'" + number + "'").append(",");
        }
        pnumbers = pnumbers.deleteCharAt(pnumbers.length() - 1);
        if (type.equals("comp")) {
            QFilter qFilters = new QFilter("yem_framenumber", QCP.equals, complete);
            DynamicObjectCollection yem_em_devicefiles = QueryServiceHelper.query("yem_em_devicefiles",
                    "id,number,name,yem_model.number,yem_analysistdate", new QFilter[]{qFilters});
            if (yem_em_devicefiles.size() > 0) {
                //查询资源
                model = yem_em_devicefiles.get(0).getString("yem_model.number");
            }
        }
        if (model.length() > 0) {
            //查询产品类型
            QFilter qFilters = new QFilter("number", QCP.equals, model);
            DynamicObjectCollection basemodelib = QueryServiceHelper.query("yem_bd_basemodelib",
                    "id,number,name,yem_productsgroup", new QFilter[]{qFilters});
            if (basemodelib.size() > 0) {
                long yem_productsgroup = basemodelib.get(0).getLong("yem_productsgroup");
                String sql = "";
                if (type.equals("comp")) {
                    sql = "select DISTINCT t2.item_code item_code,t2.COLOR_CODE COLOR_CODE,color_name from VIEW_PLAN_PIN_WM t1\n" +
                            "left  join VIEW_MATER_COLOR_WM t2 on t1.PRODUCT_COLOR= T2.PRODUCT_COLOR " +
                            "where ITEM_CODE in (" + pnumbers + ") and PLAN_PIN='" + complete + "'";
                } else {
                    sql = "Select DISTINCT item_code,color_code,color_name from VIEW_SELECT_COLOR_WM  where ITEM_CODE in (" + pnumbers + ")";
                }
                List<Map> fromCmmp = RequestCmmp.getFromCmmp(0l, sql, yem_productsgroup);
                if (fromCmmp.size() > 0) {
                    for (int i = 0; i < fromCmmp.size(); i++) {
                        Map map = fromCmmp.get(i);
                        JSONObject jsonObject = new JSONObject();
                        String colorCode = map.get("color_code").toString();
                        String colorname = map.get("color_name").toString();
                        String itemcode = map.get("item_code").toString();
//                        jsonObject.put("color_number", map.get("color_code").toString());
                        if (include(jsonArray, itemcode)) {
                            for (int k = 0; k < jsonArray.size(); k++) {
                                JSONObject jsonObject1 = jsonArray.getJSONObject(k);
                                if (jsonObject1.containsKey(itemcode)) {
                                    JSONArray jsonArray1 = jsonObject1.getJSONArray(itemcode);
                                    JSONObject ColorjsonObject = new JSONObject();
                                    ColorjsonObject.put("colorcode", colorCode);
                                    ColorjsonObject.put("colorname", colorname);
                                    jsonArray1.add(ColorjsonObject);
//                                    jsonObject.put(itemcode, jsonArray1);
                                }
                            }
                        } else {
                            JSONArray ColorjsonArray = new JSONArray();
                            JSONObject ColorjsonObject = new JSONObject();
                            ColorjsonObject.put("colorcode", colorCode);
                            ColorjsonObject.put("colorname", colorname);
                            ColorjsonArray.add(ColorjsonObject);
                            jsonObject.put(itemcode, ColorjsonArray);
                            jsonArray.add(jsonObject);
                        }
                    }
                } else {
                    object.put("status", "error");
                    object.put("mag", "未获取到对应颜色！");
                    object.put("list", jsonArray);
                    return object;
                }
            }
        } else {
            object.put("status", "error");
            object.put("mag", "根据整机编号【" + complete + "】为获取到对应资源,查询失败！");
            object.put("list", jsonArray);
            return object;
        }
        object.put("status", "success");
        object.put("mag", "");
        object.put("list", jsonArray);
        return object;
    }

    /**
     * 判断JSONArray是否包含
     *
     * @param jsonArray 数组
     * @param key       字符串
     * @return true false
     */
    public static Boolean include(JSONArray jsonArray, String key) {
        for (int k = 0; k < jsonArray.size(); k++) {
            JSONObject jsonObject1 = jsonArray.getJSONObject(k);
            if (jsonObject1.containsKey(key)) {
                return true;
            }
        }
        return false;
    }

    public static JSONObject getprice(String product, String pnumber) {
        String productname = "";
        JSONObject object = new JSONObject();
        if (product.contains("ZZJ")) {
            product = "ZZJPJ";
            productname = "装载机配件";
        } else if (product.contains("CC")) {
            product = "CCPJ";
            productname = "叉车配件";
        } else if (product.contains("TLJ")) {
            product = "TLJPJ";
            productname = "拖拉机配件";
        }
        JSONObject jsonObject = new JSONObject();
        QFilter qFilters = new QFilter("billstatus", QCP.equals, "C");
        qFilters.and(new QFilter("yem_billtype.number", QCP.equals, "yem_pricelibrary_PJ"));
        qFilters.and(new QFilter("yem_productclass.number", QCP.equals, product));
        qFilters.and(new QFilter("yem_accessory.yem_material.number", QCP.equals, pnumber));
        String selectfield = DynamicObjectUtil.getSelectfields("yem_pricelibrary", false);
        String selectfields = DynamicObjectUtil.getEntrySelectfields(selectfield, "yem_pricelibrary", "yem_accessory", false);
        DynamicObjectCollection yem_em_devicefiles = QueryServiceHelper.query("yem_pricelibrary", selectfields, new QFilter[]{qFilters}, "yem_bizdate desc");
        if (yem_em_devicefiles.size() > 0) {
            DynamicObject devicefiles = yem_em_devicefiles.get(0);
            jsonObject.put("standsalesprice", devicefiles.getBigDecimal("yem_accessory.yem_standsalesprice"));
        } else {
            object.put("status", "error");
            object.put("mag", "未获取到类型为【" + productname + "】，物料编码为【" + pnumber + "】的配件价格信息，请联系管理员！");
            object.put("data", jsonObject);
            return object;
        }
        object.put("status", "success");
        object.put("mag", "");
        object.put("data", jsonObject);
        return object;
    }

    /**
     * 查询是否有下级
     *
     * @param etcId
     * @return
     */
    public static boolean isassat(String etcId) {
        QFilter qFilter = new QFilter("number", QCP.equals, etcId);
        qFilter.and(new QFilter("yem_entryentity.yem_iscomp", QCP.equals, true));
        DynamicObjectCollection query = QueryServiceHelper.query("yem_em_assatlasdet", "id", qFilter.toArray(), null);
        if (query.size() > 0) {
            return true;
        }
        return false;
    }

    /**
     * 总成查询图册部件明细分录
     *
     * @return
     */
    public List getassatlaspartdet(String selectid, Boolean isComplete, Date yem_completetime) {
        List list = new ArrayList<>();
        List listreplace = new ArrayList<>();
        listreplace.add("A");
        listreplace.add("B");
        QFilter qFilters = new QFilter("number", QCP.equals, selectid);
        if (isComplete) {
            List getassatlasdet = AtlasUtils.getassatlasdet(selectid, yem_completetime);
            if (getassatlasdet.size() > 0) {
                qFilters.and(new QFilter("yem_entryentity.yem_partnumber", QCP.not_in, getassatlasdet));
            }
        } else {
            qFilters.and(new QFilter("yem_entryentity.yem_partisreplace", QCP.equals, false));
        }
        DynamicObjectCollection yem_em_assatlasdet = QueryServiceHelper.query("yem_em_assatlasdet",
                "yem_entryentity.id,yem_entryentity.yem_replace,yem_entryentity.yem_completetime,yem_entryentity.yem_partnumber"
                , new QFilter[]{qFilters});
        for (int i = 0; i < yem_em_assatlasdet.size(); i++) {
            DynamicObject object = yem_em_assatlasdet.get(i);
            long yem_entryentityid = object.getLong("yem_entryentity.id");
            if (isComplete) {
                String yem_replace = object.getString("yem_entryentity.yem_replace");
                Date completetime = object.getDate("yem_entryentity.yem_completetime");
                if (yem_replace.length() > 0) {
                    //新增
                    if (yem_replace.equals("D")) {
                        list.add(yem_entryentityid);
                    }
                    if (yem_replace.equals("C") || yem_replace.equals("E")) {
                        if (completetime == null) {
                            list.add(yem_entryentityid);
                        } else {
                            if (yem_completetime.before(completetime) && completetime.before(new Date())) {
                                list.add(yem_entryentityid);
                            }
                        }
                    }
                } else {
                    list.add(yem_entryentityid);
                }
            } else {
                list.add(yem_entryentityid);
            }
        }
        return list;
    }

    /**
     * 获取零件信息
     *
     * @param selectid
     * @param complete
     * @return
     */
    public DynamicObjectCollection getData(String selectid, String complete) {
        if (selectid.contains("_123245678910")) {
            String[] selec = selectid.split("_");
            String selectfield = DynamicObjectUtil.getSelectfields("yem_em_devicefiles", false);
            String selectfields = DynamicObjectUtil.getEntrySelectfields(selectfield, "yem_em_devicefiles", "yem_retrofitting", false);
            QFilter qFilterdev = new QFilter("yem_framenumber", QCP.equals, complete);
            if (selec[0].length() > 0) {
                qFilterdev.and(new QFilter("yem_retrofitting.yem_albumassemblyno.number", QCP.equals, selec[0]));
            } else {
                qFilterdev.and(new QFilter("yem_retrofitting.yem_albumassemblyno.number", QCP.equals, null));
            }
            DynamicObjectCollection yem_retrofitting = QueryServiceHelper.query("yem_em_devicefiles", selectfields, new QFilter[]{qFilterdev}, null);
            if (yem_retrofitting.size() > 0) {
                //重新构建Data
                DynamicObjectCollection data = reconData(yem_retrofitting);
                return data;
            }
        } else {
            String selectFieids = "id,number,name,yem_atlasno,yem_partenname,yem_partqty,yem_partisreplace,yem_partremark,yem_partweight," +
                    "yem_price,yem_qtytext,yem_pricetext,yem_weighttext,yem_isrepgroup";
//            List getassatlaspartdetlist = getassatlaspartdet();
//            QFilter qFilter = new QFilter("id", QCP.in, getassatlaspartdetlist);
//            DynamicObjectCollection data = QueryServiceHelper.query("yem_em_assatlaspartdet", selectFieids, new QFilter[]{qFilter}, "length(yem_atlasno),yem_atlasno");
//            // 把列数据返回
//            DynamicObjectCollection datas = DealData(data);
//            return datas;
        }
        return EmptData();
    }

    public DynamicObjectCollection EmptData() {
        DynamicObjectCollection NewdataCollection = new DynamicObjectCollection();
        DynamicObject dynamicObject = BusinessDataServiceHelper.newDynamicObject("yem_em_asspart");
        DynamicObject newDynObject = new DynamicObject(dynamicObject.getDynamicObjectType());
        NewdataCollection.add(newDynObject);
        return NewdataCollection;
    }

    /**
     * 处理特殊逻辑
     * 1. 数量
     * 2. 替换件
     *
     * @param data 查询的数据集合
     * @return 返回数据包
     */
    public DynamicObjectCollection DealData(String selectid, Boolean isComplete, Date yem_completetime, DynamicObjectCollection data) {
//        if (cache.get("yem_replacement") != null) {
//            yem_replacement = cache.get("yem_replacement").toString();
//        }
//           String  complete = get("complete").toString();

        for (int i = 0; i < data.size(); i++) {
            DynamicObject newDynObject = data.get(i);
            //通过零件编码过滤出替换零件编码
            String yem_pnumber = newDynObject.getString("number");
            //是否替换
            DynamicObject yem_em_substitution = SubRelationUtils.queryAssemblyrep(selectid, yem_pnumber, "Sub", isComplete, yem_completetime);
            if (yem_em_substitution != null) {
                String yem_replace = yem_em_substitution.getString("yem_entryentity.yem_replace");
                if (yem_replace.equals("E")) {
                    newDynObject.set("number", yem_em_substitution.getString("yem_entryentity.yem_onumber"));
                    newDynObject.set("name", yem_em_substitution.getString("yem_entryentity.yem_oname"));
                    newDynObject.set("yem_partenname", yem_em_substitution.getString("yem_entryentity.yem_oenname"));
                    newDynObject.set("yem_qtytext", yem_em_substitution.getString("yem_entryentity.yem_oqty"));
                } else {
                    newDynObject.set("number", yem_em_substitution.getString("yem_entryentity.yem_repnumber"));
                    newDynObject.set("name", yem_em_substitution.getString("yem_entryentity.yem_repname"));
                    newDynObject.set("yem_partenname", yem_em_substitution.getString("yem_entryentity.yem_repenname"));
                    newDynObject.set("yem_qtytext", yem_em_substitution.getString("yem_entryentity.yem_repenqty"));
                }

                newDynObject.set("yem_partisreplace", true);
            } else {
                //总成间零件替换关系
//                DynamicObjectCollection pra = SubRelationUtils.queryassemtopartAll(ParentNodeId, selectid, yem_pnumber, "rep", yem_replacement, isComplete, yem_completetime);
//                if (pra.size() > 0) {
//                    newDynObject.set("yem_partisreplace", true);
//                } else {
//                    newDynObject.set("yem_partisreplace", false);
//                }
            }
            //替换组
            long ReplaceGroupid = SubRelationUtils.queryReplaceGroup(selectid, yem_pnumber, isComplete, yem_completetime);
            if (ReplaceGroupid > 0) {
                newDynObject.set("yem_isrepgroup", true);
            } else {
                newDynObject.set("yem_isrepgroup", false);
            }
            BigDecimal yem_qty = newDynObject.getBigDecimal("yem_partqty");
            newDynObject.set("yem_qtytext", yem_qty.setScale(4, RoundingMode.HALF_UP));
            newDynObject.set("yem_weighttext", newDynObject.getBigDecimal("yem_partweight").setScale(4, RoundingMode.HALF_UP));
            newDynObject.set("yem_pricetext", newDynObject.getBigDecimal("yem_price").setScale(4, RoundingMode.HALF_UP));
        }
        return data;
    }

    /**
     * 加装信息重新构建Data
     *
     * @param data 待处理数据包
     * @return 返回数据包
     */
    public DynamicObjectCollection reconData(DynamicObjectCollection data) {
        DynamicObjectCollection NewdataCollection = new DynamicObjectCollection();
        DynamicObject dynamicObject = BusinessDataServiceHelper.newDynamicObject("yem_em_assatlaspartdet");
        for (int i = 0; i < data.size(); i++) {
            DynamicObject dynamicObject1 = data.get(i);
            long aLong = dynamicObject1.getLong("yem_retrofitting.yem_materiel");
            DynamicObject dynamicObject2 = BusinessDataServiceHelper.loadSingle(aLong, "bd_material");
            DynamicObject newDynObject = new DynamicObject(dynamicObject.getDynamicObjectType());
            newDynObject.set("yem_atlasno", i + 1);//图册编号
            newDynObject.set("number", dynamicObject2.getString("number"));//零件编号
            newDynObject.set("name", dynamicObject2.getString("name"));//零件名称
            newDynObject.set("yem_partenname", dynamicObject1.getString("yem_retrofitting.yem_enname"));//英文名称
            BigDecimal yem_qty1 = dynamicObject1.getBigDecimal("yem_retrofitting.yem_qty1");
            newDynObject.set("yem_qtytext", yem_qty1.setScale(4, BigDecimal.ROUND_HALF_UP).toString());//数量
            newDynObject.set("yem_partremark", dynamicObject1.getString("yem_retrofitting.yem_desc"));//备注
            NewdataCollection.add(newDynObject);
        }
        return NewdataCollection;
    }


}
