附近小店
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

svc_coupon.go 2.2 KiB

1 개월 전
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/db/model"
  5. "applet/app/e"
  6. "applet/app/utils"
  7. "fmt"
  8. "github.com/gin-gonic/gin"
  9. "github.com/jinzhu/copier"
  10. "time"
  11. )
  12. func CouponReceive(c *gin.Context) {
  13. var arg map[string]string
  14. if err := c.ShouldBindJSON(&arg); err != nil {
  15. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  16. return
  17. }
  18. coupon := db.GetCouponById(MasterDb(c), arg["id"])
  19. if coupon == nil {
  20. e.OutErr(c, 400, e.NewErr(400, "优惠券不存在"))
  21. return
  22. }
  23. user := GetUser(c)
  24. count, _ := MasterDb(c).Where("uid=? and merchant_scheme_id=?", user.Info.Uid, arg["id"]).Count(&model.CommunityTeamCouponUser{})
  25. if count > 0 {
  26. e.OutErr(c, 400, e.NewErr(400, "您已领取过"))
  27. return
  28. }
  29. var data model.CommunityTeamCouponUser
  30. err := copier.Copy(&data, coupon)
  31. fmt.Println(err)
  32. data.MerchantSchemeId = int(data.Id)
  33. data.Id = 0
  34. data.IsUse = 1
  35. data.Uid = user.Info.Uid
  36. now := time.Now()
  37. data.CreateTime = time.Now()
  38. data.UpdateTime = time.Now()
  39. if coupon.ValidTimeType == 2 {
  40. data.ValidTimeStart = now
  41. data.ValidTimeEnd = time.Unix(now.Unix()+int64(coupon.ValidDayNum)*86400, 0)
  42. }
  43. MasterDb(c).Insert(&data)
  44. e.OutSuc(c, "success", nil)
  45. return
  46. }
  47. func Coupon(c *gin.Context) {
  48. var arg map[string]string
  49. if err := c.ShouldBindJSON(&arg); err != nil {
  50. e.OutErr(c, e.ERR_INVALID_ARGS, err)
  51. return
  52. }
  53. cate := db.GetCoupon(MasterDb(c), arg)
  54. list := make([]map[string]string, 0)
  55. if cate != nil {
  56. stateMap := []string{"", "立减", "满减", "折扣"}
  57. scheme, host := ImageBucket(c)
  58. for _, v := range *cate {
  59. tmp := map[string]string{
  60. "id": utils.IntToStr(v.Id),
  61. "name": v.Name,
  62. "info": v.Info,
  63. "type": stateMap[v.Kind],
  64. "num": utils.IntToStr(v.Num),
  65. "valid_time": "",
  66. "img_url": ImageFormatWithBucket(scheme, host, v.Img),
  67. "create_at": v.CreatedTime.Format("2006-01-02 15:04:05"),
  68. }
  69. if v.ValidTimeType == 1 {
  70. tmp["valid_time"] = v.ValidTimeStart.Format("2006.01.02") + "-" + v.ValidTimeEnd.Format("2006.01.02")
  71. } else {
  72. tmp["valid_time"] = "领取后" + utils.IntToStr(v.ValidDayNum) + "天有效"
  73. }
  74. list = append(list, tmp)
  75. }
  76. }
  77. e.OutSuc(c, list, nil)
  78. return
  79. }