From 27f1012d2fecda7bcc3d9f44c5dd4c10a7cf38a4 Mon Sep 17 00:00:00 2001
From: liuxiaolong <liuxiaolong@aiotlink.com>
Date: 星期二, 18 八月 2020 12:13:54 +0800
Subject: [PATCH] add log

---
 controllers/car.go |  225 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 219 insertions(+), 6 deletions(-)

diff --git a/controllers/car.go b/controllers/car.go
index ab1a353..0585eb8 100644
--- a/controllers/car.go
+++ b/controllers/car.go
@@ -4,13 +4,134 @@
 	"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)
+							//璁板綍鎺ㄩ�佹棩蹇�
+							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 := 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)
+
+}
+
+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 缁熻鍓╀綑杞︿綅
@@ -19,10 +140,9 @@
 // @Failure 403 {string} json ""
 // @router /statistic [get]
 func (c *CarController) Statistic() {
-	//sv := service.NewCarService()
-	//sta := sv.Statistic()
+	left := getSpaceLeft()
 	sta := models.CarStatistic{
-		Left: 80,
+		Left: left,
 	}
 	resp := code.Code{
 		Success: true,
@@ -48,8 +168,9 @@
 // @Failure 403 {string} json ""
 // @router /spaceNo [get]
 func (c *CarController) SpaceNo() {
+	userId := c.GetString("userId")
 	sv := service.NewCarService()
-	spaceNos := sv.FindSpaceNo()
+	spaceNos := sv.FindSpaceNo(userId)
 	sort.Sort(spaceNos)
 	resp := code.Code{
 		Success: true,
@@ -60,11 +181,103 @@
 	c.ServeJSON()
 }
 
+
 func (c *CarController) BindCarSpace() {
-	sv := service.NewCarService()
 
-	if sv.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)
+	//璁板綍鎺ㄩ�佹棩蹇�
+	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 /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()
 }
\ No newline at end of file

--
Gitblit v1.8.0