无关风月
2025-05-08 9486766c806fe1d9e082b2fd02ea1cc558f1b443
cloud-server-management/src/main/java/com/dsh/guns/modular/system/controller/system/UserMgrController.java
@@ -1,6 +1,8 @@
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;
@@ -21,11 +23,13 @@
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.Role;
import com.dsh.guns.modular.system.model.User;
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;
@@ -39,6 +43,7 @@
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.*;
import java.util.stream.Collectors;
/**
 * 系统管理员控制器
@@ -60,7 +65,8 @@
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IDeptService deptService;
    /**
@@ -77,7 +83,18 @@
     */
    @RequestMapping("/user_add")
    public String addView(Model model) {
        model.addAttribute("role",UserExt.getUser().getObjectType());
        model.addAttribute("language",UserExt.getLanguage());
        LambdaQueryWrapper<Role> roleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(UserExt.getUser().getObjectType()!=1){
            roleLambdaQueryWrapper.eq(Role::getObjId,UserExt.getUser().getObjectId());
        }
        roleLambdaQueryWrapper.eq(Role::getObjType,UserExt.getUser().getObjectType());
        List<Role> list = roleService.list(roleLambdaQueryWrapper);
        model.addAttribute("list",list);
        // 部门列表
        List<Dept> depts = deptService.list(new QueryWrapper<>());
        model.addAttribute("depts",depts);
        return PREFIX + "user_add.html";
    }
@@ -98,21 +115,35 @@
    }
    /**
     * 跳转到编辑管理员页面
     * 跳转到编辑管理员页面 type=1编辑 type=2 查看详情
     */
    @Permission
    @RequestMapping("/user_edit/{userId}")
    public String userEdit(@PathVariable Integer userId, Model model) {
    @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);
        Role role = roleService.getById(user.getId());
        Dept dept = deptService.getById(user.getDeptid());
        model.addAttribute(user);
        model.addAttribute("roleName", ConstantFactory.me().getRoleName(user.getRoleid()));
        model.addAttribute("deptName", ConstantFactory.me().getDeptName(user.getDeptid()));
        model.addAttribute("roleName", role.getName());
        model.addAttribute("deptName", dept.getFullname());
        LogObjectHolder.me().set(user);
        LambdaQueryWrapper<Role> roleLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(UserExt.getUser().getObjectType()!=1){
            roleLambdaQueryWrapper.eq(Role::getObjId,UserExt.getUser().getObjectId());
        }
        roleLambdaQueryWrapper.eq(Role::getObjType,UserExt.getUser().getObjectType());
        List<Role> list = roleService.list(roleLambdaQueryWrapper);
        model.addAttribute("list",list);
        model.addAttribute("language",UserExt.getLanguage());
        // 部门列表
        List<Dept> depts = deptService.list(new QueryWrapper<>());
        model.addAttribute("depts",depts);
        model.addAttribute("type",type);
        return PREFIX + "user_edit.html";
    }
@@ -126,9 +157,11 @@
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        User user = this.userService.getById(userId);
        Role role = roleService.getById(user.getId());
        Dept dept = deptService.getById(user.getDeptid());
        model.addAttribute(user);
        model.addAttribute("roleName", ConstantFactory.me().getRoleName(user.getRoleid()));
        model.addAttribute("deptName", user.getDeptid()==0?"顶级":ConstantFactory.me().getDeptName(user.getDeptid()));
        model.addAttribute("roleName", role.getName());
        model.addAttribute("deptName", user.getDeptid()==0?"顶级":dept.getFullname());
        LogObjectHolder.me().set(user);
        return PREFIX + "user_view.html";
    }
