Commit 398e7c73 by guojuxing

定时任务接口修改,添加参数

parent e744cda9
package com.gic.auth.dao.mapper;
import com.gic.auth.entity.TabSysUserResource;
import com.gic.auth.entity.TabSysUserRole;
import org.apache.ibatis.annotations.Param;
import java.util.List;
......@@ -58,4 +59,23 @@ public interface TabSysUserResourceMapper {
TabSysUserResource existUserResource(@Param("enterpriseId") Integer enterpriseId, @Param("resourceId") Integer resourceId);
List<Map<String, Object>> countByResourceIds(@Param("enterpriseId") Integer enterpriseId, @Param("ids") List<Integer> resourceIds);
/**
* 删除用户关联数据
* @Title: deleteByUserId

* @Description:

 * @author guojuxing
* @param userId

* @return int


 */
int deleteByUserId(@Param("userId") Integer userId);
/**
* 查询用户关联数据
* @Title: listUserResourceByUserId

* @Description:

 * @author guojuxing
* @param userId

* @return java.util.List<com.gic.auth.entity.TabSysUserRole>


 */
List<TabSysUserResource> listUserResourceByUserId(@Param("userId") Integer userId);
}
\ No newline at end of file
......@@ -3,6 +3,8 @@ package com.gic.auth.service;
import java.util.List;
import java.util.Map;
import com.gic.auth.dto.UserResourceDTO;
/**
*
* @Description:
......@@ -13,4 +15,35 @@ public interface UserResourceService {
boolean existUserResource(Integer enterpriseId, Integer resourceId);
Map<Integer,Integer> countByResourceIds(Integer enterpriseId, List<Integer> resourceIds);
/**
* 新增
* @Title: save

* @Description:

 * @author guojuxing
* @param dto

* @return void


 */
void save(UserResourceDTO dto);
/**
* 删除用户关联数据
* @Title: deleteByUserId

* @Description:

 * @author guojuxing
* @param userId

* @return void


 */
void deleteByUserId(Integer userId);
/**
* 查询用户关联数据
* @Title: listUserRoleByUserId

* @Description:

 * @author guojuxing
* @param userId

* @return java.util.List<com.gic.auth.entity.TabSysUserRole>


 */
