liuxiaolong
2020-09-04 6e44a46c6165c3e0d4d1d334137cba0b6b6c12ee
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
package controllers
 
import (
    "car-service/extend/code"
    "car-service/models"
    "car-service/service"
    "fmt"
    "github.com/astaxie/beego"
    "github.com/satori/go.uuid"
    "net/http"
    "sort"
    "strings"
    "sync"
    "time"
)
 
type CarController struct {
    beego.Controller
}
 
//实时计算剩余车位数量,达到条件就推送
//要求:
//1.停车数量小于等于5个推送,5个以下的数量变化都推送,推送给已注册手机号的用户,未注册的不推
//2.满足上一条,如果此车辆已经进入学校停车场了,就不再给这个车牌对应的手机号推送空余车位消息
//3.晚上10点到10点半,每间隔10分钟,给停车场内的车辆推送消息:请尽快驶出停车场
func ComputeSpaceLeftRealTime() {
    ticker := time.NewTicker(3 * time.Second)
    prePushLeft, _ := beego.AppConfig.Int("initPushLeft") //初始剩余数量,上线后不能每次启动都推送消息
    sv := service.NewCarService()
    initCacheM := false
    lowerLimit,_ := beego.AppConfig.Int("pushLowerLimit") //[0,5]
    nightPushTimes := 0
    for {
        select {
            case <-ticker.C:
                hikSta, flag := sv.Statistic()
                if flag {
                    left := hikSta.Left
                    if !initCacheM {
                        models.SetSpaceNo(hikSta.TotalPlace)
                        initCacheM = true
                    }
 
                    if left <=lowerLimit && left != prePushLeft {
                        go func() {
                            t := time.Now().Format("2006-01-02 15:04:05")
                            message := fmt.Sprintf("%s 剩余车位:%d个", t, left)
                            b, e, aliasArr := service.PushByAlias("育英中学停车", message, false)
                            //记录推送日志
                            logE := models.Log{
                                Id: uuid.NewV4().String(),
                                CreateTime: t,
                                Result: b,
                                Phones: strings.Join(aliasArr, ","),
                            }
                            if e != nil {
                                logE.Content = e.Error()
                            } else {
                                logE.Content = message
                            }
                            logE.Insert()
                            prePushLeft = left
 
                            fmt.Println("b:", b,"e:",e, "message:", message)
                        }()
                    }
                    updateSpaceLeft(left)
                }
                //判断当前是否在22:00-22:30之间
                now := time.Now()
                if now.Hour() == 22 && now.Minute()>=0 && now.Minute() <=30{
                    if now.Minute() == 0 {
                        if nightPushTimes ==0 {
                            go nightPush()
                            nightPushTimes++
                        }
                    } else if now.Minute() == 10 {
                        if nightPushTimes == 1 {
                            go nightPush()
                            nightPushTimes++
                        }
                    } else if now.Minute() == 20 {
                        if nightPushTimes == 2{
                            go nightPush()
                            nightPushTimes++
                        }
                    } else if now.Minute() == 30 {
                        if nightPushTimes == 3{
                            go nightPush()
                            nightPushTimes++
                        }
                    }
                } else {
                    nightPushTimes = 0
                }
 
        default:
            time.Sleep(500 * time.Millisecond)
        }
    }
}
 
func nightPush(){
 
    message := fmt.Sprintf("%s 请尽快驶出停车场", time.Now().Format("2006-01-02 15:04:05"))
    b, e, aliasArr,carOwnNames := service.NightPush("育英中学停车", message)
    //记录推送日志
    logE := models.Log{
        Id: uuid.NewV4().String(),
        CreateTime: time.Now().Format("2006-01-02 15:04:05"),
        Result: b,
        Phones: strings.Join(aliasArr, ","),
    }
    if e != nil {
        logE.Content = e.Error()
    } else {
        logE.Content = message
    }
    logE.Insert()
    fmt.Println("b:", b,"e:",e, "message:", message)
 
    //将具体的车主信息推送给管理员
    if carOwnNames != nil && len(carOwnNames) >0 {
        //获取车主姓名
        managerMsg := fmt.Sprintf("%s 未驶离车主: %s", time.Now().Format("2006-01-02 15:04:05"), strings.Join(carOwnNames, " "))
        mb,me, managerArr := service.Push2Manager("育英中学停车", managerMsg)
        mLogE := models.Log{
            Id: uuid.NewV4().String(),
            CreateTime: time.Now().Format("2006-01-02 15:04:05"),
            Result: mb,
            Phones: strings.Join(managerArr, ","),
        }
        if me != nil {
            mLogE.Content = me.Error()
        } else {
            mLogE.Content = managerMsg
        }
        mLogE.Insert()
        fmt.Println("mb:", mb, "me:", me, "message:", managerMsg)
    }
}
 
