package com.yem.wm.bc.change.op;

import java.util.ArrayList;

import com.yem.wm.bc.change.business.ChangeModelHelper;
import com.yem.wm.bc.change.common.consts.BizChangeStatusEnum;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.metadata.IDataEntityType;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.EndOperationTransactionArgs;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.servicehelper.operation.DeleteServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

/**
 * @author 夜子
 * @ClassName XBillDeleteOp
 * @description: TODO
 * @date 2023年08月03日
 * @version: 1.0
 */
public class XBillDeleteOp extends AbstractOperationServicePlugIn {
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        e.getFieldKeys().add("sourcebillid");
        e.getFieldKeys().add("sourcebillentity");
    }

    public void endOperationTransaction(EndOperationTransactionArgs e) {
        if (e.getDataEntities() != null && e.getDataEntities().length != 0) {
            DynamicObject[] bills = e.getDataEntities();
            if (bills == null || bills.length <= 0) {
                return;
            }
            IDataEntityType xbillET = bills[0].getDataEntityType();
            ArrayList<DynamicObject> srcBills = new ArrayList<DynamicObject>();
            ArrayList<Object> xBillPks = new ArrayList<Object>();
            ArrayList<Object> zeroXBillAr = new ArrayList<Object>();
            for (DynamicObject bill : bills) {
                xBillPks.add(bill.getPkValue());
                long sourceBillId = bill.getLong("sourcebillid");
                String sourceBillEntity = bill.getString("sourcebillentity");
                if (sourceBillEntity == null)
                    continue;
                DynamicObject srcBill = BusinessDataServiceHelper.loadSingle((Object) sourceBillId,
                        (String) sourceBillEntity);
                if (null == srcBill.get("changer")) {
                    srcBill.set("changestatus", (Object) BizChangeStatusEnum.UNCHANGE.getValue());
                } else {
                    srcBill.set("changestatus", (Object) BizChangeStatusEnum.CHANGED.getValue());
                }
                srcBills.add(srcBill);
                QFilter zeroXBillQFilter1 = new QFilter("sourcebillid", "=", (Object) sourceBillId);
                QFilter zeroXBillQFilter2 = new QFilter("changestatus", "=", (Object) "D");
                QFilter zeroXBillQFilter3 = new QFilter("changebillno", "=",
                        (Object) (srcBill.getString("billno") + "_0"));
                DynamicObjectCollection zeroXBillDOCol = QueryServiceHelper.query((String) xbillET.getName(),
                        (String) "id",
                        (QFilter[]) new QFilter[]{zeroXBillQFilter1, zeroXBillQFilter2, zeroXBillQFilter3},
                        (String) null);
                for (DynamicObject zeroXBill : zeroXBillDOCol) {
                    if (!BizChangeStatusEnum.UNCHANGE.getValue().equals(srcBill.getString("changestatus"))
                            || zeroXBill == null)
                        continue;
                    zeroXBillAr.add(zeroXBill.get("id"));
                }
            }
            if (zeroXBillAr.size() > 0) {
                xBillPks.addAll(zeroXBillAr);
                DeleteServiceHelper.delete((IDataEntityType) xbillET, (Object[]) zeroXBillAr.toArray());
            }
            if (srcBills.size() != 0) {
                SaveServiceHelper.update( srcBills.toArray(new DynamicObject[]{}));
            }
            if (xBillPks.size() != 0) {
                DynamicObject changeModel = ChangeModelHelper.getChangeModel4XBill((String) xbillET.getName());
                if (changeModel == null) {
                    return;
                }
                QFilter qFilter = new QFilter("xbillid", "in", xBillPks);
                DeleteServiceHelper.delete((String) "yem_changeresume", (QFilter[]) new QFilter[]{qFilter});
                DeleteServiceHelper.delete((String) "yem_changeresumedetail", (QFilter[]) new QFilter[]{qFilter});
            }
        }
    }
}