智慧食堂
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

hdl_pay.go 6.2 KiB

1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
1 anno fa
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. "applet/app/utils"
  11. "encoding/json"
  12. "github.com/gin-gonic/gin"
  13. "time"
  14. )
  15. func BuyPackage(c *gin.Context) {
  16. var req md.BuyPackageReq
  17. err := c.ShouldBindJSON(&req)
  18. if err != nil {
  19. err = validate.HandleValidateErr(err)
  20. err1 := err.(e.E)
  21. e.OutErr(c, err1.Code, err1.Error())
  22. return
  23. }
  24. outTradeNo, tradeNo, total, err := svc.BuyPackage(c, req)
  25. if err != nil {
  26. e.OutErr(c, e.ERR, err.Error())
  27. return
  28. }
  29. e.OutSuc(c, map[string]interface{}{
  30. "out_trade_no": outTradeNo,
  31. "trade_no": tradeNo,
  32. "total": total,
  33. }, nil)
  34. return
  35. }
  36. func OrdState(c *gin.Context) {
  37. outTradeNo := c.DefaultQuery("out_trade_no", "")
  38. centralKitchenForSchoolPackageOrd := db.CentralKitchenForSchoolPackageOrd{}
  39. centralKitchenForSchoolPackageOrd.Set(outTradeNo)
  40. ord, err := centralKitchenForSchoolPackageOrd.GetCentralKitchenForSchoolPackageOrd()
  41. if err != nil {
  42. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  43. return
  44. }
  45. if ord == nil {
  46. e.OutErr(c, e.ERR_NO_DATA, "未查询到对应订单记录")
  47. return
  48. }
  49. if ord.State == enum.CentralKitchenForSchoolPackageOrdStateForWait {
  50. //处于待支付状态,请求支付宝同步订单状态
  51. sysCfgDb := db.SysCfgDb{}
  52. sysCfgDb.Set()
  53. sysCfg := sysCfgDb.SysCfgFindWithDb(enum.JsapiPayAppAutToken, enum.OpenAlipayAppid, enum.OpenAlipayAppPrivateKey, enum.OpenAlipayPublicKey)
  54. err2, result := svc.CurlAlipayTradeQuery(md.CurlAlipayTradeQueryReq{
  55. Config: struct {
  56. PayAliAppId string `json:"pay_ali_app_id" label:"支付宝开放平台-第三方应用-appid"`
  57. PayAliPrivateKey string `json:"pay_ali_private_key" label:"支付宝开放平台-第三方应用-接口加签-应用私钥"`
  58. PayAliPublicKey string `json:"pay_ali_public_key" label:"支付宝开放平台-第三方应用-接口加签-支付宝公钥"`
  59. }{
  60. PayAliAppId: sysCfg[enum.OpenAlipayAppid],
  61. PayAliPrivateKey: sysCfg[enum.OpenAlipayAppPrivateKey],
  62. PayAliPublicKey: sysCfg[enum.OpenAlipayPublicKey],
  63. },
  64. OutTradeNo: outTradeNo,
  65. AppAuthToken: sysCfg[enum.JsapiPayAppAutToken],
  66. })
  67. if err2 != nil {
  68. e.OutErr(c, e.ERR, err2.Error())
  69. return
  70. }
  71. if result.TradeStatus == "TRADE_CLOSED" {
  72. ord.State = enum.CentralKitchenForSchoolPackageOrdStateForFail
  73. }
  74. if result.TradeStatus == "TRADE_SUCCESS" {
  75. ord.State = enum.CentralKitchenForSchoolPackageOrdStateForSuccess
  76. ord.OrdState = enum.CentralKitchenForSchoolPackageOrdOrdStateForSuccess
  77. //TODO::将预留数据插入到 `central_kitchen_for_school_user_with_day`
  78. var data []*model.CentralKitchenForSchoolUserWithDay
  79. err4 := json.Unmarshal([]byte(ord.WithDayData), &data)
  80. if err4 != nil {
  81. e.OutErr(c, e.ERR, err4.Error())
  82. return
  83. }
  84. var hasMonths = map[string]map[string]interface{}{}
  85. var hasKinds = map[string][]string{}
  86. for _, v := range data {
  87. date, _ := time.ParseInLocation("2006-01-02", v.Date, time.Local)
  88. if hasMonths[utils.TimeParseStd(v.Date).Month().String()] == nil {
  89. hasMonths[utils.TimeParseStd(v.Date).Month().String()] = make(map[string]interface{})
  90. hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_start"] = date.Format("2006-01-02")
  91. hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_end"] = date.Format("2006-01-02")
  92. } else {
  93. startTime, _ := time.ParseInLocation("2006-01-02", utils.AnyToString(hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_start"]), time.Local)
  94. if startTime.After(date) {
  95. hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_start"] = date.Format("2006-01-02")
  96. }
  97. endTime, _ := time.ParseInLocation("2006-01-02", utils.AnyToString(hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_end"]), time.Local)
  98. if endTime.Before(date) {
  99. hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_end"] = date.Format("2006-01-02")
  100. }
  101. }
  102. hasMonths[utils.TimeParseStd(v.Date).Month().String()]["month"] = int(utils.TimeParseStd(v.Date).Month())
  103. hasKinds[utils.TimeParseStd(v.Date).Month().String()] = append(hasKinds[utils.TimeParseStd(v.Date).Month().String()], utils.IntToStr(v.Kind))
  104. v.OrdNo = outTradeNo
  105. }
  106. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  107. centralKitchenForSchoolUserWithDayDb.Set(0)
  108. _, err5 := centralKitchenForSchoolUserWithDayDb.BatchAddCentralKitchenForSchoolUserWithDays(data)
  109. if err != nil {
  110. e.OutErr(c, e.ERR, err5.Error())
  111. return
  112. }
  113. //TODO::插入数据至 `central_kitchen_for_school_package_ord_for_reserve`
  114. centralKitchenForSchoolPackageOrdForReserve := db.CentralKitchenForSchoolPackageOrdForReserve{}
  115. centralKitchenForSchoolPackageOrdForReserve.Set(outTradeNo)
  116. var isHasKind []string
  117. for k, v := range hasMonths {
  118. for _, v1 := range hasKinds[k] {
  119. if !utils.InArr(v1, isHasKind) {
  120. _, err3 := centralKitchenForSchoolPackageOrdForReserve.CentralKitchenForSchoolPackageOrdForReserveInsert(&model.CentralKitchenForSchoolPackageOrdForReserve{
  121. EnterpriseId: ord.EnterpriseId,
  122. Uid: ord.Uid,
  123. UserIdentityName: ord.UserIdentityName,
  124. UserIdentityId: ord.UserIdentityId,
  125. Kind: utils.StrToInt(v1),
  126. OutTradeNo: ord.OutTradeNo,
  127. ReserveMonth: utils.AnyToString(v["month"]),
  128. MealTimeStart: utils.AnyToString(v["meal_time_start"]),
  129. MealTimeEnd: utils.AnyToString(v["meal_time_end"]),
  130. CreateAt: time.Now().Format("2006-01-02 15:04:05"),
  131. UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
  132. })
  133. if err3 != nil {
  134. e.OutErr(c, e.ERR, err3.Error())
  135. return
  136. }
  137. isHasKind = append(isHasKind, v1)
  138. }
  139. }
  140. }
  141. }
  142. _, err3 := centralKitchenForSchoolPackageOrd.CentralKitchenForSchoolPackageOrdUpdate(ord, "state", "ord_state")
  143. if err3 != nil {
  144. e.OutErr(c, e.ERR_DB_ORM, err3.Error())
  145. return
  146. }
  147. }
  148. e.OutSuc(c, map[string]interface{}{
  149. "sate": ord.State,
  150. "sate_zh": enum.CentralKitchenForSchoolPackageOrdState.String(enum.CentralKitchenForSchoolPackageOrdState(ord.State)),
  151. }, nil)
  152. return
  153. }