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.base.common.entity.role.Roles;
import com.lyms.base.common.entity.role.UserRoleMaps;
import com.lyms.base.common.entity.user.Users;
import com.lyms.base.common.service.role.RolesService;
import com.lyms.base.common.service.role.UserRoleMapsService;
import com.lyms.base.common.service.user.UsersService;
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 UsersService userService;
@Autowired
private RolesService sysRolesService;
@Autowired
private UserRoleMapsService sysUserRoleMapsService;
/**
* 创建用户
* <p>
* TODO
*
* @param user
* @return
*/
@RequestMapping(value = "/create", method = RequestMethod.POST)
@ResponseBody
public AjaxResult create(Users 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<Users> page = getPage();
EntityWrapper<Users> ew = new EntityWrapper<Users>();
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)) {
Users user = userService.selectById(id);
model.addAttribute("user", user);
// 当前用户角色数据
List<UserRoleMaps> urList = sysUserRoleMapsService
.selectList(new EntityWrapper<UserRoleMaps>().where("user_id={0}", "'" + id + "'"));
StringBuilder urSB = new StringBuilder();
for (UserRoleMaps urEntity : urList) {
urSB.append("," + urEntity.getRoleId());
}
model.addAttribute("userRoles", urSB.toString().replaceFirst(",", ""));
}
// 所有角色数据
List<Roles> roleList = sysRolesService.selectList(new EntityWrapper<Roles>().where("ifDel=0"));
model.addAttribute("roleString", toJson(roleList));
return "/user/user_edit";
}
/**
* 修改信息
*/
@RequestMapping(value = "/update", method = { RequestMethod.POST })
@ResponseBody
public AjaxResult update(Users 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<UserRoleMaps> getUserRoleList(String userId, String roles) {
List<UserRoleMaps> urList = InstanceUtils.newArrayList();
if (StrUtils.isNotEmpty(roles)) {
String[] roleArray = roles.split(",");
for (String roleId : roleArray) {
if (StrUtils.isNotEmpty(roleId)) {
UserRoleMaps urentity = new UserRoleMaps();
urentity.setId(StrUtils.uuid());
urentity.setIfdel(0);
urentity.setRoleId(roleId);
urentity.setUserId(userId);
urList.add(urentity);
}
}
}
return urList;
}
}