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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import fcb.project.manager.base.entity.TmApiHouseInfo;
import fcb.project.manager.base.dao.TmApiHouseInfoDao;
import fcb.project.manager.base.enums.UuidPreEnum;
import fcb.project.manager.base.service.ITmApiHouseInfoService;
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;

/**
 * <p>
 * 房源信息表 服务实现类
 * </p>
 *
 * @author abner
 * @since 2021-01-11
 */
@Service
public class TmApiHouseInfoServiceImpl extends ServiceImpl<TmApiHouseInfoDao, TmApiHouseInfo> implements ITmApiHouseInfoService {

    public TmApiHouseInfo getHouseBySceneAndRoomId(String sceneNum , String communicateRoomId){

        if(StringUtils.isNoneBlank(sceneNum , communicateRoomId)){
            LambdaQueryWrapper<TmApiHouseInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(TmApiHouseInfo::getSceneNum , sceneNum);
            lambdaQueryWrapper.eq(TmApiHouseInfo::getCommunicateRoomId , communicateRoomId);
            lambdaQueryWrapper.eq(TmApiHouseInfo::getEnable , 1);
            lambdaQueryWrapper.last("limit 1");
            return getBaseMapper().selectOne(lambdaQueryWrapper);
        }
        return null;
    }

    public TmApiHouseInfo getHouseBySceneAndHouseId(String sceneNum , String houseId){

        if(StringUtils.isNoneBlank(sceneNum , houseId)){
            LambdaQueryWrapper<TmApiHouseInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(TmApiHouseInfo::getSceneNum , sceneNum);
            lambdaQueryWrapper.eq(TmApiHouseInfo::getHouseId , houseId);
            lambdaQueryWrapper.eq(TmApiHouseInfo::getEnable , 1);
            lambdaQueryWrapper.last("limit 1");
            return getBaseMapper().selectOne(lambdaQueryWrapper);
        }
        return null;
    }

    public int insertNew(TmApiHouseInfo tmApiHouseInfo){
        if(null == tmApiHouseInfo){
            return -1;
        }

        if(StringUtils.isBlank(tmApiHouseInfo.getRequestId())){
            tmApiHouseInfo.setRequestId(SnowFlakeUUidUtils.generaUUid(null , null , UuidPreEnum.API_HOUSE_ID_PRE.getPre()));
        }
        if(null == tmApiHouseInfo.getEnable()){
            tmApiHouseInfo.setEnable(1);
        }
        if(null == tmApiHouseInfo.getCreateTime()){
           tmApiHouseInfo.setCreateTime(LocalDateTime.now());
        }
        if(null == tmApiHouseInfo.getLastModifyDatetime()){
            tmApiHouseInfo.setLastModifyDatetime(LocalDateTime.now());
        }

        return getBaseMapper().insert(tmApiHouseInfo);
    }



}
