264 lines
10 KiB
Java
264 lines
10 KiB
Java
package cn.palmte.work.service;
|
|
|
|
import cn.palmte.work.bean.RegexConstant;
|
|
import cn.palmte.work.bean.ResponseMsg;
|
|
import cn.palmte.work.model.*;
|
|
import cn.palmte.work.utils.DESCrypto;
|
|
import cn.palmte.work.utils.InterfaceUtil;
|
|
import cn.palmte.work.utils.StrKit;
|
|
import cn.palmte.work.utils.excel.ExportUtils;
|
|
import org.apache.commons.lang.RandomStringUtils;
|
|
import org.apache.commons.lang.StringUtils;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.BeanUtils;
|
|
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;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import java.beans.Transient;
|
|
import java.io.IOException;
|
|
import java.util.Collection;
|
|
import java.util.Date;
|
|
import java.util.Map;
|
|
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
|
|
private Pagination pagination;
|
|
|
|
@Autowired
|
|
private SysRoleRepository sysRoleRepository;
|
|
|
|
@Autowired
|
|
private DeptRepository deptRepository;
|
|
|
|
@Autowired
|
|
private UserPositionRepository userPositionRepository;
|
|
|
|
public Page<Admin> getAdminList(Map<String, String> searchInfo, int pageSize, int pageNum) {
|
|
Page<Admin> adminList = adminRepositoryImpl.getAdminList(searchInfo, pageSize, pageNum);
|
|
return adminList;
|
|
}
|
|
|
|
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) {
|
|
Admin admin = adminRepository.findOne(userId);
|
|
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);
|
|
Dept dept = deptRepository.findOne(admin.getDeptId());
|
|
SysRole sysRole = sysRoleRepository.findOne(admin.getRoleId());
|
|
UserPosition userPosition = userPositionRepository.findOne(admin.getPositionId());
|
|
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);
|
|
|
|
BeanUtils.copyProperties(admin, oldAdmin);
|
|
|
|
oldAdmin.setRealName(sysRole.getName());
|
|
oldAdmin.setDeptName(dept.getName());
|
|
oldAdmin.setPositionName(userPosition.getPositionName());
|
|
oldAdmin.setPassword(newPassword);
|
|
oldAdmin.setSalt(salt);
|
|
oldAdmin.setEnabled(1);
|
|
oldAdmin.setCreatedBy(createAdminId);
|
|
oldAdmin.setTelephone(admin.getTelephone());
|
|
oldAdmin.setCreatedTime(new Date());
|
|
|
|
} else {
|
|
String userName = admin.getUserName();
|
|
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());
|
|
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;
|
|
}
|
|
|
|
public ResponseMsg check(Collection<Map> maps) {
|
|
|
|
|
|
return null;
|
|
}
|
|
}
|