1、 获取采购需求接口,增加上周销量字段:last_week_sales;
This commit is contained in:
parent
db1416c42e
commit
ddfa6b5de0
|
@ -302,6 +302,7 @@ type DemandData struct {
|
|||
StoreID uint32 `json:"store_id"` // 门店id
|
||||
StoreName string `json:"store_name"` // 门店名称
|
||||
LastMonthSales uint32 `json:"last_month_sales"` // 上月销售数
|
||||
LastWeekSales uint32 `json:"last_week_sales"` // 上周销售数
|
||||
StockCount uint32 `json:"stock_count"` // 库存数量
|
||||
NeedCount uint32 `json:"need_count"` // 需采购数
|
||||
} `json:"store_list"`
|
||||
|
@ -2077,6 +2078,34 @@ func getErpPurchaseDemandAll(req *GetErpPurchaseDemandReq, c *gin.Context) (*Get
|
|||
}
|
||||
}
|
||||
|
||||
// 批量查询商品上周销售数量
|
||||
var weekSoldOutCommodities []ErpStockCommodity
|
||||
firstWeekDay, lastWeekDay := GetLastWeekRange() // 获取上个月的时间范围
|
||||
|
||||
if len(storeIdList) > 1 {
|
||||
err = orm.Eloquent.Table("erp_stock_commodity").
|
||||
Where("state = ? AND updated_at BETWEEN ? AND ? AND store_id in (?) AND erp_commodity_id IN (?)",
|
||||
SoldOut, firstWeekDay, lastWeekDay, storeIdList, commodityIDs).Find(&weekSoldOutCommodities).Error
|
||||
} else {
|
||||
err = orm.Eloquent.Table("erp_stock_commodity").
|
||||
Where("state = ? AND updated_at BETWEEN ? AND ? AND store_id = ? AND erp_commodity_id IN (?)",
|
||||
SoldOut, firstWeekDay, lastWeekDay, storeIdList[0], commodityIDs).Find(&weekSoldOutCommodities).Error
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 按照 商品id-门店id:销售数量 组合信息
|
||||
weekSoldOutMap := make(map[string]uint32)
|
||||
for _, item := range weekSoldOutCommodities {
|
||||
key := fmt.Sprintf("%d-%d", item.ErpCommodityId, item.StoreId)
|
||||
_, exist := weekSoldOutMap[key]
|
||||
if exist { // 存在
|
||||
weekSoldOutMap[key] += 1
|
||||
} else { // 不存在
|
||||
weekSoldOutMap[key] = 1
|
||||
}
|
||||
}
|
||||
|
||||
// 批量查询商品的库存情况
|
||||
var stockCommodities []ErpStockCommodity
|
||||
if len(storeIdList) > 1 {
|
||||
|
@ -2174,7 +2203,7 @@ func getErpPurchaseDemandAll(req *GetErpPurchaseDemandReq, c *gin.Context) (*Get
|
|||
wg.Add(1)
|
||||
go func(index int, commodity ErpCommodity) {
|
||||
defer wg.Done()
|
||||
demandData, err := convertToDemandDataAll(commodity, storesMap[commodity.ID], stores, soldOutMap, inStockMap,
|
||||
demandData, err := convertToDemandDataAll(commodity, storesMap[commodity.ID], stores, soldOutMap, weekSoldOutMap, inStockMap,
|
||||
demandsMap, demandRemarksMap)
|
||||
if err != nil {
|
||||
// Handle error
|
||||
|
@ -2206,7 +2235,7 @@ func getErpPurchaseDemandAll(req *GetErpPurchaseDemandReq, c *gin.Context) (*Get
|
|||
}
|
||||
|
||||
// convertToDemandDataAll 将商品转换为采购需求数据
|
||||
func convertToDemandDataAll(commodity ErpCommodity, usedStore []uint32, stores []Store, soldOutMap, stockMap map[string]uint32,
|
||||
func convertToDemandDataAll(commodity ErpCommodity, usedStore []uint32, stores []Store, soldOutMap, weekSoldOutMap, stockMap map[string]uint32,
|
||||
demandMap map[string]ErpPurchaseDemand, demandRemarkMap map[uint32]ErpPurchaseDemandRemark) (DemandData, error) {
|
||||
demandData := DemandData{
|
||||
ErpSupplierId: commodity.ErpSupplierId,
|
||||
|
@ -2242,6 +2271,7 @@ func convertToDemandDataAll(commodity ErpCommodity, usedStore []uint32, stores [
|
|||
StoreID uint32 `json:"store_id"`
|
||||
StoreName string `json:"store_name"`
|
||||
LastMonthSales uint32 `json:"last_month_sales"`
|
||||
LastWeekSales uint32 `json:"last_week_sales"`
|
||||
StockCount uint32 `json:"stock_count"`
|
||||
NeedCount uint32 `json:"need_count"`
|
||||
}, len(stores))
|
||||
|
@ -2253,6 +2283,7 @@ func convertToDemandDataAll(commodity ErpCommodity, usedStore []uint32, stores [
|
|||
key := fmt.Sprintf("%d-%d", commodity.ID, store.ID)
|
||||
demandData.StoreList[i].StockCount = stockMap[key]
|
||||
demandData.StoreList[i].LastMonthSales = soldOutMap[key]
|
||||
demandData.StoreList[i].LastWeekSales = weekSoldOutMap[key]
|
||||
|
||||
// 设置最近采购价
|
||||
demandData.LastWholesalePrice = lastWholesalePrices[commodity.ID]
|
||||
|
@ -2535,16 +2566,19 @@ func convertToDemandData(commodity ErpCommodity, usedStore []uint32, stores []St
|
|||
var lastWholesalePrices map[uint32]float64
|
||||
var stockCounts map[uint32]uint32
|
||||
var lastMonthSales map[uint32]uint32
|
||||
var lastWeekSales map[uint32]uint32
|
||||
|
||||
lastWholesalePrices, _ = GetCommodityLastWholesalePrices(commodity.ID)
|
||||
stockCounts, _ = GetCommodityStocksByStoreID(commodity.ID, usedStore)
|
||||
lastMonthSales, _ = GetCommodityLastMonthSales(commodity.ID, usedStore)
|
||||
lastWeekSales, _ = GetCommodityLastWeekSales(commodity.ID, usedStore)
|
||||
|
||||
var totalCount uint32
|
||||
demandData.StoreList = make([]struct {
|
||||
StoreID uint32 `json:"store_id"`
|
||||
StoreName string `json:"store_name"`
|
||||
LastMonthSales uint32 `json:"last_month_sales"`
|
||||
LastWeekSales uint32 `json:"last_week_sales"`
|
||||
StockCount uint32 `json:"stock_count"`
|
||||
NeedCount uint32 `json:"need_count"`
|
||||
}, len(stores))
|
||||
|
@ -2555,6 +2589,7 @@ func convertToDemandData(commodity ErpCommodity, usedStore []uint32, stores []St
|
|||
|
||||
demandData.StoreList[i].StockCount = stockCounts[store.ID]
|
||||
demandData.StoreList[i].LastMonthSales = lastMonthSales[store.ID]
|
||||
demandData.StoreList[i].LastWeekSales = lastWeekSales[store.ID]
|
||||
|
||||
demandData.LastWholesalePrice = lastWholesalePrices[commodity.ID]
|
||||
|
||||
|
@ -2713,6 +2748,67 @@ func GetCommodityLastMonthSales(commodityID uint32, stores []uint32) (map[uint32
|
|||
return result, nil
|
||||
}
|
||||
|
||||
// GetCommodityLastWeekSales 批量查询商品的上周销售数量
|
||||
func GetCommodityLastWeekSales(commodityID uint32, stores []uint32) (map[uint32]uint32, error) {
|
||||
// 获取上个月的时间范围
|
||||
firstDay, lastDay := GetLastWeekRange()
|
||||
|
||||
// 并行查询上月销售数量
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(len(stores))
|
||||
|
||||
result := make(map[uint32]uint32)
|
||||
var mu sync.Mutex // 用于保护 totalSales 的并发访问
|
||||
|
||||
for _, store := range stores {
|
||||
go func(storeID uint32) {
|
||||
defer wg.Done()
|
||||
// 查询上月销售数量
|
||||
var sales int64
|
||||
err := orm.Eloquent.Table("erp_stock_commodity").
|
||||
Where("state = ? AND erp_commodity_id = ? AND store_id = ? AND updated_at BETWEEN ? AND ?",
|
||||
SoldOut, commodityID, storeID, firstDay, lastDay).Count(&sales).Error
|
||||
if err != nil {
|
||||
// Handle error
|
||||
return
|
||||
}
|
||||
|
||||
// 保护 totalSales 的并发访问
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
result[storeID] = uint32(sales)
|
||||
}(store)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// GetLastWeekRange 获取上周的时间范围
|
||||
func GetLastWeekRange() (time.Time, time.Time) {
|
||||
now := time.Now()
|
||||
// 获取当前时间的星期几,周日为0,周一为1,依次类推
|
||||
weekday := int(now.Weekday())
|
||||
if weekday == 0 {
|
||||
weekday = 7 // 将周日设为7,方便计算
|
||||
}
|
||||
|
||||
// 计算上周的开始日期和结束日期
|
||||
// 上周结束日期为上周日
|
||||
lastWeekEnd := now.AddDate(0, 0, -weekday)
|
||||
// 上周开始日期为上周一
|
||||
lastWeekStart := lastWeekEnd.AddDate(0, 0, -6)
|
||||
|
||||
// 设置时间为零点
|
||||
startOfDay := func(t time.Time) time.Time {
|
||||
year, month, day := t.Date()
|
||||
return time.Date(year, month, day, 0, 0, 0, 0, t.Location())
|
||||
}
|
||||
|
||||
return startOfDay(lastWeekStart), startOfDay(lastWeekEnd)
|
||||
}
|
||||
|
||||
// GetLastMonthRange 获取上个月的时间范围
|
||||
func GetLastMonthRange() (time.Time, time.Time) {
|
||||
now := time.Now()
|
||||
|
|
14
docs/docs.go
14
docs/docs.go
|
@ -8513,6 +8513,10 @@ const docTemplate = `{
|
|||
"description": "上月销售数",
|
||||
"type": "integer"
|
||||
},
|
||||
"last_week_sales": {
|
||||
"description": "上周销售数",
|
||||
"type": "integer"
|
||||
},
|
||||
"need_count": {
|
||||
"description": "需采购数",
|
||||
"type": "integer"
|
||||
|
@ -9664,6 +9668,7 @@ const docTemplate = `{
|
|||
"amount",
|
||||
"category_number",
|
||||
"name",
|
||||
"sms_content",
|
||||
"user_type"
|
||||
],
|
||||
"properties": {
|
||||
|
@ -9699,6 +9704,10 @@ const docTemplate = `{
|
|||
"description": "优惠券使用规则",
|
||||
"type": "string"
|
||||
},
|
||||
"sms_content": {
|
||||
"description": "短信提示内容",
|
||||
"type": "string"
|
||||
},
|
||||
"user_type": {
|
||||
"description": "领取人限制:1-所有人 2-未付费用户 3-已付费用户 4-尊享会员",
|
||||
"type": "integer"
|
||||
|
@ -9789,6 +9798,7 @@ const docTemplate = `{
|
|||
"category_number",
|
||||
"erp_coupon_id",
|
||||
"name",
|
||||
"sms_content",
|
||||
"user_type"
|
||||
],
|
||||
"properties": {
|
||||
|
@ -9828,6 +9838,10 @@ const docTemplate = `{
|
|||
"description": "优惠券使用规则",
|
||||
"type": "string"
|
||||
},
|
||||
"sms_content": {
|
||||
"description": "短信提示内容",
|
||||
"type": "string"
|
||||
},
|
||||
"user_type": {
|
||||
"description": "领取人限制:1-所有人 2-未付费用户 3-已付费用户 4-尊享会员",
|
||||
"type": "integer"
|
||||
|
|
|
@ -8502,6 +8502,10 @@
|
|||
"description": "上月销售数",
|
||||
"type": "integer"
|
||||
},
|
||||
"last_week_sales": {
|
||||
"description": "上周销售数",
|
||||
"type": "integer"
|
||||
},
|
||||
"need_count": {
|
||||
"description": "需采购数",
|
||||
"type": "integer"
|
||||
|
@ -9653,6 +9657,7 @@
|
|||
"amount",
|
||||
"category_number",
|
||||
"name",
|
||||
"sms_content",
|
||||
"user_type"
|
||||
],
|
||||
"properties": {
|
||||
|
@ -9688,6 +9693,10 @@
|
|||
"description": "优惠券使用规则",
|
||||
"type": "string"
|
||||
},
|
||||
"sms_content": {
|
||||
"description": "短信提示内容",
|
||||
"type": "string"
|
||||
},
|
||||
"user_type": {
|
||||
"description": "领取人限制:1-所有人 2-未付费用户 3-已付费用户 4-尊享会员",
|
||||
"type": "integer"
|
||||
|
@ -9778,6 +9787,7 @@
|
|||
"category_number",
|
||||
"erp_coupon_id",
|
||||
"name",
|
||||
"sms_content",
|
||||
"user_type"
|
||||
],
|
||||
"properties": {
|
||||
|
@ -9817,6 +9827,10 @@
|
|||
"description": "优惠券使用规则",
|
||||
"type": "string"
|
||||
},
|
||||
"sms_content": {
|
||||
"description": "短信提示内容",
|
||||
"type": "string"
|
||||
},
|
||||
"user_type": {
|
||||
"description": "领取人限制:1-所有人 2-未付费用户 3-已付费用户 4-尊享会员",
|
||||
"type": "integer"
|
||||
|
|
|
@ -1338,6 +1338,9 @@ definitions:
|
|||
last_month_sales:
|
||||
description: 上月销售数
|
||||
type: integer
|
||||
last_week_sales:
|
||||
description: 上周销售数
|
||||
type: integer
|
||||
need_count:
|
||||
description: 需采购数
|
||||
type: integer
|
||||
|
@ -2200,6 +2203,9 @@ definitions:
|
|||
rule:
|
||||
description: 优惠券使用规则
|
||||
type: string
|
||||
sms_content:
|
||||
description: 短信提示内容
|
||||
type: string
|
||||
user_type:
|
||||
description: 领取人限制:1-所有人 2-未付费用户 3-已付费用户 4-尊享会员
|
||||
type: integer
|
||||
|
@ -2208,6 +2214,7 @@ definitions:
|
|||
- amount
|
||||
- category_number
|
||||
- name
|
||||
- sms_content
|
||||
- user_type
|
||||
type: object
|
||||
models.ErpMarketingCouponDataReq:
|
||||
|
@ -2292,6 +2299,9 @@ definitions:
|
|||
rule:
|
||||
description: 优惠券使用规则
|
||||
type: string
|
||||
sms_content:
|
||||
description: 短信提示内容
|
||||
type: string
|
||||
user_type:
|
||||
description: 领取人限制:1-所有人 2-未付费用户 3-已付费用户 4-尊享会员
|
||||
type: integer
|
||||
|
@ -2301,6 +2311,7 @@ definitions:
|
|||
- category_number
|
||||
- erp_coupon_id
|
||||
- name
|
||||
- sms_content
|
||||
- user_type
|
||||
type: object
|
||||
models.ErpMarketingCouponListReq:
|
||||
|
|
Loading…
Reference in New Issue
Block a user