film/worker/worker.go

189 lines
6.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package worker
import (
"film/base/util"
"film/config"
"film/model"
"film/worker/common"
"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 {
qyClient *wxapi.WxQiye
qyImpClient *wxapi.WxQiye
dadiProcessor common.Processor
hahaProcessor *haha.Processor
notifyMap map[string]int64
}
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,
})
w.qyImpClient = wxapi.NewQiye(&wxapi.QiyeConfig{
Corpid: "ww288920bd3e8c92b2",
Secret: "-RtxFJMwM0j_4QJbHrstHuLSIzMU1Q_euJSeWmhtOTM",
Sender: "1000002",
})
}
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))
message = append(message, fmt.Sprintf("编号:%s", order.OrderNo))
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
}
cfg := config.GetDadiConfig()
bidStatus := false
realCost := int64(float64(checkInfo.TotalRealPrice) * cfg.Discount)
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 {
log.Errorf("cancel order[%s] error[%s]", order.OrderNo, err.Error())
}
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, "竞价中", "不可竞价")))
message = append(message, fmt.Sprintf("出价:%s", util.MoneyFen(bidPrice)))
w.qyClient.SendText(config.GetConfig().Weixin.Receiver, strings.Join(message, "\n"))
}
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
}
nowTime := time.Now().Unix()
if v, ok := w.notifyMap[order.OrderNo]; ok && nowTime-v < 300 {
continue
}
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)))
if err := w.qyImpClient.SendText(config.GetConfig().Weixin.Receiver, strings.Join(message, "\n")); err == nil {
w.notifyMap[order.OrderNo] = nowTime
}
}
}
func (w *Worker) Run() {
w.initQyWeixin()
newOrderChan := make(chan *model.Order, 100000)
updateOrderChan := make(chan *model.Order, 100000)
hahaSyncer := haha.NewSyncOrder(&haha.SyncOrderConfig{
Token: config.GetHahaConfig().Token,
NewOrder: newOrderChan,
UpdateOrder: updateOrderChan,
SyncInterval: config.GetHahaConfig().SyncInterval,
})
hahaSyncer.Sync()
w.notifyMap = make(map[string]int64)
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
}
//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
bidUploadTimer := time.NewTicker(10 * time.Second)
for {
select {
case order := <-newOrderChan:
if !strings.Contains(order.CinemaName, "大地影院") {
continue
}
if config.GetDadiConfig().Enable {
w.processorDadi(order)
}
case updateOrder := <-updateOrderChan:
log.Tracef("%s update", updateOrder.String())
case <-bidUploadTimer.C:
w.processBidWin()
}
}
}