package com.yeeliu.modules.system.controller;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yeeliu.common.domain.MessagePojo;
import com.yeeliu.common.mgo.MessagePojoMgo;
import com.yeeliu.common.persistence.BaseWxUser;
import com.yeeliu.common.persistence.BusinessException;
import com.yeeliu.common.persistence.PageView;
import com.yeeliu.common.persistence.Parameter;
import com.yeeliu.common.pojo.DwzPojo;
import com.yeeliu.common.utils.ConfigUtil;
import com.yeeliu.common.utils.FileUtilsInfo;
import com.yeeliu.common.utils.MapperUtil;
import com.yeeliu.common.utils.ServiceUtils;
import com.yeeliu.common.utils.SessionUtil;
import com.yeeliu.modules.system.domain.AccessRecord;
import com.yeeliu.modules.system.domain.ErrorRecord;
import com.yeeliu.modules.system.entity.Anomaly404;
import com.yeeliu.modules.system.entity.IpInfo;
import com.yeeliu.modules.system.entity.MenuInfo;
import com.yeeliu.modules.system.entity.RoleInfo;
import com.yeeliu.modules.system.entity.UserInfo;
import com.yeeliu.modules.system.mgo.AccessRecordMgo;
import com.yeeliu.modules.system.mgo.ErrorRecordMgo;
import com.yeeliu.modules.system.service.SystemService;
import com.yeeliu.modules.system.timer.SystemStateTimer;
import com.yeeliu.modules.system.timer.SystemStateTimer;
/**
* 用户、角色、菜单、权限、页面
* 创建人 sutenqiang
* 创建时间 2016-10-7 下午3:54:13
*/
@Controller
@RequestMapping(value = "/userPermission")
public class UserPermissionController {
public static final String PREFIX = "/system/";
@Autowired
SystemService svr;
@Autowired
MessagePojoMgo wxMgo;//微信记录
@Autowired
AccessRecordMgo acMgo;//访问记录
@Autowired
ErrorRecordMgo erMgo;//错误记录
/**
* 微信列表
*/
@RequestMapping(value = "wxuserList${html}")
public String wxuserList(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap,PageView page,BaseWxUser user) throws Exception {
page=ServiceUtils.getWxService().queryLsit(page,user);
modelMap.put("page",page);
return PREFIX+"wxuserList";
}
/**
* beluga用户列表
*/
@RequestMapping(value = "belugawxuserList${html}")
public String belugaWxUserList(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap,PageView page,BaseWxUser user) throws Exception {
page=ServiceUtils.getWxService().queryBelugaLsit(page,user);
modelMap.put("page",page);
return PREFIX+"wxbelugauserList";
}
/**
* 用户管理列表
*/
@RequestMapping(value = "userList${html}")
public String userList(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap,PageView page,UserInfo user) throws Exception {
svr.pageViewQuery(page,user);
modelMap.put("page",page);
return PREFIX+"userList";
}
@RequestMapping(value = "userListByLookUp${html}")
public String userListByLookUp(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap,PageView page,UserInfo user) throws Exception {
svr.pageViewQuery(page,user);
modelMap.put("page",page);
return PREFIX+"userListByLookUp";
}
//用户修改
@RequestMapping(value = "userUpdate${html}")
public String userUpdate(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap,Long id) throws Exception {
if(id!=null){
UserInfo user=(UserInfo) svr.get(UserInfo.class, id);
modelMap.put("user",user);
modelMap.put("userRoles",user.getRoles());
}else{
UserInfo user=new UserInfo();
modelMap.put("user",user);
}
modelMap.put("roles",svr.find("from RoleInfo"));
return PREFIX+"userUpdate";
}
//个人用户信息修改
@RequestMapping(value = "userInfoUpdate${html}")
public String userInfoUpdate(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception {
UserInfo user = SessionUtil.getUser(request);
modelMap.put("user",user);
return PREFIX+"userInfo";
}
//个人用户信息保存
@ResponseBody
@RequestMapping(value = "userInfoSave${json}")
public DwzPojo userInfoSave(HttpServletRequest request,HttpServletResponse response,String password,String password2,String name) throws Exception {
UserInfo user = SessionUtil.getUser(request);
if(!password.equals(password2)){
return new DwzPojo(300,"密码不一致!");
}
if(!user.getPassword().equals(password)){//新旧不一样就修改
svr.update("UserInfo", "password", password, " id= "+user.getId());
user.setPassword(password);
}
if(name!=null&&!name.equals(user.getName())){//新旧不一样就修改
svr.update("UserInfo", "name", name, " id= "+user.getId());
user.setName(name);
}
return new DwzPojo("保存成功!","");
}
//用户保存
@ResponseBody
@RequestMapping(value = "userSave${json}")
public DwzPojo userSave(HttpServletRequest request,HttpServletResponse response,UserInfo user,Long[] roles) throws Exception {
//保存角色
if(roles!=null){
for(Long r:roles){
user.getRoleList().add(new RoleInfo(r));
}
}
if(user!=null){
svr.saveBase(user);
}
return new DwzPojo("保存成功!","userList");
}
//用户删除
@ResponseBody
@RequestMapping(value = "delUser${json}")
public DwzPojo delUser(HttpServletRequest request,HttpServletResponse response,Long id) throws Exception {
if(id!=null){
UserInfo user=(UserInfo) svr.get(UserInfo.class, id);
svr.delete(user);
}
DwzPojo pojo=new DwzPojo("删除成功!","userList");
return pojo;
}
//----------------------------role---------------------------------------------------------------------------------------------------
@RequestMapping(value = "roleList${html}")
public String roleList(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap,PageView page,RoleInfo role) throws Exception {
svr.pageViewQuery(page,role);
modelMap.put("page",page);
return PREFIX+"roleList";
}
//角色修改
@RequestMapping(value = "roleUpdate${html}")
public String roleUpdate(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap,Long id) throws Exception {
if(id!=null){
RoleInfo user=(RoleInfo) svr.get(RoleInfo.class, id);
modelMap.put("obj",user);
modelMap.put("roleMenus",user.getMenus());
}
//默认菜单-独立模块
modelMap.put("menu0",svr.find("from MenuInfo where menuType=0"));
//主菜单
modelMap.put("menu1",svr.find("from MenuInfo where menuType=1"));
return PREFIX+"roleUpdate";
}
//角色保存
@ResponseBody
@RequestMapping(value = "roleSave${json}")
public DwzPojo roleSave(HttpServletRequest request,HttpServletResponse response,RoleInfo role,Long[] menus) throws Exception {
//保存角色
if(menus!=null){
for(Long m:menus){
role.getMenuList().add(new MenuInfo(m));
}
}
if(role!=null){
svr.saveBase(role);
}
return new DwzPojo("保存成功!","roleList");
}
//角色删除
@ResponseBody
@RequestMapping(value = "delRole${json}")
public DwzPojo delRole(HttpServletRequest request,HttpServletResponse response,Long id) throws Exception {
if(id!=null){
RoleInfo role=(RoleInfo) svr.get(RoleInfo.class, id);
svr.delete(role);
}
DwzPojo pojo=new DwzPojo("删除成功!","roleList");
return pojo;
}
//----------------------------menu---------------------------------------------------------------------------------------------------
@RequestMapping(value = "menuList