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

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