SysUsersController.java 6.29 KB
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
package com.lyms.cm.controller.sys;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.lyms.cm.entity.sys.SysRoles;
import com.lyms.cm.entity.sys.SysUserRoleMaps;
import com.lyms.cm.entity.sys.SysUsers;
import com.lyms.cm.service.sys.SysRolesService;
import com.lyms.cm.service.sys.SysUserRoleMapsService;
import com.lyms.cm.service.sys.SysUsersService;
import com.lyms.constants.OperationName;
import com.lyms.util.InstanceUtils;
import com.lyms.util.StrUtils;
import com.lyms.web.bean.AjaxResult;
import com.lyms.web.controller.BaseController;

/**
* <p>
* 用户表 前端控制器
* </p>
*
* @author maliang
* @since 2017-03-02
*/
@Controller
@RequestMapping("/sysUsers")
public class SysUsersController extends BaseController {

@Autowired
private SysUsersService userService;
@Autowired
private SysRolesService sysRolesService;
@Autowired
private SysUserRoleMapsService sysUserRoleMapsService;

/**
* 创建用户
* <p>
* TODO
*
* @param user
* @return
*/
@RequestMapping(value = "/create", method = RequestMethod.POST)
@ResponseBody
public AjaxResult create(SysUsers user, AjaxResult ajaxResult) {
if(!userService.isAllowUpdate(user.getAccount(),null)){
ajaxResult.setSuccess(true);
ajaxResult.setMessage("新增失败!" + user.getAccount() + " 已经存在,请修改登录账号!");
return ajaxResult;
}
String userid = StrUtils.uuid();
user.setId(userid);
boolean tag = userService.addUser(user,getUserRoleList(userid, getParameter("roles")));
return handleAjaxResult(ajaxResult, tag, OperationName.CREATE);
}

/**
* 跳转到用户列表页面
*
* @return
*/
@RequestMapping(value = "/toList", method = { RequestMethod.GET })
public String toList() {
return "/user/user_list";
}

/**
* 用户列表
*
* @param page
* @param model
* @return
*/
@RequestMapping(value = "/list", method = { RequestMethod.POST, RequestMethod.GET })
@ResponseBody
public Map<String, Object> list(Model model) {
Page<SysUsers> page = getPage();
EntityWrapper<SysUsers> ew = new EntityWrapper<SysUsers>();
ew.where("ifDel=0");
String searchName = getParameter("name");
String searchAccount = getParameter("account");
String searchPhone = getParameter("phone");
if(StrUtils.isNotEmpty(searchName)){
ew.and("name like {0}", searchName.trim() + "%");
}
if(StrUtils.isNotEmpty(searchAccount)){
ew.and("account like {0}", searchAccount.trim() + "%");
}
if(StrUtils.isNotEmpty(searchPhone)){
ew.and("phone like {0}", searchPhone.trim() + "%");
}
page = userService.selectPage(page,ew);
return toGridData(page);
}

/**
* 跳转到编辑页面
*
* @return
*/
@RequestMapping(value = { "/{id}/toEdit" }, method = RequestMethod.GET)
public String toEdit(@PathVariable String id, Model model) {
if (!StringUtils.isBlank(id)) {
SysUsers user = userService.selectById(id);
model.addAttribute("user", user);
//当前用户角色数据
List<SysUserRoleMaps> urList = sysUserRoleMapsService
.selectList(new EntityWrapper<SysUserRoleMaps>().where("user_id={0}", "'" + id + "'"));
StringBuilder urSB = new StringBuilder();
for(SysUserRoleMaps urEntity : urList){
urSB.append("," + urEntity.getRoleId());
}
model.addAttribute("userRoles", urSB.toString().replaceFirst(",", ""));
}
//所有角色数据
List<SysRoles> roleList = sysRolesService.selectList(new EntityWrapper<SysRoles>().where("ifDel=0"));
model.addAttribute("roleString",toJson(roleList));
return "/user/user_edit";
}

/**
* 修改信息
*/
@RequestMapping(value = "/update", method = { RequestMethod.POST })
@ResponseBody
public AjaxResult update(SysUsers user, AjaxResult ajaxResult) {
if(!userService.isAllowUpdate(user.getAccount(),user.getId())){
ajaxResult.setSuccess(true);
ajaxResult.setMessage("修改失败! " + user.getAccount()+" 已经存在,请修改登录账号!");
return ajaxResult;
}
boolean tag = userService.updateUser(user,getUserRoleList(user.getId(), getParameter("roles")));
return handleAjaxResult(ajaxResult, tag, OperationName.UPDATE);

}

/**
* 删除用户
*
* @return
*/
@RequestMapping(value = "/{id}/delete", method = { RequestMethod.GET, RequestMethod.POST })
@ResponseBody
public AjaxResult delete(@PathVariable String id, AjaxResult ajaxResult) {
int tag = userService.deleteLogicById(id);
return handleAjaxResult(ajaxResult, tag, OperationName.DELETE);
}
/**
* <li>@Description:获取用户角色对应关系列表
* <li>@param roleId
* <li>@param permissionIds
* <li>@return
* <li>创建人:方承
* <li>创建时间:2017年3月9日
* <li>修改人:
* <li>修改时间:
*/
private List<SysUserRoleMaps> getUserRoleList(String userId, String roles) {
List<SysUserRoleMaps> urList = InstanceUtils.newArrayList();
if (StrUtils.isNotEmpty(roles)) {
String[] roleArray = roles.split(",");
for (String roleId : roleArray) {
if (StrUtils.isNotEmpty(roleId)) {
SysUserRoleMaps urentity = new SysUserRoleMaps();
urentity.setId(StrUtils.uuid());
urentity.setIfdel(0);
urentity.setRoleId(roleId);
urentity.setUserId(userId);
urList.add(urentity);
}
}
}
return urList;
}

}