package com.yem.em.utils;

import com.yem.wm.utils.DynamicObjectUtil;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.stream.Stream;

/**
 * @author zhouc
 * @date 2024/1/3 9:29
 * @className SubRelationUtils
 * @description 替换关系查询公共类
 */
public class SubRelationUtils {

    /**
     * 查询替换关系
     *
     * @param number      父件编码
     * @param yem_parno   子件编码
     * @param replacement 替换类型 部件替换 Par  零件替换 Sub
     * @param isComplete  是否整机
     * @return 动态对象
     */
    public static DynamicObject queryAssemblyrep(String number, String yem_parno, String replacement, boolean isComplete, Date yem_completetime) {
        DynamicObject object = null;
        QFilter qFilter = new QFilter("yem_replacement", QCP.equals, replacement);//
        qFilter.and(new QFilter("status", QCP.equals, "C"));
        qFilter.and(new QFilter("number", QCP.equals, number));
        qFilter.and(new QFilter("yem_entryentity.yem_onumber", QCP.equals, yem_parno)
                .or("yem_entryentity.yem_repnumber", QCP.equals, yem_parno));
        if (isComplete) {
            qFilter.and(new QFilter("yem_entryentity.yem_replace", QCP.not_equals, "C"));
            qFilter.and(new QFilter("yem_entryentity.yem_completetime", QCP.large_than, yem_completetime));
        }
        qFilter.and(new QFilter("yem_entryentity.yem_completetime", QCP.less_equals, new Date()));
        String selectfield = DynamicObjectUtil.getSelectfields("yem_em_replace", false);
        String selectfields = DynamicObjectUtil.getEntrySelectfields(selectfield, "yem_em_replace", "yem_entryentity", false);
        DynamicObjectCollection data = QueryServiceHelper.query("yem_em_replace",
                selectfields, new QFilter[]{qFilter}, "yem_entryentity.yem_completetime DESC");
        if (data.size() > 0) {
            object = data.get(0);
        }
        return object;
    }

    /**
     * 查询替换关系
     *
     * @param number      父件编码
     * @param yem_parno   子件编码
     * @param replacement 替换类型 部件替换 Par  零件替换 Sub
     * @param isComplete  是否整机
     * @return 动态对象
     */
    public static DynamicObject querynoAssemblyrep(String number, String yem_parno, String replacement, boolean isComplete, Date yem_completetime) {
        DynamicObject object = null;
        QFilter qFilter = new QFilter("yem_replacement", QCP.equals, replacement);//
        qFilter.and(new QFilter("status", QCP.equals, "C"));
        qFilter.and(new QFilter("yem_entryentity.yem_repno", QCP.equals, number));
        qFilter.and(new QFilter("yem_entryentity.yem_repnumber", QCP.equals, yem_parno));
        qFilter.and(new QFilter("yem_entryentity.yem_replace", QCP.equals, "C"));
        qFilter.and(new QFilter("yem_entryentity.yem_completetime", QCP.large_than, yem_completetime));
        qFilter.and(new QFilter("yem_entryentity.yem_completetime", QCP.less_equals, new Date()));
        String selectfield = DynamicObjectUtil.getSelectfields("yem_em_replace", false);
        String selectfields = DynamicObjectUtil.getEntrySelectfields(selectfield, "yem_em_replace", "yem_entryentity", false);
        DynamicObjectCollection data = QueryServiceHelper.query("yem_em_replace",
                selectfields, new QFilter[]{qFilter}, "yem_entryentity.yem_completetime DESC");
        if (data.size() > 0) {
            object = data.get(0);
        }
        return object;
    }

    /**
     * 查询替换关系
     *
     * @param number      父件编码
     * @param yem_parno   子件编码
     * @param replacement 替换类型 部件替换 Par  零件替换 Sub
     * @return 动态对象集合
     */
    public static DynamicObjectCollection queryAssemblyrepAll(String number, String yem_parno, String replacement, boolean isComplete, Date yem_completetime) {
        DynamicObjectCollection object = null;
        QFilter qFilter = new QFilter("yem_replacement", QCP.equals, replacement);//
        qFilter.and(new QFilter("yem_entryentity.yem_praonumber", QCP.equals, number)//原部件编码
                .or(new QFilter("yem_entryentity.yem_repno", QCP.equals, number)));//替换部件编码
        qFilter.and(new QFilter("yem_entryentity.yem_onumber", QCP.equals, yem_parno)//原零件编码
                .or("yem_entryentity.yem_repnumber", QCP.equals, yem_parno));//替换零件编码
        qFilter.and(new QFilter("status", QCP.equals, "C"));
        if (isComplete) {
            qFilter.and(new QFilter("yem_entryentity.yem_replace", QCP.not_equals, "C"));
            qFilter.and(new QFilter("yem_entryentity.yem_completetime", QCP.large_than, yem_completetime));
        }
        qFilter.and(new QFilter("yem_entryentity.yem_completetime", QCP.less_equals, new Date()));
        String selectfield = DynamicObjectUtil.getSelectfields("yem_em_replace", false);
        String selectfields = DynamicObjectUtil.getEntrySelectfields(selectfield, "yem_em_replace", "yem_entryentity", false);
        object = QueryServiceHelper.query("yem_em_replace", selectfields, new QFilter[]{qFilter}, "yem_entryentity.yem_completetime DESC");
        if (object.size() > 0) {
            object = queryAssemblyreplevel(object, replacement, isComplete, yem_completetime);
        }

        return object;
    }

