package com.yem.wm.im.delivernotice.op;

import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
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.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.EndOperationTransactionArgs;
import kd.bos.exception.KDBizException;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.util.ArrayList;
import java.util.List;


/**
 * @author zhouc
 * @Description 发货通知单【反审核】删除《整机档案》服务插件
 * @date 11:05
 **/
public class DeliverNoticeAssUnAuditOpEdit extends AbstractOperationServicePlugIn {
    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        List<String> keys = e.getFieldKeys();
        keys.add("yem_billtype");
    }

    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        super.endOperationTransaction(e);
        DynamicObject[] dataEntities = e.getDataEntities();
        for (DynamicObject dataEntitie : dataEntities) {
            dataEntitie = BusinessDataServiceHelper.loadSingle(dataEntitie.getPkValue(), dataEntitie.getDynamicObjectType().getName());
            DynamicObject yemBilltype = dataEntitie.getDynamicObject("yem_billtype");
            if (yemBilltype != null) {
                if (yemBilltype.getString("number").equals("yem_es_delivernotice_zj")) {
                    delDeliverNotice(dataEntitie);
                }
            }
        }
    }

    /**
     * 删除整机档案
     *
     * @param dataEntitie
     */
    public void delDeliverNotice(DynamicObject dataEntitie) {
        List list = new ArrayList<>();
        DynamicObjectCollection yem_es_materialinfo = dataEntitie.getDynamicObjectCollection("yem_es_materialinfo");
        for (int i = 0; i < yem_es_materialinfo.size(); i++) {
            DynamicObject materialinfo = yem_es_materialinfo.get(i);
            DynamicObjectCollection yem_es_salesorder_z = materialinfo.getDynamicObjectCollection("yem_es_salesorder_z");
            for (int j = 0; j < yem_es_salesorder_z.size(); j++) {
                DynamicObject salesorder_z = yem_es_salesorder_z.get(j);
                long yemDevicefilesid = salesorder_z.getLong("yem_devicefilesid");
                if (yemDevicefilesid != 0) {
                    list.add(yemDevicefilesid);
                }

            }
        }
        if (list.size() > 0) {
            Object[] objects = list.toArray(new Object[list.size()]);
            OperationResult unAudit = OperationServiceHelper.executeOperate("unaudit", "yem_em_devicefiles",
                    objects, OperateOption.create());
            if (unAudit.isSuccess()) {
                OperationResult del = OperationServiceHelper.executeOperate("delete", "yem_em_devicefiles",
                        objects, null);
                if (del.isSuccess()) {
                    for (int i = 0; i < yem_es_materialinfo.size(); i++) {
                        DynamicObject materialinfo = yem_es_materialinfo.get(i);
                        DynamicObjectCollection yem_es_salesorder_z = materialinfo.getDynamicObjectCollection("yem_es_salesorder_z");
                        for (int j = 0; j < yem_es_salesorder_z.size(); j++) {
                            DynamicObject salesorder_z = yem_es_salesorder_z.get(j);
//                            salesorder_z.set("yem_devicefiles", null);
                            salesorder_z.set("yem_devicefilesid", null);

                        }
                    }
                    SaveServiceHelper.save(new DynamicObject[]{dataEntitie});
                } else {
                    List<IOperateInfo> allErrorOrValidateInfo = del.getAllErrorOrValidateInfo();
                    String message = unAudit.getMessage();
                    throw new KDBizException("《整机档案》删除失败," + message + "，" + allErrorOrValidateInfo);
                }
            } else {
                List<IOperateInfo> allErrorOrValidateInfo = unAudit.getAllErrorOrValidateInfo();
                String message = unAudit.getMessage();
                throw new KDBizException("《整机档案》反审核失败," + message + "，" + allErrorOrValidateInfo);
            }

        }
    }
}
