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.
303 lines
11 KiB
303 lines
11 KiB
package user
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/golangkit/formatime"
|
|
"github.com/jinzhu/gorm"
|
|
"github.com/shopspring/decimal"
|
|
"recook/internal/v2/model/recook/user"
|
|
"strconv"
|
|
)
|
|
|
|
var DayLogic = &dayLogic{}
|
|
|
|
type dayLogic struct {
|
|
}
|
|
|
|
// @Style 预估收益
|
|
// @Param userId uint true "支付用户id"
|
|
// @Param amount decimal.Decimal true "订单总金额"
|
|
// @Param payTime formatime.Second true "支付时间"
|
|
func (d *dayLogic) ExpectIncome(userId uint, amount decimal.Decimal, payTime formatime.Second, tx *gorm.DB) error {
|
|
recookUserDayExpectIncomeModel := &user.RecookUserDayExpectIncomeModel{}
|
|
date, err := strconv.Atoi(payTime.Time.Format("20060102"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
recookUserTreeModel := &user.RecookUserTreeModel{}
|
|
info := recookUserDayExpectIncomeModel.FindByUserIdAndDate(userId, date)
|
|
if info.Id == 0 {
|
|
// 批量插入数据
|
|
subQuery := recookUserDayExpectIncomeModel.GetDb().Table(recookUserDayExpectIncomeModel.TableName()+" as userTeamExpectIncome").
|
|
Select("userTree.root_id,?", date).
|
|
Joins("right join ? as userTree on userTeamExpectIncome.user_id = userTree.root_id and userTeamExpectIncome.day = ?", recookUserTreeModel.GetByUserIdSql(userId, "user_id,root_id"), date).
|
|
Where("userTeamExpectIncome.id is null").
|
|
SubQuery()
|
|
if err := recookUserDayExpectIncomeModel.CreateSubQuery("user_id,day", subQuery); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
// 使用事务
|
|
recookUserDayExpectIncomeModel.SetDb(tx)
|
|
// 更新统计
|
|
if err := recookUserDayExpectIncomeModel.Updates(map[string]interface{}{
|
|
"amount": gorm.Expr("amount + ?", amount),
|
|
}, "user_id in ? and day = ?", recookUserTreeModel.GetByUserIdSql(userId, "root_id"), date); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// @Style 预估收益退款
|
|
// @Param userId uint true "支付用户id"
|
|
// @Param amount decimal.Decimal true "订单总金额"
|
|
// @Param payTime formatime.Second true "支付时间"
|
|
func (d *dayLogic) ExpectIncomeRefund(userId uint, amount decimal.Decimal, payTime formatime.Second, tx *gorm.DB) error {
|
|
recookUserDayExpectIncomeModel := &user.RecookUserDayExpectIncomeModel{}
|
|
date, err := strconv.Atoi(payTime.Time.Format("20060102"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
recookUserTreeModel := &user.RecookUserTreeModel{}
|
|
// 使用事务
|
|
recookUserDayExpectIncomeModel.SetDb(tx)
|
|
// 更新统计
|
|
if err := recookUserDayExpectIncomeModel.Updates(map[string]interface{}{
|
|
"amount": gorm.Expr("amount - ?", amount),
|
|
}, "user_id in ? and day = ?", recookUserTreeModel.GetByUserIdSql(userId, "root_id"), date); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// @Style 预估自购收益
|
|
// @Param userId uint true "支付用户id"
|
|
// @Param amount decimal.Decimal true "佣金"
|
|
// @Param payTime formatime.Second true "支付时间"
|
|
func (d *dayLogic) PurchaseExpectIncome(userId uint, amount decimal.Decimal, totalAmount decimal.Decimal, payTime formatime.Second, tx *gorm.DB) error {
|
|
recookUserDayExpectIncomeModel := &user.RecookUserDayExpectIncomeModel{}
|
|
date, err := strconv.Atoi(payTime.Time.Format("20060102"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// 使用事务
|
|
recookUserDayExpectIncomeModel.SetDb(tx)
|
|
tx.Table(recookUserDayExpectIncomeModel.TableName()).Where("user_id=?", userId).Where("day=?", date).First(&recookUserDayExpectIncomeModel)
|
|
if recookUserDayExpectIncomeModel.Id > 0 {
|
|
// 更新统计
|
|
if err := recookUserDayExpectIncomeModel.Updates(map[string]interface{}{
|
|
"purchase_amount": gorm.Expr("purchase_amount + ?", amount),
|
|
"purchase_count": gorm.Expr("purchase_count + 1"),
|
|
"purchase_sales_volume": gorm.Expr("purchase_sales_volume + ?", totalAmount),
|
|
"not_recv_amount": gorm.Expr("not_recv_amount + ?", amount),
|
|
"not_recv_count": gorm.Expr("not_recv_count + 1"),
|
|
"not_recv_volume": gorm.Expr("not_recv_volume + ?", totalAmount),
|
|
}, "user_id = ? and day = ?", userId, date); err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
if err := tx.Create(&user.RecookUserDayExpectIncomeModel{
|
|
UserId: userId,
|
|
Day: date,
|
|
Amount: amount,
|
|
PurchaseAmount: amount,
|
|
PurchaseCount: 1,
|
|
PurchaseSalesVolume: totalAmount,
|
|
NotRecvAmount: amount,
|
|
NotRecvCount: 1,
|
|
NotRecvVolume: totalAmount,
|
|
}).Error; err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// @Style 预估自购收益退款
|
|
// @Param userId uint true "支付用户id"
|
|
// @Param amount decimal.Decimal true "佣金"
|
|
// @Param payTime formatime.Second true "支付时间"
|
|
func (d *dayLogic) PurchaseExpectIncomeRefund(userId uint, amount decimal.Decimal, orderCount int, totalAmount decimal.Decimal, payTime formatime.Second, tx *gorm.DB) error {
|
|
recookUserDayExpectIncomeModel := &user.RecookUserDayExpectIncomeModel{}
|
|
date, err := strconv.Atoi(payTime.Time.Format("20060102"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// 使用事务
|
|
recookUserDayExpectIncomeModel.SetDb(tx)
|
|
|
|
// 更新统计
|
|
if err := recookUserDayExpectIncomeModel.Updates(map[string]interface{}{
|
|
"purchase_amount": gorm.Expr("purchase_amount - ?", amount),
|
|
"purchase_count": gorm.Expr("purchase_count - ?", orderCount),
|
|
"purchase_sales_volume": gorm.Expr("purchase_sales_volume - ?", totalAmount),
|
|
"not_recv_amount": gorm.Expr("not_recv_amount - ?", amount),
|
|
"not_recv_count": gorm.Expr("not_recv_count - ?", orderCount),
|
|
"not_recv_volume": gorm.Expr("not_recv_volume - ?", totalAmount),
|
|
}, "user_id = ? and day = ?", userId, date); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// @Style 预估导购收益
|
|
// @Param userId uint true "支付用户id"
|
|
// @Param amount decimal.Decimal true "佣金"
|
|
// @Param payTime formatime.Second true "支付时间"
|
|
func (d *dayLogic) GuideExpectIncome(userId uint, amount decimal.Decimal, totalAmount decimal.Decimal, payTime formatime.Second, tx *gorm.DB) error {
|
|
recookUserDayExpectIncomeModel := &user.RecookUserDayExpectIncomeModel{}
|
|
date, err := strconv.Atoi(payTime.Time.Format("20060102"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// 使用事务
|
|
recookUserDayExpectIncomeModel.SetDb(tx)
|
|
tx.Table(recookUserDayExpectIncomeModel.TableName()).Where("user_id=?", userId).Where("day=?", date).First(&recookUserDayExpectIncomeModel)
|
|
if recookUserDayExpectIncomeModel.Id > 0 {
|
|
// 更新统计
|
|
if err := recookUserDayExpectIncomeModel.Updates(map[string]interface{}{
|
|
"guide_amount": gorm.Expr("guide_amount + ?", amount),
|
|
"guide_count": gorm.Expr("guide_count + 1"),
|
|
"guide_sales_volume": gorm.Expr("guide_sales_volume + ?", totalAmount),
|
|
"not_guide_amount": gorm.Expr("not_guide_amount + ?", amount),
|
|
"not_guide_count": gorm.Expr("not_guide_count + 1"),
|
|
"not_guide_volume": gorm.Expr("not_guide_volume + ?", totalAmount),
|
|
}, "user_id = ? and day = ?", userId, date); err != nil {
|
|
return err
|
|
}
|
|
} else {
|
|
err := tx.Create(&user.RecookUserDayExpectIncomeModel{
|
|
UserId: userId,
|
|
Day: date,
|
|
Amount: amount,
|
|
GuideAmount: amount,
|
|
GuideCount: 1,
|
|
GuideSalesVolume: totalAmount,
|
|
NotGuideAmount: amount,
|
|
NotGuideCount: 1,
|
|
NotGuideVolume: totalAmount,
|
|
}).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// @Style 预估导购收益退款
|
|
// @Param userId uint true "支付用户id"
|
|
// @Param amount decimal.Decimal true "佣金"
|
|
// @Param payTime formatime.Second true "支付时间"
|
|
func (d *dayLogic) GuideExpectIncomeRefund(userId uint, amount decimal.Decimal, orderCount int, totalAmount decimal.Decimal, payTime formatime.Second, tx *gorm.DB) error {
|
|
recookUserDayExpectIncomeModel := &user.RecookUserDayExpectIncomeModel{}
|
|
date, err := strconv.Atoi(payTime.Time.Format("20060102"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// 使用事务
|
|
recookUserDayExpectIncomeModel.SetDb(tx)
|
|
// 更新统计
|
|
if err := recookUserDayExpectIncomeModel.Updates(map[string]interface{}{
|
|
"guide_amount": gorm.Expr("guide_amount - ?", amount),
|
|
"guide_count": gorm.Expr("guide_count - ?", orderCount),
|
|
"guide_sales_volume": gorm.Expr("guide_sales_volume - ?", totalAmount),
|
|
"not_guide_amount": gorm.Expr("not_guide_amount - ?", amount),
|
|
"not_guide_count": gorm.Expr("not_guide_count - ?", orderCount),
|
|
"not_guide_volume": gorm.Expr("not_guide_volume - ?", totalAmount),
|
|
}, "user_id = ? and day = ?", userId, date); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// @Style 日自购收益记录
|
|
// @Param userId uint true "支付用户id"
|
|
// @Param totalAmount decimal.Decimal true "销售额"
|
|
// @Param amount decimal.Decimal true "佣金"
|
|
// @Param payTime formatime.Second true "支付时间"
|
|
func (d *dayLogic) PurchaseIncome(userId uint, totalAmount, amount decimal.Decimal, payTime formatime.Second, tx *gorm.DB) error {
|
|
recookUserDayIncomeModel := &user.RecookUserDayIncomeModel{}
|
|
date, _ := strconv.Atoi(payTime.Time.Format("20060102"))
|
|
day := recookUserDayIncomeModel.FindByUserIdAndDay(userId, date)
|
|
if day.Id == 0 {
|
|
data := user.RecookUserDayIncomeModel{
|
|
UserId: userId,
|
|
Day: date,
|
|
}
|
|
recookUserDayIncomeModel.Create(&data)
|
|
if data.Id == 0 {
|
|
return errors.New("网络错误")
|
|
}
|
|
}
|
|
|
|
// 使用事务
|
|
recookUserDayIncomeModel.SetDb(tx)
|
|
// 更新统计
|
|
if err := recookUserDayIncomeModel.Updates(map[string]interface{}{
|
|
"purchase_amount": gorm.Expr("purchase_amount + ?", amount),
|
|
"purchase_count": gorm.Expr("purchase_count + ?", 1),
|
|
"purchase_sales_volume": gorm.Expr("purchase_sales_volume + ?", totalAmount),
|
|
}, "user_id = ? and day = ?", userId, date); err != nil {
|
|
return err
|
|
}
|
|
|
|
var re user.RecookUserDayExpectIncomeModel
|
|
re.SetDb(tx)
|
|
// 更新统计
|
|
if err := re.Updates(map[string]interface{}{
|
|
"not_recv_amount": gorm.Expr("not_recv_amount - ?", amount), // 自购未到账收益更新
|
|
"not_recv_count": gorm.Expr("not_recv_count - 1"),
|
|
"not_recv_volume": gorm.Expr("not_recv_volume - ?", totalAmount),
|
|
}, "user_id = ? and day = ?", userId, date); err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// @Style 日导购收益记录
|
|
// @Param userId uint true "支付用户id"
|
|
// @Param totalAmount decimal.Decimal true "销售额"
|
|
// @Param amount decimal.Decimal true "佣金"
|
|
// @Param payTime formatime.Second true "支付时间"
|
|
func (d *dayLogic) GuideIncome(userId uint, totalAmount, amount decimal.Decimal, payTime formatime.Second, tx *gorm.DB) error {
|
|
recookUserDayIncomeModel := &user.RecookUserDayIncomeModel{}
|
|
date, _ := strconv.Atoi(payTime.Time.Format("20060102"))
|
|
day := recookUserDayIncomeModel.FindByUserIdAndDay(userId, date)
|
|
if day.Id == 0 {
|
|
data := user.RecookUserDayIncomeModel{
|
|
UserId: userId,
|
|
Day: date,
|
|
}
|
|
recookUserDayIncomeModel.Create(&data)
|
|
if data.Id == 0 {
|
|
return errors.New("网络错误")
|
|
}
|
|
}
|
|
// 使用事务
|
|
recookUserDayIncomeModel.SetDb(tx)
|
|
// 更新统计
|
|
if err := recookUserDayIncomeModel.Updates(map[string]interface{}{
|
|
"guide_amount": gorm.Expr("guide_amount + ?", amount),
|
|
"guide_count": gorm.Expr("guide_count + ?", 1),
|
|
"guide_sales_volume": gorm.Expr("guide_sales_volume + ?", totalAmount),
|
|
}, "user_id = ? and day = ?", userId, date); err != nil {
|
|
return err
|
|
}
|
|
|
|
var gre user.RecookUserDayExpectIncomeModel
|
|
gre.SetDb(tx)
|
|
// 更新统计
|
|
if err := gre.Updates(map[string]interface{}{
|
|
"not_guide_amount": gorm.Expr("not_guide_amount - ?", amount), // 自购未到账收益更新
|
|
"not_guide_count": gorm.Expr("not_guide_count - 1"),
|
|
"not_guide_volume": gorm.Expr("not_guide_volume - ?", totalAmount),
|
|
}, "user_id = ? and day = ?", userId, date); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|