    /**
     * 查询所有替换关系
     *
     * @param object      上级对象
     * @param replacement 替换类型
     * @param isComplete  是否整机
     * @return
     */
    public static DynamicObjectCollection queryAssemblyreplevel(DynamicObjectCollection object, String replacement, boolean isComplete, Date yem_completetime) {
        for (int i = 0; i < object.size(); i++) {
            DynamicObject replace = object.get(i);
            String yem_replace = replace.getString("yem_entryentity.yem_replace");
            if (yem_replace.equals("D") || yem_replace.equals("E")) {
                return object;
            }
            String yem_praonumber = replace.getString("yem_entryentity.yem_praonumber");
            String yem_onumber = replace.getString("yem_entryentity.yem_onumber");
            QFilter qFilter = new QFilter("yem_replacement", QCP.equals, replacement);//
            qFilter.and(new QFilter("yem_entryentity.yem_repno", QCP.equals, yem_praonumber));//替换部件编码
            qFilter.and(new QFilter("yem_entryentity.yem_repnumber", QCP.equals, yem_onumber));//替换零件编码
            qFilter.and(new QFilter("status", QCP.equals, "C"));
            if (isComplete) {
                qFilter.and(new QFilter("yem_entryentity.yem_replace", QCP.not_equals, "C"));
                qFilter.and(new QFilter("yem_entryentity.yem_completetime", QCP.large_than, yem_completetime));
            }
            qFilter.and(new QFilter("yem_entryentity.yem_completetime", QCP.less_equals, new Date()));

            String selectfield = DynamicObjectUtil.getSelectfields("yem_em_replace", false);
            String selectfields = DynamicObjectUtil.getEntrySelectfields(selectfield, "yem_em_replace", "yem_entryentity", false);
            DynamicObjectCollection yem_em_replace = QueryServiceHelper.query("yem_em_replace",
                    selectfields, new QFilter[]{qFilter}, "yem_entryentity.yem_completetime DESC");
            if (yem_em_replace.size() > 0) {
                for (int j = 0; j < yem_em_replace.size(); j++) {
                    object.add(yem_em_replace.get(j));
                }
                queryAssemblyreplevel(yem_em_replace, replacement, isComplete, yem_completetime);
            }
        }
        return object;
    }

    /**
     * @param map         根据替换关系获取替换前map<部件-零件，上级部件>
     * @param replacement 替换类型 部件替换 Par  零件替换 Sub
     * @param isComplete  是否整机
     * @return 返回动态对象集合
     */
    public static DynamicObjectCollection queryreplacetoass(Map<String, String> map, String replacement, boolean isComplete, String yem_replacement, Date yem_completetime) {
        DynamicObjectCollection dynamicObjectCollection = new DynamicObjectCollection();
        for (String mapkey : map.keySet()) {
            String parnumber = map.get(mapkey);
            String[] splitkey = mapkey.split("_");
            if (splitkey.length > 1) {
                String subnumber = splitkey[0];
                String repnumber = splitkey[1];
                DynamicObjectCollection rep = queryassemtopartAll(parnumber, subnumber, repnumber, "rep", yem_replacement, isComplete, yem_completetime);
                for (int i = 0; i < rep.size(); i++) {
                    DynamicObject object = rep.get(i);
                    String number = object.getString("yem_entryentity.yem_praonumber");
                    String parno = object.getString("yem_entryentity.yem_onumber");
                    dynamicObjectCollection.add(object);
                    DynamicObjectCollection emsubstitution = SubRelationUtils.queryAssemblyrepAll(number, parno, replacement, isComplete, yem_completetime);
                    if (emsubstitution != null && emsubstitution.size() > 0) {
                        for (int j = 0; j < emsubstitution.size(); j++) {
                            dynamicObjectCollection.add(emsubstitution.get(j));
                        }
                        queryreplacetoass(GetAssignMap(emsubstitution, parnumber), replacement, isComplete, yem_replacement, yem_completetime);
                    }
                }
            }
        }
        return dynamicObjectCollection;
    }

