180 lines
7.3 KiB
Go
180 lines
7.3 KiB
Go
package dao
|
||
|
||
import (
|
||
"time"
|
||
|
||
"git.rosy.net.cn/baseapi/utils"
|
||
"git.rosy.net.cn/jx-callback/business/model"
|
||
)
|
||
|
||
type StatisticsForOrdersExists struct {
|
||
StoreID int `orm:"column(store_id)" json:"storeId"`
|
||
}
|
||
|
||
type StatisticsReportForOrdersList struct {
|
||
StatisticsForOrdersExists
|
||
StoreName string `json:"name"` //门店名
|
||
OrderCounts int `json:"orderCounts"` //订单数
|
||
SalePrice int `json:"salePrice"` //GMV(售卖价)
|
||
ActualPayPrice int `json:"actualPayPrice"` //实付
|
||
ShopPrice int `json:"shopPrice"` //京西
|
||
DiscountMoney int `json:"discountMoney"` //优惠
|
||
DesiredFee int `json:"desiredFee"` //配送费
|
||
DistanceFreightMoney int `json:"distanceFreightMoney"` //远距离
|
||
WaybillTipMoney int `json:"waybillTipMoney"` //小费
|
||
TotalShopMoney int `json:"totalShopMoney"` //平台结算
|
||
PmSubsidyMoney int `json:"pmSubsidyMoney"` //平台补贴
|
||
EarningPrice int `json:"earningPrice"` //门店收益(预计收益)
|
||
TotalGrossProfit int `json:"totalGrossProfit"` //总毛利
|
||
ComGrossProfit float32 `json:"comGrossProfit"` //公司毛利
|
||
CityManagerGrossProfit float32 `json:"cityManagerGrossProfit"` //城市经理毛利
|
||
MarketManName string `json:"marketManName"` //市场负责人
|
||
OperatorName string `json:"operatorName"` //运营负责人
|
||
OperatorName2 string `json:"operatorName2"`
|
||
}
|
||
|
||
//查询统计订单信息
|
||
func GetStatisticsReportForOrders(db *DaoDB, storeIDs []int, fromDate time.Time, toDate time.Time) (statisticsReportForOrdersList []*StatisticsReportForOrdersList, err error) {
|
||
sql := `
|
||
SELECT
|
||
c.id store_id,
|
||
c.name store_name,
|
||
s.order_counts,
|
||
s.sale_price,
|
||
s.actual_pay_price,
|
||
s.shop_price,
|
||
s.discount_money,
|
||
s.desired_fee,
|
||
s.distance_freight_money,
|
||
s.waybill_tip_money,
|
||
s.total_shop_money,
|
||
s.pm_subsidy_money,
|
||
s.earning_price,
|
||
s.total_gross_profit,
|
||
IF(c.jx_brand_fee_factor = 0 AND c.market_add_fee_factor = 0,total_gross_profit,(total_gross_profit*c.jx_brand_fee_factor)/(c.jx_brand_fee_factor+market_add_fee_factor)) com_gross_profit,
|
||
IF(c.jx_brand_fee_factor = 0 AND c.market_add_fee_factor = 0,0,(total_gross_profit*c.market_add_fee_factor)/(c.jx_brand_fee_factor+market_add_fee_factor)) city_manager_gross_profit,
|
||
IF(mm.name <> '', mm.name, mm.user_id2) market_man_name,
|
||
IF(om.name <> '', om.name, om.user_id2) operator_name,
|
||
IF(om2.name <> '', om2.name, om2.user_id2) operator_name2
|
||
FROM store c
|
||
LEFT JOIN user mm ON mm.mobile <> '' AND mm.mobile = c.market_man_phone
|
||
LEFT JOIN user om ON om.mobile <> '' AND om.mobile = c.operator_phone
|
||
LEFT JOIN user om2 ON om2.mobile <> '' AND om2.mobile = c.operator_phone2
|
||
LEFT JOIN
|
||
(
|
||
SELECT
|
||
IF(a.jx_store_id <> 0,a.jx_store_id,store_id) store_id,
|
||
COUNT(*) order_counts,
|
||
SUM(sale_price) sale_price,
|
||
SUM(actual_pay_price) actual_pay_price,
|
||
SUM(shop_price) shop_price,
|
||
SUM(discount_money) discount_money,
|
||
SUM(desired_fee) desired_fee,
|
||
SUM(distance_freight_money) distance_freight_money,
|
||
SUM(IF(a.vendor_id = a.waybill_vendor_id,waybill_tip_money,0)) waybill_tip_money,
|
||
SUM(total_shop_money) total_shop_money,
|
||
SUM(pm_subsidy_money) pm_subsidy_money,
|
||
SUM(earning_price) earning_price,
|
||
SUM(total_shop_money-earning_price-desired_fee-distance_freight_money-waybill_tip_money-80) total_gross_profit
|
||
FROM goods_order a
|
||
LEFT JOIN waybill b ON IF(a.waybill_vendor_id = -1,a.vendor_order_id,a.vendor_waybill_id) = b.vendor_waybill_id
|
||
WHERE a.status != ?
|
||
`
|
||
sqlParams := []interface{}{
|
||
model.OrderStatusCanceled, //排除已取消的订单
|
||
}
|
||
if !utils.IsTimeZero(fromDate) && !utils.IsTimeZero(toDate) {
|
||
sql += ` AND a.order_created_at BETWEEN ? AND ?`
|
||
sqlParams = append(sqlParams, fromDate, toDate)
|
||
}
|
||
if len(storeIDs) > 0 {
|
||
sql += ` AND IF(a.jx_store_id != 0, a.jx_store_id, a.store_id) IN(` + GenQuestionMarks(len(storeIDs)) + `)`
|
||
sqlParams = append(sqlParams, storeIDs)
|
||
}
|
||
sql += `
|
||
GROUP BY 1
|
||
)s
|
||
ON s.store_id = c.id
|
||
`
|
||
if len(storeIDs) > 0 {
|
||
sql += ` WHERE c.id IN (` + GenQuestionMarks(len(storeIDs)) + `)`
|
||
sqlParams = append(sqlParams, storeIDs)
|
||
}
|
||
if err = GetRows(db, &statisticsReportForOrdersList, sql, sqlParams...); err == nil {
|
||
return statisticsReportForOrdersList, nil
|
||
}
|
||
return nil, err
|
||
}
|
||
|
||
//查询统计售后单信息
|
||
func GetGetStatisticsReportForAfsOrders(db *DaoDB, storeIDs []int, fromDate time.Time, toDate time.Time) (statisticsReportForOrdersList []*StatisticsReportForOrdersList, err error) {
|
||
sql := `
|
||
SELECT
|
||
c.id store_id,
|
||
c.name store_name,
|
||
s.order_counts,
|
||
s.sale_price,
|
||
s.actual_pay_price,
|
||
s.shop_price,
|
||
s.discount_money,
|
||
s.desired_fee,
|
||
s.distance_freight_money,
|
||
s.waybill_tip_money,
|
||
s.total_shop_money,
|
||
s.pm_subsidy_money,
|
||
s.earning_price,
|
||
s.total_gross_profit,
|
||
IF(c.jx_brand_fee_factor = 0 AND c.market_add_fee_factor = 0,total_gross_profit,(total_gross_profit*c.jx_brand_fee_factor)/(c.jx_brand_fee_factor+market_add_fee_factor)) com_gross_profit,
|
||
IF(c.jx_brand_fee_factor = 0 AND c.market_add_fee_factor = 0,0,(total_gross_profit*c.market_add_fee_factor)/(c.jx_brand_fee_factor+market_add_fee_factor)) city_manager_gross_profit,
|
||
IF(mm.name <> '', mm.name, mm.user_id2) market_man_name,
|
||
IF(om.name <> '', om.name, om.user_id2) operator_name,
|
||
IF(om2.name <> '', om2.name, om2.user_id2) operator_name2
|
||
FROM store c
|
||
LEFT JOIN user mm ON mm.mobile <> '' AND mm.mobile = c.market_man_phone
|
||
LEFT JOIN user om ON om.mobile <> '' AND om.mobile = c.operator_phone
|
||
LEFT JOIN user om2 ON om2.mobile <> '' AND om2.mobile = c.operator_phone2
|
||
LEFT JOIN
|
||
(
|
||
SELECT
|
||
IF(a.jx_store_id <> 0,a.jx_store_id,store_id) store_id,
|
||
COUNT(*) order_counts,
|
||
SUM(sale_price) sale_price,
|
||
SUM(actual_pay_price) actual_pay_price,
|
||
SUM(shop_price) shop_price,
|
||
SUM(discount_money) discount_money,
|
||
SUM(afs_freight_money) desired_fee,
|
||
SUM(distance_freight_money) distance_freight_money,
|
||
SUM(IF(a.vendor_id = a.waybill_vendor_id,waybill_tip_money,0)) waybill_tip_money,
|
||
SUM(total_shop_money) total_shop_money,
|
||
SUM(b.pm_subsidy_money) pm_subsidy_money,
|
||
SUM(earning_price) earning_price,
|
||
SUM(total_shop_money-earning_price-afs_freight_money-distance_freight_money-waybill_tip_money-80) total_gross_profit
|
||
FROM goods_order a JOIN afs_order b ON a.vendor_order_id = b.vendor_order_id
|
||
WHERE a.status != ?
|
||
`
|
||
sqlParams := []interface{}{
|
||
model.OrderStatusCanceled, //排除已取消的订单
|
||
}
|
||
if !utils.IsTimeZero(fromDate) && !utils.IsTimeZero(toDate) {
|
||
sql += ` AND a.order_created_at BETWEEN ? AND ?`
|
||
sqlParams = append(sqlParams, fromDate, toDate)
|
||
}
|
||
if len(storeIDs) > 0 {
|
||
sql += ` AND IF(a.jx_store_id != 0, a.jx_store_id, a.store_id) IN(` + GenQuestionMarks(len(storeIDs)) + `)`
|
||
sqlParams = append(sqlParams, storeIDs)
|
||
}
|
||
sql += `
|
||
GROUP BY 1
|
||
)s
|
||
ON s.store_id = c.id
|
||
`
|
||
if len(storeIDs) > 0 {
|
||
sql += ` WHERE c.id IN (` + GenQuestionMarks(len(storeIDs)) + `)`
|
||
sqlParams = append(sqlParams, storeIDs)
|
||
}
|
||
if err = GetRows(db, &statisticsReportForOrdersList, sql, sqlParams...); err == nil {
|
||
return statisticsReportForOrdersList, nil
|
||
}
|
||
return nil, err
|
||
}
|