film/worker/haha/haha_sync_order.go

162 lines
4.3 KiB
Go
Raw Normal View History

2023-04-09 02:11:58 +08:00
package haha
2023-04-06 13:46:19 +08:00
import (
"crypto/tls"
"encoding/json"
2023-05-16 09:42:30 +08:00
"film/base/util"
2023-04-06 13:46:19 +08:00
"film/model"
2023-08-27 16:15:29 +08:00
"film/worker/common"
2023-04-06 13:46:19 +08:00
"fmt"
"github.com/go-co-op/gocron"
log "github.com/sirupsen/logrus"
2023-04-09 13:05:52 +08:00
"github.com/smbrave/goutil"
2023-04-06 13:46:19 +08:00
"github.com/spf13/cast"
"io"
"net/http"
"net/url"
"time"
)
2023-04-09 02:11:58 +08:00
type SyncOrderConfig struct {
2023-04-09 13:05:52 +08:00
Token string
NewOrder chan *model.Order
UpdateOrder chan *model.Order
SyncInterval int
2023-04-06 13:46:19 +08:00
}
2023-04-09 02:11:58 +08:00
type SyncOrder struct {
2023-04-09 13:05:52 +08:00
token string
syncInterval int
newOrder chan *model.Order
updateOrder chan *model.Order
2023-04-09 02:11:58 +08:00
}
func NewSyncOrder(cfg *SyncOrderConfig) *SyncOrder {
return &SyncOrder{
2023-04-09 13:05:52 +08:00
token: cfg.Token,
newOrder: cfg.NewOrder,
updateOrder: cfg.UpdateOrder,
syncInterval: goutil.If(cfg.SyncInterval == 0, 30, cfg.SyncInterval),
2023-04-09 02:11:58 +08:00
}
}
func (s *SyncOrder) Sync() {
2023-04-06 13:46:19 +08:00
timezone, _ := time.LoadLocation("Asia/Shanghai")
cron := gocron.NewScheduler(timezone)
2023-04-09 13:05:52 +08:00
cron.Every(s.syncInterval).Seconds().Do(func() {
2023-04-09 02:11:58 +08:00
s.run()
2023-04-06 13:46:19 +08:00
})
cron.StartAsync()
}
2023-04-09 02:11:58 +08:00
func (s *SyncOrder) run() {
2023-04-06 13:46:19 +08:00
orderUrl := "https://hahapiao.cn/api/Synchro/toList"
2023-04-09 02:11:58 +08:00
body, err := s.httpPost(orderUrl)
2023-04-06 13:46:19 +08:00
if err != nil {
log.Errorf("syncOrder error : %s", err.Error())
return
}
result := make(map[string]interface{})
if err := json.Unmarshal(body, &result); err != nil {
log.Errorf("json.unmarshal [%s] error : %s", string(body), err.Error())
return
}
if cast.ToInt(result["status"]) != 200 || cast.ToInt(result["code"]) != 200 {
log.Errorf("status[%d] code[%d] message[%s]", cast.ToInt(result["status"]), cast.ToInt(result["code"]), cast.ToString(result["msg"]))
return
}
2023-08-27 16:17:32 +08:00
cipherText := cast.ToString(result["data"])
2023-08-27 15:58:37 +08:00
datas, err := common.Decrypt(cipherText, s.token)
if err != nil {
log.Errorf("common.Decrypt error :%s", err.Error())
return
}
2023-04-06 13:46:19 +08:00
for _, d := range datas {
data := cast.ToStringMap(d)
id := cast.ToInt64(data["id"])
order, err := model.GetOrder(id)
if err != nil {
log.Errorf("get order id : %d error :%s", id, err.Error())
}
isAdd := false
if order == nil {
order = new(model.Order)
order.Id = id
isAdd = true
}
2023-04-09 12:22:31 +08:00
order.OrderNo = cast.ToString(data["order_id"])
2023-04-06 13:46:19 +08:00
order.PayPrice = int64(cast.ToFloat64(data["payPrice"]) * 100)
order.TotalPrice = int64(cast.ToFloat64(data["total_price"]) * 100)
order.MaoyanPrice = int64(cast.ToFloat64(data["maoyan_price"]) * 100)
order.StartTime = cast.ToString(data["startTime"])
order.StartDate = cast.ToString(data["invalidateDate"])
order.SeatNum = cast.ToInt(data["seat_num"])
order.Seats = cast.ToString(data["seats"])
order.ShowTime = cast.ToInt64(data["show_time"])
order.PayTime = cast.ToInt64(data["pay_time"])
2023-04-09 10:40:20 +08:00
order.OrderCreateTime = cast.ToInt64(data["create_time"])
2023-04-06 13:46:19 +08:00
order.CityName = cast.ToString(data["cityName"])
order.ProvinceName = cast.ToString(data["provinceName"])
order.MovieName = cast.ToString(data["movieName"])
order.Address = cast.ToString(data["address"])
order.Ting = cast.ToString(data["ting"])
2023-04-06 13:59:46 +08:00
order.CinemaName = cast.ToString(data["cinemaName"])
2023-05-16 09:42:30 +08:00
order.CinemaBrand = util.CinemaBrand(order.CinemaName)
2023-04-07 23:51:09 +08:00
order.Count = cast.ToInt(data["count"])
order.IsSeat = cast.ToInt(data["is_seat"])
order.LoverSeat = cast.ToInt(data["loverSeat"])
2023-04-08 21:06:24 +08:00
order.MinPrice = int64(cast.ToFloat64(data["minPrice"]) * 100)
order.MaxPrice = int64(cast.ToFloat64(data["maxPrice"]) * 100)
2023-04-06 13:46:19 +08:00
if isAdd {
model.AddOrder(order)
2023-04-09 02:11:58 +08:00
select {
case s.newOrder <- order:
default:
log.Errorf("new order chan is full!")
2023-04-08 01:38:13 +08:00
}
2023-04-09 02:11:58 +08:00
2023-04-06 13:46:19 +08:00
} else {
model.UpdateOrder(order)
2023-04-09 02:11:58 +08:00
select {
case s.updateOrder <- order:
default:
log.Errorf("update order chan is full!")
2023-04-08 01:38:13 +08:00
}
2023-04-06 13:46:19 +08:00
}
}
2023-04-09 02:11:58 +08:00
}
func (s *SyncOrder) httpPost(requestUrl string) ([]byte, error) {
client := &http.Client{Timeout: 20 * time.Second}
//忽略https的证书
client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
p := url.Values{}
u, _ := url.Parse(requestUrl)
2023-04-06 13:46:19 +08:00
2023-04-09 02:11:58 +08:00
u.RawQuery = p.Encode()
req, err := http.NewRequest("POST", u.String(), nil)
if err != nil {
return nil, err
}
req.Header.Set("token", s.token)
2023-08-27 16:40:12 +08:00
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36")
2023-04-09 02:11:58 +08:00
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("%d:%s", resp.StatusCode, resp.Status)
}
return io.ReadAll(resp.Body)
2023-04-06 13:46:19 +08:00
}