package com.yem.em.task;

import com.yem.em.utils.AtlasUtils;
import com.yem.wm.utils.RequestCmmp;
import com.yem.wm.utils.YEM;
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 org.apache.commons.lang3.StringUtils;

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<>();
        if (map.containsKey("product_type")) {
            Object productType = map.get("product_type");
            if (YEM.isNotEmpty(productType) && productType instanceof String) {
                String typeStr = String.valueOf(productType);
                if (StringUtils.containsIgnoreCase(typeStr, "ZZJ")) {
                    productmap.put("ZZJ", "L");
                }
                if (StringUtils.containsIgnoreCase(typeStr, "CC")) {
                    productmap.put("CC", "F");
                }
                if (StringUtils.containsIgnoreCase(typeStr, "TLJ")) {
                    productmap.put("TLJ", "T");
                }
            }
        }

        if (map.entrySet().isEmpty()) {
            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", "complete_time",
                "disable_time", "state", "complete_time", "complete_user", "MATER_NAME", "CMATER_NAME", "COMPLETE_TIME", "enable_time");
        Calendar calendar = Calendar.getInstance();

        // 昨天
        calendar.add(Calendar.DAY_OF_MONTH, -1);

        // 获取昨天的00:00:00
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        String start = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());

        // 获取昨天的23:59:59
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        String end = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());

//        start = "2024-12-25 00:00:00";
//        end = "2024-12-25 23:59:59";

        for (String key : productmap.keySet()) {
            String value = productmap.get(key);
            String sql = "select  distinct " + Queryfield + "  from VIEW_CHANGEBILL_WM " +
                    "where TO_CHAR (complete_time, 'yyyy-mm-dd HH:mm:ss') >= '" + start + "' AND TO_CHAR (complete_time, 'yyyy-mm-dd HH:mm:ss') <= '" + end + "'";
            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);
        }
    }
}
