ModuleController.java 9.2 KB
Newer Older
张骞 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
package com.template.back.server.controller.system;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.template.back.common.pojo.system.Admin;
import com.template.back.common.pojo.system.Module;
import com.template.back.common.pojo.system.Role;
import com.template.back.common.service.LoggerService;
import com.template.back.common.utils.AdminThreadLocal;
import com.template.back.common.vo.R;
import com.template.back.server.service.system.ModuleService;
import com.template.back.server.service.system.RoleService;
14 15 16 17
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
张骞 已提交
18 19 20 21 22 23 24 25 26 27 28 29
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 张骞
 * @version 2.0
 * 模块管理控制层程序
 * 更新日志:前端更换为html页面,使用json格式数据进行交互,后端进行对应更改
 */
30
@Api(tags = "功能模块相关接口")  //knife4j注解,用于自动生成api文档
张骞 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
@RestController   //标记为控制层
@RequestMapping("/system/module")   //定义请求映射路径
@Slf4j   //注解日志
public class ModuleController {
    //注入自定义日志业务层
    @Autowired
    private LoggerService loggerService;

    //注入模块业务层
    @Autowired
    private ModuleService moduleService;

    //注入角色业务层
    @Autowired
    private RoleService roleService;

    /**
     * 查看模块列表的方法
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
54 55 56 57 58 59
    @ApiOperation(value = "查询模块列表接口")  //knife4j注解,用于对接口方法进行说明
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页码",required = true,defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize",value = "每页数据长度",required = true,defaultValue = "10"),
            @ApiImplicitParam(name = "name",value = "模块名称",required = false)
    })  //knife4j注解,用于对接口参数进行说明
张骞 已提交
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    @GetMapping("page")
    public R<Page> list(@RequestParam(value = "page",defaultValue = "1")Integer page,
                        @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
                        @RequestParam(value = "name",required = false)String name){
        try{
            //调用业务层的方法
            Page all = this.moduleService.queryPage(page,pageSize,name);
            //向前端存入数据
            return R.success(all);
        }catch (Exception exception){
            //出现异常,记录日志
            this.loggerService.saveLog(this.getClass().getName(),  //获取当前类名及类数据
                    Thread.currentThread().getStackTrace()[1].getMethodName(),  //获取当前方法名
                    exception, "查看模块列表");
            log.error("后台工程:controller.ModuleController出现异常!异常信息为:" + exception);
        }
        //出现异常,返回错误信息
        return R.error("查询数据列表!");
    }

    /**
     * 新增保存数据的方法
     * @param module
     * @return
     */
85 86
    @ApiOperation(value = "新增模块接口")  //knife4j注解,用于对接口方法进行说明
    @ApiImplicitParams({@ApiImplicitParam(name = "module",value = "新增的模块",required = true)})  //knife4j注解,用于对接口参数进行说明
张骞 已提交
87
    @PostMapping("save")
88
    public R<String> addSave(@RequestBody Module module){
张骞 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
        try {
            //调用业务层的方法进行数据插入
            Boolean insert = this.moduleService.insert(module);
            if(!insert){
                //出现异常,返回错误信息
                return R.error("数据保存失败!");
            }else {
                //返回页面,请求重定向到列表页
                return R.success("数据保存成功!");
            }
        } catch (Exception exception) {
            //记录日志
            this.loggerService.saveLogAndUser(this.getClass().getName(),  //获取当前类名及类数据
                    Thread.currentThread().getStackTrace()[1].getMethodName(),  //获取当前方法名
                    this.getAdmin().getName(),    //获取当前登录的用户信息
                    exception, "");
            log.error("后台工程:controller.DeptController出现异常!异常信息为:" + exception);
        }
        //出现异常,返回错误信息
        return R.error("数据保存失败!");
    }

111

张骞 已提交
112 113 114 115 116 117

