film/worker/worker.go

189 lines
6.6 KiB
Go
Raw Permalink Normal View History

2023-04-09 02:11:58 +08:00
package worker
import (
"film/base/util"
"film/config"
"film/model"
2023-04-09 11:38:19 +08:00
"film/worker/common"
2023-04-09 02:11:58 +08:00
"film/worker/dadi"
"film/worker/haha"
"fmt"
log "github.com/sirupsen/logrus"
"github.com/smbrave/goutil"
"gitlab.com/jiangyong27/gobase/wxapi"
"strings"
"time"
)
type Worker struct {
2023-04-09 11:38:19 +08:00
qyClient *wxapi.WxQiye
2023-04-09 17:47:46 +08:00
qyImpClient *wxapi.WxQiye
2023-04-09 11:38:19 +08:00
dadiProcessor common.Processor
hahaProcessor *haha.Processor
2023-04-09 17:47:46 +08:00
notifyMap map[string]int64
2023-04-09 02:11:58 +08:00
}
func (w *Worker) initQyWeixin() {
cfg := config.GetConfig()
w.qyClient = wxapi.NewQiye(&wxapi.QiyeConfig{
Corpid: cfg.Weixin.QiyeAppid,
Secret: cfg.Weixin.Qiyesecret,
Sender: cfg.Weixin.QiyeAgent,
})
2023-04-09 17:47:46 +08:00
w.qyImpClient = wxapi.NewQiye(&wxapi.QiyeConfig{
Corpid: "ww288920bd3e8c92b2",
Secret: "-RtxFJMwM0j_4QJbHrstHuLSIzMU1Q_euJSeWmhtOTM",
Sender: "1000002",
})
2023-04-09 02:11:58 +08:00
}
2023-04-09 11:38:19 +08:00
func (w *Worker) getOrderMesasge(order *model.Order) []string {
message := make([]string, 0)
message = append(message, "【哈哈票订单信息】")
message = append(message, fmt.Sprintf("城市:%s", order.CityName))
message = append(message, fmt.Sprintf("影院:%s", order.CinemaName))
message = append(message, fmt.Sprintf("影片:%s", order.MovieName))
message = append(message, fmt.Sprintf("影厅:%s", order.Ting))
message = append(message, fmt.Sprintf("时间:%s", time.Unix(order.ShowTime, 0).Format("2006-01-02 15:04")))
message = append(message, fmt.Sprintf("座位:%s%s%s", order.Seats,
goutil.If(order.IsSeat == 3, "可以调座", "不可调座"),
goutil.If(order.LoverSeat == 1, "情侣座", "普通座")))
message = append(message, fmt.Sprintf("原价:%.2f", float64(order.MaoyanPrice)/100))
2023-04-09 12:22:31 +08:00
message = append(message, fmt.Sprintf("编号:%s", order.OrderNo))
2023-04-09 11:38:19 +08:00
message = append(message, fmt.Sprintf("总金额:%.2f%d", float64(order.TotalPrice)/100, order.SeatNum))
message = append(message, fmt.Sprintf("报价范围:%.2f~%.2f", float64(order.MinPrice)/100, float64(order.MaxPrice)/100))
message = append(message, fmt.Sprintf("支付时间:%s", time.Unix(order.PayTime, 0).Format("2006-01-02 15:04")))
message = append(message, fmt.Sprintf("创建时间:%s", time.Unix(order.OrderCreateTime, 0).Format("2006-01-02 15:04")))
message = append(message, fmt.Sprintf("入库时间:%s", time.Unix(order.CreateTime, 0).Format("2006-01-02 15:04")))
return message
}
func (w *Worker) sendOrderMessage(order *model.Order, message string) {
messages := w.getOrderMesasge(order)
messages = append(messages, "\n", message)
w.qyClient.SendText(config.GetConfig().Weixin.Receiver, strings.Join(messages, "\n"))
}
func (w *Worker) processorDadi(order *model.Order) {
checkInfo, err := w.dadiProcessor.CheckOrder(order)
if err != nil {
log.Errorf("check orrder[%s] error : %s", order.String(), err.Error())
return
}
2023-04-09 13:01:04 +08:00
cfg := config.GetDadiConfig()
2023-04-09 11:38:19 +08:00
bidStatus := false
2023-04-09 15:20:16 +08:00
realCost := int64(float64(checkInfo.TotalRealPrice) * cfg.Discount)
2023-04-09 11:38:19 +08:00
realUnitCost := realCost / int64(order.SeatNum)
bidPrice := realUnitCost + config.GetDadiConfig().BidProfit
if bidPrice < order.MaxPrice {
if err := w.hahaProcessor.BidPrice(order.Id, bidPrice); err != nil {
log.Errorf("BidPrice order[%s] error[%s]", order.String(), err.Error())
if err := w.dadiProcessor.CancelOrder(order, checkInfo); err != nil {
2023-04-09 12:22:31 +08:00
log.Errorf("cancel order[%s] error[%s]", order.OrderNo, err.Error())
2023-04-09 11:38:19 +08:00
}
message := w.getOrderMesasge(order)
message = append(message, "\n", fmt.Sprintf("竞价失败:%s", err.Error()))
w.qyClient.SendText(config.GetConfig().Weixin.Receiver, strings.Join(message, "\n"))
return
}
bidStatus = true
}
// 发送信息
message := w.getOrderMesasge(order)
message = append(message, "\n")
message = append(message, "【大地订单信息】")
message = append(message, fmt.Sprintf("订单原价:%s", util.MoneyFen(checkInfo.TotalOriginPrice)))
message = append(message, fmt.Sprintf("优惠金额:%s", util.MoneyFen(checkInfo.CouponPrice)))
message = append(message, fmt.Sprintf("应付金额:%s", util.MoneyFen(checkInfo.TotalRealPrice)))
message = append(message, fmt.Sprintf("实际成本:%s", util.MoneyFen(realCost)))
message = append(message, fmt.Sprintf("实际单价:%s", util.MoneyFen(realUnitCost)))
message = append(message, "\n")
message = append(message, "【竞价信息】")
message = append(message, fmt.Sprintf("状态:%s", goutil.If(bidStatus, "竞价中", "不可竞价")))
2023-04-09 13:10:26 +08:00
message = append(message, fmt.Sprintf("出价:%s", util.MoneyFen(bidPrice)))
2023-04-09 11:38:19 +08:00
2023-04-09 17:47:46 +08:00
w.qyClient.SendText(config.GetConfig().Weixin.Receiver, strings.Join(message, "\n"))
2023-04-09 11:38:19 +08:00
}
2023-04-09 15:59:56 +08:00
func (w *Worker) processBidWin() {
bidOrders, err := w.hahaProcessor.QueryBidWin()
if err != nil {
return
}
for _, o := range bidOrders {
order, err := model.GetOrder(o.OrderId)
if err != nil {
log.Errorf("get order error : %s", err.Error())
continue
}
2023-04-09 17:47:46 +08:00
nowTime := time.Now().Unix()
if v, ok := w.notifyMap[order.OrderNo]; ok && nowTime-v < 300 {
continue
}
2023-04-09 15:59:56 +08:00
message := w.getOrderMesasge(order)
message = append(message, "\n")
message = append(message, "【竞价成功】")
message = append(message, fmt.Sprintf("出价:%s", util.MoneyFen(o.BidPrice)))
message = append(message, fmt.Sprintf("时间:%s", goutil.TimeToDateTime(o.BidTime)))
2023-04-09 17:47:46 +08:00
if err := w.qyImpClient.SendText(config.GetConfig().Weixin.Receiver, strings.Join(message, "\n")); err == nil {
w.notifyMap[order.OrderNo] = nowTime
}
2023-04-09 15:59:56 +08:00
}
}
2023-04-09 02:11:58 +08:00
func (w *Worker) Run() {
w.initQyWeixin()
newOrderChan := make(chan *model.Order, 100000)
updateOrderChan := make(chan *model.Order, 100000)
hahaSyncer := haha.NewSyncOrder(&haha.SyncOrderConfig{
2023-04-09 13:05:52 +08:00
Token: config.GetHahaConfig().Token,
NewOrder: newOrderChan,
UpdateOrder: updateOrderChan,
SyncInterval: config.GetHahaConfig().SyncInterval,
2023-04-09 02:11:58 +08:00
})
hahaSyncer.Sync()
2023-04-09 17:47:46 +08:00
w.notifyMap = make(map[string]int64)
2023-04-09 02:11:58 +08:00
2023-04-09 13:01:04 +08:00
w.hahaProcessor = haha.NewProcessor(&haha.ProcessorConfig{Token: config.GetHahaConfig().Token})
if config.GetDadiConfig().Enable {
dadiProcessor, err := dadi.NewProcessor(&dadi.ProcessorConfig{Token: config.GetDadiConfig().Token})
if err != nil {
panic(err)
}
w.dadiProcessor = dadiProcessor
2023-04-09 02:11:58 +08:00
}
2023-04-09 13:01:04 +08:00
2023-04-09 17:47:46 +08:00
//order, _ := model.GetOrder(594315)
//_, err := w.dadiProcessor.CreateOrder(order)
//if err != nil {
//log.Errorf("create order[%s] error [%s]", order.String(), err.Error())
//}
//newOrderChan <- order
2023-04-09 15:59:56 +08:00
bidUploadTimer := time.NewTicker(10 * time.Second)
2023-04-09 02:11:58 +08:00
for {
select {
case order := <-newOrderChan:
if !strings.Contains(order.CinemaName, "大地影院") {
continue
}
2023-04-09 12:16:09 +08:00
if config.GetDadiConfig().Enable {
w.processorDadi(order)
}
2023-04-09 02:11:58 +08:00
case updateOrder := <-updateOrderChan:
2023-04-09 12:16:09 +08:00
log.Tracef("%s update", updateOrder.String())
2023-04-09 15:59:56 +08:00
case <-bidUploadTimer.C:
w.processBidWin()
2023-04-09 02:11:58 +08:00
}
}
}