From ef3211d33a7bde1aeba29d4ce7482bb8d63e64ab Mon Sep 17 00:00:00 2001 From: winLsy <18332763730@163.com> Date: Sun, 18 Mar 2018 16:37:31 +0800 Subject: [PATCH] =?UTF-8?q?micro-fast-IIDWC=20=E7=94=A8=E6=88=B7=E8=A7=92?= =?UTF-8?q?=E8=89=B2=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/UpmsUserRoleController.java | 55 +++------- .../upms/dao/UpmsUserOrganizationMapper.java | 6 ++ .../fast/upms/dao/UpmsUserRoleMapper.java | 28 +++-- .../service/UpmsRolePermissionService.java | 2 - .../fast/upms/service/UpmsRoleService.java | 5 + .../upms/service/UpmsUserRoleService.java | 21 +++- .../service/impl/UpmsUserRoleServiceImpl.java | 39 +++++-- .../resources/mappers/UpmsUserRoleMapper.xml | 101 ++---------------- 8 files changed, 106 insertions(+), 151 deletions(-) diff --git a/ms/upms/src/main/java/com/micro/fast/upms/controller/UpmsUserRoleController.java b/ms/upms/src/main/java/com/micro/fast/upms/controller/UpmsUserRoleController.java index 70875d2..963bd16 100644 --- a/ms/upms/src/main/java/com/micro/fast/upms/controller/UpmsUserRoleController.java +++ b/ms/upms/src/main/java/com/micro/fast/upms/controller/UpmsUserRoleController.java @@ -2,65 +2,42 @@ package com.micro.fast.upms.controller; import com.micro.fast.boot.starter.common.response.BaseConst; import com.micro.fast.boot.starter.common.response.ServerResponse; -import com.micro.fast.upms.pojo.UpmsUserRole; import com.micro.fast.upms.service.UpmsUserRoleService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; -import org.hibernate.validator.constraints.NotBlank; import org.hibernate.validator.constraints.NotEmpty; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.validation.BindException; import org.springframework.web.bind.annotation.*; -import javax.validation.Valid; +import javax.validation.constraints.NotNull; import java.util.ArrayList; -import java.util.List; /** * * @author lsy */ -@Api("upmsUserRole") +@Api("用户和角色关系维护") @RestController @RequestMapping("/upmsUserRole") public class UpmsUserRoleController { @Autowired - private UpmsUserRoleService upmsUserRoleService; + private UpmsUserRoleService upmsUserRoleService; - @ApiOperation("添加信息") - @PostMapping - public ServerResponse addUpmsUserRole(@Valid UpmsUserRole upmsUserRole) throws BindException { - return upmsUserRoleService.add(upmsUserRole); + + @ApiOperation("用户添加角色") + @PostMapping("/userAddRoles") + public ServerResponse userAddRoles(@RequestParam("userId") @NotNull(message = BaseConst.BASEMSG_PREFIX+"请传入用户的id") Integer userId, + @RequestParam("roleIds") + @NotEmpty(message = BaseConst.BASEMSG_PREFIX+"请传入的角色的id") ArrayList roleIds){ + return upmsUserRoleService.batchInsert(userId,roleIds); } - @ApiOperation("根据id查询详细信息") - @GetMapping("/{id}") - public ServerResponse getUpmsUserRoleById( - @PathVariable(value = "id") @NotBlank(message = BaseConst.BASEMSG_PREFIX+"请传入id") String id){ - return upmsUserRoleService.getById(Integer.valueOf(id)); - } - - @ApiOperation("根据条件分页查询") - @GetMapping - public ServerResponse getUpmsUserRoleByCondition(UpmsUserRole upmsUserRole, - @RequestParam(defaultValue = "1",required = false) int pageNum, - @RequestParam(defaultValue = "10",required = false)int pageSize, - @RequestParam(required = false) String orderBy){ - return upmsUserRoleService.getByCondition(upmsUserRole,pageNum,pageSize,orderBy); - } - - @ApiOperation("修改信息") - @PutMapping("/{id}") - public ServerResponse updateUpmsUserRole(UpmsUserRole upmsUserRole, - @NotBlank(message = BaseConst.BASEMSG_PREFIX+"请传入id") String id){ - upmsUserRole.setId(Integer.valueOf(id)); - return upmsUserRoleService.update(upmsUserRole); - } - - @ApiOperation("根据id删除,传入数组") - @DeleteMapping - public ServerResponse deleteUpmsUserRole(@NotEmpty(message = BaseConst.BASEMSG_PREFIX+"请传入ids") @RequestParam("ids") ArrayList ids){ - return upmsUserRoleService.deleteByIds(ids); + @ApiOperation("用户删除角色") + @DeleteMapping("/userDeleteRoles") + public ServerResponse userDeleteRoles(@RequestParam("userId") @NotNull(message = BaseConst.BASEMSG_PREFIX+"请传入用户的id") Integer userId, + @RequestParam("roleIds") + @NotEmpty(message = BaseConst.BASEMSG_PREFIX+"请传入的角色的id") ArrayList roleIds){ + return upmsUserRoleService.batchDelete(userId,roleIds); } } diff --git a/ms/upms/src/main/java/com/micro/fast/upms/dao/UpmsUserOrganizationMapper.java b/ms/upms/src/main/java/com/micro/fast/upms/dao/UpmsUserOrganizationMapper.java index 93e1e1a..e26883d 100644 --- a/ms/upms/src/main/java/com/micro/fast/upms/dao/UpmsUserOrganizationMapper.java +++ b/ms/upms/src/main/java/com/micro/fast/upms/dao/UpmsUserOrganizationMapper.java @@ -14,5 +14,11 @@ public interface UpmsUserOrganizationMapper { */ int batchInsertOneUserWithManyOrg(@Param("userId") Integer userId, @Param("orgIds") List orgIds); + /** + * 批量删除一个用户和多个组织的关系 + * @param userId + * @param orgs + * @return + */ int batchDeleteByUserIdOrganizationId(@Param("userId")Integer userId,@Param("orgIds")List orgs); } \ No newline at end of file diff --git a/ms/upms/src/main/java/com/micro/fast/upms/dao/UpmsUserRoleMapper.java b/ms/upms/src/main/java/com/micro/fast/upms/dao/UpmsUserRoleMapper.java index b374517..ad7dd7a 100644 --- a/ms/upms/src/main/java/com/micro/fast/upms/dao/UpmsUserRoleMapper.java +++ b/ms/upms/src/main/java/com/micro/fast/upms/dao/UpmsUserRoleMapper.java @@ -1,18 +1,24 @@ package com.micro.fast.upms.dao; -import com.micro.fast.common.dao.SsmMapper; -import com.micro.fast.upms.pojo.UpmsUserRole; +import org.apache.ibatis.annotations.Param; -public interface UpmsUserRoleMapper extends SsmMapper { - int deleteByPrimaryKey(Integer id); +import java.util.List; - int insert(UpmsUserRole record); +public interface UpmsUserRoleMapper { - int insertSelective(UpmsUserRole record); + /** + * 批量建立用户和角色的关系 + * @param userId 用户的id + * @param roleIds 角色的id集合 + * @return + */ + int batchInsert(@Param("userId") Integer userId,@Param("roleIds") List roleIds); - UpmsUserRole selectByPrimaryKey(Integer userRoleId); - - int updateByPrimaryKeySelective(UpmsUserRole record); - - int updateByPrimaryKey(UpmsUserRole record); + /** + * 批量删除用户和角色的关系 + * @param userId 用户的id + * @param roleIds 角色的id集合 + * @return + */ + int batchDelete(@Param("userId") Integer userId,@Param("roleIds") List roleIds); } \ No newline at end of file diff --git a/ms/upms/src/main/java/com/micro/fast/upms/service/UpmsRolePermissionService.java b/ms/upms/src/main/java/com/micro/fast/upms/service/UpmsRolePermissionService.java index e7153a7..c01a709 100644 --- a/ms/upms/src/main/java/com/micro/fast/upms/service/UpmsRolePermissionService.java +++ b/ms/upms/src/main/java/com/micro/fast/upms/service/UpmsRolePermissionService.java @@ -1,7 +1,5 @@ package com.micro.fast.upms.service; -import com.micro.fast.common.service.SsmService; - /** * @author lsy */ diff --git a/ms/upms/src/main/java/com/micro/fast/upms/service/UpmsRoleService.java b/ms/upms/src/main/java/com/micro/fast/upms/service/UpmsRoleService.java index 2bd44a8..0ab4957 100644 --- a/ms/upms/src/main/java/com/micro/fast/upms/service/UpmsRoleService.java +++ b/ms/upms/src/main/java/com/micro/fast/upms/service/UpmsRoleService.java @@ -7,6 +7,11 @@ import com.micro.fast.common.service.SsmService; * @author lsy */ public interface UpmsRoleService extends SsmService{ + /** + * 查询用户已拥有的角色 + * @param useId + * @return + */ ServerResponse getRolesByUserId(Integer useId); /** diff --git a/ms/upms/src/main/java/com/micro/fast/upms/service/UpmsUserRoleService.java b/ms/upms/src/main/java/com/micro/fast/upms/service/UpmsUserRoleService.java index 2e8e2ca..3db4c55 100644 --- a/ms/upms/src/main/java/com/micro/fast/upms/service/UpmsUserRoleService.java +++ b/ms/upms/src/main/java/com/micro/fast/upms/service/UpmsUserRoleService.java @@ -1,10 +1,27 @@ package com.micro.fast.upms.service; -import com.micro.fast.common.service.SsmService; +import com.micro.fast.boot.starter.common.response.ServerResponse; + +import java.util.List; /** * @author lsy */ -public interface UpmsUserRoleService extends SsmService{ +public interface UpmsUserRoleService{ + /** + * 批量建立用户和角色的关系 + * @param userId 用户id + * @param roleIds 角色id的集合 + * @return + */ + ServerResponse batchInsert(Integer userId, List roleIds); + + /** + * 批量解除用户和角色的关系 + * @param userId 用户id + * @param roleIds 角色id的集合 + * @return + */ + ServerResponse batchDelete(Integer userId, List roleIds); } \ No newline at end of file diff --git a/ms/upms/src/main/java/com/micro/fast/upms/service/impl/UpmsUserRoleServiceImpl.java b/ms/upms/src/main/java/com/micro/fast/upms/service/impl/UpmsUserRoleServiceImpl.java index c74a5ee..4ee2c86 100644 --- a/ms/upms/src/main/java/com/micro/fast/upms/service/impl/UpmsUserRoleServiceImpl.java +++ b/ms/upms/src/main/java/com/micro/fast/upms/service/impl/UpmsUserRoleServiceImpl.java @@ -1,28 +1,53 @@ package com.micro.fast.upms.service.impl; +import com.micro.fast.boot.starter.common.response.ServerResponse; import com.micro.fast.common.service.impl.SsmServiceImpl; +import com.micro.fast.upms.dao.UpmsUserMapper; import com.micro.fast.upms.dao.UpmsUserRoleMapper; +import com.micro.fast.upms.pojo.UpmsUser; import com.micro.fast.upms.pojo.UpmsUserRole; import com.micro.fast.upms.service.UpmsUserRoleService; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import java.util.List; + /** * @author lsy */ @Service -public class UpmsUserRoleServiceImpl extends SsmServiceImpl -implements UpmsUserRoleService,InitializingBean { +public class UpmsUserRoleServiceImpl implements UpmsUserRoleService{ @Autowired private UpmsUserRoleMapper mapper; - /** - * 在这个bean初始化完成后覆盖父类的mapper - */ + @Autowired + private UpmsUserMapper upmsUserMapper; + @Override - public void afterPropertiesSet() throws Exception { - super.setMapper(this.mapper); + public ServerResponse batchInsert(Integer userId, List roleIds) { + UpmsUser upmsUser = upmsUserMapper.selectByPrimaryKey(userId); + if (upmsUser == null){ + return ServerResponse.errorMsg("用户不存在"); + } + int i = mapper.batchInsert(userId, roleIds); + if (i == roleIds.size()){ + return ServerResponse.errorMsg("建立关系失败"); + } + return ServerResponse.successMsg("用户添加角色成功"); + } + + @Override + public ServerResponse batchDelete(Integer userId, List roleIds) { + UpmsUser upmsUser = upmsUserMapper.selectByPrimaryKey(userId); + if (upmsUser == null){ + return ServerResponse.errorMsg("用户不存在"); + } + int i = mapper.batchDelete(userId, roleIds); + if (i == roleIds.size()){ + return ServerResponse.errorMsg("用户删除角色失败"); + } + return ServerResponse.successMsg("用户删除角色成功"); } } diff --git a/ms/upms/src/main/resources/mappers/UpmsUserRoleMapper.xml b/ms/upms/src/main/resources/mappers/UpmsUserRoleMapper.xml index 8262746..ee7ba67 100644 --- a/ms/upms/src/main/resources/mappers/UpmsUserRoleMapper.xml +++ b/ms/upms/src/main/resources/mappers/UpmsUserRoleMapper.xml @@ -14,95 +14,16 @@ id, user_id, role_id, ctime, uptime, version - - - delete from upms_user_role - where id = #{id,jdbcType=INTEGER} + + INSERT INTO upms_user_role (user_id,role_id,ctime,version) VALUES + + (#{userId},#{roleId},unix_timestamp(now()),0) + + + + DELETE FROM upms_user_role WHERE user_id = #{userId} and role_id in + + #{roleId} + - - insert into upms_user_role (id, user_id, role_id, - ctime, uptime, version - ) - values (#{id,jdbcType=INTEGER}, #{userId,jdbcType=INTEGER}, #{roleId,jdbcType=INTEGER}, - #{ctime,jdbcType=BIGINT}, #{uptime,jdbcType=BIGINT}, #{version,jdbcType=INTEGER} - ) - - - insert into upms_user_role - - - id, - - - user_id, - - - role_id, - - - ctime, - - - uptime, - - - version, - - - - - #{id,jdbcType=INTEGER}, - - - #{userId,jdbcType=INTEGER}, - - - #{roleId,jdbcType=INTEGER}, - - - #{ctime,jdbcType=BIGINT}, - - - #{uptime,jdbcType=BIGINT}, - - - #{version,jdbcType=INTEGER}, - - - - - update upms_user_role - - - user_id = #{userId,jdbcType=INTEGER}, - - - role_id = #{roleId,jdbcType=INTEGER}, - - - ctime = #{ctime,jdbcType=BIGINT}, - - - uptime = #{uptime,jdbcType=BIGINT}, - - - version = #{version,jdbcType=INTEGER}, - - - where id = #{id,jdbcType=INTEGER} - - - update upms_user_role - set user_id = #{userId,jdbcType=INTEGER}, - role_id = #{roleId,jdbcType=INTEGER}, - ctime = #{ctime,jdbcType=BIGINT}, - uptime = #{uptime,jdbcType=BIGINT}, - version = #{version,jdbcType=INTEGER} - where id = #{id,jdbcType=INTEGER} - \ No newline at end of file