This commit is contained in:
jiangyong27 2024-05-08 18:58:51 +08:00
parent a5855c525b
commit fc42f48319
7 changed files with 218 additions and 11 deletions

View File

@ -44,7 +44,7 @@ func initLog() {
log.SetLevel(log.Level(cfg.Server.LogLevel)) log.SetLevel(log.Level(cfg.Server.LogLevel))
} }
func main() { func main2() {
config.LoadServerConfig() config.LoadServerConfig()
config.LoadDadiConfig() config.LoadDadiConfig()
config.LoadHahaConfig() config.LoadHahaConfig()
@ -56,12 +56,14 @@ func main() {
work.Run() work.Run()
} }
func main2() { func main() {
config.LoadServerConfig() config.LoadServerConfig()
config.LoadDadiConfig() config.LoadDadiConfig()
config.LoadHahaConfig() config.LoadHahaConfig()
initLog() initLog()
global.InitDB() global.InitDB()
nowcar.Sync()
var ddf nowcar.Ddf
ddf.Sync()
} }

91
dao/ddf.go Normal file
View File

@ -0,0 +1,91 @@
package dao
import (
"film/global"
"film/model"
"gorm.io/gorm"
)
type DdfDao struct {
}
func NewDdfDao() *DdfDao {
return &DdfDao{}
}
func (d *DdfDao) TableName() string {
return "ddf"
}
func (d *DdfDao) Create(o *model.Ddf) (int64, error) {
res := global.GetDB().Table(d.TableName()).Create(o)
return o.Id, res.Error
}
func (d *DdfDao) Update(o *model.Ddf) error {
tx := global.GetDB().Table(d.TableName())
res := tx.Save(o)
return res.Error
}
func (d *DdfDao) GetStart(shopName, workstation string, startTime int64) (*model.Ddf, error) {
var u model.Ddf
tx := global.GetDB().Table(d.TableName())
tx = tx.Where("shop_name = ?", shopName)
tx = tx.Where("workstation = ?", workstation)
tx = tx.Where("start_time = ?", startTime)
tx = tx.Order("start_time DESC")
res := tx.First(&u)
if res.Error == gorm.ErrRecordNotFound {
return nil, nil
}
if res.Error != nil {
return nil, res.Error
}
return &u, nil
}
func (d *DdfDao) GetBusy(shopName, workstation string) (*model.Ddf, error) {
var u model.Ddf
tx := global.GetDB().Table(d.TableName())
tx = tx.Where("shop_name = ?", shopName)
tx = tx.Where("workstation = ?", workstation)
tx = tx.Where("start_time != 0")
tx = tx.Where("end_time = 0")
tx = tx.Order("start_time DESC")
res := tx.First(&u)
if res.Error == gorm.ErrRecordNotFound {
return nil, nil
}
if res.Error != nil {
return nil, res.Error
}
return &u, nil
}
func (d *DdfDao) QueryToday(shopName, workstation, day string) ([]*model.Ddf, error) {
var u []*model.Ddf
tx := global.GetDB().Table(d.TableName())
tx = tx.Where("shop_name = ?", shopName)
tx = tx.Where("workstation = ?", workstation)
tx = tx.Where("day = ?", day)
tx = tx.Where("start_time != 0")
tx = tx.Where("end_time != 0")
tx = tx.Order("start_time DESC")
res := tx.Find(&u)
if res.Error == gorm.ErrRecordNotFound {
return nil, nil
}
if res.Error != nil {
return nil, res.Error
}
return u, nil
}

2
go.mod
View File

@ -8,6 +8,7 @@ require (
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible
github.com/mitchellh/mapstructure v1.5.0 github.com/mitchellh/mapstructure v1.5.0
github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5 github.com/rifflock/lfshook v0.0.0-20180920164130-b9218ef580f5
github.com/silenceper/wechat v1.2.6
github.com/sirupsen/logrus v1.9.3 github.com/sirupsen/logrus v1.9.3
github.com/smbrave/goutil v0.0.0-20240307100306-1a2edd79979d github.com/smbrave/goutil v0.0.0-20240307100306-1a2edd79979d
github.com/spf13/cast v1.5.0 github.com/spf13/cast v1.5.0
@ -34,7 +35,6 @@ require (
github.com/pelletier/go-toml/v2 v2.0.6 // indirect github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/pkg/errors v0.9.1 // indirect github.com/pkg/errors v0.9.1 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/silenceper/wechat v1.2.6 // indirect
github.com/spf13/afero v1.9.3 // indirect github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/pflag v1.0.5 // indirect

11
model/ddf.go Normal file
View File

@ -0,0 +1,11 @@
package model
type Ddf struct {
Id int64
Day string
ShopName string
Workstation string
StartTime int64
EndTime int64
Amount int64
}

94
worker/nowcar/ddf.go Normal file
View File

@ -0,0 +1,94 @@
package nowcar
import (
"encoding/json"
"film/dao"
"film/model"
"fmt"
"github.com/silenceper/wechat/util"
log "github.com/sirupsen/logrus"
"github.com/smbrave/goutil"
"github.com/spf13/cast"
"strings"
"time"
)
type Ddf struct {
}
func (d *Ddf) Sync() {
shopIds := []int{826, 1693, 38, 1900, 372, 1613, 1372, 56, 1701, 45}
for _, shopId := range shopIds {
reqUrl := fmt.Sprintf("https://v9.ddfkj.com/mpserver/getwangdiandata.php?wdid=%d", shopId)
body, err := util.HTTPGet(reqUrl)
if err != nil {
log.Errorf("get http[%s] error :%s", reqUrl, err.Error())
continue
}
mp := make(map[string]interface{})
if err := json.Unmarshal(body, &mp); err != nil {
log.Errorf("json[%s] error:%s", string(body), err.Error())
continue
}
d.getDetail(shopId)
}
}
func (d *Ddf) getDetail(shopId int) {
reqUrl := fmt.Sprintf("https://v9.ddfkj.com/mpserver/getmcstate.php?wdid=%d", shopId)
body, err := util.HTTPGet(reqUrl)
if err != nil {
log.Errorf("get http[%s] error :%s", reqUrl, err.Error())
return
}
res := make([]interface{}, 0)
if err := json.Unmarshal(body, &res); err != nil {
log.Errorf("json[%s] error:%s", string(body), err.Error())
return
}
for _, r := range res {
obj := cast.ToStringMap(r)
status := cast.ToString(obj["zhuangtai"])
shopName := cast.ToString(obj["wangdian"])
workstation := cast.ToString(obj["address"])
if status == "维护中" {
continue
} else if status == "空闲" {
m1, _ := dao.NewDdfDao().GetBusy(shopName, workstation)
if m1 == nil {
continue
}
m1.EndTime = time.Now().Unix()
dao.NewDdfDao().Update(m1)
//完成提示
todays, _ := dao.NewDdfDao().QueryToday(shopName, workstation, time.Now().Format("2006-01-02"))
totalAmount := int64(0)
for _, day := range todays {
totalAmount += day.Amount
}
//订单完成提示
log.Errorf("[DDF] shopName=%s,workstation=%s,time=[%s],cost_minute=%d,money=%s,total=[%s/%d]",
m1.ShopName, m1.Workstation, goutil.TimeToDateTime(m1.StartTime),
(m1.EndTime-m1.StartTime)/60, goutil.FormatMoney(m1.Amount),
goutil.FormatMoney(totalAmount), len(todays))
} else if status == "忙碌中" {
yiyongtime := cast.ToString(obj["yiyongtime"])
useMinute := cast.ToFloat64(strings.TrimSuffix(yiyongtime, "分钟"))
m1, _ := dao.NewDdfDao().GetBusy(shopName, workstation)
if m1 != nil {
continue
}
m1 = new(model.Ddf)
m1.Day = time.Now().Format("2006-01-02")
m1.ShopName = shopName
m1.Workstation = workstation
m1.Amount = 600
m1.StartTime = time.Now().Unix() - int64(float64(60)*useMinute)
dao.NewDdfDao().Create(m1)
}
}
}

View File

@ -14,9 +14,12 @@ import (
"time" "time"
) )
func Sync() { type Nowcar struct {
}
devIds, err := getDevId() func (n *Nowcar) Sync() {
devIds, err := n.getDevId()
if err != nil { if err != nil {
return return
} }
@ -24,11 +27,11 @@ func Sync() {
if devId == "" { if devId == "" {
continue continue
} }
getInfo(devId) n.getInfo(devId)
} }
} }
func getInfo(devId string) ([]string, error) { func (n *Nowcar) getInfo(devId string) ([]string, error) {
reqUrl := "https://wx.wdfcar.com/NowZiZhuXiCheApp/userSiteOrder/getInfo" reqUrl := "https://wx.wdfcar.com/NowZiZhuXiCheApp/userSiteOrder/getInfo"
params := make(url.Values) params := make(url.Values)
@ -102,7 +105,7 @@ func getInfo(devId string) ([]string, error) {
totalAmount += day.Amount totalAmount += day.Amount
} }
//订单完成提示 //订单完成提示
log.Errorf("shopName=%s,workstation=%s,time=[%s],cost_minute=%d,money=%s,total=[%s/%d]", 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.ShopName, m2.Workstation, goutil.TimeToDateTime(m2.StartTime),
(m2.EndTime-m2.StartTime)/60, goutil.FormatMoney(m2.Amount), (m2.EndTime-m2.StartTime)/60, goutil.FormatMoney(m2.Amount),
goutil.FormatMoney(totalAmount), len(todays)) goutil.FormatMoney(totalAmount), len(todays))
@ -112,7 +115,8 @@ func getInfo(devId string) ([]string, error) {
return nil, nil return nil, nil
} }
func getDevId() ([]string, error) {
func (n *Nowcar) getDevId() ([]string, error) {
reqUrl := "https://wx.wdfcar.com/NowZiZhuXiCheApp/userSiteOrder/nearSite" reqUrl := "https://wx.wdfcar.com/NowZiZhuXiCheApp/userSiteOrder/nearSite"
params := make(url.Values) params := make(url.Values)

View File

@ -49,8 +49,13 @@ func (w *Worker) Run() {
hahaSyncer.Sync() hahaSyncer.Sync()
}) })
var nc nowcar.Nowcar
var ddf nowcar.Ddf
cron.Every(10).Seconds().Do(func() { cron.Every(10).Seconds().Do(func() {
nowcar.Sync() nc.Sync()
})
cron.Every(10).Seconds().Do(func() {
ddf.Sync()
}) })
cron.StartAsync() cron.StartAsync()