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.

53 lines
1.2 KiB

6 months ago
  1. package coupon
  2. import (
  3. "CouponBackendGo/internal/dao"
  4. "CouponBackendGo/internal/model/dto"
  5. "CouponBackendGo/internal/service"
  6. "context"
  7. "github.com/gogf/gf/v2/os/gtime"
  8. )
  9. type (
  10. sCoupon struct{}
  11. )
  12. func init() {
  13. service.RegisterCoupon(New())
  14. }
  15. func New() service.ICoupon {
  16. return &sCoupon{}
  17. }
  18. func (s *sCoupon) GetCouponList(ctx context.Context, pageNo int, pageSize int) (couponList []*dto.DtoCoupon, err error) {
  19. err = dao.Coupon.Ctx(ctx).Where("deleted", 0).Order("updated_at desc, end_time desc").Page(pageNo, pageSize).Scan(&couponList)
  20. if err != nil {
  21. return nil, err
  22. }
  23. return
  24. }
  25. func (s *sCoupon) GetCouponListTotal(ctx context.Context) (total int, err error) {
  26. total, err = dao.Coupon.Ctx(ctx).Where("deleted", 0).Count()
  27. if err != nil {
  28. return 0, err
  29. }
  30. return
  31. }
  32. func (s *sCoupon) GetCoupon(ctx context.Context, id int) (coupon *dto.DtoCoupon, err error) {
  33. err = dao.Coupon.Ctx(ctx).WherePri(id).Scan(&coupon)
  34. coupon.Status = 0 //待使用期
  35. if coupon.StartTime.Unix() <= gtime.Now().Unix() {
  36. if coupon.EndTime.Unix() <= gtime.Now().Unix() {
  37. coupon.Status = 2 //使用期后
  38. } else {
  39. coupon.Status = 1 //使用期中
  40. }
  41. }
  42. if err != nil {
  43. return nil, err
  44. }
  45. return
  46. }