kongdeqiang
2022-09-26 18087f533cd48ddb9f972c0848ffe920ec58ee8e
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
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<Park> 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<Park> 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<Park> list = new ArrayList<>();
        if(byId == null){
            return error("未查询到用户");
        }else {
            String parkIds = byId.getParkIds();
            if(StringUtil.isNullOrEmpty(parkIds)){
                return error("该用户未管理停车场");
            }
            List<Long> 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);
        }
    }
 
}