package com.yem.em.task;

import com.yem.em.utils.AtlasUtils;
import com.yem.wm.utils.RequestCmmp;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.exception.KDException;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.schedule.executor.AbstractTask;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.text.SimpleDateFormat;
import java.util.*;

import static com.yem.em.utils.AtlasUtils.seelctDyn;

/**
 * @author zhouc
 * @date 2023/12/2 18:31
 * @className BomChangeTaskEdit
 * @description BOM变更单获取定时任务
 */
public class BomChangeTaskEdit extends AbstractTask {
    public void execute(RequestContext requestContext, Map<String, Object> map) throws KDException {
        Map<String, String> productmap = new HashMap<>();
        productmap.put("ZZJ", "L");//装载机
        productmap.put("CC", "F");//叉车
        productmap.put("TLJ", "T");//拖拉机

        QFilter qFilter = new QFilter("number", QCP.equals, "110");
        DynamicObject bos_org = BusinessDataServiceHelper.loadSingle("bos_org", "id", qFilter.toArray());
        String Queryfield = AtlasUtils.Queryfield("changebill_code", "pcode", "ccode", "qty", "break_flag", "enable_time",
                "disable_time", "state", "complete_time", "complete_user", "MATER_NAME", "CMATER_NAME", "COMPLETE_TIME");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, -1);
        SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
        String format = f.format(calendar.getTime());
        for (String key : productmap.keySet()) {
            String value = productmap.get(key);
            String sql = "select  distinct " + Queryfield + "  from VIEW_CHANGEBILL_WM " +
                    "where TO_CHAR (enable_time, 'yyyy-mm-dd') >= '" + format + "' AND TO_CHAR (enable_time, 'yyyy-mm-dd') <= '" + format + "'";
            if (key.equals("ZZJ")) {
                //装载机
                sql = sql + " and  changebill_code like 'ZD%' or changebill_code like 'JJBOMBG%'";
            }
            if (key.equals("CC")) {
                //叉车
                sql = sql + " and changebill_code like 'GY%'";
            }
            if (key.equals("TLJ")) {
                //拖拉机
                sql = sql + " and changebill_code like 'S%'";
            }
            DynamicObject yem_bd_productsgroup = seelctDyn("yem_bd_productsgroup", "number", key);
            List<Map> fromCmmp = RequestCmmp.getFromCmmp(bos_org.getLong("id"), sql, yem_bd_productsgroup.getLong("id"));
            AtlasUtils.addbomchange(fromCmmp, value);
        }
    }
}
