kongdeqiang
2022-09-19 a9862e81851bbe037edc6bb1c7f562c1e55c0d7f
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package com.boying.controller.car;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.boying.common.SystemConfigProperties;
import com.boying.common.util.DateUtil;
import com.boying.common.util.StringUtil;
import com.boying.entity.*;
import com.boying.service.*;
import com.google.gson.JsonIOException;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
 
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;
 
import static com.boying.common.util.DateUtil.getMinute;
 
/**
 * Servlet implementation class PlateServlet
 */
@Controller
@RequestMapping("/car")
public class PlateServlet2 extends HttpServlet {
    private static final long serialVersionUID = 1L;
    @Autowired
    private SystemConfigProperties systemConfigProperties;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ParkService parkService;
 
    /**
     * 回复开闸
     */
    protected void open(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/json");
        PrintWriter out = response.getWriter();
        out.println("{\"Response_AlarmInfoPlate\":{\"info\":\"ok\",\"content\":\"...\",\"is_pay\":\"true\"}}");
        out.flush();
        out.close();
    }
 
    //转码
    public static String deCode(String str) {
        try {
            byte[] b = str.getBytes("UTF-8");//����
            String sa = new String(b);//����:��ʲô�ַ����������ʲô�ַ�������
            //String sa = new String(str.getBytes());
 
            return sa;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
 
    protected void noOpen(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/json");
        PrintWriter out = response.getWriter();
        out.println("{\"Response_AlarmInfoPlate\":{\"info\":\"error\",\"content\":\"...\",\"is_pay\":\"false\"}}");
        out.flush();
        out.close();
    }
 
    @PostMapping("/status")
    public void heart(String serialno,HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
 
        Barrier barrier = barrierService.findByCode(serialno);
 
        if(barrier!=null){
            if(barrier.getType2()==1){
                open(request,response);
                barrier.setType2(0);
                barrierService.save(barrier);
                return;
            }
 
            barrier.setStatus(0);
            barrier.setCreateTime(new Date());
            barrierService.save(barrier);
 
            OutPark outPark = outParkService.count4(barrier.getId());
            if(outPark!=null){
                open(request,response);
                outPark.setStatus2(1);
                outParkService.save(outPark);
            }else{
                noOpen(request,response);
            }
        }
    }
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    @PostMapping("/PlateServlet")
    protected void info(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String s= "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
        BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream(),"UTF-8"));
        String str = "";
        String lineStr = "";
        while((str = reader.readLine()) != null){
            lineStr += str;
        }
        System.out.println(lineStr);
 
        JSONObject jsonObject1 = JSON.parseObject(lineStr);
        JSONObject alarmInfoPlate = jsonObject1.getJSONObject("AlarmInfoPlate");
        JSONObject result = alarmInfoPlate.getJSONObject("result");
        JSONObject plateResult = result.getJSONObject("PlateResult");
        String carNo = plateResult.get("license").toString();
        String code = alarmInfoPlate.get("serialno").toString();
        System.out.println(carNo+"===="+code);
 
        try {
            JsonParser parser=new JsonParser();
            do {
                JsonObject jsonObject=(JsonObject) parser.parse(lineStr);
                if( jsonObject == null || jsonObject.isJsonNull() ) {
                    break;
                }
 
                JsonObject jsonInfoPlate = jsonObject.get("AlarmInfoPlate").getAsJsonObject();
                if( jsonInfoPlate == null || jsonInfoPlate.isJsonNull() ) {
                    break;
                }
 
                JsonObject jsonResult = jsonInfoPlate.get("result").getAsJsonObject();
                if( jsonResult == null || jsonResult.isJsonNull() ) {
                    break;
                }
 
                JsonObject jsonPlateResult = jsonResult.get("PlateResult").getAsJsonObject();
                if( jsonPlateResult == null || jsonPlateResult.isJsonNull() ) {
                    break;
                }
 
                String license = jsonPlateResult.get("license").getAsString();//车牌号
                if( license == null || license == "") {
                    break;
                }
 
                String serialno = jsonInfoPlate.get("serialno").getAsString();//摄像机序列号
                if( serialno == null || serialno == "" ) {
                    break;
                }
 
                System.out.println("编号:"+serialno+"设备识别到了:"+license);
                String format = sdf.format(new Date());
                s+= format+",车牌号为:"+license+",serialno为:"+code+"。  ";
 
                Barrier barrier = barrierService.findByCode(code);
                if(barrier==null){
                    s+="查询道闸为空\n";
                }else{
                    if(barrier.getType()==0){
                        outPark(license, barrier.getId(), barrier.getParkId());
                        s+=license+"-保存出场记录\n";
                    }else{
                        open(request, response);//抬杆
                        enterPark(license,barrier.getId(),barrier.getParkId());
                        s+=license+"-保存入场记录\n";
                    }
                }
            writeTxt(s);
            }while(false);
        }
 
        catch (JsonIOException e) {
            System.out.println("无入场记录");
            s+=" ,查询场内记录异常";
            e.printStackTrace();
            writeTxt(s);
        }
        catch (JsonSyntaxException e) {
            System.out.println("无入场记录");
            s+=" ,查询场内记录异常";
            e.printStackTrace();
            writeTxt(s);
        }
        catch (Exception e) {
            System.out.println("无入场记录");
            s+=" ,查询场内记录异常";
            writeTxt(s);
        }
    }
 
 
    private void writeTxt( String txt)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        try
        {
            FileWriter f = new FileWriter(systemConfigProperties.getLogPath()+sdf.format(new Date())+".txt",true);
            BufferedWriter bw=new BufferedWriter(f);
            bw.write(txt);
            bw.newLine();
            bw.close();
        }
        catch(Exception e)
        {
            System.out.println("打印错误");
        }
    }
 
 
    @Autowired
    private OutParkService outParkService;
    @Autowired
    private EnterParkService enterParkService;
    @Autowired
    private BarrierService barrierService;
    @Autowired
    private TicketService ticketService;
    @Autowired
    private CostRuleService costRuleService;
 
    public void enterPark(String carNo,Long barrierId,Long parkId) {
        Park park = (Park)parkService.findById(parkId);
        int num = 0;
        String s = redisTemplate.opsForValue().get("car_park_" + parkId);
        if(park != null){
            num = park.getNum();
            if(s !=null){
                if(Integer.parseInt(s) > num){
                    return;
                }
            }else {
                s= "0";
                redisTemplate.opsForValue().set("car_park_" + parkId,s,30, TimeUnit.DAYS);
            }
        }
        enterParkService.deleteByCarNo(carNo,parkId);
        EnterPark enterPark = new EnterPark();
        enterPark.setCreateTime(new Date());
        enterPark.setCarNo(carNo);
        enterPark.setBarrierId(barrierId);
        enterPark.setParkId(parkId);
 
        Specification<Ticket> specification2 = new Specification<Ticket>() {
            @Override
            public Predicate toPredicate(Root<Ticket> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                list.add(cb.equal(root.get("carNo").as(String.class), enterPark.getCarNo()));
                list.add(cb.notEqual(root.get("payStatus"), 1));
                Predicate[] arr = new Predicate[list.size()];
                cq.where(list.toArray(arr));
                return null;
            }
        };
        List<Ticket> tickets = ticketService.findAll(specification2);
        if(tickets.size()>0){
            enterPark.setStatus(1);//发现有违章
        }
        enterParkService.save(enterPark);
        int i = Integer.parseInt(s);
        i++;
        redisTemplate.opsForValue().set("car_park_" + parkId,Integer.toString(i),30, TimeUnit.DAYS);
 
        Barrier barrier = (Barrier) barrierService.findById(barrierId);
        barrier.setType2(1);
        barrierService.save(barrier);
    }
 
    public double outPark(String carNo,Long barrierId,Long parkId) {
        OutPark outPark = new OutPark();
        outPark.setCarNo(carNo);
        outPark.setParkId(parkId);
        outPark.setBarrierId(barrierId);
        outPark.setCreateTime(new Date());
        outPark.setCode(System.currentTimeMillis()+"");
        EnterPark enterPark = enterParkService.findByCarNo(carNo).get(0);
 
        if(enterPark==null){
            String s1 = "未发现入场车辆:"+carNo;
        }else{
            outPark.setEnterTime(enterPark.getCreateTime());
        }
        String redis = redisTemplate.opsForValue().get("car_park_" + parkId);
        long l = outPark.getCreateTime().getTime() - enterPark.getCreateTime().getTime();
        outPark.setTime(l/(1000*60));
        double money = 0;
        try {
            money = costRuleService.getMoney(parkId, enterPark.getCreateTime(), outPark.getCreateTime(), 1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        outPark.setPrice(Double.valueOf(String.format("%.1f", money)));
 
        outPark.setStatus3(findTicket(carNo));
        outParkService.save(outPark);
        int i = Integer.parseInt(redis);
        i--;
        if(i<0){
            redisTemplate.opsForValue().set("car_park_" + parkId,"0",30, TimeUnit.DAYS);
        }else {
            redisTemplate.opsForValue().set("car_park_" + parkId,Integer.toString(i),30, TimeUnit.DAYS);
        }
 
        Barrier barrier = (Barrier) barrierService.findById(barrierId);
        barrier.setCarNo(carNo);
        if(outPark.getPrice()==0&&outPark.getStatus3()==0){
            barrier.setType2(1);
        }
        barrierService.save(barrier);
        return money;
    }
 
    public int findTicket(String carNo){
        Specification<Ticket> specification2 = new Specification<Ticket>() {
            @Override
            public Predicate toPredicate(Root<Ticket> root, CriteriaQuery<?> cq, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                list.add(cb.equal(root.get("carNo").as(String.class), carNo));
                list.add(cb.notEqual(root.get("payStatus"), 1));
                Predicate[] arr = new Predicate[list.size()];
                cq.where(list.toArray(arr));
                return null;
            }
        };
        List<Ticket> tickets = ticketService.findAll(specification2);
        if(tickets.size()>0){
            return 1;
        }else{
            return 0;
        }
    }
}