package com.yem.wm.bc.bill.list;

import com.yem.wm.bc.change.business.ChangeModelHelper;
import com.yem.wm.utils.StringUtils;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.entity.datamodel.ListSelectedRowCollection;
import kd.bos.form.FormShowParameter;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.events.PreOpenFormEventArgs;
import kd.bos.form.operate.FormOperate;
import kd.bos.list.ListShowParameter;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;

import java.util.EventObject;

/**
 * @author 夜子
 * @ClassName YemBillPlugInBaseList
 * @description: TODO
 * @date 2023年08月11日
 * @version: 1.0
 */
public class YemBillPlugInBaseList extends AbstractListPlugin {


    @Override
    public void preOpenForm(PreOpenFormEventArgs e) {
        super.preOpenForm(e);
        ListShowParameter fsp= (ListShowParameter) e.getFormShowParameter();
        String billtype  =  fsp.getBillFormId();
        if(billtype.indexOf("_", billtype.indexOf("_") + 1)>1){
            billtype =  billtype.substring(0, billtype.indexOf("_", billtype.indexOf("_") + 1));
        }
        if(ChangeModelHelper.getChangeModel5SrcBill(billtype)!=null){
            fsp.addCustPlugin("com.yem.wm.bc.change.list.SrcBillChangeListPlugin");
        }
        if(ChangeModelHelper.getChangeModel5XBill(billtype)!=null){
            fsp.addCustPlugin("com.yem.wm.bc.change.list.XBillChangeListPlugin");
        }


    }

    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        String billtype  =  this.getModel().getDataEntityType().getName();
        this.getView().setVisible(ChangeModelHelper.getChangeModel5SrcBill(billtype) != null,"bar_change");
        this.getView().setVisible(ChangeModelHelper.getChangeModel5XBill(billtype) != null,"bar_bizvalid");
        this.getView().setVisible(ChangeModelHelper.getChangeModel5SrcBill(billtype) != null || ChangeModelHelper.getChangeModel5XBill(billtype) != null,"bar_bizchangeresume");

    }

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs e) {
        super.beforeDoOperation(e);
        ListSelectedRowCollection listSelectedData = e.getListSelectedData();
        FormOperate formOperate = (FormOperate) e.getSource();
        String itemKey = formOperate.getOperateKey();
        if("unsubmit".equals(itemKey)||"audit".equals(itemKey)){
            for (ListSelectedRow SelectedData : listSelectedData) {
                String entityType = BusinessDataServiceHelper.loadSingle(SelectedData.getBillTypeID(), "bos_billtype").getString("billformid.number");
                String billNo = SelectedData.getBillNo();
                DynamicObject obj = BusinessDataServiceHelper.loadSingle(entityType, "id", new QFilter[]{new QFilter("billno", QCP.equals, billNo)});
                obj = BusinessDataServiceHelper.loadSingle(obj.getLong("id"),entityType);
                String yem_forminstid = obj.getString("yem_forminstid");
                String yem_flowinstid = obj.getString("yem_flowinstid");
                if(!StringUtils.isBlank(yem_forminstid)||!StringUtils.isBlank(yem_flowinstid)){
                    e.setCancel(true);
                    if("unsubmit".equals(itemKey)){
                        getView().showErrorNotification("本单存在智能审批无法进行手工撤销，请在云之家进行驳回处理！");
                    }else{
                        getView().showErrorNotification("本单存在智能审批无法进行手工审核，请在云之家进行审批处理！");
                    }
                }
            }
        }
    }
}
