package com.fdkanfang.web.mq.listener;

import com.alibaba.fastjson.JSONObject;
import com.fdkanfang.common.sms.SmsService;
import com.fdkanfang.common.util.DataUtils;
import com.fdkanfang.domain.backend.HouseEntity;
import com.fdkanfang.domain.backend.UserEntity;
import com.fdkanfang.service.backend.HouseService2;
import com.fdkanfang.service.backend.UserService2;
import com.fdkanfang.web.mq.config.RabbitConfig;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Map;

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

    @Autowired
    private HouseService2 houseService2;

    @Autowired
    private UserService2 userService2;


    @Value("${scene.finish.sms.template.id}")
    private String smsTemplateId;

    @Value("${sms.common.sign}")
    private String smsCommonSign;

    /**
     * 结果监听
     */
    @RabbitHandler
//    @Transactional(rollbackFor = Exception.class)
    public void getMessage(String content)  {
        log.info("=====================收到生成模型回调消息，回调开始=====================");
        log.info("接收到的消息为:{}" , content);
        if(StringUtils.isBlank(content)){
            log.warn("MQ返回消息为空，无法更新房源状态");
            return;
        }
        Map<String ,Object> mqMap = JSONObject.parseObject(content , Map.class);
        if(CollectionUtils.isEmpty(mqMap)){
            log.error("MQ返回消息解析失败");
            return;
        }
        if(!mqMap.containsKey("result")){
            log.error("看看MQ没有返回建模结果");
            return;
        }
        String sceneNum = (String) mqMap.get("sceneNum");
        if(StringUtils.isBlank(sceneNum)){
            log.warn("看看房返回结算结果的场景码为空");
            return;
        }
        HouseEntity houseEntity = houseService2.findBySceneCode(sceneNum);
        if(null == houseEntity){
            log.error("场景码[{}]对应的房源信息不存在，不更新模型生成结果" , sceneNum);
            return;
        }
        Integer result = DataUtils.getInteger(mqMap.get("result"));
        if(null == result){
            log.error("解析看看返回的计算结果失败");
            return;
        }
        if(result.compareTo(1) == 0){
            log.info("看看返回建模计算成功");
            houseEntity.setStatus(4);
            if(null != houseEntity.getUserId()){
                UserEntity user = userService2.findById(houseEntity.getUserId());
                if(null != user){
                    //发送短信给用户
                    String param = "{\"sceneNum\":\"" + houseEntity.getSceneCode() + "\"}";
                    boolean smsResult = SmsService.sendSmsWithSignName(user.getPhone() , param , smsTemplateId , smsCommonSign);
                    if(smsResult){
                        log.info("给用户[{}]发送房源[{}]构建完成的短信成功" , user.getPhone() , houseEntity.getSceneCode());
                    }else{
                        log.info("给用户[{}]发送房源[{}]构建完成的短信失败" , user.getPhone() , houseEntity.getSceneCode());
                    }
                }else{
                    log.info("房源[{}]的传用户id为空，无法给他发短信" , houseEntity.getSceneCode());
                }

            }

        }else{
            log.info("看看返回建模计算非成功:{}" , result);
            houseEntity.setStatus(5);
        }
        int update = houseService2.update(houseEntity);
        if(update == 1){
            log.info("执行完生成模型脚本后，房源[{}]更新状态[{}]成功" , houseEntity.getId() , houseEntity.getStatus());
        }else{
            log.info("执行完生成模型脚本后，房源[{}]更新状态[{}]失败" , houseEntity.getId() , houseEntity.getStatus());
        }
        log.info("=====================房源[{}]生成模型回调结束=====================", houseEntity.getId());
    }

}
