diff --git a/hiver-core/src/main/java/cc/hiver/core/entity/User.java b/hiver-core/src/main/java/cc/hiver/core/entity/User.java index 33b1d6c5..1114550d 100644 --- a/hiver-core/src/main/java/cc/hiver/core/entity/User.java +++ b/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; } diff --git a/hiver-core/src/main/java/cc/hiver/core/service/UserService.java b/hiver-core/src/main/java/cc/hiver/core/service/UserService.java index 9353bef2..7b01e101 100644 --- a/hiver-core/src/main/java/cc/hiver/core/service/UserService.java +++ b/hiver-core/src/main/java/cc/hiver/core/service/UserService.java @@ -53,6 +53,16 @@ public interface UserService extends HiverBaseService { */ Page findByCondition(User user, SearchVo searchVo, Pageable pageable); + /** + * 多条件分页获取用户(小程序使用) + * + * @param user + * @param searchVo + * @param pageable + * @return + */ + Page findByConditionForMobile(User user, SearchVo searchVo, Pageable pageable); + /** * 通过部门id获取 * diff --git a/hiver-core/src/main/java/cc/hiver/core/serviceimpl/UserServiceImpl.java b/hiver-core/src/main/java/cc/hiver/core/serviceimpl/UserServiceImpl.java index 265cba4b..01458b25 100644 --- a/hiver-core/src/main/java/cc/hiver/core/serviceimpl/UserServiceImpl.java +++ b/hiver-core/src/main/java/cc/hiver/core/serviceimpl/UserServiceImpl.java @@ -163,6 +163,59 @@ public class UserServiceImpl implements UserService { }, pageable); } + @Override + public Page findByConditionForMobile(User user, SearchVo searchVo, Pageable pageable) { + return userDao.findAll(new Specification() { + @Nullable + @Override + public Predicate toPredicate(Root root, CriteriaQuery cq, CriteriaBuilder cb) { + Path usernameField = root.get("username"); + Path nicknameField = root.get("nickname"); + Path mobileField = root.get("mobile"); + Path sexField = root.get("sex"); + Path statusField = root.get("status"); + Path createTimeField = root.get("createTime"); + Path inviteCodeField = root.get("inviteCode"); + + List 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 findByDepartmentId(String departmentId) { return userDao.findByDepartmentId(departmentId); diff --git a/hiver-modules/hiver-mall/src/main/java/cc/hiver/mall/controller/CommonController.java b/hiver-modules/hiver-mall/src/main/java/cc/hiver/mall/controller/CommonController.java index 7c475c58..a5971bcc 100644 --- a/hiver-modules/hiver-mall/src/main/java/cc/hiver/mall/controller/CommonController.java +++ b/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 result = shopService.findByUserId(u.getId()); return new ResultUtil>().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> getByCondition(User user, + SearchVo searchVo, + PageVo pageVo) { + User u = securityUtil.getCurrUser(); + // 清除持久上下文环境 避免后面语句导致持久化 + entityManager.detach(u); + user.setInviteCode(u.getId()); // 设置关联条件 + Page page = userService.findByConditionForMobile(user, searchVo, PageUtil.initPage(pageVo)); + return new ResultUtil>().setData(page); + } }