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

275 wiersze
9.6 KiB

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