package yem.wmc.ydn.formplugin.dynamic;

import kd.bos.algo.DataSet;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.form.IFormView;
import kd.bos.orm.ORM;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.sdk.plugin.Plugin;
import yem.base.common.utils.DateUtil;

import java.math.BigDecimal;
import java.util.Date;
import java.util.EventObject;

public class LowExecuPonNeed extends AbstractBillPlugIn implements Plugin {
    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        String className = this.getClass().getName();
        IFormView view = this.getView();
        DataSet data = queyData(className, "A");
        data = data.select("yem_needtype","yem_dept","yem_operator","yem_qty","yem_isfinish");
//        data = data.groupBy(new String[]{"yem_needtype","yem_dept","yem_operator",
//                "yem_isfinish"}).sum("yem_qty").finish();
        DataSet where = data.copy();
        DataSet sumQty = where.groupBy().sum("yem_qty").finish();
        DynamicObjectCollection sumqtyresult = ORM.create().toPlainDynamicObjectCollection(sumQty);
        int sum = 0;
        if (!sumqtyresult.isEmpty()){
            sum = sumqtyresult.get(0).getInt("yem_qty");
        }
        data = data.select("yem_needtype","yem_dept","yem_operator","yem_qty","yem_isfinish"
                ,"CASE WHEN yem_isfinish THEN yem_qty/"+sum+" ELSE 0 END yem_finish");
        DynamicObjectCollection result = ORM.create().toPlainDynamicObjectCollection(data);
        this.getView().getModel().deleteEntryData("yem_needtable");
        for(DynamicObject obj :result){
            int newEntryRow = this.getView().getModel().createNewEntryRow("yem_needtable");
            this.getView().getModel().setValue("yem_needtypes",obj.getString("yem_needtype"),newEntryRow);
            this.getView().getModel().setValue("yem_dept",obj.getString("yem_dept"),newEntryRow);
            this.getView().getModel().setValue("yem_operator",obj.getString("yem_operator"),newEntryRow);
            this.getView().getModel().setValue("yem_qty",obj.getBigDecimal("yem_qty"),newEntryRow);
            this.getView().getModel().setValue("yem_finish",obj.getBigDecimal("yem_finish").multiply(new BigDecimal(100)),newEntryRow);
            this.getView().getModel().setValue("yem_isfinish",obj.getBoolean("yem_isfinish"),newEntryRow);
        }

    }
    private DataSet queyData(String className,String yem_type){
        Date yearFirst = DateUtil.getYearFirst(new Date());
        Date yearLast = DateUtil.getYearLast(new Date());
        String selectFiles = "yem_dept.name yem_dept," +
                "yem_operator.name yem_operator," +
                "yem_projectorg.name yem_projectorg," +
                "yem_needtype,yem_qty,yem_isfinish";
        DataSet data = ORM.create().queryDataSet(className,"yem_ca_homedate",selectFiles,
                new QFilter[]{
                        new QFilter("yem_needtype", QCP.equals,"B"),
                        new QFilter("yem_date",QCP.large_equals,yearFirst),
                        new QFilter("yem_isfinish",QCP.equals,true),
                        new QFilter("yem_date",QCP.less_equals,yearLast)});
        return data;
    }
}

