order.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_mini_crm_ht/models"
  5. "eta/eta_mini_crm_ht/models/request"
  6. "eta/eta_mini_crm_ht/models/response"
  7. "eta/eta_mini_crm_ht/utils"
  8. "fmt"
  9. "github.com/google/uuid"
  10. "github.com/rdlucklib/rdluck_tools/paging"
  11. "github.com/xuri/excelize/v2"
  12. "math/rand"
  13. "net/http"
  14. "net/url"
  15. "strconv"
  16. "strings"
  17. "time"
  18. )
  19. var (
  20. productCols = map[string]utils.ExcelColMapping{
  21. "A": {"订单编号", "OrderID"},
  22. "B": {"姓名", "RealName"},
  23. "C": {"手机号", "Mobile"},
  24. "D": {"商品名称", "ProductName"},
  25. "E": {"商品类型", "ProductType"},
  26. "F": {"商品价格", "TotalAmount"},
  27. "G": {"有效期", "ValidDate"},
  28. "H": {"订单状态", "Status"},
  29. "I": {"支付渠道", "PaymentWay"},
  30. "J": {"支付金额", "PaymentAmount"},
  31. "K": {"售后状态", "RefundStatus"},
  32. "L": {"付款时间", "PaymentTime"},
  33. "M": {"下单时间", "CreatedTime"},
  34. }
  35. tradeCols = map[string]utils.ExcelColMapping{
  36. "A": {"支付订单", "TransactionID"},
  37. "B": {"订单编号", "OrderID"},
  38. "C": {"姓名", "RealName"},
  39. "D": {"手机号", "Mobile"},
  40. "E": {"商品名称", "ProductName"},
  41. "F": {"支付金额", "Amount"},
  42. "G": {"支付状态", "PaymentStatus"},
  43. "H": {"支付渠道", "PaymentWay"},
  44. "I": {"支付账号", "PaymentAccount"},
  45. "J": {"收款方", "MerchantID"},
  46. "K": {"完成支付时间", "DealTime"},
  47. "L": {"创建时间", "CreatedTime"},
  48. }
  49. refundCols = map[string]utils.ExcelColMapping{
  50. "A": {"退款订单", "TransactionId"},
  51. "B": {"订单编号", "OrderID"},
  52. "C": {"姓名", "RealName"},
  53. "D": {"手机号", "Mobile"},
  54. "E": {"退款金额", "Amount"},
  55. "F": {"退回账号", "PaymentAccount"},
  56. "G": {"退款状态", "PaymentStatus"},
  57. "H": {"完成退款时间", "DealTime"},
  58. "I": {"创建时间", "CreatedTime"},
  59. }
  60. ProductOrderStatus = map[models.OrderStatus]string{
  61. "pending": "待支付",
  62. "processing": "支付中",
  63. "paid": "已支付",
  64. "closed": "已关闭",
  65. "refund": "售后",
  66. }
  67. TradeOrderStatus = map[models.PaymentStatus]string{
  68. "pending": "待支付",
  69. "failed": "支付失败",
  70. "done": "支付成功",
  71. }
  72. RefundOrderStatus = map[models.PaymentStatus]string{
  73. "pending": "退款中",
  74. "failed": "退款失败",
  75. "done": "退款成功",
  76. }
  77. RefundStatusMap = map[models.RefundStatus]string{
  78. "pending": "待退款",
  79. "processing": "退款中",
  80. "failed": "退款失败",
  81. "success": "退款成功",
  82. "canceled": "已取消",
  83. }
  84. ProductTypeMap = map[models.MerchantProductType]string{
  85. "report": "报告",
  86. "video": "视频",
  87. "audio": "音频",
  88. "package": "套餐",
  89. }
  90. PaymentWayMap = map[models.PaymentWay]string{
  91. "wechat": "微信",
  92. "alipay": "支付宝",
  93. }
  94. )
  95. type OrderController struct {
  96. BaseAuthController
  97. }
  98. // ProductOrderList
  99. // @Title 商品订单列表
  100. // @Description 商品订单列表
  101. // @Param PageSize query int true "每页数据条数"
  102. // @Param CurrentIndex query int true "当前页页码,从1开始"
  103. // @Param ClassifyIds query string true "二级分类id,可多选用英文,隔开"
  104. // @Param KeyWord query string true "报告标题/创建人"
  105. // @Param SortType query string true "排序方式"
  106. // @Success 200 {object} models.ReportAuthorResp
  107. // @router /productOrderList [get]
  108. func (this *OrderController) ProductOrderList() {
  109. br := new(models.BaseResponse).Init()
  110. defer func() {
  111. this.Data["json"] = br
  112. this.ServeJSON()
  113. }()
  114. pageSize, _ := this.GetInt("PageSize")
  115. currentIndex, _ := this.GetInt("CurrentIndex")
  116. sortType := this.GetString("SortType")
  117. KeyWord := this.GetString("KeyWord")
  118. TemplateUserId, _ := this.GetInt("TemplateUserId", 0)
  119. PaymentDate := this.GetString("PaymentDate")
  120. PaymentWay := this.GetString("PaymentWay")
  121. CreatedDate := this.GetString("CreatedDate")
  122. ProductType := this.GetString("ProductType")
  123. RefundStatus := this.GetString("RefundStatus")
  124. OrderStatus := this.GetString("OrderStatus")
  125. var condition string
  126. if pageSize <= 0 {
  127. pageSize = utils.PageSize20
  128. }
  129. if currentIndex <= 0 {
  130. currentIndex = 1
  131. }
  132. if KeyWord != "" {
  133. condition += " AND (product_name like '%" + KeyWord + "%' or real_name like '%" + KeyWord + "%'or order_id like '%" + KeyWord + "%' or mobile like '%" + KeyWord + "%')"
  134. }
  135. sortCondition := " ORDER BY created_time "
  136. if sortType == "" {
  137. sortType = "DESC"
  138. }
  139. if CreatedDate != "" {
  140. condition += " AND Date(created_time) = '" + CreatedDate + "'"
  141. }
  142. if PaymentDate != "" {
  143. condition += " AND Date(payment_time) = '" + PaymentDate + "'"
  144. }
  145. if PaymentWay != "" {
  146. condition += " AND payment_way='" + PaymentWay + "'"
  147. }
  148. if TemplateUserId > 0 {
  149. condition += fmt.Sprintf(" AND template_user_id=%d", TemplateUserId)
  150. }
  151. if OrderStatus != "" {
  152. switch OrderStatus {
  153. case "pending":
  154. condition += " AND status='pending'"
  155. case "paid":
  156. condition += " AND status='paid'"
  157. case "closed":
  158. condition += " AND status='closed'"
  159. case "refund":
  160. condition += " AND status='refund'"
  161. if RefundStatus != "" {
  162. switch RefundStatus {
  163. case "pending":
  164. condition += " AND refund_status='pending'"
  165. case "processing":
  166. condition += " AND refund_status='processing'"
  167. case "failed":
  168. condition += " AND refund_status='failed'"
  169. case "success":
  170. condition += " AND refund_status='success'"
  171. }
  172. }
  173. default:
  174. br.Msg = "无效的订单状态"
  175. br.ErrMsg = "无效的订单状态:" + OrderStatus
  176. return
  177. }
  178. }
  179. if ProductType != "" {
  180. switch ProductType {
  181. case "report":
  182. condition += " AND product_type='" + string(models.ProductReport) + "'"
  183. case "audio":
  184. condition += " AND product_type='" + string(models.ProductAudio) + "'"
  185. case "video":
  186. condition += " AND product_type='" + string(models.ProductVideo) + "'"
  187. case "package":
  188. condition += " AND product_type='" + string(models.ProductPackage) + "'"
  189. default:
  190. br.Msg = "无效的产品类型"
  191. br.ErrMsg = "无效的产品类型:" + ProductType
  192. return
  193. }
  194. }
  195. sortCondition = sortCondition + sortType
  196. total, err := models.GetProductOrderCountByCondition(condition)
  197. if err != nil {
  198. br.Msg = "获取商品列表失败"
  199. br.ErrMsg = "获取商品列表失败,Err:" + err.Error()
  200. return
  201. }
  202. startSize := utils.StartIndex(currentIndex, pageSize)
  203. List, err := models.GetProductOrderByCondition(condition, sortCondition, startSize, pageSize)
  204. if err != nil {
  205. br.Msg = "获取商品列表失败"
  206. br.ErrMsg = "获取商品列表失败,Err:" + err.Error()
  207. return
  208. }
  209. var ListView []*models.ProductOrderView
  210. for _, orderItem := range List {
  211. view := &models.ProductOrderView{
  212. OrderID: orderItem.OrderId,
  213. RealName: orderItem.RealName,
  214. Mobile: fmt.Sprintf("+%s %s", orderItem.AreaCode, orderItem.Mobile),
  215. ProductType: ProductTypeMap[orderItem.ProductType],
  216. ProductName: orderItem.ProductName,
  217. TotalAmount: fmt.Sprintf("¥%s", orderItem.TotalAmount),
  218. TradeNO: orderItem.TradeNo,
  219. RefundAmount: orderItem.RefundAmount,
  220. RefundTradeId: orderItem.RefundTradeId,
  221. PaymentWay: PaymentWayMap[orderItem.PaymentWay],
  222. Status: ProductOrderStatus[orderItem.Status],
  223. RefundStatus: RefundStatusMap[orderItem.RefundStatus],
  224. Remark: orderItem.Remark,
  225. ValidDuration: orderItem.ValidDuration,
  226. CreatedTime: orderItem.CreatedTime.Format(time.DateTime),
  227. }
  228. if orderItem.TradeNo != "" {
  229. view.PaymentTime = orderItem.PaymentTime.Format(time.DateTime)
  230. tradeOrder, tradeErr := models.GetTradeOrderByNo(orderItem.TradeNo)
  231. if tradeErr != nil {
  232. utils.FileLog.Error("获取支付订单失败,支付订单号:" + orderItem.TradeNo + ",err:" + tradeErr.Error())
  233. } else {
  234. view.PaymentAmount = fmt.Sprintf("¥%s", tradeOrder.Amount)
  235. }
  236. }
  237. if orderItem.Status == models.OrderStatusRefund && orderItem.RefundStatus == models.RefundStatusSuccess {
  238. view.RefundFinishTime = orderItem.RefundFinishTime.Format(time.DateTime)
  239. }
  240. ListView = append(ListView, view)
  241. }
  242. page := paging.GetPaging(currentIndex, pageSize, total)
  243. resp := new(response.ProductOrderListResp)
  244. resp.List = ListView
  245. resp.Paging = page
  246. br.Ret = 200
  247. br.Success = true
  248. br.Data = resp
  249. br.Msg = "获取成功"
  250. }
  251. // TradeOrderList
  252. // @Title 支付订单列表
  253. // @Description 支付订单列表
  254. // @Param PageSize query int true "每页数据条数"
  255. // @Param CurrentIndex query int true "当前页页码,从1开始"
  256. // @Param ClassifyIds query string true "二级分类id,可多选用英文,隔开"
  257. // @Param KeyWord query string true "报告标题/创建人"
  258. // @Param SortType query string true "排序方式"
  259. // @Success 200 {object} models.ReportAuthorResp
  260. // @router /tradeOrderList [get]
  261. func (this *OrderController) TradeOrderList() {
  262. br := new(models.BaseResponse).Init()
  263. defer func() {
  264. this.Data["json"] = br
  265. this.ServeJSON()
  266. }()
  267. pageSize, _ := this.GetInt("PageSize")
  268. currentIndex, _ := this.GetInt("CurrentIndex")
  269. sortType := this.GetString("SortType")
  270. KeyWord := this.GetString("KeyWord")
  271. DealDate := this.GetString("DealDate")
  272. PaymentWay := this.GetString("PaymentWay")
  273. CreatedDate := this.GetString("CreatedDate")
  274. OrderStatus := this.GetString("OrderStatus")
  275. IsRefund, _ := this.GetBool("IsRefund", false)
  276. var condition string
  277. if pageSize <= 0 {
  278. pageSize = utils.PageSize20
  279. }
  280. if currentIndex <= 0 {
  281. currentIndex = 1
  282. }
  283. if IsRefund {
  284. condition += " AND payment_type ='" + string(models.PaymentTypeRefund) + "'"
  285. } else {
  286. condition += " AND payment_type ='" + string(models.PaymentTypePay) + "'"
  287. }
  288. if KeyWord != "" {
  289. condition += " AND (product_name like '%" + KeyWord + "%' or real_name like '%" + KeyWord + "%' or product_order_id like '%" + KeyWord + "%' or mobile like '%" + KeyWord + "%')"
  290. }
  291. sortCondition := " ORDER BY created_time "
  292. if sortType == "" {
  293. sortType = "DESC"
  294. }
  295. if CreatedDate != "" {
  296. condition += " AND Date(created_time) = '" + CreatedDate + "'"
  297. }
  298. if DealDate != "" {
  299. condition += " AND Date(deal_time) = '" + DealDate + "'"
  300. }
  301. if PaymentWay != "" {
  302. condition += " AND payment_way='" + PaymentWay + "'"
  303. }
  304. if OrderStatus != "" {
  305. switch OrderStatus {
  306. case "pending":
  307. condition += " AND payment_status='pending'"
  308. case "done":
  309. condition += " AND payment_status='done'"
  310. case "failed":
  311. condition += " AND payment_status='failed'"
  312. default:
  313. br.Msg = "无效的支付订单状态"
  314. br.ErrMsg = "无效的支付订单状态:" + OrderStatus
  315. return
  316. }
  317. }
  318. sortCondition = sortCondition + sortType
  319. total, err := models.GetTradeOrderCountByCondition(condition)
  320. if err != nil {
  321. br.Msg = "获取支付明细列表失败"
  322. br.ErrMsg = "获取支付明细列表失败,Err:" + err.Error()
  323. return
  324. }
  325. startSize := utils.StartIndex(currentIndex, pageSize)
  326. List, err := models.GetTradeOrderByCondition(condition, sortCondition, startSize, pageSize)
  327. if err != nil {
  328. br.Msg = "获取支付明细列表失败"
  329. br.ErrMsg = "获取支付明细列表失败,Err:" + err.Error()
  330. return
  331. }
  332. var ListView []*models.TradeOrderView
  333. for i := 0; i < len(List); i++ {
  334. order := List[i]
  335. view := &models.TradeOrderView{
  336. RealName: order.RealName,
  337. Mobile: fmt.Sprintf("+%s %s", order.AreaCode, order.Mobile),
  338. ProductName: order.ProductName,
  339. Amount: fmt.Sprintf("¥%s", order.Amount),
  340. TransactionID: order.TransactionId,
  341. ProductOrderID: order.ProductOrderId,
  342. PaymentWay: PaymentWayMap[order.PaymentWay],
  343. PaymentAccount: order.PaymentAccount,
  344. MerchantID: order.MerchantId,
  345. CreatedTime: order.CreatedTime.Format(time.DateTime),
  346. }
  347. if order.PaymentStatus == models.PaymentStatusDone {
  348. view.DealTime = order.DealTime.Format(time.DateTime)
  349. }
  350. if IsRefund {
  351. view.PaymentStatus = RefundOrderStatus[order.PaymentStatus]
  352. } else {
  353. view.PaymentStatus = TradeOrderStatus[order.PaymentStatus]
  354. }
  355. ListView = append(ListView, view)
  356. }
  357. page := paging.GetPaging(currentIndex, pageSize, total)
  358. resp := new(response.TradeOrderListResp)
  359. resp.List = ListView
  360. resp.Paging = page
  361. br.Ret = 200
  362. br.Success = true
  363. br.Data = resp
  364. br.Msg = "获取成功"
  365. }
  366. // ExportProductOrder
  367. // @Title 临时用户列表
  368. // @Description 临时用户列表
  369. // @Param PageSize query int true "每页数据条数"
  370. // @Param CurrentIndex query int true "当前页页码,从1开始"
  371. // @Param Keyword query string false "手机号"
  372. // @Param SortParam query string false "排序字段参数,用来排序的字段, 枚举值:0:注册时间,1:阅读数,2:最近一次阅读时间"
  373. // @Param SortType query string true "如何排序,是正序还是倒序,0:倒序,1:正序"
  374. // @Success 200 {object} response.TemplateUserListResp
  375. // @router /productOrder/export [get]
  376. func (this *OrderController) ExportProductOrder() {
  377. br := new(models.BaseResponse).Init()
  378. defer func() {
  379. this.Data["json"] = br
  380. this.ServeJSON()
  381. }()
  382. pageSize, _ := this.GetInt("PageSize")
  383. currentIndex, _ := this.GetInt("CurrentIndex")
  384. sortType := this.GetString("SortType")
  385. KeyWord := this.GetString("KeyWord")
  386. PaymentDate := this.GetString("PaymentDate")
  387. PaymentWay := this.GetString("PaymentWay")
  388. CreatedDate := this.GetString("CreatedDate")
  389. ProductType := this.GetString("ProductType")
  390. RefundStatus := this.GetString("RefundStatus")
  391. OrderStatus := this.GetString("OrderStatus")
  392. var condition string
  393. if pageSize <= 0 {
  394. pageSize = utils.PageSize20
  395. }
  396. if currentIndex <= 0 {
  397. currentIndex = 1
  398. }
  399. if KeyWord != "" {
  400. condition += " AND (product_name like '%" + KeyWord + "%' or real_name like '%" + KeyWord + "%' order_id like '%" + KeyWord + "%' or mobile like '%" + KeyWord + "%')"
  401. }
  402. sortCondition := " ORDER BY created_time "
  403. if sortType == "" {
  404. sortType = "DESC"
  405. }
  406. if CreatedDate != "" {
  407. condition += " AND Date(created_time) = '" + CreatedDate + "'"
  408. }
  409. if PaymentDate != "" {
  410. condition += " AND Date(payment_time) = '" + PaymentDate + "'"
  411. }
  412. if PaymentWay != "" {
  413. condition += " AND payment_way='" + PaymentWay + "'"
  414. }
  415. if OrderStatus != "" {
  416. switch OrderStatus {
  417. case "pending":
  418. condition += " AND status='pending'"
  419. case "paid":
  420. condition += " AND status='paid'"
  421. case "closed":
  422. condition += " AND status='closed'"
  423. case "refund":
  424. condition += " AND status='refund'"
  425. if RefundStatus != "" {
  426. switch RefundStatus {
  427. case "pending":
  428. condition += " AND refund_status='pending'"
  429. case "processing":
  430. condition += " AND refund_status='processing'"
  431. case "failed":
  432. condition += " AND refund_status='failed'"
  433. case "success":
  434. condition += " AND refund_status='success'"
  435. }
  436. }
  437. default:
  438. br.Msg = "无效的订单状态"
  439. br.ErrMsg = "无效的订单状态:" + OrderStatus
  440. return
  441. }
  442. }
  443. if ProductType != "" {
  444. switch ProductType {
  445. case "report":
  446. condition += " AND product_type='" + string(models.ProductReport) + "'"
  447. case "audio":
  448. condition += " AND product_type='" + string(models.ProductAudio) + "'"
  449. case "video":
  450. condition += " AND product_type='" + string(models.ProductVideo) + "'"
  451. case "package":
  452. condition += " AND product_type='" + string(models.ProductPackage) + "'"
  453. default:
  454. br.Msg = "无效的产品类型"
  455. br.ErrMsg = "无效的产品类型:" + ProductType
  456. return
  457. }
  458. }
  459. sortCondition = sortCondition + sortType
  460. List, err := models.GetProductOrderListByCondition(condition, sortCondition)
  461. if err != nil {
  462. br.Msg = "导出商品订单失败"
  463. br.ErrMsg = "导出商品订单失败,Err:" + err.Error()
  464. return
  465. }
  466. var ListView []models.ProductOrderView
  467. for _, orderItem := range List {
  468. view := models.ProductOrderView{
  469. OrderID: orderItem.OrderId,
  470. RealName: orderItem.RealName,
  471. Mobile: fmt.Sprintf("+%s %s", orderItem.AreaCode, orderItem.Mobile),
  472. ProductType: ProductTypeMap[orderItem.ProductType],
  473. ProductName: orderItem.ProductName,
  474. TotalAmount: fmt.Sprintf("¥%s", orderItem.TotalAmount),
  475. TradeNO: orderItem.TradeNo,
  476. RefundAmount: orderItem.RefundAmount,
  477. RefundTradeId: orderItem.RefundTradeId,
  478. PaymentWay: PaymentWayMap[orderItem.PaymentWay],
  479. PaymentTime: orderItem.PaymentTime.Format(time.DateTime),
  480. Status: ProductOrderStatus[orderItem.Status],
  481. RefundStatus: RefundStatusMap[orderItem.RefundStatus],
  482. Remark: orderItem.Remark,
  483. CreatedTime: orderItem.CreatedTime.Format(time.DateTime),
  484. ValidDuration: orderItem.ValidDuration,
  485. }
  486. if orderItem.TradeNo != "" {
  487. view.PaymentTime = orderItem.PaymentTime.Format(time.DateTime)
  488. tradeOrder, tradeErr := models.GetTradeOrderByNo(orderItem.TradeNo)
  489. if tradeErr != nil {
  490. utils.FileLog.Error("获取支付订单失败,支付订单号:" + orderItem.TradeNo + ",err:" + tradeErr.Error())
  491. } else {
  492. view.PaymentAmount = fmt.Sprintf("¥%s", tradeOrder.Amount)
  493. }
  494. }
  495. //if orderItem.Status == models.OrderStatusPaid {
  496. // access, accessErr := models.GetAccess(orderItem.ProductId, orderItem.TemplateUserId)
  497. // if accessErr != nil {
  498. // utils.FileLog.Error("获取用户订阅记录失败,templateUserId:" + string(rune(orderItem.TemplateUserId)) + "productId:" + string(rune(orderItem.ProductId)) + ",err:" + accessErr.Error())
  499. // } else {
  500. // if access.ProductType == models.ProductPackage {
  501. // view.ValidDuration = fmt.Sprintf("%s~%s", access.BeginDate.Format(time.DateOnly), access.EndDate.Format(time.DateOnly))
  502. // } else {
  503. // view.ValidDuration = "永久有效"
  504. // }
  505. // }
  506. //}
  507. if orderItem.Status == models.OrderStatusRefund && orderItem.RefundStatus == models.RefundStatusSuccess {
  508. view.RefundFinishTime = orderItem.RefundFinishTime.Format(time.DateTime)
  509. }
  510. ListView = append(ListView, view)
  511. }
  512. year, month, day := time.Now().Date()
  513. yearStr := strconv.Itoa(year)[2:]
  514. fileName := fmt.Sprintf("商品订单%s.%d.%d.xlsx", yearStr, month, day)
  515. file, err := utils.ExportExcel("商品订单", productCols, ListView)
  516. _ = this.downloadExcelFile(file, fileName)
  517. br.Ret = 200
  518. br.Success = true
  519. br.Msg = "下载成功"
  520. }
  521. // ExportTradeOrder
  522. // @Title 临时用户列表
  523. // @Description 临时用户列表
  524. // @Param PageSize query int true "每页数据条数"
  525. // @Param CurrentIndex query int true "当前页页码,从1开始"
  526. // @Param Keyword query string false "手机号"
  527. // @Param SortParam query string false "排序字段参数,用来排序的字段, 枚举值:0:注册时间,1:阅读数,2:最近一次阅读时间"
  528. // @Param SortType query string true "如何排序,是正序还是倒序,0:倒序,1:正序"
  529. // @Success 200 {object} response.TemplateUserListResp
  530. // @router /tradeOrder/export [get]
  531. func (this *OrderController) ExportTradeOrder() {
  532. br := new(models.BaseResponse).Init()
  533. defer func() {
  534. this.Data["json"] = br
  535. this.ServeJSON()
  536. }()
  537. pageSize, _ := this.GetInt("PageSize")
  538. currentIndex, _ := this.GetInt("CurrentIndex")
  539. sortType := this.GetString("SortType")
  540. KeyWord := this.GetString("KeyWord")
  541. DealDate := this.GetString("DealDate")
  542. PaymentWay := this.GetString("PaymentWay")
  543. CreatedDate := this.GetString("CreatedDate")
  544. OrderStatus := this.GetString("OrderStatus")
  545. IsRefund, _ := this.GetBool("IsRefund", false)
  546. var condition string
  547. if pageSize <= 0 {
  548. pageSize = utils.PageSize20
  549. }
  550. if currentIndex <= 0 {
  551. currentIndex = 1
  552. }
  553. if IsRefund {
  554. condition += " AND payment_type ='" + string(models.PaymentTypeRefund) + "'"
  555. } else {
  556. condition += " AND payment_type ='" + string(models.PaymentTypePay) + "'"
  557. }
  558. if KeyWord != "" {
  559. condition += " AND (product_name like '%" + KeyWord + "%' or real_name like '%" + KeyWord + "%' or product_order_id like '%" + KeyWord + "%' or mobile like '%" + KeyWord + "%')"
  560. }
  561. sortCondition := " ORDER BY created_time "
  562. if sortType == "" {
  563. sortType = "DESC"
  564. }
  565. if CreatedDate != "" {
  566. condition += " AND Date(created_time) = '" + CreatedDate + "'"
  567. }
  568. if DealDate != "" {
  569. condition += " AND Date(deal_time) = '" + DealDate + "'"
  570. }
  571. if PaymentWay != "" {
  572. condition += " AND payment_way='" + PaymentWay + "'"
  573. }
  574. if OrderStatus != "" {
  575. switch OrderStatus {
  576. case "pending":
  577. condition += " AND payment_status='pending'"
  578. case "done":
  579. condition += " AND payment_status='done'"
  580. case "failed":
  581. condition += " AND payment_status='failed'"
  582. default:
  583. br.Msg = "无效的支付订单状态"
  584. br.ErrMsg = "无效的支付订单状态:" + OrderStatus
  585. return
  586. }
  587. }
  588. sortCondition = sortCondition + sortType
  589. List, err := models.GetTradeOrderListByCondition(condition, sortCondition)
  590. if err != nil {
  591. br.Msg = "获取支付明细列表失败"
  592. br.ErrMsg = "获取支付明细列表失败,Err:" + err.Error()
  593. return
  594. }
  595. var ListView []models.TradeOrderView
  596. for i := 0; i < len(List); i++ {
  597. order := List[i]
  598. view := models.TradeOrderView{
  599. RealName: order.RealName,
  600. Mobile: fmt.Sprintf("+%s %s", order.AreaCode, order.Mobile),
  601. ProductName: order.ProductName,
  602. Amount: fmt.Sprintf("¥%s", order.Amount),
  603. TransactionID: order.TransactionId,
  604. ProductOrderID: order.ProductOrderId,
  605. PaymentWay: PaymentWayMap[order.PaymentWay],
  606. PaymentAccount: order.PaymentAccount,
  607. MerchantID: order.MerchantId,
  608. CreatedTime: order.CreatedTime.Format(time.DateTime),
  609. }
  610. if order.PaymentStatus == models.PaymentStatusDone {
  611. view.DealTime = order.DealTime.Format(time.DateTime)
  612. }
  613. if IsRefund {
  614. view.PaymentStatus = RefundOrderStatus[order.PaymentStatus]
  615. } else {
  616. view.PaymentStatus = TradeOrderStatus[order.PaymentStatus]
  617. }
  618. ListView = append(ListView, view)
  619. }
  620. year, month, day := time.Now().Date()
  621. yearStr := strconv.Itoa(year)[2:]
  622. if IsRefund {
  623. fileName := fmt.Sprintf("退款明细%s.%d.%d.xlsx", yearStr, month, day)
  624. file, _ := utils.ExportExcel("退款明细", refundCols, ListView)
  625. _ = this.downloadExcelFile(file, fileName)
  626. } else {
  627. fileName := fmt.Sprintf("支付明细%s.%d.%d.xlsx", yearStr, month, day)
  628. file, _ := utils.ExportExcel("支付明细", tradeCols, ListView)
  629. _ = this.downloadExcelFile(file, fileName)
  630. }
  631. br.Ret = 200
  632. br.Success = true
  633. br.Msg = "下载成功"
  634. }
  635. // encodeChineseFilename 将中文文件名编码为 ISO-8859-1
  636. func (this *OrderController) downloadExcelFile(file *excelize.File, filename string) (err error) {
  637. // 对文件名进行 ISO-8859-1 编码
  638. fn := url.QueryEscape(filename)
  639. if filename == fn {
  640. fn = "filename=" + fn
  641. } else {
  642. fn = "filename=" + filename + "; filename*=utf-8''" + fn
  643. }
  644. this.Ctx.ResponseWriter.Header().Set("Access-Control-Expose-Headers", "Content-Disposition")
  645. this.Ctx.ResponseWriter.Header().Set("Content-Disposition", "attachment; "+fn)
  646. this.Ctx.ResponseWriter.Header().Set("Content-Description", "File Transfer")
  647. this.Ctx.ResponseWriter.Header().Set("Content-Type", "application/octet-stream")
  648. this.Ctx.ResponseWriter.Header().Set("Content-Transfer-Encoding", "binary")
  649. this.Ctx.ResponseWriter.Header().Set("Expires", "0")
  650. this.Ctx.ResponseWriter.Header().Set("Cache-Control", "must-revalidate")
  651. this.Ctx.ResponseWriter.Header().Set("Pragma", "public")
  652. this.Ctx.ResponseWriter.Header().Set("File-Name", filename)
  653. // 写入文件
  654. if err = file.Write(this.Ctx.ResponseWriter); err != nil {
  655. utils.FileLog.Error("导出excel文件失败:", err)
  656. http.Error(this.Ctx.ResponseWriter, "导出excel文件失败", http.StatusInternalServerError)
  657. }
  658. return
  659. }
  660. // Refund
  661. // @Title 退款
  662. // @Description 退款
  663. // @Param PageSize query int true "每页数据条数"
  664. // @Param CurrentIndex query int true "当前页页码,从1开始"
  665. // @Param ClassifyIds query string true "二级分类id,可多选用英文,隔开"
  666. // @Param KeyWord query string true "报告标题/创建人"
  667. // @Param SortType query string true "排序方式"
  668. // @Success 200 {object} models.ReportAuthorResp
  669. // @router /refund [post]
  670. func (this *OrderController) Refund() {
  671. br := new(models.BaseResponse).Init()
  672. defer func() {
  673. this.Data["json"] = br
  674. this.ServeJSON()
  675. }()
  676. var req request.RefundReq
  677. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  678. br.Msg = "参数解析失败"
  679. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  680. return
  681. }
  682. if req.ProductOrderNo == "" {
  683. br.Msg = "商品订单号不能为空"
  684. br.ErrMsg = "商品订单号不能为空"
  685. return
  686. }
  687. productOrder, err := models.GetProductOrderByID(req.ProductOrderNo)
  688. if err != nil {
  689. br.Msg = "获取商品订单失败"
  690. br.ErrMsg = "获取商品订单失败,err:" + err.Error()
  691. return
  692. }
  693. if productOrder.Status == models.OrderStatusPending {
  694. br.Msg = "退款失败,"
  695. br.ErrMsg = "退款失败,退款状态异常,当前订单已关闭"
  696. return
  697. }
  698. if productOrder.Status == models.OrderStatusRefund && productOrder.RefundStatus != models.RefundStatusFailed {
  699. br.Msg = "退款失败,"
  700. br.ErrMsg = "退款失败,当前订单退款处理中"
  701. return
  702. }
  703. tradeOrder, err := models.GetTradeOrderByNo(productOrder.TradeNo)
  704. if err != nil {
  705. br.Msg = "退款失败,获取原订单失败"
  706. br.ErrMsg = "退款失败,获取原订单失败,err:" + err.Error()
  707. return
  708. }
  709. if tradeOrder.PaymentType != models.PaymentTypePay {
  710. br.Msg = "退款失败,原订单非支付订单"
  711. br.ErrMsg = "退款失败,原订单非支付订单"
  712. return
  713. }
  714. if tradeOrder.PaymentStatus != models.PaymentStatusDone {
  715. br.Msg = "退款失败,原订单未完成支付"
  716. br.ErrMsg = "退款失败,原订单未完成支付"
  717. return
  718. }
  719. uuidStr := strings.ReplaceAll(uuid.New().String(), "-", "")
  720. key := fmt.Sprintf("refund_lock_%s", productOrder.OrderId)
  721. defer func() {
  722. utils.ReleaseLock(key, uuidStr)
  723. }()
  724. if utils.AcquireLock(key, 10, uuidStr) {
  725. aa := GenerateProductOrderNo()
  726. refundOrder := &models.TradeOrder{
  727. TransactionId: aa,
  728. OrgTransactionId: productOrder.TradeNo,
  729. ProductOrderId: productOrder.OrderId,
  730. PaymentAccount: tradeOrder.PaymentAccount,
  731. PaymentWay: tradeOrder.PaymentWay,
  732. ProductName: productOrder.ProductName,
  733. Mobile: productOrder.Mobile,
  734. AreaCode: productOrder.AreaCode,
  735. RealName: productOrder.RealName,
  736. Amount: tradeOrder.Amount,
  737. Currency: tradeOrder.Currency,
  738. UserId: tradeOrder.UserId,
  739. TemplateUserId: tradeOrder.TemplateUserId,
  740. PaymentType: models.PaymentTypeRefund,
  741. PaymentStatus: models.PaymentStatusPending,
  742. CreatedTime: time.Now(),
  743. }
  744. productOrder.RefundStatus = models.RefundStatusProcessing
  745. productOrder.Status = models.OrderStatusRefund
  746. productOrder.RefundTradeId = refundOrder.TransactionId
  747. productOrder.Remark = req.Remark
  748. productOrder.RefundAmount = tradeOrder.Amount
  749. err = refundOrder.Refund(productOrder)
  750. if err != nil {
  751. br.Msg = "退款失败"
  752. br.ErrMsg = "退款失败,,Err:" + err.Error()
  753. return
  754. }
  755. refundflow := models.RefundDealFlow{
  756. ProductOrderNo: productOrder.OrderId,
  757. RefundOrderNo: refundOrder.TransactionId,
  758. OperatorUserId: this.SysUser.SysUserId,
  759. CreatedTime: time.Now(),
  760. }
  761. //增加一条退款流水
  762. _ = refundflow.Insert()
  763. br.Ret = 200
  764. br.Success = true
  765. br.Msg = "退款处理成功"
  766. } else {
  767. br.Msg = "退款失败,当前订单正在退款中"
  768. br.ErrMsg = "退款失败,当前订单正在退款中"
  769. return
  770. }
  771. }
  772. func GenerateProductOrderNo() string {
  773. timestamp := time.Now().UnixNano() / 1000000 // 毫秒级时间戳
  774. // 生成随机数
  775. rand.New(rand.NewSource(time.Now().UnixNano()))
  776. randomPart := rand.Intn(999999)
  777. // 格式化订单号
  778. orderNumber := fmt.Sprintf("R%d%06d", timestamp, randomPart)
  779. return orderNumber
  780. }
  781. // RefundDetail
  782. // @Title 退款详情
  783. // @Description 退款详情
  784. // @Param PageSize query int true "每页数据条数"
  785. // @Param CurrentIndex query int true "当前页页码,从1开始"
  786. // @Param ClassifyIds query string true "二级分类id,可多选用英文,隔开"
  787. // @Param KeyWord query string true "报告标题/创建人"
  788. // @Param SortType query string true "排序方式"
  789. // @Success 200 {object} models.ReportAuthorResp
  790. // @router /refundDetail [get]
  791. func (this *OrderController) RefundDetail() {
  792. br := new(models.BaseResponse).Init()
  793. defer func() {
  794. this.Data["json"] = br
  795. this.ServeJSON()
  796. }()
  797. ProductOrderNo := this.GetString("ProductOrderNo")
  798. if ProductOrderNo == "" {
  799. br.Msg = "商品订单号不能为空"
  800. br.ErrMsg = "商品订单号不能为空"
  801. return
  802. }
  803. productOrder, err := models.GetProductOrderByID(ProductOrderNo)
  804. if err != nil {
  805. br.Msg = "获取商品订单失败"
  806. br.ErrMsg = "获取商品订单失败,err:" + err.Error()
  807. return
  808. }
  809. if productOrder.Status != models.OrderStatusRefund && productOrder.RefundStatus != models.RefundStatusSuccess {
  810. br.Msg = "当前订单未完成退款"
  811. br.ErrMsg = "当前订单未完成退款"
  812. return
  813. }
  814. refundOrder, err := models.GetTradeOrderByNo(productOrder.RefundTradeId)
  815. if err != nil {
  816. br.Msg = "获取退款订单失败"
  817. br.ErrMsg = "获取退款订单失败,err:" + err.Error()
  818. return
  819. }
  820. refundResp := response.RefundResp{
  821. Account: refundOrder.PaymentAccount,
  822. RealName: productOrder.RealName,
  823. RefundAmount: productOrder.RefundAmount,
  824. RefundFinishTime: productOrder.RefundFinishTime.Format(time.DateTime),
  825. Remark: productOrder.Remark,
  826. }
  827. br.Ret = 200
  828. br.Success = true
  829. br.Data = refundResp
  830. br.Msg = "退款详情获取成功"
  831. return
  832. }