智慧食堂
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

359 行
12 KiB

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