diff --git a/luoo_user/src/main/java/com/luoo/user/controller/CMSUserController.java b/luoo_user/src/main/java/com/luoo/user/controller/CMSUserController.java index 71821ec..da4993f 100644 --- a/luoo_user/src/main/java/com/luoo/user/controller/CMSUserController.java +++ b/luoo_user/src/main/java/com/luoo/user/controller/CMSUserController.java @@ -10,8 +10,11 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.web.bind.annotation.*; +import com.luoo.user.dto.UserQueryReq; import com.luoo.user.pojo.UserInfo; import com.luoo.user.service.UserInfoService; + +import annotation.GlobalInterceptor; import annotation.VerifyParam; import api.PageResult; import api.Result; @@ -58,36 +61,13 @@ public class CMSUserController { public Result findById(@PathVariable String id) { return Result.success(userInfoService.findById(id)); } - - /** - * 分页+多条件查询 - * - * @param searchMap 查询条件封装 - * @param page 页码 - * @param size 页大小 - * @return 分页结果 - */ + @ApiOperation(value = "分页+多条件查询") - @ApiImplicitParams({ @ApiImplicitParam(name = "searchMap", value = "查询条件封装", required = true), - @ApiImplicitParam(name = "page", value = "页码", required = true), - @ApiImplicitParam(name = "size", value = "页大小", required = true)}) - @PostMapping("/search/{page}/{size}") - public Result> findSearch(@RequestBody Map searchMap, @PathVariable int page, - @PathVariable int size) { - Page pageList = userInfoService.findSearch(searchMap, page, size); - return Result.success(new PageResult(pageList.getTotalElements(), pageList.getContent())); - } - - /** - * 根据条件查询 - * - * @param searchMap - * @return - */ - @ApiOperation(value = "根据条件查询") - @PostMapping("/search") - public Result> findSearch(@RequestBody Map searchMap) { - return Result.success(userInfoService.findSearch(searchMap)); + @GetMapping("/search") + @GlobalInterceptor + public Result> findSearch(@VerifyParam(required = true) UserQueryReq userQueryReq) { + Page pageList =userInfoService.findSearch(userQueryReq); + return Result.success(new PageResult(pageList.getTotalElements(),pageList.getContent())); } /** @@ -117,15 +97,15 @@ public class CMSUserController { /** * 通过UserInfo用户ids批量查询UserInfo + * * @param ids * @return */ @ApiOperation(value = "通过UserInfo用户ids批量查询UserInfo") - @ApiImplicitParams({ - @ApiImplicitParam(name = "ids", value = "多个id以','分隔", required = true) }) + @ApiImplicitParams({ @ApiImplicitParam(name = "ids", value = "多个id以','分隔", required = true) }) @GetMapping("/ids/{ids}") - public Result> orderByField(@PathVariable @VerifyParam(required=true) String ids){ - List idList=Arrays.stream(ids.split(",")).map(String::trim).collect(Collectors.toList()); + public Result> orderByField(@PathVariable @VerifyParam(required = true) String ids) { + List idList = Arrays.stream(ids.split(",")).map(String::trim).collect(Collectors.toList()); return Result.success(userInfoService.orderByField(idList)); } } diff --git a/luoo_user/src/main/java/com/luoo/user/dto/UserQueryReq.java b/luoo_user/src/main/java/com/luoo/user/dto/UserQueryReq.java new file mode 100644 index 0000000..6253b69 --- /dev/null +++ b/luoo_user/src/main/java/com/luoo/user/dto/UserQueryReq.java @@ -0,0 +1,46 @@ +package com.luoo.user.dto; + +import com.fasterxml.jackson.annotation.JsonFormat; + +import annotation.VerifyParam; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import org.springframework.format.annotation.DateTimeFormat; + +import java.io.Serializable; +import java.util.Date; + +/** + * app用户列表查询参数 + */ +@Data +public class UserQueryReq { + @ApiModelProperty(value = "用户ID") + private String id; + @ApiModelProperty(value = "手机号码") + private String mobile; + @ApiModelProperty(value = "昵称") + private String nickName; + @ApiModelProperty(value = "用户标识,1:贡献者 2:官方 3: 音乐人") + private String badge; + @ApiModelProperty(value = "账号状态 0:停用 1:正常") + private Integer status; + @ApiModelProperty(value = "在线状态 0:离线 1:在线") + private Integer onlineStatus; + @ApiModelProperty(value = "注册时间-开始") + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + private Date createDateStart; + @ApiModelProperty(value = "注册时间-结束") + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + private Date createDateEnd; + @ApiModelProperty(value = "分页: 页码", example = "1") + @VerifyParam(required = true) + private Integer pageNum = 1; + + @ApiModelProperty(value = "分页: 每页数量", example = "10") + @VerifyParam(required = true) + private Integer pageSize = 10; +} diff --git a/luoo_user/src/main/java/com/luoo/user/pojo/UserInfo.java b/luoo_user/src/main/java/com/luoo/user/pojo/UserInfo.java index 680046d..6bb67d6 100644 --- a/luoo_user/src/main/java/com/luoo/user/pojo/UserInfo.java +++ b/luoo_user/src/main/java/com/luoo/user/pojo/UserInfo.java @@ -63,6 +63,8 @@ public class UserInfo implements Serializable { /** * 出生年月日 */ + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") + @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") private Date birthday; /** * 粉丝数 diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java b/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java index 9b9040b..3e36302 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java @@ -9,6 +9,7 @@ import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; import javax.servlet.http.HttpServletRequest; +import org.apache.commons.lang.StringUtils; import org.apache.commons.lang3.RandomStringUtils; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; @@ -25,7 +26,7 @@ import util.IdWorker; import constants.Constants; import com.luoo.user.dao.UserInfoDao; -import com.luoo.user.pojo.Admin; +import com.luoo.user.dto.UserQueryReq; import com.luoo.user.pojo.UserInfo; import com.luoo.user.util.NickNameUtil; @@ -82,31 +83,6 @@ public class UserInfoService { return userInfoDao.findAll(); } - /** - * 条件查询+分页 - * - * @param whereMap - * @param page - * @param size - * @return - */ - public Page findSearch(Map whereMap, int page, int size) { - Specification specification = createSpecification(whereMap); - PageRequest pageRequest = PageRequest.of(page - 1, size); - return userInfoDao.findAll(specification, pageRequest); - } - - /** - * 条件查询 - * - * @param whereMap - * @return - */ - public List findSearch(Map whereMap) { - Specification specification = createSpecification(whereMap); - return userInfoDao.findAll(specification); - } - /** * 根据ID查询实体 * @@ -141,76 +117,6 @@ public class UserInfoService { userInfoDao.deleteById(id); } - /** - * 动态条件构建 - * - * @param searchMap - * @return - */ - private Specification createSpecification(Map searchMap) { - - return new Specification() { - - @Override - public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) { - List predicateList = new ArrayList(); - // ID - if (searchMap.get("id") != null && !"".equals(searchMap.get("id"))) { - predicateList - .add(cb.like(root.get("id").as(String.class), "%" + (String) searchMap.get("id") + "%")); - } - // 手机号码 - if (searchMap.get("mobile") != null && !"".equals(searchMap.get("mobile"))) { - predicateList.add( - cb.like(root.get("mobile").as(String.class), "%" + (String) searchMap.get("mobile") + "%")); - } - // 用户名 - if (searchMap.get("loginname") != null && !"".equals(searchMap.get("loginname"))) { - predicateList.add(cb.like(root.get("loginname").as(String.class), - "%" + (String) searchMap.get("loginname") + "%")); - } - // 密码 - if (searchMap.get("password") != null && !"".equals(searchMap.get("password"))) { - predicateList.add(cb.like(root.get("password").as(String.class), - "%" + (String) searchMap.get("password") + "%")); - } - // 昵称 - if (searchMap.get("nickname") != null && !"".equals(searchMap.get("nickname"))) { - predicateList.add(cb.like(root.get("nickname").as(String.class), - "%" + (String) searchMap.get("nickname") + "%")); - } - // 性别 - if (searchMap.get("sex") != null && !"".equals(searchMap.get("sex"))) { - predicateList - .add(cb.like(root.get("sex").as(String.class), "%" + (String) searchMap.get("sex") + "%")); - } - // 头像 - if (searchMap.get("avatar") != null && !"".equals(searchMap.get("avatar"))) { - predicateList.add( - cb.like(root.get("avatar").as(String.class), "%" + (String) searchMap.get("avatar") + "%")); - } - // E-Mail - if (searchMap.get("email") != null && !"".equals(searchMap.get("email"))) { - predicateList.add( - cb.like(root.get("email").as(String.class), "%" + (String) searchMap.get("email") + "%")); - } - // 兴趣 - if (searchMap.get("interest") != null && !"".equals(searchMap.get("interest"))) { - predicateList.add(cb.like(root.get("interest").as(String.class), - "%" + (String) searchMap.get("interest") + "%")); - } - // 个性 - if (searchMap.get("personality") != null && !"".equals(searchMap.get("personality"))) { - predicateList.add(cb.like(root.get("personality").as(String.class), - "%" + (String) searchMap.get("personality") + "%")); - } - - return cb.and(predicateList.toArray(new Predicate[predicateList.size()])); - - } - }; - - } public void sendSms(String deviceId, String mobile) { // 生成6位数字随机数 @@ -315,4 +221,61 @@ public class UserInfoService { public List getSimpleUserOrderByField(List idList) { return userInfoDao.getSimpleUserOrderByField(idList); } + + public Page findSearch(UserQueryReq queryReq) { + Specification specification = createUserInfoSpecification(queryReq); + PageRequest pageRequest = PageRequest.of(queryReq.getPageNum()-1, queryReq.getPageSize()); + Page userInfoPage = userInfoDao.findAll(specification, pageRequest); + return userInfoPage.map(this::updateAvatarPath); + } + + private UserInfo updateAvatarPath(UserInfo userInfo) { + if (null != userInfo.getAvatar()) { + userInfo.setAvatar(Constants.RESOURCE_PREFIX + userInfo.getAvatar()); + } + return userInfo; + } + public Page findSearch(UserQueryReq queryReq, int page, int size) { + Specification specification = createUserInfoSpecification(queryReq); + PageRequest pageRequest = PageRequest.of(page - 1, size); + return userInfoDao.findAll(specification, pageRequest); + } + private Specification createUserInfoSpecification(UserQueryReq queryReq) { + return (root, criteriaQuery, criteriaBuilder) -> { + List predicateList = new ArrayList<>(); + if(StringUtils.isNotBlank(queryReq.getId())){ + predicateList.add(criteriaBuilder.equal(root.get("id"), queryReq.getId())); + } + if(StringUtils.isNotBlank(queryReq.getMobile())){ + predicateList.add(criteriaBuilder.equal(root.get("mobile").as(String.class), queryReq.getMobile())); + } + if(StringUtils.isNotBlank(queryReq.getNickName())){ + predicateList.add(criteriaBuilder.like(root.get("nickName"), '%'+queryReq.getNickName()+'%')); + } + if(StringUtils.isNotBlank(queryReq.getBadge())){ + predicateList.add(criteriaBuilder.equal(root.get("badge").as(String.class), queryReq.getBadge())); + } + if(Objects.nonNull(queryReq.getStatus())){ + predicateList.add(criteriaBuilder.equal(root.get("status").as(Integer.class), queryReq.getStatus())); + } + /* + * if(Objects.nonNull(queryReq.getOnlineStatus())){ + * predicateList.add(criteriaBuilder.equal(root.get("onlineStatus").as(Integer. + * class), queryReq.getOnlineStatus())); } + */ + + if(Objects.nonNull(queryReq.getCreateDateStart())){ + predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("joinTime").as(Date.class), + queryReq.getCreateDateStart())); + } + if(Objects.nonNull(queryReq.getCreateDateEnd())){ + predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("joinTime").as(Date.class), + queryReq.getCreateDateEnd())); + } + Predicate[] predicates = new Predicate[predicateList.size()]; + return criteriaBuilder.and(predicateList.toArray(predicates)); + }; + } + + }