智慧食堂
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

125 rader
3.7 KiB

  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:按天购买 4:补餐)
  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. if req.Kind == 4 {
  58. totalPrice, data, err = CalcByDay(user.Id, isTeacher, req)
  59. if err != nil {
  60. return
  61. }
  62. }
  63. total = utils.Float64ToStr(totalPrice)
  64. //3、生成订单号
  65. outTradeNo = utils.OrderUUID(user.Id)
  66. //4、请求 alipay.trade.create(统一收单交易创建接口)
  67. sysCfgDb := db.SysCfgDb{}
  68. sysCfgDb.Set()
  69. sysCfg := sysCfgDb.SysCfgFindWithDb(enum2.OpenAppletAppid, enum2.OpenAlipayAppid, enum2.OpenAlipayAppPrivateKey, enum2.OpenAlipayPublicKey, enum2.JsapiPayAppAutToken)
  70. err, resp := CurlAlipayTradeCreate(md.CurlAlipayTradeCreateReq{
  71. Config: struct {
  72. PayAliAppId string `json:"pay_ali_app_id" label:"支付宝开放平台-第三方应用-appid"`
  73. PayAliPrivateKey string `json:"pay_ali_private_key" label:"支付宝开放平台-第三方应用-接口加签-应用私钥"`
  74. PayAliPublicKey string `json:"pay_ali_public_key" label:"支付宝开放平台-第三方应用-接口加签-支付宝公钥"`
  75. }{
  76. PayAliAppId: sysCfg[enum2.OpenAlipayAppid],
  77. PayAliPrivateKey: sysCfg[enum2.OpenAlipayAppPrivateKey],
  78. PayAliPublicKey: sysCfg[enum2.OpenAlipayPublicKey],
  79. },
  80. OpAppId: sysCfg[enum2.OpenAppletAppid],
  81. BuyerId: user.UserId,
  82. TotalAmount: total,
  83. OutTradeNo: outTradeNo,
  84. Subject: "购买食堂套餐",
  85. AppAuthToken: sysCfg[enum2.JsapiPayAppAutToken],
  86. })
  87. if err != nil {
  88. return
  89. }
  90. //5、插入订单记录
  91. now := time.Now()
  92. centralKitchenForSchoolPackageOrd := db.CentralKitchenForSchoolPackageOrd{}
  93. centralKitchenForSchoolPackageOrd.Set(outTradeNo)
  94. _, err = centralKitchenForSchoolPackageOrd.CentralKitchenForSchoolPackageOrdInsertBySession(session, &model.CentralKitchenForSchoolPackageOrd{
  95. EnterpriseId: req.EnterpriseId,
  96. Uid: user.Id,
  97. UserIdentityId: req.UserIdentityId,
  98. UserIdentityName: userIdentity.Name,
  99. TotalPrice: total,
  100. Kind: req.Kind,
  101. OutTradeNo: outTradeNo,
  102. TradeNo: resp.TradeNo,
  103. State: enum2.CentralKitchenForSchoolPackageOrdStateForWait,
  104. OrdState: enum2.CentralKitchenForSchoolPackageOrdOrdStateForWait,
  105. ReqContent: string(utils.Serialize(req)),
  106. WithDayData: string(utils.Serialize(data)),
  107. CreateAt: now.Format("2006-01-02 15:04:05"),
  108. UpdateAt: now.Format("2006-01-02 15:04:05"),
  109. })
  110. if err != nil {
  111. _ = session.Rollback()
  112. return
  113. }
  114. session.Commit()
  115. tradeNo = resp.TradeNo
  116. return
  117. }