    /**
     * 查询替换组
     *
     * @param number    父件编码
     * @param yem_parno 子件编码
     * @return 替换组id
     */
    public static long queryReplaceGroup(String number, String yem_parno, boolean isComplete, Date yem_completetime) {
        long fid = 0;
        QFilter qFilter = new QFilter("number", QCP.equals, number);//
        qFilter.and(new QFilter("yem_repbefore.yem_repbeforenumber", QCP.equals, yem_parno));
        if (isComplete) {
            qFilter.and(new QFilter("yem_date", QCP.large_than, yem_completetime));
        }
        qFilter.and(new QFilter("yem_date", QCP.less_equals, new Date()));
        DynamicObjectCollection data = QueryServiceHelper.query("yem_em_replacementgroup",
                "id,number,name,yem_date", new QFilter[]{qFilter}, "yem_date DESC");
        if (data.size() > 0) {
            DynamicObject object = data.get(0);
            if (object != null) {
                fid = object.getLong("id");
            }
        }
        return fid;
    }

    /**
     * 查询上级部件
     *
     * @param subnumber 部件编码
     * @param type      类型
     * @return 返回上级部件编码
     */
    public static DynamicObject querySuperior(String subnumber, String type) {
        DynamicObject result = null;
        QFilter qFilter = new QFilter("yem_entryentity.yem_partnumber", QCP.equals, subnumber);//
        qFilter.and(new QFilter("yem_entryentity.yem_iscomp", QCP.equals, true));
        qFilter.and(new QFilter("status", QCP.equals, "C"));
        DynamicObjectCollection data = QueryServiceHelper.query("yem_em_assatlasdet",
                "id,number,name", new QFilter[]{qFilter}, null);
        if (data.size() > 0) {
            result = data.get(0);
        }
        return result;
    }

    /**
     * 根据原子项总成和原子项零件
     * 查询总成件零件替换关系
     *
     * @param parnumber       父件总成
     * @param subnumber       子项总成
     * @param repnumber       零件编码
     * @param type            类型 pra-根据原子项查询 rep-根据替换子项查询
     * @param yem_replacement 总成类型 Par-资源 Sub-部件
     * @return 返回动态对象
     */
    public static DynamicObject queryassemtopart(String parnumber, String subnumber, String repnumber, String type, String yem_replacement, boolean isComplete, Date yem_completetime) {
        DynamicObject result = null;
        QFilter qFilter = new QFilter("number", QCP.equals, parnumber);//
        qFilter.and(new QFilter("status", QCP.equals, "C"));
        if (type.equals("pra")) {
            qFilter.and(new QFilter("yem_entryentity.yem_praonumber", QCP.equals, subnumber));
            if (repnumber.length() > 0) {
                qFilter.and(new QFilter("yem_entryentity.yem_onumber", QCP.equals, repnumber));
            }
        }
        if (type.equals("rep")) {
            qFilter.and(new QFilter("yem_entryentity.yem_repno", QCP.equals, subnumber));
            if (repnumber.length() > 0) {
                qFilter.and(new QFilter("yem_entryentity.yem_repnumber", QCP.equals, repnumber));
            }
        }
        if (isComplete) {
            qFilter.and(new QFilter("yem_entryentity.yem_completetime", QCP.large_than, yem_completetime));
        } else {
            qFilter.and(new QFilter("yem_entryentity.yem_completetime", QCP.less_equals, new Date()));
        }
//        qFilter.and(new QFilter("yem_replacement", QCP.equals, yem_replacement));
        String selectfield = DynamicObjectUtil.getSelectfields("yem_em_assemtopart", false);
        String selectfields = DynamicObjectUtil.getEntrySelectfields(selectfield, "yem_em_assemtopart", "yem_entryentity", false);
        DynamicObjectCollection data = QueryServiceHelper.query("yem_em_assemtopart", selectfields, new QFilter[]{qFilter}, null);
        if (data.size() > 0) {
            result = data.get(0);
        }
        return result;
    }

