kongdeqiang
2023-03-02 ee83188936c8ac306144f6c8cd119b6d7574dfc6
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
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.boying.common.R;
import com.boying.common.SystemConfigProperties;
import com.boying.entity.*;
import com.boying.service.*;
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.util.*;
import java.util.concurrent.TimeUnit;
 
@RestController
@RequestMapping("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,OutPark outPark) {
        QueryWrapper<OutPark> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByDesc(OutPark::getId);
        return R.ok(outParkService.page(page,wrapper));
    }
 
    //道闸code
    @PostMapping("findByBarrierCode")
    public Object findById(String code) {
       QueryWrapper<Barrier> wrapper = new QueryWrapper<>();
       wrapper.lambda()
               .eq(Barrier::getCode2,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());
                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")
    public Object statisticParkOrder(Integer userId) {
        User byId = userService.getById(userId);
        Map<String,Object> resultMap = new HashMap<>();
        if(byId != null){
            String parkIds = byId.getParkIds();
            if(parkIds != null){
                String[] split = parkIds.split(",");
                int[] array = Arrays.stream(split).mapToInt(Integer::parseInt).toArray();
                List<Integer> parkIdList = new ArrayList<>();
                for (int 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);
                int count = outParkService.count(wrapper);
                resultMap.put("count",count);
                return R.ok(resultMap);
            }else {
                return R.failed(null,"该用户未管理停车场");
            }
        }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));
        }else {
            return R.failed();
        }
    }
 
 
    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("打印错误");
        }
    }
}