Files
jx-callback/business/model/dao/report.go
2019-10-31 13:33:38 +08:00

113 lines
4.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package dao
import (
"strconv"
"time"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/model"
)
type StatisticsForOrdersExists struct {
StoreID int `orm:"column(storeID)" json:"storeID"`
}
type StatisticsReportForOrdersList struct {
StatisticsForOrdersExists
OrderCounts int `orm:"column(orderCounts)" json:"orderCounts"` //订单数
SalePrice int `orm:"column(salePrice)" json:"salePrice"` //GMV售卖价
ActualPayPrice int `orm:"column(actualPayPrice)" json:"actualPayPrice"` //实付
ShopPrice int `orm:"column(shopPrice)" json:"shopPrice"` //京西
DiscountMoney int `orm:"column(discountMoney)" json:"discountMoney"` //优惠
DesiredFee int `orm:"column(desiredFee)" json:"desiredFee"` //配送费
DistanceFreightMoney int `orm:"column(distanceFreightMoney)" json:"distanceFreightMoney"` //远距离
WaybillTipMoney int `orm:"column(waybillTipMoney)" json:"waybillTipMoney"` //
TotalShopMoney int `orm:"column(totalShopMoney)" json:"totalShopMoney"`
PmSubsidyMoney int `orm:"column(pmSubsidyMoney)" json:"pmSubsidyMoney"`
EarningPrice int `orm:"column(earningPrice)" json:"earningPrice"`
TotalGrossProfit int `orm:"column(totalGrossProfit)" json:"totalGrossProfit"`
ComGrossProfit float32 `orm:"column(comGrossProfit)" json:"comGrossProfit"`
CityManagerGrossProfit float32 `orm:"column(cityManagerGrossProfit)" json:"cityManagerGrossProfit"`
}
//是否存在这个门店
func IsStoreExist(db *DaoDB, storeID int) (StatisticsForOrdersExists *StatisticsForOrdersExists, err error) {
sql := `
SELECT *
FROM store
WHERE id = ?
`
sqlParams := []interface{}{}
sqlParams = append(sqlParams, storeID)
if err = GetRow(db, &StatisticsForOrdersExists, sql, sqlParams...); err == nil {
return StatisticsForOrdersExists, nil
}
return nil, err
}
//查询条件内是否存在订单
func GetStatisticsForOrdersExist(db *DaoDB, storeID int) (StatisticsForOrdersExists *StatisticsForOrdersExists, err error) {
sql := `
SELECT store_id
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.store_id = ?
GROUP BY a.store_id
`
sqlParams := []interface{}{}
sqlParams = append(sqlParams, storeID)
if err = GetRow(db, &StatisticsForOrdersExists, sql, sqlParams...); err == nil {
return StatisticsForOrdersExists, nil
}
return nil, err
}
//查询统计订单信息
func GetStatisticsReportForOrders(db *DaoDB, storeIDs []int, fromDate time.Time, toDate time.Time) (statisticsReportForOrdersList []*StatisticsReportForOrdersList, err error) {
//排除已取消的订单
status := strconv.Itoa(model.OrderStatusCanceled)
sql := `
SELECT s.*,
if(c.jx_brand_fee_factor = 0 AND c.market_add_fee_factor = 0,totalGrossProfit,(totalGrossProfit*c.jx_brand_fee_factor)/(c.jx_brand_fee_factor+market_add_fee_factor)) comGrossProfit,
if(c.jx_brand_fee_factor = 0 AND c.market_add_fee_factor = 0,0,(totalGrossProfit*c.market_add_fee_factor)/(c.jx_brand_fee_factor+market_add_fee_factor)) cityManagerGrossProfit
FROM store c,(
SELECT
a.store_id storeID,
count(*) orderCounts,
sum(sale_price) salePrice,
sum(actual_pay_price) actualPayPrice,
sum(shop_price) shopPrice,
sum(discount_money) discountMoney,
sum(desired_fee) desiredFee,
sum(distance_freight_money) distanceFreightMoney,
sum(waybill_tip_money) waybillTipMoney,
sum(total_shop_money) totalShopMoney,
sum(pm_subsidy_money) pmSubsidyMoney,
sum(earning_price) EarningPrice,
sum(total_shop_money-earning_price-desired_fee-distance_freight_money-waybill_tip_money-80) totalGrossProfit
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 != ` + status + `
`
sqlParams := []interface{}{}
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 a.store_id in(` + GenQuestionMarks(len(storeIDs)) + `)
`
sqlParams = append(sqlParams, storeIDs)
}
sql += `
GROUP BY a.store_id
)s
WHERE s.storeID = c.id
`
if err = GetRows(db, &statisticsReportForOrdersList, sql, sqlParams...); err == nil {
return statisticsReportForOrdersList, nil
}
return nil, err
}