log
liuxiaolong
2020-08-18 0ad0f24393f5db3224a8e3900b97bb31cf60f56d
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
package controllers
 
import (
    "car-service/extend/code"
    "car-service/models"
    "encoding/json"
    "fmt"
    "github.com/astaxie/beego"
    "net/http"
    "strings"
    "time"
)
 
type RestrictionController struct {
    beego.Controller
}
 
// @Title 查询限行尾号
// @Description 查询限行尾号
// @Success 200 {object} controllers.RestrictionResult
// @Failure 403 {string} json ""
// @router /restriction [get]
func (c *RestrictionController) Restriction() {
    resp := code.Code{}
    var restric models.Restriction
    err := restric.GetOne()
 
    if err == nil {
        resp.Success= true
        resp.Status= http.StatusOK
        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
        resp.Data= "数据请求失败,请稍后重试"
    }
    c.Data["json"] = resp
    c.ServeJSON()
}
 
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 isBetween(st string, et string, nowDate string, nowTime time.Time) bool {
    if st == nowDate || et == nowDate {
        return true
    } else {
        st, e1 := time.Parse("2006-01-02", st)
        et, e2 := time.Parse("2006-01-02", et)
        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 {
    Date         string         `json:"date"`
    Week         string         `json:"week"`
    CityName     string        `json:"cityName"`
    IsXianXing     int         `json:"isxianxing"`
    XxWeiHao     []int        `json:"xxWeiHao"`
}
 
//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"`
//}
 
//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()
//    }
//}