package fcb.project.manager.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import fcb.project.manager.base.entity.TmApiHouseRecommend;
import fcb.project.manager.base.dao.TmApiHouseRecommendDao;
import fcb.project.manager.base.enums.UuidPreEnum;
import fcb.project.manager.base.service.ITmApiHouseRecommendService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import fdage.back.sdk.base.uuid.SnowFlakeUUidUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 房源和推荐房源的关联关系表 服务实现类
 * </p>
 *
 * @author abner
 * @since 2021-01-11
 */
@Service
public class TmApiHouseRecommendServiceImpl extends ServiceImpl<TmApiHouseRecommendDao, TmApiHouseRecommend> implements ITmApiHouseRecommendService {

    public TmApiHouseRecommend getRecommendByScene(String fromSceneNum , String toSceneNum){
        if(StringUtils.isNoneBlank(fromSceneNum , toSceneNum)){
            LambdaQueryWrapper<TmApiHouseRecommend> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(TmApiHouseRecommend::getSceneNum , fromSceneNum);
            lambdaQueryWrapper.eq(TmApiHouseRecommend::getRecommendSceneNum , toSceneNum);
            lambdaQueryWrapper.eq(TmApiHouseRecommend::getEnable , 1);
            lambdaQueryWrapper.last(" limit 1");
            return getBaseMapper().selectOne(lambdaQueryWrapper);
        }
        return null;
    }

    public TmApiHouseRecommend getRecommendByScene(String fromSceneNum , String toSceneNum , String communicatRoomId){
        if(StringUtils.isNoneBlank(fromSceneNum , toSceneNum)){
            LambdaQueryWrapper<TmApiHouseRecommend> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(TmApiHouseRecommend::getSceneNum , fromSceneNum);
            lambdaQueryWrapper.eq(TmApiHouseRecommend::getRecommendSceneNum , toSceneNum);
            lambdaQueryWrapper.eq(TmApiHouseRecommend::getEnable , 1);
            lambdaQueryWrapper.eq(TmApiHouseRecommend::getCommunicateRoomId , communicatRoomId);
            lambdaQueryWrapper.last(" limit 1");
            return getBaseMapper().selectOne(lambdaQueryWrapper);
        }
        return null;
    }

    public List<TmApiHouseRecommend> getRecommendList(String sceneNum){
        if(StringUtils.isNotBlank(sceneNum)){
            LambdaQueryWrapper<TmApiHouseRecommend> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(TmApiHouseRecommend::getSceneNum , sceneNum);
            lambdaQueryWrapper.eq(TmApiHouseRecommend::getEnable , 1);
            return getBaseMapper().selectList(lambdaQueryWrapper);
        }
        return null;
    }

    public List<TmApiHouseRecommend> getRecommendList(String sceneNum , String communicateRoomId){
        if(StringUtils.isNotBlank(sceneNum)){
            LambdaQueryWrapper<TmApiHouseRecommend> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(TmApiHouseRecommend::getSceneNum , sceneNum);
            lambdaQueryWrapper.eq(TmApiHouseRecommend::getEnable , 1);
            lambdaQueryWrapper.eq(TmApiHouseRecommend::getCommunicateRoomId , communicateRoomId);
            return getBaseMapper().selectList(lambdaQueryWrapper);
        }
        return null;
    }

    public int insertRecommend(TmApiHouseRecommend tmApiHouseRecommend){
        if(null == tmApiHouseRecommend){
            return -1;
        }
        if(StringUtils.isBlank(tmApiHouseRecommend.getRelationNo())){
            tmApiHouseRecommend.setRelationNo(SnowFlakeUUidUtils.generaUUid(null ,
                    null , UuidPreEnum.API_HOUSE_RECOMMEND_ID_PRE.getPre()));
            if(null == tmApiHouseRecommend.getEnable()){
                tmApiHouseRecommend.setEnable(1);
            }
            if(null == tmApiHouseRecommend.getCreateTime()){
                tmApiHouseRecommend.setCreateTime(LocalDateTime.now());
            }
        }
        tmApiHouseRecommend.setLastModifyDatetime(LocalDateTime.now());
        return getBaseMapper().insert(tmApiHouseRecommend);
    }


}
