CampusController.java 8.1 KB
Newer Older
张骞 已提交
1 2 3 4 5 6 7 8 9
package com.template.back.server.controller.school;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.template.back.common.pojo.school.Campus;
import com.template.back.common.pojo.system.Admin;
import com.template.back.common.service.LoggerService;
import com.template.back.common.utils.AdminThreadLocal;
10
import com.template.back.common.vo.R;
张骞 已提交
11
import com.template.back.server.service.school.CampusService;
12 13 14 15
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
张骞 已提交
16 17
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
18 19
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
张骞 已提交
20 21
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
22
import org.springframework.web.bind.annotation.*;
张骞 已提交
23 24
import org.springframework.web.servlet.ModelAndView;

25
import java.util.List;
张骞 已提交
26 27 28 29 30 31 32
import java.util.Map;

/**
 * @author 张骞
 * @version 1.0.0
 * 加盟校控制层代码,本框架单表增删改查标准代码
 */
33 34 35
@Api(tags = "校区管理相关接口")
@RestController  //标记为restful风格的控制层
@RequestMapping("/school/campus/")  //注解请求映射路径
张骞 已提交
36 37 38 39 40 41 42 43 44 45
@Slf4j  //注解日志
public class CampusController {
    //注入自定义日志业务层
    @Autowired
    private LoggerService loggerService;

    //注入业务层接口
    @Autowired
    private CampusService campusService;

46

张骞 已提交
47
    /**
48
     * 查询部门列表
张骞 已提交
49
     *
50 51
     * @param page     当前页码
     * @param pageSize 每页的数据长度
张骞 已提交
52 53
     * @return
     */
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    @ApiOperation(value = "查看加盟校列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "当前页码",required = true,defaultValue = "1"),
            @ApiImplicitParam(name = "pageSize",value = "每页数据长度",required = true,defaultValue = "10"),
            @ApiImplicitParam(name = "campusName",value = "根据校区名称查询",required = false),
            @ApiImplicitParam(name = "address",value = "根据校区地址查询",required = false),
            @ApiImplicitParam(name = "startData",value = "根据开始时间查询,和结束时间共用",required = false),
            @ApiImplicitParam(name = "endData",value = "根据结束时间查询,和开始时间共用",required = false),
    })  //knife4j注解,用于对接口参数进行说明
    @Cacheable(value = "campusCache",key = "#page + '_' + #pageSize+ '_' +#campusName + '_' + #address+ '_' + #startData + '_' + #endData")  //spring cache写入缓存
    @GetMapping("page")
    public R<Page> list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                        @RequestParam(value = "campusName", required = false) String campusName,
                        @RequestParam(value = "address", required = false) String address,
                        @RequestParam(value = "startData", required = false)String startData,
                        @RequestParam(value = "endData", required = false)String endData) {
张骞 已提交
71 72
        try {
            //调用业务层方法
73
            Page all = this.campusService.findPage(page, pageSize, campusName, address,startData,endData);
张骞 已提交
74
            //向前端存入数据
75
            return R.success(all);
张骞 已提交
76 77
        } catch (Exception exception) {
            //记录日志
78
            this.loggerService.saveLog(this.getClass().getName(),  //获取当前类名及类数据
张骞 已提交
79
                    Thread.currentThread().getStackTrace()[1].getMethodName(),  //获取当前方法名
80 81
                    exception, "查询加盟校列表");
            log.error("后台工程:controller.CampusController出现异常!异常信息为:" + exception);
张骞 已提交
82 83
        }
        //出现异常,返回错误信息
84
        return R.error("查询数据列表异常!");
张骞 已提交
85 86 87
    }

    /**
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
     * 新增数据的方法
     * @param campus
     * @return
     */
    @ApiOperation(value = "新增加盟校数据")
    @ApiImplicitParams({@ApiImplicitParam(name = "campus",value = "新增的加盟校数据",required = true)})  //knife4j注解,用于对接口参数进行说明
    @CacheEvict(value = "campusCache",allEntries = true)  //新增数据后,删除对应的缓存数据
    @PostMapping("save")
    public R<String> addSave(@RequestBody Campus campus){
        //调用业务层的方法进行保存
        Boolean insert = this.campusService.save(campus);
        if(insert){
            return R.success("数据保存成功!");
        }else {
            return R.error("数据保存失败!");
        }

    }

    /**
     * 根据id查询单条数据,用于数据回显
张骞 已提交
109
     *
110
     * @param id
张骞 已提交
111 112
     * @return
     */
