智慧食堂
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package svc
  2. import (
  3. "applet/app/customer/md"
  4. "applet/app/db"
  5. "applet/app/db/model"
  6. enum2 "applet/app/enum"
  7. "applet/app/utils"
  8. "errors"
  9. "github.com/gin-gonic/gin"
  10. "time"
  11. )
  12. func BuyPackage(c *gin.Context, req md.BuyPackageReq) (outTradeNo, tradeNo, total string, err error) {
  13. user := GetUser(c)
  14. session := db.Db.NewSession()
  15. defer session.Close()
  16. session.Begin()
  17. if err != nil {
  18. _ = session.Rollback()
  19. return
  20. }
  21. //1、判断是否为教师
  22. isTeacher := false
  23. userIdentityDb := db.UserIdentityDb{}
  24. userIdentityDb.Set(0)
  25. userIdentity, err := userIdentityDb.GetUserIdentity(req.UserIdentityId)
  26. if err != nil {
  27. return
  28. }
  29. if userIdentity == nil {
  30. err = errors.New("未查询到对应身份记录")
  31. return
  32. }
  33. if userIdentity.Kind == enum2.UserIdentityForCentralKitchenForTeacher {
  34. isTeacher = true
  35. }
  36. //2、计算数据(1:按学期购买 2:按月购买 3:按天购买)
  37. var totalPrice float64
  38. var data []*model.CentralKitchenForSchoolUserWithDay
  39. if req.Kind == 1 {
  40. totalPrice, data, err = CalcBySchoolTerm(user.Id, isTeacher, req)
  41. if err != nil {
  42. return
  43. }
  44. }
  45. if req.Kind == 2 {
  46. totalPrice, data, err = CalcByMonth(user.Id, isTeacher, req)
  47. if err != nil {
  48. return
  49. }
  50. }
  51. if req.Kind == 3 {
  52. totalPrice, data, err = CalcByDay(user.Id, isTeacher, req)
  53. if err != nil {
  54. return
  55. }
  56. }
  57. total = utils.Float64ToStr(totalPrice)
  58. //3、生成订单号
  59. outTradeNo = utils.OrderUUID(user.Id)
  60. //4、请求 alipay.trade.create(统一收单交易创建接口)
  61. sysCfgDb := db.SysCfgDb{}
  62. sysCfgDb.Set()
  63. sysCfg := sysCfgDb.SysCfgFindWithDb(enum2.OpenAlipayAppid, enum2.OpenAlipayAppPrivateKey, enum2.OpenAlipayPublicKey, enum2.JsapiPayAppAutToken)
  64. err, resp := CurlAlipayTradeCreate(md.CurlAlipayTradeCreateReq{
  65. Config: struct {
  66. PayAliAppId string `json:"pay_ali_app_id" binding:"required" label:"支付宝开放平台-第三方应用-appid"`
  67. PayAliPrivateKey string `json:"pay_ali_private_key" binding:"required" label:"支付宝开放平台-第三方应用-接口加签-应用私钥"`
  68. PayAliPublicKey string `json:"pay_ali_public_key" binding:"required" label:"支付宝开放平台-第三方应用-接口加签-支付宝公钥"`
  69. }{
  70. PayAliAppId: sysCfg[enum2.OpenAlipayAppid],
  71. PayAliPrivateKey: sysCfg[enum2.OpenAlipayAppPrivateKey],
  72. PayAliPublicKey: sysCfg[enum2.OpenAlipayPublicKey],
  73. },
  74. BuyerId: user.UserId,
  75. TotalAmount: total,
  76. OutTradeNo: outTradeNo,
  77. Subject: "购买食堂套餐",
  78. AppAuthToken: sysCfg[enum2.JsapiPayAppAutToken],
  79. })
  80. if err != nil {
  81. return
  82. }
  83. //5、插入订单记录
  84. now := time.Now()
  85. centralKitchenForSchoolPackageOrd := db.CentralKitchenForSchoolPackageOrd{}
  86. centralKitchenForSchoolPackageOrd.Set(outTradeNo)
  87. _, err = centralKitchenForSchoolPackageOrd.CentralKitchenForSchoolPackageOrdInsertBySession(session, &model.CentralKitchenForSchoolPackageOrd{
  88. EnterpriseId: req.EnterpriseId,
  89. Uid: user.Id,
  90. UserIdentityId: req.UserIdentityId,
  91. TotalPrice: total,
  92. Kind: req.Kind,
  93. OutTradeNo: outTradeNo,
  94. TradeNo: resp.TradeNo,
  95. State: enum2.CentralKitchenForSchoolPackageOrdStateForWait,
  96. OrdState: enum2.CentralKitchenForSchoolPackageOrdOrdStateForWait,
  97. ReqContent: string(utils.Serialize(req)),
  98. WithDayData: string(utils.Serialize(data)),
  99. CreateAt: now.Format("2006-01-02 15:04:05"),
  100. UpdateAt: now.Format("2006-01-02 15:04:05"),
  101. })
  102. if err != nil {
  103. _ = session.Rollback()
  104. return
  105. }
  106. session.Commit()
  107. tradeNo = resp.TradeNo
  108. return
  109. }