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

333 rivejä
11 KiB

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