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

import com.yem.em.utils.AtlasUtils;
import com.yem.em.ass.cmmptable.validator.CMMPTableAuditvalidator;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.operate.result.IOperateInfo;
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.exception.KDBizException;
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.math.BigDecimal;
import java.util.List;

/**
 * @author zhouc
 * @date 2023/11/2 18:14
 * @className CMMPTableAuditEdit
 * @description CMMP部件明细表【审核】服务插件
 */
public class CMMPTableAuditEdit 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_isassequipment");
        keys.add("yem_entryentity.yem_isnormal");
        keys.add("yem_entryentity.yem_isassatlasdet");
    }

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

    @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 number = bill.getString("number");
                QFilter qFilter = new QFilter("number", QCP.equals, number);
                DynamicObject Object = BusinessDataServiceHelper.loadSingle("yem_em_assequipment", "id,number,name", qFilter.toArray());
                if (Object != null) {
                    boolean yemIsassequipment = bill.getBoolean("yem_isassequipment");
                    if (!yemIsassequipment) {
                        bill.set("yem_isassequipment", true);
                        SaveServiceHelper.save(new DynamicObject[]{bill});
                    }
                } else {
                    QFilter qFilter1 = new QFilter("number", QCP.equals, number);
                    DynamicObject Object1 = BusinessDataServiceHelper.loadSingle("yem_bd_basemodelib",
                            "id,number,name,yem_products.number,yem_productsgroup", qFilter1.toArray());
                    if (Object1 != null) {
                        OperationResult addassequipment = AtlasUtils.addassequipment(number);
                        if (addassequipment.isSuccess()) {
                            bill.set("yem_isassequipment", true);
                            SaveServiceHelper.save(new DynamicObject[]{bill});
                        } else {
                            List<IOperateInfo> allErrorOrValidateInfo = addassequipment.getAllErrorOrValidateInfo();
                            String message = addassequipment.getMessage();
                            throw new KDBizException("保存《配件图册》失败，失败信息为：," + message + "，" + allErrorOrValidateInfo);
                        }
                    } else {
                        throw new KDBizException("未查询到编码为：【" + number + "】的产品资源明细，请维护后重试！");
                    }
                }
            }
        }
    }
}