List<UserResourceDTO> listUserResourceByUserId(Integer userId);
}
package com.gic.auth.service.outer.impl;
package com.gic.auth.service.impl;
import java.util.*;
import com.gic.auth.dao.mapper.TabSysUserResourceMapper;
import com.gic.auth.entity.TabSysUserResource;
import com.gic.auth.service.UserResourceService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.gic.auth.dao.mapper.TabSysUserResourceMapper;
import com.gic.auth.dto.UserResourceDTO;
import com.gic.auth.entity.TabSysUserResource;
import com.gic.auth.service.UserResourceService;
import com.gic.commons.util.EntityUtil;
/**
*
......@@ -44,4 +44,23 @@ public class UserResourceServiceImpl implements UserResourceService {
return Collections.emptyMap();
}
}
@Override
public void save(UserResourceDTO dto) {
tabSysUserResourceMapper.insert(EntityUtil.changeEntityNew(TabSysUserResource.class, dto));
}
@Override
public void deleteByUserId(Integer userId) {
tabSysUserResourceMapper.deleteByUserId(userId);
}
@Override
public List<UserResourceDTO> listUserResourceByUserId(Integer userId) {
List<TabSysUserResource> list = tabSysUserResourceMapper.listUserResourceByUserId(userId);
if (CollectionUtils.isNotEmpty(list)) {
return EntityUtil.changeEntityListNew(UserResourceDTO.class, list);
}
return new ArrayList<>();
}
}
\ No newline at end of file
......@@ -4,14 +4,16 @@ import java.util.Date;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.auth.dto.UserDTO;
import com.gic.auth.dto.UserResourceDTO;
import com.gic.auth.dto.UserRoleDTO;
import com.gic.auth.entity.TabSysUser;
import com.gic.auth.service.UserApiService;
import com.gic.auth.service.UserService;
import com.gic.auth.service.*;
import com.gic.commons.util.EntityUtil;
import com.gic.enterprise.error.ErrorCode;
import com.gic.store.utils.valid.ValidUtil;
......@@ -24,6 +26,14 @@ import com.gic.store.utils.valid.ValidUtil;
public class UserApiServiceImpl implements UserApiService{
@Autowired
private UserService userService;
@Autowired
private UserRoleApiService userRoleApiService;
@Autowired
private UserResourceApiService userResourceApiService;
@Autowired
private UserRoleService userRoleService;
@Autowired
private UserResourceService userResourceService;
@Override
public ServiceResponse<Integer> saveUser(UserDTO userDTO) {
//valid param
......@@ -38,7 +48,9 @@ public class UserApiServiceImpl implements UserApiService{
userDTO.setCreateTime(new Date());
userDTO.setUpdateTime(new Date());
userDTO.setStatus(1);
return ServiceResponse.success(userService.saveUser(userDTO));
Integer userId = userService.saveUser(userDTO);
return saveRole(userId, userDTO.getEnterpriseId(), userDTO.getUserRoleIds(), userDTO.getUserResourceIds());
}
@Override
......@@ -57,7 +69,7 @@ public class UserApiServiceImpl implements UserApiService{
}
userDTO.setUpdateTime(new Date());
userService.editUser(userDTO);
return ServiceResponse.success();
return saveRole(userDTO.getUserId(), userDTO.getEnterpriseId(), userDTO.getUserRoleIds(), userDTO.getUserResourceIds());
}
@Override
......@@ -66,7 +78,25 @@ public class UserApiServiceImpl implements UserApiService{
if (tabUser == null) {
return ServiceResponse.failure(ErrorCode.PARAMETER_ERROR.getCode(), "用户ID输入有误");
}
return ServiceResponse.success(EntityUtil.changeEntityNew(UserDTO.class, tabUser));
UserDTO result = EntityUtil.changeEntityNew(UserDTO.class, tabUser);
//角色集
List<UserRoleDTO> userRoleDTOList = userRoleService.listUserRoleByUserId(userId);
StringBuilder userRoleStr = new StringBuilder();
for (UserRoleDTO userRoleDTO : userRoleDTOList) {
userRoleStr.append(userRoleDTO.getRoleId()).append(",");
}
String userRoleResult = userRoleStr.toString();
result.setUserRoleIds(userRoleResult.substring(0, userRoleResult.length() - 1));
//资源集
List<UserResourceDTO> userResourceDTOList = userResourceService.listUserResourceByUserId(userId);
StringBuilder userResourceStr = new StringBuilder();
for (UserResourceDTO userResourceDTO : userResourceDTOList) {
userResourceStr.append(userResourceDTO.getResourceId()).append(",");
}
String userResourceResult = userResourceStr.toString();
result.setUserResourceIds(userResourceResult.substring(0, userResourceResult.length() - 1));
return ServiceResponse.success(result);
}
@Override
......@@ -92,4 +122,64 @@ public class UserApiServiceImpl implements UserApiService{
TabSysUser user = this.userService.login(phoneNumber, enterpriseId, password);
return ServiceResponse.success(EntityUtil.changeEntityByJSON(UserDTO.class, user));
}
/**
* 保存关联数据,角色关联、资源关联
* @Title: saveRole

* @Description:

 * @author guojuxing
* @param userId
* @param enterpriseId
* @param userRoleIds
* @param userResourceIds

* @return com.gic.api.base.commons.ServiceResponse<java.lang.Integer>


 */
