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

124 lines
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" binding:"required" label:"支付宝开放平台-第三方应用-appid"`
  73. PayAliPrivateKey string `json:"pay_ali_private_key" binding:"required" label:"支付宝开放平台-第三方应用-接口加签-应用私钥"`
  74. PayAliPublicKey string `json:"pay_ali_public_key" binding:"required" 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. TotalPrice: total,
  99. Kind: req.Kind,
  100. OutTradeNo: outTradeNo,
  101. TradeNo: resp.TradeNo,
  102. State: enum2.CentralKitchenForSchoolPackageOrdStateForWait,
  103. OrdState: enum2.CentralKitchenForSchoolPackageOrdOrdStateForWait,
  104. ReqContent: string(utils.Serialize(req)),
  105. WithDayData: string(utils.Serialize(data)),
  106. CreateAt: now.Format("2006-01-02 15:04:05"),
  107. UpdateAt: now.Format("2006-01-02 15:04:05"),
  108. })
  109. if err != nil {
  110. _ = session.Rollback()
  111. return
  112. }
  113. session.Commit()
  114. tradeNo = resp.TradeNo
  115. return
  116. }