You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
158 lines
4.9 KiB
158 lines
4.9 KiB
9 months ago
|
package service
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"pure/dto"
|
||
|
"pure/global"
|
||
|
"pure/model"
|
||
|
"pure/model/request"
|
||
|
"pure/utils"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func GetBillData(userId string, info *request.GetBillDataParams) (dto.BillData, error) {
|
||
|
var (
|
||
|
err error
|
||
|
result dto.BillData
|
||
|
)
|
||
|
now := time.Now()
|
||
|
switch info.Uint {
|
||
|
case "1": //累计
|
||
|
var statistic model.DtStatisticOrder
|
||
|
statistic, err = getStatisticsOrderInfluence(userId, "", nil)
|
||
|
if err != nil {
|
||
|
return result, err
|
||
|
}
|
||
|
result.Income = statistic.SettleReward
|
||
|
result.OrderMoney = statistic.OrderMoney
|
||
|
result.OrderNum = statistic.OrderNum
|
||
|
result.CancelOrderNum = statistic.OrderCancelNum
|
||
|
result.CancelOrderMoney = statistic.OrderCancelMoney
|
||
|
_, result.RewardUnfinished = getMissionClaimRewardUnfinished(userId)
|
||
|
|
||
|
result.FlowIncome, result.FlowExpend, err = getBillFlowData(userId, &request.SearchBillParams{Uint: "1"})
|
||
|
case "2":
|
||
|
var (
|
||
|
timeArea = make([]string, 2)
|
||
|
statistic model.DtStatisticOrder
|
||
|
)
|
||
|
timeArea[0] = now.AddDate(0, 0, -1).Format(utils.DateFormat)
|
||
|
timeArea[1] = now.Format(utils.DateFormat)
|
||
|
statistic, err = getStatisticsOrderInfluence(userId, "", timeArea)
|
||
|
if err != nil {
|
||
|
return result, err
|
||
|
}
|
||
|
result.Income = statistic.SettleReward
|
||
|
result.OrderMoney = statistic.OrderMoney
|
||
|
result.OrderNum = statistic.OrderNum
|
||
|
result.CancelOrderNum = statistic.OrderCancelNum
|
||
|
result.CancelOrderMoney = statistic.OrderCancelMoney
|
||
|
|
||
|
result.FlowIncome, result.FlowExpend, err = getBillFlowData(userId, &request.SearchBillParams{Uint: "2"})
|
||
|
case "3":
|
||
|
var (
|
||
|
timeArea = make([]string, 2)
|
||
|
statistic model.DtStatisticOrder
|
||
|
)
|
||
|
timeArea[0] = now.AddDate(0, 0, -29).Format(utils.DateFormat)
|
||
|
timeArea[1] = now.AddDate(0, 0, 1).Format(utils.DateFormat)
|
||
|
statistic, err = getStatisticsOrderInfluence(userId, "", timeArea)
|
||
|
if err != nil {
|
||
|
return result, err
|
||
|
}
|
||
|
result.Income = statistic.SettleReward
|
||
|
result.OrderMoney = statistic.OrderMoney
|
||
|
result.OrderNum = statistic.OrderNum
|
||
|
result.CancelOrderNum = statistic.OrderCancelNum
|
||
|
result.CancelOrderMoney = statistic.OrderCancelMoney
|
||
|
result.FlowIncome, result.FlowExpend, err = getBillFlowData(userId, &request.SearchBillParams{Uint: "3"})
|
||
|
default:
|
||
|
return dto.BillData{}, errors.New("uint取值有误")
|
||
|
}
|
||
|
return result, err
|
||
|
}
|
||
|
|
||
|
func getBillData(userId string, info *request.SearchBillParams) (float64, error) {
|
||
|
var (
|
||
|
err error
|
||
|
result float64
|
||
|
)
|
||
|
db := global.MG_DB.Model(&model.Bill{}).Where("user_id = ?", userId)
|
||
|
now := time.Now()
|
||
|
switch info.Uint {
|
||
|
case "2": // 昨天
|
||
|
db = db.Where("created_at >= ? AND created_at < ?", now.AddDate(0, 0, -1).Format(utils.DateFormat), now.Format(utils.DateFormat))
|
||
|
case "3": // 30天
|
||
|
db = db.Where("created_at >= ? AND created_at < ?", now.AddDate(0, 0, -29).Format(utils.DateFormat), now.AddDate(0, 0, 1).Format(utils.DateFormat))
|
||
|
}
|
||
|
if info.Status != "" {
|
||
|
db = db.Where("`status` = ?", info.Status)
|
||
|
}
|
||
|
err = db.Select("SUM(price) as price").Scan(&result).Error
|
||
|
if err != nil {
|
||
|
return 0, err
|
||
|
}
|
||
|
return result, nil
|
||
|
}
|
||
|
|
||
|
func getBillFlowData(userId string, info *request.SearchBillParams) (income float64, expend float64, err error) {
|
||
|
var list []model.BillFlowData
|
||
|
db := global.MG_DB.Model(&model.Bill{}).Where("user_id = ?", userId)
|
||
|
now := time.Now()
|
||
|
switch info.Uint {
|
||
|
case "2": // 昨天
|
||
|
db = db.Where("created_at >= ? AND created_at < ?", now.AddDate(0, 0, -1).Format(utils.DateFormat), now.Format(utils.DateFormat))
|
||
|
case "3": // 30天
|
||
|
db = db.Where("created_at >= ? AND created_at < ?", now.AddDate(0, 0, -29).Format(utils.DateFormat), now.AddDate(0, 0, 1).Format(utils.DateFormat))
|
||
|
}
|
||
|
if info.Status != "" {
|
||
|
db = db.Where("`status` = ?", info.Status)
|
||
|
}
|
||
|
err = db.Select("`status`,SUM(price) as price").Group("`status`").Find(&list).Error
|
||
|
if err != nil {
|
||
|
return 0, 0, err
|
||
|
}
|
||
|
for _, data := range list {
|
||
|
if data.Status == 1 {
|
||
|
expend = data.Price
|
||
|
} else {
|
||
|
income = data.Price
|
||
|
}
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
|
func GetBillList(userId string, info *request.SearchBillParams) ([]model.Bill, error) {
|
||
|
var (
|
||
|
err error
|
||
|
result []model.Bill
|
||
|
)
|
||
|
limit := info.PageSize
|
||
|
offset := info.PageSize * (info.Page - 1)
|
||
|
db := global.MG_DB.Model(&model.Bill{}).Where("user_id = ?", userId)
|
||
|
now := time.Now()
|
||
|
switch info.Uint {
|
||
|
case "2": // 昨天
|
||
|
db = db.Where("created_at >= ? AND created_at < ?", now.AddDate(0, 0, -1).Format(utils.DateFormat), now.Format(utils.DateFormat))
|
||
|
case "3": // 30天
|
||
|
db = db.Where("created_at >= ? AND created_at < ?", now.AddDate(0, 0, -29).Format(utils.DateFormat), now.AddDate(0, 0, 1).Format(utils.DateFormat))
|
||
|
}
|
||
|
if info.Status != "" {
|
||
|
db = db.Where("`status` = ?", info.Status)
|
||
|
}
|
||
|
err = db.Order("id DESC").Offset(offset).Limit(limit).Find(&result).Error
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
return result, nil
|
||
|
}
|
||
|
|
||
|
func getBillView(orderId string) (model.BillView, error) {
|
||
|
var (
|
||
|
err error
|
||
|
result model.BillView
|
||
|
)
|
||
|
err = global.MG_DB.Model(&model.Bill{}).Where("order_id = ?", orderId).Select("id,order_id,transaction_id").First(&result).Error
|
||
|
return result, err
|
||
|
}
|