package com.boying.controller; import com.boying.common.BaseController; import com.boying.common.util.StringUtil; import com.boying.entity.Park; import com.boying.entity.User; import com.boying.service.ParkService; import com.boying.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.jpa.domain.Specification; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; @RestController @RequestMapping("park") public class ParkController extends BaseController { @Autowired private StringRedisTemplate redisTemplate; @Autowired private ParkService parkService; @Autowired private UserService userService; @PostMapping("findPage") public Object findPage(int page,int pageSize) { Pageable pageable = PageRequest.of(page-1,pageSize, Sort.Direction.DESC,"id"); Page pages = parkService.findPage(pageable); return success("",pages); } @PostMapping("save") public Object save(Park park) { parkService.save(park); return success("保存成功"); } @PostMapping("delete") public Object delete(Long id) { parkService.delete(id); return success("删除成功"); } @PostMapping("findAll") public Object findAll() { List all = parkService.findAll(); for (Park park : all) { String s = redisTemplate.opsForValue().get("car_park_" + park.getId()); if(StringUtil.isNullOrEmpty(s)){ park.setCarNum(0); }else { park.setCarNum(Integer.parseInt(s)); } } return success("查询成功",all); } @PostMapping("getCarNum") public Object getCarNum(Long parkId) { Park byId = (Park) parkService.findById(parkId); String s = redisTemplate.opsForValue().get("car_park_" + parkId); if(StringUtil.isNullOrEmpty(s)){ byId.setCarNum(0); }else { byId.setCarNum(Integer.parseInt(s)); } return success("查询成功",byId); } @PostMapping("getByUserId") public Object getByUserId(Long userId) { User byId = (User)userService.findById(userId); List list = new ArrayList<>(); if(byId == null){ return error("未查询到用户"); }else { String parkIds = byId.getParkIds(); if(StringUtil.isNullOrEmpty(parkIds)){ return error("该用户未管理停车场"); } List ids= Arrays.stream(parkIds.split(",")).map(s->Long.parseLong(s.trim())).collect(Collectors.toList()); for (Long id : ids) { list.add((Park) parkService.findById(id)); } return success("查询成功",list); } } }