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.TmLiveRoomInfo;
import com.platform.enums.IdStarterEnum;
import com.platform.service.impl.TmLiveRoomInfoServiceImpl;
import com.platform.utils.Constant;
import com.platform.utils.StringUtils;
import com.platform.utils.UUidGenerator;
import com.platform.vos.CurrentUserLoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

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

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

    public IPage<TmLiveRoomInfo> getOrQuery(String key , long pageNum , long pageSize , Long userId , Long userDeptId, CurrentUserLoginVo loginVo){
        IPage<TmLiveRoomInfo> page = new Page<>(pageNum , pageSize);
        QueryWrapper<TmLiveRoomInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enable" , 1);
        if(StringUtils.isNotEmpty(key)){
            //TODO:这里需要改进，防止慢查询
            queryWrapper.like("name"  , key).or().like("anchor_name" , key);
        }
        List<Long> roleIdList = loginVo.getRoleIdList();
        if (!roleIdList.contains(1L) && !roleIdList.contains(5L)) {
            if(null == userDeptId){
                return new Page<TmLiveRoomInfo>(pageNum , pageSize);
            }
            queryWrapper.eq("create_user_dept_id" , userDeptId);
        }
        return getBaseMapper().selectPage(page , queryWrapper);
    }

    public TmLiveRoomInfo getByRoomId(int roomId){
        QueryWrapper<TmLiveRoomInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enable" , 1);
        queryWrapper.eq("room_id" , roomId);
        queryWrapper.last("limit 1");
        return getBaseMapper().selectOne(queryWrapper);
    }

    public IPage<TmLiveRoomInfo> getListByCreateTime(long pageNum , long pageSize){
        IPage<TmLiveRoomInfo> page = new Page<>(pageNum , pageSize);
        QueryWrapper<TmLiveRoomInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enable" , 1);
      /*  LocalDateTime now = LocalDateTime.now();
        LocalDateTime day = LocalDateTime.of(now.getYear() , now.getMonth() , now.getDayOfMonth() , 0 ,
                0 , 0);
        log.info("当前日期是:{}" , day);
        queryWrapper.ge("create_time" , day);*/
        queryWrapper.orderByAsc("create_time");
        return getBaseMapper().selectPage(page , queryWrapper);

    }

}
