From ddfa6b5de07bc820a76c6e5db03531c85499c8a3 Mon Sep 17 00:00:00 2001 From: chenlin Date: Fri, 20 Dec 2024 10:17:24 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=20=E8=8E=B7=E5=8F=96=E9=87=87?= =?UTF-8?q?=E8=B4=AD=E9=9C=80=E6=B1=82=E6=8E=A5=E5=8F=A3=EF=BC=8C=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E4=B8=8A=E5=91=A8=E9=94=80=E9=87=8F=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=EF=BC=9Alast=5Fweek=5Fsales=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/models/purchase.go | 100 ++++++++++++++++++++++++++++++++++- docs/docs.go | 14 +++++ docs/swagger.json | 14 +++++ docs/swagger.yaml | 11 ++++ 4 files changed, 137 insertions(+), 2 deletions(-) diff --git a/app/admin/models/purchase.go b/app/admin/models/purchase.go index 23afad3..d90cb12 100644 --- a/app/admin/models/purchase.go +++ b/app/admin/models/purchase.go @@ -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() diff --git a/docs/docs.go b/docs/docs.go index 8fbb551..7d479a2 100644 --- a/docs/docs.go +++ b/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" diff --git a/docs/swagger.json b/docs/swagger.json index 1c8e819..bd66c96 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -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" diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 6b32715..749e907 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -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: