package com.yem.wm.rf.longleadinventory.form;

import com.yem.wm.utils.RequestCmmp;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.events.BizDataEventArgs;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.EventObject;
import java.util.List;
import java.util.Map;

/**
 * yem_longleadinventory 插件
 */
public class LongLeadInventoryFormPlugin extends AbstractFormPlugin {

    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        //获取库存数据
        String sql = "SELECT mater_code,plan_pin,in_date from VIEW_MATER_STOCK_WM where plan_pin is not null";
        long l = 1736174630718146560l;
        DynamicObject[] cmmpConfigs = BusinessDataServiceHelper.load("yem_cmmpconfig", "id", null);
        for (DynamicObject cmmpConfig : cmmpConfigs) {
            long connid = cmmpConfig.getLong("id");
            List<Map> fromCmmp = RequestCmmp.getFromCmmpWithConnId(connid, sql);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            long date = new Date().getTime();
            IDataModel model = this.getModel();
            for (Map map : fromCmmp) {
                String mater_code = (String) map.get("mater_code");
                String plan_pin = (String) map.get("plan_pin");//整机编号
                Timestamp in_date = (Timestamp) map.get("in_date");
                Long l1 = date - in_date.getTime();
                Long l2 = l1 / 24 / 60 / 60 / 1000;
                DynamicObject yem_resourcenog = QueryServiceHelper.queryOne("yem_bd_basemodelib",
                        "number", new QFilter[]{new QFilter("number", QCP.equals, mater_code)});
                int row = model.createNewEntryRow("yem_entryentity");
                model.setValue("yem_in_date", in_date, row);
                model.setValue("yem_storeday", l2.intValue(), row);
                model.setValue("yem_resourcenog", yem_resourcenog, row);
                model.setValue("yem_vehicleno", plan_pin, row);
            }
        }

    }
}
