package yem.wmc.ydn.formplugin.dynamic;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.IFormView;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import yem.base.common.utils.DateUtil;

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

public class PurtotalTopFormPlugin extends AbstractFormPlugin {
    @Override
    public void afterCreateNewData(EventObject e) {
        setValue(this.getView(), this.getModel());
    }
    public void setValue(IFormView view, IDataModel model) {
        Date yearFirst = DateUtil.getCurrYearFirst();//本年第一天
        Date yearLast = DateUtil.getCurrYearLast();//本年最后一天
        List<QFilter> qFilters = new ArrayList<>();
        qFilters.add(new QFilter("yem_date", QCP.large_equals, yearFirst));
        qFilters.add(new QFilter("yem_date", QCP.less_equals, yearLast));
        qFilters.add(new QFilter("yem_needtype", QCP.equals, "B"));
        DynamicObject[] ca_homedate = BusinessDataServiceHelper.load("yem_ca_homedate", "id,yem_qty", qFilters.toArray(new QFilter[]{}));
        BigDecimal qty = BigDecimal.ZERO;
        for (DynamicObject homedate : ca_homedate) {
            qty = qty.add(homedate.getBigDecimal("yem_qty"));
        }
        model.setValue("yem_grossqty", qty);
    }
}
