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

import com.yem.em.utils.AtlasUtils;
import com.yem.wm.utils.StringUtils;
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 java.util.List;

/**
 * @author zhouc
 * @date 2023/12/25 22:12
 * @className BomChangeNewUnAuditEdit
 * @description 新BOM变更单【反审核】服务插件
 */
public class BomChangeNewUnAuditEdit extends AbstractOperationServicePlugIn {
    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        List<String> keys = e.getFieldKeys();
        keys.add("createorg");
        keys.add("useorg");
        keys.add("number");
        keys.add("yem_completetime");
        keys.add("yem_remark");
        keys.add("yem_entryentity.yem_subno");
        keys.add("yem_entryentity.yem_subname");
        keys.add("yem_entryentity.yem_replace");
        keys.add("yem_entryentity.yem_qty");
        keys.add("yem_entryentity.yem_enablesubno");
        keys.add("yem_entryentity.yem_enablesubname");
        keys.add("yem_entryentity.yem_enablesubqty");
        keys.add("yem_entryentity.yem_subname");
        keys.add("yem_entryentity.yem_enablesta");
        keys.add("yem_entryentity.yem_isass");
        keys.add("yem_entryentity.yem_isresource");
        keys.add("yem_entryentity.yem_parentno");
        keys.add("yem_entryentity.yem_parentname");
        keys.add("yem_entryentity.yem_enableddate");
        keys.add("yem_entryentity.yem_downdate");
        keys.add("yem_entryentity.yem_subenname");
        keys.add("yem_entryentity.yem_enablesubenname");
        keys.add("yem_entryentity.yem_enableparno");
        keys.add("yem_entryentity.yem_enableparname");
    }

    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 id = bill.getString("id");
                DynamicObjectCollection yem_entryentity = bill.getDynamicObjectCollection("yem_entryentity");
                for (int i = 0; i < yem_entryentity.size(); i++) {
                    DynamicObject entryentity = yem_entryentity.get(i);
                    String yem_enablesta = entryentity.getString("yem_enablesta");
                    String yem_enablesubno = entryentity.getString("yem_enablesubno");
                    String yem_replace = entryentity.getString("yem_replace");
                    boolean yem_isass = entryentity.getBoolean("yem_isass");
                    boolean yem_isresource = entryentity.getBoolean("yem_isresource");
                    String yem_parentno = entryentity.getString("yem_parentno");//父件编码
                    String yem_subno = entryentity.getString("yem_subno");//子件编码
                    if (yem_isass || yem_isresource) {
                        if (yem_enablesta.equals("B") && StringUtils.isNotEmpty(yem_enablesubno)) {
                            QFilter qFilter = new QFilter("number", QCP.equals, yem_parentno);//替换零件编码
                            qFilter.and(new QFilter("yem_parno", QCP.equals, yem_subno));
                            qFilter.and(new QFilter("yem_sourcetype", QCP.equals, "BOM"));
                            DynamicObject yem_em_replace = BusinessDataServiceHelper.loadSingle("yem_em_replace", "id"
                                    , new QFilter[]{qFilter});
                            if (yem_em_replace != null) {
                                DynamicObject replace = BusinessDataServiceHelper.loadSingle(yem_em_replace.getLong("id"), "yem_em_replace");
                                if (replace != null) {
                                    AtlasUtils.Deletereplace(bill, entryentity, replace);
                                }
                            }
                        }
                        if (StringUtils.isNotEmpty(yem_replace)) {
                            if (yem_replace.equals("D") || yem_replace.equals("E")) {
                                QFilter qFilter = new QFilter("number", QCP.equals, yem_parentno);//替换零件编码
                                qFilter.and(new QFilter("yem_parno", QCP.equals, yem_subno));
                                qFilter.and(new QFilter("yem_sourcetype", QCP.equals, "BOM"));
                                DynamicObject yem_em_replace = BusinessDataServiceHelper.loadSingle("yem_em_replace", "id"
                                        , new QFilter[]{qFilter});
                                if (yem_em_replace != null) {
                                    DynamicObject replace = BusinessDataServiceHelper.loadSingle(yem_em_replace.getLong("id"), "yem_em_replace");
                                    if (replace != null) {
                                        AtlasUtils.Deletereplace(bill, entryentity, replace);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
