kongdeqiang
2023-11-15 6a86b993e89ce5d90c8707f2855aa1fe183d4b73
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package com.boying.controller;
 
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.boying.common.R;
import com.boying.common.SystemConfigProperties;
import com.boying.entity.*;
import com.boying.service.*;
import com.boying.util.DateUtilOther;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.data.jpa.domain.Specification;
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.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.TimeUnit;
 
@RestController
@RequestMapping("ffzf/outpark")
@RequiredArgsConstructor
public class OutParkController  {
 
 
    private final OutParkService outParkService;
    private final EnterParkService enterParkService;
    private final BarrierService barrierService;
    private final TicketService ticketService;
    private final SystemConfigProperties systemConfigProperties;
    private final UserService userService;
 
    @Autowired
    private CostRuleService costRuleService;
    @Autowired
    private ParkService parkService;
    @Autowired
    private StringRedisTemplate redisTemplate;
 
    @PostMapping("/findPage")
    public Object findPage(Page page, String  carNo,Long parkId,String  payCode,String date) throws ParseException {
        List<OutPark> list = outParkService.getList(page.getCurrent(), page.getSize(), carNo,parkId,payCode, date);
        System.out.println(list);
        long count = outParkService.getCount(carNo,parkId,payCode, date);
        for (OutPark record : list) {
            record.setParkName(parkService.getById(record.getParkId()).getName());
            if(record.getEnterTime() != null){
                record.setTimeStr(DateUtilOther.millisToDayHrMinSec(new Double(record.getTime()).longValue()));
            }else {
                record.setTimeStr("未发现入场记录");
            }
            if(record.getImgId() != null){
                record.setImgPath("/ffzf/fileinfo/showImgById/"+record.getImgId());
            }
        }
        page.setRecords(list);
        page.setTotal(count);
        return R.ok(page);
    }
    //getById
    @PostMapping("/getById")
    public Object getById(Long id) {
        return R.ok(outParkService.getById(id));
    }
    //道闸code
    @PostMapping("/findByBarrierCode")
    public Object findById(String code) {
       QueryWrapper<Barrier> wrapper = new QueryWrapper<>();
       wrapper.lambda()
               .eq(Barrier::getCode2,code);
        System.out.println("code   :" +code);
        List<Barrier> all = barrierService.list(wrapper);
        if(all.size()==0){
            return R.failed("未找到该设备");
        }else{
            Barrier barrier = all.get(0);
            String carNo = barrier.getCarNo();
 
            OutPark outPark = outParkService.findByCarNoAndBarrierId(carNo,barrier.getId());
            if(outPark==null){
                return R.failed("未识别到车牌号");
            }else{
                outPark.setParkName(barrier.getName());
                if(outPark.getTime() > 0){
                    outPark.setTimeStr(DateUtilOther.millisToDayHrMinSec(new Double(outPark.getTime()).longValue()));
                }else {
                    outPark.setTimeStr("不足一分钟");
                }
                return R.ok(outPark);
            }
        }
    }
 
    //道闸code
    @PostMapping("/findByBarrierCode2")
    public Object findById2(String code2) {
        System.out.println("code2     :"+code2);
        QueryWrapper<Barrier> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(Barrier::getCode2,code2);
        List<Barrier> all = barrierService.list(wrapper);
        if(all.size()==0){
            return R.failed("未找到该设备");
        }else{
            Barrier barrier = all.get(0);
            String carNo = barrier.getCarNo();
 
            OutPark outPark = outParkService.findByCarNoAndBarrierId(carNo,barrier.getId());
            if(outPark==null){
                return R.failed("未识别到车牌号");
            }else{
                outPark.setParkName(barrier.getName());
                if(outPark.getTime() > 0){
                    outPark.setTimeStr(DateUtilOther.millisToDayHrMinSec(new Double(outPark.getTime()).longValue()));
                }else {
                    outPark.setTimeStr("不足一分钟");
                }
                return R.ok(outPark);
            }
        }
    }
 
    @PostMapping("/delete")
    public Object delete(Long id) {
        outParkService.removeById(id);
        return R.ok("删除成功");
    }
 
