package com.boying.controller;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.boying.common.R;
|
import com.boying.entity.Park;
|
import com.boying.entity.User;
|
import com.boying.service.ParkService;
|
import com.boying.service.UserService;
|
import lombok.RequiredArgsConstructor;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RestController;
|
|
import java.util.ArrayList;
|
import java.util.Arrays;
|
import java.util.List;
|
import java.util.concurrent.TimeUnit;
|
import java.util.stream.Collectors;
|
|
@RestController
|
@RequestMapping("/park")
|
@RequiredArgsConstructor
|
public class ParkController {
|
|
@Autowired
|
private StringRedisTemplate redisTemplate;
|
private final ParkService parkService;
|
private final UserService userService;
|
|
@PostMapping("/findPage")
|
public Object findPage(Page page) {
|
Page page1 = parkService.page(page, new QueryWrapper<Park>().lambda().orderByDesc(Park::getId));
|
List<Park> records = page1.getRecords();
|
for (Park record : records) {
|
String num = redisTemplate.opsForValue().get("car_park_" + record.getId());
|
record.setCarNum(Integer.parseInt(num));
|
}
|
page1.setRecords(records);
|
return R.ok(page1);
|
}
|
|
@PostMapping("/save")
|
public Object save(Park park) {
|
parkService.saveOrUpdate(park);
|
String num = redisTemplate.opsForValue().get("car_park_" + park.getId());
|
if(StringUtils.isBlank(num)){
|
redisTemplate.opsForValue().set("car_park_" + park.getId(),"0",30, TimeUnit.DAYS);
|
redisTemplate.opsForValue().set("park_up_" + park.getId(),"true",30, TimeUnit.DAYS);
|
}
|
return R.ok("保存成功");
|
}
|
|
@PostMapping("/delete")
|
public Object delete(Long id) {
|
parkService.removeById(id);
|
return R.ok("删除成功");
|
}
|
|
@PostMapping("findAll")
|
public Object findAll() {
|
List<Park> all = parkService.list();
|
for (Park park : all) {
|
String s = redisTemplate.opsForValue().get("car_park_" + park.getId());
|
if(StringUtils.isBlank(s)){
|
park.setCarNum(0);
|
}else {
|
park.setCarNum(Integer.parseInt(s));
|
}
|
}
|
return R.ok(all);
|
}
|
|
@PostMapping("/getCarNum")
|
public Object getCarNum(Long parkId) {
|
Park byId = (Park) parkService.getById(parkId);
|
String s = redisTemplate.opsForValue().get("car_park_" + parkId);
|
if(StringUtils.isBlank(s)){
|
byId.setCarNum(0);
|
}else {
|
byId.setCarNum(Integer.parseInt(s));
|
}
|
return R.ok(byId);
|
}
|
|
@PostMapping("/getByUserId")
|
public Object getByUserId(Long userId) {
|
User byId = userService.getById(userId);
|
List<Park> list = new ArrayList<>();
|
if(byId == null){
|
return R.failed("未查询到用户");
|
}else {
|
String parkIds = byId.getParkIds();
|
if(StringUtils.isBlank(parkIds)){
|
return R.failed("该用户未管理停车场");
|
}
|
List<Long> ids= Arrays.stream(parkIds.split(",")).map(s->Long.parseLong(s.trim())).collect(Collectors.toList());
|
for (Long id : ids) {
|
Park byId1 = parkService.getById(id);
|
String num = redisTemplate.opsForValue().get("car_park_" + byId1.getId());
|
byId1.setCarNum(Integer.parseInt(num));
|
list.add(byId1);
|
}
|
return R.ok(list);
|
}
|
}
|
@PostMapping("/getById")
|
public Object getById(Long parkId) {
|
Park byId = parkService.getById(parkId);
|
if(byId == null){
|
return R.failed("未查询到该停车场");
|
}else {
|
String s = redisTemplate.opsForValue().get("car_park_" + parkId);
|
byId.setCarNum(Integer.parseInt(s));
|
return R.ok(byId,"查询成功");
|
}
|
}
|
@PostMapping("/editParkCarNum")
|
public Object editParkCarNum(Park park) {
|
Park byId = parkService.getById(park.getId());
|
if(byId == null){
|
return R.failed("未查询到该停车场");
|
}else {
|
byId.setNum(park.getNum());
|
redisTemplate.opsForValue().set("car_park_" + byId.getId(),park.getCarNum()+"",30, TimeUnit.DAYS);
|
redisTemplate.opsForValue().set("park_up_" + byId.getId(),"true",30, TimeUnit.DAYS);
|
parkService.updateById(byId);
|
return R.ok(null,"修改成功");
|
}
|
}
|
|
}
|