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

import com.yem.em.utils.AtlasUtils;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
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.SaveServiceHelper;

import java.util.List;

/**
 * @author zhouc
 * @date 2023/10/24 17:00
 * @className SubstitutionUnAuditEdit
 * @description 替换关系【反审核】服务插件
 */
public class SubstitutionUnAuditEdit extends AbstractOperationServicePlugIn {
    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        List<String> keys = e.getFieldKeys();
        keys.add("createorg");
        keys.add("number");
        keys.add("yem_pnumber");
        keys.add("yem_pname");
        keys.add("yem_penname");
        keys.add("yem_pqty");
        keys.add("yem_tnumber");
        keys.add("yem_tname");
        keys.add("yem_tenname");
        keys.add("yem_tqty");
        keys.add("yem_sourcetype");

        keys.add("yem_entryentity.yem_onumber");
        keys.add("yem_entryentity.yem_oname");
        keys.add("yem_entryentity.yem_oenname");
        keys.add("yem_entryentity.yem_oqty");

        keys.add("yem_entryentity.yem_repnumber");
        keys.add("yem_entryentity.yem_repname");
        keys.add("yem_entryentity.yem_repenname");
        keys.add("yem_entryentity.yem_repenqty");
    }

    @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) {
                String yem_sourcetype = bill.getString("yem_sourcetype");
                if (yem_sourcetype.equals("Add")) {
                    QFilter qFilter = new QFilter("yem_entryentity.yem_trackingid", QCP.equals, bill.getString("id"));//替换零件编码
                    qFilter.and(new QFilter("yem_entryentity.yem_sourcetypeen", QCP.equals, "yem_em_substitution"));
                    DynamicObject[] yem_em_substitution = BusinessDataServiceHelper.load("yem_em_substitution", "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) {
                                DynamicObjectCollection yem_entryentity = component.getDynamicObjectCollection("yem_entryentity");
                                for (int j = 0; j < yem_entryentity.size(); j++) {
                                    DynamicObject dynamicObject = yem_entryentity.get(j);
                                    String yem_trackingid = dynamicObject.getString("yem_trackingid");
                                    if (yem_trackingid.equals(bill.getString("id"))) {
                                        yem_entryentity.remove(j--);
                                        break;
                                    }
                                }
                                //更改表头替换件信息
                                component.set("yem_tnumber", bill.getString("yem_pnumber"));
                                component.set("yem_tname", bill.getString("yem_pname"));
                                component.set("yem_tenname", bill.getString("yem_penname"));
                                component.set("yem_tqty", bill.getString("yem_pqty"));
                                //保存
                                SaveServiceHelper.save(new DynamicObject[]{component});
                            }
                        }
                    }
                    AtlasUtils.UpReplaceStatus(bill.getString("yem_pnumber"),"Par","UnAudit");
                }
            }
        }
    }
}
