智慧食堂
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.

hdl_pay.go 9.1 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. "fmt"
  13. "github.com/gin-gonic/gin"
  14. "time"
  15. )
  16. func BuyPackage(c *gin.Context) {
  17. var req md.BuyPackageReq
  18. err := c.ShouldBindJSON(&req)
  19. if err != nil {
  20. err = validate.HandleValidateErr(err)
  21. err1 := err.(e.E)
  22. e.OutErr(c, err1.Code, err1.Error())
  23. return
  24. }
  25. fmt.Println("req>>>>>>>>>>>>", utils.Serialize(req))
  26. outTradeNo, tradeNo, total, err := svc.BuyPackage(c, req)
  27. if err != nil {
  28. e.OutErr(c, e.ERR, err.Error())
  29. return
  30. }
  31. e.OutSuc(c, map[string]interface{}{
  32. "out_trade_no": outTradeNo,
  33. "trade_no": tradeNo,
  34. "total": total,
  35. }, nil)
  36. return
  37. }
  38. func OrdState(c *gin.Context) {
  39. outTradeNo := c.DefaultQuery("out_trade_no", "")
  40. centralKitchenForSchoolPackageOrd := db.CentralKitchenForSchoolPackageOrd{}
  41. centralKitchenForSchoolPackageOrd.Set(outTradeNo)
  42. ord, err := centralKitchenForSchoolPackageOrd.GetCentralKitchenForSchoolPackageOrd()
  43. if err != nil {
  44. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  45. return
  46. }
  47. if ord == nil {
  48. e.OutErr(c, e.ERR_NO_DATA, "未查询到对应订单记录")
  49. return
  50. }
  51. //if ord.State == enum.CentralKitchenForSchoolPackageOrdStateForWait {
  52. // //处于待支付状态,请求支付宝同步订单状态
  53. // sysCfgDb := db.SysCfgDb{}
  54. // sysCfgDb.Set()
  55. // sysCfg := sysCfgDb.SysCfgFindWithDb(enum.JsapiPayAppAutToken, enum.OpenAlipayAppid, enum.OpenAlipayAppPrivateKey, enum.OpenAlipayPublicKey)
  56. // err2, result := svc.CurlAlipayTradeQuery(md.CurlAlipayTradeQueryReq{
  57. // Config: struct {
  58. // PayAliAppId string `json:"pay_ali_app_id" label:"支付宝开放平台-第三方应用-appid"`
  59. // PayAliPrivateKey string `json:"pay_ali_private_key" label:"支付宝开放平台-第三方应用-接口加签-应用私钥"`
  60. // PayAliPublicKey string `json:"pay_ali_public_key" label:"支付宝开放平台-第三方应用-接口加签-支付宝公钥"`
  61. // }{
  62. // PayAliAppId: sysCfg[enum.OpenAlipayAppid],
  63. // PayAliPrivateKey: sysCfg[enum.OpenAlipayAppPrivateKey],
  64. // PayAliPublicKey: sysCfg[enum.OpenAlipayPublicKey],
  65. // },
  66. // OutTradeNo: outTradeNo,
  67. // AppAuthToken: sysCfg[enum.JsapiPayAppAutToken],
  68. // })
  69. // if err2 != nil {
  70. // e.OutErr(c, e.ERR, err2.Error())
  71. // return
  72. // }
  73. // ord.State, err2 = svc.DealCentralKitchenForSchoolOrderCallBack(outTradeNo, result.TradeStatus)
  74. // if err != nil {
  75. // e.OutErr(c, e.ERR, err2.Error())
  76. // return
  77. // }
  78. //}
  79. e.OutSuc(c, map[string]interface{}{
  80. "sate": ord.State,
  81. "sate_zh": enum.CentralKitchenForSchoolPackageOrdState.String(enum.CentralKitchenForSchoolPackageOrdState(ord.State)),
  82. }, nil)
  83. return
  84. }
  85. func NursingHomeBuyPackage(c *gin.Context) {
  86. var req md.NursingHomeBuyPackageReq
  87. err := c.ShouldBindJSON(&req)
  88. if err != nil {
  89. err = validate.HandleValidateErr(err)
  90. err1 := err.(e.E)
  91. e.OutErr(c, err1.Code, err1.Error())
  92. return
  93. }
  94. outTradeNo, tradeNo, total, err := svc.NursingHomeBuyPackageReq(c, req)
  95. if err != nil {
  96. e.OutErr(c, e.ERR, err.Error())
  97. return
  98. }
  99. e.OutSuc(c, map[string]interface{}{
  100. "out_trade_no": outTradeNo,
  101. "trade_no": tradeNo,
  102. "total": total,
  103. }, nil)
  104. return
  105. }
  106. func NursingHomeOrdState(c *gin.Context) {
  107. outTradeNo := c.DefaultQuery("out_trade_no", "")
  108. centralKitchenForSchoolPackageOrd := db.NursingHomePackageOrd{}
  109. centralKitchenForSchoolPackageOrd.Set(outTradeNo)
  110. ord, err := centralKitchenForSchoolPackageOrd.GetNursingHomePackageOrd()
  111. if err != nil {
  112. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  113. return
  114. }
  115. if ord == nil {
  116. e.OutErr(c, e.ERR_NO_DATA, "未查询到对应订单记录")
  117. return
  118. }
  119. if ord.State == enum.NursingHomePackageOrdStateForWait {
  120. //处于待支付状态,请求支付宝同步订单状态
  121. sysCfgDb := db.SysCfgDb{}
  122. sysCfgDb.Set()
  123. sysCfg := sysCfgDb.SysCfgFindWithDb(enum.JsapiPayAppAutToken, enum.OpenAlipayAppid, enum.OpenAlipayAppPrivateKey, enum.OpenAlipayPublicKey)
  124. err2, result := svc.CurlAlipayTradeQuery(md.CurlAlipayTradeQueryReq{
  125. Config: struct {
  126. PayAliAppId string `json:"pay_ali_app_id" label:"支付宝开放平台-第三方应用-appid"`
  127. PayAliPrivateKey string `json:"pay_ali_private_key" label:"支付宝开放平台-第三方应用-接口加签-应用私钥"`
  128. PayAliPublicKey string `json:"pay_ali_public_key" label:"支付宝开放平台-第三方应用-接口加签-支付宝公钥"`
  129. }{
  130. PayAliAppId: sysCfg[enum.OpenAlipayAppid],
  131. PayAliPrivateKey: sysCfg[enum.OpenAlipayAppPrivateKey],
  132. PayAliPublicKey: sysCfg[enum.OpenAlipayPublicKey],
  133. },
  134. OutTradeNo: outTradeNo,
  135. AppAuthToken: sysCfg[enum.JsapiPayAppAutToken],
  136. })
  137. if err2 != nil {
  138. e.OutErr(c, e.ERR, err2.Error())
  139. return
  140. }
  141. if result.TradeStatus == "TRADE_CLOSED" {
  142. ord.State = enum.NursingHomePackageOrdStateForFail
  143. }
  144. if result.TradeStatus == "TRADE_SUCCESS" {
  145. ord.State = enum.NursingHomePackageOrdStateForSuccess
  146. ord.OrdState = enum.NursingHomePackageOrdOrdStateForSuccess
  147. //TODO::将预留数据插入到 `central_kitchen_for_school_user_with_day`
  148. var data []*model.NursingHomeUserWithDay
  149. err4 := json.Unmarshal([]byte(ord.WithDayData), &data)
  150. if err4 != nil {
  151. e.OutErr(c, e.ERR, err4.Error())
  152. return
  153. }
  154. var hasMonths = map[string]map[string]interface{}{}
  155. var hasKinds = map[string]map[string]string{}
  156. var hasTotalPrice = map[string]map[string]float64{}
  157. for _, v := range data {
  158. date, _ := time.ParseInLocation("2006-01-02", v.Date, time.Local)
  159. if hasMonths[utils.TimeParseStd(v.Date).Month().String()] == nil {
  160. hasMonths[utils.TimeParseStd(v.Date).Month().String()] = make(map[string]interface{})
  161. hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_start"] = date.Format("2006-01-02")
  162. hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_end"] = date.Format("2006-01-02")
  163. } else {
  164. startTime, _ := time.ParseInLocation("2006-01-02", utils.AnyToString(hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_start"]), time.Local)
  165. if startTime.After(date) {
  166. hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_start"] = date.Format("2006-01-02")
  167. }
  168. endTime, _ := time.ParseInLocation("2006-01-02", utils.AnyToString(hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_end"]), time.Local)
  169. if endTime.Before(date) {
  170. hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_end"] = date.Format("2006-01-02")
  171. }
  172. }
  173. if hasTotalPrice[utils.TimeParseStd(v.Date).Month().String()] == nil {
  174. hasTotalPrice[utils.TimeParseStd(v.Date).Month().String()] = make(map[string]float64)
  175. }
  176. if hasKinds[utils.TimeParseStd(v.Date).Month().String()] == nil {
  177. hasKinds[utils.TimeParseStd(v.Date).Month().String()] = make(map[string]string)
  178. }
  179. hasMonths[utils.TimeParseStd(v.Date).Month().String()]["month"] = int(utils.TimeParseStd(v.Date).Month())
  180. hasKinds[utils.TimeParseStd(v.Date).Month().String()][utils.IntToStr(v.Kind)] = utils.IntToStr(v.Kind)
  181. hasTotalPrice[utils.TimeParseStd(v.Date).Month().String()][utils.IntToStr(v.Kind)] += utils.StrToFloat64(v.Amount)
  182. v.OrdNo = outTradeNo
  183. }
  184. centralKitchenForSchoolUserWithDayDb := db.NursingHomeUserWithDayDb{}
  185. centralKitchenForSchoolUserWithDayDb.Set(0)
  186. _, err5 := centralKitchenForSchoolUserWithDayDb.BatchAddNursingHomeUserWithDays(data)
  187. if err != nil {
  188. e.OutErr(c, e.ERR, err5.Error())
  189. return
  190. }
  191. //TODO::插入数据至 `nursing_home_package_ord_for_reserve`
  192. centralKitchenForSchoolPackageOrdForReserve := db.NursingHomePackageOrdForReserve{}
  193. centralKitchenForSchoolPackageOrdForReserve.Set(outTradeNo)
  194. var isHasKind []string
  195. for k, v := range hasMonths {
  196. for _, v1 := range hasKinds[k] {
  197. if !utils.InArr(v1, isHasKind) {
  198. _, err3 := centralKitchenForSchoolPackageOrdForReserve.NursingHomePackageOrdForReserveInsert(&model.NursingHomePackageOrdForReserve{
  199. EnterpriseId: ord.EnterpriseId,
  200. Uid: ord.Uid,
  201. UserIdentityName: ord.UserIdentityName,
  202. UserIdentityId: ord.UserIdentityId,
  203. Kind: utils.StrToInt(v1),
  204. OutTradeNo: ord.OutTradeNo,
  205. ReserveMonth: utils.AnyToString(v["month"]),
  206. MealTimeStart: utils.AnyToString(v["meal_time_start"]),
  207. MealTimeEnd: utils.AnyToString(v["meal_time_end"]),
  208. TotalPrice: utils.Float64ToStr(hasTotalPrice[k][v1]),
  209. CreateAt: time.Now().Format("2006-01-02 15:04:05"),
  210. UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
  211. })
  212. if err3 != nil {
  213. e.OutErr(c, e.ERR, err3.Error())
  214. return
  215. }
  216. isHasKind = append(isHasKind, v1)
  217. }
  218. }
  219. }
  220. }
  221. _, err3 := centralKitchenForSchoolPackageOrd.NursingHomePackageOrdUpdate(ord, "state", "ord_state")
  222. if err3 != nil {
  223. e.OutErr(c, e.ERR_DB_ORM, err3.Error())
  224. return
  225. }
  226. }
  227. e.OutSuc(c, map[string]interface{}{
  228. "sate": ord.State,
  229. "sate_zh": enum.NursingHomePackageOrdState.String(enum.NursingHomePackageOrdState(ord.State)),
  230. }, nil)
  231. return
  232. }