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;
/**
*
* 用户表 前端控制器
*
*
* @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;
/**
* 创建用户
*
* 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 list(Model model) {
Page page = getPage();
EntityWrapper ew = new EntityWrapper();
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 urList = sysUserRoleMapsService
.selectList(new EntityWrapper().where("user_id={0}", "'" + id + "'"));
StringBuilder urSB = new StringBuilder();
for(SysUserRoleMaps urEntity : urList){
urSB.append("," + urEntity.getRoleId());
}
model.addAttribute("userRoles", urSB.toString().replaceFirst(",", ""));
}
//所有角色数据
List roleList = sysRolesService.selectList(new EntityWrapper().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);
}
/**
* @Description:获取用户角色对应关系列表
* @param roleId
* @param permissionIds
* @return
* 创建人:方承
* 创建时间:2017年3月9日
* 修改人:
* 修改时间:
*/
private List getUserRoleList(String userId, String roles) {
List 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;
}
}