package com.yem.em.bd.ProductsGroup.op;

import com.yem.em.utils.AtlasUtils;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.AddValidatorsEventArgs;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.EndOperationTransactionArgs;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.util.List;

/**
 * @author zhouc
 * @date 2023/10/25 11:18
 * @className ProductsGroupAudit
 * @description 插入配件图册分类
 */
public class ProductsGroupAudit extends AbstractOperationServicePlugIn {
    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        List<String> keys = e.getFieldKeys();
        keys.add("number");
        keys.add("name");
        keys.add("yem_businesssort");
        keys.add("group");
        keys.add("parent");
        keys.add("longnumber");
        keys.add("fullname");
        keys.add("yem_businesssort");
        keys.add("yem_classification");
        keys.add("level");
    }

    @Override
    public void onAddValidators(AddValidatorsEventArgs e) {
        super.onAddValidators(e);
//        e.addValidator(new BomChangeAuditvalidatorEdit());
    }

    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        super.endOperationTransaction(e);
        if (e.getDataEntities() != null && e.getDataEntities().length != 0) {
            DynamicObject[] bills = e.getDataEntities();
            for (DynamicObject bill : bills) {
                //产品类型 getproductsgroup
                String type = null;
                String yem_sourcetype = null;
                String businesssort = null;
                String fromid = bill.getDataEntityType().getName();
                if (fromid.equals("yem_bd_productsgroup")) {
                    type = "productsgroup";
                    businesssort = "yem_businesssort";
                }
                if (fromid.equals("yem_bd_productstree")) {
                    type = "productstree";
                    businesssort = "yem_classification";
                }
                String yem_businesssort = bill.getString(businesssort);
                if (yem_businesssort.equals("Z")) {
                    String number = bill.getString("number");
                    QFilter qFilter = new QFilter("yem_sourceid", QCP.equals, bill.getString("id"));//
                    qFilter.and(new QFilter("enable", QCP.equals, "1"));
                    qFilter.and(new QFilter("yem_sourcetype", QCP.equals, fromid));
                    DynamicObject[] yem_em_electronicgroup = BusinessDataServiceHelper.load("yem_em_electronicgroup", "id"
                            , new QFilter[]{qFilter}, null);
                    if (yem_em_electronicgroup.length > 0) {
                        //更新
                        DynamicObject dynamicObject = yem_em_electronicgroup[0];
                        DynamicObject component = BusinessDataServiceHelper.loadSingle(dynamicObject.getLong("id"), "yem_em_electronicgroup");
                        component.set("number", number);
                        component.set("name", bill.getString("name"));
                        component.set("yem_businesssort", yem_businesssort);//业务分类
                        if (fromid.equals("yem_bd_productsgroup")) {
                            component.set("longnumber", bill.getString("longnumber"));//级
                            component.set("fullname", bill.getString("fullname"));//级
                            DynamicObject parent = bill.getDynamicObject("parent");
                            if (parent != null) {
                                QFilter qFilter1 = new QFilter("number", QCP.equals, parent.getString("number"));//
                                DynamicObject electronicgroup = BusinessDataServiceHelper.loadSingle("yem_em_electronicgroup",
                                        "id,name,number,level", new QFilter[]{qFilter1});
                                if (electronicgroup != null) {
                                    component.set("parent", electronicgroup);//上级
                                }
                            }
                        }
                        if (fromid.equals("yem_bd_productstree")) {
                            DynamicObject group = bill.getDynamicObject("group");
                            if (group != null) {
                                QFilter qFilter1 = new QFilter("number", QCP.equals, group.getString("number"));//
                                DynamicObject electronicgroup = BusinessDataServiceHelper.loadSingle("yem_em_electronicgroup",
                                        "id,name,number,level", new QFilter[]{qFilter1});
                                if (electronicgroup != null) {
                                    component.set("parent", electronicgroup);//上级
                                }
                            }
                        }
//                        component.set("level", bill.getString("level"));
                        OperationResult Operate = OperationServiceHelper.executeOperate("save",
                                component.getDynamicObjectType().getName(), new DynamicObject[]{component}, OperateOption.create());
                        if (Operate.isSuccess()) {

                        }
                    } else {
                        AtlasUtils.Addelectronicgroup(bill, type);
                    }
                }
            }
        }
    }
}