    /**
     * 根据id集合删除数据的方法
     * @param ids
     * @return
     */
118 119
    @ApiOperation(value = "删除模块接口")  //knife4j注解,用于对接口方法进行说明
    @ApiImplicitParams({@ApiImplicitParam(name = "ids",value = "选中的模块id集合",required = true)})  //knife4j注解,用于对接口参数进行说明
张骞 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
    @PostMapping("delete")
    public R<String> delete(@RequestParam("ids") List<Long> ids) {
        try {
            //当前模块下有子模块,不允许删除
            for (Long id : ids) {
                List<Module> moduleList = this.moduleService.findChildrenById(id);
                if(CollUtil.isNotEmpty(moduleList)){
                    return R.error("当前模块含有子模块,不允许删除!");
                }
            }

            //当前模块被角色关联,不允许删除
            for (Long id : ids) {
                List<Role> roleList = this.roleService.findRoleByModuleId(id);
                if(CollUtil.isNotEmpty(roleList)){
                    return R.error("当前模块关联了角色,不允许删除!");
                }
            }

            //调用业务层的删除方法
            Boolean delete = this.moduleService.deleteByIds(ids);
            //判断业务层返回的状态
            if(!delete){
                //写入错误数据
                return R.error("数据删除失败!");
            }
            return R.success("数据删除成功!");
        } catch (Exception exception) {
            //记录日志
            this.loggerService.saveLog(this.getClass().getName(),  //获取当前类名及类数据
                    Thread.currentThread().getStackTrace()[1].getMethodName(),  //获取当前方法名
                    exception, "");
            log.error("后台工程:controller.ModuleController出现异常!异常信息为:" + exception);
        }
        //写入错误数据
        return R.error("数据删除失败!");
    }

    /**
     * 根据id查询单个的方法
     * @param id
     * @return
     */
163 164
    @ApiOperation(value = "根据id查询单条数据接口")  //knife4j注解,用于对接口方法进行说明
    @ApiImplicitParams({@ApiImplicitParam(name = "id",value = "选中的模块id",required = true)})  //knife4j注解,用于对接口参数进行说明
张骞 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
    @GetMapping("/findById")
    public R<Module> findById(@RequestParam("id") Long id) {
        //01.数据回显
        if(ObjectUtil.isNotEmpty(id)){
            Module module = this.moduleService.queryById(id);
            if(ObjectUtil.isNotEmpty(module)){
                //将数据写入
                return R.success(module);
            }
        }
        //02.返回错误信息
        return R.error("未查询到要修改的数据!");
    }

    /**
     * 保存修改后数据的方法
     * @return
     */
183 184
    @ApiOperation(value = "修改后台用户接口")  //knife4j注解,用于对接口方法进行说明
    @ApiImplicitParams({@ApiImplicitParam(name = "module",value = "修改的模块",required = true)})  //knife4j注解,用于对接口参数进行说明
张骞 已提交
185
    @PutMapping("edit")
186
    public R<String> update(@RequestBody Module module){
张骞 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
        try {
            //调用业务层的方法进行数据插入
            Boolean update = this.moduleService.update(module);
            //返回页面,请求重定向到列表页
            return R.success("数据修改成功!");
        } catch (Exception exception) {
            //记录日志
            this.loggerService.saveLog(this.getClass().getName(),  //获取当前类名及类数据
                    Thread.currentThread().getStackTrace()[1].getMethodName(),  //获取当前方法名
                    exception, "");
            log.error("后台工程:controller.DeptController出现异常!异常信息为:" + exception);
        }
        //出现异常,返回错误信息
        return R.error("数据修改失败!");
    }

    /**
     * 查询列表的方法
     * @return
     */
207
    @ApiOperation(value = "查询全部模块接口")  //knife4j注解,用于对接口方法进行说明
张骞 已提交
208 209 210 211 212 213 214 215
    @GetMapping("/list")
    public R<List<Module>> list(){
        //01.调用业务层方法
        List<Module> moduleList = this.moduleService.findAll();
        //03.返回数据
        return R.success(moduleList);
    }

216 217 218 219 220 221 222
    /**
     * 获取当前登录用户信息的方法
     * @return 返回当前登录的用户对象
     */
    public Admin getAdmin(){
        return AdminThreadLocal.get();
    }
张骞 已提交
223 224

}