package nckd.jimin.jyyy.hr.tsrsc.plugin.form;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.EntityType;
import kd.bos.entity.datamodel.ListSelectedRowCollection;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.operate.FormOperate;
import kd.bos.list.BillList;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import org.apache.commons.lang3.ObjectUtils;

/**
 * Module           :人才供应云-招聘直通车-首页-年度招聘计划
 * Description      :年度招聘计划列表插件
 * @author Tyx
 * @date  2025/5/8
 * 标识 nckd_yearcasreplan
 */

public class YearCrApplyPlanListPlugin extends AbstractListPlugin {

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        super.beforeDoOperation(args);
        FormOperate opreate = (FormOperate) args.getSource();
        switch (opreate.getOperateKey()) {
            case "submit":
            case "save":
//                ListSelectedRowCollection selectCols = args.getListSelectedData();
//                StringBuffer exceedMsg = new StringBuffer();
//                StringBuffer errorMsg = new StringBuffer();
//                Object[] primaryKeyValues = selectCols.getPrimaryKeyValues();
//                BillList billlistap = this.getView().getControl("billlistap");
//                EntityType entityType = billlistap.getEntityType();
//                //获取完整数据
//                DynamicObject[] casPaybillArr = BusinessDataServiceHelper.load(primaryKeyValues, entityType);
//                for (DynamicObject dynamicObject : casPaybillArr) {
//                    if(!getErrorMsg(dynamicObject)){
//                        errorMsg.append("招聘单位："+dynamicObject.getString("org.name")+"：已生成该年度招聘计划，请勿重复生成！\n");
//                    }
//                }
//                if(ObjectUtils.isNotEmpty(errorMsg)){
//                    args.setCancelMessage(exceedMsg.toString());
//                    args.setCancel(true);
//                }
        }

    }

    public  boolean getErrorMsg(DynamicObject dynamicObject){
        Object nckdYear = dynamicObject.get("nckd_year");
        String billno = dynamicObject.getString("billno");
        QFilter nckdYear1 = new QFilter("nckd_year", QCP.equals, nckdYear);
        QFilter qBillstatus = new QFilter("billstatus", QCP.equals, "C");
        qBillstatus.and("billno",QCP.not_equals,billno);
        QFilter qFilter = new QFilter("org.id", QCP.equals, dynamicObject.getDynamicObject("org").getPkValue());
        DynamicObject dynamicObject1 = BusinessDataServiceHelper.loadSingle("nckd_yearcasreplan", "id,org,org.id", new QFilter[]{nckdYear1, qBillstatus, qFilter});
        if(ObjectUtils.isNotEmpty(dynamicObject1)){
            return false;
        }
        return true;
    }


}
