智慧食堂
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

232 wiersze
7.0 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.Identity == 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. }
  118. func NursingHomeBuyPackageReq(c *gin.Context, req md.NursingHomeBuyPackageReq) (outTradeNo, tradeNo, total string, err error) {
  119. user := GetUser(c)
  120. session := db.Db.NewSession()
  121. defer session.Close()
  122. session.Begin()
  123. if err != nil {
  124. _ = session.Rollback()
  125. return
  126. }
  127. userIdentityDb := db.UserIdentityDb{}
  128. userIdentityDb.Set(0)
  129. userIdentity, err := userIdentityDb.GetUserIdentity(req.UserIdentityId)
  130. if err != nil {
  131. return
  132. }
  133. if userIdentity == nil {
  134. err = errors.New("未查询到对应身份记录")
  135. return
  136. }
  137. //1、计算数据(1:按年购买 2:按月购买 3:按天购买 4:补餐)
  138. var totalPrice float64
  139. var data []*model.NursingHomeUserWithDay
  140. if req.Kind == 1 {
  141. totalPrice, data, err = CalcByYearForNursingHome(user.Id, req)
  142. if err != nil {
  143. return
  144. }
  145. }
  146. if req.Kind == 2 {
  147. totalPrice, data, err = CalcByMonthForNursingHome(user.Id, req)
  148. if err != nil {
  149. return
  150. }
  151. }
  152. if req.Kind == 3 {
  153. totalPrice, data, err = CalcByDayForNursingHome(user.Id, req)
  154. if err != nil {
  155. return
  156. }
  157. }
  158. if req.Kind == 4 {
  159. totalPrice, data, err = CalcByDayForNursingHome(user.Id, req)
  160. if err != nil {
  161. return
  162. }
  163. }
  164. total = utils.Float64ToStr(totalPrice)
  165. //3、生成订单号
  166. outTradeNo = utils.OrderUUID(user.Id)
  167. //4、请求 alipay.trade.create(统一收单交易创建接口)
  168. sysCfgDb := db.SysCfgDb{}
  169. sysCfgDb.Set()
  170. sysCfg := sysCfgDb.SysCfgFindWithDb(enum2.OpenAppletAppid, enum2.OpenAlipayAppid, enum2.OpenAlipayAppPrivateKey, enum2.OpenAlipayPublicKey, enum2.JsapiPayAppAutToken)
  171. err, resp := CurlAlipayTradeCreate(md.CurlAlipayTradeCreateReq{
  172. Config: struct {
  173. PayAliAppId string `json:"pay_ali_app_id" label:"支付宝开放平台-第三方应用-appid"`
  174. PayAliPrivateKey string `json:"pay_ali_private_key" label:"支付宝开放平台-第三方应用-接口加签-应用私钥"`
  175. PayAliPublicKey string `json:"pay_ali_public_key" label:"支付宝开放平台-第三方应用-接口加签-支付宝公钥"`
  176. }{
  177. PayAliAppId: sysCfg[enum2.OpenAlipayAppid],
  178. PayAliPrivateKey: sysCfg[enum2.OpenAlipayAppPrivateKey],
  179. PayAliPublicKey: sysCfg[enum2.OpenAlipayPublicKey],
  180. },
  181. OpAppId: sysCfg[enum2.OpenAppletAppid],
  182. BuyerId: user.UserId,
  183. TotalAmount: total,
  184. OutTradeNo: outTradeNo,
  185. Subject: "购买食堂套餐",
  186. AppAuthToken: sysCfg[enum2.JsapiPayAppAutToken],
  187. })
  188. if err != nil {
  189. return
  190. }
  191. //5、插入订单记录
  192. now := time.Now()
  193. NursingHomePackageOrd := db.NursingHomePackageOrd{}
  194. NursingHomePackageOrd.Set(outTradeNo)
  195. _, err = NursingHomePackageOrd.NursingHomePackageOrdInsertBySession(session, &model.NursingHomePackageOrd{
  196. EnterpriseId: req.EnterpriseId,
  197. Uid: user.Id,
  198. UserIdentityId: req.UserIdentityId,
  199. UserIdentityName: userIdentity.Name,
  200. TotalPrice: total,
  201. Kind: req.Kind,
  202. OutTradeNo: outTradeNo,
  203. TradeNo: resp.TradeNo,
  204. State: enum2.NursingHomePackageOrdStateForWait,
  205. OrdState: enum2.NursingHomePackageOrdOrdStateForWait,
  206. ReqContent: string(utils.Serialize(req)),
  207. WithDayData: string(utils.Serialize(data)),
  208. CreateAt: now.Format("2006-01-02 15:04:05"),
  209. UpdateAt: now.Format("2006-01-02 15:04:05"),
  210. })
  211. if err != nil {
  212. _ = session.Rollback()
  213. return
  214. }
  215. session.Commit()
  216. tradeNo = resp.TradeNo
  217. return
  218. }