智慧食堂
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

112 lines
3.5 KiB

  1. package hdl
  2. import (
  3. "applet/app/customer/lib/validate"
  4. "applet/app/customer/md"
  5. "applet/app/customer/svc"
  6. "applet/app/db"
  7. "applet/app/db/model"
  8. "applet/app/e"
  9. "applet/app/enum"
  10. "encoding/json"
  11. "github.com/gin-gonic/gin"
  12. )
  13. func BuyPackage(c *gin.Context) {
  14. var req md.BuyPackageReq
  15. err := c.ShouldBindJSON(&req)
  16. if err != nil {
  17. err = validate.HandleValidateErr(err)
  18. err1 := err.(e.E)
  19. e.OutErr(c, err1.Code, err1.Error())
  20. return
  21. }
  22. outTradeNo, tradeNo, total, err := svc.BuyPackage(c, req)
  23. if err != nil {
  24. e.OutErr(c, e.ERR, err.Error())
  25. return
  26. }
  27. e.OutSuc(c, map[string]interface{}{
  28. "out_trade_no": outTradeNo,
  29. "trade_no": tradeNo,
  30. "total": total,
  31. }, nil)
  32. return
  33. }
  34. func OrdState(c *gin.Context) {
  35. outTradeNo := c.DefaultQuery("out_trade_no", "")
  36. centralKitchenForSchoolPackageOrd := db.CentralKitchenForSchoolPackageOrd{}
  37. centralKitchenForSchoolPackageOrd.Set(outTradeNo)
  38. ord, err := centralKitchenForSchoolPackageOrd.GetCentralKitchenForSchoolPackageOrd()
  39. if err != nil {
  40. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  41. return
  42. }
  43. if ord == nil {
  44. e.OutErr(c, e.ERR_NO_DATA, "未查询到对应订单记录")
  45. return
  46. }
  47. if ord.State == enum.CentralKitchenForSchoolPackageOrdStateForWait {
  48. //处于待支付状态,请求支付宝同步订单状态
  49. sysCfgDb := db.SysCfgDb{}
  50. sysCfgDb.Set()
  51. sysCfg := sysCfgDb.SysCfgFindWithDb(enum.JsapiPayAppAutToken, enum.OpenAlipayAppid, enum.OpenAlipayAppPrivateKey, enum.OpenAlipayPublicKey)
  52. err2, result := svc.CurlAlipayTradeQuery(md.CurlAlipayTradeQueryReq{
  53. Config: struct {
  54. PayAliAppId string `json:"pay_ali_app_id" label:"支付宝开放平台-第三方应用-appid"`
  55. PayAliPrivateKey string `json:"pay_ali_private_key" label:"支付宝开放平台-第三方应用-接口加签-应用私钥"`
  56. PayAliPublicKey string `json:"pay_ali_public_key" label:"支付宝开放平台-第三方应用-接口加签-支付宝公钥"`
  57. }{
  58. PayAliAppId: sysCfg[enum.OpenAlipayAppid],
  59. PayAliPrivateKey: sysCfg[enum.OpenAlipayAppPrivateKey],
  60. PayAliPublicKey: sysCfg[enum.OpenAlipayPublicKey],
  61. },
  62. OutTradeNo: outTradeNo,
  63. AppAuthToken: sysCfg[enum.JsapiPayAppAutToken],
  64. })
  65. if err2 != nil {
  66. e.OutErr(c, e.ERR, err2.Error())
  67. return
  68. }
  69. if result.TradeStatus == "TRADE_CLOSED" {
  70. ord.State = enum.CentralKitchenForSchoolPackageOrdStateForFail
  71. }
  72. if result.TradeStatus == "TRADE_SUCCESS" {
  73. ord.State = enum.CentralKitchenForSchoolPackageOrdStateForSuccess
  74. ord.OrdState = enum.CentralKitchenForSchoolPackageOrdOrdStateForSuccess
  75. //TODO::将预留数据插入到 `central_kitchen_for_school_user_with_day`
  76. var data []*model.CentralKitchenForSchoolUserWithDay
  77. err4 := json.Unmarshal([]byte(ord.WithDayData), &data)
  78. if err4 != nil {
  79. e.OutErr(c, e.ERR, err4.Error())
  80. return
  81. }
  82. for _, v := range data {
  83. v.OrdNo = outTradeNo
  84. }
  85. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  86. centralKitchenForSchoolUserWithDayDb.Set(0)
  87. _, err5 := centralKitchenForSchoolUserWithDayDb.BatchAddCentralKitchenForSchoolUserWithDays(data)
  88. if err != nil {
  89. e.OutErr(c, e.ERR, err5.Error())
  90. return
  91. }
  92. }
  93. _, err3 := centralKitchenForSchoolPackageOrd.CentralKitchenForSchoolPackageOrdUpdate(ord, "state", "ord_state")
  94. if err3 != nil {
  95. e.OutErr(c, e.ERR_DB_ORM, err3.Error())
  96. return
  97. }
  98. }
  99. e.OutSuc(c, map[string]interface{}{
  100. "sate": ord.State,
  101. "sate_zh": enum.CentralKitchenForSchoolPackageOrdState.String(enum.CentralKitchenForSchoolPackageOrdState(ord.State)),
  102. }, nil)
  103. return
  104. }