Commit 3eead39e by 朱继来

获取用户下单数

parent 7712c104
package main
/**
每天零点获取用户下单数
*/
import (
"crm-server/internal/dao"
"crm-server/internal/model"
"flag"
"fmt"
"github.com/ichunt2019/logger"
"time"
)
// 日志目录
var LogDir string
// 解析命令行参数
func initArgs() {
flag.StringVar(&LogDir, "logDir", "", "日志目录")
flag.Parse()
}
func main() {
initArgs()
logConfig := make(map[string]string)
logConfig["log_path"] = LogDir+"/task/user_order_count"
logConfig["log_chan_size"] = "100"
logger.InitLogger("file",logConfig)
logger.Init()
var current_time = time.Now().Unix()
db := dao.GetLiexinDb()
row, err := db.Queryx("SELECT user_id, FROM_UNIXTIME(create_time, \"%Y%m%d\") AS date, COUNT(*) AS count FROM `lie_order` " +
"WHERE user_id > 0 AND is_type = 0 AND create_time > ? GROUP BY user_id, date ORDER BY create_time", current_time)
defer db.Close()
if err != nil {
fmt.Println("获取用户下单数失败:" + err.Error())
logger.Info("获取用户下单数失败: ", err.Error())
return
}
crm_db := dao.GetDb()
defer crm_db.Close()
if !row.Next() {
fmt.Println("未获取到用户下单数")
return
}
for row.Next() {
var user_order_count model.UserOrderCount
row.StructScan(&user_order_count)
var user_order_id int
crm_db.Get(&user_order_id, "select id from lie_user_order_count where user_id = ? and date = ?", user_order_count.User_Id, user_order_count.Date)
if user_order_id != 0 { // 若存在,则跳过
continue
}
_, err := crm_db.Exec("insert into lie_user_order_count (user_id, date, count, create_time) values (?, ?, ?, ?)", user_order_count.User_Id, user_order_count.Date,
user_order_count.Count, current_time)
if err != nil {
fmt.Println("添加用户下单数失败:" + err.Error())
logger.Info("添加用户下单数失败: ", err.Error())
return
}
}
fmt.Println("添加用户下单数成功")
}
\ No newline at end of file
package model
type UserOrderCount struct {
Id int `db:"id"`
User_Id int `db:"user_id"`
Date string `db:"date"`
Count int `db:"count"`
Create_Time int64 `db:"create_time"`
}
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