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

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