智慧食堂
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

108 рядки
3.0 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, err1 := sysCfgDb.SysCfgGetOne(enum.JsapiPayAppAutToken)
  52. if err1 != nil {
  53. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  54. return
  55. }
  56. err2, result := svc.CurlAlipayTradeQuery(md.CurlAlipayTradeQueryReq{
  57. OutTradeNo: outTradeNo,
  58. AppAuthToken: sysCfg.Val,
  59. })
  60. if err2 != nil {
  61. e.OutErr(c, e.ERR, err2.Error())
  62. return
  63. }
  64. if result.TradeStatus == "TRADE_CLOSED" {
  65. ord.State = enum.CentralKitchenForSchoolPackageOrdStateForFail
  66. }
  67. if result.TradeStatus == "TRADE_SUCCESS" {
  68. ord.State = enum.CentralKitchenForSchoolPackageOrdStateForSuccess
  69. ord.OrdState = enum.CentralKitchenForSchoolPackageOrdOrdStateForSuccess
  70. //TODO::将预留数据插入到 `central_kitchen_for_school_user_with_day`
  71. var data []*model.CentralKitchenForSchoolUserWithDay
  72. err4 := json.Unmarshal([]byte(ord.WithDayData), &data)
  73. if err4 != nil {
  74. e.OutErr(c, e.ERR, err4.Error())
  75. return
  76. }
  77. for _, v := range data {
  78. v.OrdNo = outTradeNo
  79. }
  80. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  81. centralKitchenForSchoolUserWithDayDb.Set(0)
  82. _, err5 := centralKitchenForSchoolUserWithDayDb.BatchAddCentralKitchenForSchoolUserWithDays(data)
  83. if err != nil {
  84. e.OutErr(c, e.ERR, err5.Error())
  85. return
  86. }
  87. }
  88. _, err3 := centralKitchenForSchoolPackageOrd.CentralKitchenForSchoolPackageOrdUpdate(ord, "state", "ord_state")
  89. if err3 != nil {
  90. e.OutErr(c, e.ERR_DB_ORM, err3.Error())
  91. return
  92. }
  93. }
  94. e.OutSuc(c, map[string]interface{}{
  95. "sate": ord.State,
  96. "sate_zh": enum.CentralKitchenForSchoolPackageOrdState.String(enum.CentralKitchenForSchoolPackageOrdState(ord.State)),
  97. }, nil)
  98. return
  99. }