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

645 Zeilen
22 KiB

  1. package svc
  2. import (
  3. "applet/app/customer/enum"
  4. "applet/app/customer/md"
  5. "applet/app/db"
  6. "applet/app/db/model"
  7. enum2 "applet/app/enum"
  8. svc3 "applet/app/svc"
  9. "applet/app/utils"
  10. "applet/app/utils/logx"
  11. "encoding/json"
  12. "errors"
  13. "fmt"
  14. "github.com/gin-gonic/gin"
  15. "github.com/wechatpay-apiv3/wechatpay-go/core"
  16. "github.com/wechatpay-apiv3/wechatpay-go/services/partnerpayments/jsapi"
  17. jsapi2 "github.com/wechatpay-apiv3/wechatpay-go/services/payments/jsapi"
  18. utils2 "github.com/wechatpay-apiv3/wechatpay-go/utils"
  19. "io/ioutil"
  20. "math"
  21. "net/url"
  22. "strconv"
  23. "time"
  24. )
  25. func BuyPackageForAli(c *gin.Context, req md.BuyPackageReq) (outTradeNo, tradeNo, total string, err error) {
  26. user := GetUser(c)
  27. session := db.Db.NewSession()
  28. defer session.Close()
  29. session.Begin()
  30. if err != nil {
  31. _ = session.Rollback()
  32. return
  33. }
  34. //1、判断是否为教师
  35. isTeacher := false
  36. userIdentityDb := db.UserIdentityDb{}
  37. userIdentityDb.Set(0)
  38. userIdentity, err := userIdentityDb.GetUserIdentity(req.UserIdentityId)
  39. if err != nil {
  40. return
  41. }
  42. if userIdentity == nil {
  43. err = errors.New("未查询到对应身份记录")
  44. return
  45. }
  46. if userIdentity.Identity == enum2.UserIdentityForCentralKitchenForTeacher {
  47. isTeacher = true
  48. }
  49. //2、计算数据(1:按学期购买 2:按月购买 3:按天购买 4:补餐)
  50. var totalPrice float64
  51. var data []*model.CentralKitchenForSchoolUserWithDay
  52. if req.Kind == 1 {
  53. totalPrice, data, err = CalcBySchoolTerm(user.Id, isTeacher, req)
  54. if err != nil {
  55. return
  56. }
  57. }
  58. if req.Kind == 2 {
  59. totalPrice, data, err = CalcByMonth(user.Id, isTeacher, req)
  60. if err != nil {
  61. return
  62. }
  63. }
  64. if req.Kind == 3 {
  65. totalPrice, data, err = CalcByDay(user.Id, isTeacher, req)
  66. if err != nil {
  67. return
  68. }
  69. }
  70. if req.Kind == 4 {
  71. totalPrice, data, err = CalcSupplementaryByDay(user.Id, isTeacher, req)
  72. if err != nil {
  73. return
  74. }
  75. }
  76. total = utils.Float64ToStr(totalPrice)
  77. //3、生成订单号
  78. outTradeNo = utils.OrderUUID(user.Id)
  79. //4、请求 alipay.trade.create(统一收单交易创建接口)
  80. sysCfgDb := db.SysCfgDb{}
  81. sysCfgDb.Set()
  82. sysCfg := sysCfgDb.SysCfgFindWithDb(enum2.OpenAppletAppid, enum2.OpenAlipayAppid, enum2.OpenAlipayAppPrivateKey, enum2.OpenAlipayPublicKey, enum2.JsapiPayAppAutToken, enum2.JsapiPayNotifyUrl)
  83. err, resp := CurlAlipayTradeCreate(md.CurlAlipayTradeCreateReq{
  84. Config: struct {
  85. PayAliAppId string `json:"pay_ali_app_id" label:"支付宝开放平台-第三方应用-appid"`
  86. PayAliPrivateKey string `json:"pay_ali_private_key" label:"支付宝开放平台-第三方应用-接口加签-应用私钥"`
  87. PayAliPublicKey string `json:"pay_ali_public_key" label:"支付宝开放平台-第三方应用-接口加签-支付宝公钥"`
  88. }{
  89. PayAliAppId: sysCfg[enum2.OpenAlipayAppid],
  90. PayAliPrivateKey: sysCfg[enum2.OpenAlipayAppPrivateKey],
  91. PayAliPublicKey: sysCfg[enum2.OpenAlipayPublicKey],
  92. },
  93. OpAppId: sysCfg[enum2.OpenAppletAppid],
  94. BuyerId: user.UserId,
  95. TotalAmount: total,
  96. OutTradeNo: outTradeNo,
  97. Subject: "购买食堂套餐",
  98. AppAuthToken: sysCfg[enum2.JsapiPayAppAutToken],
  99. NotifyUrl: sysCfg[enum2.JsapiPayNotifyUrl],
  100. })
  101. if err != nil {
  102. return
  103. }
  104. //5、插入订单记录
  105. now := time.Now()
  106. centralKitchenForSchoolPackageOrd := db.CentralKitchenForSchoolPackageOrd{}
  107. centralKitchenForSchoolPackageOrd.Set(outTradeNo)
  108. _, err = centralKitchenForSchoolPackageOrd.CentralKitchenForSchoolPackageOrdInsertBySession(session, &model.CentralKitchenForSchoolPackageOrd{
  109. EnterpriseId: req.EnterpriseId,
  110. Uid: user.Id,
  111. UserIdentityId: req.UserIdentityId,
  112. UserIdentityName: userIdentity.Name,
  113. TotalPrice: total,
  114. Kind: req.Kind,
  115. OutTradeNo: outTradeNo,
  116. TradeNo: resp.TradeNo,
  117. State: enum2.CentralKitchenForSchoolPackageOrdStateForWait,
  118. OrdState: enum2.CentralKitchenForSchoolPackageOrdOrdStateForWait,
  119. ReqContent: string(utils.Serialize(req)),
  120. WithDayData: string(utils.Serialize(data)),
  121. PayWay: enum.PayWayForAli,
  122. CreateAt: now.Format("2006-01-02 15:04:05"),
  123. UpdateAt: now.Format("2006-01-02 15:04:05"),
  124. PayAt: "2006-01-02 15:04:05",
  125. })
  126. if err != nil {
  127. _ = session.Rollback()
  128. return
  129. }
  130. session.Commit()
  131. tradeNo = resp.TradeNo
  132. return
  133. }
  134. func BuyPackageForWx(c *gin.Context, req md.BuyPackageReq) (outTradeNo, total string, resp *jsapi2.PrepayWithRequestPaymentResponse, err error) {
  135. user := GetUser(c)
  136. session := db.Db.NewSession()
  137. defer session.Close()
  138. session.Begin()
  139. if err != nil {
  140. _ = session.Rollback()
  141. return
  142. }
  143. //1、判断是否为教师
  144. isTeacher := false
  145. userIdentityDb := db.UserIdentityDb{}
  146. userIdentityDb.Set(0)
  147. userIdentity, err := userIdentityDb.GetUserIdentity(req.UserIdentityId)
  148. if err != nil {
  149. return
  150. }
  151. if userIdentity == nil {
  152. err = errors.New("未查询到对应身份记录")
  153. return
  154. }
  155. if userIdentity.Identity == enum2.UserIdentityForCentralKitchenForTeacher {
  156. isTeacher = true
  157. }
  158. //2、计算数据(1:按学期购买 2:按月购买 3:按天购买 4:补餐)
  159. var totalPrice float64
  160. var data []*model.CentralKitchenForSchoolUserWithDay
  161. if req.Kind == 1 {
  162. totalPrice, data, err = CalcBySchoolTerm(user.Id, isTeacher, req)
  163. if err != nil {
  164. return
  165. }
  166. }
  167. if req.Kind == 2 {
  168. totalPrice, data, err = CalcByMonth(user.Id, isTeacher, req)
  169. if err != nil {
  170. return
  171. }
  172. }
  173. if req.Kind == 3 {
  174. totalPrice, data, err = CalcByDay(user.Id, isTeacher, req)
  175. if err != nil {
  176. return
  177. }
  178. }
  179. if req.Kind == 4 {
  180. totalPrice, data, err = CalcSupplementaryByDay(user.Id, isTeacher, req)
  181. if err != nil {
  182. return
  183. }
  184. }
  185. total = utils.Float64ToStr(totalPrice)
  186. //3、生成订单号
  187. outTradeNo = utils.OrderUUID(user.Id)
  188. //4、请求 /v3/pay/partner/transactions/jsapi (生成预支付交易单)
  189. sysCfgDb := db.SysCfgDb{}
  190. sysCfgDb.Set()
  191. client, err := svc3.NewWxPayClient(c)
  192. if err != nil {
  193. return
  194. }
  195. wxSvc := jsapi.JsapiApiService{Client: client}
  196. companyWithWxpayInfoDb := db.CompanyWithWxpayInfoDb{}
  197. companyWithWxpayInfoDb.Set()
  198. wxMchId, err := companyWithWxpayInfoDb.GetCompanyWithWxpayInfoByEnterprise(req.EnterpriseId)
  199. if err != nil {
  200. return "", "", nil, err
  201. }
  202. sysCfg := sysCfgDb.SysCfgFindWithDb(enum2.WxAppletAppId, enum2.WxSpAppId, enum2.WxSpMchId, enum2.WxAppletAppId, enum2.WxJsapiPayNotifyUrl)
  203. fmt.Println(int64(math.Round(totalPrice * 100)))
  204. result, _, err := wxSvc.Prepay(c,
  205. jsapi.PrepayRequest{
  206. SpAppid: core.String(sysCfg[enum2.WxSpAppId]),
  207. SpMchid: core.String(sysCfg[enum2.WxSpMchId]),
  208. SubAppid: core.String(sysCfg[enum2.WxAppletAppId]),
  209. SubMchid: core.String(wxMchId),
  210. Description: core.String("购买食堂套餐"),
  211. OutTradeNo: core.String(outTradeNo),
  212. TimeExpire: core.Time(time.Now().Add(20 * time.Minute)),
  213. //Attach: core.String("自定义数据说明"),
  214. NotifyUrl: core.String(sysCfg[enum2.WxJsapiPayNotifyUrl]),
  215. Amount: &jsapi.Amount{
  216. Currency: core.String("CNY"),
  217. Total: core.Int64(int64(math.Round(totalPrice * 100))),
  218. },
  219. Payer: &jsapi.Payer{
  220. //SpOpenid: core.String("oUpF8uMuAJO_M2pxb1Q9zNjWeS6o"),
  221. SubOpenid: core.String(user.OpenId),
  222. },
  223. },
  224. )
  225. fmt.Println(">>>>>>>>>>>", core.Int64(utils.StrToInt64(total)*100))
  226. if err != nil {
  227. _ = session.Rollback()
  228. return
  229. }
  230. //5、插入订单记录
  231. now := time.Now()
  232. centralKitchenForSchoolPackageOrd := db.CentralKitchenForSchoolPackageOrd{}
  233. centralKitchenForSchoolPackageOrd.Set(outTradeNo)
  234. _, err = centralKitchenForSchoolPackageOrd.CentralKitchenForSchoolPackageOrdInsertBySession(session, &model.CentralKitchenForSchoolPackageOrd{
  235. EnterpriseId: req.EnterpriseId,
  236. Uid: user.Id,
  237. UserIdentityId: req.UserIdentityId,
  238. UserIdentityName: userIdentity.Name,
  239. TotalPrice: total,
  240. Kind: req.Kind,
  241. OutTradeNo: outTradeNo,
  242. TradeNo: "",
  243. State: enum2.CentralKitchenForSchoolPackageOrdStateForWait,
  244. OrdState: enum2.CentralKitchenForSchoolPackageOrdOrdStateForWait,
  245. ReqContent: string(utils.Serialize(req)),
  246. WithDayData: string(utils.Serialize(data)),
  247. PayWay: enum.PayWayForWx,
  248. PrepayId: *result.PrepayId,
  249. CreateAt: now.Format("2006-01-02 15:04:05"),
  250. UpdateAt: now.Format("2006-01-02 15:04:05"),
  251. PayAt: "2006-01-02 15:04:05",
  252. })
  253. if err != nil {
  254. _ = session.Rollback()
  255. return
  256. }
  257. session.Commit()
  258. //prepayId = *resp.PrepayId
  259. resp = new(jsapi2.PrepayWithRequestPaymentResponse)
  260. resp.PrepayId = result.PrepayId
  261. resp.SignType = core.String("RSA")
  262. appid := sysCfg[enum2.WxAppletAppId]
  263. resp.Appid = &appid
  264. resp.TimeStamp = core.String(strconv.FormatInt(time.Now().Unix(), 10))
  265. nonce, err := utils2.GenerateNonce()
  266. if err != nil {
  267. _ = session.Rollback()
  268. return
  269. }
  270. resp.NonceStr = core.String(nonce)
  271. resp.Package = core.String("prepay_id=" + *result.PrepayId)
  272. message := fmt.Sprintf("%s\n%s\n%s\n%s\n", *resp.Appid, *resp.TimeStamp, *resp.NonceStr, *resp.Package)
  273. signatureResult, err := wxSvc.Client.Sign(c, message)
  274. if err != nil {
  275. _ = session.Rollback()
  276. return
  277. }
  278. resp.PaySign = core.String(signatureResult.Signature)
  279. return
  280. }
  281. func NursingHomeBuyPackageReq(c *gin.Context, req md.NursingHomeBuyPackageReq) (outTradeNo, tradeNo, total string, err error) {
  282. user := GetUser(c)
  283. session := db.Db.NewSession()
  284. defer session.Close()
  285. session.Begin()
  286. if err != nil {
  287. _ = session.Rollback()
  288. return
  289. }
  290. userIdentityDb := db.UserIdentityDb{}
  291. userIdentityDb.Set(0)
  292. userIdentity, err := userIdentityDb.GetUserIdentity(req.UserIdentityId)
  293. if err != nil {
  294. return
  295. }
  296. if userIdentity == nil {
  297. err = errors.New("未查询到对应身份记录")
  298. return
  299. }
  300. //1、计算数据(1:按年购买 2:按月购买 3:按天购买 4:补餐)
  301. var totalPrice float64
  302. var data []*model.NursingHomeUserWithDay
  303. if req.Kind == 1 {
  304. totalPrice, data, err = CalcByYearForNursingHome(user.Id, req)
  305. if err != nil {
  306. return
  307. }
  308. }
  309. if req.Kind == 2 {
  310. totalPrice, data, err = CalcByMonthForNursingHome(user.Id, req)
  311. if err != nil {
  312. return
  313. }
  314. }
  315. if req.Kind == 3 {
  316. totalPrice, data, err = CalcByDayForNursingHome(user.Id, req)
  317. if err != nil {
  318. return
  319. }
  320. }
  321. if req.Kind == 4 {
  322. totalPrice, data, err = CalcByDayForNursingHome(user.Id, req)
  323. if err != nil {
  324. return
  325. }
  326. }
  327. total = utils.Float64ToStr(totalPrice)
  328. //3、生成订单号
  329. outTradeNo = utils.OrderUUID(user.Id)
  330. //4、请求 alipay.trade.create(统一收单交易创建接口)
  331. sysCfgDb := db.SysCfgDb{}
  332. sysCfgDb.Set()
  333. sysCfg := sysCfgDb.SysCfgFindWithDb(enum2.OpenAppletAppid, enum2.OpenAlipayAppid, enum2.OpenAlipayAppPrivateKey, enum2.OpenAlipayPublicKey, enum2.JsapiPayAppAutToken)
  334. err, resp := CurlAlipayTradeCreate(md.CurlAlipayTradeCreateReq{
  335. Config: struct {
  336. PayAliAppId string `json:"pay_ali_app_id" label:"支付宝开放平台-第三方应用-appid"`
  337. PayAliPrivateKey string `json:"pay_ali_private_key" label:"支付宝开放平台-第三方应用-接口加签-应用私钥"`
  338. PayAliPublicKey string `json:"pay_ali_public_key" label:"支付宝开放平台-第三方应用-接口加签-支付宝公钥"`
  339. }{
  340. PayAliAppId: sysCfg[enum2.OpenAlipayAppid],
  341. PayAliPrivateKey: sysCfg[enum2.OpenAlipayAppPrivateKey],
  342. PayAliPublicKey: sysCfg[enum2.OpenAlipayPublicKey],
  343. },
  344. OpAppId: sysCfg[enum2.OpenAppletAppid],
  345. BuyerId: user.UserId,
  346. TotalAmount: total,
  347. OutTradeNo: outTradeNo,
  348. Subject: "购买食堂套餐",
  349. AppAuthToken: sysCfg[enum2.JsapiPayAppAutToken],
  350. })
  351. if err != nil {
  352. return
  353. }
  354. //5、插入订单记录
  355. now := time.Now()
  356. NursingHomePackageOrd := db.NursingHomePackageOrd{}
  357. NursingHomePackageOrd.Set(outTradeNo)
  358. _, err = NursingHomePackageOrd.NursingHomePackageOrdInsertBySession(session, &model.NursingHomePackageOrd{
  359. EnterpriseId: req.EnterpriseId,
  360. Uid: user.Id,
  361. UserIdentityId: req.UserIdentityId,
  362. UserIdentityName: userIdentity.Name,
  363. TotalPrice: total,
  364. Kind: req.Kind,
  365. OutTradeNo: outTradeNo,
  366. TradeNo: resp.TradeNo,
  367. State: enum2.NursingHomePackageOrdStateForWait,
  368. OrdState: enum2.NursingHomePackageOrdOrdStateForWait,
  369. ReqContent: string(utils.Serialize(req)),
  370. WithDayData: string(utils.Serialize(data)),
  371. CreateAt: now.Format("2006-01-02 15:04:05"),
  372. UpdateAt: now.Format("2006-01-02 15:04:05"),
  373. })
  374. if err != nil {
  375. _ = session.Rollback()
  376. return
  377. }
  378. session.Commit()
  379. tradeNo = resp.TradeNo
  380. return
  381. }
  382. func DealCentralKitchenForSchoolOrderCallBack(outTradeNo, tradeStatus, successTime string) (state int, err error) {
  383. centralKitchenForSchoolPackageOrd := db.CentralKitchenForSchoolPackageOrd{}
  384. centralKitchenForSchoolPackageOrd.Set(outTradeNo)
  385. ord, err := centralKitchenForSchoolPackageOrd.GetCentralKitchenForSchoolPackageOrd()
  386. if err != nil {
  387. return
  388. }
  389. if ord == nil {
  390. err = errors.New("未查询到对应订单记录")
  391. return
  392. }
  393. if ord.State == enum2.CentralKitchenForSchoolPackageOrdStateForWait {
  394. if tradeStatus == "TRADE_CLOSED" {
  395. ord.State = enum2.CentralKitchenForSchoolPackageOrdStateForFail
  396. }
  397. if tradeStatus == "TRADE_SUCCESS" {
  398. ord.State = enum2.CentralKitchenForSchoolPackageOrdStateForSuccess
  399. ord.OrdState = enum2.CentralKitchenForSchoolPackageOrdOrdStateForSuccess
  400. ord.PayAt = successTime
  401. //TODO::将预留数据插入到 `central_kitchen_for_school_user_with_day`
  402. var data []*model.CentralKitchenForSchoolUserWithDay
  403. err = json.Unmarshal([]byte(ord.WithDayData), &data)
  404. if err != nil {
  405. return
  406. }
  407. var hasMonths = map[string]map[string]interface{}{}
  408. var hasKinds = map[string]map[string]string{}
  409. var hasTotalPrice = map[string]map[string]float64{}
  410. for _, v := range data {
  411. date, _ := time.ParseInLocation("2006-01-02", v.Date, time.Local)
  412. if hasMonths[utils.TimeParseStd(v.Date).Month().String()] == nil {
  413. hasMonths[utils.TimeParseStd(v.Date).Month().String()] = make(map[string]interface{})
  414. hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_start"] = date.Format("2006-01-02")
  415. hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_end"] = date.Format("2006-01-02")
  416. } else {
  417. startTime, _ := time.ParseInLocation("2006-01-02", utils.AnyToString(hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_start"]), time.Local)
  418. if startTime.After(date) {
  419. hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_start"] = date.Format("2006-01-02")
  420. }
  421. endTime, _ := time.ParseInLocation("2006-01-02", utils.AnyToString(hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_end"]), time.Local)
  422. if endTime.Before(date) {
  423. hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_end"] = date.Format("2006-01-02")
  424. }
  425. }
  426. if hasTotalPrice[utils.TimeParseStd(v.Date).Month().String()] == nil {
  427. hasTotalPrice[utils.TimeParseStd(v.Date).Month().String()] = make(map[string]float64)
  428. }
  429. if hasKinds[utils.TimeParseStd(v.Date).Month().String()] == nil {
  430. hasKinds[utils.TimeParseStd(v.Date).Month().String()] = make(map[string]string)
  431. }
  432. hasMonths[utils.TimeParseStd(v.Date).Month().String()]["month"] = int(utils.TimeParseStd(v.Date).Month())
  433. hasKinds[utils.TimeParseStd(v.Date).Month().String()][utils.IntToStr(v.Kind)] = utils.IntToStr(v.Kind)
  434. hasTotalPrice[utils.TimeParseStd(v.Date).Month().String()][utils.IntToStr(v.Kind)] += utils.StrToFloat64(v.Amount)
  435. v.OrdNo = outTradeNo
  436. }
  437. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  438. centralKitchenForSchoolUserWithDayDb.Set(0)
  439. _, err = centralKitchenForSchoolUserWithDayDb.BatchAddCentralKitchenForSchoolUserWithDays(data)
  440. if err != nil {
  441. return
  442. }
  443. //TODO::插入数据至 `central_kitchen_for_school_package_ord_for_reserve`
  444. centralKitchenForSchoolPackageOrdForReserve := db.CentralKitchenForSchoolPackageOrdForReserve{}
  445. centralKitchenForSchoolPackageOrdForReserve.Set(outTradeNo)
  446. for k, v := range hasMonths {
  447. for _, v1 := range hasKinds[k] {
  448. _, err = centralKitchenForSchoolPackageOrdForReserve.CentralKitchenForSchoolPackageOrdForReserveInsert(&model.CentralKitchenForSchoolPackageOrdForReserve{
  449. EnterpriseId: ord.EnterpriseId,
  450. Uid: ord.Uid,
  451. UserIdentityName: ord.UserIdentityName,
  452. UserIdentityId: ord.UserIdentityId,
  453. Kind: utils.StrToInt(v1),
  454. OutTradeNo: ord.OutTradeNo,
  455. ReserveMonth: utils.AnyToString(v["month"]),
  456. MealTimeStart: utils.AnyToString(v["meal_time_start"]),
  457. MealTimeEnd: utils.AnyToString(v["meal_time_end"]),
  458. TotalPrice: utils.Float64ToStr(hasTotalPrice[k][v1]),
  459. CreateAt: time.Now().Format("2006-01-02 15:04:05"),
  460. UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
  461. })
  462. if err != nil {
  463. return
  464. }
  465. }
  466. }
  467. }
  468. _, err = centralKitchenForSchoolPackageOrd.CentralKitchenForSchoolPackageOrdUpdate(ord, "state", "ord_state", "pay_at")
  469. if err != nil {
  470. return
  471. }
  472. }
  473. state = ord.State
  474. return
  475. }
  476. func DealCentralKitchenForSchoolOrderCallBackForWx(outTradeNo, transactionId, tradeStatus, successTime string) (state int, err error) {
  477. centralKitchenForSchoolPackageOrd := db.CentralKitchenForSchoolPackageOrd{}
  478. centralKitchenForSchoolPackageOrd.Set(outTradeNo)
  479. ord, err := centralKitchenForSchoolPackageOrd.GetCentralKitchenForSchoolPackageOrd()
  480. if err != nil {
  481. return
  482. }
  483. if ord == nil {
  484. err = errors.New("未查询到对应订单记录")
  485. return
  486. }
  487. if ord.State == enum2.CentralKitchenForSchoolPackageOrdStateForWait {
  488. if tradeStatus == "CLOSED" || tradeStatus == "PAYERROR" {
  489. ord.State = enum2.CentralKitchenForSchoolPackageOrdStateForFail
  490. }
  491. if tradeStatus == "SUCCESS" {
  492. ord.State = enum2.CentralKitchenForSchoolPackageOrdStateForSuccess
  493. ord.OrdState = enum2.CentralKitchenForSchoolPackageOrdOrdStateForSuccess
  494. ord.TradeNo = transactionId
  495. ord.PayAt = successTime
  496. //TODO::将预留数据插入到 `central_kitchen_for_school_user_with_day`
  497. var data []*model.CentralKitchenForSchoolUserWithDay
  498. err = json.Unmarshal([]byte(ord.WithDayData), &data)
  499. if err != nil {
  500. return
  501. }
  502. var hasMonths = map[string]map[string]interface{}{}
  503. var hasKinds = map[string]map[string]string{}
  504. var hasTotalPrice = map[string]map[string]float64{}
  505. for _, v := range data {
  506. date, _ := time.ParseInLocation("2006-01-02", v.Date, time.Local)
  507. if hasMonths[utils.TimeParseStd(v.Date).Month().String()] == nil {
  508. hasMonths[utils.TimeParseStd(v.Date).Month().String()] = make(map[string]interface{})
  509. hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_start"] = date.Format("2006-01-02")
  510. hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_end"] = date.Format("2006-01-02")
  511. } else {
  512. startTime, _ := time.ParseInLocation("2006-01-02", utils.AnyToString(hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_start"]), time.Local)
  513. if startTime.After(date) {
  514. hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_start"] = date.Format("2006-01-02")
  515. }
  516. endTime, _ := time.ParseInLocation("2006-01-02", utils.AnyToString(hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_end"]), time.Local)
  517. if endTime.Before(date) {
  518. hasMonths[utils.TimeParseStd(v.Date).Month().String()]["meal_time_end"] = date.Format("2006-01-02")
  519. }
  520. }
  521. if hasTotalPrice[utils.TimeParseStd(v.Date).Month().String()] == nil {
  522. hasTotalPrice[utils.TimeParseStd(v.Date).Month().String()] = make(map[string]float64)
  523. }
  524. if hasKinds[utils.TimeParseStd(v.Date).Month().String()] == nil {
  525. hasKinds[utils.TimeParseStd(v.Date).Month().String()] = make(map[string]string)
  526. }
  527. hasMonths[utils.TimeParseStd(v.Date).Month().String()]["month"] = int(utils.TimeParseStd(v.Date).Month())
  528. hasKinds[utils.TimeParseStd(v.Date).Month().String()][utils.IntToStr(v.Kind)] = utils.IntToStr(v.Kind)
  529. hasTotalPrice[utils.TimeParseStd(v.Date).Month().String()][utils.IntToStr(v.Kind)] += utils.StrToFloat64(v.Amount)
  530. v.OrdNo = outTradeNo
  531. }
  532. centralKitchenForSchoolUserWithDayDb := db.CentralKitchenForSchoolUserWithDayDb{}
  533. centralKitchenForSchoolUserWithDayDb.Set(0)
  534. _, err = centralKitchenForSchoolUserWithDayDb.BatchAddCentralKitchenForSchoolUserWithDays(data)
  535. if err != nil {
  536. return
  537. }
  538. //TODO::插入数据至 `central_kitchen_for_school_package_ord_for_reserve`
  539. centralKitchenForSchoolPackageOrdForReserve := db.CentralKitchenForSchoolPackageOrdForReserve{}
  540. centralKitchenForSchoolPackageOrdForReserve.Set(outTradeNo)
  541. for k, v := range hasMonths {
  542. for _, v1 := range hasKinds[k] {
  543. _, err = centralKitchenForSchoolPackageOrdForReserve.CentralKitchenForSchoolPackageOrdForReserveInsert(&model.CentralKitchenForSchoolPackageOrdForReserve{
  544. EnterpriseId: ord.EnterpriseId,
  545. Uid: ord.Uid,
  546. UserIdentityName: ord.UserIdentityName,
  547. UserIdentityId: ord.UserIdentityId,
  548. Kind: utils.StrToInt(v1),
  549. OutTradeNo: ord.OutTradeNo,
  550. ReserveMonth: utils.AnyToString(v["month"]),
  551. MealTimeStart: utils.AnyToString(v["meal_time_start"]),
  552. MealTimeEnd: utils.AnyToString(v["meal_time_end"]),
  553. TotalPrice: utils.Float64ToStr(hasTotalPrice[k][v1]),
  554. CreateAt: time.Now().Format("2006-01-02 15:04:05"),
  555. UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
  556. })
  557. if err != nil {
  558. return
  559. }
  560. }
  561. }
  562. }
  563. _, err = centralKitchenForSchoolPackageOrd.CentralKitchenForSchoolPackageOrdUpdate(ord, "state", "ord_state", "trade_no", "pay_at")
  564. if err != nil {
  565. return
  566. }
  567. }
  568. state = ord.State
  569. return
  570. }
  571. func CheckAllCallbackParams(c *gin.Context) interface{} {
  572. body, _ := ioutil.ReadAll(c.Request.Body)
  573. utils.FilePutContents("CheckAllCallbackParams", utils.SerializeStr(string(body)))
  574. dataAlipay, _ := GetAlipayCallbackParams(body)
  575. if dataAlipay != nil {
  576. return dataAlipay
  577. }
  578. return nil
  579. }
  580. // GetAlipayCallbackParams 支付宝参数解析
  581. func GetAlipayCallbackParams(body []byte) (*md.AliPayCallback, error) {
  582. decodeArgs, err := url.QueryUnescape(string(body))
  583. if err != nil {
  584. _ = logx.Warn(err)
  585. return nil, logx.Warn("回调参数解码错误")
  586. }
  587. data, err := url.ParseQuery(decodeArgs)
  588. if err != nil {
  589. return nil, err
  590. }
  591. dataMap := make(map[string]interface{})
  592. for k := range data {
  593. dataMap[k] = data.Get(k)
  594. }
  595. callbackStr := utils.Serialize(dataMap)
  596. fmt.Println("支付宝回调数据", string(callbackStr))
  597. var args md.AliPayCallback
  598. if err := json.Unmarshal(callbackStr, &args); err != nil {
  599. return nil, logx.Warn(err)
  600. }
  601. return &args, nil
  602. }