package com.dsh.guns.modular.system.controller.system; import cn.hutool.crypto.SecureUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.dsh.course.mapper.UserMapper; import com.dsh.guns.config.UserExt; import com.dsh.guns.config.properties.GunsProperties; import com.dsh.guns.core.base.controller.BaseController; import com.dsh.guns.core.base.tips.Tip; import com.dsh.guns.core.common.annotion.BussinessLog; import com.dsh.guns.core.common.annotion.Permission; import com.dsh.guns.core.common.constant.Const; import com.dsh.guns.core.common.constant.dictmap.UserDict; import com.dsh.guns.core.common.constant.factory.ConstantFactory; import com.dsh.guns.core.common.constant.factory.PageFactory; import com.dsh.guns.core.common.constant.state.ManagerStatus; import com.dsh.guns.core.common.exception.BizExceptionEnum; import com.dsh.guns.core.db.Db; import com.dsh.guns.core.exception.GunsException; import com.dsh.guns.core.log.LogObjectHolder; import com.dsh.guns.core.util.SinataUtil; import com.dsh.guns.modular.system.controller.util.MD5; import com.dsh.guns.modular.system.factory.UserFactory; import com.dsh.guns.modular.system.model.*; import com.dsh.guns.modular.system.service.IDeptService; import com.dsh.guns.modular.system.service.IRoleService; import com.dsh.guns.modular.system.service.IStoreService; import com.dsh.guns.modular.system.service.IUserService; import com.dsh.guns.modular.system.transfer.UserDto; import com.dsh.guns.modular.system.util.OBSUploadUtil; import com.dsh.guns.modular.system.util.OssUploadUtil; import com.dsh.guns.modular.system.util.ResultUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.naming.NoPermissionException; import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; import java.util.*; import java.util.stream.Collectors; /** * 系统管理员控制器 * * @author fengshuonan * @Date 2017年1月11日 下午1:08:17 */ @Controller @RequestMapping("/mgr") public class UserMgrController extends BaseController { private static String PREFIX = "/system/user/"; @Autowired private GunsProperties gunsProperties; @Autowired private IUserService userService; @Autowired private IRoleService roleService; @Autowired private IDeptService deptService; /** * 跳转到查看管理员列表的页面 */ @RequestMapping("") public String index(Model model) { model.addAttribute("language",UserExt.getLanguage()); return PREFIX + "user.html"; } /** * 跳转到查看管理员列表的页面 */ @RequestMapping("/user_add") public String addView(Model model) { model.addAttribute("role",UserExt.getUser().getObjectType()); model.addAttribute("language",UserExt.getLanguage()); LambdaQueryWrapper roleLambdaQueryWrapper = new LambdaQueryWrapper<>(); if(UserExt.getUser().getObjectType()!=1){ roleLambdaQueryWrapper.eq(Role::getObjId,UserExt.getUser().getObjectId()); } roleLambdaQueryWrapper.eq(Role::getObjType,UserExt.getUser().getObjectType()); List list = roleService.list(roleLambdaQueryWrapper); model.addAttribute("list",list); // 部门列表 List depts = deptService.list(new QueryWrapper<>()); model.addAttribute("depts",depts); return PREFIX + "user_add.html"; } /** * 跳转到角色分配页面 */ //@RequiresPermissions("/mgr/role_assign") //利用shiro自带的权限检查 @Permission @RequestMapping("/role_assign/{userId}") public String roleAssign(@PathVariable Integer userId, Model model) { if (Objects.isNull(userId)) { throw new GunsException(BizExceptionEnum.REQUEST_NULL); } User user = (User) Db.create(UserMapper.class).selectOneByCon("id", userId); model.addAttribute("userId", userId); model.addAttribute("userAccount", user.getAccount()); return PREFIX + "user_roleassign.html"; } /** * 跳转到编辑管理员页面 type=1编辑 type=2 查看详情 */ @Permission @RequestMapping("/user_edit/{userId}/{type}") public String userEdit(@PathVariable Integer userId,@PathVariable Integer type, Model model) { model.addAttribute("role",UserExt.getUser().getObjectType()); if (Objects.isNull(userId)) { throw new GunsException(BizExceptionEnum.REQUEST_NULL); } assertAuth(userId); User user = this.userService.getById(userId); model.addAttribute(user); model.addAttribute("roleName", ConstantFactory.me().getRoleName(user.getRoleid())); model.addAttribute("deptName", ConstantFactory.me().getDeptName(user.getDeptid())); LogObjectHolder.me().set(user); LambdaQueryWrapper roleLambdaQueryWrapper = new LambdaQueryWrapper<>(); if(UserExt.getUser().getObjectType()!=1){ roleLambdaQueryWrapper.eq(Role::getObjId,UserExt.getUser().getObjectId()); } roleLambdaQueryWrapper.eq(Role::getObjType,UserExt.getUser().getObjectType()); List list = roleService.list(roleLambdaQueryWrapper); model.addAttribute("list",list); model.addAttribute("language",UserExt.getLanguage()); // 部门列表 List depts = deptService.list(new QueryWrapper<>()); model.addAttribute("depts",depts); model.addAttribute("type",type); return PREFIX + "user_edit.html"; } /** * 跳转到查看用户详情页面 */ @RequestMapping("/user_info") public String userInfo(Model model) { Integer userId = UserExt.getUser().getId(); if (Objects.isNull(userId)) { throw new GunsException(BizExceptionEnum.REQUEST_NULL); } User user = this.userService.getById(userId); model.addAttribute(user); model.addAttribute("roleName", ConstantFactory.me().getRoleName(user.getRoleid())); model.addAttribute("deptName", user.getDeptid()==0?"顶级":ConstantFactory.me().getDeptName(user.getDeptid())); LogObjectHolder.me().set(user); return PREFIX + "user_view.html"; } /** * 跳转到修改密码界面 */ @RequestMapping("/user_chpwd") public String chPwd() { return PREFIX + "user_chpwd.html"; } /** * 修改当前用户的密码 */ @RequestMapping("/changePwd") @ResponseBody public Object changePwd(@RequestParam String oldPwd, @RequestParam String newPwd, @RequestParam String rePwd) { if (!newPwd.equals(rePwd)) { throw new GunsException(BizExceptionEnum.TWO_PWD_NOT_MATCH); } Integer userId = UserExt.getUser().getId(); User user = userService.getById(userId); String oldMd5 = MD5.md5(oldPwd); if (user.getPassword().equals(oldMd5)) { String newMd5 = MD5.md5(newPwd); user.setPassword(newMd5); user.updateById(); return SUCCESS_TIP; } else { throw new GunsException(BizExceptionEnum.OLD_PWD_NOT_RIGHT); } } @Autowired private IStoreService storeService; /** * 查询管理员列表 */ @RequestMapping("/list") @Permission @ResponseBody public List list(String name, String phone, String idCard, Integer isSales, Integer state) { UserQuery userQuery = new UserQuery(); userQuery.setName(name); userQuery.setPhone(phone); userQuery.setIdCard(idCard); userQuery.setIsSales(isSales); userQuery.setStatus(state); // 根据当前登陆人类型来查询员工列表 Integer objectType = UserExt.getUser().getObjectType(); Integer objectId = UserExt.getUser().getObjectId(); switch (objectType){ case 1: // 平台查询所有 List res= userService.searchUsers(userQuery); return res; case 2: // 运营商查询运营商账号的员工和属于该运营商门店的员工 // userQuery.setObjectType(2); // userQuery.setObjectId(objectId); // // 获取属于这个运营商的门店的店长ids // List storeStaffIds = storeService.list(new QueryWrapper().eq("operatorId", objectId) // .ne("state", 3)).stream().map(TStore::getStoreStaffId).collect(Collectors.toList()); // userQuery.setStoreIds(storeStaffIds); // List res1= userService.searchUsers(userQuery); List res1= userService.searchUsers(userQuery); List filteredList = res1.stream() .filter(vo -> Objects.equals(vo.getVersion(), UserExt.getUser().getId())) .collect(Collectors.toList()); // return res1; return filteredList; case 3: // 门店只查询门店员工 TStore store = storeService.getById(objectId); List storeIds = userQuery.getStoreIds(); storeIds.add(store.getStoreStaffId()); List res2= userService.searchUsers(userQuery); return res2; } return new ArrayList<>(); // //创建日期 // String beginTime = null; // String endTime = null; // if (SinataUtil.isNotEmpty(createTime)){ // String[] timeArray = createTime.split(" - "); // beginTime = timeArray[0]; // endTime = timeArray[1]; // } // User user = UserExt.getUser(); // Role role = roleService.getBaseMapper().selectById(user.getRoleid()); // if ("administrator".equals(role.getTips())) { // Page> page = new PageFactory>().defaultPage(); // page.setRecords(userService.getUserListPage(page,null, name, beginTime, endTime, deptid)); // return super.packForBT(page); // } else { // Page> page = new PageFactory>().defaultPage(); // page.setRecords(userService.getUserListPage(page,null, name, beginTime, endTime, deptid)); // return super.packForBT(page); // } } /** * 添加管理员 */ @RequestMapping("/add") @BussinessLog(value = "添加管理员", key = "account", dict = UserDict.class) @Permission(Const.ADMIN_NAME) @ResponseBody public Object add(@Valid UserDto user, BindingResult result) { if (result.hasErrors()) { throw new GunsException(BizExceptionEnum.REQUEST_NULL); } // 判断账号是否重复 User theUser = new User(); if (user.getPhone()!=""){ theUser = userService.getByAccount(user.getPhone()); } if (theUser != null){ return 5001; } // 完善账号信息 user.setPassword(SecureUtil.md5(user.getPassword())); user.setStatus(ManagerStatus.OK.getCode()); user.setCreatetime(new Date()); User objectUser = UserFactory.createUser(user); if (user.getRoleid().equals("")){ objectUser.setObjectType(2); }else { objectUser.setObjectType(Integer.valueOf(user.getRoleid())); } objectUser.setObjectId(UserExt.getUser().getObjectId()); objectUser.setRoleid(user.getRoleid()); objectUser.setAccount(user.getPhone()); objectUser.setName(user.getName()); objectUser.setPhone(user.getPhone()); objectUser.setBirthday(user.getBirthday()); objectUser.setDeptid(user.getDeptid()); objectUser.setIdCard(user.getIdCard()); objectUser.setSex(user.getSex()); objectUser.setStatus(1); objectUser.setPassword(SecureUtil.md5("a123456")); objectUser.setCreatetime(new Date()); // 运营商添加员工 if (UserExt.getUser().getObjectType() == 2){ objectUser.setObjectType(UserExt.getUser().getObjectType()); objectUser.setRoleid("2"); objectUser.setObjectId(UserExt.getUser().getObjectId()); } // 门店添加员工 if (UserExt.getUser().getObjectType() == 3){ objectUser.setObjectType(UserExt.getUser().getObjectType()); objectUser.setRoleid("3"); objectUser.setObjectId(UserExt.getUser().getObjectId()); } objectUser.setVersion(UserExt.getUser().getId()); this.userService.save(objectUser); return SUCCESS_TIP; } /** * 修改管理员 * * @throws NoPermissionException */ @RequestMapping("/edit") @BussinessLog(value = "修改管理员", key = "account", dict = UserDict.class) @ResponseBody public Object edit(@Valid UserDto user, BindingResult result) throws NoPermissionException { if (result.hasErrors()) { throw new GunsException(BizExceptionEnum.REQUEST_NULL); } User oldUser = userService.getById(user.getId()); // 判断是否更改电话 if (!oldUser.getPhone().equals(user.getPhone())){ User phone = userService.getOne(new QueryWrapper().eq("phone", user.getPhone())); if (phone == null){ oldUser.setPhone(user.getPhone()); }else{ return 5001; } } oldUser.setName(user.getName()); oldUser.setBirthday(user.getBirthday()); oldUser.setRoleid(user.getRoleid()); oldUser.setDeptid(user.getDeptid()); oldUser.setIdCard(user.getIdCard()); oldUser.setSex(user.getSex()); if (user.getAvatar()!=null && (!user.getAvatar().equals(""))) oldUser.setAvatar(user.getAvatar()); this.userService.updateById(oldUser); return SUCCESS_TIP; } /** * 删除管理员(逻辑删除) * Delete administrator (logical deletion) */ @RequestMapping("/delete") @BussinessLog(value = "删除管理员", key = "userId", dict = UserDict.class) @Permission @ResponseBody public Tip delete(@RequestParam Integer userId) { if (Objects.isNull(userId)) { throw new GunsException(BizExceptionEnum.REQUEST_NULL); } //不能删除超级管理员 //Cannot delete super administrator. if (userId.equals(Const.ADMIN_ID)) { throw new GunsException(BizExceptionEnum.CANT_DELETE_ADMIN); } assertAuth(userId); this.userService.setStatus(userId, ManagerStatus.DELETED.getCode()); return SUCCESS_TIP; } /** * 查看管理员详情 */ @RequestMapping("/view/{userId}") @ResponseBody public User view(@PathVariable Integer userId) { if (Objects.isNull(userId)) { throw new GunsException(BizExceptionEnum.REQUEST_NULL); } assertAuth(userId); return this.userService.getById(userId); } /** * 重置管理员的密码 */ @RequestMapping("/reset") @BussinessLog(value = "重置管理员密码", key = "userId", dict = UserDict.class) @Permission(Const.ADMIN_NAME) @ResponseBody public Tip reset(@RequestParam Integer userId) { if (Objects.isNull(userId)) { throw new GunsException(BizExceptionEnum.REQUEST_NULL); } assertAuth(userId); User user = this.userService.getById(userId); user.setPassword(SecureUtil.md5(Const.DEFAULT_PWD)); this.userService.updateById(user); return SUCCESS_TIP; } /** * 冻结用户 */ @RequestMapping("/freeze") @BussinessLog(value = "冻结用户", key = "userId", dict = UserDict.class) @Permission(Const.ADMIN_NAME) @ResponseBody public Tip freeze(@RequestParam Integer userId) { if (Objects.isNull(userId)) { throw new GunsException(BizExceptionEnum.REQUEST_NULL); } //不能冻结超级管理员 Cannot freeze super administrator. if (userId.equals(Const.ADMIN_ID)) { throw new GunsException(BizExceptionEnum.CANT_FREEZE_ADMIN); } assertAuth(userId); this.userService.setStatus(userId, ManagerStatus.FREEZED.getCode()); return SUCCESS_TIP; } /** * 解除冻结用户 */ @RequestMapping("/unfreeze") @BussinessLog(value = "解除冻结用户", key = "userId", dict = UserDict.class) @Permission(Const.ADMIN_NAME) @ResponseBody public Tip unfreeze(@RequestParam Integer userId) { if (Objects.isNull(userId)) { throw new GunsException(BizExceptionEnum.REQUEST_NULL); } assertAuth(userId); this.userService.setStatus(userId, ManagerStatus.OK.getCode()); return SUCCESS_TIP; } /** * 分配角色 */ @RequestMapping("/setRole") @BussinessLog(value = "分配角色", key = "userId,roleIds", dict = UserDict.class) @Permission(Const.ADMIN_NAME) @ResponseBody public Tip setRole(@RequestParam("userId") Integer userId, @RequestParam("roleIds") String roleIds) { if (Objects.isNull(userId) || Objects.isNull(roleIds) ) { throw new GunsException(BizExceptionEnum.REQUEST_NULL); } //不能修改超级管理员 //Cannot modify super administrator if (userId.equals(Const.ADMIN_ID)) { throw new GunsException(BizExceptionEnum.CANT_CHANGE_ADMIN); } assertAuth(userId); this.userService.setRoles(userId, roleIds); return SUCCESS_TIP; } /** * 上传图片 */ @RequestMapping(method = RequestMethod.POST, path = "/uploadImg") @ResponseBody public String uploadImg(@RequestPart("file") MultipartFile picture) { String pictureName = ""; try { pictureName = OssUploadUtil.ossUpload("img/", picture); }catch (Exception e){ e.printStackTrace(); } return pictureName; } /** * 上传图片 */ /** * 上传图片Amis */ @RequestMapping(method = RequestMethod.POST, path = "/uploadImgAmis") @ResponseBody public ResultUtil uploadImgAmis(@RequestPart("file") MultipartFile picture, HttpServletResponse response) { String value = ""; try { value = OssUploadUtil.ossUpload("img/", picture); }catch (Exception e){ e.printStackTrace(); } System.out.println("图片返回地址-------"+value); Map map = new HashMap<>(); map.put("value",value); // return ResultUtil.success(0,map); return ResultUtil.getResult(0,null,null,map); } @RequestMapping(method = RequestMethod.POST, path = "/uploadFile") @ResponseBody public String uploadFile(@RequestPart("file") MultipartFile picture) { String pictureName = ""; try { pictureName = OssUploadUtil.ossUpload("video/", picture); }catch (Exception e){ e.printStackTrace(); } return pictureName; } /** * 判断当前登录的用户是否有操作这个用户的权限 */ private void assertAuth(Integer userId) { // if (ShiroKit.isAdmin()) { // return; // } // List deptDataScope = ShiroKit.getDeptDataScope(); // User user = this.userService.getById(userId); // Integer deptid = user.getDeptid(); // if (deptDataScope.contains(deptid)) { // return; // } else { // throw new GunsException(BizExceptionEnum.NO_PERMITION); // } } }