Commit de4c9c78 by 朱继来

添加团购任务

parent 458d90c1
......@@ -4,4 +4,5 @@ send_mail="http://crm.liexin.net/api/sendCaiGouEmailByOrderAdmin"
[Ding_msg]
webhook="https://oapi.dingtalk.com/robot/send?access_token=ec03b2cba0d62ef0bd7b9ebb16aaf69b5f626658dfb6b194045381324b0786cf"
jingDiao="https://oapi.dingtalk.com/robot/send?access_token=eaeae1253e9c125a053bc8a7bcfec5096a23625a98044eaa346591863449ba17"
\ No newline at end of file
jingDiao="https://oapi.dingtalk.com/robot/send?access_token=eaeae1253e9c125a053bc8a7bcfec5096a23625a98044eaa346591863449ba17"
tuangou_webhook="https://oapi.dingtalk.com/robot/send?access_token=a13d6128de5f918fc0f0a4cef70cab8b1962fca709649f7856aea4b1ca4dddf5"
\ No newline at end of file
......@@ -9,3 +9,8 @@ exchange="ichunt_order_msg"
type="direct"
dns="amqp://guest:guest@192.168.2.232:5672/"
[redis_config]
host="192.168.1.235"
password="icDb29mLy2s"
port="6379"
package Order
import (
_"database/sql"
_"fmt"
"github.com/ichunt2019/logger"
"go-queue-server/dal/db"
"go-queue-server/util"
_"time"
)
func initDb(dns string) (err error) {
err = db.Init(dns)
if err != nil {
return
}
return
}
func UpdateStatus(order_id int, status_extend int) (err error) {
initDb(util.Configs.Liexin_databases.Dns) //初始化db
_, err1 := db.DB.Exec("update lie_order set status_extend = ?, wms_syn = ? where order_id = ?", status_extend, 1, order_id)
if err1 != nil {
logger.Fatal("数据库操作失败")
return err1
}
return err
}
\ No newline at end of file
package UserGroup
import (
"database/sql"
_"fmt"
"github.com/ichunt2019/logger"
"go-queue-server/dal/db"
"go-queue-server/util"
"encoding/json"
)
type UserGroup struct {
Id int `db:"id"`
UserId int `db:"user_id"`
Account string `db:"account"`
OrderId int `db:"order_id"`
OrderSn string `db:"order_sn"`
Status int `db:"status"`
IsAssign int `db:"is_assign"`
LimitedTime int `db:"limited_time"`
CreateTime int `db:"create_time"`
}
func initDb(dns string) (err error) {
err = db.Init(dns)
if err != nil {
return
}
return
}
// 单行查询
func QueryRow(order_id int) []byte {
initDb(util.Configs.Liexin_databases.Dns) //初始化db
var userGroup UserGroup
err := db.DB.Get(&userGroup, "select * from lie_user_group where order_id = ?", order_id)
if err == sql.ErrNoRows {
logger.Info("没有获取到团购订单,order_id:%d", order_id)
return nil
}
res, _ := json.Marshal(userGroup)
return res
}
// 更新参团状态
func UpdateStatus(order_id int) (err error) {
initDb(util.Configs.Liexin_databases.Dns) //初始化db
_, err1 := db.DB.Exec("update lie_user_group set status = ?, is_assign = ? where order_id = ?", 1, 1, order_id)
if err1 != nil {
logger.Fatal("数据库操作失败")
return err1
}
return err
}
\ No newline at end of file
package UserGroupJoin
import (
"github.com/ichunt2019/logger"
"go-queue-server/dal/db"
"go-queue-server/util"
"time"
)
func initDb(dns string) (err error) {
err = db.Init(dns)
if err != nil {
return
}
return
}
// 新增虚拟参团人员
func InsertGroupJoin(group_id int, order_id int, account string) (err error) {
initDb(util.Configs.Liexin_databases.Dns) //初始化db
current := time.Now().Unix()
_, err1 := db.DB.Exec("insert into lie_user_group_join (group_id, account, order_id, join_time) values (?, ?, ?, ?)", group_id, account, order_id, current)
if err1 != nil {
logger.Fatal("数据库操作失败")
return err1
}
return err
}
\ No newline at end of file
......@@ -4,8 +4,10 @@ go 1.12
require (
github.com/BurntSushi/toml v0.3.1
github.com/garyburd/redigo v1.6.0 // indirect
github.com/gin-gonic/gin v1.5.0
github.com/go-sql-driver/mysql v1.4.1
github.com/gomodule/redigo v2.0.0+incompatible
github.com/ichunt2019/go-msgserver v1.0.4
github.com/ichunt2019/logger v1.0.5
github.com/jmoiron/sqlx v1.2.0
......
......@@ -10,6 +10,8 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/garyburd/redigo v1.6.0 h1:0VruCpn7yAIIu7pWVClQC8wxCJEcG3nyzpMSHKi1PQc=
github.com/garyburd/redigo v1.6.0/go.mod h1:NR3MbYisc3/PwhQ00EMzDiPmrwpPxAn5GI05/YaO1SY=
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.5.0 h1:fi+bqFAx/oLK54somfCtEZs9HeH1LHVoEPUgARpTqyc=
github.com/gin-gonic/gin v1.5.0/go.mod h1:Nd6IXA8m5kNZdNEHMBd93KT+mdY3+bewLgRvmCsR2Do=
......@@ -27,6 +29,8 @@ github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7a
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/gomodule/redigo v2.0.0+incompatible h1:K/R+8tc58AaqLkqG2Ol3Qk+DR/TlNuhuh457pBFPtt0=
github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/ichunt2019/go-msgserver v1.0.4 h1:4BsRE1Ca4J0h9xyDqUF0pRMpMjDC5G7023hESrzszsY=
github.com/ichunt2019/go-msgserver v1.0.4/go.mod h1:fWAvbry0W9nhmkqgT2agwRqYCWlguUJXgy2rgoWOUmA=
......
package main
import (
"encoding/json"
"flag"
_"fmt"
"github.com/ichunt2019/logger"
"go-queue-server/dal/db"
"go-queue-server/util"
"go-queue-server/util/ding"
_"go-queue-server/util/ding"
"strconv"
"time"
"github.com/garyburd/redigo/redis"
"go-queue-server/dal/order/OrderActionLog"
"go-queue-server/dal/order/Order"
"go-queue-server/dal/order/UserGroup"
"go-queue-server/dal/order/UserGroupJoin"
"math/rand"
)
func initDb(dns string) (err error) {
err = db.Init(dns)
if err != nil {
......@@ -27,6 +30,8 @@ func initDb(dns string) (err error) {
var ConfigDir string
var LogDir string
var RedisConn redis.Conn
var OrderChan = make(chan int, 100)
// 解析命令行参数
func initArgs() {
......@@ -37,40 +42,182 @@ func initArgs() {
flag.Parse()
}
// redis连接
func redis_connect(config *util.Config) redis.Conn {
redis_server := config.Redis_config.Host + ":" + config.Redis_config.Port
option := redis.DialPassword(config.Redis_config.Password)
conn, redis_err := redis.Dial("tcp", redis_server, option)
func main() {
if redis_err != nil {
logger.Info("redis connect server failed:", redis_err)
return conn
}
return conn
}
func main() {
initArgs()
//初始化配置文件
util.Init(ConfigDir)
//
logConfig := make(map[string]string)
logConfig["log_path"] = LogDir+"order/orderSendPurchaseEmail"
logConfig["log_path"] = LogDir+"order/tuangouOrder"
logConfig["log_chan_size"] = "1000"
logger.InitLogger("file",logConfig)
logger.Init()
//初始化db
initDb(util.Configs.Liexin_databases.Dns)
RedisConn = redis_connect(util.Configs)
t1 := time.NewTimer(time.Second * 5)
t2 := time.NewTimer(time.Second * 8)
t1 := time.NewTimer(time.Minute * 5)
for {
select {
case order_id, ok := <- OrderChan: // 从管道获取数据
if ok {
//fmt.Printf("管道获取:%d\n", order_id)
res := updateData(order_id) // 更新数据
// 钉钉通知 默认参数
var mobile []string = make([]string, 0)
var isAtAll bool = false
var msg_text string
if res == nil { // 更新成功则删除redis数据
_, err := RedisConn.Do("ZREM", "user_tuangou_order", order_id)
if err != nil {
logger.Info("删除redis团购订单ID失败:%d,失败原因:%s", order_id, err.Error())
} else {
logger.Info("删除redis团购订单ID成功:%d", order_id)
}
msg_text = "系统默认分配参团成功,订单ID:" + strconv.Itoa(order_id)
} else {
logger.Info("更新团购订单失败,订单ID:%d,失败原因:%s", order_id, res)
msg_text = "系统默认分配参团失败,订单ID:" + strconv.Itoa(order_id)
}
ding.Send(util.Configs.Ding_msg.TuangouWebhook, "用户团购下单告警", msg_text, mobile, isAtAll)
}
case <-t1.C:
if (len(OrderChan) == 0) { // 若管道为空,则获取
assign()
}
//t1.Reset(time.Second * 1)
t1.Reset(time.Minute * 5)
}
}
}
case <-t1.C:
println("5s timer")
t1.Reset(time.Second * 5)
// 获取团购订单ID,放入管道
func assign() {
// 执行一个有序zset插入
//for i := 1; i < 20; i++ {
// limited_time := 1581110000 + i * 500
// _, err := RedisConn.Do("ZADD", "user_tuangou_order", limited_time, 1 + i)
// if err != nil {
// fmt.Println("redis set failed:", err)
// }
//}
current_time := time.Now().Unix() // 当前时间戳
score_start := current_time - 5 * 60
score_end := current_time + 5 * 60
// 获取时间戳最小的十个订单
tuangou_order, err := redis.Values(RedisConn.Do("ZRANGEBYSCORE", "user_tuangou_order", score_start, score_end))
if err != nil {
logger.Info("redis get failed:", err)
return
}
case <-t2.C:
println("8s timer")
t2.Reset(time.Second * 8)
if tuangou_order != nil {
for _, v := range tuangou_order {
value, _ := v.([]byte)
order_id, _ := strconv.Atoi(string(value))
//fmt.Println(order_id)
// 写入到chan
select {
case OrderChan <- order_id:
default:
}
}
}
}
// 修改DB
func updateData(order_id int) (err error) {
// 获取团购ID
group := UserGroup.QueryRow(order_id)
if group == nil {
return
}
//fmt.Println(string(group))
var group_info *UserGroup.UserGroup
json.Unmarshal([]byte(group), &group_info)
group_id := group_info.Id
account := virtualAccount()
err = UserGroupJoin.InsertGroupJoin(group_id, order_id, account) // 新增参团表
if err != nil {
logger.Info("user_group_join insert failed:", err)
return
}
err = UserGroup.UpdateStatus(order_id) // 更新团购表
if err != nil {
logger.Info("user_group status update failed:", err)
return
}
err = Order.UpdateStatus(order_id, 16) // 更新订单状态
if err != nil {
logger.Info("order status update failed:", err)
return
}
err = OrderActionLog.Insert(order_id, 1000, 2, "系统默认分配参团成功") // 操作日志
if err != nil {
logger.Info("order_action_log insert failed:", err)
return
}
return
}
// 生成虚拟账号
func virtualAccount() string {
var prefixArr = []string{"132", "134", "135", "136", "137", "155", "199"} // 前缀集合
var prefix string
var mid = "******" // 中间六位用*表示
var suffix string
var account string
len := len(prefixArr)
rand_num := randNum(len)
prefix = prefixArr[rand_num]
for i := 0; i < 2; i++ {
suffix += strconv.Itoa(randNum(10 - i))
}
account = prefix + mid + suffix
return account
}
// 随机数
func randNum(num int) int {
rand.Seed(time.Now().UnixNano())
return rand.Intn(num)
}
}
\ No newline at end of file
......@@ -41,7 +41,7 @@ func SendPurchaseEmail(data string) (err error) {
var isAtAll bool = false
if err != nil {
ding.Send("邮件接口请求失败,原因:" + err.Error(), mobile, isAtAll)
ding.Send(util.Configs.Ding_msg.Webhook,"监控告警", "邮件接口请求失败,原因:" + err.Error(), mobile, isAtAll)
logger.Info(err.Error())
return err
}
......@@ -57,11 +57,11 @@ func SendPurchaseEmail(data string) (err error) {
err = OrderActionLog.Insert(order_id, operator_id, 2, operator_event)
if err != nil {
ding.Send("添加操作日志失败", mobile, isAtAll)
ding.Send(util.Configs.Ding_msg.Webhook,"监控告警","添加操作日志失败", mobile, isAtAll)
return err
}
ding.Send("订单ID:" + strconv.Itoa(order_id) + "," + operator_event, mobile, isAtAll)
ding.Send(util.Configs.Ding_msg.Webhook, "监控告警", "订单ID:" + strconv.Itoa(order_id) + "," + operator_event, mobile, isAtAll)
// 推送ERP
api_url := util.Configs.Api_domain.ApiUrl + "order/sysiteminfo"
......@@ -73,7 +73,7 @@ func SendPurchaseEmail(data string) (err error) {
err = http_post(api_url, params, api_header_map)
if err != nil {
ding.Send("ERP同步接口请求失败,原因:" + err.Error(), mobile, isAtAll)
ding.Send(util.Configs.Ding_msg.Webhook, "监控告警", "ERP同步接口请求失败,原因:" + err.Error(), mobile, isAtAll)
logger.Info(err.Error())
return err
}
......
......@@ -12,6 +12,7 @@ type Config struct{
Crm_domain *SendMail
Ding_msg *Ding
Api_domain *ApiDomain
Redis_config *RedisConn
}
......@@ -34,12 +35,19 @@ type SendMail struct{
type Ding struct {
Webhook string `toml:"webhook"`
JingDiao string `toml:"jingDiao"`
TuangouWebhook string `toml:"tuangou_webhook"`
}
type ApiDomain struct {
ApiUrl string `toml:"api_url"`
}
type RedisConn struct {
Host string `toml:"host"`
Password string `toml:"password"`
Port string `toml:"port"`
}
var Configs *Config =new (Config)
func Init(ConfigDir string){
......@@ -57,4 +65,5 @@ func Init(ConfigDir string){
//
//fmt.Printf("%+v",Configs.Crm_domain)
//fmt.Printf("%+v",Configs.Rabbitmq_ichunt)
//fmt.Printf("%+v",Configs.Redis_config)
}
......@@ -2,7 +2,6 @@ package ding
import (
_"fmt"
"go-queue-server/util"
"net/http"
"strings"
"encoding/json"
......@@ -31,10 +30,10 @@ type JosnResp struct {
Errmsg string `json:"errmsg"`
}
func Send(textMsg string, mobiles []string, isAtAll bool) (jsonStr string) {
func Send(ding_url string, ding_tag string, textMsg string, mobiles []string, isAtAll bool) (jsonStr string) {
var msg Msg
msg = Msg{Msgtype:"text"}
msg.Text.Content = "监控告警: " + textMsg // 固定标签 + 文本
msg.Text.Content = ding_tag + ":" + textMsg // 固定标签 + 文本
msg.At.AtMobiles = mobiles
msg.At.IsAtAll = isAtAll;
......@@ -47,8 +46,6 @@ func Send(textMsg string, mobiles []string, isAtAll bool) (jsonStr string) {
// "content": "`+ msg + `"
// }
// }`
ding_url := util.Configs.Ding_msg.Webhook // 钉钉请求接口
client := &http.Client{}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment