智慧食堂
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

328 Zeilen
13 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. "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]map[string]string{}
  86. var hasTotalPrice = map[string]map[string]float64{}
  87. for _, v := range data {
  88. date, _ := time.ParseInLocation("2006-01-02", v.Date, time.Local)
  89. if hasMonths[utils.TimeParseStd(v.Date).Month().String()] == nil {
  90. hasMonths[utils.TimeParseStd(v.Date).Month().String()] = make(map[string]interface{})
  91. hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_start"] = date.Format("2006-01-02")
  92. hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_end"] = date.Format("2006-01-02")
  93. } else {
  94. startTime, _ := time.ParseInLocation("2006-01-02", utils.AnyToString(hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_start"]), time.Local)
  95. if startTime.After(date) {
  96. hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_start"] = date.Format("2006-01-02")
  97. }
  98. endTime, _ := time.ParseInLocation("2006-01-02", utils.AnyToString(hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_end"]), time.Local)
  99. if endTime.Before(date) {
  100. hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_end"] = date.Format("2006-01-02")
  101. }
  102. }
  103. if hasTotalPrice[utils.TimeParseStd(v.Date).Month().String()] == nil {
  104. hasTotalPrice[utils.TimeParseStd(v.Date).Month().String()] = make(map[string]float64)
  105. }
  106. if hasKinds[utils.TimeParseStd(v.Date).Month().String()] == nil {
  107. hasKinds[utils.TimeParseStd(v.Date).Month().String()] = make(map[string]string)
  108. }
  109. hasMonths[utils.TimeParseStd(v.Date).Month().String()]["month"] = int(utils.TimeParseStd(v.Date).Month())
  110. hasKinds[utils.TimeParseStd(v.Date).Month().String()][utils.IntToStr(v.Kind)] = utils.IntToStr(v.Kind)
  111. hasTotalPrice[utils.TimeParseStd(v.Date).Month().String()][utils.IntToStr(v.Kind)] += utils.StrToFloat64(v.Amount)
  112. v.OrdNo = outTradeNo
  113. }
  114. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  115. centralKitchenForSchoolUserWithDayDb.Set(0)
  116. _, err5 := centralKitchenForSchoolUserWithDayDb.BatchAddCentralKitchenForSchoolUserWithDays(data)
  117. if err != nil {
  118. e.OutErr(c, e.ERR, err5.Error())
  119. return
  120. }
  121. //TODO::插入数据至 `central_kitchen_for_school_package_ord_for_reserve`
  122. centralKitchenForSchoolPackageOrdForReserve := db.CentralKitchenForSchoolPackageOrdForReserve{}
  123. centralKitchenForSchoolPackageOrdForReserve.Set(outTradeNo)
  124. for k, v := range hasMonths {
  125. for _, v1 := range hasKinds[k] {
  126. _, err3 := centralKitchenForSchoolPackageOrdForReserve.CentralKitchenForSchoolPackageOrdForReserveInsert(&model.CentralKitchenForSchoolPackageOrdForReserve{
  127. EnterpriseId: ord.EnterpriseId,
  128. Uid: ord.Uid,
  129. UserIdentityName: ord.UserIdentityName,
  130. UserIdentityId: ord.UserIdentityId,
  131. Kind: utils.StrToInt(v1),
  132. OutTradeNo: ord.OutTradeNo,
  133. ReserveMonth: utils.AnyToString(v["month"]),
  134. MealTimeStart: utils.AnyToString(v["meal_time_start"]),
  135. MealTimeEnd: utils.AnyToString(v["meal_time_end"]),
  136. TotalPrice: utils.Float64ToStr(hasTotalPrice[k][v1]),
  137. CreateAt: time.Now().Format("2006-01-02 15:04:05"),
  138. UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
  139. })
  140. if err3 != nil {
  141. e.OutErr(c, e.ERR, err3.Error())
  142. return
  143. }
  144. }
  145. }
  146. }
  147. _, err3 := centralKitchenForSchoolPackageOrd.CentralKitchenForSchoolPackageOrdUpdate(ord, "state", "ord_state")
  148. if err3 != nil {
  149. e.OutErr(c, e.ERR_DB_ORM, err3.Error())
  150. return
  151. }
  152. }
  153. e.OutSuc(c, map[string]interface{}{
  154. "sate": ord.State,
  155. "sate_zh": enum.CentralKitchenForSchoolPackageOrdState.String(enum.CentralKitchenForSchoolPackageOrdState(ord.State)),
  156. }, nil)
  157. return
  158. }
  159. func NursingHomeBuyPackage(c *gin.Context) {
  160. var req md.NursingHomeBuyPackageReq
  161. err := c.ShouldBindJSON(&req)
  162. if err != nil {
  163. err = validate.HandleValidateErr(err)
  164. err1 := err.(e.E)
  165. e.OutErr(c, err1.Code, err1.Error())
  166. return
  167. }
  168. outTradeNo, tradeNo, total, err := svc.NursingHomeBuyPackageReq(c, req)
  169. if err != nil {
  170. e.OutErr(c, e.ERR, err.Error())
  171. return
  172. }
  173. e.OutSuc(c, map[string]interface{}{
  174. "out_trade_no": outTradeNo,
  175. "trade_no": tradeNo,
  176. "total": total,
  177. }, nil)
  178. return
  179. }
  180. func NursingHomeOrdState(c *gin.Context) {
  181. outTradeNo := c.DefaultQuery("out_trade_no", "")
  182. centralKitchenForSchoolPackageOrd := db.NursingHomePackageOrd{}
  183. centralKitchenForSchoolPackageOrd.Set(outTradeNo)
  184. ord, err := centralKitchenForSchoolPackageOrd.GetNursingHomePackageOrd()
  185. if err != nil {
  186. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  187. return
  188. }
  189. if ord == nil {
  190. e.OutErr(c, e.ERR_NO_DATA, "未查询到对应订单记录")
  191. return
  192. }
  193. if ord.State == enum.NursingHomePackageOrdStateForWait {
  194. //处于待支付状态,请求支付宝同步订单状态
  195. sysCfgDb := db.SysCfgDb{}
  196. sysCfgDb.Set()
  197. sysCfg := sysCfgDb.SysCfgFindWithDb(enum.JsapiPayAppAutToken, enum.OpenAlipayAppid, enum.OpenAlipayAppPrivateKey, enum.OpenAlipayPublicKey)
  198. err2, result := svc.CurlAlipayTradeQuery(md.CurlAlipayTradeQueryReq{
  199. Config: struct {
  200. PayAliAppId string `json:"pay_ali_app_id" label:"支付宝开放平台-第三方应用-appid"`
  201. PayAliPrivateKey string `json:"pay_ali_private_key" label:"支付宝开放平台-第三方应用-接口加签-应用私钥"`
  202. PayAliPublicKey string `json:"pay_ali_public_key" label:"支付宝开放平台-第三方应用-接口加签-支付宝公钥"`
  203. }{
  204. PayAliAppId: sysCfg[enum.OpenAlipayAppid],
  205. PayAliPrivateKey: sysCfg[enum.OpenAlipayAppPrivateKey],
  206. PayAliPublicKey: sysCfg[enum.OpenAlipayPublicKey],
  207. },
  208. OutTradeNo: outTradeNo,
  209. AppAuthToken: sysCfg[enum.JsapiPayAppAutToken],
  210. })
  211. if err2 != nil {
  212. e.OutErr(c, e.ERR, err2.Error())
  213. return
  214. }
  215. if result.TradeStatus == "TRADE_CLOSED" {
  216. ord.State = enum.NursingHomePackageOrdStateForFail
  217. }
  218. if result.TradeStatus == "TRADE_SUCCESS" {
  219. ord.State = enum.NursingHomePackageOrdStateForSuccess
  220. ord.OrdState = enum.NursingHomePackageOrdOrdStateForSuccess
  221. //TODO::将预留数据插入到 `central_kitchen_for_school_user_with_day`
  222. var data []*model.NursingHomeUserWithDay
  223. err4 := json.Unmarshal([]byte(ord.WithDayData), &data)
  224. if err4 != nil {
  225. e.OutErr(c, e.ERR, err4.Error())
  226. return
  227. }
  228. var hasMonths = map[string]map[string]interface{}{}
  229. var hasKinds = map[string]map[string]string{}
  230. var hasTotalPrice = map[string]map[string]float64{}
  231. for _, v := range data {
  232. date, _ := time.ParseInLocation("2006-01-02", v.Date, time.Local)
  233. if hasMonths[utils.TimeParseStd(v.Date).Month().String()] == nil {
  234. hasMonths[utils.TimeParseStd(v.Date).Month().String()] = make(map[string]interface{})
  235. hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_start"] = date.Format("2006-01-02")
  236. hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_end"] = date.Format("2006-01-02")
  237. } else {
  238. startTime, _ := time.ParseInLocation("2006-01-02", utils.AnyToString(hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_start"]), time.Local)
  239. if startTime.After(date) {
  240. hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_start"] = date.Format("2006-01-02")
  241. }
  242. endTime, _ := time.ParseInLocation("2006-01-02", utils.AnyToString(hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_end"]), time.Local)
  243. if endTime.Before(date) {
  244. hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_end"] = date.Format("2006-01-02")
  245. }
  246. }
  247. if hasTotalPrice[utils.TimeParseStd(v.Date).Month().String()] == nil {
  248. hasTotalPrice[utils.TimeParseStd(v.Date).Month().String()] = make(map[string]float64)
  249. }
  250. if hasKinds[utils.TimeParseStd(v.Date).Month().String()] == nil {
  251. hasKinds[utils.TimeParseStd(v.Date).Month().String()] = make(map[string]string)
  252. }
  253. hasMonths[utils.TimeParseStd(v.Date).Month().String()]["month"] = int(utils.TimeParseStd(v.Date).Month())
  254. hasKinds[utils.TimeParseStd(v.Date).Month().String()][utils.IntToStr(v.Kind)] = utils.IntToStr(v.Kind)
  255. hasTotalPrice[utils.TimeParseStd(v.Date).Month().String()][utils.IntToStr(v.Kind)] += utils.StrToFloat64(v.Amount)
  256. v.OrdNo = outTradeNo
  257. }
  258. centralKitchenForSchoolUserWithDayDb := db.NursingHomeUserWithDayDb{}
  259. centralKitchenForSchoolUserWithDayDb.Set(0)
  260. _, err5 := centralKitchenForSchoolUserWithDayDb.BatchAddNursingHomeUserWithDays(data)
  261. if err != nil {
  262. e.OutErr(c, e.ERR, err5.Error())
  263. return
  264. }
  265. //TODO::插入数据至 `nursing_home_package_ord_for_reserve`
  266. centralKitchenForSchoolPackageOrdForReserve := db.NursingHomePackageOrdForReserve{}
  267. centralKitchenForSchoolPackageOrdForReserve.Set(outTradeNo)
  268. var isHasKind []string
  269. for k, v := range hasMonths {
  270. for _, v1 := range hasKinds[k] {
  271. if !utils.InArr(v1, isHasKind) {
  272. _, err3 := centralKitchenForSchoolPackageOrdForReserve.NursingHomePackageOrdForReserveInsert(&model.NursingHomePackageOrdForReserve{
  273. EnterpriseId: ord.EnterpriseId,
  274. Uid: ord.Uid,
  275. UserIdentityName: ord.UserIdentityName,
  276. UserIdentityId: ord.UserIdentityId,
  277. Kind: utils.StrToInt(v1),
  278. OutTradeNo: ord.OutTradeNo,
  279. ReserveMonth: utils.AnyToString(v["month"]),
  280. MealTimeStart: utils.AnyToString(v["meal_time_start"]),
  281. MealTimeEnd: utils.AnyToString(v["meal_time_end"]),
  282. TotalPrice: utils.Float64ToStr(hasTotalPrice[k][v1]),
  283. CreateAt: time.Now().Format("2006-01-02 15:04:05"),
  284. UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
  285. })
  286. if err3 != nil {
  287. e.OutErr(c, e.ERR, err3.Error())
  288. return
  289. }
  290. isHasKind = append(isHasKind, v1)
  291. }
  292. }
  293. }
  294. }
  295. _, err3 := centralKitchenForSchoolPackageOrd.NursingHomePackageOrdUpdate(ord, "state", "ord_state")
  296. if err3 != nil {
  297. e.OutErr(c, e.ERR_DB_ORM, err3.Error())
  298. return
  299. }
  300. }
  301. e.OutSuc(c, map[string]interface{}{
  302. "sate": ord.State,
  303. "sate_zh": enum.NursingHomePackageOrdState.String(enum.NursingHomePackageOrdState(ord.State)),
  304. }, nil)
  305. return
  306. }