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

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