/*
package fcb.project.manager.core.rocketmqConsumer;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import fcb.project.manager.base.entity.TmApiHouseInfo;
import fcb.project.manager.base.entity.TmApiHouseRecommend;
import fcb.project.manager.base.rocketMq.HouseFormatEvent;
import fcb.project.manager.base.service.impl.TmApiHouseInfoServiceImpl;
import fcb.project.manager.base.service.impl.TmApiHouseRecommendServiceImpl;
import fdage.back.sdk.base.enums.ResultCodeEnum;
import fdage.back.sdk.base.exception.CommonBaseException;
import lombok.extern.log4j.Log4j2;
import net.bytebuddy.asm.Advice;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;

*/
/**
 * 2 * @Author: Abner
 * 3 * @Date: 2021/1/12 14:22
 * 4
 *//*


@RocketMQMessageListener(topic = "abner-test", consumerGroup = "rocketmq-cluster5",
        selectorExpression = "*")
@Component
@Log4j2
public class AsyncWriteDbConsumerListener implements RocketMQListener<String> {

    @Autowired
    private TmApiHouseInfoServiceImpl tmApiHouseInfoService;

    @Autowired
    private TmApiHouseRecommendServiceImpl tmApiHouseRecommendService;

    @Override
    public void onMessage(String houseInfoStr) {
        log.info("收到消息房源列表:{}", houseInfoStr);
        if(StringUtils.isBlank(houseInfoStr)){
            return;
        }
        TmApiHouseInfo tmApiHouseInfo = JSON.parseObject(houseInfoStr , TmApiHouseInfo.class);
        if(null == tmApiHouseInfo){
            log.error("mq收到的消息中房源格式有误，无法反序列化出来");
            return;
        }
        if(!StringUtils.isNoneBlank(tmApiHouseInfo.getRequestId() ,
                tmApiHouseInfo.getSceneNum() , tmApiHouseInfo.getHouseId() , tmApiHouseInfo.getCommunicateRoomId())){
            log.error("请求ID或者场景码或者房源ID或者rommId都不能为空");
            return;
        }
        //房源以及推荐房源落库 , TODO:这里需要将推荐房源写入缓存
        doWriteDb(tmApiHouseInfo);

    }

    @Transactional(rollbackFor = Exception.class)
    public void doWriteDb(TmApiHouseInfo tmApiHouseInfo){
        int insertHouse = tmApiHouseInfoService.insertNew(tmApiHouseInfo);
        if(insertHouse != 1){
            log.error("插入房源:场景码-{}， 房源ID-{}失败" , tmApiHouseInfo.getSceneNum() , tmApiHouseInfo.getHouseId());
            return;
        }
        if(StringUtils.isNotBlank(tmApiHouseInfo.getRecommendHouses())){
            //包含了推荐房源
            List<TmApiHouseInfo> recommendList = JSON.parseArray(tmApiHouseInfo.getRecommendHouses() , TmApiHouseInfo.class);
            if(!CollectionUtils.isEmpty(recommendList)){
                for (TmApiHouseInfo recHouse : recommendList) {
                    if(StringUtils.isBlank(recHouse.getSceneNum())){
                        log.warn("房源[{}]的推荐房源[{}]的场景码为空,不落库，跳过" , tmApiHouseInfo.getHouseId() , recHouse.getHouseId());
                        continue;
                    }
                    if(StringUtils.isNotBlank(recHouse.getCommunicateRoomId())){
                        //默认为存量数据，无需重复插入 TODO:这里可能会有问题
                        log.info("房源[{}]的推荐房源[{}]在roomId={}的情况下已经存在，无需重复插入" , tmApiHouseInfo.getSceneNum() , recHouse.getSceneNum() , tmApiHouseInfo.getCommunicateRoomId());
                        continue;
                    }
                    recHouse.setCommunicateRoomId(tmApiHouseInfo.getCommunicateRoomId());
                    int insertRec = tmApiHouseInfoService.insertNew(recHouse);
                    if(insertRec != 1){
                        log.warn("房源[{}]的推荐房源[{}]在roomId[{}]下落库 失败" , tmApiHouseInfo.getSceneNum() , recHouse.getSceneNum() , tmApiHouseInfo.getCommunicateRoomId());
                        //TODO:真的要回滚吗？回滚了就没有，这里先不会滚了
//                        throw  new CommonBaseException(ResultCodeEnum.D101 , "插入推荐房源数据失败，整个回滚");
                    }else{
                        //这里插入房源和推荐房源的关联关系
                        boolean needInsert = false;
                        if(StringUtils.isNoneBlank(tmApiHouseInfo.getCommunicateRoomId() , recHouse.getCommunicateRoomId())){
                            if(StringUtils.equals(tmApiHouseInfo.getCommunicateRoomId() , recHouse.getCommunicateRoomId())){
                                ///2、先插入单向 A->B
                                insertHouseRelation(tmApiHouseInfo , recHouse);

                                //3、插入双向 ， B->A
                                insertHouseRelation(recHouse , tmApiHouseInfo);
                                needInsert = true;
                            }
                        }
                        if(!needInsert){
                            log.warn("房源[{}]和推荐房源[{}]的roomId不一样->[{}]-[{}]，不生成关联关系" , tmApiHouseInfo.getHouseId() ,recHouse.getHouseId(),
                                    tmApiHouseInfo.getAgencyId() , recHouse.getAgencyId());
                        }
                    }
                }
                //TODO:这里可以优化下，这个时间复杂度是0(N^2)了
                for (int i = 0; i < recommendList.size(); i++) {
                    for(int j= i + 1 ; j < recommendList.size(); j++){
                        boolean needInsert = false;
                        if(StringUtils.isNoneBlank(recommendList.get(i).getCommunicateRoomId() , recommendList.get(j).getCommunicateRoomId())){
                            if(StringUtils.equals(recommendList.get(i).getCommunicateRoomId() , recommendList.get(j).getCommunicateRoomId())){
                                //A->B
                                insertHouseRelation(recommendList.get(i) , recommendList.get(j));
                                //B->A
                                insertHouseRelation(recommendList.get(j) , recommendList.get(i));
                                needInsert = true;
                            }
                        }
                        if(!needInsert){
                            log.warn("房源[{}]和推荐房源[{}]的RoomId不一样->[{}]-[{}]，不生成关联关系" , recommendList.get(i).getHouseId() ,recommendList.get(j).getHouseId(),
                                    recommendList.get(i).getAgencyId() , recommendList.get(j).getAgencyId());
                        }

                    }
                }


            }


        }

    }

    @Transactional(rollbackFor = Exception.class)
    public void insertHouseRelation(TmApiHouseInfo mainHouse , TmApiHouseInfo recommendHouse){
        if(null == mainHouse || null == recommendHouse){
            log.error("参数不够，无法建立关联关系");
        }
        TmApiHouseRecommend houseRecommend = tmApiHouseRecommendService.getRecommendByScene(mainHouse.getSceneNum() ,
                recommendHouse.getSceneNum() , mainHouse.getCommunicateRoomId());
        if(null != houseRecommend){
            log.info("场景码为：[{}]的房源和场景码为:[{}]的房源关联关系已经绑定，无需重复绑定" , mainHouse.getSceneNum() , recommendHouse.getSceneNum());
        }else{
            //新增关联关系
            if(!StringUtils.isNoneBlank(mainHouse.getSceneNum() , recommendHouse.getSceneNum())){
                log.warn("房源[{}]和房源[{}]的场景有一个为空:[{}]-[{}]" , mainHouse.getHouseId() ,recommendHouse.getHouseId() , mainHouse.getSceneNum() , recommendHouse.getSceneNum());
                return;
            }
            int recommendInsert = tmApiHouseRecommendService.insertRecommend(houseRecommend);
            if(recommendInsert != 1){
                log.error("插入主房源:[{}]和推荐房源[{}]的关联关系失败:", mainHouse.getSceneNum() , recommendHouse.getSceneNum());
//                //TODO:这里不做回滚
//                throw new CommonBaseException(ResultCodeEnum.D101 , "插入房源和推荐房源关联关系失败");
            }
        }
    }
}
*/
