package com.yem.em.utils;

import com.yem.wm.utils.DynamicObjectUtil;
import com.yem.wm.utils.YEM;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.operate.result.IOperateInfo;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.exception.KDBizException;
import kd.bos.form.control.Control;
import kd.bos.form.control.events.ISuportClick;
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 kd.bos.servicehelper.operation.SaveServiceHelper;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author zhouc
 * @date 2023/10/24 11:33
 * @className AtlasUtils
 * @description 图册公共类
 */
public class AtlasUtils {

    /**
     * BOM审核生成替换关系
     *
     * @param bill        表头数据包
     * @param entryentity 分录行数据包
     * @param entityName  单据标识
     */
    public static void Addreplace(DynamicObject bill, DynamicObject entryentity, DynamicObject replace, String entityName, String type) {
        DynamicObject billObj = null;
        if (type.equals("add")) {
            billObj = BusinessDataServiceHelper.newDynamicObject(entityName);
            billObj.set("createorg", bill.getDynamicObject("createorg"));
            billObj.set("enable", "1");
            billObj.set("status", "C");
            billObj.set("yem_producttype", bill.getString("yem_producttype"));
            billObj.set("number", entryentity.getString("yem_parentno"));
            billObj.set("name", entryentity.getString("yem_parentname"));
            billObj.set("creator", YEM.getCurrentUserId());
            billObj.set("yem_sourcetype", "BOM");
            billObj.set("yem_parno", entryentity.getString("yem_subno"));
            billObj.set("yem_parname", entryentity.getString("yem_subname"));
            boolean yem_isass = entryentity.getBoolean("yem_isass");
            if (yem_isass) {
                billObj.set("yem_replacement", "Sub");
            }
            boolean yem_isresource = entryentity.getBoolean("yem_isresource");
            if (yem_isresource) {
                billObj.set("yem_replacement", "Par");
            }
        } else {
            billObj = replace;
        }
        //明细信息
        DynamicObjectCollection yem_entryentity = billObj.getDynamicObjectCollection("yem_entryentity");
        DynamicObject subentryentity = yem_entryentity.addNew();
        String yem_replace = entryentity.getString("yem_replace");
        //新增
        if (yem_replace.equals("D")) {
            //替换件信息
            subentryentity.set("yem_repno", entryentity.getString("yem_parentno"));
            subentryentity.set("yem_repnoname", entryentity.getString("yem_parentname"));
            subentryentity.set("yem_repnumber", entryentity.getString("yem_subno"));
            subentryentity.set("yem_repname", entryentity.getString("yem_subname"));
            subentryentity.set("yem_repenname", entryentity.getString("yem_subenname"));
            subentryentity.set("yem_repenqty", entryentity.getBigDecimal("yem_qty"));
        } else if (yem_replace.equals("E")) {
            //原零件信息
            subentryentity.set("yem_praonumber", entryentity.getString("yem_parentno"));
            subentryentity.set("yem_praoname", entryentity.getString("yem_parentname"));
            subentryentity.set("yem_onumber", entryentity.getString("yem_subno"));
            subentryentity.set("yem_oname", entryentity.getString("yem_subname"));
            subentryentity.set("yem_oenname", entryentity.getString("yem_subenname"));
            subentryentity.set("yem_oqty", entryentity.getBigDecimal("yem_qty"));
        } else {
            //原零件信息
            subentryentity.set("yem_praonumber", entryentity.getString("yem_parentno"));
            subentryentity.set("yem_praoname", entryentity.getString("yem_parentname"));
            subentryentity.set("yem_onumber", entryentity.getString("yem_subno"));
            subentryentity.set("yem_oname", entryentity.getString("yem_enablesubenname"));
            subentryentity.set("yem_oenname", entryentity.getString("yem_subenname"));
            subentryentity.set("yem_oqty", entryentity.getBigDecimal("yem_qty"));
            //替换件信息
            subentryentity.set("yem_repno", entryentity.getString("yem_enableparno"));
            subentryentity.set("yem_repnoname", entryentity.getString("yem_enableparname"));
            subentryentity.set("yem_repnumber", entryentity.getString("yem_enablesubno"));
            subentryentity.set("yem_repname", entryentity.getString("yem_enablesubname"));
            subentryentity.set("yem_repenname", entryentity.getString("yem_enablesubenname"));
            subentryentity.set("yem_repenqty", entryentity.getString("yem_enablesubqty"));
        }
        subentryentity.set("yem_replace", entryentity.getString("yem_replace"));
        subentryentity.set("yem_completetime", entryentity.getDate("yem_completetime"));
        subentryentity.set("yem_replacedate", new Date());
        subentryentity.set("yem_statedate", entryentity.getDate("yem_enableddate"));
        subentryentity.set("yem_downdate", entryentity.getDate("yem_downdate"));
        subentryentity.set("yem_technology", new Date());
        subentryentity.set("yem_remark", entryentity.getString("yem_remark"));
        subentryentity.set("yem_operator", YEM.getCurrentUserId());
        subentryentity.set("yem_sourcetypeen", "yem_em_bomchange");
        subentryentity.set("yem_trackingno", bill.getString("number"));
        subentryentity.set("yem_trackingid", bill.getPkValue());
        subentryentity.set("yem_sourceid", entryentity.getLong("id"));
        //重新赋值行号
        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",
                billObj.getDynamicObjectType().getName(),
                new DynamicObject[]{billObj}, OperateOption.create());
        if (Operate.isSuccess()) {
            //总成
            boolean yem_isass = entryentity.getBoolean("yem_isass");
            if (yem_isass) {
                AtlasUtils.Upassatlasdet(bill, entryentity, "Audit");
            }
            //资源
            boolean yem_isresource = entryentity.getBoolean("yem_isresource");
            if (yem_isresource) {
//                AtlasUtils.Upassatlasdet(bill, entryentity, "Audit");
                SubRelationUtils.BomUpdateCMMPTable(bill, entryentity, "Audit");
            }
        } else {
            List<IOperateInfo> allErrorOrValidateInfo = Operate.getAllErrorOrValidateInfo();
            String message = Operate.getMessage();
            throw new KDBizException("保存《替换关系》失败，失败信息为：," + message + "，" + allErrorOrValidateInfo);
        }
    }

    /**
     * BOM反审核删除生成的替换关系
     *
     * @param bill        表头数据包
     * @param entryentity 分录行数据包
     * @param replace     查询到的《替换关系数》数据包
     */
    public static void Deletereplace(DynamicObject bill, DynamicObject entryentity, DynamicObject replace) {
        DynamicObjectCollection yem_repentryentity = replace.getDynamicObjectCollection("yem_entryentity");
        if (yem_repentryentity.size() > 0) {
            if (yem_repentryentity.size() == 1) {
                //反审核
                OperationResult unAudit = OperationServiceHelper.executeOperate("unaudit", "yem_em_replace", new Object[]{replace.getLong("id")}, OperateOption.create());
                if (unAudit.isSuccess()) {
                    OperationResult delete = OperationServiceHelper.executeOperate("delete", "yem_em_replace", new Object[]{replace.getLong("id")}, OperateOption.create());
                    if (!delete.isSuccess()) {
                        String message = delete.getMessage();
                        List<IOperateInfo> allErrorOrValidateInfo = delete.getAllErrorOrValidateInfo();
                        throw new KDBizException(String.format("《替换关系》删除失败，%s，%s", message, allErrorOrValidateInfo));
                    } else {
                        boolean yem_isass = entryentity.getBoolean("yem_isass");
                        if (yem_isass) {
                            AtlasUtils.Upassatlasdet(bill, entryentity, "UnAudit");
                        }
                        boolean yem_isresource = entryentity.getBoolean("yem_isresource");
                        if (yem_isresource) {
                            SubRelationUtils.BomUpdateCMMPTable(bill, entryentity, "UnAudit");
                        }
                    }
                } else {
                    String message = unAudit.getMessage();
                    List<IOperateInfo> allErrorOrValidateInfo = unAudit.getAllErrorOrValidateInfo();
                    throw new KDBizException(String.format("《替换关系》删除失败，%s，%s", message, allErrorOrValidateInfo));
                }
            } else {
                for (int j = 0; j < yem_repentryentity.size(); j++) {
                    DynamicObject dynamicObject = yem_repentryentity.get(j);
                    String yem_sourceid = dynamicObject.getString("yem_sourceid");
                    if (yem_sourceid.equals(entryentity.getString("id"))) {
                        yem_repentryentity.remove(j--);
                        break;
                    }
                }
                //重新赋值行号
                for (int i = 0; i < yem_repentryentity.size(); i++) {
                    DynamicObject object = yem_repentryentity.get(i);
                    object.set("seq", i + 1);
                }
                OperationResult Operate = OperationServiceHelper.executeOperate("save", replace.getDynamicObjectType().getName(),
                        new DynamicObject[]{replace}, OperateOption.create());
                if (Operate.isSuccess()) {
                    //更新产品资源明细是否替换
                    boolean yem_isass = entryentity.getBoolean("yem_isass");
                    if (yem_isass) {
                        AtlasUtils.Upassatlasdet(bill, entryentity, "UnAudit");
                    }
                    boolean yem_isresource = entryentity.getBoolean("yem_isresource");
                    if (yem_isresource) {
                        SubRelationUtils.BomUpdateCMMPTable(bill, entryentity, "UnAudit");
                    }
                } else {
                    List<IOperateInfo> allErrorOrValidateInfo = Operate.getAllErrorOrValidateInfo();
                    String message = Operate.getMessage();
                    throw new KDBizException("保存《替换关系》失败，失败信息为：," + message + "，" + allErrorOrValidateInfo);
                }
            }
        }
    }

    /**
     * 根据子件编码  查询替换关系中的替换零件编码
     * 如查询到数据
     * 分录插入一条数据  并更新 替换件编码
     *
     * @param subno 子件编码 作废
     */
    public static void Selectsubstitution(String subno, DynamicObject bill, DynamicObject entryentity, String tpye, String entityName) {
        QFilter qFilter = new QFilter("yem_tnumber", QCP.equals, subno);//替换零件编码
        qFilter.and(new QFilter("yem_replace1", QCP.not_equals, "C"));
        qFilter.and(new QFilter("yem_parentno", QCP.equals, entryentity.getString("yem_parentno")));
        DynamicObject[] yem_em_substitution = BusinessDataServiceHelper.load(entityName, "id"
                , new QFilter[]{qFilter}, null);
        if (yem_em_substitution.length > 0) {
            for (int i = 0; i < yem_em_substitution.length; i++) {
                DynamicObject substitution = yem_em_substitution[i];
                //根据id加载所有数据
                DynamicObject component = BusinessDataServiceHelper.loadSingle(substitution.getLong("id"), "yem_em_substitution");
                if (component != null) {
                    //获取表头替换关系
                    String yem_replace1 = component.getString("yem_replace1");
                    if (!yem_replace1.equals("C")) {//A:相互替换  B:新替换旧 C:互不替换
                        //新增明细信息
                        DynamicObjectCollection yem_entryentity = component.getDynamicObjectCollection("yem_entryentity");
                        DynamicObject subentryentity = yem_entryentity.addNew();
                        subentryentity.set("yem_trackingno", bill.getString("number"));
                        subentryentity.set("yem_trackingid", bill.getPkValue());
                        subentryentity.set("yem_sourcetypeen", bill.getDataEntityType().toString());
                        //原零件信息
                        if (tpye.equals("Head")) {
                            subentryentity.set("yem_onumber", entryentity.getString("yem_onumber"));
                            subentryentity.set("yem_oname", entryentity.getString("yem_oname"));
                            subentryentity.set("yem_oenname", "");
                            subentryentity.set("yem_oqty", entryentity.getBigDecimal("yem_oqty"));
                            //替换件信息
                            subentryentity.set("yem_repnumber", entryentity.getString("yem_repnumber"));
                            subentryentity.set("yem_repname", entryentity.getString("yem_repname"));
                            subentryentity.set("yem_repenname", "");
                            subentryentity.set("yem_repenqty", entryentity.getString("yem_repenqty"));
                            subentryentity.set("yem_replace", entryentity.getString("yem_replace"));
                            subentryentity.set("yem_replacedate", new Date());
                            subentryentity.set("yem_technology", new Date());
                            subentryentity.set("yem_operator", YEM.getCurrentUserId());
                            //更改表头替换件信息
                            component.set("yem_tnumber", bill.getString("yem_tnumber"));
                            component.set("yem_tname", bill.getString("yem_tname"));
                            component.set("yem_tenname", bill.getString("yem_tenname"));
                            component.set("yem_tqty", bill.getString("yem_tqty"));
                        } else {
                            subentryentity.set("yem_onumber", entryentity.getString("yem_subno"));
                            subentryentity.set("yem_oname", entryentity.getString("yem_subname"));
                            subentryentity.set("yem_oenname", "");
                            subentryentity.set("yem_oqty", entryentity.getBigDecimal("yem_qty"));
                            //替换件信息
                            subentryentity.set("yem_repnumber", entryentity.getString("yem_enablesubno"));
                            subentryentity.set("yem_repname", entryentity.getString("yem_enablesubname"));
                            subentryentity.set("yem_repenname", "");
                            subentryentity.set("yem_repenqty", entryentity.getString("yem_enablesubqty"));
                            subentryentity.set("yem_replace", entryentity.getString("yem_replace"));
                            subentryentity.set("yem_replacedate", new Date());
                            subentryentity.set("yem_technology", new Date());
                            subentryentity.set("yem_operator", YEM.getCurrentUserId());
                            //更改表头替换件信息
                            component.set("yem_tnumber", entryentity.getString("yem_enablesubno"));
                            component.set("yem_tname", entryentity.getString("yem_enablesubname"));
                            component.set("yem_tenname", "");//entryentity.getString("yem_repenname")
                            component.set("yem_tqty", entryentity.getString("yem_enablesubqty"));
                        }
                        //保存
                        SaveServiceHelper.save(new DynamicObject[]{component});
                    }
                }
            }
        }
    }


    /**
     * BOM变更单根据父件编码和子件编码更新子件
     * 是否替换  是否删除 是否新增
     *
     * @param bill        单据对象
     * @param entryentity 单据行对象
     */
    public static void Upassatlasdet(DynamicObject bill, DynamicObject entryentity, String Optype) {
        boolean yem_isass = entryentity.getBoolean("yem_isass");
        if (yem_isass) {
            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_assatlasdet",
                    "id,number,name,yem_isreplace", qFilter.toArray());
            if (Object != null) {
                DynamicObject yem_em_assatlasdet = BusinessDataServiceHelper.loadSingle(Object.getLong("id"), "yem_em_assatlasdet");
                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();
                            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());
                            subentryentity.set("yem_partasspartlib", yem_partasspartlib);
                            subentryentity.set("yem_partnumber", entryentity.getString("yem_subno"));
                            subentryentity.set("yem_partname", entryentity.getString("yem_subname"));
                            if (yem_partasspartlib != null) {
                                subentryentity.set("yem_partmaterielmodel", yem_partasspartlib.getString("modelnum"));
                                subentryentity.set("yem_unit", yem_partasspartlib.getDynamicObject("baseunit"));
                            }
                            subentryentity.set("yem_partenname", entryentity.getString("yem_subenname"));
                            subentryentity.set("yem_partqty", entryentity.getBigDecimal("yem_qty"));
                            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());
                            int yem_enableparno = SubRelationUtils.Getatlasno(entryentity.getString("yem_enableparno"));
                            subentryentity.set("yem_atlasno", yem_enableparno + 1);
                        } 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_partnumber");
                                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_partnumber").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", "");
                                    }
                                } else {
                                    if ("Audit".equals(Optype)) {
                                        DynamicObject subentryentity = yem_entryentity.addNew();
                                        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());
                                        subentryentity.set("yem_partasspartlib", yem_partasspartlib);
                                        subentryentity.set("yem_partnumber", entryentity.getString("yem_enablesubno"));
                                        subentryentity.set("yem_partname", entryentity.getString("yem_enablesubname"));
                                        if (yem_partasspartlib != null) {
                                            subentryentity.set("yem_partmaterielmodel", yem_partasspartlib.getString("modelnum"));
                                            subentryentity.set("yem_unit", yem_partasspartlib.getDynamicObject("baseunit"));
                                        }
                                        subentryentity.set("yem_partenname", entryentity.getString("yem_enablesubenname"));
                                        subentryentity.set("yem_partqty", entryentity.getBigDecimal("yem_enablesubqty"));
                                        subentryentity.set("yem_creationtime", new Date());
                                        subentryentity.set("yem_atlasno", entryObject.getString("yem_atlasno"));

                                        entryObject.set("yem_replace", entryentity.getString("yem_replace"));
                                        entryObject.set("yem_completetime", entryentity.getDate("yem_completetime"));
                                        entryObject.set("yem_partisreplace", true);
                                        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_partnumber");
                                            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;
                            }
                        }
                    }
                    //重新赋值行号
                    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()) {

                    }
                }
            }
        }
    }

    /**
     * 替换关系 根据父件编码和子件编码更新子件
     * 是否替换  是否删除 是否新增
     *
     * @param bill        单据对象
     * @param entryentity 单据行对象
     */
    public static void Uprepassatlasdet(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_assatlasdet",
                "id,number,name,yem_isreplace", qFilter.toArray());
        if (Object != null) {
            DynamicObject yem_em_assatlasdet = BusinessDataServiceHelper.loadSingle(Object.getLong("id"), "yem_em_assatlasdet");
            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();
                        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());
                        subentryentity.set("yem_partasspartlib", yem_partasspartlib);
                        subentryentity.set("yem_partnumber", entryentity.getString("yem_repnumber"));
                        subentryentity.set("yem_partname", entryentity.getString("yem_repname"));
                        if (yem_partasspartlib != null) {
                            subentryentity.set("yem_partmaterielmodel", yem_partasspartlib.getString("modelnum"));
                            subentryentity.set("yem_unit", yem_partasspartlib.getDynamicObject("baseunit"));
                        }

                        subentryentity.set("yem_partenname", entryentity.getString("yem_repenname"));
                        subentryentity.set("yem_partqty", entryentity.getBigDecimal("yem_repenqty"));
                        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());
                        int yem_enableparno = SubRelationUtils.Getatlasno(entryentity.getString("yem_repno"));
                        subentryentity.set("yem_atlasno", yem_enableparno + 1);

                    } 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);
                        if (entryObject.getString("yem_partnumber").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();
                                    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());
                                    subentryentity.set("yem_partasspartlib", yem_partasspartlib);
                                    subentryentity.set("yem_partnumber", entryentity.getString("yem_repnumber"));
                                    subentryentity.set("yem_partname", entryentity.getString("yem_repname"));
                                    if (yem_partasspartlib != null) {
                                        subentryentity.set("yem_partmaterielmodel", yem_partasspartlib.getString("modelnum"));
                                        subentryentity.set("yem_unit", yem_partasspartlib.getDynamicObject("baseunit"));
                                    }
                                    subentryentity.set("yem_partenname", entryentity.getString("yem_repenname"));
                                    subentryentity.set("yem_partqty", entryentity.getBigDecimal("yem_repenqty"));
                                    subentryentity.set("yem_creationtime", new Date());
                                    subentryentity.set("yem_atlasno", entryObject.getString("yem_atlasno"));

                                    entryObject.set("yem_replace", entryentity.getString("yem_replace"));
                                    entryObject.set("yem_completetime", entryentity.getDate("yem_completetime"));
                                    entryObject.set("yem_partisreplace", true);
                                    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_partnumber");
                                        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()) {

                }
            }
        }
    }

    /**
     * 根据子件编码  查询替换关系中的替换零件编码
     * 如查询到数据
     * 分录插入一条数据  并更新 替换件编码
     *
     * @param subno 子件编码
     */
    public static void SelectCompSub(String subno, DynamicObject bill, DynamicObject entryentity, String tpye) {
        QFilter qFilter = new QFilter("yem_tnumber", QCP.equals, subno);//替换零件编码
        qFilter.and(new QFilter("yem_replace1", QCP.not_equals, "C"));
        DynamicObject[] yem_em_substitution = BusinessDataServiceHelper.load("yem_em_compsub", "id"
                , new QFilter[]{qFilter}, null);
        if (yem_em_substitution.length > 0) {
            for (int i = 0; i < yem_em_substitution.length; i++) {
                DynamicObject substitution = yem_em_substitution[i];
                //根据id加载所有数据
                DynamicObject component = BusinessDataServiceHelper.loadSingle(substitution.getLong("id"), "yem_em_compsub");
                if (component != null) {
                    //获取表头替换关系
                    String yem_replace1 = component.getString("yem_replace1");
                    if (!yem_replace1.equals("C")) {//A:相互替换  B:新替换旧 C:互不替换
                        //新增明细信息
                        DynamicObjectCollection yem_entryentity = component.getDynamicObjectCollection("yem_entryentity");
                        DynamicObject subentryentity = yem_entryentity.addNew();
                        subentryentity.set("yem_trackingno", bill.getString("number"));
                        subentryentity.set("yem_trackingid", bill.getPkValue());
                        subentryentity.set("yem_sourcetypeen", bill.getDataEntityType().toString());
                        //原零件信息
                        if (tpye.equals("Head")) {
                            subentryentity.set("yem_onumber", entryentity.getString("yem_onumber"));
                            subentryentity.set("yem_oname", entryentity.getString("yem_oname"));
                            subentryentity.set("yem_oenname", entryentity.getString("yem_oenname"));
                            subentryentity.set("yem_oqty", entryentity.getBigDecimal("yem_oqty"));
                            //替换件信息
                            subentryentity.set("yem_repnumber", entryentity.getString("yem_repnumber"));
                            subentryentity.set("yem_repname", entryentity.getString("yem_repname"));
                            subentryentity.set("yem_repenname", entryentity.getString("yem_repenname"));
                            subentryentity.set("yem_repenqty", entryentity.getString("yem_repenqty"));
                            subentryentity.set("yem_replace", entryentity.getString("yem_replace"));
                            subentryentity.set("yem_replacedate", new Date());
                            subentryentity.set("yem_technology", new Date());
                            subentryentity.set("yem_operator", YEM.getCurrentUserId());
                            subentryentity.set("yem_remark", entryentity.getString("yem_remark"));
                            //更改表头替换件信息
                            component.set("yem_tnumber", bill.getString("yem_tnumber"));
                            component.set("yem_tname", bill.getString("yem_tname"));
                            component.set("yem_tenname", bill.getString("yem_tenname"));
                            component.set("yem_tqty", bill.getString("yem_tqty"));
                        } else {
                            subentryentity.set("yem_onumber", entryentity.getString("yem_subno"));
                            subentryentity.set("yem_oname", entryentity.getString("yem_subname"));
                            subentryentity.set("yem_oenname", "");
                            subentryentity.set("yem_oqty", entryentity.getBigDecimal("yem_qty"));
                            //替换件信息
                            subentryentity.set("yem_repnumber", entryentity.getString("yem_enablesubno"));
                            subentryentity.set("yem_repname", entryentity.getString("yem_enablesubname"));
                            subentryentity.set("yem_repenname", entryentity.getString("yem_repenname"));
                            subentryentity.set("yem_repenqty", entryentity.getString("yem_enablesubqty"));
                            subentryentity.set("yem_replace", entryentity.getString("yem_replace"));
                            subentryentity.set("yem_replacedate", new Date());
                            subentryentity.set("yem_technology", new Date());
                            subentryentity.set("yem_operator", YEM.getCurrentUserId());
                            //更改表头替换件信息
                            component.set("yem_tnumber", entryentity.getString("yem_repnumber"));
                            component.set("yem_tname", entryentity.getString("yem_repname"));
                            component.set("yem_tenname", entryentity.getString("yem_repenname"));
                            component.set("yem_tqty", entryentity.getString("yem_repenqty"));
                        }
                        //保存
                        SaveServiceHelper.save(new DynamicObject[]{component});
                    }
                }
            }
        }
    }

    /**
     * 新增配件图册分组单据
     *
     * @param object 数据集合
     */
    public static void Addelectronicgroup(DynamicObject object, String type) {
        DynamicObject dynamicObject = BusinessDataServiceHelper.newDynamicObject("yem_em_electronicgroup");
        dynamicObject.set("status", "C");
        dynamicObject.set("enable", "1");

        dynamicObject.set("number", object.getString("number"));
        dynamicObject.set("name", object.getString("name"));
        if (type.equals("productsgroup")) {
            dynamicObject.set("yem_businesssort", object.getString("yem_businesssort"));
            DynamicObject getparent = getparent(object.getString("parent.number"));
            dynamicObject.set("parent", getparent);//上级
            if (getparent == null) {
                dynamicObject.set("level", "1");//上级
            } else {
                //获取级次
                dynamicObject.set("level", getparent.getString("level"));
            }
            dynamicObject.set("yem_sourcetype", "yem_bd_productsgroup");
        } else {
            dynamicObject.set("yem_businesssort", object.getString("yem_classification"));
            DynamicObject getparent = getparent(object.getString("group.number"));
            dynamicObject.set("parent", getparent);//上级
            if (getparent == null) {
                dynamicObject.set("level", "1");//上级
            } else {
                //获取级次
                dynamicObject.set("level", getparent.getString("level"));
            }
            dynamicObject.set("yem_sourcetype", "yem_bd_productstree");
        }
        dynamicObject.set("yem_sourceid", object.getString("id"));
        OperationResult Operate = OperationServiceHelper.executeOperate("save", dynamicObject.getDynamicObjectType().getName(), new DynamicObject[]{dynamicObject}, OperateOption.create());
        if (Operate.isSuccess()) {

        }
    }

    /**
     * 获取上级基础资料
     *
     * @param number 上级编码
     * @return long
     */
    public static DynamicObject getparent(String number) {
        DynamicObject id = null;
        if (number != null && number != "") {
            QFilter qFilter = new QFilter("number", QCP.equals, number);//替换零件编码
            DynamicObject[] yem_em_substitution = BusinessDataServiceHelper.load("yem_em_electronicgroup", "id"
                    , new QFilter[]{qFilter}, null);
            if (yem_em_substitution.length > 0) {
                DynamicObject dynamicObject = yem_em_substitution[0];
                if (dynamicObject != null) {
                    id = BusinessDataServiceHelper.loadSingle(dynamicObject.getLong("id"), "yem_em_electronicgroup");
                }
            }
        }

        return id;
    }


    /**
     * 拼接查询字段
     *
     * @return 拼接字符串
     */
    public static String Queryfield(String... keys) {
        String result = null;
        List<String> list = new ArrayList<>();
        String[] var2 = keys;
        for (int var4 = 0; var4 < keys.length; ++var4) {
            String key = var2[var4];
            list.add(key);
        }
        result = String.join(",", list);
        return result;
    }

    /**
     * 测试CMMP返回数据暂时写死
     *
     * @return
     */
    public static List<Map> Getasscomponent() {
        ArrayList<Map> list = new ArrayList<>();
        List<String> jglist = new ArrayList<>();
        jglist.add("55642B00000000");
        jglist.add("56424A00000000");
        jglist.add("56426A00000000");
        jglist.add("56428A00000000");
        jglist.add("56429A00000000");
        jglist.add("56434B00000000");
        for (int i = 0; i < jglist.size(); i++) {
            String s = jglist.get(i);
            Map map = new HashMap<>();
            map.put("ccode", s);
            list.add(map);
        }
        return list;
    }

    /**
     * 根据部件编码获取部件名称
     * 查询总成图册部件
     *
     * @param Getasscomponent CMMP查询listmap集合
     * @param Type            查询类型 Ass 总成部件  Com 图册部件
     * @return
     */
    public static Map<String, String> GetasscomponentName(List<Map> Getasscomponent, String Type) {
        Map remap = new HashMap<>();
        List list = new ArrayList<>();
//        List<Map> fromCmmp = AtlasUtils.Getasscomponent();
        for (int i = 0; i < Getasscomponent.size(); i++) {
            Map map = Getasscomponent.get(i);
            if (map != null) {
                String ccode = map.get("ccode").toString();
                list.add(ccode);
            }
        }
        if (list.size() > 0) {
            QFilter qFilter = new QFilter("number", QCP.in, list);
            DynamicObject[] yem_em_asscomponent = null;
            if (Type.equals("Ass")) {
                yem_em_asscomponent = BusinessDataServiceHelper.load("bd_material",
                        "id,number,name",
                        new QFilter[]{qFilter}, null);
            }
            if (Type.equals("Com")) {
                yem_em_asscomponent = BusinessDataServiceHelper.load("yem_em_component",
                        "id,number,name,parent,level,longnumber",
                        new QFilter[]{qFilter}, null);
            }
            for (int k = 0; k < yem_em_asscomponent.length; k++) {
                DynamicObject component = yem_em_asscomponent[k];
                remap.put(component.getString("number"), component.getString("name"));
            }
        }
        return remap;
    }

    /**
     * 根据编码获取总成图册部件id
     *
     * @param list 总成图册部件编码集合
     * @return
     */
    public static Map<String, String> Getcomponentid(List list) {
        Map remap = new HashMap<>();
        if (list.size() > 0) {
            QFilter qFilter = new QFilter("number", QCP.in, list);
//            DynamicObject[] yem_em_asscomponent = BusinessDataServiceHelper.load("yem_em_asscomponent",
//                    "id,number,name,parent,level,longnumber",
            DynamicObject[] yem_em_asscomponent = BusinessDataServiceHelper.load("bd_material",
                    "id,number,name",
                    new QFilter[]{qFilter}, null);
            for (int k = 0; k < yem_em_asscomponent.length; k++) {
                DynamicObject component = yem_em_asscomponent[k];
                remap.put(component.getString("number"), component.getString("id"));
            }
        }
        return remap;
    }

    /**
     * 根据编码获取总成图册部件id
     *
     * @param list 总成图册部件编码集合
     * @return
     */
    public static Map<String, String> Getassatlasdet(List list) {
        Map remap = new HashMap<>();
        if (list.size() > 0) {
            QFilter qFilter = new QFilter("number", QCP.in, list);
            DynamicObject[] yem_em_asscomponent = BusinessDataServiceHelper.load("yem_em_assatlasdet",
                    "id,number,name",
                    new QFilter[]{qFilter}, null);
            for (int k = 0; k < yem_em_asscomponent.length; k++) {
                DynamicObject component = yem_em_asscomponent[k];
                remap.put(component.getString("number"), component.getString("id"));
            }
        }
        return remap;
    }

    /**
     * 获取替换部件信息
     *
     * @param yem_pnumber 原编码
     * @return
     */
    public static Map<String, String> GetReplace(String yem_pnumber) {
        Map remap = new HashMap<>();
        QFilter qFilter = new QFilter("yem_pnumber", QCP.equals, yem_pnumber);
        qFilter.and(new QFilter("yem_replace1", QCP.not_equals, "C"));
        qFilter.and(new QFilter("status", QCP.equals, "C"));
        DynamicObject yem_em_compsub = BusinessDataServiceHelper.loadSingle("yem_em_compsub",
                "id,yem_tnumber,yem_tname,yem_tenname,yem_tqty", qFilter.toArray());
        if (yem_em_compsub != null) {
            remap.put("yem_pnumber", yem_em_compsub.getString("yem_tnumber"));
            remap.put("name", yem_em_compsub.getString("yem_tname"));
            remap.put("yem_enname", yem_em_compsub.getString("yem_tenname"));
            remap.put("yem_qtytext", yem_em_compsub.getString("yem_tqty"));
        }
        return remap;
    }

    /**
     * 根据员编码更新【是否替换】
     *
     * @param yem_pnumber 原编码
     * @param type        Com部件    Par零件
     */
    public static void UpReplaceStatus(String yem_pnumber, String type, String Optype) {
        String entityName = null;
        if ("Com".equals(type)) {
            QFilter qFilter = new QFilter("number", QCP.equals, yem_pnumber);
            DynamicObject Object = BusinessDataServiceHelper.loadSingle("yem_em_assatlasdet",
                    "id,number,name,yem_isreplace", qFilter.toArray());
            if (Object != null) {
                if ("Audit".equals(Optype)) {
                    Object.set("yem_isreplace", true);
                }
                if ("UnAudit".equals(Optype)) {
                    Object.set("yem_isreplace", false);
                }
                SaveServiceHelper.save(new DynamicObject[]{Object});
            }
        }
        if ("Par".equals(type)) {
            QFilter qFilter = new QFilter("number", QCP.equals, yem_pnumber);
            DynamicObject[] Object = BusinessDataServiceHelper.load("yem_em_assatlasdet",
                    "id,number,name,yem_isreplace", qFilter.toArray());
            if (Object != null) {
                for (int i = 0; i < Object.length; i++) {
                    DynamicObject dynamicObject = Object[i];
                    if ("Audit".equals(Optype)) {
                        dynamicObject.set("yem_isreplace", true);
                    }
                    if ("UnAudit".equals(Optype)) {
                        dynamicObject.set("yem_isreplace", false);
                    }
                    SaveServiceHelper.save(new DynamicObject[]{dynamicObject});
                }
            }
        }

    }

    /**
     * 根据CMMP返回结果进行BOM变更单新增
     *
     * @param fromCmmp
     */
    public static void addbomchange(List<Map> fromCmmp, String yem_producttype) {
        QFilter qFilter = new QFilter("number", QCP.equals, "110");
        DynamicObject bos_org = BusinessDataServiceHelper.loadSingle("bos_org", "id", qFilter.toArray());
        for (int i = 0; i < fromCmmp.size(); i++) {
            Map map = fromCmmp.get(i);
            if (map != null) {
                String changebill_code = map.get("changebill_code").toString();
//                QFilter qFilter1 = new QFilter("number", QCP.equals, changebill_code);
//                DynamicObject yem_em_bomchange = BusinessDataServiceHelper.loadSingle("yem_em_bomchange", "id", qFilter1.toArray());
//                if (yem_em_bomchange != null) {
//                    DynamicObject bomchange = BusinessDataServiceHelper.loadSingle(yem_em_bomchange.getLong("id"), "yem_em_bomchange");
//                    if (bomchange != null) {
//                        DynamicObjectCollection yem_entryentity = bomchange.getDynamicObjectCollection("yem_entryentity");
//                        if (yem_entryentity.size() == 0) {
//                            DynamicObject dynamicObject = yem_entryentity.addNew();
//                            dynamicObject.set("yem_changeno", changebill_code);//更改单号
//                            dynamicObject.set("yem_parentno", map.get("pcode"));//父件编码
//                            dynamicObject.set("yem_parentname", map.get("mater_name"));//父件名称
//                            dynamicObject.set("yem_subno", map.get("ccode"));//父件编码
//                            dynamicObject.set("yem_subname", map.get("cmater_name"));//子件名称
//                            dynamicObject.set("yem_qty", map.get("qty"));//数量
//                            if (map.get("break_flag").toString().equals("启用")) {
//                                dynamicObject.set("yem_enablesta", "A");//启用停用状态
//                            }
//                            if (map.get("break_flag").toString().equals("停用")) {
//                                dynamicObject.set("yem_enablesta", "B");//启用停用状态
//                            }
//                            dynamicObject.set("yem_enableparno", map.get("pcode"));//替换父件名称
//                            dynamicObject.set("yem_enableparname", map.get("mater_name"));//替换父件名称
//                            dynamicObject.set("yem_enableddate", map.get("enable_time"));//启用时间
//                            dynamicObject.set("yem_downdate", map.get("disable_time"));//停用时间
//                            dynamicObject.set("yem_exestate", map.get("state"));//执行状态
//                            DynamicObject assatlasdet = seelctDyn("yem_em_assatlasdet", "number", map.get("pcode").toString());
//                            if (assatlasdet != null) {
//                                //是否总成 yem_isass
//                                dynamicObject.set("yem_isass", true);
//                            }
//                            DynamicObject basemodelib = seelctDyn("yem_bd_basemodelib", "number", map.get("pcode").toString());
//                            if (basemodelib != null) {
//                                //是否资源 yem_isass
//                                dynamicObject.set("yem_isresource", true);
//                            }
//
//                            dynamicObject.set("yem_completetime", map.get("complete_time"));
//                        } else {
//                            Boolean fag = true;
//                            String code = map.get("pcode").toString() + "_" + map.get("ccode").toString();
//                            for (int j = 0; j < yem_entryentity.size(); j++) {
//                                DynamicObject dynamicObject = yem_entryentity.get(j);
//                                String key = dynamicObject.getString("yem_parentno") + "_" + dynamicObject.getString("yem_subno");
//                                if (key.equals(code)) {
//                                    fag = false;
//                                    break;
//                                }
//                            }
//                            if (fag) {
//                                DynamicObject dynamicObject = yem_entryentity.addNew();
//                                dynamicObject.set("yem_changeno", changebill_code);//更改单号
//                                dynamicObject.set("yem_parentno", map.get("pcode"));//父件编码
//                                dynamicObject.set("yem_parentname", map.get("mater_name"));//父件名称
//                                dynamicObject.set("yem_subno", map.get("ccode"));//父件编码
//                                dynamicObject.set("yem_subname", map.get("cmater_name"));//子件名称
//                                dynamicObject.set("yem_qty", map.get("qty"));//数量
//                                if (map.get("break_flag").toString().equals("启用")) {
//                                    dynamicObject.set("yem_enablesta", "A");//启用停用状态
//                                }
//                                if (map.get("break_flag").toString().equals("停用")) {
//                                    dynamicObject.set("yem_enablesta", "B");//启用停用状态
//                                }
//                                dynamicObject.set("yem_enableparno", map.get("pcode"));//替换父件名称
//                                dynamicObject.set("yem_enableparname", map.get("mater_name"));//替换父件名称
//                                dynamicObject.set("yem_enableddate", map.get("enable_time"));//启用时间
//                                dynamicObject.set("yem_downdate", map.get("disable_time"));//停用时间
//                                dynamicObject.set("yem_exestate", map.get("state"));//执行状态
//                                DynamicObject assatlasdet = seelctDyn("yem_em_assatlasdet", "number", map.get("pcode").toString());
//                                if (assatlasdet != null) {
//                                    //是否总成 yem_isass
//                                    dynamicObject.set("yem_isass", true);
//                                }
//                                DynamicObject basemodelib = seelctDyn("yem_bd_basemodelib", "number", map.get("pcode").toString());
//                                if (basemodelib != null) {
//                                    //是否资源 yem_isass
//                                    dynamicObject.set("yem_isresource", true);
//                                }
//                                dynamicObject.set("yem_completetime", map.get("complete_time"));
//                            }
//                        }
//                    }
//                    OperationResult Operate = OperationServiceHelper.executeOperate("save",
//                            bomchange.getDynamicObjectType().getName(), new DynamicObject[]{bomchange}, OperateOption.create());
//                    if (Operate.isSuccess()) {
//
//                    }
//                } else {
                DynamicObject cmmptable = BusinessDataServiceHelper.newDynamicObject("yem_em_bomchange");
                cmmptable.set("yem_producttype", yem_producttype);
                cmmptable.set("enable", "1");
                cmmptable.set("status", "A");
                cmmptable.set("name", changebill_code);
                cmmptable.set("number", changebill_code);
                cmmptable.set("createorg", bos_org);
                cmmptable.set("useorg", bos_org);
                cmmptable.set("creator", YEM.getCurrentUserId());
                cmmptable.set("yem_date", new Date());
                DynamicObjectCollection yem_entryentity = cmmptable.getDynamicObjectCollection("yem_entryentity");
                DynamicObject dynamicObject = yem_entryentity.addNew();
                dynamicObject.set("yem_changeno", changebill_code);//更改单号
                dynamicObject.set("yem_parentno", map.get("pcode"));//父件编码
                dynamicObject.set("yem_parentname", map.get("mater_name"));//父件名称
                dynamicObject.set("yem_enableparno", map.get("pcode"));//替换父件名称
                dynamicObject.set("yem_enableparname", map.get("mater_name"));//替换父件名称
                dynamicObject.set("yem_subno", map.get("ccode"));//父件编码
                dynamicObject.set("yem_subname", map.get("cmater_name"));//子件名称
                dynamicObject.set("yem_qty", map.get("qty"));//数量
                if (map.get("break_flag").toString().equals("启用")) {
                    dynamicObject.set("yem_enablesta", "A");//启用停用状态
                }
                if (map.get("break_flag").toString().equals("停用")) {
                    dynamicObject.set("yem_enablesta", "B");//启用停用状态
                }
                dynamicObject.set("yem_enableddate", map.get("enable_time"));//启用时间
                dynamicObject.set("yem_downdate", map.get("disable_time"));//停用时间
                dynamicObject.set("yem_exestate", map.get("state"));//执行状态
                DynamicObject assatlasdet = seelctDyn("yem_em_assatlasdet", "number", map.get("pcode").toString());
                if (assatlasdet != null) {
                    //是否总成 yem_isass
                    dynamicObject.set("yem_isass", true);
                }
                DynamicObject basemodelib = seelctDyn("yem_bd_basemodelib", "number", map.get("pcode").toString());
                if (basemodelib != null) {
                    //是否资源 yem_isass
                    dynamicObject.set("yem_isresource", true);
                }
                dynamicObject.set("yem_completetime", map.get("complete_time"));
                OperationResult Operate = OperationServiceHelper.executeOperate("save",
                        cmmptable.getDynamicObjectType().getName(), new DynamicObject[]{cmmptable}, OperateOption.create());
                if (Operate.isSuccess()) {

                }
            }
        }
//        }
    }

    /**
     * 生成图册
     *
     * @param number 资源号
     * @return true false
     */
    public static OperationResult addassequipment(String number) {
        DynamicObject dynamicObject = BusinessDataServiceHelper.newDynamicObject("yem_em_assequipment");
        dynamicObject.set("status", "C");
        dynamicObject.set("enable", "1");
        dynamicObject.set("createorg", seelctDyn("bos_org", "number", "110"));

        QFilter qFilter = new QFilter("number", QCP.equals, number);
        DynamicObject Object = BusinessDataServiceHelper.loadSingle("yem_bd_basemodelib", "id,number,name,yem_products.number,yem_productsgroup", qFilter.toArray());
        dynamicObject.set("name", Object.getString("number"));
        dynamicObject.set("number", number);
        //机型
        dynamicObject.set("yem_brandmodel", seelctDyn("yem_em_electronicgroup", "number", Object.getString("yem_products.number")));
        dynamicObject.set("yem_eqctype", Object.getDynamicObject("yem_productsgroup"));//设备类别
        OperationResult Operate = OperationServiceHelper.executeOperate("save",
                dynamicObject.getDynamicObjectType().getName(), new DynamicObject[]{dynamicObject}, OperateOption.create());
        if (Operate.isSuccess()) {

        }
        return Operate;
    }

    /**
     * 根据条件查询是否包含
     *
     * @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;
    }

    public static List getassatlasdet(String number, Date yem_completetime) {
        List list = new ArrayList<>();
        QFilter qFilter = new QFilter("number", QCP.equals, number);//
        qFilter.and(new QFilter("status", QCP.equals, "C"));
        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_assatlasdet", false);
        String selectfields = DynamicObjectUtil.getEntrySelectfields(selectfield, "yem_em_assatlasdet", "yem_entryentity", false);
        DynamicObjectCollection data = QueryServiceHelper.query("yem_em_assatlasdet",
                selectfields, new QFilter[]{qFilter}, "yem_entryentity.yem_completetime DESC");
        if (data.size() > 0) {
            for (int i = 0; i < data.size(); i++) {
                DynamicObject object = data.get(i);
                String yem_enablesubno = object.getString("yem_entryentity.yem_enablesubno");
                if (yem_enablesubno.length() > 0) {
                    list.add(yem_enablesubno);
                }
            }
        }
        return list;
    }


}
