package com.fdkanfang.web.mq.listener;

import cn.hutool.core.io.FileUtil;
import com.fdkanfang.common.constant.MsgCode;
import com.fdkanfang.common.exception.BaseRuntimeException;
import com.fdkanfang.common.util.AliyunOssUtil;
import com.fdkanfang.common.util.CreateObjUtil;
import com.fdkanfang.domain.backend.HouseEntity;
import com.fdkanfang.service.backend.HouseService2;
import com.fdkanfang.web.mq.config.RabbitConfig;
import lombok.extern.log4j.Log4j2;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;

/**
 * Created by owen on 2020/3/16 0016 18:17
 *
 * 垂直校验消息监听
 */
@Log4j2
@Component
@RabbitListener(queues = RabbitConfig.VERTICAL_QUEUE) // 指定监听队列
public class VerticalListener {

    @Autowired
    private HouseService2 houseService2;

    /**
     * order结果监听
     */
    @RabbitHandler
    public void getMessage(HashMap<Object, Object> mqMap){
        log.info("run VerticalListener ");

//        HashMap<String, String>  rsaUploadMap = (HashMap<String, String>)mqMap.get("ossUpload");
        Long houseId = (Long)mqMap.get("id");
        String basePath = (String)mqMap.get("basePath");


        HashMap<String, String>  ossImageHigh = (HashMap<String, String>)mqMap.get("ossImageHigh");
        HashMap<String, String>  ossImageLow = (HashMap<String, String>)mqMap.get("ossImageLow");

        HouseEntity house = houseService2.findById(houseId);
        if (house == null) {
            throw new BaseRuntimeException(MsgCode.e_COMMON_3002, MsgCode.msg_COMMON_3002);
        }

        long start = System.currentTimeMillis();

        // 保存数据开关
        boolean flag = false;
        String filePath = basePath+house.getSceneCode();
        try {
            // 后面跟目录路径就可以了
            String cmd  = "bash /home/ubuntu/HorizonNet/run.sh " + filePath ;
            log.info("cmd : {}", cmd);
            // 调用算法
            CreateObjUtil.callshell(cmd);

            long end = System.currentTimeMillis();
            log.info("time: {} s", (end-start)/1000);

            /**
             * 假如算法少于2秒就结束，证明算法发生了错误；
             */
            if (end < 2) {
                log.error("rsaVertical 计算异常");
                throw new BaseRuntimeException(MsgCode.e_RSA_VERTICAL_6000, MsgCode.msg_RSA_VERTICAL_6000);
            }

            // 模型上传oss
//            AliyunOssUtil.uploadMulFiles(rsaUploadMap);
            AliyunOssUtil.uploadMulFiles(ossImageHigh);
            AliyunOssUtil.uploadMulFiles(ossImageLow);
            log.info("oss上传完成");

            // 保存信息到db

            // 读取算法生成的scene.json内容到数据库
            String scenePath = filePath + "/scene.json";
            String sceneJson = FileUtil.readString(scenePath, "utf-8");

            house.setSceneJson(sceneJson);

            // 1：成功
            house.setStatus(1);

            log.info("t houseId: {}", house.toString());


        } catch (Exception e) {
            log.error("error VerticalListener 转换异常");
            // 2：失败
            house.setStatus(2);

            e.printStackTrace();
        } finally {

            house.setUpdateTime(new Date());
            int n = houseService2.update(house);
            log.info("update db success");
        }

        log.info("end VerticalListener houseId:{}", house.getId());
    }



}