113 114 115 116 117 118 119 120 121 122 123 124
    @ApiOperation(value = "根据id查询单条数据")
    @ApiImplicitParams({@ApiImplicitParam(name = "id",value = "选中的校区id",required = true)})  //knife4j注解,用于对接口参数进行说明
    @PostMapping("findById")
    public R<Campus> findById(@RequestParam(value = "id") String id) {
        //如果传入的数据不为空,则开始查询
        if (ObjectUtil.isNotEmpty(id)) {
            Campus campus = this.campusService.findOne(id);
            if (ObjectUtil.isNotEmpty(campus)) {
                return R.success(campus);
            }
        }
        return R.error("未查询到要修改的数据!");
张骞 已提交
125 126 127
    }

    /**
128 129
     * 修数据的方法
     * @return
张骞 已提交
130
     */
131 132 133 134 135
    @ApiOperation(value = "修改加盟校数据")
    @ApiImplicitParams({@ApiImplicitParam(name = "campus",value = "修改的加盟校数据",required = true)})  //knife4j注解,用于对接口参数进行说明
    @CacheEvict(value = "campusCache",allEntries = true)
    @PutMapping("edit")
    public R<String> edit(@RequestBody Campus campus){
张骞 已提交
136 137
        try {
            //调用业务层的方法进行数据插入
138 139
            Boolean update = this.campusService.update(campus);
            return R.success("数据修改成功!");
张骞 已提交
140 141
        } catch (Exception exception) {
            //记录日志
142
            this.loggerService.saveLog(this.getClass().getName(),  //获取当前类名及类数据
张骞 已提交
143
                    Thread.currentThread().getStackTrace()[1].getMethodName(),  //获取当前方法名
144 145
                    exception, "");
            log.error("后台工程:controller.CampusController出现异常!异常信息为:" + exception);
张骞 已提交
146 147
        }
        //出现异常,返回错误信息
148
        return R.error("数据修改失败!");
张骞 已提交
149 150 151 152
    }

    /**
     * 根据id删除数据的方法
153
     * @param ids
张骞 已提交
154 155
     * @return
     */
156 157 158 159 160
    @ApiOperation(value = "根据id删除数据的方法")
    @ApiImplicitParams({@ApiImplicitParam(name = "ids",value = "选中的加盟校id集合",required = true)})  //knife4j注解,用于对接口参数进行说明
    @CacheEvict(value = "campusCache",allEntries = true)
    @PostMapping("delete")
    public R<String> delete(@RequestParam("ids") List<String> ids) {
张骞 已提交
161
        try {
162

张骞 已提交
163
            //调用业务层的删除方法
164 165
            Boolean delete = this.campusService.removeByIds(ids);

张骞 已提交
166 167 168
            //判断业务层返回的状态
            if(!delete){
                //写入错误数据
169
                return R.error("数据删除失败!");
张骞 已提交
170
            }
171
            return R.success("数据删除成功!");
张骞 已提交
172 173
        } catch (Exception exception) {
            //记录日志
174
            this.loggerService.saveLog(this.getClass().getName(),  //获取当前类名及类数据
张骞 已提交
175
                    Thread.currentThread().getStackTrace()[1].getMethodName(),  //获取当前方法名
176 177
                    exception, "");
            log.error("后台工程:controller.CampusController出现异常!异常信息为:" + exception);
张骞 已提交
178 179
        }
        //出现异常,返回错误信息
180
        return R.error("数据删除失败!");
张骞 已提交
181 182 183 184
    }

    /**
     * 获取当前登录用户信息的方法
185
     *
张骞 已提交
186 187
     * @return 返回当前登录的用户对象
     */
188
    public Admin getAdmin() {
张骞 已提交
189 190 191
        return AdminThreadLocal.get();
    }
}
192