move friend service to luoo_user

main
wangqing 10 months ago
parent 80e4db53b6
commit 4a8ca4f2e6

@ -0,0 +1,89 @@
package com.luoo.user.controller;
import api.Result;
import api.StatusCode;
import com.luoo.user.service.FriendService;
import com.luoo.user.service.UserInfoService;
import controller.BaseController;
import dto.UserLoginDto;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
@RestController
@RequestMapping("/friend")
public class FriendController extends BaseController {
@Autowired
private HttpServletRequest request;
@Autowired
private FriendService friendService;
@Autowired
private UserInfoService userInfoService;
/**
* 12
*
* @return
*/
@PutMapping("/like/{friendid}/{type}")
public Result<Void> addFriend(@PathVariable String friendid, @PathVariable String type) {
//验证是否登录并且拿到ID
String token = (String) request.getAttribute("claims_user");
UserLoginDto userLoginDto = getUserLoginDto(token);
if (null == userLoginDto) {
return Result.unauthorized(null);
}
String userid = userLoginDto.getUserId();
//判断是添加好友还是非好友(判断是关注操作还是拉黑操作)
if (type != null) {
if (type.equals("1")) {
//添加好友
int flag = friendService.addFriend(userid,friendid);
if (flag == 0) {
return Result.failed(StatusCode.FRIEND_CAN_NOT_REPEAT_ADD_FRIEND); // 不能重复添加好友
}
if (flag == 1) {
userInfoService.updatefanscountandfollowcount(1,userid,friendid);
return Result.success();
}
} else if (type.equals("2")) {
//添加非好友
int flag = friendService.addNoFriend(userid,friendid);
if (flag == 0) {
return Result.failed(StatusCode.FRIEND_CAN_NOT_REPEAT_ADD_NON_FRIEND);
}
if (flag == 1) {
return Result.success();
}
}
return Result.failed(StatusCode.VALIDATE_FAILED);
} else {
return Result.failed(StatusCode.VALIDATE_FAILED);
}
}
@DeleteMapping("/{friendid}")
public Result<Void> deleteFriend(@PathVariable String friendid){
//验证是否登录并且拿到ID
String token = (String) request.getAttribute("claims_user");
UserLoginDto userLoginDto = getUserLoginDto(token);
if (null == userLoginDto) {
return Result.unauthorized(null);
}
String userid = userLoginDto.getUserId();
friendService.deleteFriend(userid,friendid);
userInfoService.updatefanscountandfollowcount(-1,userid,friendid);
return Result.success();
}
}

