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

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