Commit 51ba1c9a by mushishixian

有货提醒任务

parent 9b78daed
...@@ -14,6 +14,8 @@ fk_company="https://oapi.dingtalk.com/robot/send?access_token=231bee055924688720 ...@@ -14,6 +14,8 @@ fk_company="https://oapi.dingtalk.com/robot/send?access_token=231bee055924688720
[Api_domain] [Api_domain]
api_url="http://api.liexin.com/" api_url="http://api.liexin.com/"
goods_url="http://192.168.2.232:60004/synchronization"
message_url="http://api.liexin.com/msg/sendMessageByAuto"
[Fengkong_domain] [Fengkong_domain]
go_url="http://192.168.2.179:8000" go_url="http://192.168.2.179:8000"
\ No newline at end of file
...@@ -4,17 +4,20 @@ go 1.12 ...@@ -4,17 +4,20 @@ go 1.12
require ( require (
github.com/BurntSushi/toml v0.3.1 github.com/BurntSushi/toml v0.3.1
github.com/garyburd/redigo v1.6.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gin-gonic/gin v1.5.0 github.com/garyburd/redigo v1.6.0
github.com/go-sql-driver/mysql v1.4.1 github.com/go-sql-driver/mysql v1.5.0
github.com/gomodule/redigo v2.0.0+incompatible github.com/gohouse/gorose/v2 v2.1.10
github.com/ichunt2019/go-msgserver v1.0.4 github.com/ichunt2019/go-msgserver v1.0.4
github.com/ichunt2019/go-rabbitmq v1.0.1
github.com/ichunt2019/golang-rbmq-sl v0.0.0-20200515075131-59a37ab77d7d github.com/ichunt2019/golang-rbmq-sl v0.0.0-20200515075131-59a37ab77d7d
github.com/ichunt2019/logger v1.0.5 github.com/ichunt2019/logger v1.0.5
github.com/imroc/req v0.3.0
github.com/jmoiron/sqlx v1.2.0 github.com/jmoiron/sqlx v1.2.0
github.com/prometheus/common v0.7.0 // indirect github.com/kr/pretty v0.1.0 // indirect
github.com/streadway/amqp v0.0.0-20200108173154-1c71cc93ed71 github.com/streadway/amqp v0.0.0-20200108173154-1c71cc93ed71
github.com/stretchr/testify v1.4.0 // indirect
github.com/syyongx/php2go v0.9.4 github.com/syyongx/php2go v0.9.4
github.com/tealeg/xlsx v1.0.5
github.com/tidwall/gjson v1.6.0 github.com/tidwall/gjson v1.6.0
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
) )
package main
import (
"flag"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/gohouse/gorose/v2"
"github.com/imroc/req"
"github.com/tidwall/gjson"
"go-queue-server/order/goods_remind/model"
"go-queue-server/order/goods_remind/service"
"go-queue-server/util"
"strconv"
"strings"
"time"
)
var ConfigDir string
var err error
var engine *gorose.Engin
//设置一天最大的短信发送数量
const MaxMessageNumber = 100
func init() {
initArgs()
util.Init(ConfigDir)
initDB()
}
// 解析命令行参数
func initArgs() {
//go run main.go -configDir xxx
flag.StringVar(&ConfigDir, "configDir", "", "配置文件")
flag.Parse()
}
//初始化数据库
func initDB() {
dsn := util.Configs.Liexin_databases.Dns
engine, err = gorose.Open(&gorose.Config{Driver: "mysql", Dsn: dsn})
if err != nil {
panic(err)
}
}
func DB() gorose.IOrm {
return engine.NewOrm()
}
//有货提醒
func main() {
//先去遍历数据库,全部过一遍
db := DB()
defer db.Close()
//10个一组
messageCount := 0
oneMonthAgeTimestamp := time.Now().Unix() - 30*24*3600
//先把一个月以前的状态为0(未提醒的)统一修改为2(已过期)
err = model.UpdateExpiredGoodsRemindStatus(db)
if err != nil {
fmt.Println(err)
}
db = DB()
err = db.Table("lie_goods_remind").Where("add_time", ">", oneMonthAgeTimestamp).Where("status", "=", 0).Chunk(10, func(data []gorose.Data) error {
var goodsIds []string
var remindStatus int
goodsIdToIdMap := make(map[int64]int64)
goodsIdUserIdMap := make(map[int64]int64)
for _, item := range data {
id := item["id"].(int64)
//判断是否过期,如果过期了,就修改状态为过期
if item["add_time"].(int64) < oneMonthAgeTimestamp {
db := DB()
err = model.UpdateGoodsRemindStatus(db, id, 2)
if err != nil {
panic(err)
}
continue
}
goodsId := item["goods_id"].(int64)
//构建一个goodsId : id的字典,方便下面的修改
goodsIdToIdMap[goodsId] = id
goodsIdStr := strconv.FormatInt(goodsId, 10)
goodsIds = append(goodsIds, goodsIdStr)
goodsIdUserIdMap[goodsId] = item["user_id"].(int64)
}
goodsIdsStr := strings.Join(goodsIds, ",")
//去请求商品服务
resp, _ := req.Get(util.Configs.Api_domain.GoodsUrl + "?goods_id=" + goodsIdsStr)
//先判断返回的data是不是字典,不是字典代表可能是返回字符串了
if gjson.Get(resp.String(), "data").IsObject() {
for goodsIdKey, data := range gjson.Get(resp.String(), "data").Map() {
//还要去判断是否是bool
if data.IsObject() {
goodsId := data.Get("goods_id").Int()
stock := data.Get("stock").Int()
//如果库存大于0,就去通知用户
if stock > 0 {
//通知完成以后就修改这条记录
content := data.Get("goods_name").String()
if service.SendMessage(int(goodsIdUserIdMap[goodsId]), content) {
messageCount++
if messageCount >= MaxMessageNumber {
break
}
remindStatus = 1
}
}
} else {
//商品不存在(返回值为false)
remindStatus = 3
}
//更新状态
goodsId, _ := strconv.ParseInt(goodsIdKey, 10, 64)
db := DB()
err = model.UpdateGoodsRemindStatus(db, goodsIdToIdMap[goodsId], remindStatus)
if err != nil {
panic(err)
}
}
}
return nil
})
if err != nil {
fmt.Println(err)
panic(err)
}
}
package model
import (
"github.com/gohouse/gorose/v2"
"time"
)
func UpdateGoodsRemindStatus(db gorose.IOrm, id int64, remindStatus int) (err error) {
_, err = db.Table("lie_goods_remind").Where("id", id).Data(map[string]interface{}{
"status": remindStatus,
"last_remind_time": time.Now().Unix(),
}).Update()
return err
}
func UpdateExpiredGoodsRemindStatus(db gorose.IOrm) (err error) {
oneMonthAgeTimestamp := time.Now().Unix() - 30*24*3600
_, err = db.Table("lie_goods_remind").Where("add_time", "<", oneMonthAgeTimestamp).Data(map[string]interface{}{
"status": 2,
"last_remind_time": time.Now().Unix(),
}).Update()
return err
}
package service
import (
"encoding/json"
"fmt"
"github.com/syyongx/php2go"
"go-queue-server/util"
"net/http"
"net/url"
"strconv"
"time"
)
const ApiMd5Str string = "fh6y5t4rr351d2c3bryi"
func SendMessage(userId int, content string) bool {
if userId != 0 {
timeNow := time.Now().Unix()
requestContent, _ := json.Marshal(map[string]string{"content": content})
requestTel, _ := json.Marshal([]int{userId})
resp, err := http.PostForm(util.Configs.Api_domain.MessageUrl, url.Values{
"data": {string(requestContent)},
"touser": {string(requestTel)},
"keyword": {"remind_goods"},
"k1": {strconv.FormatInt(timeNow, 10)},
"k2": {php2go.Md5(php2go.Md5(strconv.FormatInt(timeNow, 10)) + ApiMd5Str)},
"is_ignore": {"false"},
})
defer resp.Body.Close()
if err != nil {
fmt.Print(err)
return false
}
}
return true
}
...@@ -87,6 +87,8 @@ type Ding struct { ...@@ -87,6 +87,8 @@ type Ding struct {
type ApiDomain struct { type ApiDomain struct {
ApiUrl string `toml:"api_url"` ApiUrl string `toml:"api_url"`
GoodsUrl string `toml:"goods_url"`
MessageUrl string `toml:"message_url"`
} }
type RedisConn struct { type RedisConn struct {
......
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