kongdeqiang
2024-12-04 80cacfd0dcee0174f2a8d9ae322a2fcf857cef63
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package com.boying.controller;
 
 
import com.alibaba.fastjson.JSON;
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.EditParkNumLog;
import com.boying.entity.EnterPark;
import com.boying.entity.Park;
import com.boying.entity.User;
import com.boying.service.EditParkNumLogService;
import com.boying.service.EnterParkService;
import com.boying.service.ParkService;
import com.boying.service.UserService;
import com.boying.util.IpUtil;
import com.boying.util.RedisJsonUtil;
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 javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
 
@RestController
@RequestMapping("ffzf/park")
@RequiredArgsConstructor
public class ParkController {
 
    @Autowired
    private StringRedisTemplate redisTemplate;
    private final ParkService parkService;
    private final UserService userService;
    private final RedisJsonUtil redisJsonUtil;
    private final EnterParkService enterParkService;
    private final EditParkNumLogService editParkNumLogService;
 
    @PostMapping("/findPage")
    public Object findPage(Page page,Park park) {
        Page page1 = parkService.page(page, new QueryWrapper<Park>().lambda().eq(park.getId()!=null,Park::getId,park.getId()).orderByDesc(Park::getId));
        List<Park> records = page1.getRecords();
        for (Park record : records) {
            String num = redisTemplate.opsForValue().get("car_park_" +  record.getId());
            if(num != null){
                record.setCarNum(Integer.parseInt(num));
            }else {
                record.setCarNum(0);
            }
 
        }
        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);
        }
        String jsonValue = JSON.toJSONString(park);
        redisTemplate.opsForValue().set("park-"+park.getId(), jsonValue);
        return R.ok("保存成功");
    }
 
    @PostMapping("/delete")
    public Object delete(Long id) {
        parkService.removeById(id);
        redisTemplate.delete("park-"+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(String parkIds) {
        List<Park> list = new ArrayList<>();
        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());
            try {
                if(StringUtils.isBlank(num)){
                    num = "0";
                }
                byId1.setCarNum(Integer.parseInt(num));
            }catch (Exception e){
                byId1.setCarNum(0);
                e.printStackTrace();
            }
            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);
            if(StringUtils.isBlank(s)){
                redisTemplate.opsForValue().set("car_park_" + parkId,"0",30, TimeUnit.DAYS);
                s = "0";
            }
            byId.setCarNum(Integer.parseInt(s));
            return R.ok(byId,"查询成功");
        }
    }
    @PostMapping("/editParkCarNum")
    public Object editParkCarNum(HttpServletRequest request, Park park) {
        Park byId = parkService.getById(park.getId());
        int num = byId.getNum();
        String old = redisTemplate.opsForValue().get("car_park_" + byId.getId());
        if(old==null){
            old="0";
        }
        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);
            String jsonValue = JSON.toJSONString(park);
            redisTemplate.opsForValue().set("park-"+park.getId(), jsonValue);
 
            String ipAddr = IpUtil.getIpAddr(request);
            int newNum = park.getNum();
            EditParkNumLog numLog = new EditParkNumLog();
            numLog.setIp(ipAddr);
            numLog.setNum(num);
            numLog.setUseNum(Integer.parseInt(old));
            numLog.setNewNum(newNum);
            numLog.setNewUseNum(park.getCarNum());
            numLog.setParkName(byId.getName());
            numLog.setCreateTime(LocalDateTime.now());
            editParkNumLogService.save(numLog);
 
            return R.ok(null,"修改成功");
        }
    }
 
 
 
 
    @PostMapping("/getParkCar")
    public Object getParkCar(Page page, Integer parkId,String carNo) {
        QueryWrapper<EnterPark> enterParkQueryWrapper = new QueryWrapper<>();
        enterParkQueryWrapper.lambda()
                .eq(parkId != null,EnterPark::getParkId,parkId)
                .like(StringUtils.isNotBlank(carNo),EnterPark::getCarNo,carNo)
                .orderByDesc(EnterPark::getCreateTime);
        Page<EnterPark> page1 = enterParkService.page(page, enterParkQueryWrapper);
        for (EnterPark record : page1.getRecords()) {
            record.setParkName(parkService.getById(record.getParkId()).getName());
            if(record.getImgId() != null){
                record.setImgPath("/ffzf/fileinfo/showImgById/"+record.getImgId());
            }
        }
        return R.ok(page1);
    }
 
}