private ServiceResponse<Integer> saveRole(Integer userId, Integer enterpriseId, String userRoleIds, String userResourceIds) {
String[] userRoleIdArr;
String[] userResourceIdArr;
ServiceResponse<Object> userRoleValid = validStr(userRoleIds);
if (userRoleValid.isSuccess()) {
userRoleIdArr = (String[]) userRoleValid.getResult();
} else {
return ServiceResponse.failure(userRoleValid.getCode(), userRoleValid.getMessage());
}
ServiceResponse<Object> userResourceIdValid = validStr(userResourceIds);
if (userResourceIdValid.isSuccess()) {
userResourceIdArr = (String[]) userRoleValid.getResult();
} else {
return ServiceResponse.failure(userResourceIdValid.getCode(), userResourceIdValid.getMessage());
}
//新增用户角色关联数据
UserRoleDTO userRoleDTO = new UserRoleDTO();
userRoleDTO.setEnterpriseId(enterpriseId);
userRoleDTO.setUserId(userId);
for (String userRole : userRoleIdArr) {
userRoleDTO.setRoleId(Integer.parseInt(userRole));
ServiceResponse result = userRoleApiService.save(userRoleDTO);
if (!result.isSuccess()) {
return result;
}
}
//新增用户资源关联数据
UserResourceDTO userResourceDTO = new UserResourceDTO();
userResourceDTO.setUserId(userId);
userResourceDTO.setEnterpriseId(enterpriseId);
for (String userResource : userResourceIdArr) {
userResourceDTO.setResourceId(Integer.parseInt(userResource));
ServiceResponse result = userResourceApiService.save(userResourceDTO);
if (!result.isSuccess()) {
return result;
}
}
return ServiceResponse.success(userId);
}
private static ServiceResponse<Object> validStr(String param) {
if (!StringUtils.isNumeric(param.replaceAll(",", ""))) {
return ServiceResponse.failure(ErrorCode.PARAMETER_ERROR.getCode(), "数据格式有误,不是英文逗号隔开的数据");
}
String[] strArr = param.split(",");
return ServiceResponse.success(strArr);
}
}
package com.gic.auth.service.outer.impl;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.auth.dto.UserResourceDTO;
import com.gic.auth.entity.TabSysUser;
import com.gic.auth.service.UserResourceApiService;
import org.springframework.stereotype.Service;
import com.gic.auth.service.UserResourceService;
import com.gic.auth.service.UserService;
import com.gic.enterprise.error.ErrorCode;
import com.gic.enterprise.utils.valid.ValidParamsUtils;
@Service("userResourceApiService")
public class UserResourceApiServiceImpl implements UserResourceApiService{
@Autowired
private UserResourceService userResourceService;
@Autowired
private UserService userService;
@Override
public ServiceResponse<Void> save(UserResourceDTO dto) {
return null;
ServiceResponse paramValid = ValidParamsUtils.allCheckValidate(dto, UserResourceDTO.SaveUserResourceValid.class);
if (!paramValid.isSuccess()) {
return paramValid;
}
TabSysUser user = userService.getUserById(dto.getUserId());
if (user == null) {
return ServiceResponse.failure(ErrorCode.PARAMETER_ERROR.getCode(), "用户数据有误,查无数据");
}
userResourceService.deleteByUserId(user.getUserId());
dto.setCreateTime(new Date());
dto.setUpdateTime(new Date());
dto.setStatus(1);
userResourceService.save(dto);
return ServiceResponse.success();
}
}
......@@ -29,7 +29,7 @@
delete from tab_sys_user
where user_id = #{userId,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.gic.auth.entity.TabSysUser">
<insert id="insert" parameterType="com.gic.auth.entity.TabSysUser" useGeneratedKeys="true" keyProperty="userId">
insert into tab_sys_user (user_id, user_name, phone_number,
password, super_admin, status,
create_time, update_time, enterprise_id,
......
......@@ -137,4 +137,19 @@
</foreach>
</if>
</select>
<update id="deleteByUserId" parameterType="java.lang.Integer">
UPDATE tab_sys_user_resource set status = 0
where user_id = #{userId}
and status = 1
</update>
<select id="listUserResourceByUserId" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from tab_sys_user_resource
where user_id = #{userId}
and status = 1
</select>
</mapper>
\ No newline at end of file
package com.gic.auth.web.controller;
import com.gic.auth.service.UserApiService;
import com.gic.auth.web.vo.UserDetailVO;
import com.gic.enterprise.utils.ResultControllerUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.auth.dto.UserDTO;
import com.gic.auth.service.UserApiService;
import com.gic.auth.web.utils.UserPasswordUtils;
import com.gic.commons.webapi.reponse.RestResponse;
import com.gic.enterprise.response.EnterpriseRestResponse;
import com.gic.store.constant.StoreGroupConstant;
import com.gic.store.utils.CommonResultControllerUtils;
@RestController
@RequestMapping("/user")
public class UserController {
......@@ -16,4 +27,42 @@ public class UserController {
@Autowired
private UserApiService userApiService;
@RequestMapping("/save-or-update-user")
public RestResponse editUser(@Validated({ UserDTO.UserQoValid.class }) UserDTO userDTO) {
userDTO.setEnterpriseId(StoreGroupConstant.TEST_ENTERPRISE_ID);
String password = UserPasswordUtils.validPassword(userDTO.getPasswordType(), userDTO.getPassword(),
userDTO.getConfirmPassword());
userDTO.setPassword(UserPasswordUtils.getEncryptPassword(password));
ServiceResponse userResult;
if (userDTO.getUserId() == null) {
userResult = userApiService.saveUser(userDTO);
} else {
userResult = userApiService.editUser(userDTO);
}
if (userResult.isSuccess()) {
return RestResponse.success();
} else {
return EnterpriseRestResponse.failure(userResult);
}
}
/**
* 用户详情
* @Title: getUserById

* @Description:

 * @author guojuxing
* @param userId

* @return com.gic.commons.webapi.reponse.RestResponse


 */
@RequestMapping("/get-user-by-id")
public RestResponse getUserById(Integer userId) {
return ResultControllerUtils.commonResultOne(userApiService.getUserById(userId), UserDetailVO.class);
}
@RequestMapping("/get-user")
public RestResponse getUser() {
return RestResponse.success(CommonResultControllerUtils
.commonResult(userApiService.getUserByEnterpriseId(StoreGroupConstant.TEST_ENTERPRISE_ID)));
}
}
package com.gic.auth.web.utils;
import org.apache.commons.lang3.StringUtils;
import com.gic.auth.constant.UserConstants;
import com.gic.commons.util.Md5Util;
import com.gic.enterprise.error.ErrorCode;
import com.gic.enterprise.exception.CommonException;
import com.gic.enterprise.utils.CreateRandomUtils;
/**
* 用户密码生成
* @ClassName: UserPasswordUtils

* @Description: 

* @author guojuxing

* @date 2019/9/2 2:03 PM

*/
public class UserPasswordUtils {
public static String getEncryptPassword(String password) {
return encryptPassword(password);
}
/**
* 自动生成n位密码
* @Title: createPasswordAuto

* @Description:

 * @author guojuxing 

* @return java.lang.String


 */
public static String createPasswordAuto(Integer length) {
if (length == null) {
length = 8;
}
return CreateRandomUtils.getStringRandom(length);
}
private static String encryptPassword(String password) {
Md5Util md5 = new Md5Util();
return md5.encrypt(password + password);
}
/**
* 获取密码
* @param passwordType
* @param password
* @param confirmPassword
* @return
*/
public static String validPassword(int passwordType, String password, String confirmPassword) {
//加密
if (UserConstants.CREATE_AUTO == passwordType) {
//自动随机生成
return createPasswordAuto(8);
} else {
if (StringUtils.isNotBlank(password) && password.equals(confirmPassword)) {
//如果一致
return password;
} else {
throw new CommonException(ErrorCode.PARAMETER_ERROR.getCode(), "密码不一致");
}
}
}
}
package com.gic.auth.web.vo;
import java.io.Serializable;
/**
* 用户详情
* @ClassName: UserDetailVO

* @Description: 

* @author guojuxing

* @date 2019/9/2 2:58 PM

*/
public class UserDetailVO implements Serializable{
private static final long serialVersionUID = -4487059516368072062L;
/**
* 用户id
*/
private Integer userId;
/**
* 用户名
*/
private String userName;
/**
* 手机号码
*/
private String phoneNumber;
/**
* 是否是超级管理员
*/
private Integer superAdmin;
/**
* 国际区号,如中国 86
*/
private String phoneAreaCode;
/**
* 受审组,可以有多个,用英文逗号隔开
*/
private String userGroupIds;
/**
* 用户角色,可以多选,用英文逗号隔开
*/
private String userRoleIds;
/**
* 用户资源组授权,可以多选,用英文逗号隔开
*/
private String userResourceIds;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public Integer getSuperAdmin() {
return superAdmin;
}
public void setSuperAdmin(Integer superAdmin) {
this.superAdmin = superAdmin;
}
public String getPhoneAreaCode() {
return phoneAreaCode;
}
public void setPhoneAreaCode(String phoneAreaCode) {
this.phoneAreaCode = phoneAreaCode;
}
public String getUserGroupIds() {
return userGroupIds;
}
public void setUserGroupIds(String userGroupIds) {
this.userGroupIds = userGroupIds;
}
public String getUserRoleIds() {
return userRoleIds;
}
public void setUserRoleIds(String userRoleIds) {
this.userRoleIds = userRoleIds;
}
public String getUserResourceIds() {
return userResourceIds;
}
public void setUserResourceIds(String userResourceIds) {
this.userResourceIds = userResourceIds;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment