Commit b64ff6f1 by mushishixian

供应商sku日志

parent 46af5aa0
package common
import "time"
func GetStartOfDayTimeStamp(timestamp int64) int64 {
// 获取当前时间
now := time.Unix(timestamp, 0)
// 获取今天凌晨的时间
startOfDay := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
// 转换成 Unix 时间戳
return startOfDay.Unix()
}
......@@ -2,7 +2,9 @@ package dao
import (
"fmt"
common "go_supplier_sever/app/common/function"
"go_supplier_sever/app/model"
"math"
"strconv"
"time"
......@@ -18,6 +20,11 @@ func StatisticsSuppliersSkuNum() (err error) {
if err != nil {
return err
}
//先重置所有供应商的平均数
supplierSkuNum := new(model.Supplier)
supplier.AverageSkuNum = 0
_, err = Dao.GetDb("supplier").Table("lie_supplier_channel").Where("supplier_id >= ?", 1).MustCols("average_sku_num").Update(supplierSkuNum)
defer rows.Close()
var num int
for rows.Next() {
......@@ -44,6 +51,13 @@ func StatisticsSuppliersSkuNum() (err error) {
if err != nil {
return err
}
if skuNum > 0 {
err = UpdateSupplierSkuLog(supplier.SupplierId, int(skuNum))
if err != nil {
return err
}
}
}
fmt.Println("执行完成,有上传sku的供应商数量为 : ", num)
return
......@@ -133,7 +147,7 @@ func UpdateSupplierHasUploadedSku(supplierId int, hasUploadedSku int) (err error
return err
}
supplier.UploadedSku = hasUploadedSku
//还要去写映射表lie_supplier_merger_mapping
_, err = Dao.GetDb("supplier").Table("lie_supplier_channel").Where("supplier_id = ?", supplierId).Cols("uploaded_sku").Update(supplier)
if err != nil {
return err
......@@ -141,6 +155,83 @@ func UpdateSupplierHasUploadedSku(supplierId int, hasUploadedSku int) (err error
return
}
//写入统计时间,并且更新每个供应商的sku上架平均数量
func UpdateSupplierSkuLog(supplierId int, skuNum int) (err error) {
skuLog := new(model.SupplierSkuLog)
todayTime := common.GetStartOfDayTimeStamp(time.Now().Unix())
//todayTime = todayTime + 86400*2
if err != nil {
return err
}
skuLog.SkuNum = skuNum
skuLog.SupplierId = supplierId
skuLog.Date = todayTime
//判断是否存在记录
exists, err := Dao.GetDb("supplier").Table("lie_supplier_sku_log").Where("supplier_id = ?", supplierId).Where("date = ?", todayTime).Exist()
if exists {
_, err := Dao.GetDb("supplier").Table("lie_supplier_sku_log").Where("supplier_id = ?", supplierId).Cols("sku_num").Where("date = ?", todayTime).Update(skuLog)
if err != nil {
return err
}
} else {
skuLog.CreateTime = time.Now().Unix()
_, err := Dao.GetDb("supplier").Table("lie_supplier_sku_log").Insert(skuLog)
if err != nil {
return err
}
}
//获取平均sku数量(180天内的)
type AverageResult struct {
TotalSkuNum float64
AverageSkuNum float64
}
var averageResult AverageResult
timestampAgo := time.Now().Unix() - 180*24*3600
_, err = Dao.GetDb("supplier").Table("lie_supplier_sku_log").Select("sum(sku_num) AS total_sku_num").
Where("supplier_id = ?", supplierId).Where("create_time > ?", timestampAgo).Get(&averageResult)
if err != nil {
return err
}
//找出和今天的天数差距
specificTimestamp := int64(1684339200)
supplier := new(model.Supplier)
//先去根据供应商内部编码找出对应的supplierId
_, err = Dao.GetDb("supplier").Table("lie_supplier_channel").Where("supplier_id = ?", supplierId).Get(supplier)
if err != nil {
return err
}
supplierCreateTime := supplier.CreateTime
//如果供应商的创建时间小于上面写死的时间,那么就那写死的时间,否则拿创建时间
var startStatisticTime int64
if specificTimestamp > supplierCreateTime {
startStatisticTime = specificTimestamp
} else {
startStatisticTime = common.GetStartOfDayTimeStamp(supplierCreateTime)
//fmt.Println("获取供应商时间:", supplierCreateTime)
}
totalDays := (todayTime - startStatisticTime) / (24 * 60 * 60)
totalDays = int64(math.Ceil(float64(totalDays)))
if totalDays < 1 {
totalDays = 1
}
//fmt.Println(totalDays)
//获取平均数
averageResult.AverageSkuNum = math.Ceil(averageResult.TotalSkuNum / float64(totalDays))
//fmt.Println("平均数", averageResult.AverageSkuNum)
supplier = new(model.Supplier)
supplier.AverageSkuNum = averageResult.AverageSkuNum
//先去根据供应商内部编码找出对应的supplierId
//fmt.Println(supplier.AverageSkuNum)
affected, err := Dao.GetDb("supplier").Table("lie_supplier_channel").Where("supplier_id = ?", supplierId).MustCols("average_sku_num").Update(supplier)
if affected > 0 {
}
if err != nil {
return err
}
return
}
//修改sku_mode
func UpdateSupplierSkuMode(supplierId, skuMode int) (err error) {
supplier := new(model.Supplier)
......
package model
type SupplierSkuLog struct {
SupplierId int
SkuNum int
Date int64
CreateTime int64
}
package model
type Supplier struct {
SupplierId int
SupplierCode string
SupplierName string
UploadedSku int
SkuNum int
SkuMode int
SupplierId int
SupplierCode string
SupplierName string
UploadedSku int
SkuNum int
SkuMode int
AverageSkuNum float64
CreateTime int64
}
......@@ -9,10 +9,10 @@ func StatisticsSupplierSkuStatus() (err error) {
fmt.Println("开始统计")
//先找出所有需要判断的供应商
err = dao.StatisticsSuppliersSkuNum()
// if err != nil {
// return err
// }
// err = dao.StatisticsSuppliersHasUploadedSku()
//if err != nil {
// return err
//}
//
//err = dao.StatisticsSuppliersHasUploadedSku()
return err
}
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