@ -1,7 +1,10 @@
package cc.hiver.core.serviceimpl ;
import cc.hiver.core.common.constant.CommonConstant ;
import cc.hiver.core.common.constant.UserConstant ;
import cc.hiver.core.common.utils.PageUtil ;
import cc.hiver.core.common.utils.SecurityUtil ;
import cc.hiver.core.common.vo.PageVo ;
import cc.hiver.core.common.vo.SearchVo ;
import cc.hiver.core.dao.UserDao ;
import cc.hiver.core.dao.mapper.PermissionMapper ;
@ -9,12 +12,17 @@ import cc.hiver.core.dao.mapper.UserRoleMapper;
import cc.hiver.core.entity.Permission ;
import cc.hiver.core.entity.Role ;
import cc.hiver.core.entity.User ;
import cc.hiver.core.entity.UserRole ;
import cc.hiver.core.service.RoleService ;
import cc.hiver.core.service.UserRoleService ;
import cc.hiver.core.service.UserService ;
import cc.hiver.core.vo.PermissionDTO ;
import cc.hiver.core.vo.RoleDTO ;
import cn.hutool.core.date.DateUtil ;
import cn.hutool.core.util.Str Util ;
import cn.hutool.core.text.CharSequence Util ;
import lombok.extern.slf4j.Slf4j ;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage ;
import me.chanjar.weixin.mp.bean.result.WxMpUser ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.data.domain.Page ;
import org.springframework.data.domain.Pageable ;
@ -24,9 +32,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional ;
import javax.persistence.criteria.* ;
import java.util.ArrayList ;
import java.util.Date ;
import java.util.List ;
import java.util.* ;
import java.util.stream.Collectors ;
/ * *
@ -50,6 +56,12 @@ public class UserServiceImpl implements UserService {
@Autowired
private SecurityUtil securityUtil ;
@Autowired
private UserRoleService userRoleService ;
@Autowired
private RoleService roleService ;
@Override
public UserDao getRepository ( ) {
return userDao ;
@ -57,35 +69,36 @@ public class UserServiceImpl implements UserService {
@Override
public User findByUsername ( String username ) {
User user = userDao . findByUsername ( username ) ;
final User user = userDao . findByUsername ( username ) ;
return userToDTO ( user ) ;
}
@Override
public User findByMobile ( String mobile ) {
User user = userDao . findByMobile ( mobile ) ;
final User user = userDao . findByMobile ( mobile ) ;
return userToDTO ( user ) ;
}
@Override
public User findByEmail ( String email ) {
User user = userDao . findByEmail ( email ) ;
final User user = userDao . findByEmail ( email ) ;
return userToDTO ( user ) ;
}
@Override
public User userToDTO ( User user ) {
if ( user = = null ) {
return null ;
}
// 关联角色
List < Role > roleList = userRoleMapper . findByUserId ( user . getId ( ) ) ;
List < RoleDTO > roleDTOList = roleList . stream ( ) . map ( e - > {
final List < Role > roleList = userRoleMapper . findByUserId ( user . getId ( ) ) ;
final List < RoleDTO > roleDTOList = roleList . stream ( ) . map ( e - > {
return new RoleDTO ( ) . setId ( e . getId ( ) ) . setName ( e . getName ( ) ) ;
} ) . collect ( Collectors . toList ( ) ) ;
user . setRoles ( roleDTOList ) ;
// 关联权限菜单
List < Permission > permissionList = permissionMapper . findByUserId ( user . getId ( ) ) ;
List < PermissionDTO > permissionDTOList = permissionList . stream ( )
final List < Permission > permissionList = permissionMapper . findByUserId ( user . getId ( ) ) ;
final List < PermissionDTO > permissionDTOList = permissionList . stream ( )
. filter ( e - > CommonConstant . PERMISSION_OPERATION . equals ( e . getType ( ) ) )
. map ( e - > {
return new PermissionDTO ( ) . setTitle ( e . getTitle ( ) ) . setPath ( e . getPath ( ) ) ;
@ -100,43 +113,43 @@ public class UserServiceImpl implements UserService {
@Nullable
@Override
public Predicate toPredicate ( Root < User > root , CriteriaQuery < ? > cq , CriteriaBuilder cb ) {
Path < String > idField = root . get ( "id" ) ;
Path < String > usernameField = root . get ( "username" ) ;
Path < String > nicknameField = root . get ( "nickname" ) ;
Path < String > mobileField = root . get ( "mobile" ) ;
Path < String > emailField = root . get ( "email" ) ;
Path < String > departmentIdField = root . get ( "departmentId" ) ;
Path < String > sexField = root . get ( "sex" ) ;
Path < Integer > statusField = root . get ( "status" ) ;
Path < Date > createTimeField = root . get ( "createTime" ) ;
List < Predicate > list = new ArrayList < > ( ) ;
if ( Str Util. isNotBlank ( user . getId ( ) ) ) {
final Path < String > idField = root . get ( "id" ) ;
final Path < String > usernameField = root . get ( "username" ) ;
final Path < String > nicknameField = root . get ( "nickname" ) ;
final Path < String > mobileField = root . get ( "mobile" ) ;
final Path < String > emailField = root . get ( "email" ) ;
final Path < String > departmentIdField = root . get ( "departmentId" ) ;
final Path < String > sexField = root . get ( "sex" ) ;
final Path < Integer > statusField = root . get ( "status" ) ;
final Path < Date > createTimeField = root . get ( "createTime" ) ;
final List < Predicate > list = new ArrayList < > ( ) ;
if ( CharSequence Util. isNotBlank ( user . getId ( ) ) ) {
list . add ( cb . equal ( idField , user . getId ( ) ) ) ;
}
// 模糊搜素
if ( Str Util. isNotBlank ( user . getUsername ( ) ) ) {
if ( CharSequence Util. isNotBlank ( user . getUsername ( ) ) ) {
list . add ( cb . like ( usernameField , '%' + user . getUsername ( ) + '%' ) ) ;
}
if ( Str Util. isNotBlank ( user . getNickname ( ) ) ) {
if ( CharSequence Util. isNotBlank ( user . getNickname ( ) ) ) {
list . add ( cb . like ( nicknameField , '%' + user . getNickname ( ) + '%' ) ) ;
}
if ( Str Util. isNotBlank ( user . getMobile ( ) ) ) {
if ( CharSequence Util. isNotBlank ( user . getMobile ( ) ) ) {
list . add ( cb . like ( mobileField , '%' + user . getMobile ( ) + '%' ) ) ;
}
if ( Str Util. isNotBlank ( user . getEmail ( ) ) ) {
if ( CharSequence Util. isNotBlank ( user . getEmail ( ) ) ) {
list . add ( cb . like ( emailField , '%' + user . getEmail ( ) + '%' ) ) ;
}
// 部门
if ( Str Util. isNotBlank ( user . getDepartmentId ( ) ) ) {
if ( CharSequence Util. isNotBlank ( user . getDepartmentId ( ) ) ) {
list . add ( cb . equal ( departmentIdField , user . getDepartmentId ( ) ) ) ;
}
// 性别
if ( Str Util. isNotBlank ( user . getSex ( ) ) ) {
if ( CharSequence Util. isNotBlank ( user . getSex ( ) ) ) {
list . add ( cb . equal ( sexField , user . getSex ( ) ) ) ;
}
// 状态
@ -144,19 +157,19 @@ public class UserServiceImpl implements UserService {
list . add ( cb . equal ( statusField , user . getStatus ( ) ) ) ;
}
// 创建时间
if ( Str Util. isNotBlank ( searchVo . getStartDate ( ) ) & & Str Util. isNotBlank ( searchVo . getEndDate ( ) ) ) {
Date start = DateUtil . parse ( searchVo . getStartDate ( ) ) ;
Date end = DateUtil . parse ( searchVo . getEndDate ( ) ) ;
if ( CharSequence Util. isNotBlank ( searchVo . getStartDate ( ) ) & & CharSequence Util. isNotBlank ( searchVo . getEndDate ( ) ) ) {
final Date start = DateUtil . parse ( searchVo . getStartDate ( ) ) ;
final Date end = DateUtil . parse ( searchVo . getEndDate ( ) ) ;
list . add ( cb . between ( createTimeField , start , DateUtil . endOfDay ( end ) ) ) ;
}
// 数据权限
List < String > depIds = securityUtil . getDeparmentIds ( ) ;
final List < String > depIds = securityUtil . getDeparmentIds ( ) ;
if ( depIds ! = null & & depIds . size ( ) > 0 ) {
list . add ( departmentIdField . in ( depIds ) ) ;
}
Predicate [ ] arr = new Predicate [ list . size ( ) ] ;
final Predicate [ ] arr = new Predicate [ list . size ( ) ] ;
cq . where ( list . toArray ( arr ) ) ;
return null ;
}
@ -169,33 +182,33 @@ public class UserServiceImpl implements UserService {
@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" ) ;
final Path < String > usernameField = root . get ( "username" ) ;
final Path < String > nicknameField = root . get ( "nickname" ) ;
final Path < String > mobileField = root . get ( "mobile" ) ;
final Path < String > sexField = root . get ( "sex" ) ;
final Path < Integer > statusField = root . get ( "status" ) ;
final Path < Date > createTimeField = root . get ( "createTime" ) ;
final Path < String > inviteCodeField = root . get ( "inviteCode" ) ;
List < Predicate > list = new ArrayList < > ( ) ;
final List < Predicate > list = new ArrayList < > ( ) ;
// 模糊搜素
if ( Str Util. isNotBlank ( user . getUsername ( ) ) ) {
if ( CharSequence Util. isNotBlank ( user . getUsername ( ) ) ) {
list . add ( cb . like ( usernameField , '%' + user . getUsername ( ) + '%' ) ) ;
}
if ( Str Util. isNotBlank ( user . getNickname ( ) ) ) {
if ( CharSequence Util. isNotBlank ( user . getNickname ( ) ) ) {
list . add ( cb . like ( nicknameField , '%' + user . getNickname ( ) + '%' ) ) ;
}
if ( Str Util. isNotBlank ( user . getMobile ( ) ) ) {
if ( CharSequence Util. isNotBlank ( user . getMobile ( ) ) ) {
list . add ( cb . like ( mobileField , '%' + user . getMobile ( ) + '%' ) ) ;
}
// 性别
if ( Str Util. isNotBlank ( user . getSex ( ) ) ) {
if ( CharSequence Util. isNotBlank ( user . getSex ( ) ) ) {
list . add ( cb . equal ( sexField , user . getSex ( ) ) ) ;
}
// 邀请人
if ( Str Util. isNotBlank ( user . getInviteCode ( ) ) ) {
if ( CharSequence Util. isNotBlank ( user . getInviteCode ( ) ) ) {
list . add ( cb . equal ( inviteCodeField , user . getInviteCode ( ) ) ) ;
}
// 状态
@ -203,13 +216,13 @@ public class UserServiceImpl implements UserService {
list . add ( cb . equal ( statusField , user . getStatus ( ) ) ) ;
}
// 创建时间
if ( Str Util. isNotBlank ( searchVo . getStartDate ( ) ) & & Str Util. isNotBlank ( searchVo . getEndDate ( ) ) ) {
Date start = DateUtil . parse ( searchVo . getStartDate ( ) ) ;
Date end = DateUtil . parse ( searchVo . getEndDate ( ) ) ;
if ( CharSequence Util. isNotBlank ( searchVo . getStartDate ( ) ) & & CharSequence Util. isNotBlank ( searchVo . getEndDate ( ) ) ) {
final Date start = DateUtil . parse ( searchVo . getStartDate ( ) ) ;
final Date end = DateUtil . parse ( searchVo . getEndDate ( ) ) ;
list . add ( cb . between ( createTimeField , start , DateUtil . endOfDay ( end ) ) ) ;
}
Predicate [ ] arr = new Predicate [ list . size ( ) ] ;
final Predicate [ ] arr = new Predicate [ list . size ( ) ] ;
cq . where ( list . toArray ( arr ) ) ;
return null ;
}
@ -241,16 +254,86 @@ public class UserServiceImpl implements UserService {
return userDao . findByRoleName ( roleName ) ;
}
public User findByUserNameAndType ( String userName , String type ) {
User user = userDao . findByUserNameAndType ( userName , type ) ;
@Override
public User findByUserNameAndType ( String userName , String type ) {
final User user = userDao . findByUserNameAndType ( userName , type ) ;
return userToDTO ( user ) ;
}
public User findByMobileAndType ( String mobile , String type ) {
User user = userDao . findByMobileAndType ( mobile , type ) ;
@Override
public User findByMobileAndType ( String mobile , String type ) {
final User user = userDao . findByMobileAndType ( mobile , type ) ;
return userToDTO ( user ) ;
}
public List < User > findByShopId ( String shopId ) {
@Override
public List < User > findByShopId ( String shopId ) {
return userDao . findByShopId ( shopId ) ;
}
@Override
public void subscribeWxServer ( WxMpUser user ) {
// 判断是否绑定用户
final String unionid = user . getUnionId ( ) ;
final String openid = user . getOpenId ( ) ;
User wechatUser = findByUnionid ( unionid ) ;
final Map < String , Object > resultMap = new HashMap < > ( ) ;
if ( wechatUser ! = null ) {
wechatUser . setOfficialAccountOpenid ( openid ) ;
} else {
try {
// 新增用户信息
wechatUser = new User ( ) ;
wechatUser . setUnionid ( unionid ) ;
wechatUser . setOfficialAccountOpenid ( openid ) ;
wechatUser . setStatus ( UserConstant . USER_STATUS_NORMAL ) ;
wechatUser = save ( wechatUser ) ;
// 设置用户角色
final UserRole userRole = new UserRole ( ) ;
final PageVo pageVo = new PageVo ( ) ;
pageVo . setPageNumber ( 1 ) ;
pageVo . setPageSize ( 1 ) ;
final Page < Role > roleCustomer = roleService . findByCondition ( "ROLE_CUSTOMER" , PageUtil . initPage ( pageVo ) ) ;
final Role role = roleCustomer . getContent ( ) . get ( 0 ) ;
//客户角色
userRole . setRoleId ( role . getId ( ) ) ;
userRole . setRoleName ( "客户" ) ;
userRole . setUserId ( wechatUser . getId ( ) ) ;
userRoleService . save ( userRole ) ;
// 更新客户信息,绑定登录用户
// customerService.updateUserId(wechatVo.getCustomId(),wechatUser.getId());
// 返回当前登录人的信息
resultMap . put ( "msg" , "新增用户信息,并绑定成功!" ) ;
// resultMap.put("customId", wechatVo.getCustomId());
} catch ( Exception e ) {
log . error ( "公众号关注出现错误,请查看!" + e . getMessage ( ) , e ) ;
}
}
save ( wechatUser ) ;
}
@Override
public void unSubscribeWxServer ( WxMpXmlMessage wxMessage ) {
}
@Override
public User findByUnionid ( String unionid ) {
return userDao . findByUnionid ( unionid ) ;
}
@Override
public User findByMiniProgramOpenid ( String miniProgramOpenid ) {
return userDao . findByMiniProgramOpenid ( miniProgramOpenid ) ;
}
@Override
public void setUserPhoneToNull ( String userPhone ) {
userDao . setUserPhoneToNull ( userPhone ) ;
}
@Override
public void updateUserPhoneById ( String userPhone , String userId ) {
userDao . updateUserPhoneById ( userPhone , userId ) ;
}
}