package com.yem.em.ass.assatlasdet.op;


import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
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.SaveServiceHelper;

import java.util.List;

/**
 * @author zhouc
 * @date 2023/11/2 18:14
 * @className AssAtlasdetUnAuditOpEdit
 * @description 总成图册明细【反审核】服务插件
 */
public class AssAtlasdetUnAuditOpEdit extends AbstractOperationServicePlugIn {
    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        List<String> keys = e.getFieldKeys();
        keys.add("createorg");
        keys.add("number");
        keys.add("name");
        keys.add("yem_issup");
        keys.add("yem_entryentity.yem_partnumber");
        keys.add("yem_entryentity.yem_iscomp");
        keys.add("yem_entryentity.yem_compid");
//        keys.add("yem_entryentity.yem_iscomp");
    }

    public void endOperationTransaction(EndOperationTransactionArgs e) {
        super.endOperationTransaction(e);
        if (e.getDataEntities() != null && e.getDataEntities().length != 0) {
            DynamicObject[] bills = e.getDataEntities();
            for (DynamicObject bill : bills) {
                String number = bill.getString("number");
                QFilter qFilter = new QFilter("enable", QCP.equals, "1");//
                qFilter.and(new QFilter("yem_entryentity.yem_partnumber", QCP.equals, number));//
                DynamicObject[] yem_em_assatlasdet = BusinessDataServiceHelper.load("yem_em_assatlasdet", "id"
                        , new QFilter[]{qFilter}, null);
                if (yem_em_assatlasdet.length > 0) {
                    bill.set("yem_issup", false);
                    for (int i = 0; i < yem_em_assatlasdet.length; i++) {
                        DynamicObject assatlasdet = yem_em_assatlasdet[i];
                        //根据id加载所有数据
                        DynamicObject component = BusinessDataServiceHelper.loadSingle(assatlasdet.getLong("id"), "yem_em_assatlasdet");
                        DynamicObjectCollection yemEntryentity = component.getDynamicObjectCollection("yem_entryentity");
                        for (int j = 0; j < yemEntryentity.size(); j++) {
                            DynamicObject dynamicObject = yemEntryentity.get(j);
                            String yemPartnumber = dynamicObject.getString("yem_partnumber");
                            if (yemPartnumber.equals(number)) {
                                dynamicObject.set("yem_iscomp", false);
                                dynamicObject.set("yem_compid", null);
                            }
                        }
                        //保存
                        SaveServiceHelper.save(new DynamicObject[]{component});
                    }

                }
                DynamicObjectCollection yem_entryentity = bill.getDynamicObjectCollection("yem_entryentity");
                for (int i = 0; i < yem_entryentity.size(); i++) {
                    DynamicObject object = yem_entryentity.get(i);
                    boolean yem_iscomp = object.getBoolean("yem_iscomp");
                    if (yem_iscomp) {
                        String yem_partnumber = object.getString("yem_partnumber");
                        QFilter qFilter1 = new QFilter("enable", QCP.equals, "1");//
                        qFilter1.and(new QFilter("number", QCP.equals, yem_partnumber));//
                        DynamicObject assatlasdet = BusinessDataServiceHelper.loadSingle("yem_em_assatlasdet", "id,yem_issup,number,name"
                                , new QFilter[]{qFilter1});
                        if (assatlasdet != null) {
                            assatlasdet.set("yem_issup", false);
                            object.set("yem_iscomp", false);
                            object.set("yem_compid", null);
                            SaveServiceHelper.save(new DynamicObject[]{assatlasdet});
                        }
                    }
                }
                SaveServiceHelper.save(new DynamicObject[]{bill});
            }
        }
    }
}
