film/worker/nowcar/nowcar.go

151 lines
3.8 KiB
Go
Raw Permalink Normal View History

2024-04-27 23:12:44 +08:00
package nowcar
import (
"bytes"
"film/dao"
"film/model"
"fmt"
log "github.com/sirupsen/logrus"
2024-04-27 23:43:21 +08:00
"github.com/smbrave/goutil"
2024-04-27 23:12:44 +08:00
"github.com/tidwall/gjson"
"io"
"net/http"
"net/url"
"time"
)
2024-05-08 18:58:51 +08:00
type Nowcar struct {
}
func (n *Nowcar) Sync() {
2024-04-27 23:12:44 +08:00
2024-05-08 18:58:51 +08:00
devIds, err := n.getDevId()
2024-04-27 23:12:44 +08:00
if err != nil {
return
}
for _, devId := range devIds {
if devId == "" {
continue
}
2024-05-08 18:58:51 +08:00
n.getInfo(devId)
2024-04-27 23:12:44 +08:00
}
}
2024-05-08 18:58:51 +08:00
func (n *Nowcar) getInfo(devId string) ([]string, error) {
2024-04-27 23:12:44 +08:00
reqUrl := "https://wx.wdfcar.com/NowZiZhuXiCheApp/userSiteOrder/getInfo"
params := make(url.Values)
params.Set("user_lng", "106.55072784423828")
params.Set("user_lat", "29.56471061706543")
params.Set("dev_id", devId)
params.Set("lt-id", "1811")
params.Set("lt-token", "c5e139a7e118b346149750489c57560b")
resp, err := http.Post(reqUrl, "application/x-www-form-urlencoded",
bytes.NewBuffer([]byte(params.Encode())))
if err != nil {
log.Error("http poset error :%s", err.Error())
return nil, err
}
defer resp.Body.Close()
rspbody, _ := io.ReadAll(resp.Body)
code := gjson.GetBytes(rspbody, "code").Int()
if code != 0 {
log.Errorf("devId:%s rspbody error :%s", devId, string(rspbody))
return nil, fmt.Errorf("code is :%d", code)
}
data := gjson.GetBytes(rspbody, "data")
shopName := data.Get("addr_name").String()
doors := data.Get("doors").Array()
m := new(model.Nowcar)
m.ShopName = shopName
for _, door := range doors {
workstation := door.Get("number").String()
status := door.Get("status").String()
if status != "空闲" {
startTime, _ := time.ParseInLocation("2006-01-02 15:04:05", status, time.Local)
m1, err := dao.NewNowCarDao().GetStart(shopName, workstation, startTime.Unix())
if err != nil {
log.Errorf("db error :%s", err.Error())
continue
}
if m1 != nil {
continue
}
m.Id = 0
2024-04-27 23:18:04 +08:00
m.Day = time.Now().Format("2006-01-02")
2024-04-27 23:12:44 +08:00
m.Workstation = workstation
m.StartTime = startTime.Unix()
dao.NewNowCarDao().Create(m)
} else {
m2, err := dao.NewNowCarDao().GetBusy(shopName, workstation)
if err != nil {
log.Errorf("db error :%s", err.Error())
continue
}
if m2 == nil || m2.EndTime != 0 {
continue
}
m2.EndTime = time.Now().Unix()
2024-04-28 00:03:57 +08:00
m2.Amount = goutil.If(m2.EndTime-m2.StartTime <= 600, 660,
2024-04-28 00:19:10 +08:00
(m2.EndTime-m2.StartTime-600)+660)
2024-04-27 23:12:44 +08:00
err = dao.NewNowCarDao().Update(m2)
if err != nil {
log.Errorf("db error :%s", err.Error())
2024-04-27 23:43:21 +08:00
continue
2024-04-27 23:12:44 +08:00
}
2024-04-30 22:36:41 +08:00
todays, err := dao.NewNowCarDao().QueryToday(shopName, workstation, time.Now().Format("2006-01-02"))
totalAmount := int64(0)
for _, day := range todays {
totalAmount += day.Amount
}
2024-04-27 23:43:21 +08:00
//订单完成提示
2024-05-11 17:36:57 +08:00
log.Infof("[NowCar] shopName=%s,workstation=%s,time=[%s],cost_minute=%d,money=%s,total=[%s/%d]",
2024-04-27 23:43:21 +08:00
m2.ShopName, m2.Workstation, goutil.TimeToDateTime(m2.StartTime),
2024-04-30 22:36:41 +08:00
(m2.EndTime-m2.StartTime)/60, goutil.FormatMoney(m2.Amount),
goutil.FormatMoney(totalAmount), len(todays))
2024-04-27 23:12:44 +08:00
}
}
return nil, nil
}
2024-05-08 18:58:51 +08:00
func (n *Nowcar) getDevId() ([]string, error) {
2024-04-27 23:12:44 +08:00
reqUrl := "https://wx.wdfcar.com/NowZiZhuXiCheApp/userSiteOrder/nearSite"
params := make(url.Values)
params.Set("user_lng", "106.55072784423828")
params.Set("user_lat", "29.56471061706543")
resp, err := http.Post(reqUrl, "application/x-www-form-urlencoded",
bytes.NewBuffer([]byte(params.Encode())))
if err != nil {
log.Error("http poset error :%s", err.Error())
return nil, err
}
defer resp.Body.Close()
rspbody, _ := io.ReadAll(resp.Body)
code := gjson.GetBytes(rspbody, "code").Int()
if code != 0 {
log.Errorf("rspbody error :%s", string(rspbody))
return nil, fmt.Errorf("code is :%d", code)
}
devids := make([]string, 0)
results := gjson.GetBytes(rspbody, "data").Array()
for _, res := range results {
devId := res.Get("dev_id").String()
shopName := res.Get("addr_name").String()
log.Infof("shop=%s devid=%s", shopName, devId)
devids = append(devids, devId)
}
return devids, nil
}