    /**
     * 根据原子项总成和原子项零件
     * 查询总成件零件替换关系
     *
     * @param parnumber       父件总成
     * @param subnumber       子项总成
     * @param repnumber       零件编码
     * @param type            类型 pra-根据原子项查询 rep-根据替换子项查询
     * @param yem_replacement 总成类型 Par-资源 Sub-部件
     * @return 返回动态对象集合
     */
    public static DynamicObjectCollection queryassemtopartAll(String parnumber, String subnumber, String repnumber, String type, String yem_replacement, boolean isComplete, Date yem_completetime) {
        QFilter qFilter = new QFilter("number", QCP.equals, parnumber);//
        qFilter.and(new QFilter("status", QCP.equals, "C"));
        if (type.equals("pra")) {
            qFilter.and(new QFilter("yem_entryentity.yem_praonumber", QCP.equals, subnumber));
            if (repnumber.length() > 0) {
                qFilter.and(new QFilter("yem_entryentity.yem_onumber", QCP.equals, repnumber));
            }
        }
        if (type.equals("rep")) {
            qFilter.and(new QFilter("yem_entryentity.yem_repno", QCP.equals, subnumber));
            if (repnumber.length() > 0) {
                qFilter.and(new QFilter("yem_entryentity.yem_repnumber", QCP.equals, repnumber));
            }
        }
        if (isComplete) {
            qFilter.and(new QFilter("yem_entryentity.yem_completetime", QCP.large_than, yem_completetime));
        } else {
            qFilter.and(new QFilter("yem_entryentity.yem_completetime", QCP.less_equals, new Date()));
        }
//        qFilter.and(new QFilter("yem_replacement", QCP.equals, yem_replacement));
        String selectfield = DynamicObjectUtil.getSelectfields("yem_em_assemtopart", false);
        String selectfields = DynamicObjectUtil.getEntrySelectfields(selectfield, "yem_em_assemtopart", "yem_entryentity", false);
        DynamicObjectCollection data = QueryServiceHelper.query("yem_em_assemtopart", selectfields, new QFilter[]{qFilter}, null);
        return data;
    }

    /**
     * 根据《总成图册明细》零件编码获取零件英文名称
     *
     * @param subnumber
     * @return
     */
    public static DynamicObject querypartenname(String subnumber) {
        DynamicObject result = null;
        String selectfield = DynamicObjectUtil.getSelectfields("yem_em_assatlasdet", false);
        String selectfields = DynamicObjectUtil.getEntrySelectfields(selectfield, "yem_em_assatlasdet", "yem_entryentity", false);
        QFilter qFilter = new QFilter("yem_entryentity.yem_partnumber", QCP.equals, subnumber);//
        qFilter.and(new QFilter("status", QCP.equals, "C"));
        DynamicObjectCollection data = QueryServiceHelper.query("yem_em_assatlasdet", selectfields, new QFilter[]{qFilter}, null);
        if (data.size() > 0) {
            result = data.get(0);
        }
        return result;
    }

