kongdeqiang
2024-04-09 fb050c0dafa5363a73540dd9e52b78487e25ba0a
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
package com.boying.controller;
 
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boying.common.R;
import com.boying.entity.Barrier;
import com.boying.entity.BarrierOpenLog;
import com.boying.entity.OutPark;
import com.boying.service.BarrierOpenLogService;
import com.boying.service.BarrierService;
import com.boying.service.OutParkService;
import com.boying.service.ParkService;
import com.boying.util.RedisJsonUtil;
import lombok.RequiredArgsConstructor;
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 java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import java.util.List;
 
@RestController
@RequestMapping("ffzf/barrier")
@RequiredArgsConstructor
public class BarrierController {
 
    private final BarrierService barrierService;
    private final BarrierOpenLogService barrierOpenLogService;
    private final OutParkService outParkService;
    private final ParkService parkService;
    private StringRedisTemplate redisTemplate;
 
 
    @PostMapping("findPage")
    public Object findPage(Page page, String parkId) {
        QueryWrapper<Barrier> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(Barrier::getParkId,Integer.parseInt(parkId));
        Page page1 = barrierService.page(page, wrapper);
        List<Barrier> records = page1.getRecords();
        for(Barrier barrier:records){
            long between = ChronoUnit.SECONDS.between(barrier.getUpdateTime(), LocalDateTime.now());
            if(between>121){
                barrier.setStatus(1);
            }
        }
        page1.setRecords(records);
        return R.ok(page1);
    }
 
    @PostMapping("save")
    public Object save(Barrier barrier) {
        barrier.setUpdateTime(LocalDateTime.now());
        barrierService.saveOrUpdate(barrier);
        try {
            String jsonValue = JSON.toJSONString(barrier);
            redisTemplate.opsForValue().set("barrier-"+barrier.getCode(), jsonValue);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok("保存成功");
    }
 
    @PostMapping("delete")
    public Object delete(Integer id) {
        Barrier byId = barrierService.getById(id);
        barrierService.removeById(id);
        redisTemplate.delete("barrier-"+byId.getCode());
        return R.ok("删除成功");
    }
 
    @PostMapping("findAll")
    public Object findAll(Long parkId) {
        if (parkId==null) {
            return R.failed("未发现该停车场");
        }
        QueryWrapper<Barrier> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(Barrier::getParkId,parkId);
        return R.ok(barrierService.list(wrapper));
    }
 
    @PostMapping("open")
    public Object open(Long barrierId) {
        Barrier b=  barrierService.getById(barrierId);
        if (b==null) {
            return R.failed("未找到该道闸");
        }
        b.setType2(1);
        b.setUpdateTime(LocalDateTime.now());
        barrierService.saveOrUpdate(b);
        try {
            String jsonValue = JSON.toJSONString(b);
            redisTemplate.opsForValue().set("barrier-"+b.getCode(), jsonValue);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok("请求成功");
    }
 
    @PostMapping("getByCode")
    public Object getByCode(String code) {
        Barrier b=  barrierService.findByCode2(code);
        if (b==null) {
            return R.failed("未找到该道闸");
        }
        b.setParkName(parkService.getById(b.getParkId()).getName());
        return R.ok(b,"请求成功");
    }
 
 
 
    @PostMapping("openBarrier")
    public Object openBarrier(Long barrierId,Integer type,String carNo,String remark) {
        Barrier b=  barrierService.getById(barrierId);
        if (b==null) {
            return R.failed("未找到该道闸");
        }else {
           if(b.getType() == 1){
               System.out.println("手动开入口闸");
               //入口闸,直接开启
               b.setType2(1);
               b.setUpdateTime(LocalDateTime.now());
               barrierService.saveOrUpdate(b);
               try {
                   String jsonValue = JSON.toJSONString(b);
                   redisTemplate.opsForValue().set("barrier-"+b.getCode(), jsonValue);
                   BarrierOpenLog barrierOpenLog = new BarrierOpenLog();
                   barrierOpenLog.setCarNo(carNo);
                   barrierOpenLog.setRemark(remark);
                   barrierOpenLog.setParkId(b.getParkId());
                   barrierOpenLog.setType(b.getType());
                   barrierOpenLogService.save(barrierOpenLog);
               } catch (Exception e) {
                   e.printStackTrace();
               }
               return R.ok("请求成功");
           }else {
               System.out.println("手动开出口闸");
               //出口闸,判断状态
               if(type == null || type == 0){
                   //取消计费
                   OutPark outPark = outParkService.findByCarNoAndBarrierId(b.getCarNo(), b.getId());
                   if(outPark != null){
                       outPark.setPrice(0);
                       outPark.setUpdateTime(LocalDateTime.now());
                       outParkService.saveOrUpdate(outPark);
                   }
                   b.setType2(1);
                   b.setUpdateTime(LocalDateTime.now());
                   barrierService.saveOrUpdate(b);
                   try {
                       String jsonValue = JSON.toJSONString(b);
                       redisTemplate.opsForValue().set("barrier-"+b.getCode(), jsonValue);
                       BarrierOpenLog barrierOpenLog = new BarrierOpenLog();
                       barrierOpenLog.setCarNo(carNo);
                       barrierOpenLog.setRemark(remark);
                       barrierOpenLog.setParkId(b.getParkId());
                       barrierOpenLog.setType(b.getType());
                       barrierOpenLogService.save(barrierOpenLog);
                   } catch (Exception e) {
                       e.printStackTrace();
                   }
                   return R.ok("请求成功");
               }else {
                   //计费
                   b.setType2(1);
                   b.setUpdateTime(LocalDateTime.now());
                   barrierService.saveOrUpdate(b);
                   try {
                       String jsonValue = JSON.toJSONString(b);
                       redisTemplate.opsForValue().set("barrier-"+b.getCode(), jsonValue);
                       BarrierOpenLog barrierOpenLog = new BarrierOpenLog();
                       barrierOpenLog.setCarNo(carNo);
                       barrierOpenLog.setRemark(remark);
                       barrierOpenLog.setParkId(b.getParkId());
                       barrierOpenLog.setType(b.getType());
                       barrierOpenLogService.save(barrierOpenLog);
                   } catch (Exception e) {
                       e.printStackTrace();
                   }
                   return R.ok("请求成功");
               }
           }
        }
    }
}