package com.platform.service.custom;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.platform.entity.ApiTmLiveRoomInfoVo;
import com.platform.enums.IdStarterEnum;
import com.platform.service.impl.ApiTmLiveRoomInfoServiceImpl;
import com.platform.utils.StringUtils;
import com.platform.utils.UUidGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author abnerhou
 * @date 2020/9/14 9:08
 * @desciption
 */
@Slf4j
@Service
public class ApiLiveRoomInfoService extends ApiTmLiveRoomInfoServiceImpl {

    public int addNew(ApiTmLiveRoomInfoVo tmLiveRoomInfo){
        if(null == tmLiveRoomInfo){
          return -1;
        }
        tmLiveRoomInfo.setLiveId(UUidGenerator.generatorUuid(IdStarterEnum.LIVE_ROOM_ID.getStarter()));
        tmLiveRoomInfo.setEnable(1);
        tmLiveRoomInfo.setCreateTime(LocalDateTime.now());
        tmLiveRoomInfo.setUpdateTime(LocalDateTime.now());
        return getBaseMapper().insert(tmLiveRoomInfo);
    }

    public int updateLiveRoom(ApiTmLiveRoomInfoVo tmLiveRoomInfo){
        if(null == tmLiveRoomInfo){
            return -1;
        }
        String liveId = tmLiveRoomInfo.getLiveId();
        if(StringUtils.isNullOrEmpty(liveId)){
            log.info("直播ID为空了");
            return -1;
        }
        QueryWrapper<ApiTmLiveRoomInfoVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("live_id" , liveId);
        queryWrapper.last("limit 1");
        return  getBaseMapper().update(tmLiveRoomInfo , queryWrapper);
    }

    public IPage<ApiTmLiveRoomInfoVo> getOrQuery(String key , long pageNum , long pageSize){
        IPage<ApiTmLiveRoomInfoVo> page = new Page<>(pageNum , pageSize);
        QueryWrapper<ApiTmLiveRoomInfoVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enable" , 1);
        if(StringUtils.isNotEmpty(key)){
            //TODO:这里需要改进，防止慢查询
            queryWrapper.like("name"  , key).or().like("anchor_name" , key);
        }
        return getBaseMapper().selectPage(page , queryWrapper);

    }

    public ApiTmLiveRoomInfoVo getRoomById(String liveId){
        if(StringUtils.isNullOrEmpty(liveId)){
           return new ApiTmLiveRoomInfoVo();
        }
        QueryWrapper<ApiTmLiveRoomInfoVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("live_id" , liveId);
        queryWrapper.eq("enable" , 1);
        queryWrapper.last("limit 1");
        return getBaseMapper().selectOne(queryWrapper);
    }

}
