order.go 23 KB

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