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

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