package com.sw.mes.yb.scheduled;

import com.alibaba.fastjson.JSON;
import com.sw.mes.base.system.dao.InventoryManageInfoDao;
import com.sw.mes.base.system.entity.DictionaryItem;
import com.sw.mes.base.system.service.DictionaryItemService;
import com.sw.mes.core.utils.DateUtil;
import com.sw.mes.core.utils.NumberUtil;
import com.sw.mes.config.rabbit.InspectDataMsgProducer;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author zhaowenjie
 * 定向划线数据下发任务，通过xxl-job进行调度
 */
@Slf4j
@Component
public class LineScribeMarkDataScheduling {
    @Autowired
    private InventoryManageInfoDao inventoryManageInfoDao;
    @Autowired
    private DictionaryItemService dictionaryItemService;
    //@Autowired
    //private InspectDataMsgProducer inspectDataMsgProducer;

    @XxlJob("lineScribeMarkDataScheduling")
    public void lineScribeMarkDataScheduling() {
        DictionaryItem dictionaryItem = dictionaryItemService.findByThemeValueAndValueAndEnable("PipHour", "YbWms", true);
        DictionaryItem dictionaryItemfb = dictionaryItemService.findByThemeValueAndValueAndEnable("PipHour", "FbWms", true);
        Integer time = null, timefb = null;
        if (dictionaryItem != null) {
            time = NumberUtil.String2Integer(dictionaryItem.getExtendValue(), null);
        }
        if (dictionaryItemfb != null) {
            timefb = NumberUtil.String2Integer(dictionaryItemfb.getExtendValue(), null);
        }
        List<Map<String, Object>> inventoryManageInfos = new ArrayList<>();
        List<Map<String, Object>> inventoryManageInfoFbs = new ArrayList<>();
        if (time != null) {
            Date date = DateUtil.addHour(new Date(), -time);
            inventoryManageInfos = inventoryManageInfoDao.getLineScribeMarkData(date);
        }
        if (timefb != null) {
            Date date = DateUtil.addHour(new Date(), -timefb);
            inventoryManageInfoFbs = inventoryManageInfoDao.getLineScribeMarkDataFb(date);
        }
        List<String> fbsns = new ArrayList<>();
        for (Map<String, Object> map : inventoryManageInfoFbs) {
            fbsns.add(NumberUtil.object2String(map.get("sectionNo"), null));
        }
        List<Map<String, Object>> inventoryManageInfoDto = new ArrayList<>();
        for (Map<String, Object> map : inventoryManageInfos) {
            if (!fbsns.contains(NumberUtil.object2String(map.get("sectionNo"), null))) {
                inventoryManageInfoDto.add(map);
            }
        }
        inventoryManageInfoDto.addAll(inventoryManageInfoFbs);
        //inspectDataMsgProducer.sendLineScribeMarkDataMsg(JSON.toJSONString(inventoryManageInfoDto));
        //todo scm



    }
}
