Commit 76032385 by 朱继来

生成竞调订单

parent b6431d4e
...@@ -4,12 +4,11 @@ import ( ...@@ -4,12 +4,11 @@ import (
"database/sql" "database/sql"
_ "encoding/json" _ "encoding/json"
"fmt" "fmt"
_ "fmt"
"github.com/ichunt2019/logger" "github.com/ichunt2019/logger"
"github.com/streadway/amqp" "github.com/streadway/amqp"
"go-queue-server/util"
"go-queue-server/dal/db" "go-queue-server/dal/db"
_ "time" "go-queue-server/util"
"time"
) )
// 查询用户订单数 // 查询用户订单数
...@@ -38,7 +37,6 @@ func UpdateStatus(order_id int, status_extend int) (err error) { ...@@ -38,7 +37,6 @@ func UpdateStatus(order_id int, status_extend int) (err error) {
return err return err
} }
func makeOrder(order_id int) (err error) { func makeOrder(order_id int) (err error) {
conn, err := amqp.Dial(util.Configs.Rabbitmq_order_push_stock.Dns) conn, err := amqp.Dial(util.Configs.Rabbitmq_order_push_stock.Dns)
...@@ -81,5 +79,21 @@ func makeOrder(order_id int) (err error) { ...@@ -81,5 +79,21 @@ func makeOrder(order_id int) (err error) {
logger.Fatal(fmt.Sprintf(" err %s 推入仓库失败 订单号 %d", err, order_id)) logger.Fatal(fmt.Sprintf(" err %s 推入仓库失败 订单号 %d", err, order_id))
} }
return return
}
// 更新昨天的竞调订单
func UpdateJingDiaoOrder() {
now := time.Now().Format("2006-01-02")
t, _ := time.ParseInLocation("2006-01-02", now, time.Local)
start_time := t.Unix() - 86400
end_time := t.Unix() - 1
_, err := db.DB.Exec("update lie_order set status = ? where is_type = ? and create_time between ? and ?", 8, 1, start_time, end_time)
if err != nil {
logger.Fatal("更新昨天的竞调订单操作失败")
return
}
} }
\ No newline at end of file
package OrderAddress
type OrderAddress struct {
OrderAddressId int `db:"order_address_id"`
OrderId int `db:"order_id"`
AddressId int `db:"address_id"`
OrderSn string `db:"order_sn"`
AddressType int `db:"address_type"`
Province int `db:"province"`
City int `db:"city"`
District int `db:"district"`
Address string `db:"address"`
Consignee string `db:"consignee"`
Mobile string `db:"mobile"`
AreaCode string `db:"area_code"`
Telphone string `db:"telphone"`
}
package OrderItems
type OrderItems struct {
RecId int `db:"rec_id"`
OrderId int `db:"order_id"`
UserId int `db:"user_id"`
ErpRecId int `db:"erp_rec_id"`
GoodsId int `db:"goods_id"`
SupplierId int `db:"supplier_id"`
BrandId int `db:"brand_id"`
StandardBrandId int `db:"standard_brand_id"`
GoodsSn string `db:"goods_sn"`
GoodsName string `db:"goods_name"`
SkuName string `db:"sku_name"`
SupplierName string `db:"supplier_name"`
BrandName string `db:"brand_name"`
StandardBrandName string `db:"standard_brand_name"`
GoodsType int `db:"goods_type"`
SelfSupplierType int `db:"self_supplier_type"`
GoodsNumber int `db:"goods_number"`
GoodsPrice float64 `db:"goods_price"`
DeliveryTime string `db:"delivery_time"`
SaleType int `db:"sale_type"`
AcType int `db:"ac_type"`
OrderGoodsType int `db:"order_goods_type"`
SinglePrePrice float64 `db:"single_pre_price"`
BuyerId int `db:"buyer_id"`
Batch string `db:"batch"`
}
package OtherAddress
type OtherAddress struct {
OrderAddressId int `db:"order_address_id"`
OrderId int `db:"order_id"`
AddressId int `db:"address_id"`
OrderSn string `db:"order_sn"`
AddressType int `db:"address_type"`
Province int `db:"province"`
City int `db:"city"`
District int `db:"district"`
Address string `db:"address"`
Consignee string `db:"consignee"`
Mobile string `db:"mobile"`
AreaCode string `db:"area_code"`
Telphone string `db:"telphone"`
}
package handle
import (
"fmt"
"go-queue-server/dal/db"
"go-queue-server/dal/order/OrderItems"
"go-queue-server/dal/order/OtherAddress"
"math/rand"
"strconv"
"time"
)
// 随机数
func RandInt(min, max int) int {
if min >= max || min==0 || max==0{
return max
}
rand.Seed(time.Now().UnixNano()) //以时间作为初始化种子
return rand.Intn(max - min) + min
}
// 随机获取竞调用户user_id
func GetJingDiaoUserId() int {
var max_count int
var min_count int
db.DB.Get(&max_count, "select Max(user_id) from lie_user_main where is_type = 1")
db.DB.Get(&min_count, "select Min(user_id) from lie_user_main where is_type = 1")
rand_num := RandInt(min_count - 1, max_count + 1) // 从总数中取个随机数
var user_id int
db.DB.Get(&user_id, "select user_id from lie_user_main where user_id = ? and is_type = ? limit 1", rand_num, 1)
if user_id == 0 {
return GetJingDiaoUserId()
}
return user_id
}
// 随机获取一条商品明细
func GetOneOrderItem() OrderItems.OrderItems {
var count int
db.DB.QueryRow("select count(*) from lie_order_items where goods_id != ? and goods_price > ? and status = ?", 0, 0.1, 1).Scan(&count)
rand_num := RandInt(1, count) // 从总数中取个随机数
var order_item OrderItems.OrderItems
var field string = "rec_id,goods_id,supplier_id,brand_id,standard_brand_id,goods_sn,goods_name,sku_name,supplier_name,brand_name,standard_brand_name,goods_type," +
"self_supplier_type,goods_number,goods_price,delivery_time,sale_type,ac_type,order_goods_type,buyer_id,batch"
db.DB.Get(&order_item, "select "+field+" from lie_order_items where rec_id = ? and goods_id != ? and goods_price > ? and status = ?", rand_num, 0, 0.1, 1)
if order_item.RecId == 0 {
return GetOneOrderItem()
}
return order_item
}
// 随机获取一条收货地址
func GetOneOrderAddress() OtherAddress.OtherAddress {
var max_count int
var min_count int
db.DB.Get(&max_count, "select Max(order_address_id) from lie_order_address_tmp where address_type = 1")
db.DB.Get(&min_count, "select Min(order_address_id) from lie_order_address_tmp where address_type = 1")
rand_num := RandInt(min_count - 1, max_count + 1) // 从总数中取个随机数
var order_address OtherAddress.OtherAddress
var field string = "order_address_id,address_id,province,city,district,address,consignee,mobile,area_code,telphone"
db.DB.Get(&order_address, "select "+field+" from lie_order_address_tmp where order_address_id = ? and address_type = ?", rand_num, 1)
if order_address.OrderAddressId == 0 {
return GetOneOrderAddress()
}
return order_address
}
// 随机获取客服
func GetSaleId() int {
var max_count int
var min_count int
db.CmsDB.Get(&max_count, "select Max(userId) from user_info where position_id = 62 and status = 0")
db.CmsDB.Get(&min_count, "select Min(userId) from user_info where position_id = 62 and status = 0")
rand_num := RandInt(min_count - 1, max_count + 1) // 从总数中取个随机数
var sale_id int
db.CmsDB.Get(&sale_id, "select userId from user_info where userId = ? and position_id = ? and status = ?", rand_num, 62, 0)
if sale_id == 0 {
return GetSaleId()
}
return sale_id
}
// 保留两位小数
func Decimal(value float64) float64 {
value, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", value), 64)
return value
}
// 生成订单号
func createOrderSn() string {
var date string
var randnum int
date = time.Now().Format("20060102")
randnum = RandInt(10000, 99999)
return "1" + date + strconv.Itoa(randnum)
}
// 获取订单号
func GetOrderSn() string {
var sn string = createOrderSn()
var order_id int
db.DB.Get(&order_id, "select order_id from lie_order where order_sn = ?", sn)
if order_id != 0 {
return GetOrderSn()
}
return sn
}
// 获取两个日期间隔天数
func GetDiffDay(start, end string) int {
timeLayout := "2006-01-02"
// 转成时间戳
startUnix,_ := time.ParseInLocation(timeLayout, start, time.Local)
endUnix,_ := time.ParseInLocation(timeLayout, end, time.Local)
startTime := startUnix.Unix()
endTime := endUnix.Unix()
// 求相差天数
sub := (endTime - startTime) / 86400 + 1
sub_str := strconv.FormatInt(sub, 10)
diff_day, _ := strconv.Atoi(sub_str)
return diff_day
}
\ No newline at end of file
package main
import (
"flag"
"fmt"
"github.com/ichunt2019/logger"
"go-queue-server/dal/db"
"go-queue-server/dal/order/Order"
"go-queue-server/dal/order/OrderItems"
"go-queue-server/dal/order/OtherAddress"
"go-queue-server/order/jingdiao/handle"
"go-queue-server/util"
"strconv"
"time"
)
func initDb(action string, dns string) (err error) {
err = db.CreateDB(action, dns)
if err != nil {
return
}
return
}
var ConfigDir string
var LogDir string
var StartTime string
var EndTime string
// 解析命令行参数
func initArgs() {
flag.StringVar(&ConfigDir, "configDir", "", "配置文件")
flag.StringVar(&LogDir, "logDir", "", "日志目录")
flag.StringVar(&StartTime, "startTime", "", "开始时间")
flag.StringVar(&EndTime, "endTime", "", "结束时间")
flag.Parse()
}
func main() {
initArgs()
//初始化配置文件
util.Init(ConfigDir)
logConfig := make(map[string]string)
logConfig["log_path"] = LogDir + "order/jingdiao"
logConfig["log_chan_size"] = "1000"
logger.InitLogger("file", logConfig)
logger.Init()
//初始化db
initDb("Init", util.Configs.Liexin_databases.Dns)
initDb("InitCmsDB", util.Configs.Liexincms_databases.Dns)
forever := make(chan bool)
fmt.Println("执行完成后, 按 CTRL+C 退出程序...")
if StartTime != "" && EndTime != "" { // 开始时间和结束时间都存在
diff_day := handle.GetDiffDay(StartTime, EndTime)
// 开始日期时间戳
start, _ := time.ParseInLocation("2006-01-02", StartTime, time.Local)
start_time := start.Unix()
for i := 0; i < diff_day; i++ {
order_count := handle.RandInt(600, 700) // 每天生成的订单数
// 创建订单
for i := 0; i < order_count; i++ {
var user_id int = handle.GetJingDiaoUserId()
go CreateOrder(user_id, start_time)
}
start_time += 86400
}
<-forever
} else if StartTime != "" { // 仅存在开始时间
// 开始日期时间戳
start, _ := time.ParseInLocation("2006-01-02", StartTime, time.Local)
start_time := start.Unix()
order_count := handle.RandInt(600, 700) // 每天生成的订单数
// 创建订单
for i := 0; i < order_count; i++ {
var user_id int = handle.GetJingDiaoUserId()
go CreateOrder(user_id, start_time)
}
<-forever
}
// 开始时间和结束时间不存在,则按定时任务执行
if StartTime == "" && EndTime == "" {
order_count := handle.RandInt(70, 80) // 每小时生成的订单数
fmt.Println(order_count)
// 查找昨天的竞调订单,并修改为待收货状态
Order.UpdateJingDiaoOrder()
// 创建订单
for i := 0; i < order_count; i++ {
var user_id int = handle.GetJingDiaoUserId()
CreateOrder(user_id, time.Now().Unix())
}
}
}
// 创建订单
func CreateOrder(user_id int, create_time int64) {
var min_amount int // 订单总额不低于随机生成数
var order_item OrderItems.OrderItems // 随机获取一条明细
var order_address OtherAddress.OtherAddress // 随机获取一条地址
var goods_number int // 商品数量
var order_sn string // 订单号
var order_amount float64 // 订单总额
min_amount = handle.RandInt(8000, 15000) / 100 * 100
order_item = handle.GetOneOrderItem()
order_address = handle.GetOneOrderAddress()
goods_number = int(float64(min_amount) / order_item.GoodsPrice)
order_amount = handle.Decimal(order_item.GoodsPrice * float64(goods_number))
order_sn = handle.GetOrderSn()
// 订单数据
order := make(map[string]interface{}, 0)
goods_data := make(map[string]interface{}, 0)
address_data := make(map[string]interface{}, 0)
order["order_sn"] = order_sn
order["user_id"] = user_id
order["order_amount"] = order_amount
order["status"] = 4
order["sale_id"] = handle.GetSaleId()
order["is_type"] = 1
goods_data["user_id"] = user_id
goods_data["goods_id"] = order_item.GoodsId
goods_data["supplier_id"] = order_item.SupplierId
goods_data["brand_id"] = order_item.BrandId
goods_data["standard_brand_id"] = order_item.StandardBrandId
goods_data["goods_sn"] = order_item.GoodsSn
goods_data["goods_name"] = order_item.GoodsName
goods_data["sku_name"] = order_item.SkuName
goods_data["supplier_name"] = order_item.SupplierName
goods_data["brand_name"] = order_item.BrandName
goods_data["standard_brand_name"] = order_item.StandardBrandId
goods_data["goods_type"] = order_item.GoodsType
goods_data["self_supplier_type"] = order_item.SelfSupplierType
goods_data["goods_number"] = order_item.GoodsNumber
goods_data["goods_price"] = order_item.GoodsPrice
goods_data["delivery_time"] = order_item.DeliveryTime
goods_data["sale_type"] = order_item.SaleType
goods_data["ac_type"] = order_item.AcType
goods_data["order_goods_type"] = order_item.OrderGoodsType
goods_data["buyer_id"] = order_item.BuyerId
goods_data["batch"] = order_item.Batch
address_data["address_id"] = order_address.AddressId
address_data["order_sn"] = order_sn
address_data["address_type"] = order_address.AddressType
address_data["province"] = order_address.Province
address_data["city"] = order_address.City
address_data["district"] = order_address.District
address_data["address"] = order_address.Address
address_data["consignee"] = order_address.Consignee
address_data["mobile"] = order_address.Mobile
address_data["area_code"] = order_address.AreaCode
address_data["telphone"] = order_address.Telphone
conn, err := db.DB.Begin() // 开启事务
if err != nil {
return
}
// 添加lie_order
res, _ := conn.Exec("insert into lie_order (order_sn, user_id, order_amount, status, create_time, sale_id, is_type) values (?, ?, ?, ?, ?, ?, ?)", order["order_sn"],
order["user_id"], order["order_amount"], order["status"], create_time, order["sale_id"], order["is_type"])
LastInsertId, err := res.LastInsertId()
if err != nil {
conn.Rollback() //回滚
return
}
strInt64 := strconv.FormatInt(LastInsertId, 10)
order_id, _ := strconv.Atoi(strInt64)
// 添加lie_order_items
_, err = conn.Exec("insert into lie_order_items (order_id, user_id, goods_id, supplier_id, brand_id, standard_brand_id, goods_sn, goods_name, sku_name, supplier_name, brand_name, " +
"standard_brand_name, goods_type, self_supplier_type, goods_number, goods_price, delivery_time, sale_type, ac_type, order_goods_type, buyer_id, batch) values (?, ?, ?, ?, " +
"?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", order_id, goods_data["user_id"], goods_data["goods_id"], goods_data["supplier_id"],
goods_data["brand_id"], goods_data["standard_brand_id"], goods_data["goods_sn"], goods_data["goods_name"], goods_data["sku_name"], goods_data["supplier_name"],
goods_data["brand_name"], goods_data["standard_brand_name"], goods_data["goods_type"], goods_data["self_supplier_type"], goods_data["goods_number"], goods_data["goods_price"],
goods_data["delivery_time"], goods_data["sale_type"], goods_data["ac_type"], goods_data["order_goods_type"], goods_data["buyer_id"], goods_data["batch"])
if err != nil {
conn.Rollback() //回滚
return
}
// 添加lie_other_address
_, err = conn.Exec("insert into lie_other_address (order_id, address_id, order_sn, province, city, district, address, consignee, mobile, area_code, telphone) " +
"values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", order_id, address_data["address_id"], address_data["order_sn"], address_data["province"], address_data["city"],
address_data["district"], address_data["address"], address_data["consignee"], address_data["mobile"], address_data["area_code"], address_data["telphone"])
if err != nil {
conn.Rollback() //回滚
return
}
conn.Commit() // 提交事务
fmt.Println("生成竞调订单ID:" + strconv.Itoa(order_id))
}
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