智慧食堂
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

359 рядки
12 KiB

  1. package hdl
  2. import (
  3. "applet/app/cfg"
  4. enum2 "applet/app/customer/enum"
  5. "applet/app/customer/lib/validate"
  6. "applet/app/customer/md"
  7. "applet/app/customer/svc"
  8. "applet/app/db"
  9. "applet/app/db/model"
  10. "applet/app/e"
  11. "applet/app/enum"
  12. svc3 "applet/app/svc"
  13. "applet/app/utils"
  14. "encoding/json"
  15. "fmt"
  16. "github.com/gin-gonic/gin"
  17. "github.com/wechatpay-apiv3/wechatpay-go/core"
  18. "github.com/wechatpay-apiv3/wechatpay-go/services/partnerpayments/jsapi"
  19. jsapi2 "github.com/wechatpay-apiv3/wechatpay-go/services/payments/jsapi"
  20. utils2 "github.com/wechatpay-apiv3/wechatpay-go/utils"
  21. "strconv"
  22. "time"
  23. )
  24. func BuyPackage(c *gin.Context) {
  25. var req md.BuyPackageReq
  26. err := c.ShouldBindJSON(&req)
  27. if err != nil {
  28. err = validate.HandleValidateErr(err)
  29. err1 := err.(e.E)
  30. e.OutErr(c, err1.Code, err1.Error())
  31. return
  32. }
  33. if cfg.Prd && req.EnterpriseId != 7 {
  34. //TODO::暂只开放金茂小学支付宝购买权限
  35. e.OutErr(c, e.ERR, "暂不支持支付宝支付!")
  36. return
  37. }
  38. err = svc.CheckIsOpenLimitOrderFood(req.EnterpriseId)
  39. if err != nil {
  40. e.OutErr(c, e.ERR, err.Error())
  41. return
  42. }
  43. fmt.Println("req>>>>>>>>>>>>", utils.Serialize(req))
  44. outTradeNo, tradeNo, total, err := svc.BuyPackageForAli(c, req)
  45. if err != nil {
  46. e.OutErr(c, e.ERR, err.Error())
  47. return
  48. }
  49. e.OutSuc(c, map[string]interface{}{
  50. "out_trade_no": outTradeNo,
  51. "trade_no": tradeNo,
  52. "total": total,
  53. }, nil)
  54. return
  55. }
  56. func BuyPackageForWx(c *gin.Context) {
  57. var req md.BuyPackageReq
  58. err := c.ShouldBindJSON(&req)
  59. if err != nil {
  60. err = validate.HandleValidateErr(err)
  61. err1 := err.(e.E)
  62. e.OutErr(c, err1.Code, err1.Error())
  63. return
  64. }
  65. err = svc.CheckIsOpenLimitOrderFood(req.EnterpriseId)
  66. if err != nil {
  67. e.OutErr(c, e.ERR, err.Error())
  68. return
  69. }
  70. fmt.Println("req>>>>>>>>>>>>", utils.SerializeStr(req))
  71. outTradeNo, total, resp, err := svc.BuyPackageForWx(c, req)
  72. if err != nil {
  73. e.OutErr(c, e.ERR, err.Error())
  74. return
  75. }
  76. e.OutSuc(c, map[string]interface{}{
  77. "out_trade_no": outTradeNo,
  78. "prams": resp,
  79. "total": total,
  80. }, nil)
  81. return
  82. }
  83. func RePayBuyPackageForWx(c *gin.Context) {
  84. prepayId := c.DefaultQuery("prepay_id", "")
  85. centralKitchenForSchoolPackageOrd := db.CentralKitchenForSchoolPackageOrd{}
  86. centralKitchenForSchoolPackageOrd.Set("")
  87. ord, err := centralKitchenForSchoolPackageOrd.GetCentralKitchenForSchoolPackageOrdByPrepayId(prepayId)
  88. if err != nil {
  89. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  90. return
  91. }
  92. if ord == nil {
  93. e.OutErr(c, e.ERR_NO_DATA, "未查询到对应订单记录")
  94. return
  95. }
  96. err = svc.CheckIsOpenLimitOrderFood(ord.EnterpriseId)
  97. if err != nil {
  98. e.OutErr(c, e.ERR, err.Error())
  99. return
  100. }
  101. sysCfgDb := db.SysCfgDb{}
  102. sysCfgDb.Set()
  103. client, err := svc3.NewWxPayClient(c)
  104. if err != nil {
  105. e.OutErr(c, e.ERR, err.Error())
  106. return
  107. }
  108. wxSvc := jsapi.JsapiApiService{Client: client}
  109. resp := new(jsapi2.PrepayWithRequestPaymentResponse)
  110. sysCfg := sysCfgDb.SysCfgFindWithDb(enum.WxAppletAppId)
  111. resp.PrepayId = &prepayId
  112. resp.SignType = core.String("RSA")
  113. appid := sysCfg[enum.WxAppletAppId]
  114. resp.Appid = &appid
  115. resp.TimeStamp = core.String(strconv.FormatInt(time.Now().Unix(), 10))
  116. nonce, err := utils2.GenerateNonce()
  117. if err != nil {
  118. e.OutErr(c, e.ERR, err.Error())
  119. return
  120. }
  121. resp.NonceStr = core.String(nonce)
  122. resp.Package = core.String("prepay_id=" + prepayId)
  123. message := fmt.Sprintf("%s\n%s\n%s\n%s\n", *resp.Appid, *resp.TimeStamp, *resp.NonceStr, *resp.Package)
  124. signatureResult, err := wxSvc.Client.Sign(c, message)
  125. if err != nil {
  126. e.OutErr(c, e.ERR, err.Error())
  127. return
  128. }
  129. resp.PaySign = core.String(signatureResult.Signature)
  130. e.OutSuc(c, map[string]interface{}{
  131. //"out_trade_no": ord.OutTradeNo,
  132. "prams": resp,
  133. //"total": ord.TradeNo,
  134. }, nil)
  135. return
  136. }
  137. func OrdState(c *gin.Context) {
  138. outTradeNo := c.DefaultQuery("out_trade_no", "")
  139. centralKitchenForSchoolPackageOrd := db.CentralKitchenForSchoolPackageOrd{}
  140. centralKitchenForSchoolPackageOrd.Set(outTradeNo)
  141. ord, err := centralKitchenForSchoolPackageOrd.GetCentralKitchenForSchoolPackageOrd()
  142. if err != nil {
  143. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  144. return
  145. }
  146. if ord == nil {
  147. e.OutErr(c, e.ERR_NO_DATA, "未查询到对应订单记录")
  148. return
  149. }
  150. if !cfg.Prd && ord.PayWay == enum2.PayWayForAli {
  151. if ord.State == enum.CentralKitchenForSchoolPackageOrdStateForWait {
  152. //处于待支付状态,请求支付宝同步订单状态
  153. sysCfgDb := db.SysCfgDb{}
  154. sysCfgDb.Set()
  155. sysCfg := sysCfgDb.SysCfgFindWithDb(enum.JsapiPayAppAutToken, enum.OpenAlipayAppid, enum.OpenAlipayAppPrivateKey, enum.OpenAlipayPublicKey)
  156. err2, result := svc.CurlAlipayTradeQuery(md.CurlAlipayTradeQueryReq{
  157. Config: struct {
  158. PayAliAppId string `json:"pay_ali_app_id" label:"支付宝开放平台-第三方应用-appid"`
  159. PayAliPrivateKey string `json:"pay_ali_private_key" label:"支付宝开放平台-第三方应用-接口加签-应用私钥"`
  160. PayAliPublicKey string `json:"pay_ali_public_key" label:"支付宝开放平台-第三方应用-接口加签-支付宝公钥"`
  161. }{
  162. PayAliAppId: sysCfg[enum.OpenAlipayAppid],
  163. PayAliPrivateKey: sysCfg[enum.OpenAlipayAppPrivateKey],
  164. PayAliPublicKey: sysCfg[enum.OpenAlipayPublicKey],
  165. },
  166. OutTradeNo: outTradeNo,
  167. AppAuthToken: sysCfg[enum.JsapiPayAppAutToken],
  168. })
  169. if err2 != nil {
  170. e.OutErr(c, e.ERR, err2.Error())
  171. return
  172. }
  173. ord.State, err2 = svc.DealCentralKitchenForSchoolOrderCallBack(outTradeNo, result.TradeStatus, result.Date)
  174. if err != nil {
  175. e.OutErr(c, e.ERR, err2.Error())
  176. return
  177. }
  178. }
  179. }
  180. e.OutSuc(c, map[string]interface{}{
  181. "sate": ord.State,
  182. "sate_zh": enum.CentralKitchenForSchoolPackageOrdState.String(enum.CentralKitchenForSchoolPackageOrdState(ord.State)),
  183. }, nil)
  184. return
  185. }
  186. func NursingHomeBuyPackage(c *gin.Context) {
  187. var req md.NursingHomeBuyPackageReq
  188. err := c.ShouldBindJSON(&req)
  189. if err != nil {
  190. err = validate.HandleValidateErr(err)
  191. err1 := err.(e.E)
  192. e.OutErr(c, err1.Code, err1.Error())
  193. return
  194. }
  195. outTradeNo, tradeNo, total, err := svc.NursingHomeBuyPackageReq(c, req)
  196. if err != nil {
  197. e.OutErr(c, e.ERR, err.Error())
  198. return
  199. }
  200. e.OutSuc(c, map[string]interface{}{
  201. "out_trade_no": outTradeNo,
  202. "trade_no": tradeNo,
  203. "total": total,
  204. }, nil)
  205. return
  206. }
  207. func NursingHomeOrdState(c *gin.Context) {
  208. outTradeNo := c.DefaultQuery("out_trade_no", "")
  209. centralKitchenForSchoolPackageOrd := db.NursingHomePackageOrd{}
  210. centralKitchenForSchoolPackageOrd.Set(outTradeNo)
  211. ord, err := centralKitchenForSchoolPackageOrd.GetNursingHomePackageOrd()
  212. if err != nil {
  213. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  214. return
  215. }
  216. if ord == nil {
  217. e.OutErr(c, e.ERR_NO_DATA, "未查询到对应订单记录")
  218. return
  219. }
  220. if ord.State == enum.NursingHomePackageOrdStateForWait {
  221. //处于待支付状态,请求支付宝同步订单状态
  222. sysCfgDb := db.SysCfgDb{}
  223. sysCfgDb.Set()
  224. sysCfg := sysCfgDb.SysCfgFindWithDb(enum.JsapiPayAppAutToken, enum.OpenAlipayAppid, enum.OpenAlipayAppPrivateKey, enum.OpenAlipayPublicKey)
  225. err2, result := svc.CurlAlipayTradeQuery(md.CurlAlipayTradeQueryReq{
  226. Config: struct {
  227. PayAliAppId string `json:"pay_ali_app_id" label:"支付宝开放平台-第三方应用-appid"`
  228. PayAliPrivateKey string `json:"pay_ali_private_key" label:"支付宝开放平台-第三方应用-接口加签-应用私钥"`
  229. PayAliPublicKey string `json:"pay_ali_public_key" label:"支付宝开放平台-第三方应用-接口加签-支付宝公钥"`
  230. }{
  231. PayAliAppId: sysCfg[enum.OpenAlipayAppid],
  232. PayAliPrivateKey: sysCfg[enum.OpenAlipayAppPrivateKey],
  233. PayAliPublicKey: sysCfg[enum.OpenAlipayPublicKey],
  234. },
  235. OutTradeNo: outTradeNo,
  236. AppAuthToken: sysCfg[enum.JsapiPayAppAutToken],
  237. })
  238. if err2 != nil {
  239. e.OutErr(c, e.ERR, err2.Error())
  240. return
  241. }
  242. if result.TradeStatus == "TRADE_CLOSED" {
  243. ord.State = enum.NursingHomePackageOrdStateForFail
  244. }
  245. if result.TradeStatus == "TRADE_SUCCESS" {
  246. ord.State = enum.NursingHomePackageOrdStateForSuccess
  247. ord.OrdState = enum.NursingHomePackageOrdOrdStateForSuccess
  248. //TODO::将预留数据插入到 `central_kitchen_for_school_user_with_day`
  249. var data []*model.NursingHomeUserWithDay
  250. err4 := json.Unmarshal([]byte(ord.WithDayData), &data)
  251. if err4 != nil {
  252. e.OutErr(c, e.ERR, err4.Error())
  253. return
  254. }
  255. var hasMonths = map[string]map[string]interface{}{}
  256. var hasKinds = map[string]map[string]string{}
  257. var hasTotalPrice = map[string]map[string]float64{}
  258. for _, v := range data {
  259. date, _ := time.ParseInLocation("2006-01-02", v.Date, time.Local)
  260. if hasMonths[utils.TimeParseStd(v.Date).Month().String()] == nil {
  261. hasMonths[utils.TimeParseStd(v.Date).Month().String()] = make(map[string]interface{})
  262. hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_start"] = date.Format("2006-01-02")
  263. hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_end"] = date.Format("2006-01-02")
  264. } else {
  265. startTime, _ := time.ParseInLocation("2006-01-02", utils.AnyToString(hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_start"]), time.Local)
  266. if startTime.After(date) {
  267. hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_start"] = date.Format("2006-01-02")
  268. }
  269. endTime, _ := time.ParseInLocation("2006-01-02", utils.AnyToString(hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_end"]), time.Local)
  270. if endTime.Before(date) {
  271. hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_end"] = date.Format("2006-01-02")
  272. }
  273. }
  274. if hasTotalPrice[utils.TimeParseStd(v.Date).Month().String()] == nil {
  275. hasTotalPrice[utils.TimeParseStd(v.Date).Month().String()] = make(map[string]float64)
  276. }
  277. if hasKinds[utils.TimeParseStd(v.Date).Month().String()] == nil {
  278. hasKinds[utils.TimeParseStd(v.Date).Month().String()] = make(map[string]string)
  279. }
  280. hasMonths[utils.TimeParseStd(v.Date).Month().String()]["month"] = int(utils.TimeParseStd(v.Date).Month())
  281. hasKinds[utils.TimeParseStd(v.Date).Month().String()][utils.IntToStr(v.Kind)] = utils.IntToStr(v.Kind)
  282. hasTotalPrice[utils.TimeParseStd(v.Date).Month().String()][utils.IntToStr(v.Kind)] += utils.StrToFloat64(v.Amount)
  283. v.OrdNo = outTradeNo
  284. }
  285. centralKitchenForSchoolUserWithDayDb := db.NursingHomeUserWithDayDb{}
  286. centralKitchenForSchoolUserWithDayDb.Set(0)
  287. _, err5 := centralKitchenForSchoolUserWithDayDb.BatchAddNursingHomeUserWithDays(data)
  288. if err != nil {
  289. e.OutErr(c, e.ERR, err5.Error())
  290. return
  291. }
  292. //TODO::插入数据至 `nursing_home_package_ord_for_reserve`
  293. centralKitchenForSchoolPackageOrdForReserve := db.NursingHomePackageOrdForReserve{}
  294. centralKitchenForSchoolPackageOrdForReserve.Set(outTradeNo)
  295. var isHasKind []string
  296. for k, v := range hasMonths {
  297. for _, v1 := range hasKinds[k] {
  298. if !utils.InArr(v1, isHasKind) {
  299. _, err3 := centralKitchenForSchoolPackageOrdForReserve.NursingHomePackageOrdForReserveInsert(&model.NursingHomePackageOrdForReserve{
  300. EnterpriseId: ord.EnterpriseId,
  301. Uid: ord.Uid,
  302. UserIdentityName: ord.UserIdentityName,
  303. UserIdentityId: ord.UserIdentityId,
  304. Kind: utils.StrToInt(v1),
  305. OutTradeNo: ord.OutTradeNo,
  306. ReserveMonth: utils.AnyToString(v["month"]),
  307. MealTimeStart: utils.AnyToString(v["meal_time_start"]),
  308. MealTimeEnd: utils.AnyToString(v["meal_time_end"]),
  309. TotalPrice: utils.Float64ToStr(hasTotalPrice[k][v1]),
  310. CreateAt: time.Now().Format("2006-01-02 15:04:05"),
  311. UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
  312. })
  313. if err3 != nil {
  314. e.OutErr(c, e.ERR, err3.Error())
  315. return
  316. }
  317. isHasKind = append(isHasKind, v1)
  318. }
  319. }
  320. }
  321. }
  322. _, err3 := centralKitchenForSchoolPackageOrd.NursingHomePackageOrdUpdate(ord, "state", "ord_state")
  323. if err3 != nil {
  324. e.OutErr(c, e.ERR_DB_ORM, err3.Error())
  325. return
  326. }
  327. }
  328. e.OutSuc(c, map[string]interface{}{
  329. "sate": ord.State,
  330. "sate_zh": enum.NursingHomePackageOrdState.String(enum.NursingHomePackageOrdState(ord.State)),
  331. }, nil)
  332. return
  333. }