    /**
     * 《替换关系》更新CMMP部件底表
     *
     * @param bill        表头数据
     * @param entryentity 表体行数据
     * @param Optype      操作类型
     */
    public static void UpdateCMMPTable(DynamicObject bill, DynamicObject entryentity, String Optype) {
        String yem_praonumber = entryentity.getString("yem_praonumber");//原部件编码
        String yem_replace = entryentity.getString("yem_replace");//替换关系
        QFilter qFilter = new QFilter("number", QCP.equals, yem_praonumber);
        DynamicObject Object = BusinessDataServiceHelper.loadSingle("yem_em_cmmptable",
                "id,number,name,yem_isalter", qFilter.toArray());
        if (Object != null) {
            DynamicObject yem_em_assatlasdet = BusinessDataServiceHelper.loadSingle(Object.getLong("id"), "yem_em_cmmptable");
            if (yem_em_assatlasdet != null) {
                DynamicObjectCollection yem_entryentity = yem_em_assatlasdet.getDynamicObjectCollection("yem_entryentity");
                if (yem_replace.equals("D")) {
                    if ("Audit".equals(Optype)) {
                        DynamicObject subentryentity = yem_entryentity.addNew();
//                        subentryentity.set("yem_ccode", entryentity.getString("yem_repnumber"));
//                        subentryentity.set("yem_ccodename", entryentity.getString("yem_repname"));
//                        subentryentity.set("yem_qty", entryentity.getBigDecimal("yem_repenqty"));

                        QFilter qFilter1 = new QFilter("number", QCP.equals, entryentity.getString("yem_repnumber"));
                        DynamicObject yem_partasspartlib = BusinessDataServiceHelper.loadSingle("bd_material",
                                "id,number,name,modelnum,baseunit", qFilter1.toArray());
                        if (yem_partasspartlib != null) {
                            subentryentity.set("yem_isnormal", true);
                            subentryentity.set("yem_asscomponent", yem_partasspartlib);
                            subentryentity.set("yem_assname", entryentity.getString("yem_repname"));
                            subentryentity.set("yem_assqty", entryentity.getString("yem_repenqty"));
                        }

                        QFilter qFilter2 = new QFilter("number", QCP.equals, entryentity.getString("yem_repnumber"));
                        DynamicObject yem_assatlasdet = BusinessDataServiceHelper.loadSingle("yem_em_assatlasdet",
                                "id,number,name,modelnum,baseunit", qFilter2.toArray());
                        if (yem_assatlasdet != null) {
                            subentryentity.set("yem_assatlasdet", yem_assatlasdet);
                            subentryentity.set("yem_isassatlasdet", true);
                        }
                        subentryentity.set("yem_isadd", true);
                        subentryentity.set("yem_completetime", entryentity.getDate("yem_completetime"));
                        subentryentity.set("yem_replace", entryentity.getString("yem_replace"));
                        subentryentity.set("yem_creationtime", new Date());
                    } else {
                        String yem_repnumber = entryentity.getString("yem_repnumber");
                        for (int j = 0; j < yem_entryentity.size(); j++) {
                            DynamicObject dynamicObject = yem_entryentity.get(j);
                            String yem_partnumber = dynamicObject.getString("yem_partnumber");
                            if (yem_partnumber.equals(yem_repnumber)) {
                                yem_entryentity.remove(j--);
                                break;
                            }
                        }
                    }
                } else {
                    String yem_onumber = entryentity.getString("yem_onumber");//原零件编码
                    for (int i = 0; i < yem_entryentity.size(); i++) {
                        DynamicObject entryObject = yem_entryentity.get(i);
                        DynamicObject yem_asscomponent = entryObject.getDynamicObject("yem_asscomponent");
                        if (yem_asscomponent != null) {
                            String number = yem_asscomponent.getString("number");
                            if (number.equals(yem_onumber)) {
                                if (yem_replace.equals("E")) {
                                    if ("Audit".equals(Optype)) {
                                        entryObject.set("yem_isdelete", true);
                                        entryObject.set("yem_completetime", entryentity.getDate("yem_completetime"));
                                        entryObject.set("yem_replace", entryentity.getString("yem_replace"));
                                    } else {
                                        entryObject.set("yem_isdelete", false);
                                        entryObject.set("yem_completetime", null);
                                        entryObject.set("yem_replace", "");
                                    }
                                } else {
                                    if ("Audit".equals(Optype)) {
                                        DynamicObject subentryentity = yem_entryentity.addNew();

//                                    subentryentity.set("yem_ccode", entryentity.getString("yem_repnumber"));
//                                    subentryentity.set("yem_ccodename", entryentity.getString("yem_repname"));
//                                    subentryentity.set("yem_qty", entryentity.getBigDecimal("yem_repenqty"));

                                        QFilter qFilter1 = new QFilter("number", QCP.equals, entryentity.getString("yem_repnumber"));
                                        DynamicObject yem_partasspartlib = BusinessDataServiceHelper.loadSingle("bd_material",
                                                "id,number,name,modelnum,baseunit", qFilter1.toArray());
                                        if (yem_partasspartlib != null) {
                                            subentryentity.set("yem_isnormal", true);
                                            subentryentity.set("yem_asscomponent", yem_partasspartlib);
                                            subentryentity.set("yem_assname", entryentity.getString("yem_repname"));
                                            subentryentity.set("yem_assqty", entryentity.getString("yem_repenqty"));
                                        }
                                        QFilter qFilter2 = new QFilter("number", QCP.equals, entryentity.getString("yem_repnumber"));
                                        DynamicObject yem_assatlasdet = BusinessDataServiceHelper.loadSingle("yem_em_assatlasdet",
                                                "id,number,name,modelnum,baseunit", qFilter2.toArray());
                                        if (yem_assatlasdet != null) {
                                            subentryentity.set("yem_assatlasdet", yem_assatlasdet);
                                            subentryentity.set("yem_isassatlasdet", true);
                                        }
//                                    subentryentity.set("yem_completetime", entryentity.getDate("yem_completetime"));
                                        subentryentity.set("yem_creationtime", new Date());

                                        entryObject.set("yem_replace", entryentity.getString("yem_replace"));
                                        entryObject.set("yem_partisreplace", true);
                                        entryObject.set("yem_completetime", entryentity.getDate("yem_completetime"));
                                        entryObject.set("yem_enablesubno", entryentity.getString("yem_repnumber"));
                                        entryObject.set("yem_enablesubname", entryentity.getString("yem_repname"));
                                        entryObject.set("yem_enablesubenname", entryentity.getString("yem_repenname"));
                                        entryObject.set("yem_enablesubqty", entryentity.getBigDecimal("yem_repenqty"));
                                    } else {
                                        String yem_repnumber = entryentity.getString("yem_repnumber");
                                        for (int j = 0; j < yem_entryentity.size(); j++) {
                                            DynamicObject dynamicObject = yem_entryentity.get(j);
                                            String yem_partnumber = dynamicObject.getString("yem_asscomponent.number");
                                            if (yem_partnumber.equals(yem_repnumber)) {
                                                yem_entryentity.remove(j--);
                                                break;
                                            }
                                        }
                                        entryObject.set("yem_replace", "");
                                        entryObject.set("yem_completetime", null);
                                        entryObject.set("yem_partisreplace", false);
                                        entryObject.set("yem_enablesubno", "");
                                        entryObject.set("yem_enablesubname", "");
                                        entryObject.set("yem_enablesubenname", "");
                                        entryObject.set("yem_enablesubqty", BigDecimal.ZERO);
                                    }
                                }
                                break;
                            }
                        }

                    }
                }
                for (int i = 0; i < yem_entryentity.size(); i++) {
                    DynamicObject object = yem_entryentity.get(i);
                    object.set("seq", i + 1);
                }
                OperationResult Operate = OperationServiceHelper.executeOperate("save", yem_em_assatlasdet.getDynamicObjectType().getName(),
                        new DynamicObject[]{yem_em_assatlasdet}, OperateOption.create());
                if (Operate.isSuccess()) {

                }
            }
        }
    }

