package com.yem.wm.cm.tracktaskconsole.op;


import com.yem.wm.es.salesorder.utils.SalesOrderUtils;
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 kd.sdk.plugin.Plugin;

import java.util.List;

/**
 * 单据操作
 */
public class TrackTaskConsoleDeleteOp extends AbstractOperationServicePlugIn implements Plugin {

    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        List<String> keys = e.getFieldKeys();
        keys.add("id");
        keys.add("yem_sourcebillid");
    }

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

    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        super.endOperationTransaction(e);
        if ("delete".equals(e.getOperationKey()) && null != e.getDataEntities()) {
            DynamicObject[] bills = e.getDataEntities();
            for (int i = 0; i < bills.length; ++i) {
                DynamicObject bill = bills[i];
                delete_yem_shippingplan_entry(bill);
            }
        }
    }

    /**
     * 删除外销合同跟单任务分录数据
     * @param bill
     */
    public void delete_yem_shippingplan_entry(DynamicObject bill){

        long id = bill.getLong("id");
        long yem_sourcebillid = bill.getLong("yem_sourcebillid");
        QFilter qFilter1 = new QFilter("id", QCP.equals, yem_sourcebillid);
        DynamicObject[] yem_es_salesorder_s = BusinessDataServiceHelper.load("yem_es_salesorder",
                "id,billno,billstatus", new QFilter[] { qFilter1 }, null);
        if(null != yem_es_salesorder_s && yem_es_salesorder_s.length>0) {
            DynamicObject yem_es_salesorder = yem_es_salesorder_s[0];
            yem_es_salesorder = BusinessDataServiceHelper.loadSingle(yem_es_salesorder.getPkValue(),yem_es_salesorder.getDynamicObjectType().getName());
            int rowIndex = -1;
            DynamicObjectCollection yem_shippingplan_entry_s = yem_es_salesorder.getDynamicObjectCollection("yem_shippingplan_entry");
            for(int i=0;i<yem_shippingplan_entry_s.size();i++){
                DynamicObject yem_shippingplan_entry = yem_shippingplan_entry_s.get(i);//yem_plandetail_entry
                DynamicObjectCollection yem_plandetail_entry = yem_shippingplan_entry.getDynamicObjectCollection("yem_plandetail_entry");
                for (int k=0;k<yem_plandetail_entry.size();k++){
                    DynamicObject plandetail = yem_plandetail_entry.get(k);
                    long yem_tracktaskconsoleid = plandetail.getLong("yem_tracktaskconsoleid");
                    if(id == yem_tracktaskconsoleid){
                        rowIndex = i;
                    }
                }

            }
            if(-1 != rowIndex){
                yem_shippingplan_entry_s.remove(rowIndex);
                SalesOrderUtils.cal_yem_orderqty(yem_es_salesorder);
                SaveServiceHelper.save(new DynamicObject[] { yem_es_salesorder });
            }
        }
    }
}