智慧食堂
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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