    /**
     * 《BOM变更单》更新CMMP部件底表
     *
     * @param bill        表头数据
     * @param entryentity 表体行数据
     * @param Optype      操作类型
     */
    public static void BomUpdateCMMPTable(DynamicObject bill, DynamicObject entryentity, String Optype) {
        boolean yem_isresource = entryentity.getBoolean("yem_isresource");
        if (yem_isresource) {
            String yem_parentno = entryentity.getString("yem_parentno");//父件编码
            String yem_subno = entryentity.getString("yem_subno");//子件编码
            String yem_replace = entryentity.getString("yem_replace");//替换关系
            QFilter qFilter = new QFilter("number", QCP.equals, yem_parentno);
            DynamicObject Object = BusinessDataServiceHelper.loadSingle("yem_em_cmmptable",
                    "id,number,name,yem_isreplace", qFilter.toArray());
            if (Object != null) {
                DynamicObject yem_em_assatlasdet = BusinessDataServiceHelper.loadSingle(Object.getLong("id"), "yem_em_cmmptable");
                if (yem_em_assatlasdet != null) {
                    DynamicObjectCollection yem_entryentity = yem_em_assatlasdet.getDynamicObjectCollection("yem_entryentity");
                    if (yem_replace.equals("D")) {
                        if ("Audit".equals(Optype)) {
                            DynamicObject subentryentity = yem_entryentity.addNew();
//                            subentryentity.set("yem_ccode", entryentity.getString("yem_subno"));
//                            subentryentity.set("yem_ccodename", entryentity.getString("yem_subname"));
//                            subentryentity.set("yem_qty", entryentity.getBigDecimal("yem_qty"));
                            QFilter qFilter1 = new QFilter("number", QCP.equals, entryentity.getString("yem_subno"));
                            DynamicObject yem_partasspartlib = BusinessDataServiceHelper.loadSingle("bd_material",
                                    "id,number,name,modelnum,baseunit", qFilter1.toArray());
                            if (yem_partasspartlib != null) {
                                subentryentity.set("yem_isnormal", true);
                                subentryentity.set("yem_asscomponent", yem_partasspartlib);
                                subentryentity.set("yem_assname", entryentity.getString("yem_subname"));
                                subentryentity.set("yem_assqty", entryentity.getBigDecimal("yem_qty"));
                            }
                            QFilter qFilter2 = new QFilter("number", QCP.equals, entryentity.getString("yem_subno"));
                            DynamicObject yem_assatlasdet = BusinessDataServiceHelper.loadSingle("yem_em_assatlasdet",
                                    "id,number,name,modelnum,baseunit", qFilter2.toArray());
                            if (yem_em_assatlasdet != null) {
                                subentryentity.set("yem_assatlasdet", yem_assatlasdet);
                                subentryentity.set("yem_isassatlasdet", true);
                            }
                            subentryentity.set("yem_isadd", true);
                            subentryentity.set("yem_completetime", entryentity.getDate("yem_completetime"));
                            subentryentity.set("yem_replace", entryentity.getString("yem_replace"));
                            subentryentity.set("yem_creationtime", new Date());

                        } else {
                            String yem_enablesubno = entryentity.getString("yem_subno");
                            for (int j = 0; j < yem_entryentity.size(); j++) {
                                DynamicObject dynamicObject = yem_entryentity.get(j);
                                String yem_partnumber = dynamicObject.getString("yem_asscomponent.number");//CMMP部件编码
                                if (yem_partnumber.equals(yem_enablesubno)) {
                                    yem_entryentity.remove(j--);
                                    break;
                                }
                            }
                        }

                    } else {
                        for (int i = 0; i < yem_entryentity.size(); i++) {
                            DynamicObject entryObject = yem_entryentity.get(i);
                            if (entryObject.getString("yem_asscomponent.number").equals(yem_subno)) {
                                if (yem_replace.equals("E")) {
                                    if ("Audit".equals(Optype)) {
                                        entryObject.set("yem_isdelete", true);
                                        entryObject.set("yem_completetime", entryentity.getDate("yem_completetime"));
                                        entryObject.set("yem_replace", entryentity.getString("yem_replace"));
                                    } else {
                                        entryObject.set("yem_isdelete", false);
                                        entryObject.set("yem_completetime", null);
                                        entryObject.set("yem_replace", entryentity.getString("yem_replace"));
                                    }
                                } else {
                                    if ("Audit".equals(Optype)) {
                                        DynamicObject subentryentity = yem_entryentity.addNew();
//                                        subentryentity.set("yem_ccode", entryentity.getString("yem_enablesubno"));
//                                        subentryentity.set("yem_ccodename", entryentity.getString("yem_enablesubname"));
//                                        subentryentity.set("yem_qty", entryentity.getBigDecimal("yem_enablesubqty"));
                                        QFilter qFilter1 = new QFilter("number", QCP.equals, entryentity.getString("yem_enablesubno"));
                                        DynamicObject yem_partasspartlib = BusinessDataServiceHelper.loadSingle("bd_material",
                                                "id,number,name,modelnum,baseunit", qFilter1.toArray());
                                        if (yem_partasspartlib != null) {
                                            subentryentity.set("yem_isnormal", true);
                                            subentryentity.set("yem_asscomponent", yem_partasspartlib);
                                            subentryentity.set("yem_assname", entryentity.getString("yem_enablesubname"));
                                            subentryentity.set("yem_assqty", entryentity.getBigDecimal("yem_enablesubqty"));
                                        }
                                        QFilter qFilter2 = new QFilter("number", QCP.equals, entryentity.getString("yem_enablesubno"));
                                        DynamicObject yem_assatlasdet = BusinessDataServiceHelper.loadSingle("yem_em_assatlasdet",
                                                "id,number,name,modelnum,baseunit", qFilter2.toArray());
                                        if (yem_assatlasdet != null) {
                                            subentryentity.set("yem_assatlasdet", yem_assatlasdet);
                                            subentryentity.set("yem_isassatlasdet", true);
                                        }
//                                        subentryentity.set("yem_completetime", entryentity.getDate("yem_completetime"));
                                        subentryentity.set("yem_creationtime", new Date());

                                        entryObject.set("yem_replace", entryentity.getString("yem_replace"));
                                        entryObject.set("yem_partisreplace", true);
                                        entryObject.set("yem_completetime", entryentity.getDate("yem_completetime"));
                                        entryObject.set("yem_enablesubno", entryentity.getString("yem_enablesubno"));
                                        entryObject.set("yem_enablesubname", entryentity.getString("yem_enablesubname"));
                                        entryObject.set("yem_enablesubenname", entryentity.getString("yem_enablesubenname"));
                                        entryObject.set("yem_enablesubqty", entryentity.getBigDecimal("yem_enablesubqty"));
                                    } else {
                                        String yem_enablesubno = entryentity.getString("yem_enablesubno");
                                        for (int j = 0; j < yem_entryentity.size(); j++) {
                                            DynamicObject dynamicObject = yem_entryentity.get(j);
                                            String yem_partnumber = dynamicObject.getString("yem_asscomponent.number");
                                            if (yem_partnumber.equals(yem_enablesubno)) {
                                                yem_entryentity.remove(j--);
                                                break;
                                            }
                                        }
                                        entryObject.set("yem_replace", "");
                                        entryObject.set("yem_completetime", null);
                                        entryObject.set("yem_partisreplace", false);
                                        entryObject.set("yem_enablesubno", "");
                                        entryObject.set("yem_enablesubname", "");
                                        entryObject.set("yem_enablesubenname", "");
                                        entryObject.set("yem_enablesubqty", BigDecimal.ZERO);
                                    }
                                }
                                break;
                            }
                        }
                    }
                    OperationResult Operate = OperationServiceHelper.executeOperate("save", yem_em_assatlasdet.getDynamicObjectType().getName(),
                            new DynamicObject[]{yem_em_assatlasdet}, OperateOption.create());
                    if (Operate.isSuccess()) {

                    }
                }
            }
        }
    }