@ -7,7 +7,10 @@ import java.io.InputStream;
import java.nio.file.Paths;
import java.util.Date;
import java.util.EnumMap;
import java.util.List;
import com.luoo.user.pojo.UserCollectInfo;
import com.luoo.user.service.UserCollectInfoService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
@ -27,7 +30,6 @@ import controller.BaseController;
import com.luoo.user.dto.response.UserRespDTO;
import com.luoo.user.pojo.UserInfo;
import com.luoo.user.service.S3Service;
import com.luoo.user.service.UserCollectService;
import com.luoo.user.service.UserInfoService;
import annotation.GlobalInterceptor;
@ -56,7 +58,9 @@ public class MyController extends BaseController{
private UserInfoService userInfoService;
@Autowired
private UserCollectService userCollectService;
private UserCollectInfoService userCollectInfoService;
// private UserCollectService userCollectService;
public static String UPLOAD_DIRECTORY = "user/avatar/";
@ -70,20 +74,32 @@ public class MyController extends BaseController{
}
UserRespDTO userRespDTO = new UserRespDTO();
UserInfo user = userInfoService.findById(userLoginDto.getUserId());
UserCollectInfo userCollectInfo = userCollectInfoService.findByUserId(userLoginDto.getUserId());
List songList =userCollectInfo.getSongList();
List journalList = userCollectInfo.getJournalList();
BeanUtils.copyProperties(user, userRespDTO);
//EnumMap<CollectTypeEnum,Long> map=userCollectService.getUserCollectTypeMap(user.getId());
int fansCount=userCollectService.getFansCount(user.getId());
int followCount=userCollectService.getCount(user.getId(),CollectTypeEnum.FOLLOW.getType());
int thumbUpCount=userCollectService.getCount(user.getId(),CollectTypeEnum.THUMB_UP.getType());
int songCount=userCollectService.getCount(user.getId(),CollectTypeEnum.SONG.getType());
int journalCount=userCollectService.getCount(user.getId(),CollectTypeEnum.JOURNAL.getType());
userRespDTO.setFollowCount(followCount);
userRespDTO.setFansCount(fansCount);
userRespDTO.setThumbUpCount(thumbUpCount);
userRespDTO.setCommentReplyCount(0);
userRespDTO.setSongCount(songCount);
userRespDTO.setJournalCount(journalCount);
// int fansCount=userCollectService.getFansCount(user.getId());
// int followCount=userCollectService.getCount(user.getId(),CollectTypeEnum.FOLLOW.getType());
// int thumbUpCount=userCollectService.getCount(user.getId(),CollectTypeEnum.THUMB_UP.getType());
// int songCount=userCollectService.getCount(user.getId(),CollectTypeEnum.SONG.getType());
// int journalCount=userCollectService.getCount(user.getId(),CollectTypeEnum.JOURNAL.getType());
//
// userRespDTO.setFollowCount(followCount);
// userRespDTO.setFansCount(fansCount);
// userRespDTO.setThumbUpCount(thumbUpCount);
// userRespDTO.setCommentReplyCount(0);
// userRespDTO.setSongCount(songCount);
// userRespDTO.setJournalCount(journalCount);
userRespDTO.setJournalList(journalList);
userRespDTO.setSongList(songList);
if(null != songList){
userRespDTO.setSongCount(songList.size());
}
if(null != journalList) {
userRespDTO.setJournalCount(journalList.size());
}
if(null!=user.getBirthday()) {
userRespDTO.setBirthDay(DateUtil.format(user.getBirthday(), DateTimePatternEnum.YYYY_DOT_MM_DOT_DD.getPattern()));
}

@ -24,7 +24,7 @@ public class UserCollectInfoController {
@GetMapping("/show")
public Result show(){
UserCollectInfo userCollectInfo =userCollectInfoService.findByUserId();
UserCollectInfo userCollectInfo =userCollectInfoService.findByUserId("111222");
return Result.success(userCollectInfo);
}

@ -0,0 +1,19 @@
package com.luoo.user.dao;
import com.luoo.user.pojo.Friend;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
public interface FriendDao extends JpaRepository<Friend,String> {
public Friend findByUseridAndFriendid(String userid,String friendid);
@Modifying
@Query(value = "update tb_friend set islike = ? where userid = ? and friendid = ?",nativeQuery = true)
public void updateIslike(String islike,String userid,String friendid);
@Modifying
@Query(value = "delete from tb_friend where userid = ? and friendid = ?",nativeQuery = true)
void deletefriend(String userid, String friendid);
}

@ -0,0 +1,11 @@
package com.luoo.user.dao;
import com.luoo.user.pojo.NoFriend;
import org.springframework.data.jpa.repository.JpaRepository;
public interface NoFriendDao extends JpaRepository<NoFriend,String> {
public NoFriend findByUseridAndFriendid(String userid,String friendid);
}

@ -1,8 +1,12 @@
package com.luoo.user.dto.response;
import com.luoo.user.dto.UserCollectJournalDto;
import com.luoo.user.dto.UserCollectSongDto;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.List;
@Data
public class UserRespDTO {
@ApiModelProperty(value = "ID")
@ -25,8 +29,12 @@ public class UserRespDTO {
private int thumbUpCount;
@ApiModelProperty(value = "喜欢歌曲数")
private int songCount;
private List songList;
@ApiModelProperty(value = "收藏期刊数")
private int journalCount;
private List journalList;
@ApiModelProperty(value = "获得评论数")
private int commentReplyCount;
@ApiModelProperty(value = "生日,格式为: yyyy.MM.dd")

@ -0,0 +1,45 @@
package com.luoo.user.pojo;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Table;
import java.io.Serializable;
@Entity
@Table(name = "tb_friend")
@IdClass(Friend.class)
public class Friend implements Serializable {
@Id
private String userid;
@Id
private String friendid;
private String islike;
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getFriendid() {
return friendid;
}
public void setFriendid(String friendid) {
this.friendid = friendid;
}
public String getIslike() {
return islike;
}
public void setIslike(String islike) {
this.islike = islike;
}
}

@ -0,0 +1,38 @@
package com.luoo.user.pojo;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Table;
import java.io.Serializable;
@Entity
@Table(name = "tb_nofriend")
@IdClass(NoFriend.class)
public class NoFriend implements Serializable {
@Id
private String userid;
@Id
private String friendid;
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getFriendid() {
return friendid;
}
public void setFriendid(String friendid) {
this.friendid = friendid;
}
}

@ -0,0 +1,72 @@
package com.luoo.user.service;
import com.luoo.user.dao.FriendDao;
import com.luoo.user.dao.NoFriendDao;
import com.luoo.user.pojo.Friend;
import com.luoo.user.pojo.NoFriend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class FriendService {
@Autowired
private FriendDao friendDao;
@Autowired
private NoFriendDao noFriendDao;
public int addFriend(String userid, String friendid) {
// 先判断 userid到friendid是否有数据有就是重复添加好友
Friend friend = friendDao.findByUseridAndFriendid(userid, friendid);
if (friend!=null) {
return 0;
}
// 直接添加好友让好友表中userid到friend方向的type为0
friend = new Friend();
friend.setUserid(userid);
friend.setFriendid(friendid);
friend.setIslike("0");
friendDao.save(friend);
//判断从friendid到userid是否有数据如果有把双方的状态都改为1
if (friendDao.findByUseridAndFriendid(friendid,userid)!=null) {
// 把双方的islike改成1
friendDao.updateIslike("1",userid,friendid);
friendDao.updateIslike("1",friendid,userid);
}
return 1;
}
public int addNoFriend(String userid, String friendid) {
//先判断是否已经是非好友
NoFriend nofriend = noFriendDao.findByUseridAndFriendid(userid, friendid);
if (nofriend !=null) {
return 0;
}
nofriend = new NoFriend();
nofriend.setUserid(userid);
nofriend.setFriendid(friendid);
noFriendDao.save(nofriend);
return 1;
}
public void deleteFriend(String userid, String friendid) {
// 删除好友表中userid到friendid这条数据
friendDao.deletefriend(userid,friendid);
// 更新friendid到userid的islike为0
friendDao.updateIslike("0",friendid,userid);
// 非好友表中添加数据
NoFriend nofriend = new NoFriend();
nofriend.setFriendid(friendid);
nofriend.setUserid(userid);
noFriendDao.save(nofriend);
}
}

@ -45,9 +45,9 @@ public class UserCollectInfoService {
}
public UserCollectInfo findByUserId(){
public UserCollectInfo findByUserId(String userId){
return userCollectInfoDao.findUserCollectInfoByUserId("111222");
return userCollectInfoDao.findUserCollectInfoByUserId(userId);
}
public void unCollect(){

Loading…
Cancel
Save