    @PostMapping("enterPark2")
    public Object enterPark(String carNo,Integer barrierId,Integer parkId,String code2) {
        Barrier barrier1 = findBarrier(code2);
        barrierId = barrier1.getId();
        parkId = barrier1.getParkId();
        Park park = parkService.getById(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){
                    redisTemplate.opsForValue().set("park_up_" + parkId,"false",30, TimeUnit.DAYS);
                    return "false";
                }
            }else {
                s= "0";
                redisTemplate.opsForValue().set("car_park_" + parkId,s,30, TimeUnit.DAYS);
                redisTemplate.opsForValue().set("park_up_" + parkId,"true",30, TimeUnit.DAYS);
            }
        }
        enterParkService.deleteByCarNo(carNo,parkId);
        EnterPark enterPark = new EnterPark();
        enterPark.setCreateTime(LocalDateTime.now());
        enterPark.setCarNo(carNo);
        enterPark.setBarrierId(barrierId);
        enterPark.setParkId(parkId);
        enterParkService.saveOrUpdate(enterPark);
        int i = Integer.parseInt(s);
        i++;
        redisTemplate.opsForValue().set("car_park_" + parkId,Integer.toString(i),30, TimeUnit.DAYS);
        redisTemplate.opsForValue().set("park_up_" + parkId,"true",30, TimeUnit.DAYS);
 
        Barrier barrier =barrierService.getById(barrierId);
        barrier.setType2(1);
        barrier.setUpdateTime(LocalDateTime.now());
        barrierService.saveOrUpdate(barrier);
        return R.ok("请求成功");
    }
 
    @PostMapping("outPark2")
    public Object outPark(String carNo,Integer barrierId,Integer parkId,String code2) {
        String s = "开始执行出场接口------>\n";
        Barrier barrier1 = findBarrier(code2);
        barrierId = barrier1.getId();
        parkId = barrier1.getParkId();
        OutPark outPark = new OutPark();
        outPark.setCarNo(carNo);
        outPark.setParkId(parkId);
        outPark.setBarrierId(barrierId);
        outPark.setCreateTime(LocalDateTime.now());
        outPark.setCode(System.currentTimeMillis()+"");
        EnterPark enterPark = null;
        List<EnterPark> byCarNo = enterParkService.findByCarNo(carNo,parkId);
        if(byCarNo.size() > 0){
           enterPark =  byCarNo.get(0);
        }
        if(enterPark==null){
           s += "未发现入场车辆:"+carNo+"\n";
            writeTxt(s);
            return R.failed("无进场记录或手机号进出输入不一致",null);
        }else{
            s += "发现入场车辆: "+enterPark.getCarNo()+",道闸id为:"+enterPark.getBarrierId()+",停车场id:"+enterPark.getParkId()+",违章标识:"+enterPark.getStatus()+"\n";
            outPark.setEnterTime(enterPark.getCreateTime());
        }
        String redis = redisTemplate.opsForValue().get("car_park_" + parkId);
        long l = outPark.getCreateTime().toInstant(ZoneOffset.of("+8")).toEpochMilli() - enterPark.getCreateTime().toInstant(ZoneOffset.of("+8")).toEpochMilli();;
        s+= "场内时长为:"+l+"毫秒,合计为: "+l/(1000*60)+"分\n";
        outPark.setTime(l/(1000*60));
        double money = 0;
        try {
            money = costRuleService.getMoney(parkId, enterPark.getCreateTime(), outPark.getCreateTime(), 1);
            s+="金额为:"+money+"\n";
        } catch (ParseException e) {
            e.printStackTrace();
        }
        outPark.setPrice(Double.valueOf(String.format("%.1f", money)));
 
        //outPark.setStatus3(findTicket(carNo));
        outPark.setUpdateTime(LocalDateTime.now());
        outParkService.saveOrUpdate(outPark);
        int i = Integer.parseInt(redis);
        i--;
        if(i<0){
            redisTemplate.opsForValue().set("car_park_" + parkId,"0",30, TimeUnit.DAYS);
            redisTemplate.opsForValue().set("park_up_" + parkId,"true",30, TimeUnit.DAYS);
        }else {
            redisTemplate.opsForValue().set("car_park_" + parkId,Integer.toString(i),30, TimeUnit.DAYS);
            redisTemplate.opsForValue().set("park_up_" + parkId,"true",30, TimeUnit.DAYS);
        }
 
        Barrier barrier = barrierService.getById(barrierId);
        barrier.setCarNo(carNo);
        if(outPark.getPrice()==0&&outPark.getStatus3()==0){
            barrier.setType2(1);
        }else {
            barrier.setType2(0);
        }
        barrier.setUpdateTime(LocalDateTime.now());
        barrierService.saveOrUpdate(barrier);
        s += "\n";
        writeTxt(s);
        return R.ok(outPark);
    }
 
    @GetMapping("/statisticParkOrder/count")
    public R statisticParkOrder(String parkIds) {
        Map<String,Object> resultMap = new HashMap<>();
        if(StringUtils.isNotBlank(parkIds)){
            String[] split = parkIds.split(",");
            long[] array = Arrays.stream(split).mapToLong(Long::parseLong).toArray();
            List<Long> parkIdList = new ArrayList<>();
            for (long i : array) {
                parkIdList.add(i);
            }
            QueryWrapper<OutPark> wrapper = new QueryWrapper<>();
            wrapper.lambda()
                    .between(OutPark::getCreateTime, DateUtil.beginOfDay(new Date()),DateUtil.endOfDay(new Date()))
                    .eq(OutPark::getStatus,1)
                    .in(OutPark::getParkId,parkIdList);
            wrapper.select("IFNULL(ROUND(SUM(price)),0) as num");
            Map<String, Object> map = outParkService.getMap(wrapper);
            String a = map.get("num").toString();
            if(a != null){
                resultMap.put("money",Double.parseDouble(a));
            }else {
                resultMap.put("money",0.0);
            }
            wrapper.clear();
            wrapper.lambda()
                    .between(OutPark::getCreateTime, DateUtil.beginOfDay(new Date()),DateUtil.endOfDay(new Date()))
                    .eq(OutPark::getStatus,1)
                    .in(OutPark::getParkId,parkIdList);
            long count = outParkService.count(wrapper);
            resultMap.put("count",count);
            return R.ok(resultMap);
        }else {
            return R.failed(null,"该用户未管理停车场");
        }
    }
 
    @GetMapping("/isJS")
    public Object isJS(String carNo ,Integer parkId) {
        List<EnterPark> byCarNo = enterParkService.findByCarNo(carNo, parkId);
        if(byCarNo.size() > 0){
            return R.ok(byCarNo.get(0),"true");
        }else {
            return R.ok(null,"false");
        }
    }
 
    @GetMapping("/getByCarAndPark")
    public Object getByCarAndPark(String carNo ,Integer parkId) {
        QueryWrapper<OutPark> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(OutPark::getCarNo,carNo)
                .eq(OutPark::getParkId,parkId)
                .orderByDesc(OutPark::getCreateTime);
        List<OutPark> list = outParkService.list(wrapper);
        if(list.size() > 0){
            OutPark outPark = list.get(0);
            if(outPark.getTime() > 0){
                outPark.setTimeStr(DateUtilOther.millisToDayHrMinSec(new Double(outPark.getTime()).longValue()));
            }else {
                outPark.setTimeStr("不足一分钟");
            }
            return R.ok(outPark,"找到数据");
        }else {
            return R.failed(null,"未找到数据");
        }
    }
    @GetMapping("/editEnterPark")
    public Object editEnterPark(String carNo ,Integer parkId,String time) {
        QueryWrapper<Barrier> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(Barrier::getParkId,parkId)
                .eq(Barrier::getType,1);
        List<Barrier> list = barrierService.list(wrapper);
        EnterPark enterPark = new EnterPark();
        enterPark.setCreateTime(LocalDateTime.parse(time, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        enterPark.setCarNo(carNo);
        enterPark.setParkId(parkId);
        enterPark.setBarrierId(list.get(0).getId());
        enterPark.setStatus(0);
        enterPark.setIsPay(0);
        enterPark.setImgId(null);
        enterParkService.saveOrUpdate(enterPark);
        return R.ok("修改成功");
    }
 
 
    public Barrier findBarrier(String code2) {
        QueryWrapper<Barrier> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(Barrier::getCode2,code2);
        List<Barrier> all = barrierService.list(wrapper);
        if(all.size()==0){
            return null;
        }else{
            Barrier barrier = all.get(0);
            return barrier;
        }
    }
 
    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("打印错误");
        }
    }
}