    /**
     * 根据替换编码获取最大图册号
     *
     * @param repno
     * @return
     */
    public static int Getatlasno(String repno) {
        int result = 0;
        QFilter qFilter = new QFilter("number", QCP.in, repno);
        DynamicObjectCollection data = QueryServiceHelper.query("yem_em_assatlasdet",
                "id,number,name,yem_entryentity.yem_atlasno", new QFilter[]{qFilter},
                "length(yem_entryentity.yem_atlasno),yem_entryentity.yem_atlasno  DESC");
        if (data.size() > 0) {
            result = data.get(0).getInt("yem_entryentity.yem_atlasno");
        }
        return result;
    }


    /**
     * 获取替换关系中原部件信息
     *
     * @param assemtopart 数据结果集合
     */
    public static Map<String, String> GetAssignMap(DynamicObjectCollection assemtopart, String Parnumber) {
        Map<String, String> map = new HashMap<>();
        if (assemtopart != null && assemtopart.size() > 0) {
            if (assemtopart.size() > 0) {
                Stream<DynamicObject> distinct = assemtopart.stream().distinct();
                distinct.forEach((DynamicObject obj) -> {
                    String mapkey = obj.getString("yem_entryentity.yem_praonumber") + "_" + obj.getString("yem_entryentity.yem_onumber");
                    if (!map.containsKey(mapkey)) {
                        map.put(mapkey, Parnumber);
                    }
                });
            }
        }
        return map;
    }

