kongdeqiang
2022-09-18 3343bf1c7b1b9369ff4e56b7929c0eeac4b9ab63
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
package com.boying.controller.car;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.boying.common.util.DateUtil;
import com.boying.common.util.StringUtil;
import com.boying.entity.Barrier;
import com.boying.entity.EnterPark;
import com.boying.entity.OutPark;
import com.boying.entity.Ticket;
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.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.util.*;
 
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;
 
    /**
     * 回复开闸
     */
    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 {
 
        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( license == null || license == "" ) {
                    break;
                }
 
                System.out.println("编号:"+serialno+"设备识别到了:"+license);
 
                Barrier barrier = barrierService.findByCode(code);
                if(barrier==null){
 
                }else{
                    if(barrier.getType()==0){
                        outPark(license, barrier.getId(), barrier.getParkId());
                    }else{
                        open(request, response);//抬杆
                        enterPark(license,barrier.getId(),barrier.getParkId());
                    }
                }
 
            }while(false);
        }
        catch (JsonIOException e) {
            e.printStackTrace();
        }
        catch (JsonSyntaxException e) {
            e.printStackTrace();
        }
        catch (Exception e) {
 
        }
    }
 
    protected void WriteTxt( String path, String txt)
    {
        try
        {
            FileWriter  f = new FileWriter(path);
            BufferedWriter bw=new BufferedWriter(f);
            bw.write(txt);
            bw.close();
        }
        catch(Exception e)
        {
        }
    }
 
    @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) {
 
        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);
 
        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);
 
        if(enterPark==null){
 
        }else{
            outPark.setEnterTime(enterPark.getCreateTime());
        }
        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);
 
        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;
        }
    }
}