-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
添加工作日和节假日判断功能
- Loading branch information
Showing
6 changed files
with
306 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package common | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/chanyipiaomiao/cal" | ||
"github.com/chanyipiaomiao/hltool" | ||
"github.com/tidwall/gjson" | ||
) | ||
|
||
const ( | ||
holiworkdayTableName = "holiworkday" | ||
dateTemplate = "2006-01-02" | ||
) | ||
|
||
// ReqHoliday 请求过来的节假日和工作日设置 | ||
type ReqHoliday struct { | ||
Holiday []struct { | ||
EndTime string `json:"end_time"` | ||
Name string `json:"name"` | ||
StartTime string `json:"start_time"` | ||
ZhName string `json:"zh_name"` | ||
} `json:"holiday"` | ||
Workday []string `json:"workday"` | ||
Year string `json:"year"` | ||
} | ||
|
||
// HoliWorkday 节假日和工作日 | ||
type HoliWorkday struct{} | ||
|
||
// 解析工作日节假日json字符串 | ||
func (h *HoliWorkday) parse(jsonstr []byte) (*cal.Calendar, error) { | ||
|
||
r := new(ReqHoliday) | ||
err := json.Unmarshal(jsonstr, r) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
calendar := cal.NewCalendar() | ||
|
||
for _, v := range r.Holiday { | ||
endTime, _ := time.Parse(dateTemplate, v.EndTime) | ||
startTime, _ := time.Parse(dateTemplate, v.StartTime) | ||
calendar.AddHoliday(cal.NewHolidayExact(startTime.Month(), startTime.Day(), startTime.Year())) | ||
sub := endTime.Sub(startTime).Hours() / 24 | ||
for i := 1; i < int(sub); i++ { | ||
t := startTime.AddDate(0, 0, i) | ||
calendar.AddHoliday(cal.NewHolidayExact(t.Month(), t.Day(), t.Year())) | ||
} | ||
calendar.AddHoliday(cal.NewHolidayExact(endTime.Month(), endTime.Day(), endTime.Year())) | ||
} | ||
|
||
for _, v := range r.Workday { | ||
t, _ := time.Parse(dateTemplate, v) | ||
calendar.AddExtraWorkday(t) | ||
} | ||
|
||
calendar.Observed = cal.ObservedExact | ||
|
||
return calendar, nil | ||
} | ||
|
||
// Setting 保存节假日和工作日设置 | ||
func (h *HoliWorkday) Setting(reqBody []byte) error { | ||
|
||
db, err := hltool.NewBoltDB(DBPath, holiworkdayTableName) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
year := gjson.Get(string(reqBody), "year").String() | ||
err = db.Set(map[string][]byte{ | ||
year: reqBody, | ||
}) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// IsHoliWorkday 检查给定的日期是工作日还是节假日 | ||
func (h *HoliWorkday) IsHoliWorkday(date string) (string, error) { | ||
|
||
t, err := time.Parse(dateTemplate, date) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
db, err := hltool.NewBoltDB(DBPath, holiworkdayTableName) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
year := strconv.Itoa(t.Year()) | ||
|
||
result, err := db.Get([]string{year}) | ||
if err != nil { | ||
return "", err | ||
} | ||
if result[year] == nil { | ||
return "", fmt.Errorf("%s year holiday setting not in db, please setting", year) | ||
} | ||
|
||
calendar, err := h.parse(result[year]) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if calendar.IsExtraWorkday(t) { | ||
return "workday", nil | ||
} | ||
|
||
if calendar.IsWorkday(t) { | ||
return "workday", nil | ||
} | ||
|
||
if calendar.IsHoliday(t) { | ||
return "holiday", nil | ||
} | ||
|
||
return "weekend", nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package controllers | ||
|
||
import ( | ||
"fmt" | ||
|
||
"devops-api/common" | ||
) | ||
|
||
// Post 接收中国的节假日安排, 为判断节假日和工作日准备 | ||
func (h *HolidayController) Post() { | ||
requestID := h.Data["RequestID"].(string) | ||
holidayLog := map[string]interface{}{ | ||
"entryType": "setting holiday/workday", | ||
"requestID": requestID, | ||
} | ||
holiday := &common.HoliWorkday{} | ||
err := holiday.Setting(h.Ctx.Input.RequestBody) | ||
if err != nil { | ||
holidayLog["statuscode"] = 1 | ||
holidayLog["errmsg"] = fmt.Sprintf("%s", err) | ||
holidayLog["result"] = "error" | ||
common.GetLogger().Error(holidayLog, "设置节假日和工作日") | ||
h.Data["json"] = holidayLog | ||
h.ServeJSON() | ||
return | ||
} | ||
|
||
holidayLog["statuscode"] = 0 | ||
holidayLog["errmsg"] = "" | ||
holidayLog["result"] = "ok" | ||
common.GetLogger().Info(holidayLog, "设置节假日和工作日") | ||
h.Data["json"] = holidayLog | ||
h.ServeJSON() | ||
} | ||
|
||
// Get 接收一个日期,判断是节假日还是工作日 | ||
func (h *HolidayController) Get() { | ||
requestID := h.Data["RequestID"].(string) | ||
holidayLog := map[string]interface{}{ | ||
"entryType": "judgment holiday/workday", | ||
"requestID": requestID, | ||
} | ||
date := h.GetString("date") | ||
holiworkday := &common.HoliWorkday{} | ||
r, err := holiworkday.IsHoliWorkday(date) | ||
if err != nil { | ||
holidayLog["statuscode"] = 1 | ||
holidayLog["errmsg"] = fmt.Sprintf("%s", err) | ||
common.GetLogger().Error(holidayLog, "判断节假日和工作日") | ||
h.Data["json"] = holidayLog | ||
h.ServeJSON() | ||
return | ||
} | ||
|
||
holidayLog["statuscode"] = 0 | ||
holidayLog["errmsg"] = "" | ||
holidayLog["date"] = date | ||
holidayLog["dateType"] = r | ||
common.GetLogger().Info(holidayLog, "判断节假日和工作日") | ||
h.Data["json"] = holidayLog | ||
h.ServeJSON() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters