1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 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
| package com.jcDemo.controller;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import com.jcDemo.commom.CommonException; import com.jcDemo.entity.entities.Device; import com.jcDemo.entity.res.PageResult; import com.jcDemo.entity.res.Result; import com.jcDemo.entity.res.ResultCode; import com.jcDemo.service.device.DeviceService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiOperation; import lombok.extern.slf4j.Slf4j; import lombok.var; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import java.text.ParseException; import java.util.List; import java.util.concurrent.TimeUnit;
@Slf4j @Api(tags = "设备管理") @RestController @RequestMapping("/api/device") public class DeviceController {
@Autowired DeviceService deviceService;
@Resource RedisTemplate redisTemplate;
@ApiOperation("查找所有设备") @GetMapping("/getDevices") public Result getDevices(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10") int pageSize) { PageResult pr = null; try { Page page = PageHelper.startPage(pageNum, pageSize); List<Device> devices = deviceService.getDevices();
pr = new PageResult(page.getTotal(), devices); log.info("pageNum:{},pageSize:{}", pageNum, pageSize); } catch (CommonException e) { e.printStackTrace(); return new Result(ResultCode.EMPTY); } return new Result(ResultCode.SUCCESS, pr); }
@ApiOperation("查找设备ByNo") @GetMapping("/getDeviceByNo/{no}") public Result getDeviceByNo(@PathVariable("no") String no) throws CommonException { Device device = null; try { if (redisTemplate.hasKey("devices:" + no)) { log.info("重置时间->devices:{}", no); redisTemplate.expire("devices:" + no, 300, TimeUnit.SECONDS); log.info("从redis取出来的devices:{}", no); device = JSON.parseObject(String.valueOf(redisTemplate.opsForValue().get("devices:" + no)), Device.class); } else { log.info("从mysql取出来的{}", no); device = deviceService.getDeviceByNo(no); redisTemplate.opsForValue().set("devices:" + no, JSON.toJSONString(device), 300, TimeUnit.SECONDS); } return new Result(ResultCode.SUCCESS, device); } catch (CommonException e) { e.printStackTrace(); return new Result(ResultCode.EMPTY); } }
@ApiOperation("头部查找") @GetMapping("/searchDevice") public Result searchDevice(@RequestBody Device device) { List<Device> devices; try { devices = deviceService.searchDevice(device); return new Result(ResultCode.SUCCESS, devices); } catch (CommonException e) { e.printStackTrace(); return new Result(ResultCode.EMPTY); } }
@ApiOperation("更新设备") @PostMapping("/updateDevice") public Result updateDevice(@RequestBody Device device) throws ParseException { if (deviceService.updateDevice(device) == 1) { if (redisTemplate.hasKey("devices:" + device.getDeviceNo())) { Boolean delete = redisTemplate.delete("devices:" + device.getDeviceNo()); if (delete) { redisTemplate.opsForValue().set("devices:" + device.getDeviceNo(), JSON.toJSONString(device), 300, TimeUnit.SECONDS); } else { log.warn("缓存更新失败,请检查!"); } } else { redisTemplate.opsForValue().set("devices:" + device.getDeviceNo(), JSON.toJSONString(device), 300, TimeUnit.SECONDS); } return new Result(ResultCode.SUCCESS); } else { return new Result(ResultCode.ERROR); } }
@ApiOperation("删除设备") @DeleteMapping("/delete/{no}") public Result deleteDeviceById(@PathVariable("no") String no) { if (deviceService.deleteDeviceByNo(no) == 1) { if (redisTemplate.delete("devices:" + no)) { log.warn("缓存删除成功"); } else { log.warn("缓存删除失败,请检查!"); } return new Result(ResultCode.SUCCESS); } else { return new Result(ResultCode.ERROR); } }
@ApiOperation("新增设备") @PostMapping("/insert") public Result insertDevice(@RequestBody Device device) throws ParseException { if (deviceService.insertDevice(device) == 1) { redisTemplate.opsForValue().set("devices:" + device.getDeviceNo(), JSON.toJSONString(device), 300, TimeUnit.SECONDS); return new Result(ResultCode.SUCCESS); } else { return new Result(ResultCode.ERROR); } } }
|