film/worker/nowcar/nowcar.go

151 lines
3.8 KiB
Go

package nowcar
import (
"bytes"
"film/dao"
"film/model"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/smbrave/goutil"
"github.com/tidwall/gjson"
"io"
"net/http"
"net/url"
"time"
)
type Nowcar struct {
}
func (n *Nowcar) Sync() {
devIds, err := n.getDevId()
if err != nil {
return
}
for _, devId := range devIds {
if devId == "" {
continue
}
n.getInfo(devId)
}
}
func (n *Nowcar) getInfo(devId string) ([]string, error) {
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
m.Day = time.Now().Format("2006-01-02")
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()
m2.Amount = goutil.If(m2.EndTime-m2.StartTime <= 600, 660,
(m2.EndTime-m2.StartTime-600)+660)
err = dao.NewNowCarDao().Update(m2)
if err != nil {
log.Errorf("db error :%s", err.Error())
continue
}
todays, err := dao.NewNowCarDao().QueryToday(shopName, workstation, time.Now().Format("2006-01-02"))
totalAmount := int64(0)
for _, day := range todays {
totalAmount += day.Amount
}
//订单完成提示
log.Errorf("[NowCar] shopName=%s,workstation=%s,time=[%s],cost_minute=%d,money=%s,total=[%s/%d]",
m2.ShopName, m2.Workstation, goutil.TimeToDateTime(m2.StartTime),
(m2.EndTime-m2.StartTime)/60, goutil.FormatMoney(m2.Amount),
goutil.FormatMoney(totalAmount), len(todays))
}
}
return nil, nil
}
func (n *Nowcar) getDevId() ([]string, error) {
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
}