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

332 行
10 KiB

  1. package hdl
  2. import (
  3. "applet/app/admin/lib/validate"
  4. "applet/app/admin/md"
  5. svc "applet/app/admin/svc/order"
  6. md2 "applet/app/customer/md"
  7. svc2 "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. "fmt"
  15. "github.com/gin-gonic/gin"
  16. "github.com/wechatpay-apiv3/wechatpay-go/core"
  17. "github.com/wechatpay-apiv3/wechatpay-go/services/refunddomestic"
  18. "math"
  19. "time"
  20. )
  21. // Demo 测试
  22. func Demo(c *gin.Context) {
  23. eg := db.Db
  24. var mm []model.CentralKitchenForSchoolPackageOrdForReserve
  25. if err1 := eg.Asc("id").Find(&mm); err1 != nil {
  26. e.OutErr(c, e.ERR, err1.Error())
  27. return
  28. }
  29. for _, v := range mm {
  30. m := new(model.CentralKitchenForSchoolUserWithDay)
  31. has, err := db.Db.Where("ord_no =?", v.OutTradeNo).Get(m)
  32. if err != nil {
  33. fmt.Println(err)
  34. return
  35. }
  36. if !has {
  37. utils.FilePutContents("demo_demo", utils.SerializeStr(v))
  38. }
  39. if m.IdentityId != v.UserIdentityId {
  40. utils.FilePutContents("demo", utils.SerializeStr(v))
  41. }
  42. if m.EnterpriseId != v.EnterpriseId {
  43. utils.FilePutContents("demo111", utils.SerializeStr(v))
  44. }
  45. }
  46. return
  47. var m []model.CentralKitchenForSchoolUserWithDay
  48. if err1 := eg.Where("state = 3").Asc("id").Find(&m); err1 != nil {
  49. e.OutErr(c, e.ERR, err1.Error())
  50. return
  51. }
  52. now := time.Now()
  53. var records []int
  54. for _, v := range m {
  55. mm := new(model.CentralKitchenForSchoolUserRefundDay)
  56. has, err := db.Db.Where("records_id =? and state = 1", v.Id).Get(mm)
  57. if err != nil {
  58. fmt.Println(err)
  59. return
  60. }
  61. if has == false {
  62. centralKitchenForSchoolUserRefundDayDb := db.CentralKitchenForSchoolUserRefundDayDb{}
  63. centralKitchenForSchoolUserRefundDayDb.Set(0)
  64. _, err = centralKitchenForSchoolUserRefundDayDb.CentralKitchenForSchoolUserRefundDayInsert(&model.CentralKitchenForSchoolUserRefundDay{
  65. OutTradeNo: v.OrdNo,
  66. OutRequestNo: "",
  67. Uid: v.Uid,
  68. IdentityId: v.IdentityId,
  69. RecordsId: v.Id,
  70. State: enum.CentralKitchenForSchoolUserRefundDayStateForAuditing,
  71. Amount: v.Amount,
  72. Memo: "fill",
  73. CreateAt: now.Format("2006-01-02 15:04:05"),
  74. UpdateAt: now.Format("2006-01-02 15:04:05"),
  75. })
  76. records = append(records, v.Id)
  77. }
  78. }
  79. e.OutSuc(c, records, nil)
  80. return
  81. var req md.CentralKitchenForSchoolOrderRefundAuditReq
  82. err := c.ShouldBindJSON(&req)
  83. if err != nil {
  84. err = validate.HandleValidateErr(err)
  85. err1 := err.(e.E)
  86. e.OutErr(c, err1.Code, err1.Error())
  87. return
  88. }
  89. err1 := svc.CentralKitchenForSchoolOrderRefundAudit(c, req)
  90. if err1 != nil {
  91. fmt.Println(err1.Error())
  92. e.OutErr(c, e.ERR, err1.Error())
  93. return
  94. }
  95. e.OutSuc(c, "success", nil)
  96. return
  97. sysCfgDb := db.SysCfgDb{}
  98. sysCfgDb.Set()
  99. sysCfg := sysCfgDb.SysCfgFindWithDb(enum.OpenAlipayAppid, enum.OpenAlipayAppPrivateKey, enum.OpenAlipayPublicKey)
  100. //调用支付宝进行退款
  101. err6, _ := svc2.OldCurlAlipayTradeRefund(md2.OldCurlAlipayTradeRefundReq{
  102. Config: struct {
  103. PayAliAppId string `json:"pay_ali_app_id" label:"支付宝开放平台-第三方应用-appid"`
  104. PayAliPrivateKey string `json:"pay_ali_private_key" label:"支付宝开放平台-第三方应用-接口加签-应用私钥"`
  105. PayAliPublicKey string `json:"pay_ali_public_key" label:"支付宝开放平台-第三方应用-接口加签-支付宝公钥"`
  106. }{
  107. PayAliAppId: sysCfg[enum.OpenAlipayAppid],
  108. PayAliPrivateKey: sysCfg[enum.OpenAlipayAppPrivateKey],
  109. PayAliPublicKey: sysCfg[enum.OpenAlipayPublicKey],
  110. },
  111. OutTradeNo: "800169659125681442",
  112. RefundAmount: "180.00",
  113. RefundReason: "央厨订餐退款",
  114. OutRequestNo: "r_800169659125681442_1705546121",
  115. AppAuthToken: "202308BB8ef30019cd8d4a40a3853192c397dX35",
  116. })
  117. if err6 != nil {
  118. fmt.Println(err6.Error())
  119. }
  120. return
  121. qrcodeWithBatchRecordsDb := db.AdminDb{}
  122. qrcodeWithBatchRecordsDb.Set()
  123. data, _, err := qrcodeWithBatchRecordsDb.GetAdminRolePermission(1001)
  124. if err != nil {
  125. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  126. return
  127. }
  128. e.OutSuc(c, data, nil)
  129. go GoOnExecuting()
  130. return
  131. }
  132. // Demo 测试
  133. func TempDemo(c *gin.Context) {
  134. eg := db.Db
  135. var list []*model.CentralKitchenForSchoolUserWithDay
  136. err := db.Db.Where("state = 3").Asc("id").Find(&list)
  137. if err != nil {
  138. return
  139. }
  140. for _, m := range list {
  141. mm := new(model.CentralKitchenForSchoolUserRefundDay)
  142. has, err := db.Db.Where("records_id =? and state = 4", m.Id).Get(mm)
  143. if err != nil {
  144. fmt.Println(err)
  145. return
  146. }
  147. if has {
  148. m.State = 4
  149. updateAffetcd, err := db.Db.Where("id =?", m.Id).Cols("state").Update(m)
  150. if err != nil {
  151. fmt.Println(err)
  152. return
  153. }
  154. fmt.Println(">>>>>>>>>>>", updateAffetcd)
  155. }
  156. }
  157. return
  158. var m []model.CentralKitchenForSchoolUserWithDay
  159. if err1 := eg.Where("state = 3").Asc("id").Find(&m); err1 != nil {
  160. e.OutErr(c, e.ERR, err1.Error())
  161. return
  162. }
  163. now := time.Now()
  164. var records []int
  165. for _, v := range m {
  166. mm := new(model.CentralKitchenForSchoolUserRefundDay)
  167. has, err := db.Db.Where("records_id =? and state = 1", v.Id).Get(mm)
  168. if err != nil {
  169. fmt.Println(err)
  170. return
  171. }
  172. if has == false {
  173. centralKitchenForSchoolUserRefundDayDb := db.CentralKitchenForSchoolUserRefundDayDb{}
  174. centralKitchenForSchoolUserRefundDayDb.Set(0)
  175. _, err = centralKitchenForSchoolUserRefundDayDb.CentralKitchenForSchoolUserRefundDayInsert(&model.CentralKitchenForSchoolUserRefundDay{
  176. OutTradeNo: v.OrdNo,
  177. OutRequestNo: "",
  178. Uid: v.Uid,
  179. IdentityId: v.IdentityId,
  180. RecordsId: v.Id,
  181. State: enum.CentralKitchenForSchoolUserRefundDayStateForAuditing,
  182. Amount: v.Amount,
  183. Memo: "fill",
  184. CreateAt: now.Format("2006-01-02 15:04:05"),
  185. UpdateAt: now.Format("2006-01-02 15:04:05"),
  186. })
  187. records = append(records, v.Id)
  188. }
  189. }
  190. e.OutSuc(c, records, nil)
  191. return
  192. var req md.CentralKitchenForSchoolOrderRefundAuditReq
  193. err = c.ShouldBindJSON(&req)
  194. if err != nil {
  195. err = validate.HandleValidateErr(err)
  196. err1 := err.(e.E)
  197. e.OutErr(c, err1.Code, err1.Error())
  198. return
  199. }
  200. err1 := svc.CentralKitchenForSchoolOrderRefundAudit(c, req)
  201. if err1 != nil {
  202. fmt.Println(err1.Error())
  203. e.OutErr(c, e.ERR, err1.Error())
  204. return
  205. }
  206. e.OutSuc(c, "success", nil)
  207. return
  208. sysCfgDb := db.SysCfgDb{}
  209. sysCfgDb.Set()
  210. sysCfg := sysCfgDb.SysCfgFindWithDb(enum.OpenAlipayAppid, enum.OpenAlipayAppPrivateKey, enum.OpenAlipayPublicKey)
  211. //调用支付宝进行退款
  212. err6, _ := svc2.OldCurlAlipayTradeRefund(md2.OldCurlAlipayTradeRefundReq{
  213. Config: struct {
  214. PayAliAppId string `json:"pay_ali_app_id" label:"支付宝开放平台-第三方应用-appid"`
  215. PayAliPrivateKey string `json:"pay_ali_private_key" label:"支付宝开放平台-第三方应用-接口加签-应用私钥"`
  216. PayAliPublicKey string `json:"pay_ali_public_key" label:"支付宝开放平台-第三方应用-接口加签-支付宝公钥"`
  217. }{
  218. PayAliAppId: sysCfg[enum.OpenAlipayAppid],
  219. PayAliPrivateKey: sysCfg[enum.OpenAlipayAppPrivateKey],
  220. PayAliPublicKey: sysCfg[enum.OpenAlipayPublicKey],
  221. },
  222. OutTradeNo: "800169659125681442",
  223. RefundAmount: "180.00",
  224. RefundReason: "央厨订餐退款",
  225. OutRequestNo: "r_800169659125681442_1705546121",
  226. AppAuthToken: "202308BB8ef30019cd8d4a40a3853192c397dX35",
  227. })
  228. if err6 != nil {
  229. fmt.Println(err6.Error())
  230. }
  231. return
  232. qrcodeWithBatchRecordsDb := db.AdminDb{}
  233. qrcodeWithBatchRecordsDb.Set()
  234. data, _, err := qrcodeWithBatchRecordsDb.GetAdminRolePermission(1001)
  235. if err != nil {
  236. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  237. return
  238. }
  239. e.OutSuc(c, data, nil)
  240. go GoOnExecuting()
  241. return
  242. }
  243. func DemoV1(c *gin.Context) {
  244. var outRequestNo = c.Query("out_request_no")
  245. var outTradeNo = c.Query("out_no")
  246. centralKitchenForSchoolUserRefundDayDb := db.CentralKitchenForSchoolUserRefundDayDb{}
  247. centralKitchenForSchoolUserRefundDayDb.Set(0)
  248. var m []model.CentralKitchenForSchoolUserRefundDay
  249. if err := centralKitchenForSchoolUserRefundDayDb.Db.Where("out_request_no =?", outRequestNo).Desc("id").Find(&m); err != nil {
  250. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  251. return
  252. }
  253. centralKitchenForSchoolPackageOrd := db.CentralKitchenForSchoolPackageOrd{}
  254. centralKitchenForSchoolPackageOrd.Set(outTradeNo)
  255. ord, _ := centralKitchenForSchoolPackageOrd.GetCentralKitchenForSchoolPackageOrd()
  256. var dealReturnOrdMapForWx = map[string]struct {
  257. OutRequestNo string `json:"out_request_no"`
  258. Amount float64 `json:"amount"`
  259. Total string `json:"total"`
  260. WxMchId string `json:"wx_mch_id"`
  261. }{}
  262. companyWithWxpayInfoDb := db.CompanyWithWxpayInfoDb{}
  263. companyWithWxpayInfoDb.Set()
  264. wxMchId, _ := companyWithWxpayInfoDb.GetCompanyWithWxpayInfoByEnterprise(ord.EnterpriseId)
  265. for _, v := range m {
  266. dealReturnOrdMapForWx[ord.OutTradeNo] = struct {
  267. OutRequestNo string `json:"out_request_no"`
  268. Amount float64 `json:"amount"`
  269. Total string `json:"total"`
  270. WxMchId string `json:"wx_mch_id"`
  271. }(struct {
  272. OutRequestNo string
  273. Amount float64
  274. Total string
  275. WxMchId string
  276. }{OutRequestNo: outRequestNo, Amount: dealReturnOrdMapForWx[v.OutTradeNo].Amount + utils.StrToFloat64(v.Amount), Total: ord.TotalPrice, WxMchId: wxMchId})
  277. }
  278. client, err1 := svc3.NewWxPayClient(c) //初始化微信连接
  279. if err1 != nil {
  280. e.OutErr(c, e.ERR, err1.Error())
  281. return
  282. }
  283. sysCfgDb := db.SysCfgDb{}
  284. sysCfgDb.Set()
  285. sysCfg := sysCfgDb.SysCfgFindWithDb(enum.JsapiPayAppAutToken, enum.OpenAlipayAppid, enum.OpenAlipayAppPrivateKey, enum.OpenAlipayPublicKey, enum.WxJsapiRefundPayNotifyUrl)
  286. wxSvc := refunddomestic.RefundsApiService{Client: client}
  287. for k, v := range dealReturnOrdMapForWx {
  288. resp, _, err6 := wxSvc.Create(c,
  289. refunddomestic.CreateRequest{
  290. SubMchid: core.String(v.WxMchId),
  291. OutTradeNo: core.String(k),
  292. OutRefundNo: core.String(v.OutRequestNo),
  293. Reason: core.String("央厨订餐退款"),
  294. NotifyUrl: core.String(sysCfg[enum.WxJsapiRefundPayNotifyUrl]),
  295. Amount: &refunddomestic.AmountReq{
  296. Currency: core.String("CNY"),
  297. Refund: core.Int64(int64(math.Round(v.Amount * 100))),
  298. Total: core.Int64(int64(math.Round(utils.StrToFloat64(v.Total) * 100))),
  299. },
  300. },
  301. )
  302. fmt.Println(">>>>wxRefund<<<<", resp)
  303. if err6 != nil {
  304. fmt.Println(err6)
  305. e.OutErr(c, e.ERR, err6.Error())
  306. return
  307. }
  308. }
  309. e.OutSuc(c, dealReturnOrdMapForWx, nil)
  310. return
  311. }
  312. func GoOnExecuting() {
  313. for i := 0; i < 20; i++ {
  314. time.Sleep(1 * time.Second)
  315. fmt.Println(i)
  316. }
  317. }