    /**
     * 获取上级部件
     *
     * @param yem_partnumber 零件编码
     * @return
     */
    public static DynamicObject getfirstNumber(String yem_partnumber) {
        QFilter qFilter = new QFilter("yem_entryentity.yem_partnumber", QCP.equals, yem_partnumber);//
        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);
        DynamicObject Object = BusinessDataServiceHelper.loadSingle("yem_em_assatlasdet", selectfields, qFilter.toArray());
        return Object;
    }

    /**
     * 获取零件一级部件
     *
     * @param yem_partnumber 零件编码
     * @return
     */
    public static DynamicObject getfirstid(String yem_partnumber) {
        Map map = new HashMap<>();
        QFilter qFilter = new QFilter("yem_entryentity.yem_partnumber", QCP.equals, yem_partnumber);//
        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);
        DynamicObject Object = BusinessDataServiceHelper.loadSingle("yem_em_assatlasdet", selectfields, qFilter.toArray());
        if (Object != null) {
            String number = Object.getString("number");
            boolean yem_issup = Object.getBoolean("yem_issup");
            if (yem_issup) {
                Object = getfirstid(number);
            }
        }
        return Object;
    }

    /**
     * 获取零件一级部件
     *
     * @param yem_partnumber 零件编码
     * @return
     */
    public static DynamicObjectCollection queallpartnumber(String yem_partnumber) {
        DynamicObjectCollection Object = null;
        QFilter qFilter = new QFilter("yem_entryentity.yem_partnumber", QCP.like, "%" + yem_partnumber + "%");//
        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);
        Object = QueryServiceHelper.query("yem_em_assatlasdet", selectfields, qFilter.toArray());
        if (Object.size() > 0) {
            Object = querypartnumberlevel(Object);
        }
        return Object;
    }

    public static DynamicObjectCollection querypartnumberlevel(DynamicObjectCollection object) {
        for (int i = 0; i < object.size(); i++) {
            DynamicObject replace = object.get(i);
            String number = replace.getString("number");
            boolean yem_issup = replace.getBoolean("yem_issup");
            if (yem_issup) {
                QFilter qFilter = new QFilter("yem_entryentity.yem_partnumber", QCP.equals, number);//
                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);
                DynamicObjectCollection yem_em_replace = QueryServiceHelper.query("yem_em_assatlasdet", selectfields, qFilter.toArray());
                if (yem_em_replace.size() > 0) {
                    for (int j = 0; j < yem_em_replace.size(); j++) {
                        object.add(yem_em_replace.get(j));
                    }
                    querypartnumberlevel(yem_em_replace);
                }
            }
        }
        return object;
    }


}
