package com.yem.wm.es.encasement.from;

import com.alibaba.druid.util.StringUtils;
import com.yem.wm.utils.YEM;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.operate.AbstractOperate;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.util.Date;

/**
 * @author 张立攀
 * @version 1.0
 * @description 装箱单物流
 * @date 2023-08-24 22:48
 */
public class EncaseMentWLEdit extends AbstractBillPlugIn {
    @Override
    public void afterDoOperation(AfterDoOperationEventArgs e) {
        String key = e.getOperateKey();
        if ("statusconvertwc".equals(key) ) {
            boolean boonle = true;
            int i = 1;
            String string = "";
            DynamicObject yemTransportstyle = (DynamicObject) this.getModel().getValue("yem_transportstyle");//运输方式
            DynamicObjectCollection yemImPackinginforPi = this.getModel().getEntryEntity("yem_im_encasement_ci");
            if (yemTransportstyle != null) {
                String number = yemTransportstyle.getString("number");
                if ("YSFS-0004".equals(number) || "YSFS-0006".equals(number) || "YSFS-1003".equals(number) ||
                        "YSFS-1004".equals(number) || "YSFS-0011".equals(number) || "YSFS-1001".equals(number)) {
                    if (yemImPackinginforPi.size() > 0) {
                        for (DynamicObject dynamicObject : yemImPackinginforPi) {
                            String yemContainernumber = dynamicObject.getString("yem_encasenum");//集装箱号
                            String yemCarnum = dynamicObject.getString("yem_carnums");//车号
                            String seq = dynamicObject.getString("seq");
                            if ("".equals(yemContainernumber) ) {
                                string = string + i + ". 第"+seq+"行集装箱号为空，请稍后重试！\n";
                                boonle = false;
                                i++;
                            }
                            if ("".equals(yemCarnum)) {
                                string = string + i + ". 第"+seq+"行车号为空，请稍后重试！\n";
                                boonle = false;
                                i++;
                            }
                        }
                    }else {
                        string="装箱信息分录为空，请稍后重试！";
                        boonle = false;
                    }
                }
            }
            if (boonle) {
                this.getModel().setValue("yem_containerstatus", "B");
                IDataModel model = this.getModel();
                DynamicObject dataEntity = model.getDataEntity(true);
                dataEntity.set("yem_containerstatus", "B");
                long userId = YEM.getCurrentUserId();
                DynamicObject user = BusinessDataServiceHelper.loadSingle(userId, "bos_user");
                dataEntity.set("yem_containerman", user);
                dataEntity.set("yem_containerdate", new Date());
                SaveServiceHelper.save(new DynamicObject[]{dataEntity});
                this.getView().invokeOperation("refresh");
                this.getView().showSuccessNotification("操作成功");
            } else {
                this.getView().showErrorNotification(string);
            }
        }

        if ("statusconvertqx".equals(key)) {
            DynamicObject dataEntity = this.getModel().getDataEntity();
            dataEntity.set("yem_containerstatus", "A");
            dataEntity.set("yem_containerman", null);
            dataEntity.set("yem_containerdate", null);
            SaveServiceHelper.save(new DynamicObject[]{dataEntity});
        }

        if ("custom_save".equals(key)){
            this.getModel().setValue("yem_containerstatus", "B");
            IDataModel model = this.getModel();
            DynamicObject dataEntity = model.getDataEntity(true);
            SaveServiceHelper.save(new DynamicObject[]{dataEntity});
            this.getView().invokeOperation("refresh");
            this.getView().showSuccessNotification("操作成功");
        }
    }

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs e) {
        AbstractOperate formOperate = (AbstractOperate) e.getSource();
        if (StringUtils.equals("save", formOperate.getOperateKey())) {
            this.getModel().setValue("yem_state", "已回填");
        }

    }
}
