order.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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. var wg sync.WaitGroup
  203. wg.Add(len(List))
  204. for _, orderItem := range List {
  205. go func(orderItem *models.ProductOrder) {
  206. defer wg.Done()
  207. view := &models.ProductOrderView{
  208. OrderID: orderItem.OrderID,
  209. RealName: orderItem.RealName,
  210. Mobile: fmt.Sprintf("+%s %s", orderItem.AreaCode, orderItem.Mobile),
  211. ProductType: ProductTypeMap[orderItem.ProductType],
  212. ProductName: orderItem.ProductName,
  213. TotalAmount: orderItem.TotalAmount,
  214. TradeNO: orderItem.TradeNO,
  215. RefundAmount: orderItem.RefundAmount,
  216. PaymentWay: PaymentWayMap[orderItem.PaymentWay],
  217. Status: ProductOrderStatus[orderItem.Status],
  218. RefundStatus: RefundStatusMap[orderItem.RefundStatus],
  219. Remark: orderItem.Remark,
  220. CreatedTime: orderItem.CreatedTime.Format(time.DateTime),
  221. }
  222. if orderItem.TradeNO != "" {
  223. view.PaymentTime = orderItem.PaymentTime.Format(time.DateTime)
  224. tradeOrder, tradeErr := models.GetTradeOrderByNo(orderItem.TradeNO)
  225. if tradeErr != nil {
  226. utils.FileLog.Error("获取支付订单失败,支付订单号:" + orderItem.TradeNO + ",err:" + tradeErr.Error())
  227. } else {
  228. view.PaymentAmount = fmt.Sprintf("%s %.2f", tradeOrder.Currency, tradeOrder.Amount)
  229. }
  230. }
  231. if orderItem.Status == models.OrderStatusPaid {
  232. access, accessErr := models.GetAccess(orderItem.ProductID, orderItem.TemplateUserID)
  233. if accessErr != nil {
  234. utils.FileLog.Error("获取用户订阅记录失败,templateUserId:" + string(rune(orderItem.TemplateUserID)) + "productId:" + string(rune(orderItem.ProductID)) + ",err:" + accessErr.Error())
  235. }
  236. if access.ProductType == models.ProductPackage {
  237. view.ValidDuration = fmt.Sprintf("%s~%s", access.BeginDate.Format(time.DateOnly), access.EndDate.Format(time.DateOnly))
  238. } else {
  239. view.ValidDuration = "永久有效"
  240. }
  241. }
  242. if orderItem.Status == models.OrderStatusRefund && orderItem.RefundStatus == models.RefundStatusSuccess {
  243. view.RefundFinishTime = orderItem.RefundFinishTime.Format(time.DateTime)
  244. }
  245. ListView = append(ListView, view)
  246. }(orderItem)
  247. }
  248. wg.Wait()
  249. page := paging.GetPaging(currentIndex, pageSize, total)
  250. resp := new(response.ProductOrderListResp)
  251. resp.List = ListView
  252. resp.Paging = page
  253. br.Ret = 200
  254. br.Success = true
  255. br.Data = resp
  256. br.Msg = "获取成功"
  257. }
  258. // TradeOrderList
  259. // @Title 支付订单列表
  260. // @Description 支付订单列表
  261. // @Param PageSize query int true "每页数据条数"
  262. // @Param CurrentIndex query int true "当前页页码,从1开始"
  263. // @Param ClassifyIds query string true "二级分类id,可多选用英文,隔开"
  264. // @Param KeyWord query string true "报告标题/创建人"
  265. // @Param SortType query string true "排序方式"
  266. // @Success 200 {object} models.ReportAuthorResp
  267. // @router /tradeOrderList [get]
  268. func (this *OrderController) TradeOrderList() {
  269. br := new(models.BaseResponse).Init()
  270. defer func() {
  271. this.Data["json"] = br
  272. this.ServeJSON()
  273. }()
  274. pageSize, _ := this.GetInt("PageSize")
  275. currentIndex, _ := this.GetInt("CurrentIndex")
  276. sortType := this.GetString("SortType")
  277. KeyWord := this.GetString("KeyWord")
  278. DealDate := this.GetString("DealDate")
  279. PaymentWay := this.GetString("PaymentWay")
  280. CreatedDate := this.GetString("CreatedDate")
  281. OrderStatus := this.GetString("OrderStatus")
  282. IsRefund, _ := this.GetBool("IsRefund", false)
  283. var condition string
  284. if pageSize <= 0 {
  285. pageSize = utils.PageSize20
  286. }
  287. if currentIndex <= 0 {
  288. currentIndex = 1
  289. }
  290. if IsRefund {
  291. condition += " AND payment_type ='" + string(models.PaymentTypeRefund) + "'"
  292. } else {
  293. condition += " AND payment_type ='" + string(models.PaymentTypePay) + "'"
  294. }
  295. if KeyWord != "" {
  296. condition += " AND (product_name like '%" + KeyWord + "%' or real_name like '%" + KeyWord + "%' order_id like '%" + KeyWord + "%' or mobile like '%" + KeyWord + "%')"
  297. }
  298. sortCondition := " ORDER BY created_time "
  299. if sortType == "" {
  300. sortType = "DESC"
  301. }
  302. if CreatedDate != "" {
  303. condition += " AND Date(created_time) = '" + CreatedDate + "'"
  304. }
  305. if DealDate != "" {
  306. condition += " AND Date(deal_time) = '" + DealDate + "'"
  307. }
  308. if PaymentWay != "" {
  309. condition += " AND payment_way='" + PaymentWay + "'"
  310. }
  311. if OrderStatus != "" {
  312. switch OrderStatus {
  313. case "pending":
  314. condition += " AND payment_status='pending'"
  315. case "done":
  316. condition += " AND payment_status='done'"
  317. case "failed":
  318. condition += " AND payment_status='failed'"
  319. default:
  320. br.Msg = "无效的支付订单状态"
  321. br.ErrMsg = "无效的支付订单状态:" + OrderStatus
  322. return
  323. }
  324. }
  325. sortCondition = sortCondition + sortType
  326. total, err := models.GetTradeOrderCountByCondition(condition)
  327. if err != nil {
  328. br.Msg = "获取支付明细列表失败"
  329. br.ErrMsg = "获取支付明细列表失败,Err:" + err.Error()
  330. return
  331. }
  332. startSize := utils.StartIndex(currentIndex, pageSize)
  333. List, err := models.GetTradeOrderByCondition(condition, sortCondition, startSize, pageSize)
  334. if err != nil {
  335. br.Msg = "获取支付明细列表失败"
  336. br.ErrMsg = "获取支付明细列表失败,Err:" + err.Error()
  337. return
  338. }
  339. var ListView []*models.TradeOrderView
  340. var wg sync.WaitGroup
  341. wg.Add(len(List))
  342. for i := 0; i < len(List); i++ {
  343. go func(order *models.TradeOrder) {
  344. defer wg.Done()
  345. productOrder, pdErr := models.GetProductOrderByID(order.ProductOrderId)
  346. if pdErr != nil {
  347. utils.FileLog.Error("获取商品订单信息失败,Err:" + pdErr.Error())
  348. }
  349. view := &models.TradeOrderView{
  350. RealName: productOrder.RealName,
  351. Mobile: fmt.Sprintf("+%s %s", productOrder.AreaCode, productOrder.Mobile),
  352. ProductName: productOrder.ProductName,
  353. Amount: order.Amount,
  354. TransactionID: order.TransactionId,
  355. ProductOrderID: order.ProductOrderId,
  356. PaymentWay: PaymentWayMap[order.PaymentWay],
  357. PaymentAccount: order.PaymentAccount,
  358. MerchantID: order.MerchantId,
  359. DealTime: order.DealTime.Format(time.DateTime),
  360. CreatedTime: order.CreatedTime.Format(time.DateTime),
  361. }
  362. if IsRefund {
  363. view.PaymentStatus = RefundOrderStatus[order.PaymentStatus]
  364. } else {
  365. view.PaymentStatus = TradeOrderStatus[order.PaymentStatus]
  366. }
  367. ListView = append(ListView, view)
  368. }(List[i])
  369. }
  370. wg.Wait()
  371. page := paging.GetPaging(currentIndex, pageSize, total)
  372. resp := new(response.TradeOrderListResp)
  373. resp.List = ListView
  374. resp.Paging = page
  375. br.Ret = 200
  376. br.Success = true
  377. br.Data = resp
  378. br.Msg = "获取成功"
  379. }
  380. // ExportProductOrder
  381. // @Title 临时用户列表
  382. // @Description 临时用户列表
  383. // @Param PageSize query int true "每页数据条数"
  384. // @Param CurrentIndex query int true "当前页页码,从1开始"
  385. // @Param Keyword query string false "手机号"
  386. // @Param SortParam query string false "排序字段参数,用来排序的字段, 枚举值:0:注册时间,1:阅读数,2:最近一次阅读时间"
  387. // @Param SortType query string true "如何排序,是正序还是倒序,0:倒序,1:正序"
  388. // @Success 200 {object} response.TemplateUserListResp
  389. // @router /productOrder/export [get]
  390. func (this *OrderController) ExportProductOrder() {
  391. br := new(models.BaseResponse).Init()
  392. defer func() {
  393. this.Data["json"] = br
  394. this.ServeJSON()
  395. }()
  396. pageSize, _ := this.GetInt("PageSize")
  397. currentIndex, _ := this.GetInt("CurrentIndex")
  398. sortType := this.GetString("SortType")
  399. KeyWord := this.GetString("KeyWord")
  400. PaymentDate := this.GetString("PaymentDate")
  401. PaymentWay := this.GetString("PaymentWay")
  402. CreatedDate := this.GetString("CreatedDate")
  403. ProductType := this.GetString("ProductType")
  404. RefundStatus := this.GetString("RefundStatus")
  405. OrderStatus := this.GetString("OrderStatus")
  406. var condition string
  407. if pageSize <= 0 {
  408. pageSize = utils.PageSize20
  409. }
  410. if currentIndex <= 0 {
  411. currentIndex = 1
  412. }
  413. if KeyWord != "" {
  414. condition += " AND (product_name like '%" + KeyWord + "%' or real_name like '%" + KeyWord + "%' order_id like '%" + KeyWord + "%' or mobile like '%" + KeyWord + "%')"
  415. }
  416. sortCondition := " ORDER BY created_time "
  417. if sortType == "" {
  418. sortType = "DESC"
  419. }
  420. if CreatedDate != "" {
  421. condition += " AND Date(created_time) = '" + CreatedDate + "'"
  422. }
  423. if PaymentDate != "" {
  424. condition += " AND Date(payment_time) = '" + PaymentDate + "'"
  425. }
  426. if PaymentWay != "" {
  427. condition += " AND payment_way='" + PaymentWay + "'"
  428. }
  429. if OrderStatus != "" {
  430. switch OrderStatus {
  431. case "pending":
  432. condition += " AND status='pending'"
  433. case "paid":
  434. condition += " AND status='paid'"
  435. case "closed":
  436. condition += " AND status='closed'"
  437. case "refund":
  438. condition += " AND status='refund'"
  439. if RefundStatus != "" {
  440. switch RefundStatus {
  441. case "pending":
  442. condition += " AND refund_status='pending'"
  443. case "processing":
  444. condition += " AND refund_status='processing'"
  445. case "failed":
  446. condition += " AND refund_status='failed'"
  447. case "success":
  448. condition += " AND refund_status='success'"
  449. }
  450. }
  451. default:
  452. br.Msg = "无效的订单状态"
  453. br.ErrMsg = "无效的订单状态:" + OrderStatus
  454. return
  455. }
  456. }
  457. if ProductType != "" {
  458. switch ProductType {
  459. case "report":
  460. condition += " AND product_type='" + string(models.ProductReport) + "'"
  461. case "audio":
  462. condition += " AND product_type='" + string(models.ProductAudio) + "'"
  463. case "video":
  464. condition += " AND product_type='" + string(models.ProductVideo) + "'"
  465. case "package":
  466. condition += " AND product_type='" + string(models.ProductPackage) + "'"
  467. default:
  468. br.Msg = "无效的产品类型"
  469. br.ErrMsg = "无效的产品类型:" + ProductType
  470. return
  471. }
  472. }
  473. sortCondition = sortCondition + sortType
  474. List, err := models.GetProductOrderListByCondition(condition, sortCondition)
  475. if err != nil {
  476. br.Msg = "导出商品订单失败"
  477. br.ErrMsg = "导出商品订单失败,Err:" + err.Error()
  478. return
  479. }
  480. var ListView []models.ProductOrderView
  481. for _, order := range List {
  482. view := models.ProductOrderView{
  483. OrderID: order.OrderID,
  484. RealName: order.RealName,
  485. Mobile: fmt.Sprintf("+%s %s", order.AreaCode, order.Mobile),
  486. ProductType: ProductTypeMap[order.ProductType],
  487. ProductName: order.ProductName,
  488. TotalAmount: order.TotalAmount,
  489. TradeNO: order.TradeNO,
  490. RefundAmount: order.RefundAmount,
  491. PaymentWay: PaymentWayMap[order.PaymentWay],
  492. PaymentTime: order.PaymentTime.Format(time.DateTime),
  493. Status: ProductOrderStatus[order.Status],
  494. RefundStatus: RefundStatusMap[order.RefundStatus],
  495. RefundFinishTime: order.RefundFinishTime.Format(time.DateTime),
  496. Remark: order.Remark,
  497. CreatedTime: order.CreatedTime.Format(time.DateTime),
  498. }
  499. ListView = append(ListView, view)
  500. }
  501. year, month, day := time.Now().Date()
  502. yearStr := strconv.Itoa(year)[2:]
  503. fileName := fmt.Sprintf("商品订单%s.%d.%d.xlsx", yearStr, month, day)
  504. file, err := utils.ExportExcel("商品订单", productCols, ListView)
  505. _ = this.downloadExcelFile(file, fileName)
  506. br.Ret = 200
  507. br.Success = true
  508. br.Msg = "下载成功"
  509. }
  510. // ExportTradeOrder
  511. // @Title 临时用户列表
  512. // @Description 临时用户列表
  513. // @Param PageSize query int true "每页数据条数"
  514. // @Param CurrentIndex query int true "当前页页码,从1开始"
  515. // @Param Keyword query string false "手机号"
  516. // @Param SortParam query string false "排序字段参数,用来排序的字段, 枚举值:0:注册时间,1:阅读数,2:最近一次阅读时间"
  517. // @Param SortType query string true "如何排序,是正序还是倒序,0:倒序,1:正序"
  518. // @Success 200 {object} response.TemplateUserListResp
  519. // @router /tradeOrder/export [get]
  520. func (this *OrderController) ExportTradeOrder() {
  521. br := new(models.BaseResponse).Init()
  522. defer func() {
  523. this.Data["json"] = br
  524. this.ServeJSON()
  525. }()
  526. pageSize, _ := this.GetInt("PageSize")
  527. currentIndex, _ := this.GetInt("CurrentIndex")
  528. sortType := this.GetString("SortType")
  529. KeyWord := this.GetString("KeyWord")
  530. DealDate := this.GetString("DealDate")
  531. PaymentWay := this.GetString("PaymentWay")
  532. CreatedDate := this.GetString("CreatedDate")
  533. OrderStatus := this.GetString("OrderStatus")
  534. IsRefund, _ := this.GetBool("IsRefund", false)
  535. var condition string
  536. if pageSize <= 0 {
  537. pageSize = utils.PageSize20
  538. }
  539. if currentIndex <= 0 {
  540. currentIndex = 1
  541. }
  542. if IsRefund {
  543. condition += " AND payment_type ='" + string(models.PaymentTypeRefund) + "'"
  544. } else {
  545. condition += " AND payment_type ='" + string(models.PaymentTypePay) + "'"
  546. }
  547. if KeyWord != "" {
  548. condition += " AND (product_name like '%" + KeyWord + "%' or real_name like '%" + KeyWord + "%' order_id like '%" + KeyWord + "%' or mobile like '%" + KeyWord + "%')"
  549. }
  550. sortCondition := " ORDER BY created_time "
  551. if sortType == "" {
  552. sortType = "DESC"
  553. }
  554. if CreatedDate != "" {
  555. condition += " AND Date(created_time) = '" + CreatedDate + "'"
  556. }
  557. if DealDate != "" {
  558. condition += " AND Date(deal_time) = '" + DealDate + "'"
  559. }
  560. if PaymentWay != "" {
  561. condition += " AND payment_way='" + PaymentWay + "'"
  562. }
  563. if OrderStatus != "" {
  564. switch OrderStatus {
  565. case "pending":
  566. condition += " AND payment_status='pending'"
  567. case "done":
  568. condition += " AND payment_status='done'"
  569. case "failed":
  570. condition += " AND payment_status='failed'"
  571. default:
  572. br.Msg = "无效的支付订单状态"
  573. br.ErrMsg = "无效的支付订单状态:" + OrderStatus
  574. return
  575. }
  576. }
  577. sortCondition = sortCondition + sortType
  578. List, err := models.GetTradeOrderListByCondition(condition, sortCondition)
  579. if err != nil {
  580. br.Msg = "获取支付明细列表失败"
  581. br.ErrMsg = "获取支付明细列表失败,Err:" + err.Error()
  582. return
  583. }
  584. var ListView []models.TradeOrderView
  585. var wg sync.WaitGroup
  586. wg.Add(len(List))
  587. for i := 0; i < len(List); i++ {
  588. go func(order *models.TradeOrder) {
  589. defer wg.Done()
  590. productOrder, pdErr := models.GetProductOrderByID(order.ProductOrderId)
  591. if pdErr != nil {
  592. utils.FileLog.Error("获取商品订单信息失败,Err:" + pdErr.Error())
  593. }
  594. view := models.TradeOrderView{
  595. RealName: productOrder.RealName,
  596. Mobile: fmt.Sprintf("+%s %s", productOrder.AreaCode, productOrder.Mobile),
  597. ProductName: productOrder.ProductName,
  598. Amount: order.Amount,
  599. TransactionID: order.TransactionId,
  600. ProductOrderID: order.ProductOrderId,
  601. PaymentWay: PaymentWayMap[order.PaymentWay],
  602. PaymentAccount: order.PaymentAccount,
  603. MerchantID: order.MerchantId,
  604. DealTime: order.DealTime.Format(time.DateTime),
  605. CreatedTime: order.CreatedTime.Format(time.DateTime),
  606. }
  607. if IsRefund {
  608. view.PaymentStatus = RefundOrderStatus[order.PaymentStatus]
  609. } else {
  610. view.PaymentStatus = TradeOrderStatus[order.PaymentStatus]
  611. }
  612. ListView = append(ListView, view)
  613. }(List[i])
  614. }
  615. wg.Wait()
  616. year, month, day := time.Now().Date()
  617. yearStr := strconv.Itoa(year)[2:]
  618. if IsRefund {
  619. fileName := fmt.Sprintf("退款明细%s.%d.%d.xlsx", yearStr, month, day)
  620. file, _ := utils.ExportExcel("退款明细", refundCols, ListView)
  621. _ = this.downloadExcelFile(file, fileName)
  622. } else {
  623. fileName := fmt.Sprintf("支付明细%s.%d.%d.xlsx", yearStr, month, day)
  624. file, _ := utils.ExportExcel("支付明细", tradeCols, ListView)
  625. _ = this.downloadExcelFile(file, fileName)
  626. }
  627. br.Ret = 200
  628. br.Success = true
  629. br.Msg = "下载成功"
  630. }
  631. // encodeChineseFilename 将中文文件名编码为 ISO-8859-1
  632. func (this *OrderController) downloadExcelFile(file *excelize.File, filename string) (err error) {
  633. // 对文件名进行 ISO-8859-1 编码
  634. fn := url.QueryEscape(filename)
  635. if filename == fn {
  636. fn = "filename=" + fn
  637. } else {
  638. fn = "filename=" + filename + "; filename*=utf-8''" + fn
  639. }
  640. this.Ctx.ResponseWriter.Header().Set("Access-Control-Expose-Headers", "Content-Disposition")
  641. this.Ctx.ResponseWriter.Header().Set("Content-Disposition", "attachment; "+fn)
  642. this.Ctx.ResponseWriter.Header().Set("Content-Description", "File Transfer")
  643. this.Ctx.ResponseWriter.Header().Set("Content-Type", "application/octet-stream")
  644. this.Ctx.ResponseWriter.Header().Set("Content-Transfer-Encoding", "binary")
  645. this.Ctx.ResponseWriter.Header().Set("Expires", "0")
  646. this.Ctx.ResponseWriter.Header().Set("Cache-Control", "must-revalidate")
  647. this.Ctx.ResponseWriter.Header().Set("Pragma", "public")
  648. this.Ctx.ResponseWriter.Header().Set("File-Name", filename)
  649. // 写入文件
  650. if err = file.Write(this.Ctx.ResponseWriter); err != nil {
  651. utils.FileLog.Error("导出excel文件失败:", err)
  652. http.Error(this.Ctx.ResponseWriter, "导出excel文件失败", http.StatusInternalServerError)
  653. }
  654. return
  655. }