package com.yem.wm.es.shippingdetails.op;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.yem.wm.bc.companytitle.form.CompanyTitleEdit;
import com.yem.wm.utils.YEM;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
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.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;

/**
 * @author 张立攀
 * @version 1.0
 * @description   出运明细 删除 服务按钮
 * @date 2023-09-04 18:04
 */
public class ShippingDetailDeleteOp extends AbstractOperationServicePlugIn {

    private static final Log log = LogFactory.getLog(CompanyTitleEdit.class);
    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        e.getFieldKeys().add("yem_es_materialinfo");
        e.getFieldKeys().add("yem_es_salesorder_z");
        e.getFieldKeys().add("yem_ifdelivered");
        e.getFieldKeys().add("yem_sourcebillno");
    }

    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        DynamicObject[] dataEntities1 = e.getDataEntities();
        for (DynamicObject temp:dataEntities1){
            String yemsourcebillno = temp.getString("yem_sourcebillno");
            if (YEM.isNotEmpty(yemsourcebillno)) {
                DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle("yem_es_salesorder", "", new QFilter[]{
                        new QFilter("billno", QCP.equals, yemsourcebillno),
                        new QFilter("billstatus", QCP.equals, "C")
                });
                if (dynamicObject != null) {
                    dynamicObject = BusinessDataServiceHelper.loadSingle(dynamicObject.getPkValue(), dynamicObject.getDynamicObjectType().getName());
                    DynamicObjectCollection dynamicObjectCollection = dynamicObject.getDynamicObjectCollection("yem_es_materialinfo");
                    for(DynamicObject dynamicObject1:dynamicObjectCollection){
                        DynamicObjectCollection yem_es_salesorder_z = dynamicObject1.getDynamicObjectCollection("yem_es_salesorder_z");
                        int rowIndex = 0;
                        for(DynamicObject dc:yem_es_salesorder_z){
                            if(dc != null){
                                dc.set("yem_ifdelivered",false);
                            }
                            yem_es_salesorder_z.set(rowIndex,dc);
                            rowIndex ++;
                        }

                    }
                    OperationResult operationResult1 = OperationServiceHelper.executeOperate("save", "yem_es_salesorder", new DynamicObject[]{dynamicObject}, OperateOption.create());
                    if(operationResult1.isSuccess()){
//                        log.error("保存成功-"+operationResult1.getMessage());
                    }else {
                        log.error("保存失败-"+operationResult1.getMessage());
                    }

                }
            }
        }
    }
}
