From c2217c8ff10605844779ec8a8c0a91f608d26155 Mon Sep 17 00:00:00 2001
From: liuxiaolong <liuxiaolong@aiotlink.com>
Date: 星期五, 14 八月 2020 14:57:24 +0800
Subject: [PATCH] rm juhe restriction
---
models/restriction.go | 22 +++-
controllers/restriction.go | 237 ++++++++++++++++++++++++++++-------------------
main.go | 2
3 files changed, 157 insertions(+), 104 deletions(-)
diff --git a/controllers/restriction.go b/controllers/restriction.go
index 36e716e..43770d0 100644
--- a/controllers/restriction.go
+++ b/controllers/restriction.go
@@ -2,46 +2,17 @@
import (
"car-service/extend/code"
- "car-service/extend/util"
"car-service/models"
"encoding/json"
"fmt"
"github.com/astaxie/beego"
"net/http"
- "github.com/robfig/cron"
- "strconv"
"strings"
"time"
)
type RestrictionController struct {
beego.Controller
-}
-
-func InitRestriction() {
- date := time.Now().Format("2006-01-02")
- var r models.Restriction
- err := r.SelectByDate(date)
- if err == nil {
- //浠婃棩宸茶幏鍙栬繃闄愯淇℃伅
- todayRes = &RestrictionResult{
- Date: date,
- Week: r.Week,
- CityName: r.CityName,
- IsXianXing: r.IsXianXing,
- }
- arr := strings.Split(r.WeiHao, ",")
- if len(arr) >0 {
- for _,s := range arr {
- n, e := strconv.Atoi(s)
- if e ==nil {
- todayRes.XxWeiHao = append(todayRes.XxWeiHao,n)
- }
- }
- }
- } else {
- getRestrictionInfo()
- }
}
// @Title 鏌ヨ闄愯灏惧彿
@@ -51,10 +22,29 @@
// @router /restriction [get]
func (c *RestrictionController) Restriction() {
resp := code.Code{}
- if todayRes !=nil {
+ var restric models.Restriction
+ err := restric.GetOne()
+
+ if err == nil {
resp.Success= true
resp.Status= http.StatusOK
- resp.Data= *todayRes
+ now := time.Now()
+ nowDate := now.Format("2006-01-02")
+ week := now.Weekday()
+ result := RestrictionResult{}
+ result.CityName = "鍖椾含"
+ result.Date = nowDate
+ result.Week = getWeek(week)
+ //鍛ㄥ叚鍛ㄦ棩锛屼互鍙婃硶瀹氳妭鍋囨棩涓嶉檺琛�
+ if week == time.Sunday || week == time.Saturday || isHoliday(nowDate, restric.Holidays) {
+ result.IsXianXing = 0
+ result.XxWeiHao = []int{}
+ } else {
+ result.IsXianXing = 1
+ //鑾峰彇闄愯灏惧彿
+ result.XxWeiHao = getXianxingWeiHao(now, nowDate, restric.Setting)
+ }
+ resp.Data= result
} else {
resp.Success= false
resp.Status= http.StatusBadRequest
@@ -64,16 +54,74 @@
c.ServeJSON()
}
-var todayRes *RestrictionResult
+func getXianxingWeiHao(nowTime time.Time, nowDate string, setting string) []int {
+ var set []models.SetWeiHao
+ err := json.Unmarshal([]byte(setting), &set)
+ if err != nil {
+ fmt.Println("unmarshal xianxing set err:", err)
+ return []int{}
+ } else {
+ list := make([]int, 0)
+ for _,d := range set {
+ if isBetween(d.StartTime, d.EndTime, nowDate, nowTime) {
+ for _,xx := range d.XianXing {
+ if xx.Day == int(nowTime.Weekday()) {
+ list = xx.WeiHao
+ break
+ }
+ }
+ break
+ }
+ }
+ return list
+ }
+}
-func Schedule() {
- c := cron.New()
- //姣忓ぉ鍑屾櫒5鐐规墽琛屼竴娆★紝鑾峰彇褰撴棩鏁版嵁
- c.AddFunc("0 0 1 * * ?", func() {
- todayRes = nil
- getRestrictionInfo()
- })
- c.Start()
+func isBetween(st string, et string, nowDate string, nowTime time.Time) bool {
+ if st == nowDate || et == nowDate {
+ return true
+ } else {
+ st, e1 := time.Parse(st, "2006-01-02")
+ et, e2 := time.Parse(et, "2006-01-02")
+ if e1 == nil && e2 == nil && st.Before(nowTime) && et.After(nowTime) {
+ return true
+ }
+ return false
+ }
+}
+
+func getWeek(week time.Weekday) string {
+ s := ""
+ switch week {
+ case time.Sunday:
+ s = "鏄熸湡鏃�"
+ case time.Monday:
+ s = "鏄熸湡涓�"
+ case time.Tuesday:
+ s = "鏄熸湡浜�"
+ case time.Wednesday:
+ s = "鏄熸湡涓�"
+ case time.Thursday:
+ s = "鏄熸湡鍥�"
+ case time.Friday:
+ s = "鏄熸湡浜�"
+ case time.Saturday:
+ s = "鏄熸湡鍏�"
+ default:
+ s = ""
+ }
+ return s
+}
+
+//鍒ゆ柇鏄惁鏄妭鍋囨棩
+func isHoliday(nowDate string, holidays string) bool {
+ arr := strings.Split(holidays, ",")
+ for _,s := range arr {
+ if s == nowDate {
+ return true
+ }
+ }
+ return false
}
type RestrictionResult struct {
@@ -84,59 +132,58 @@
XxWeiHao []int `json:"xxWeiHao"`
}
-type JuHeResult struct {
- Reason string `json:"reason"`
- Result CityRestriction `json:"result"`
- ErrorCode int `json:"error_code"`
-}
+//type JuHeResult struct {
+// Reason string `json:"reason"`
+// Result CityRestriction `json:"result"`
+// ErrorCode int `json:"error_code"`
+//}
+//
+//type CityRestriction struct {
+// Date string `json:"date"`
+// Week string `json:"week"`
+// City string `json:"city"`
+// CityName string `json:"cityname"`
+// Des []interface{} `json:"des"`
+// Fine string `json:"fine"`
+// Remarks string `json:"remarks"`
+// IsXianXing int `json:"isxianxing"`
+// XXWeiHao []int `json:"xxweihao"`
+// Holiday string `json:"holiday"`
+//}
-type CityRestriction struct {
- Date string `json:"date"`
- Week string `json:"week"`
- City string `json:"city"`
- CityName string `json:"cityname"`
- Des []interface{} `json:"des"`
- Fine string `json:"fine"`
- Remarks string `json:"remarks"`
- IsXianXing int `json:"isxianxing"`
- XXWeiHao []int `json:"xxweihao"`
- Holiday string `json:"holiday"`
-}
-
-func getRestrictionInfo() {
- juHeAppKey := beego.AppConfig.String("juheweihaokey")
- url := "http://v.juhe.cn/xianxing/index?key="+ juHeAppKey +"&city=beijing&type=1"
- b, err := util.DoPostRequest(url, util.CONTENT_TYPE_JSON, nil, nil, nil)
- if err != nil {
- fmt.Println("err:", err)
- return
- }
- var result JuHeResult
- err = json.Unmarshal(b, &result)
- if err != nil {
- fmt.Println("unmarshal err:", err)
- return
- }
- if result.ErrorCode == 0 {
- todayRes = &RestrictionResult{
- Date: result.Result.Date,
- Week: result.Result.Week,
- CityName: result.Result.CityName,
- IsXianXing: result.Result.IsXianXing,
- XxWeiHao: result.Result.XXWeiHao,
- }
- rc := models.Restriction{
- Date: result.Result.Date,
- Week: result.Result.Week,
- CityName: result.Result.CityName,
- IsXianXing: result.Result.IsXianXing,
- }
- var arr []string
- for _,n := range result.Result.XXWeiHao {
- arr=append(arr, strconv.Itoa(n))
- }
- rc.WeiHao = strings.Join(arr, ",")
- rc.Insert()
- }
-}
-
+//func getRestrictionInfo() {
+// juHeAppKey := beego.AppConfig.String("juheweihaokey")
+// url := "http://v.juhe.cn/xianxing/index?key="+ juHeAppKey +"&city=beijing&type=1"
+// b, err := util.DoPostRequest(url, util.CONTENT_TYPE_JSON, nil, nil, nil)
+// if err != nil {
+// fmt.Println("err:", err)
+// return
+// }
+// var result JuHeResult
+// err = json.Unmarshal(b, &result)
+// if err != nil {
+// fmt.Println("unmarshal err:", err)
+// return
+// }
+// if result.ErrorCode == 0 {
+// todayRes = &RestrictionResult{
+// Date: result.Result.Date,
+// Week: result.Result.Week,
+// CityName: result.Result.CityName,
+// IsXianXing: result.Result.IsXianXing,
+// XxWeiHao: result.Result.XXWeiHao,
+// }
+// rc := models.Restriction{
+// Date: result.Result.Date,
+// Week: result.Result.Week,
+// CityName: result.Result.CityName,
+// IsXianXing: result.Result.IsXianXing,
+// }
+// var arr []string
+// for _,n := range result.Result.XXWeiHao {
+// arr=append(arr, strconv.Itoa(n))
+// }
+// rc.WeiHao = strings.Join(arr, ",")
+// rc.Insert()
+// }
+//}
\ No newline at end of file
diff --git a/main.go b/main.go
index 6b31762..1af406e 100644
--- a/main.go
+++ b/main.go
@@ -15,8 +15,6 @@
beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
}
models.InitDb()
- controllers.InitRestriction()
- controllers.Schedule()
go controllers.ComputeSpaceLeftRealTime()
//姣忓ぉ鍚屾娴峰悍鏁版嵁锛岀湅浜哄憳id鏄惁鏈夊彂鐢熷彉鍖�
c := cron.New()
diff --git a/models/restriction.go b/models/restriction.go
index 8a29351..5f80feb 100644
--- a/models/restriction.go
+++ b/models/restriction.go
@@ -5,11 +5,19 @@
)
type Restriction struct {
- Date string `orm:"pk;size(50);column(date)" json:"date"`
- Week string `orm:"size(50);column(week)" json:"week"`
- CityName string `orm:"size(50);column(cityName)" json:"cityName"`
- IsXianXing int `orm:"size(8);column(isXianXing)" json:"isXianXing"`
- WeiHao string `orm:"size(50);column(weiHao)" json:"weiHao"`
+ Holidays string `orm:"size(2000);column(holidays)" json:"holidays"` //娉曞畾鑺傚亣鏃�
+ Setting string `orm:"size(2000);column(setting)" json:"setting"`
+}
+
+type SetWeiHao struct {
+ StartTime string `json:"startTime"`
+ EndTime string `json:"endTime"`
+ XianXing []DayRes `json:"xianXing"`
+}
+
+type DayRes struct {
+ Day int `json:"day"` //浠庡懆涓�鍒板懆浜斿垎鍒槸1,2,3,4,5
+ WeiHao []int `json:"weiHao"`
}
func (r *Restriction) TableName() string {
@@ -21,8 +29,8 @@
return o.Insert(r)
}
-func (r *Restriction) SelectByDate(date string) error {
+func (r *Restriction) GetOne() error {
o := orm.NewOrm()
- err := o.QueryTable(r.TableName()).Filter("date", date).One(r)
+ err := o.QueryTable(r.TableName()).One(r)
return err
}
\ No newline at end of file
--
Gitblit v1.8.0