WxAppServer/src/main/java/com/bigdata/wxappserver/service/UserService.java

81 lines
3.3 KiB
Java
Raw Normal View History

2024-08-29 16:46:49 +08:00
package com.bigdata.wxappserver.service;
2024-08-29 23:50:44 +08:00
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
2024-08-29 16:46:49 +08:00
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bigdata.wxappserver.entity.User;
import com.bigdata.wxappserver.mapper.UserMapper;
import org.springframework.stereotype.Service;
2024-08-29 23:50:44 +08:00
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
2024-08-29 16:46:49 +08:00
@Service
public class UserService extends ServiceImpl<UserMapper, User> {
2024-08-29 23:50:44 +08:00
@Transactional(rollbackFor = Exception.class)
public JSONObject addOrUpdate(JSONObject jsonObject) {
User user = jsonObject.toJavaObject(User.class);
if (user == null) {
return new JSONObject().fluentPut("success", false).fluentPut("message", "参数错误");
}
List<User> userList = list();
List<String> openIdList = userList.stream().map(User::getOpenId).collect(Collectors.toList());
if (user.getId() == null && !CollectionUtils.isEmpty(openIdList) && openIdList.contains(user.getOpenId())) {
return new JSONObject().fluentPut("message", "该用户已存在").fluentPut("success", false);
}
// TODO 判空
boolean success = saveOrUpdate(user);
return new JSONObject().fluentPut("success", success);
}
public JSONObject list(JSONObject jsonObject) {
String userId = jsonObject.getString("userId");
Integer currentPage = jsonObject.getInteger("currentPage");
Integer pageSize = jsonObject.getInteger("pageSize");
IPage<User> page = new Page<>(currentPage, pageSize);
LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
wrapper.eq(userId != null, User::getId, userId);
IPage<User> userIPage = page(page, wrapper);
dealList(userIPage.getRecords());
JSONObject returnData = new JSONObject();
returnData.fluentPut("data", userIPage.getRecords())
.fluentPut("total", userIPage.getTotal())
.fluentPut("current", userIPage.getCurrent());
return returnData;
}
private void dealList(List<User> records) {
if (CollectionUtils.isEmpty(records)) {
return;
}
}
@Transactional(rollbackFor = Exception.class)
public JSONObject delete(Integer id) {
if (id == null) {
return new JSONObject().fluentPut("success", false).fluentPut("message", "缺少Id");
}
boolean success = removeById(id);
if (success) {
return new JSONObject().fluentPut("success", true);
} else {
return new JSONObject().fluentPut("success", false).fluentPut("message", "出现未知错误,请联系管理员!");
}
}
public JSONObject queryById(Integer id) {
if (id == null) {
return new JSONObject().fluentPut("success", false).fluentPut("message", "缺少ID");
}
User user = getById(id);
return new JSONObject().fluentPut("data", user);
}
2024-08-29 16:46:49 +08:00
}