优化交易创建流程,客户管理新增折扣与来源字段及功能
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"gorm.io/gen/field"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
@@ -16,6 +17,80 @@ var Coupon = &couponService{}
|
||||
|
||||
type couponService struct{}
|
||||
|
||||
func (s *couponService) All() (result []*m.Coupon, err error) {
|
||||
return q.Coupon.Find()
|
||||
}
|
||||
|
||||
func (s *couponService) Page(req *core.PageReq) (result []*m.Coupon, count int64, err error) {
|
||||
return q.Coupon.FindByPage(req.GetOffset(), req.GetLimit())
|
||||
}
|
||||
|
||||
func (s *couponService) Create(data CreateCouponData) error {
|
||||
return q.Coupon.Create(&m.Coupon{
|
||||
UserID: data.UserID,
|
||||
Code: data.Code,
|
||||
Remark: data.Remark,
|
||||
Amount: data.Amount,
|
||||
MinAmount: data.MinAmount,
|
||||
Status: m.CouponStatusUnused,
|
||||
ExpireAt: data.ExpireAt,
|
||||
})
|
||||
}
|
||||
|
||||
type CreateCouponData struct {
|
||||
UserID *int32 `json:"user_id"`
|
||||
Code string `json:"code" validate:"required"`
|
||||
Remark *string `json:"remark"`
|
||||
Amount decimal.Decimal `json:"amount" validate:"required"`
|
||||
MinAmount decimal.Decimal `json:"min_amount"`
|
||||
ExpireAt *time.Time `json:"expire_at"`
|
||||
}
|
||||
|
||||
func (s *couponService) Update(data UpdateCouponData) error {
|
||||
do := make([]field.AssignExpr, 0)
|
||||
|
||||
if data.UserID != nil {
|
||||
do = append(do, q.Coupon.UserID.Value(*data.UserID))
|
||||
}
|
||||
if data.Code != nil {
|
||||
do = append(do, q.Coupon.Code.Value(*data.Code))
|
||||
}
|
||||
if data.Remark != nil {
|
||||
do = append(do, q.Coupon.Remark.Value(*data.Remark))
|
||||
}
|
||||
if data.Amount != nil {
|
||||
do = append(do, q.Coupon.Amount.Value(*data.Amount))
|
||||
}
|
||||
if data.MinAmount != nil {
|
||||
do = append(do, q.Coupon.MinAmount.Value(*data.MinAmount))
|
||||
}
|
||||
if data.Status != nil {
|
||||
do = append(do, q.Coupon.Status.Value(int(*data.Status)))
|
||||
}
|
||||
if data.ExpireAt != nil {
|
||||
do = append(do, q.Coupon.ExpireAt.Value(*data.ExpireAt))
|
||||
}
|
||||
|
||||
_, err := q.Coupon.Where(q.Coupon.ID.Eq(data.ID)).UpdateSimple(do...)
|
||||
return err
|
||||
}
|
||||
|
||||
type UpdateCouponData struct {
|
||||
ID int32 `json:"id" validate:"required"`
|
||||
UserID *int32 `json:"user_id"`
|
||||
Code *string `json:"code"`
|
||||
Remark *string `json:"remark"`
|
||||
Amount *decimal.Decimal `json:"amount"`
|
||||
MinAmount *decimal.Decimal `json:"min_amount"`
|
||||
Status *m.CouponStatus `json:"status"`
|
||||
ExpireAt *time.Time `json:"expire_at"`
|
||||
}
|
||||
|
||||
func (s *couponService) Delete(id int32) error {
|
||||
_, err := q.Coupon.Where(q.Coupon.ID.Eq(id)).UpdateColumn(q.Coupon.DeletedAt, time.Now())
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *couponService) GetCouponAvailableByCode(code string, amount decimal.Decimal, uid *int32) (*m.Coupon, error) {
|
||||
// 获取优惠券
|
||||
coupon, err := q.Coupon.Where(
|
||||
|
||||
Reference in New Issue
Block a user