2021-10-28 08:09:50 +00:00
package cn.palmte.work.service ;
import cn.palmte.work.bean.RegexConstant ;
2021-11-03 09:17:33 +00:00
import cn.palmte.work.bean.ResponseMsg ;
2021-10-28 08:09:50 +00:00
import cn.palmte.work.model.* ;
import cn.palmte.work.utils.DESCrypto ;
import cn.palmte.work.utils.InterfaceUtil ;
import cn.palmte.work.utils.StrKit ;
2021-11-03 09:17:33 +00:00
import cn.palmte.work.utils.excel.ExportUtils ;
2021-10-28 08:09:50 +00:00
import org.apache.commons.lang.RandomStringUtils ;
import org.apache.commons.lang.StringUtils ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2021-11-03 09:17:33 +00:00
import org.springframework.beans.BeanUtils ;
2021-10-28 08:09:50 +00:00
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.stereotype.Service ;
import org.springframework.transaction.annotation.Transactional ;
import top.jfunc.common.db.QueryHelper ;
import top.jfunc.common.db.bean.Page ;
import top.jfunc.common.db.utils.Pagination ;
2021-11-03 09:17:33 +00:00
import javax.servlet.http.HttpServletResponse ;
2021-10-28 08:09:50 +00:00
import java.beans.Transient ;
2021-11-03 09:17:33 +00:00
import java.io.IOException ;
import java.util.Collection ;
2021-10-28 08:09:50 +00:00
import java.util.Date ;
2021-11-03 09:17:33 +00:00
import java.util.Map ;
2021-10-28 08:09:50 +00:00
import java.util.concurrent.ConcurrentHashMap ;
/ * *
* Created by wang . lin @esstx.cn on 2018 / 4 / 20.
* /
@Service
public class AccountService {
private static final Logger logger = LoggerFactory . getLogger ( AccountService . class ) ;
@Autowired
public AdminRepository adminRepository ;
@Autowired
public AdminRepositoryImpl adminRepositoryImpl ;
@Autowired
public SysUserRoleRepository sysUserRoleRepository ;
@Autowired
2021-11-03 09:17:33 +00:00
private Pagination pagination ;
2021-10-28 08:09:50 +00:00
@Autowired
private SysRoleRepository sysRoleRepository ;
2021-11-03 09:17:33 +00:00
@Autowired
private DeptRepository deptRepository ;
@Autowired
private UserPositionRepository userPositionRepository ;
public Page < Admin > getAdminList ( ConcurrentHashMap < String , String > searchInfo , int pageSize , int pageNum ) {
Page < Admin > adminList = adminRepositoryImpl . getAdminList ( searchInfo , pageSize , pageNum ) ;
return adminList ;
2021-10-28 08:09:50 +00:00
}
public Page < Admin > list ( ConcurrentHashMap < String , String > searchInfo , int pageNumber , int pageSize ) {
QueryHelper queryHelper = new QueryHelper ( "select su.id,su.user_name,su.real_name,su.created_time,su.enabled,sr.type,sr.name as roleName,su.telephone,su.region_name" , " FROM sys_user su left join sys_user_role " +
"sur on su.id = sur.user_id left join sys_role sr on sur.role_id = sr.id" ) ;
queryHelper . addCondition ( "su.is_deleted = 0" ) ;
queryHelper . addCondition ( searchInfo . containsKey ( "roleId" ) & &
StrKit . notBlank ( searchInfo . get ( "roleId" ) ) & & ! "-1" . equals ( searchInfo . get ( "roleId" ) ) ,
"sr.id =?" , searchInfo . get ( "roleId" ) ) ;
queryHelper . addCondition ( searchInfo . containsKey ( "userName" ) , "su.user_name like ?" , "%" +
"su.is_deleted = 0" + "%" ) ;
queryHelper . addCondition ( searchInfo . containsKey ( "telephone" ) , "su.telephone = ?" + searchInfo . get ( "telephone" ) ) ;
queryHelper . addCondition ( searchInfo . containsKey ( "regionId" ) & &
StrKit . notBlank ( searchInfo . get ( "regionId" ) ) & & ! "-1" . equals ( searchInfo . get ( "regionId" ) ) ,
"su.region_id =?" , searchInfo . get ( "regionId" ) ) ;
queryHelper . addOrderProperty ( "su.created_time" , false ) ;
Page < Admin > page = pagination . paginate ( queryHelper . getSql ( ) , Admin . class , pageNumber , pageSize ) ;
return page ;
}
@Transient
public boolean changeStatus ( int userId , int enabled ) {
Admin admin = adminRepository . findOne ( userId ) ;
if ( admin = = null ) {
return false ;
}
admin . setEnabled ( enabled ) ;
adminRepository . save ( admin ) ;
return true ;
}
public Admin findUserById ( int userId ) {
2021-11-03 09:17:33 +00:00
Admin admin = adminRepository . findOne ( userId ) ;
2021-10-28 08:09:50 +00:00
return admin ;
}
@Transactional ( rollbackFor = Exception . class )
public void saveOrUpdateAccount ( int userId , int roleId , Admin admin , String privateKey ) {
int createAdminId = InterfaceUtil . getAdminId ( ) ;
Admin oldAdmin = adminRepository . findOne ( userId ) ;
2021-11-03 09:17:33 +00:00
Dept dept = deptRepository . findOne ( admin . getDeptId ( ) ) ;
SysRole sysRole = sysRoleRepository . findOne ( admin . getRoleId ( ) ) ;
UserPosition userPosition = userPositionRepository . findOne ( admin . getPositionId ( ) ) ;
2021-10-28 08:09:50 +00:00
if ( oldAdmin = = null ) {
oldAdmin = new Admin ( ) ;
String userName = admin . getUserName ( ) ;
oldAdmin . setUserName ( userName ) ;
String salt = RandomStringUtils . randomAlphanumeric ( 6 ) . toUpperCase ( ) ;
String newPassword = decEncPassword ( admin . getTelephone ( ) . substring ( 5 ) , salt , privateKey ) ;
2021-11-03 09:17:33 +00:00
BeanUtils . copyProperties ( admin , oldAdmin ) ;
oldAdmin . setRealName ( sysRole . getName ( ) ) ;
oldAdmin . setDeptName ( dept . getName ( ) ) ;
oldAdmin . setPositionName ( userPosition . getPositionName ( ) ) ;
2021-10-28 08:09:50 +00:00
oldAdmin . setPassword ( newPassword ) ;
oldAdmin . setSalt ( salt ) ;
oldAdmin . setEnabled ( 1 ) ;
oldAdmin . setCreatedBy ( createAdminId ) ;
oldAdmin . setTelephone ( admin . getTelephone ( ) ) ;
oldAdmin . setCreatedTime ( new Date ( ) ) ;
2021-11-03 09:17:33 +00:00
2021-10-28 08:09:50 +00:00
} else {
String userName = admin . getUserName ( ) ;
2021-11-03 09:17:33 +00:00
oldAdmin . setDeptId ( admin . getDeptId ( ) ) ;
oldAdmin . setPositionId ( admin . getPositionId ( ) ) ;
oldAdmin . setRealName ( sysRole . getName ( ) ) ;
oldAdmin . setDeptName ( dept . getName ( ) ) ;
oldAdmin . setPositionName ( userPosition . getPositionName ( ) ) ;
oldAdmin . setEmpCode ( admin . getEmpCode ( ) ) ;
oldAdmin . setWorkLocation ( admin . getWorkLocation ( ) ) ;
oldAdmin . setDirectManager ( admin . getDirectManager ( ) ) ;
oldAdmin . setCompanyEmail ( admin . getCompanyEmail ( ) ) ;
2021-10-28 08:09:50 +00:00
oldAdmin . setUserName ( userName ) ;
oldAdmin . setRealName ( admin . getRealName ( ) ) ;
oldAdmin . setTelephone ( admin . getTelephone ( ) ) ;
oldAdmin . setLastUpdatedBy ( createAdminId ) ;
oldAdmin . setLastUpdatedTime ( new Date ( ) ) ;
}
oldAdmin . setRoleId ( admin . getRoleId ( ) ) ;
SysRole one = sysRoleRepository . findOne ( admin . getRoleId ( ) ) ;
if ( null ! = one ) {
oldAdmin . setRoleName ( one . getName ( ) ) ;
}
admin = adminRepository . saveAndFlush ( oldAdmin ) ;
//设置当前用户角色关系状态为删除
userId = admin . getId ( ) ;
sysUserRoleRepository . deleteSysUserRoleByUserId ( createAdminId , new Date ( ) , userId ) ;
SysUserRole sysUserRole = new SysUserRole ( ) ;
//保存用户角色关系
sysUserRole . setUserId ( userId ) ;
sysUserRole . setRoleId ( roleId ) ;
sysUserRole . setCreatedBy ( createAdminId ) ;
sysUserRole . setCreatedTime ( new Date ( ) ) ;
sysUserRoleRepository . save ( sysUserRole ) ;
}
/ * *
* 检 查 用 户 是 否 存 在 于 系 统 中
* /
public String validateUserExistInfo ( int userId , Admin admin ) {
String message = "" ;
//校验手机号格式
String phone = admin . getTelephone ( ) ;
if ( ! phone . matches ( RegexConstant . REGEX ) ) {
return "请填写正确的电话号码!" ;
}
Admin byTelephoneEquals = adminRepository . findByTelephone ( phone ) ;
if ( userId = = - 1 ) {
//新增校验手机号是否存在
if ( null ! = byTelephoneEquals ) {
message = "该手机号已存在!" ;
return message ;
}
} else {
//编辑校验手机号是否存在
if ( null ! = byTelephoneEquals & & byTelephoneEquals . getId ( ) ! = userId ) {
message = "该手机号已存在!" ;
return message ;
}
}
if ( StringUtils . isEmpty ( admin . getUserName ( ) ) ) {
message = "亲,用户名不能为空!" ;
return message ;
}
Admin existAdmin = adminRepository . getAdminByUsername ( admin . getUserName ( ) ) ;
if ( userId = = - 1 ) { //只在新增时候校验
if ( existAdmin ! = null ) {
message = "亲,该账号已存在!" ;
return message ;
}
} else {
if ( existAdmin ! = null & & existAdmin . getId ( ) ! = userId ) {
message = "亲,该账号名称已存在!" ;
return message ;
}
}
existAdmin = adminRepository . getAdminByRealName ( admin . getRealName ( ) ) ;
if ( existAdmin ! = null & & existAdmin . getId ( ) ! = userId ) {
message = "亲,该账号名称已存在!" ;
return message ;
}
return message ;
}
public String decEncPassword ( String password , String salt , String privateKey ) {
try {
return DESCrypto . encryptPassword ( password , salt ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
return "" ;
}
public boolean resetPassword ( int userId , String privateKey ) {
try {
Admin oldAdmin = adminRepository . findOne ( userId ) ;
if ( oldAdmin = = null ) {
return false ;
}
String salt = RandomStringUtils . randomAlphanumeric ( 6 ) . toUpperCase ( ) ;
String telephone = oldAdmin . getTelephone ( ) ;
String newPassword = decEncPassword ( telephone . substring ( 5 ) , salt , privateKey ) ;
oldAdmin . setPassword ( newPassword ) ;
oldAdmin . setSalt ( salt ) ;
oldAdmin . setLastUpdatedBy ( InterfaceUtil . getAdminId ( ) ) ;
oldAdmin . setLastUpdatedTime ( new Date ( ) ) ;
adminRepository . save ( oldAdmin ) ;
} catch ( Exception e ) {
logger . error ( "充值密码错误!" + e . toString ( ) ) ;
return false ;
}
return true ;
}
@Transactional ( rollbackFor = Exception . class )
public boolean deleteAccount ( int userId ) {
try {
Admin admin = adminRepository . findOne ( userId ) ;
admin . setDeleted ( true ) ;
admin . setLastUpdatedBy ( InterfaceUtil . getAdminId ( ) ) ;
//删除用户角色关系
sysUserRoleRepository . deleteSysUserRoleByUserId ( InterfaceUtil . getAdminId ( ) , new Date ( ) , userId ) ;
adminRepository . save ( admin ) ;
return true ;
} catch ( Exception e ) {
logger . error ( "账户ID:" + userId + "删除错误!" + e . toString ( ) ) ;
}
return false ;
}
2021-11-03 09:17:33 +00:00
public void exportExcel ( HttpServletResponse response , ConcurrentHashMap < String , String > searchInfo ) throws IOException {
String [ ] headers = { "工号" , "手机号码" , "姓名" , "常驻地" , "一级部门" , "直接主管" , "职位" , "所属角色" , "公司邮件地址" } ;
String [ ] exportColumns = { "empCode" , "telephone" , "workLocation" , "deptName" , "directManager" , "positionName" , "roleName" , "companyEmail" } ;
ExportUtils . exportToExcel ( headers , exportColumns , 1 , 5000 ,
response . getOutputStream ( ) , ( pN , pS ) - > getAdminList ( searchInfo , pN , pS ) . getList ( ) ) ;
}
public ResponseMsg check ( Collection < Map > maps ) {
return null ;
}
2021-10-28 08:09:50 +00:00
}