order.go 23 KB

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