package com.yem.wm.bd.basemodelib.op;

import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
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;

/**
 * @author zhouc
 * @date 2024/6/19 10:12
 * @className BasemodeLibOp
 * @description 产品资源明细【审核】服务插件
 */
public class BasemodeLibOp extends AbstractOperationServicePlugIn {
    @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) {
                bill = BusinessDataServiceHelper.loadSingle(bill.getPkValue(), bill.getDynamicObjectType().getName());
                boolean yem_iscmmp = bill.getBoolean("yem_iscmmp");
                if (!yem_iscmmp) {
                    String number = bill.getString("number");
                    String name = bill.getString("name");
                    DynamicObject yem_productsgroup = bill.getDynamicObject("yem_productsgroup");
                    if (yem_productsgroup != null) {
                        String yem_producttype = "";
                        String productsgroupno = yem_productsgroup.getString("number");
                        if (productsgroupno.contains("ZZJ")) {
                            yem_producttype = "ZZJ";
                        }
                        if (productsgroupno.contains("CC")) {
                            yem_producttype = "CC";
                        }
                        if (productsgroupno.contains("TLJ")) {
                            yem_producttype = "TLJ";
                        }

                        DynamicObject cmmptable = BusinessDataServiceHelper.newDynamicObject("yem_em_cmmptable");
                        cmmptable.set("enable", "1");
                        cmmptable.set("status", "A");
                        cmmptable.set("name", name);
                        cmmptable.set("number", number);
                        QFilter qFilter = new QFilter("number", QCP.equals, "110");
                        DynamicObject bos_org = BusinessDataServiceHelper.loadSingle("bos_org", "id", qFilter.toArray());
                        cmmptable.set("createorg", bos_org);
                        cmmptable.set("yem_producttype", yem_producttype);
                        OperationResult Operate = OperationServiceHelper.executeOperate("save",
                                cmmptable.getDynamicObjectType().getName(), new DynamicObject[]{cmmptable}, OperateOption.create());
                        if (Operate.isSuccess()) {
                            bill.set("yem_iscmmp", true);
                            SaveServiceHelper.update(new DynamicObject[]{bill});
                        }
                    }
                }
            }
        }
    }
}
