Browse Source

用户表t_user增加邀请人ID字段。

cangku
delicacylee 3 years ago
parent
commit
4525b2879b
  1. 3
      hiver-core/src/main/java/cc/hiver/core/entity/User.java
  2. 10
      hiver-core/src/main/java/cc/hiver/core/service/UserService.java
  3. 53
      hiver-core/src/main/java/cc/hiver/core/serviceimpl/UserServiceImpl.java
  4. 66
      hiver-modules/hiver-mall/src/main/java/cc/hiver/mall/controller/CommonController.java

3
hiver-core/src/main/java/cc/hiver/core/entity/User.java

@ -113,4 +113,7 @@ public class User extends HiverBaseEntity {
@TableField(exist = false)
@ApiModelProperty(value = "导入数据时使用")
private Integer defaultRole;
@ApiModelProperty(value = "邀请人ID")
private String inviteCode;
}

10
hiver-core/src/main/java/cc/hiver/core/service/UserService.java

@ -53,6 +53,16 @@ public interface UserService extends HiverBaseService<User, String> {
*/
Page<User> findByCondition(User user, SearchVo searchVo, Pageable pageable);
/**
* 多条件分页获取用户小程序使用
*
* @param user
* @param searchVo
* @param pageable
* @return
*/
Page<User> findByConditionForMobile(User user, SearchVo searchVo, Pageable pageable);
/**
* 通过部门id获取
*

53
hiver-core/src/main/java/cc/hiver/core/serviceimpl/UserServiceImpl.java

@ -163,6 +163,59 @@ public class UserServiceImpl implements UserService {
}, pageable);
}
@Override
public Page<User> findByConditionForMobile(User user, SearchVo searchVo, Pageable pageable) {
return userDao.findAll(new Specification<User>() {
@Nullable
@Override
public Predicate toPredicate(Root<User> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
Path<String> usernameField = root.get("username");
Path<String> nicknameField = root.get("nickname");
Path<String> mobileField = root.get("mobile");
Path<String> sexField = root.get("sex");
Path<Integer> statusField = root.get("status");
Path<Date> createTimeField = root.get("createTime");
Path<String> inviteCodeField = root.get("inviteCode");
List<Predicate> list = new ArrayList<>();
// 模糊搜素
if (StrUtil.isNotBlank(user.getUsername())) {
list.add(cb.like(usernameField, '%' + user.getUsername() + '%'));
}
if (StrUtil.isNotBlank(user.getNickname())) {
list.add(cb.like(nicknameField, '%' + user.getNickname() + '%'));
}
if (StrUtil.isNotBlank(user.getMobile())) {
list.add(cb.like(mobileField, '%' + user.getMobile() + '%'));
}
// 性别
if (StrUtil.isNotBlank(user.getSex())) {
list.add(cb.equal(sexField, user.getSex()));
}
// 邀请人
if (StrUtil.isNotBlank(user.getInviteCode())) {
list.add(cb.equal(inviteCodeField, user.getInviteCode()));
}
// 状态
if (user.getStatus() != null) {
list.add(cb.equal(statusField, user.getStatus()));
}
// 创建时间
if (StrUtil.isNotBlank(searchVo.getStartDate()) && StrUtil.isNotBlank(searchVo.getEndDate())) {
Date start = DateUtil.parse(searchVo.getStartDate());
Date end = DateUtil.parse(searchVo.getEndDate());
list.add(cb.between(createTimeField, start, DateUtil.endOfDay(end)));
}
Predicate[] arr = new Predicate[list.size()];
cq.where(list.toArray(arr));
return null;
}
}, pageable);
}
@Override
public List<User> findByDepartmentId(String departmentId) {
return userDao.findByDepartmentId(departmentId);

66
hiver-modules/hiver-mall/src/main/java/cc/hiver/mall/controller/CommonController.java

@ -2,22 +2,31 @@ package cc.hiver.mall.controller;
import cc.hiver.app.entity.Shop;
import cc.hiver.app.service.mybatis.IShopService;
import cc.hiver.core.common.exception.HiverException;
import cc.hiver.core.common.utils.PageUtil;
import cc.hiver.core.common.utils.ResultUtil;
import cc.hiver.core.common.utils.SecurityUtil;
import cc.hiver.core.common.vo.PageVo;
import cc.hiver.core.common.vo.Result;
import cc.hiver.core.entity.User;
import cc.hiver.core.common.vo.SearchVo;
import cc.hiver.core.entity.*;
import cc.hiver.core.service.UserService;
import cc.hiver.mall.pojo.vo.MallUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.validation.Valid;
import java.util.List;
@Slf4j
@ -26,6 +35,9 @@ import java.util.List;
@RequestMapping(value = "/hiver/app/common/")
@Transactional
public class CommonController {
@Autowired
private UserService userService;
@Autowired
private IShopService shopService;
@ -57,4 +69,56 @@ public class CommonController {
List<Shop> result = shopService.findByUserId(u.getId());
return new ResultUtil<List<Shop>>().setData(result);
}
@RequestMapping(value = "/user/add", method = RequestMethod.POST)
@ApiOperation(value = "添加用户")
public Result add(@Valid User user) {
User u = securityUtil.getCurrUser();
// 清除持久上下文环境 避免后面语句导致持久化
entityManager.detach(u);
if (userService.findByMobile(user.getMobile()) != null) {
throw new HiverException("该手机号已被注册");
}
if(userService.findByUsername(user.getUsername()) != null) {
throw new HiverException(("该用户名已被注册"));
}
// Username/UID 邀请码
user.setInviteCode(u.getId()).setDepartmentId(null).setDepartmentTitle("").setDescription("");
String newEncryptPass = new BCryptPasswordEncoder().encode(user.getPassword());
user.setPassword(newEncryptPass);
User result = userService.save(user);
return ResultUtil.success("添加成功");
}
@RequestMapping(value = "/user/edit", method = RequestMethod.POST)
@ApiOperation(value = "管理员修改资料")
public Result edit(User u) {
User old = userService.get(u.getId());
old.setNickname(u.getNickname()).setEmail(u.getEmail()).setAddress(u.getAddress())
.setStreet(u.getStreet()).setSex(u.getSex()).setAvatar(u.getAvatar());
userService.update(old);
return ResultUtil.success("修改成功");
}
@RequestMapping(value = "/user/delByIds", method = RequestMethod.POST)
@ApiOperation(value = "批量通过ids删除")
public Result delAllByIds(@RequestParam String[] ids) {
for (String id : ids) {
userService.delete(id);
}
return ResultUtil.success("批量通过id删除数据成功");
}
@RequestMapping(value = "/user/getByCondition", method = RequestMethod.GET)
@ApiOperation(value = "多条件分页获取用户列表")
public Result<Page<User>> getByCondition(User user,
SearchVo searchVo,
PageVo pageVo) {
User u = securityUtil.getCurrUser();
// 清除持久上下文环境 避免后面语句导致持久化
entityManager.detach(u);
user.setInviteCode(u.getId()); // 设置关联条件
Page<User> page = userService.findByConditionForMobile(user, searchVo, PageUtil.initPage(pageVo));
return new ResultUtil<Page<User>>().setData(page);
}
}

Loading…
Cancel
Save