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

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