//package com.fdkankan.scene.listener;
//
//import com.alibaba.fastjson.JSONObject;
//import com.fdkankan.common.constant.ModelingBuildStatus;
//import com.fdkankan.common.util.SceneUtil;
//import com.fdkankan.fyun.oss.UploadToOssUtil;
//import com.fdkankan.mq.message.BuildSceneMqMessage;
//import com.fdkankan.mq.util.RocketMQProducer;
//import com.fdkankan.platform.api.feign.PlatformGoodsClient;
//import com.fdkankan.platform.api.feign.PlatformUserClient;
//import com.fdkankan.redis.constant.RedisKey;
//import com.fdkankan.redis.util.RedisUtil;
//import com.fdkankan.scene.service.IBuildScenePreService;
//import com.fdkankan.scene.service.ISceneEditControlsService;
//import com.fdkankan.scene.service.ISceneEditInfoService;
//import com.fdkankan.scene.service.ISceneFileBuildService;
//import com.fdkankan.scene.service.ISceneProExtService;
//import com.fdkankan.scene.service.ISceneProService;
//import com.fdkankan.scene.service.ISceneService;
//import java.time.Duration;
//import java.time.temporal.ChronoUnit;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.rocketmq.spring.annotation.MessageModel;
//import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
//import org.apache.rocketmq.spring.core.RocketMQListener;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Component;
//
//
///**
// * <p>
//        场景计算前准备工作，如下载资源等
// * </p>
// * @author dengsixing
// * @date 2022/2/23
// **/
//@Slf4j
////@Component
////@RocketMQMessageListener(
////        consumerGroup = "${rocketmq.consumer.build-scene-pre-group}",
////        topic = "${rocketmq.build-scene.topicName.topic-modeling-a-pre}",
////        messageModel = MessageModel.CLUSTERING,//负载均衡模式
////        consumeThreadMax = 10,
////        consumeTimeout = 172801000L
////)
//public class BuildScenePreMQListener implements RocketMQListener<String> {
//
//
//    @Value("${main.url}")
//    private String mainUrl;
//
//    @Value("${scene.pro.url}")
//    private String sceneProUrl;
//
//    @Value("${scene.pro.new.url}")
//    private String sceneProNewUrl;
//
//    @Autowired
//    ISceneService sceneService;
//    @Autowired
//    ISceneFileBuildService sceneFileBuildService;
//    @Autowired
//    ISceneProService sceneProService;
//    @Autowired
//    ISceneProExtService sceneProExtService;
//    @Autowired
//    PlatformGoodsClient platformGoodsClient;
//    @Autowired
//    PlatformUserClient platformUserClient;
//    @Autowired
//    ISceneEditInfoService sceneEditInfoService;
//    @Autowired
//    ISceneEditControlsService sceneEditControlsService;
//    @Autowired
//    UploadToOssUtil uploadToOssUtil;
//    @Autowired
//    IBuildScenePreService buildScenePreService;
//    @Autowired
//    private RocketMQProducer producer;
//    @Value("${rocketmq.build-scene.topicName.topic-modeling-a}")
//    private String topicName;
//    @Autowired
//    RedisUtil redisUtil;
//
//    @Value("${model.timeOut:48}")
//    private int modelTimeOut;
//
//
//    @Override
//    public void onMessage(String message) {
//
//        try {
//            BuildSceneMqMessage buildSceneMqMessage = JSONObject.parseObject(message, BuildSceneMqMessage.class);
//            log.info("开始处理消息，消费者组：{}，主题名：{}， 消息内容：{}", "build-scene-pre-group", "topic-modeling-a-pre", message);
//            process(buildSceneMqMessage);
//        }catch (Exception e){
//
//        }
//
//    }
//
//    private void process(BuildSceneMqMessage message){
//        boolean success = false;
//        String reason = null;
//        try {
//            String key = String.format(RedisKey.SCENE_BUILDING, message.getSceneNum());
//            Long building = redisUtil.incr(key, 1);
//            if (building.compareTo(1L) != 0) {
//                log.error("场景正在构建中,退出构建,参数：{}", JSONObject.toJSONString(message));
//                reason = "重复计算";
//                return;
//            } else {
//                redisUtil.expire(key, Duration.of(modelTimeOut, ChronoUnit.HOURS));
//            }
//
//            //根据相机类型，组装资源路径
//            String path = SceneUtil.getPath(message.getPath(), message.getCameraName(),
//                message.getFileId(), Integer.parseInt(message.getCameraType()), message.getUnicode());
//
//            //下载资源到本地
//            this.buildScenePreService.downLoadSource(message, path);
//
//            //发送mq，就进行计算
//            producer.syncSend(topicName, message);
//
//            success = true;
//
//            log.info("场景计算前置处理完成，场景码：{}", message.getSceneNum());
//
//        }catch (Exception e){
//            log.error("场景计算前置处理出错", e);
//            reason = "场景计算前置处理出错";
//        }finally {
//            //如果前置处理失败，发送钉钉消息
//            if(!success){
//                String serverPath = message.getPath().substring(0,message.getPath().lastIndexOf("/")+1).concat(message.getPrefix());
////            buildScenePreService.handFail(reason, serverPath, message.getSceneNum(), BuildScenePreServiceImpl.hostName, null);
//            }
//
//        }
//    }
//
//}
