kongdeqiang
2024-03-18 ffa5f49a2bcb6311486d00777b3629538eb3e6f0
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
package com.boying.service.impl;
 
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.boying.entity.Barrier;
import com.boying.entity.EnterPark;
import com.boying.entity.OutPark;
import com.boying.entity.Park;
import com.boying.mapper.BarrierMapper;
import com.boying.mapper.EnterParkMapper;
import com.boying.mapper.OutParkMapper;
import com.boying.mapper.ParkMapper;
import com.boying.service.BarrierService;
import com.boying.service.EnterParkService;
import com.boying.util.RedisJsonUtil;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
 
import java.io.IOException;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
 
/**
 * @author kdq
 * @version 1.0.0
 * @ClassName BarrierServiceImpl.java
 * @Description TODO
 * @createTime 2022年11月20日 22:25:00
 */
@Service
@AllArgsConstructor
public class BarrierServiceImpl extends ServiceImpl<BarrierMapper, Barrier> implements BarrierService {
    private EnterParkMapper enterParkMapper;
    private OutParkMapper outParkMapper;
    private StringRedisTemplate redisTemplate;
    private ParkMapper parkMapper;
    private RedisJsonUtil redisJsonUtil;
    @Override
    public Barrier findByCode(String code) {
        Barrier barrier = null;
        try {
           barrier =  redisJsonUtil.get("barrier-"+code,Barrier.class);
           if(barrier != null){
               return barrier;
           }else {
               QueryWrapper<Barrier> wrapper = new QueryWrapper<>();
               wrapper.lambda()
                       .eq(Barrier::getCode,code);
               List<Barrier> list = list(wrapper);
               if(list.size()>0){
                   redisJsonUtil.set("barrier-"+code,list.get(0));
                   return list.get(0);
               }
           }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
      return null;
    }
 
    @Override
    public Barrier findByCode2(String code) {
        QueryWrapper<Barrier> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(Barrier::getCode2,code);
        List<Barrier> list = list(wrapper);
        if(list.size()>0){
            return list.get(0);
        }else {
            return null;
        }
    }
 
    @Override
    public boolean getDateDifIn(Integer barrierId) {
        QueryWrapper<EnterPark> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(EnterPark::getBarrierId,barrierId)
                .orderByDesc(EnterPark::getId)
                .last(" limit 1");
        EnterPark enterPark = enterParkMapper.selectOne(wrapper);
        if(enterPark != null){
            long dif = DateUtil.between(Date.from( enterPark.getCreateTime().atZone( ZoneId.systemDefault()).toInstant()), new Date(), DateUnit.SECOND, false);
            if(dif >= 5 && dif <= 7){
                return true;
            }else {
                //判断停车场是否有了车位
                String s = redisTemplate.opsForValue().get("park_change_in_" + enterPark.getParkId());
                if("true".equals(s)){
                    redisTemplate.opsForValue().set("park_change_in_"+enterPark.getParkId(),"false",1, TimeUnit.DAYS);
                    return true;
                }else {
                    return false;
                }
            }
        }else {
            return false;
        }
 
 
    }
 
    @Override
    public boolean getDateDifOut(Integer barrierId) {
        QueryWrapper<OutPark> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(OutPark::getBarrierId,barrierId)
                .orderByDesc(OutPark::getId)
                .last(" limit 1");
        OutPark outPark = outParkMapper.selectOne(wrapper);
        if(outPark != null){
            long dif = DateUtil.between(Date.from( outPark.getCreateTime().atZone( ZoneId.systemDefault()).toInstant()), new Date(), DateUnit.SECOND, false);
            if(dif >= 20 && dif <= 23){
                return true;
            }else {
                return false;
            }
        }else {
            return false;
        }
    }
}