package com.fdkankan.tk.service.impl;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.jwt.JWTUtil;
import com.auth0.jwt.JWT;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fdkankan.redis.constant.RedisKey;
import com.fdkankan.redis.util.RedisUtil;
import com.fdkankan.tk.common.FilePath;
import com.fdkankan.tk.common.PageInfo;
import com.fdkankan.tk.common.ResultCode;
import com.fdkankan.tk.common.WxSharCodePath;
import com.fdkankan.tk.common.util.JwtUtil;
import com.fdkankan.tk.common.util.RedisKeyUtil;
import com.fdkankan.tk.common.util.RoomUtil;
import com.fdkankan.tk.common.util.UploadToOssUtil;
import com.fdkankan.tk.entity.Room;
import com.fdkankan.tk.entity.WxUser;
import com.fdkankan.tk.exception.BusinessException;
import com.fdkankan.tk.mapper.IRoomMapper;
import com.fdkankan.tk.request.RoomAddParam;
import com.fdkankan.tk.request.RoomListParam;
import com.fdkankan.tk.response.RoomVo;
import com.fdkankan.tk.response.SceneVo;
import com.fdkankan.tk.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
/**
*
* 服务实现类
*
*
* @author
* @since 2022-09-19
*/
@Service
public class RoomServiceImpl extends ServiceImpl implements IRoomService {
@Autowired
IRoomNumService roomNumService;
@Autowired
ISceneService sceneService;
@Autowired
UploadToOssUtil uploadToOssUtil;
@Autowired
IWxService wxService;
@Value("${upload.query-path}")
private String queryPath;
@Autowired
RedisUtil redisUtil;
@Autowired
IWxUserService wxUserService;
@Override
public PageInfo pageList(RoomListParam param, String token,String wxToken) {
LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>();
if(StringUtils.isNotBlank(token)){
String fdToken = String.format(RedisKey.TOKEN_V3,token);
if(! redisUtil.hasKey(fdToken)){
throw new BusinessException(ResultCode.USER_NOT_LOGIN);
}
redisUtil.expire(fdToken,6 * 60 * 60);
String userName = JwtUtil.getUserName(token);
wrapper.eq(Room::getRoomUserName,userName);
}
if(StringUtils.isNotBlank(wxToken) ){
String wxUserId = JwtUtil.getUserName(wxToken);
WxUser wxUser = wxUserService.getById(wxUserId);
if(wxUser != null){
wrapper.eq(Room::getRoomUserName,wxUser.getPhoneNumber());
}
}
if(StringUtils.isBlank(token) && StringUtils.isBlank(wxToken) ){
Page page = new Page<>(param.getPageNum(),param.getPageSize());
return PageInfo.PageInfo(page);
}
if (StringUtils.isNotBlank(param.getRoomTitle()) ) {
wrapper.like(Room::getRoomTitle,param.getRoomTitle());
}
wrapper.orderByDesc(Room::getCreateTime);
Page page = this.page(new Page<>(param.getPageNum(), param.getPageSize()), wrapper);
return PageInfo.PageInfo(page);
}
@Override
public Room addOrUpdate(RoomAddParam param, String token) {
if(param.getNumList() == null || param.getNumList().size() <=0){
throw new BusinessException(ResultCode.PARAM_MISS);
}
if(StringUtils.isEmpty(param.getRoomTitle()) ){
throw new BusinessException(ResultCode.PARAM_MISS);
}
Room room = new Room();
BeanUtils.copyProperties(param,room);
room.setRoomUserName(JwtUtil.getUserName(token));
if(StringUtils.isEmpty(room.getRoomHostName())){
room.setRoomHostName(room.getRoomUserName());
}
if(StringUtils.isNotBlank(room.getRoomId())){
room.setUpdateTime(null);
Room byId = this.getById(room.getRoomId());
if(byId.getRoomStatus() == 1){
throw new BusinessException(ResultCode.ROOM_ING);
}
}else {
room.setRoomId(RoomUtil.genRoomId());
}
if(param.getNumList().size() >0){
List list = sceneService.getListByNumList(param.getNumList());
if(list.size() >0){
room.setRoomCoverUrl(list.get(0).getThumb());
}
}
this.saveOrUpdate(room);
roomNumService.addBatch(room.getRoomId(),param.getNumList());
param.setRoomId(room.getRoomId());
return param;
}
@Override
public void deleteById(String roomId) {
Room room = this.getById(roomId);
if(room == null ){
throw new BusinessException(ResultCode.ROOM_MISS);
}
if(room.getRoomStatus() == 1){
throw new BusinessException(ResultCode.ROOM_ING);
}
roomNumService.deleteByRoomId(roomId);
if(StringUtils.isNotBlank(room.getRoomShareUrl())){
uploadToOssUtil.delete(room.getRoomShareUrl());
}
this.removeById(roomId);
}
@Override
public RoomVo getRoomInfo(String roomId,String wxToken) {
Room room = this.getById(roomId);
List numList = roomNumService.getListByRoomId(roomId);
List list = new ArrayList<>();
if(numList.size() >0){
list = sceneService.getListByNumList(numList);
}
RoomVo roomVo = new RoomVo();
BeanUtils.copyProperties(room,roomVo);
try {
if(StringUtils.isNotBlank(wxToken)){
WxUser wxUser = wxUserService.getById(JwtUtil.getUserName(wxToken));
if(wxUser.getPhoneNumber().equals(room.getRoomUserName())){
roomVo.setIsHost(1);
}
}
}catch (Exception e){
log.error("查询微信用户失败:{}",e);
}
roomVo.setSceneData(list);
return roomVo;
}
@Override
public void roomAddView(String roomId) {
LambdaUpdateWrapper wrapper = new LambdaUpdateWrapper<>();
wrapper.setSql("room_view_count = room_view_count +1");
wrapper.eq(Room::getRoomId,roomId);
this.update(wrapper);
}
@Override
public String getWxQrCode(String roomId,String type) {
Room room = this.getById(roomId);
if(room == null){
throw new BusinessException(ResultCode.ROOM_MISS);
}
if(type.equals("follower") && StringUtils.isNotBlank(room.getRoomShareCode())){ //普通用户二维码
if(uploadToOssUtil.existKey(room.getRoomShareCode()) && uploadToOssUtil.getSize(room.getRoomShareCode()) >0){
return room.getRoomShareCode() +"?"+System.currentTimeMillis();
}
}
if(type.equals("leader") && StringUtils.isNotBlank(room.getRoomHostCode())){ //主持人二维码
if(uploadToOssUtil.existKey(room.getRoomHostCode()) && uploadToOssUtil.getSize(room.getRoomHostCode()) >0){
return room.getRoomHostCode() +"?"+System.currentTimeMillis();
}
}
if(type.equals("leader")){ //检查房间场景可用性
RoomVo roomVo = this.getRoomInfo(roomId,null);
List sceneData = roomVo.getSceneData();
for (SceneVo sceneDatum : sceneData) {
if(sceneDatum.getStatus() != 2){
throw new BusinessException(ResultCode.SCENE_STATUS_ERROR);
}
}
}
String path = null;
if(type.equals("leader")){
path = String.format(WxSharCodePath.hostCodePath,roomId,type,room.getRoomUserName());
}else {
path = String.format(WxSharCodePath.shardCodePath,roomId,"customer");
}
String localPath = String.format(FilePath.LOCAL_QRCODE_PATH, type,roomId);
String ossPath = String.format(FilePath.OSS_QRCODE_PATH,type, roomId);
wxService.getWxQRCode(path,localPath,0);
if(!new File(localPath).exists()){
throw new BusinessException(ResultCode.QRCODE_MISS);
}
uploadToOssUtil.uploadOss(localPath,ossPath);
if(!uploadToOssUtil.existKey(ossPath)){
FileUtil.del(localPath);
throw new BusinessException(ResultCode.QRCODE_MISS);
}
if(type.equals("leader")){
room.setRoomHostCode(queryPath + ossPath);
}else {
room.setRoomShareCode(queryPath + ossPath);
}
this.updateById(room);
FileUtil.del(localPath);
return queryPath + ossPath +"?"+System.currentTimeMillis();
}
@Override
public void inOrOutRoom(String roomId, String role, Integer type) {
if(roomId == null || StringUtils.isBlank(role)
|| ( !role.equals("leader") && !role.equals("follower"))){
throw new BusinessException(ResultCode.PARAM_MISS);
}
Room room = this.getById(roomId);
if(room == null){
throw new BusinessException(ResultCode.ROOM_MISS);
}
if(role.equals("follower")){
return;
}
if(type == 0 ){
room.setRoomStatus(1);
}
if(type == 1 ){
room.setRoomStatus(2);
}
room.setLastLookTime(DateUtil.formatDateTime(new Date()));
room.setUpdateTime(null);
this.updateById(room);
}
}