package com.ho.business.task;

import com.alibaba.fastjson.JSONObject;
import com.ho.business.config.WebSocketServer;
import com.ho.business.controller.BoZhouController;
import com.ho.business.controller.OpenStationController;
import com.ho.business.service.DeviceService;
import com.ho.business.service.OpenStationService;
import com.ho.business.vo.req.StationReq;
import com.ho.business.vo.resp.bozhou.BoZhouStationStatus;
import com.ho.business.vo.resp.point.WareHouse;
import com.ho.common.tools.constant.CommonConstant;
import com.ho.common.tools.exception.DataResult;
import com.ho.common.tools.service.RedisService;
import com.ho.common.tools.util.BigDecimalUtil;
import com.ho.datacollect.api.constant.DataCollectConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class TimeTask {

    @Autowired
    DeviceService deviceService;

    @Autowired
    RedisService redisService;

    @Autowired
    BigDecimalUtil bigDecimalUtil;

    @Autowired
    WebSocketServer webSocketServer;

    @Autowired
    OpenStationService openStationService;

    @Scheduled(cron = "0/30 * * * * ?")
    public void sendMessage(){
        ConcurrentHashMap<String, String> map = WebSocketServer.map;
        Integer stationId;
        Integer method;
        for (Map.Entry<String, String> m:map.entrySet()) {
            try {
                StationReq stationReq = new StationReq();
                String str = m.getValue();
                String[] split = str.split(",");
                stationId = Integer.valueOf(split[0]);
                method = Integer.valueOf(split[1]);
                stationReq.setStationId(stationId);
                if(CommonConstant.ONE.equals(method)){
                    OpenStationController controller = new OpenStationController();
                    controller.setDeviceService(deviceService);
                    controller.setBigDecimalUtil(bigDecimalUtil);
                    controller.setRedisService(redisService);
                    controller.setOpenStationService(openStationService);
                    DataResult<List<WareHouse>> listDataResult = controller.middlePart(stationReq);
                    WebSocketServer.sendInfo(m.getKey(),JSONObject.toJSONString(listDataResult));
                }else{
                    BoZhouController controller = new BoZhouController();
                    controller.setDeviceService(deviceService);
                    controller.setBigDecimalUtil(bigDecimalUtil);
                    controller.setRedisService(redisService);
                    DataResult<BoZhouStationStatus> status = controller.getStatus(stationReq);
                    WebSocketServer.sendInfo(m.getKey(),JSONObject.toJSONString(status));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}
