order.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. package controllers
  2. import (
  3. "eta/eta_mini_crm_ht/models"
  4. "eta/eta_mini_crm_ht/models/response"
  5. "eta/eta_mini_crm_ht/utils"
  6. "fmt"
  7. "github.com/rdlucklib/rdluck_tools/paging"
  8. "github.com/xuri/excelize/v2"
  9. "net/http"
  10. "net/url"
  11. "strconv"
  12. "sync"
  13. "time"
  14. )
  15. var (
  16. productCols = map[string]utils.ExcelColMapping{
  17. "A": {"订单编号", "OrderID"},
  18. "B": {"姓名", "RealName"},
  19. "C": {"手机号", "Mobile"},
  20. "D": {"商品名称", "ProductName"},
  21. "E": {"商品类型", "ProductType"},
  22. "F": {"商品价格", "Price"},
  23. "G": {"有效期", "ValidDate"},
  24. "H": {"订单状态", "Status"},
  25. "I": {"支付渠道", "PaymentWay"},
  26. "J": {"支付金额", "TotalAmount"},
  27. "K": {"售后状态", "RefundStatus"},
  28. "L": {"付款时间", "PaymentTime"},
  29. "M": {"下单时间", "CreatedTime"},
  30. }
  31. tradeCols = map[string]utils.ExcelColMapping{
  32. "A": {"支付订单", "TransactionId"},
  33. "B": {"订单编号", "OrderID"},
  34. "C": {"姓名", "RealName"},
  35. "D": {"手机号", "Mobile"},
  36. "E": {"商品名称", "ProductName"},
  37. "F": {"支付金额", "Amount"},
  38. "G": {"支付状态", "PaymentStatus"},
  39. "H": {"支付渠道", "PaymentWay"},
  40. "I": {"支付账号", "PaymentAccount"},
  41. "J": {"收款方", "MerchantId"},
  42. "K": {"完成支付时间", "DealTime"},
  43. "L": {"创建时间", "CreatedTime"},
  44. }
  45. refundCols = map[string]utils.ExcelColMapping{
  46. "A": {"退款订单", "TransactionId"},
  47. "B": {"订单编号", "OrderID"},
  48. "C": {"姓名", "RealName"},
  49. "D": {"手机号", "Mobile"},
  50. "E": {"退款金额", "Amount"},
  51. "F": {"退回账号", "PaymentAccount"},
  52. "G": {"退款状态", "PaymentStatus"},
  53. "H": {"完成退款时间", "DealTime"},
  54. "I": {"创建时间", "CreatedTime"},
  55. }
  56. ProductOrderStatus = map[models.OrderStatus]string{
  57. "pending": "待支付",
  58. "processing": "支付中",
  59. "paid": "已支付",
  60. "closed": "已关闭",
  61. "refund": "已退款",
  62. }
  63. TradeOrderStatus = map[models.PaymentStatus]string{
  64. "pending": "待支付",
  65. "failed": "支付失败",
  66. "done": "支付成功",
  67. }
  68. RefundOrderStatus = map[models.PaymentStatus]string{
  69. "pending": "退款中",
  70. "failed": "退款失败",
  71. "done": "退款成功",
  72. }
  73. RefundStatusMap = map[models.RefundStatus]string{
  74. "pending": "待退款",
  75. "processing": "退款中",
  76. "failed": "退款失败",
  77. "success": "退款成功",
  78. "canceled": "已取消",
  79. }
  80. ProductTypeMap = map[models.MerchantProductType]string{
  81. "report": "报告",
  82. "video": "视频",
  83. "audio": "音频",
  84. "package": "套餐",
  85. }
  86. PaymentWayMap = map[models.PaymentWay]string{
  87. "wechat": "微信",
  88. "alipay": "支付宝",
  89. }
  90. )
  91. type OrderController struct {
  92. BaseAuthController
  93. }
  94. // ProductOrderList
  95. // @Title 商品订单列表
  96. // @Description 商品订单列表
  97. // @Param PageSize query int true "每页数据条数"
  98. // @Param CurrentIndex query int true "当前页页码,从1开始"
  99. // @Param ClassifyIds query string true "二级分类id,可多选用英文,隔开"
  100. // @Param KeyWord query string true "报告标题/创建人"
  101. // @Param SortType query string true "排序方式"
  102. // @Success 200 {object} models.ReportAuthorResp
  103. // @router /productOrderList [get]
  104. func (this *OrderController) ProductOrderList() {
  105. br := new(models.BaseResponse).Init()
  106. defer func() {
  107. this.Data["json"] = br
  108. this.ServeJSON()
  109. }()
  110. pageSize, _ := this.GetInt("PageSize")
  111. currentIndex, _ := this.GetInt("CurrentIndex")
  112. sortType := this.GetString("SortType")
  113. KeyWord := this.GetString("KeyWord")
  114. PaymentDate := this.GetString("PaymentDate")
  115. PaymentWay := this.GetString("PaymentWay")
  116. CreatedDate := this.GetString("CreatedDate")
  117. ProductType := this.GetString("ProductType")
  118. RefundStatus := this.GetString("RefundStatus")
  119. OrderStatus := this.GetString("OrderStatus")
  120. var condition string
  121. if pageSize <= 0 {
  122. pageSize = utils.PageSize20
  123. }
  124. if currentIndex <= 0 {
  125. currentIndex = 1
  126. }
  127. if KeyWord != "" {
  128. condition += " AND (product_name like '%" + KeyWord + "%' or real_name like '%" + KeyWord + "%' order_id like '%" + KeyWord + "%' or mobile like '%" + KeyWord + "%')"
  129. }
  130. sortCondition := " ORDER BY created_time "
  131. if sortType == "" {
  132. sortType = "DESC"
  133. }
  134. if CreatedDate != "" {
  135. condition += " AND Date(created_time) = '" + CreatedDate + "'"
  136. }
  137. if PaymentDate != "" {
  138. condition += " AND Date(payment_time) = '" + PaymentDate + "'"
  139. }
  140. if PaymentWay != "" {
  141. condition += " AND payment_way='" + PaymentWay + "'"
  142. }
  143. if OrderStatus != "" {
  144. switch OrderStatus {
  145. case "pending":
  146. condition += " AND status='pending'"
  147. case "paid":
  148. condition += " AND status='paid'"
  149. case "closed":
  150. condition += " AND status='closed'"
  151. case "refund":
  152. condition += " AND status='refund'"
  153. if RefundStatus != "" {
  154. switch RefundStatus {
  155. case "pending":
  156. condition += " AND refund_status='pending'"
  157. case "processing":
  158. condition += " AND refund_status='processing'"
  159. case "failed":
  160. condition += " AND refund_status='failed'"
  161. case "success":
  162. condition += " AND refund_status='success'"
  163. }
  164. }
  165. default:
  166. br.Msg = "无效的订单状态"
  167. br.ErrMsg = "无效的订单状态:" + OrderStatus
  168. return
  169. }
  170. }
  171. if ProductType != "" {
  172. switch ProductType {
  173. case "report":
  174. condition += " AND product_type='" + string(models.ProductReport) + "'"
  175. case "audio":
  176. condition += " AND product_type='" + string(models.ProductAudio) + "'"
  177. case "video":
  178. condition += " AND product_type='" + string(models.ProductVideo) + "'"
  179. case "package":
  180. condition += " AND product_type='" + string(models.ProductPackage) + "'"
  181. default:
  182. br.Msg = "无效的产品类型"
  183. br.ErrMsg = "无效的产品类型:" + ProductType
  184. return
  185. }
  186. }
  187. sortCondition = sortCondition + sortType
  188. total, err := models.GetProductOrderCountByCondition(condition)
  189. if err != nil {
  190. br.Msg = "获取商品列表失败"
  191. br.ErrMsg = "获取商品列表失败,Err:" + err.Error()
  192. return
  193. }
  194. startSize := utils.StartIndex(currentIndex, pageSize)
  195. List, err := models.GetProductOrderByCondition(condition, sortCondition, startSize, pageSize)
  196. if err != nil {
  197. br.Msg = "获取商品列表失败"
  198. br.ErrMsg = "获取商品列表失败,Err:" + err.Error()
  199. return
  200. }
  201. var ListView []*models.ProductOrderView
  202. for _, order := range List {
  203. view := &models.ProductOrderView{
  204. OrderID: order.OrderID,
  205. RealName: order.RealName,
  206. Mobile: fmt.Sprintf("+%s %s", order.AreaCode, order.Mobile),
  207. ProductType: ProductTypeMap[order.ProductType],
  208. ProductName: order.ProductName,
  209. TotalAmount: order.TotalAmount,
  210. TradeNO: order.TradeNO,
  211. RefundAmount: order.RefundAmount,
  212. PaymentWay: PaymentWayMap[order.PaymentWay],
  213. PaymentTime: order.PaymentTime.Format(time.DateTime),
  214. Status: ProductOrderStatus[order.Status],
  215. RefundStatus: RefundStatusMap[order.RefundStatus],
  216. RefundFinishTime: order.RefundFinishTime.Format(time.DateTime),
  217. Remark: order.Remark,
  218. CreatedTime: order.CreatedTime.Format(time.DateTime),
  219. }
  220. ListView = append(ListView, view)
  221. }
  222. page := paging.GetPaging(currentIndex, pageSize, total)
  223. resp := new(response.ProductOrderListResp)
  224. resp.List = ListView
  225. resp.Paging = page
  226. br.Ret = 200
  227. br.Success = true
  228. br.Data = resp
  229. br.Msg = "获取成功"
  230. }
  231. // TradeOrderList
  232. // @Title 支付订单列表
  233. // @Description 支付订单列表
  234. // @Param PageSize query int true "每页数据条数"
  235. // @Param CurrentIndex query int true "当前页页码,从1开始"
  236. // @Param ClassifyIds query string true "二级分类id,可多选用英文,隔开"
  237. // @Param KeyWord query string true "报告标题/创建人"
  238. // @Param SortType query string true "排序方式"
  239. // @Success 200 {object} models.ReportAuthorResp
  240. // @router /tradeOrderList [get]
  241. func (this *OrderController) TradeOrderList() {
  242. br := new(models.BaseResponse).Init()
  243. defer func() {
  244. this.Data["json"] = br
  245. this.ServeJSON()
  246. }()
  247. pageSize, _ := this.GetInt("PageSize")
  248. currentIndex, _ := this.GetInt("CurrentIndex")
  249. sortType := this.GetString("SortType")
  250. KeyWord := this.GetString("KeyWord")
  251. DealDate := this.GetString("DealDate")
  252. PaymentWay := this.GetString("PaymentWay")
  253. CreatedDate := this.GetString("CreatedDate")
  254. OrderStatus := this.GetString("OrderStatus")
  255. IsRefund, _ := this.GetBool("IsRefund", false)
  256. var condition string
  257. if pageSize <= 0 {
  258. pageSize = utils.PageSize20
  259. }
  260. if currentIndex <= 0 {
  261. currentIndex = 1
  262. }
  263. if IsRefund {
  264. condition += " AND payment_type ='" + string(models.PaymentTypeRefund) + "'"
  265. } else {
  266. condition += " AND payment_type ='" + string(models.PaymentTypePay) + "'"
  267. }
  268. if KeyWord != "" {
  269. condition += " AND (product_name like '%" + KeyWord + "%' or real_name like '%" + KeyWord + "%' order_id like '%" + KeyWord + "%' or mobile like '%" + KeyWord + "%')"
  270. }
  271. sortCondition := " ORDER BY created_time "
  272. if sortType == "" {
  273. sortType = "DESC"
  274. }
  275. if CreatedDate != "" {
  276. condition += " AND Date(created_time) = '" + CreatedDate + "'"
  277. }
  278. if DealDate != "" {
  279. condition += " AND Date(deal_time) = '" + DealDate + "'"
  280. }
  281. if PaymentWay != "" {
  282. condition += " AND payment_way='" + PaymentWay + "'"
  283. }
  284. if OrderStatus != "" {
  285. switch OrderStatus {
  286. case "pending":
  287. condition += " AND payment_status='pending'"
  288. case "done":
  289. condition += " AND payment_status='done'"
  290. case "failed":
  291. condition += " AND payment_status='failed'"
  292. default:
  293. br.Msg = "无效的支付订单状态"
  294. br.ErrMsg = "无效的支付订单状态:" + OrderStatus
  295. return
  296. }
  297. }
  298. sortCondition = sortCondition + sortType
  299. total, err := models.GetTradeOrderCountByCondition(condition)
  300. if err != nil {
  301. br.Msg = "获取支付明细列表失败"
  302. br.ErrMsg = "获取支付明细列表失败,Err:" + err.Error()
  303. return
  304. }
  305. startSize := utils.StartIndex(currentIndex, pageSize)
  306. List, err := models.GetTradeOrderByCondition(condition, sortCondition, startSize, pageSize)
  307. if err != nil {
  308. br.Msg = "获取支付明细列表失败"
  309. br.ErrMsg = "获取支付明细列表失败,Err:" + err.Error()
  310. return
  311. }
  312. var ListView []*models.TradeOrderView
  313. var wg sync.WaitGroup
  314. wg.Add(len(List))
  315. for i := 0; i < len(List); i++ {
  316. go func(order *models.TradeOrder) {
  317. defer wg.Done()
  318. productOrder, pdErr := models.GetProductOrderByID(order.ProductOrderId)
  319. if pdErr != nil {
  320. utils.FileLog.Error("获取商品订单信息失败,Err:" + pdErr.Error())
  321. }
  322. view := &models.TradeOrderView{
  323. RealName: productOrder.RealName,
  324. Mobile: fmt.Sprintf("+%s %s", productOrder.AreaCode, productOrder.Mobile),
  325. ProductName: productOrder.ProductName,
  326. Amount: order.Amount,
  327. TransactionID: order.TransactionId,
  328. ProductOrderID: order.ProductOrderId,
  329. PaymentWay: PaymentWayMap[order.PaymentWay],
  330. PaymentAccount: order.PaymentAccount,
  331. MerchantID: order.MerchantId,
  332. DealTime: order.DealTime.Format(time.DateTime),
  333. CreatedTime: order.CreatedTime.Format(time.DateTime),
  334. }
  335. if IsRefund {
  336. view.PaymentStatus = RefundOrderStatus[order.PaymentStatus]
  337. } else {
  338. view.PaymentStatus = TradeOrderStatus[order.PaymentStatus]
  339. }
  340. ListView = append(ListView, view)
  341. }(List[i])
  342. }
  343. wg.Wait()
  344. page := paging.GetPaging(currentIndex, pageSize, total)
  345. resp := new(response.TradeOrderListResp)
  346. resp.List = ListView
  347. resp.Paging = page
  348. br.Ret = 200
  349. br.Success = true
  350. br.Data = resp
  351. br.Msg = "获取成功"
  352. }
  353. // ExportProductOrder
  354. // @Title 临时用户列表
  355. // @Description 临时用户列表
  356. // @Param PageSize query int true "每页数据条数"
  357. // @Param CurrentIndex query int true "当前页页码,从1开始"
  358. // @Param Keyword query string false "手机号"
  359. // @Param SortParam query string false "排序字段参数,用来排序的字段, 枚举值:0:注册时间,1:阅读数,2:最近一次阅读时间"
  360. // @Param SortType query string true "如何排序,是正序还是倒序,0:倒序,1:正序"
  361. // @Success 200 {object} response.TemplateUserListResp
  362. // @router /exportProductOrder [get]
  363. func (this *OrderController) ExportProductOrder() {
  364. br := new(models.BaseResponse).Init()
  365. defer func() {
  366. this.Data["json"] = br
  367. this.ServeJSON()
  368. }()
  369. pageSize, _ := this.GetInt("PageSize")
  370. currentIndex, _ := this.GetInt("CurrentIndex")
  371. sortType := this.GetString("SortType")
  372. KeyWord := this.GetString("KeyWord")
  373. PaymentDate := this.GetString("PaymentDate")
  374. PaymentWay := this.GetString("PaymentWay")
  375. CreatedDate := this.GetString("CreatedDate")
  376. ProductType := this.GetString("ProductType")
  377. RefundStatus := this.GetString("RefundStatus")
  378. OrderStatus := this.GetString("OrderStatus")
  379. var condition string
  380. if pageSize <= 0 {
  381. pageSize = utils.PageSize20
  382. }
  383. if currentIndex <= 0 {
  384. currentIndex = 1
  385. }
  386. if KeyWord != "" {
  387. condition += " AND (product_name like '%" + KeyWord + "%' or real_name like '%" + KeyWord + "%' order_id like '%" + KeyWord + "%' or mobile like '%" + KeyWord + "%')"
  388. }
  389. sortCondition := " ORDER BY created_time "
  390. if sortType == "" {
  391. sortType = "DESC"
  392. }
  393. if CreatedDate != "" {
  394. condition += " AND Date(created_time) = '" + CreatedDate + "'"
  395. }
  396. if PaymentDate != "" {
  397. condition += " AND Date(payment_time) = '" + PaymentDate + "'"
  398. }
  399. if PaymentWay != "" {
  400. condition += " AND payment_way='" + PaymentWay + "'"
  401. }
  402. if OrderStatus != "" {
  403. switch OrderStatus {
  404. case "pending":
  405. condition += " AND status='pending'"
  406. case "paid":
  407. condition += " AND status='paid'"
  408. case "closed":
  409. condition += " AND status='closed'"
  410. case "refund":
  411. condition += " AND status='refund'"
  412. if RefundStatus != "" {
  413. switch RefundStatus {
  414. case "pending":
  415. condition += " AND refund_status='pending'"
  416. case "processing":
  417. condition += " AND refund_status='processing'"
  418. case "failed":
  419. condition += " AND refund_status='failed'"
  420. case "success":
  421. condition += " AND refund_status='success'"
  422. }
  423. }
  424. default:
  425. br.Msg = "无效的订单状态"
  426. br.ErrMsg = "无效的订单状态:" + OrderStatus
  427. return
  428. }
  429. }
  430. if ProductType != "" {
  431. switch ProductType {
  432. case "report":
  433. condition += " AND product_type='" + string(models.ProductReport) + "'"
  434. case "audio":
  435. condition += " AND product_type='" + string(models.ProductAudio) + "'"
  436. case "video":
  437. condition += " AND product_type='" + string(models.ProductVideo) + "'"
  438. case "package":
  439. condition += " AND product_type='" + string(models.ProductPackage) + "'"
  440. default:
  441. br.Msg = "无效的产品类型"
  442. br.ErrMsg = "无效的产品类型:" + ProductType
  443. return
  444. }
  445. }
  446. sortCondition = sortCondition + sortType
  447. List, err := models.GetProductOrderListByCondition(condition, sortCondition)
  448. if err != nil {
  449. br.Msg = "导出商品订单失败"
  450. br.ErrMsg = "导出商品订单失败,Err:" + err.Error()
  451. return
  452. }
  453. var ListView []*models.ProductOrderView
  454. for _, order := range List {
  455. view := &models.ProductOrderView{
  456. OrderID: order.OrderID,
  457. RealName: order.RealName,
  458. Mobile: fmt.Sprintf("+%s %s", order.AreaCode, order.Mobile),
  459. ProductType: ProductTypeMap[order.ProductType],
  460. ProductName: order.ProductName,
  461. TotalAmount: order.TotalAmount,
  462. TradeNO: order.TradeNO,
  463. RefundAmount: order.RefundAmount,
  464. PaymentWay: PaymentWayMap[order.PaymentWay],
  465. PaymentTime: order.PaymentTime.Format(time.DateTime),
  466. Status: ProductOrderStatus[order.Status],
  467. RefundStatus: RefundStatusMap[order.RefundStatus],
  468. RefundFinishTime: order.RefundFinishTime.Format(time.DateTime),
  469. Remark: order.Remark,
  470. CreatedTime: order.CreatedTime.Format(time.DateTime),
  471. }
  472. ListView = append(ListView, view)
  473. }
  474. year, month, day := time.Now().Date()
  475. yearStr := strconv.Itoa(year)[2:]
  476. fileName := fmt.Sprintf("商品订单%s.%d.%d.xlsx", yearStr, month, day)
  477. file, err := utils.ExportExcel("商品订单", productCols, ListView)
  478. _ = this.downloadExcelFile(file, fileName)
  479. br.Ret = 200
  480. br.Success = true
  481. br.Msg = "下载成功"
  482. }
  483. // ExportTradeOrder
  484. // @Title 临时用户列表
  485. // @Description 临时用户列表
  486. // @Param PageSize query int true "每页数据条数"
  487. // @Param CurrentIndex query int true "当前页页码,从1开始"
  488. // @Param Keyword query string false "手机号"
  489. // @Param SortParam query string false "排序字段参数,用来排序的字段, 枚举值:0:注册时间,1:阅读数,2:最近一次阅读时间"
  490. // @Param SortType query string true "如何排序,是正序还是倒序,0:倒序,1:正序"
  491. // @Success 200 {object} response.TemplateUserListResp
  492. // @router /exportTradeOrder [get]
  493. func (this *OrderController) ExportTradeOrder() {
  494. br := new(models.BaseResponse).Init()
  495. defer func() {
  496. this.Data["json"] = br
  497. this.ServeJSON()
  498. }()
  499. pageSize, _ := this.GetInt("PageSize")
  500. currentIndex, _ := this.GetInt("CurrentIndex")
  501. sortType := this.GetString("SortType")
  502. KeyWord := this.GetString("KeyWord")
  503. DealDate := this.GetString("DealDate")
  504. PaymentWay := this.GetString("PaymentWay")
  505. CreatedDate := this.GetString("CreatedDate")
  506. OrderStatus := this.GetString("OrderStatus")
  507. IsRefund, _ := this.GetBool("IsRefund", false)
  508. var condition string
  509. if pageSize <= 0 {
  510. pageSize = utils.PageSize20
  511. }
  512. if currentIndex <= 0 {
  513. currentIndex = 1
  514. }
  515. if IsRefund {
  516. condition += " AND payment_type ='" + string(models.PaymentTypeRefund) + "'"
  517. } else {
  518. condition += " AND payment_type ='" + string(models.PaymentTypePay) + "'"
  519. }
  520. if KeyWord != "" {
  521. condition += " AND (product_name like '%" + KeyWord + "%' or real_name like '%" + KeyWord + "%' order_id like '%" + KeyWord + "%' or mobile like '%" + KeyWord + "%')"
  522. }
  523. sortCondition := " ORDER BY created_time "
  524. if sortType == "" {
  525. sortType = "DESC"
  526. }
  527. if CreatedDate != "" {
  528. condition += " AND Date(created_time) = '" + CreatedDate + "'"
  529. }
  530. if DealDate != "" {
  531. condition += " AND Date(deal_time) = '" + DealDate + "'"
  532. }
  533. if PaymentWay != "" {
  534. condition += " AND payment_way='" + PaymentWay + "'"
  535. }
  536. if OrderStatus != "" {
  537. switch OrderStatus {
  538. case "pending":
  539. condition += " AND payment_status='pending'"
  540. case "done":
  541. condition += " AND payment_status='done'"
  542. case "failed":
  543. condition += " AND payment_status='failed'"
  544. default:
  545. br.Msg = "无效的支付订单状态"
  546. br.ErrMsg = "无效的支付订单状态:" + OrderStatus
  547. return
  548. }
  549. }
  550. sortCondition = sortCondition + sortType
  551. List, err := models.GetTradeOrderListByCondition(condition, sortCondition)
  552. if err != nil {
  553. br.Msg = "获取支付明细列表失败"
  554. br.ErrMsg = "获取支付明细列表失败,Err:" + err.Error()
  555. return
  556. }
  557. var ListView []*models.TradeOrderView
  558. var wg sync.WaitGroup
  559. wg.Add(len(List))
  560. for i := 0; i < len(List); i++ {
  561. go func(order *models.TradeOrder) {
  562. defer wg.Done()
  563. productOrder, pdErr := models.GetProductOrderByID(order.ProductOrderId)
  564. if pdErr != nil {
  565. utils.FileLog.Error("获取商品订单信息失败,Err:" + pdErr.Error())
  566. }
  567. view := &models.TradeOrderView{
  568. RealName: productOrder.RealName,
  569. Mobile: fmt.Sprintf("+%s %s", productOrder.AreaCode, productOrder.Mobile),
  570. ProductName: productOrder.ProductName,
  571. Amount: order.Amount,
  572. TransactionID: order.TransactionId,
  573. ProductOrderID: order.ProductOrderId,
  574. PaymentWay: PaymentWayMap[order.PaymentWay],
  575. PaymentAccount: order.PaymentAccount,
  576. MerchantID: order.MerchantId,
  577. DealTime: order.DealTime.Format(time.DateTime),
  578. CreatedTime: order.CreatedTime.Format(time.DateTime),
  579. }
  580. if IsRefund {
  581. view.PaymentStatus = RefundOrderStatus[order.PaymentStatus]
  582. } else {
  583. view.PaymentStatus = TradeOrderStatus[order.PaymentStatus]
  584. }
  585. ListView = append(ListView, view)
  586. }(List[i])
  587. }
  588. wg.Wait()
  589. year, month, day := time.Now().Date()
  590. yearStr := strconv.Itoa(year)[2:]
  591. if IsRefund {
  592. fileName := fmt.Sprintf("退款明细%s.%d.%d.xlsx", yearStr, month, day)
  593. file, _ := utils.ExportExcel("退款明细", refundCols, ListView)
  594. _ = this.downloadExcelFile(file, fileName)
  595. } else {
  596. fileName := fmt.Sprintf("支付明细%s.%d.%d.xlsx", yearStr, month, day)
  597. file, _ := utils.ExportExcel("支付明细", tradeCols, ListView)
  598. _ = this.downloadExcelFile(file, fileName)
  599. }
  600. br.Ret = 200
  601. br.Success = true
  602. br.Msg = "下载成功"
  603. }
  604. // encodeChineseFilename 将中文文件名编码为 ISO-8859-1
  605. func (this *OrderController) downloadExcelFile(file *excelize.File, filename string) (err error) {
  606. // 对文件名进行 ISO-8859-1 编码
  607. fn := url.QueryEscape(filename)
  608. if filename == fn {
  609. fn = "filename=" + fn
  610. } else {
  611. fn = "filename=" + filename + "; filename*=utf-8''" + fn
  612. }
  613. this.Ctx.ResponseWriter.Header().Set("Access-Control-Expose-Headers", "Content-Disposition")
  614. this.Ctx.ResponseWriter.Header().Set("Content-Disposition", "attachment; "+fn)
  615. this.Ctx.ResponseWriter.Header().Set("Content-Description", "File Transfer")
  616. this.Ctx.ResponseWriter.Header().Set("Content-Type", "application/octet-stream")
  617. this.Ctx.ResponseWriter.Header().Set("Content-Transfer-Encoding", "binary")
  618. this.Ctx.ResponseWriter.Header().Set("Expires", "0")
  619. this.Ctx.ResponseWriter.Header().Set("Cache-Control", "must-revalidate")
  620. this.Ctx.ResponseWriter.Header().Set("Pragma", "public")
  621. this.Ctx.ResponseWriter.Header().Set("File-Name", filename)
  622. // 写入文件
  623. if err = file.Write(this.Ctx.ResponseWriter); err != nil {
  624. utils.FileLog.Error("导出excel文件失败:", err)
  625. http.Error(this.Ctx.ResponseWriter, "导出excel文件失败", http.StatusInternalServerError)
  626. }
  627. return
  628. }