智慧食堂
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

svc_pay.go 24 KiB

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