package com.fdkanfang.web.mq.listener;

import com.fdkanfang.common.constant.MsgCode;
import com.fdkanfang.common.exception.BaseRuntimeException;
import com.fdkanfang.common.util.AliyunOssUtil;
import com.fdkanfang.common.util.ComputerUtil;
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 org.springframework.transaction.annotation.Transactional;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by owen on 2020/3/16 0016 18:17
 *
 * 生成模型消息监听
 */
@Log4j2
@Component
@RabbitListener(queues = RabbitConfig.PANO_QUEUE) // 指定监听队列
public class PanoListener {

    @Autowired
    private HouseService2 houseService2;

    /**
     * 结果监听
     */
    @RabbitHandler
//    @Transactional(rollbackFor = Exception.class)
    public void getMessage(HashMap<Object, Object> mqMap)  {
        log.info("=====================收到生成模型消息，开始生成模型=====================");

        LocalDateTime starTime = LocalDateTime.now();

        Long houseId = (Long)mqMap.get("id");
        String basePath = (String)mqMap.get("basePath");
        log.info("生成模型基础文件目录为:{}" , basePath);
        HouseEntity house = houseService2.findById(houseId);
        if (house == null) {
            log.error("房源不存在，无法生成模型");
            throw new BaseRuntimeException(MsgCode.e_COMMON_3002, MsgCode.msg_COMMON_3002);
        }
        String sceneCode = house.getSceneCode();
        try {
            //计算模型并返回需要上传oss的文件集合
            Map<String, String> map = ComputerUtil.computer(sceneCode, sceneCode, basePath);
            // 模型上传oss
            AliyunOssUtil.uploadMulFiles(map);
            log.info("房源[{}]执行生成模型之后上传oss完成" , house.getId());
            // 3：rsaPano 成功
            house.setStatus(4);
        } catch (Exception e) {
            // 4：rsaPano失败
            log.error("房源[{}]执行生成模型脚本出现异常:{}" , house.getId() , e);
            house.setStatus(5);
            e.printStackTrace();
        } finally {
            house.setUpdateTime(new Date());
            int update = houseService2.update(house);
            if(update == 1){
                log.info("执行完生成模型脚本，房源[{}]更新状态[{}]成功" , house.getId() , house.getStatus());
            }else{
                log.info("执行完生成模型脚本，房源[{}]更新状态[{}]失败" , house.getId() , house.getStatus());
            }

        }
        LocalDateTime endTime = LocalDateTime.now();
        Duration.between(starTime, endTime);
        log.info("调用生成模型脚本耗时: {} s", Duration.between(starTime, endTime).getSeconds());
        log.info("=====================房源[{}]生成模型结束=====================", house.getId());
    }

    @RabbitHandler
    public void getMessage(String msg)  {
        log.info("run testPanoListener ");


        log.info("end testPanoListener msg: {}", msg);
    }



}