var cacheSpaceLeft int
var cLock sync.RWMutex
func updateSpaceLeft(num int) {
    cLock.Lock()
    defer cLock.Unlock()
    cacheSpaceLeft = num
}
func getSpaceLeft() int {
    cLock.Lock()
    defer cLock.Unlock()
    return cacheSpaceLeft
}
 
// @Title 统计剩余车位
// @Description 统计剩余车位
// @Success 200 {object} models.CarStatistic
// @Failure 403 {string} json ""
// @router /statistic [get]
func (c *CarController) Statistic() {
    left := getSpaceLeft()
    sta := models.CarStatistic{
        Left: left,
    }
    resp := code.Code{
        Success: true,
        Status: http.StatusOK,
        Data: sta,
    }
    c.Data["json"] = resp
    c.ServeJSON()
}
 
// @Title 根据车牌号查找车位信息
// @Description 根据车牌号查找车位信息
// @Success 200 {object} models.CarPos
// @Failure 403 {string} json ""
// @router /carPos [get]
func (c *CarController) findCar() {
 
}
 
// @Title 车位占用情况
// @Description 车位占用情况
// @Success 200 {object} models.PosResult
// @Failure 403 {string} json ""
// @router /spaceNo [get]
func (c *CarController) SpaceNo() {
    userId := c.GetString("userId")
    sv := service.NewCarService()
    spaceNos := sv.FindSpaceNo(userId)
    sort.Sort(spaceNos)
    resp := code.Code{
        Success: true,
        Status: http.StatusOK,
        Data: spaceNos,
    }
    c.Data["json"] = resp
    c.ServeJSON()
}
 
 
func (c *CarController) BindCarSpace() {
 
    c.ServeJSON()
}
 
// @router /testPush [get]
func (c *CarController) TestPush() {
    left := getSpaceLeft()
    message := fmt.Sprintf("%s 剩余车位:%d个", time.Now().Format("2006-01-02 15:04:05"), left)
    b, e, aliasArr := service.PushByAlias("育英中学停车", message, true)
    //记录推送日志
    logE := models.Log{
        Id: uuid.NewV4().String(),
        CreateTime: time.Now().Format("2006-01-02 15:04:05"),
        Result: b,
        Phones: strings.Join(aliasArr, ","),
    }
    if e != nil {
        logE.Content = e.Error()
    } else {
        logE.Content = message
    }
    logE.Insert()
 
    fmt.Println("b:", b,"e:",e, "message:", message)
    resp := code.Code{}
    if b {
        resp.Success = true
        resp.Status = http.StatusOK
        resp.Data = "推送成功"
    } else {
        resp.Success = false
        resp.Status = http.StatusInternalServerError
        resp.Data = e.Error()
    }
    c.Data["json"] = resp
    c.ServeJSON()
}
 
// @router /spaceInfo [get]
func (c *CarController) SpaceInfo() {
    sv := service.NewCarService()
    spaceInfo := sv.FindHikSpaceInfo()
    c.Data["json"] = code.Code{
        Success: true,
        Status: http.StatusOK,
        Data: spaceInfo,
    }
    c.ServeJSON()
}
 
// @router /spaceUser [get]
func (c *CarController) SpaceUser() {
    sv := service.NewCarService()
    spaceUser := sv.FindHikSpaceUser()
    c.Data["json"] = code.Code{
        Success: true,
        Status: http.StatusOK,
        Data: spaceUser,
    }
    c.ServeJSON()
}
 
// @router /pushLog [get]
func (c *CarController) PushLog() {
    st := c.GetString("startTime")
    et := c.GetString("endTime")
    if st == "" {
        st = time.Now().Format("2006-01-02")
    }
    if et == "" {
        et = time.Now().AddDate(0,0,1).Format("2006-01-02")
    }
    curPage, err := c.GetInt("curPage")
    if err != nil {
        curPage = 1
    }
    pageSize, err := c.GetInt("pageSize")
    if err != nil {
        pageSize = 20
    }
    var l models.Log
    total, logs := l.Find(curPage, pageSize, st, et)
    var rl []models.Log
    for _,le := range logs {
        rl = append(rl, *le)
    }
    resp := code.Code{
        Success: true,
        Status:  http.StatusOK,
        Data:    map[string]interface{}{
            "total": total,
            "list": rl,
        },
    }
    c.Data["json"] = resp
    c.ServeJSON()
}
 
// @router /crossRecord [get]
func (c *CarController) CrossRecord() {
    sv := service.NewCarService()
    records := sv.CrossRecords()
    resp := code.Code{
        Success: true,
        Status:  http.StatusOK,
        Data:    records,
    }
    c.Data["json"] = resp
    c.ServeJSON()
}