@@ -163,32 +196,74 @@
        }
    }
    @Autowired
    private IStoreService storeService;
    /**
     * 查询管理员列表
     */
    @RequestMapping("/list")
    @Permission
    @ResponseBody
    public Object list(@RequestParam(required = false) String name, @RequestParam(required = false) String createTime, @RequestParam(required = false) Integer deptid) {
        //创建日期
        String beginTime = null;
        String endTime = null;
        if (SinataUtil.isNotEmpty(createTime)){
            String[] timeArray = createTime.split(" - ");
            beginTime = timeArray[0];
            endTime = timeArray[1];
    public List<UserListVO> 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<UserListVO> res= userService.searchUsers(userQuery);
                return res;
            case 2:
                // 运营商查询运营商账号的员工和属于该运营商门店的员工
//                userQuery.setObjectType(2);
//                userQuery.setObjectId(objectId);
//                // 获取属于这个运营商的门店的店长ids
//                List<Integer> storeStaffIds = storeService.list(new QueryWrapper<TStore>().eq("operatorId", objectId)
//                        .ne("state", 3)).stream().map(TStore::getStoreStaffId).collect(Collectors.toList());
//                userQuery.setStoreIds(storeStaffIds);
//                List<UserListVO> res1= userService.searchUsers(userQuery);
                List<UserListVO> res1= userService.searchUsers(userQuery);
                List<UserListVO> 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<Integer> storeIds = userQuery.getStoreIds();
                storeIds.add(store.getStoreStaffId());
                List<UserListVO> res2= userService.searchUsers(userQuery);
                return res2;
        }
        User user = UserExt.getUser();
        Role role = roleService.getBaseMapper().selectById(user.getRoleid());
        if ("administrator".equals(role.getTips())) {
            Page<Map<String, Object>> page = new PageFactory<Map<String, Object>>().defaultPage();
            page.setRecords(userService.getUserListPage(page,null, name, beginTime, endTime, deptid));
            return super.packForBT(page);
        } else {
            Page<Map<String, Object>> page = new PageFactory<Map<String, Object>>().defaultPage();
            page.setRecords(userService.getUserListPage(page,null, name, beginTime, endTime, deptid));
            return super.packForBT(page);
        }
        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<Map<String, Object>> page = new PageFactory<Map<String, Object>>().defaultPage();
//            page.setRecords(userService.getUserListPage(page,null, name, beginTime, endTime, deptid));
//            return super.packForBT(page);
//        } else {
//            Page<Map<String, Object>> page = new PageFactory<Map<String, Object>>().defaultPage();
//            page.setRecords(userService.getUserListPage(page,null, name, beginTime, endTime, deptid));
//            return super.packForBT(page);
//        }
    }
    /**
@@ -198,29 +273,58 @@
    @BussinessLog(value = "添加管理员", key = "account", dict = UserDict.class)
    @Permission(Const.ADMIN_NAME)
    @ResponseBody
    public Tip add(@Valid UserDto user, BindingResult result) {
    public Object add(@Valid UserDto user, BindingResult result) {
        if (result.hasErrors()) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        // 判断账号是否重复
        //Check if the account is duplicated
        User theUser = userService.getByAccount(user.getAccount());
        if (theUser != null) {
            throw new GunsException(BizExceptionEnum.USER_ALREADY_REG);
        User theUser = new User();
        if (user.getPhone()!=""){
         theUser = userService.getByAccount(user.getPhone());
        }
        if (theUser != null){
            return 5001;
        }
        // 完善账号信息
        // Complete account information
        user.setPassword(SecureUtil.md5(user.getPassword()));
        user.setStatus(ManagerStatus.OK.getCode());
        user.setCreatetime(new Date());
        User objectUser = UserFactory.createUser(user);
        //查找平台所属公司
        //Search for the company that owns the platform.
        this.userService.save(objectUser);
        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;
    }
@@ -232,27 +336,30 @@
    @RequestMapping("/edit")
    @BussinessLog(value = "修改管理员", key = "account", dict = UserDict.class)
    @ResponseBody
    public Tip edit(@Valid UserDto user, BindingResult result) throws NoPermissionException {
    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());
//        UserExt.hasRole(Const.ADMIN_NAME)
        if (false) {
            this.userService.updateById(UserFactory.editUser(user, oldUser));
            return SUCCESS_TIP;
        } else {
            assertAuth(user.getId());
            User shiroUser = UserExt.getUser();
            if (shiroUser.getId().equals(user.getId()) || shiroUser.getId()==1) {
                this.userService.updateById(UserFactory.editUser(user, oldUser));
                return SUCCESS_TIP;
            } else {
                throw new GunsException(BizExceptionEnum.NO_PERMITION);
        // 判断是否更改电话
        if (!oldUser.getPhone().equals(user.getPhone())){
            User phone = userService.getOne(new QueryWrapper<User>().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;
    }
    /**
@@ -389,6 +496,29 @@
        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<String, String> 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")