package cn.iocoder.yudao.module.system.schedul;

import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.infra.api.websocket.WebSocketSenderApi;
import cn.iocoder.yudao.module.system.api.robot.websocket.RobotInformationVO;
import cn.iocoder.yudao.module.system.constant.CommonConstant;
import cn.iocoder.yudao.module.system.constant.area.FloorAreaConstant;
import cn.iocoder.yudao.module.system.constant.robot.RobotTaskChcheConstant;
import cn.iocoder.yudao.module.system.constant.webSocket.WebSocketConstant;
import cn.iocoder.yudao.module.system.dal.dataobject.positionmap.PositionMapDO;
import cn.iocoder.yudao.module.system.service.positionmap.PositionMapService;
import cn.iocoder.yudao.module.system.util.redis.RedisUtil;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

@EnableScheduling
@Component
@Slf4j
public class ThreeDimensionalScheduled {

    @Resource
    private PositionMapService positionMapService;

    @Resource
    private RedisUtil redisUtil;

    @Resource
    public WebSocketSenderApi webSocketSenderApi;

    private static final ExecutorService FIXED_THREAD_POOL = Executors.newFixedThreadPool(3);

    // 这个方法将每200毫秒执行一次
//    @Scheduled(fixedDelay = 300, timeUnit = TimeUnit.MILLISECONDS)
    public void executeTask() {
        FIXED_THREAD_POOL.execute(this::sendData);
    }

    public void sendData() {
        TenantContextHolder.setTenantId(1L);
        Object o = redisUtil.get(FloorAreaConstant.FLOOR_AREA_ALL);
        List<PositionMapDO> allMap = null;
        if (ObjectUtil.isEmpty(o)) {
            allMap = positionMapService.getAllMap();
            if (ObjectUtil.isEmpty(allMap)) {
                return;
            }
            redisUtil.set(FloorAreaConstant.FLOOR_AREA_ALL,JSON.toJSONString(allMap),600);
        }else {
            allMap = JSON.parseArray(String.valueOf(o), PositionMapDO.class);
        }

        for (PositionMapDO positionMap : allMap) {
            String key = FloorAreaConstant.FLOOR_AREA_ROBOT + positionMap.getFloor() + CommonConstant.SYMBOL + positionMap.getArea();
            Map<Object, Object> data = redisUtil.hmget(key);
            if (ObjectUtil.isEmpty(data)) {
                continue;
            }

            Map<Object, Object> map = new HashMap<>();
            data.forEach((k,v) ->{
                Object skuInfo = redisUtil.get(RobotTaskChcheConstant.ROBOT_TASK_SKU_INFO + k);
                if (ObjectUtil.isNotEmpty(skuInfo)) {
                    RobotInformationVO informationVO = JSON.parseObject(v.toString(), RobotInformationVO.class);
                    informationVO.setSkuInfo(skuInfo);
                    map.put(k,informationVO);
                }else {
                    map.put(k,v);
                }
            });

//            log.info("3D发送数据:{}", JSON.toJSONString(map));
            webSocketSenderApi.sendObject(positionMap.getFloor() + CommonConstant.SYMBOL + positionMap.getArea(), WebSocketConstant.THREE_D_MAP_PUSH, map);
        }
    }



























}
