order.go 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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. PaymentTime: orderItem.PaymentTime.Format(time.DateTime),
  223. Status: ProductOrderStatus[orderItem.Status],
  224. RefundStatus: RefundStatusMap[orderItem.RefundStatus],
  225. Remark: orderItem.Remark,
  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.OrderStatusPaid {
  238. access, accessErr := models.GetAccess(orderItem.ProductId, orderItem.TemplateUserId)
  239. if accessErr != nil {
  240. utils.FileLog.Error("获取用户订阅记录失败,templateUserId:" + string(rune(orderItem.TemplateUserId)) + "productId:" + string(rune(orderItem.ProductId)) + ",err:" + accessErr.Error())
  241. } else {
  242. if access.ProductType == models.ProductPackage {
  243. view.ValidDuration = fmt.Sprintf("%s~%s", access.BeginDate.Format(time.DateOnly), access.EndDate.Format(time.DateOnly))
  244. } else {
  245. view.ValidDuration = "永久有效"
  246. }
  247. }
  248. }
  249. if orderItem.Status == models.OrderStatusRefund && orderItem.RefundStatus == models.RefundStatusSuccess {
  250. view.RefundFinishTime = orderItem.RefundFinishTime.Format(time.DateTime)
  251. }
  252. ListView = append(ListView, view)
  253. }
  254. page := paging.GetPaging(currentIndex, pageSize, total)
  255. resp := new(response.ProductOrderListResp)
  256. resp.List = ListView
  257. resp.Paging = page
  258. br.Ret = 200
  259. br.Success = true
  260. br.Data = resp
  261. br.Msg = "获取成功"
  262. }
  263. // TradeOrderList
  264. // @Title 支付订单列表
  265. // @Description 支付订单列表
  266. // @Param PageSize query int true "每页数据条数"
  267. // @Param CurrentIndex query int true "当前页页码,从1开始"
  268. // @Param ClassifyIds query string true "二级分类id,可多选用英文,隔开"
  269. // @Param KeyWord query string true "报告标题/创建人"
  270. // @Param SortType query string true "排序方式"
  271. // @Success 200 {object} models.ReportAuthorResp
  272. // @router /tradeOrderList [get]
  273. func (this *OrderController) TradeOrderList() {
  274. br := new(models.BaseResponse).Init()
  275. defer func() {
  276. this.Data["json"] = br
  277. this.ServeJSON()
  278. }()
  279. pageSize, _ := this.GetInt("PageSize")
  280. currentIndex, _ := this.GetInt("CurrentIndex")
  281. sortType := this.GetString("SortType")
  282. KeyWord := this.GetString("KeyWord")
  283. DealDate := this.GetString("DealDate")
  284. PaymentWay := this.GetString("PaymentWay")
  285. CreatedDate := this.GetString("CreatedDate")
  286. OrderStatus := this.GetString("OrderStatus")
  287. IsRefund, _ := this.GetBool("IsRefund", false)
  288. var condition string
  289. if pageSize <= 0 {
  290. pageSize = utils.PageSize20
  291. }
  292. if currentIndex <= 0 {
  293. currentIndex = 1
  294. }
  295. if IsRefund {
  296. condition += " AND payment_type ='" + string(models.PaymentTypeRefund) + "'"
  297. } else {
  298. condition += " AND payment_type ='" + string(models.PaymentTypePay) + "'"
  299. }
  300. if KeyWord != "" {
  301. condition += " AND (product_name like '%" + KeyWord + "%' or real_name like '%" + KeyWord + "%' or product_order_id like '%" + KeyWord + "%' or mobile like '%" + KeyWord + "%')"
  302. }
  303. sortCondition := " ORDER BY created_time "
  304. if sortType == "" {
  305. sortType = "DESC"
  306. }
  307. if CreatedDate != "" {
  308. condition += " AND Date(created_time) = '" + CreatedDate + "'"
  309. }
  310. if DealDate != "" {
  311. condition += " AND Date(deal_time) = '" + DealDate + "'"
  312. }
  313. if PaymentWay != "" {
  314. condition += " AND payment_way='" + PaymentWay + "'"
  315. }
  316. if OrderStatus != "" {
  317. switch OrderStatus {
  318. case "pending":
  319. condition += " AND payment_status='pending'"
  320. case "done":
  321. condition += " AND payment_status='done'"
  322. case "failed":
  323. condition += " AND payment_status='failed'"
  324. default:
  325. br.Msg = "无效的支付订单状态"
  326. br.ErrMsg = "无效的支付订单状态:" + OrderStatus
  327. return
  328. }
  329. }
  330. sortCondition = sortCondition + sortType
  331. total, err := models.GetTradeOrderCountByCondition(condition)
  332. if err != nil {
  333. br.Msg = "获取支付明细列表失败"
  334. br.ErrMsg = "获取支付明细列表失败,Err:" + err.Error()
  335. return
  336. }
  337. startSize := utils.StartIndex(currentIndex, pageSize)
  338. List, err := models.GetTradeOrderByCondition(condition, sortCondition, startSize, pageSize)
  339. if err != nil {
  340. br.Msg = "获取支付明细列表失败"
  341. br.ErrMsg = "获取支付明细列表失败,Err:" + err.Error()
  342. return
  343. }
  344. var ListView []*models.TradeOrderView
  345. for i := 0; i < len(List); i++ {
  346. order := List[i]
  347. view := &models.TradeOrderView{
  348. RealName: order.RealName,
  349. Mobile: fmt.Sprintf("+%s %s", order.AreaCode, order.Mobile),
  350. ProductName: order.ProductName,
  351. Amount: fmt.Sprintf("¥%s", order.Amount),
  352. TransactionID: order.TransactionId,
  353. ProductOrderID: order.ProductOrderId,
  354. PaymentWay: PaymentWayMap[order.PaymentWay],
  355. PaymentAccount: order.PaymentAccount,
  356. MerchantID: order.MerchantId,
  357. CreatedTime: order.CreatedTime.Format(time.DateTime),
  358. }
  359. if order.PaymentStatus == models.PaymentStatusDone {
  360. view.DealTime = order.DealTime.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. }
  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 /productOrder/export [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 _, orderItem := range List {
  480. view := models.ProductOrderView{
  481. OrderID: orderItem.OrderId,
  482. RealName: orderItem.RealName,
  483. Mobile: fmt.Sprintf("+%s %s", orderItem.AreaCode, orderItem.Mobile),
  484. ProductType: ProductTypeMap[orderItem.ProductType],
  485. ProductName: orderItem.ProductName,
  486. TotalAmount: fmt.Sprintf("¥%s", orderItem.TotalAmount),
  487. TradeNO: orderItem.TradeNo,
  488. RefundAmount: orderItem.RefundAmount,
  489. RefundTradeId: orderItem.RefundTradeId,
  490. PaymentWay: PaymentWayMap[orderItem.PaymentWay],
  491. PaymentTime: orderItem.PaymentTime.Format(time.DateTime),
  492. Status: ProductOrderStatus[orderItem.Status],
  493. RefundStatus: RefundStatusMap[orderItem.RefundStatus],
  494. Remark: orderItem.Remark,
  495. CreatedTime: orderItem.CreatedTime.Format(time.DateTime),
  496. }
  497. if orderItem.TradeNo != "" {
  498. view.PaymentTime = orderItem.PaymentTime.Format(time.DateTime)
  499. tradeOrder, tradeErr := models.GetTradeOrderByNo(orderItem.TradeNo)
  500. if tradeErr != nil {
  501. utils.FileLog.Error("获取支付订单失败,支付订单号:" + orderItem.TradeNo + ",err:" + tradeErr.Error())
  502. } else {
  503. view.PaymentAmount = fmt.Sprintf("¥%s", tradeOrder.Amount)
  504. }
  505. }
  506. if orderItem.Status == models.OrderStatusPaid {
  507. access, accessErr := models.GetAccess(orderItem.ProductId, orderItem.TemplateUserId)
  508. if accessErr != nil {
  509. utils.FileLog.Error("获取用户订阅记录失败,templateUserId:" + string(rune(orderItem.TemplateUserId)) + "productId:" + string(rune(orderItem.ProductId)) + ",err:" + accessErr.Error())
  510. } else {
  511. if access.ProductType == models.ProductPackage {
  512. view.ValidDuration = fmt.Sprintf("%s~%s", access.BeginDate.Format(time.DateOnly), access.EndDate.Format(time.DateOnly))
  513. } else {
  514. view.ValidDuration = "永久有效"
  515. }
  516. }
  517. }
  518. if orderItem.Status == models.OrderStatusRefund && orderItem.RefundStatus == models.RefundStatusSuccess {
  519. view.RefundFinishTime = orderItem.RefundFinishTime.Format(time.DateTime)
  520. }
  521. ListView = append(ListView, view)
  522. }
  523. year, month, day := time.Now().Date()
  524. yearStr := strconv.Itoa(year)[2:]
  525. fileName := fmt.Sprintf("商品订单%s.%d.%d.xlsx", yearStr, month, day)
  526. file, err := utils.ExportExcel("商品订单", productCols, ListView)
  527. _ = this.downloadExcelFile(file, fileName)
  528. br.Ret = 200
  529. br.Success = true
  530. br.Msg = "下载成功"
  531. }
  532. // ExportTradeOrder
  533. // @Title 临时用户列表
  534. // @Description 临时用户列表
  535. // @Param PageSize query int true "每页数据条数"
  536. // @Param CurrentIndex query int true "当前页页码,从1开始"
  537. // @Param Keyword query string false "手机号"
  538. // @Param SortParam query string false "排序字段参数,用来排序的字段, 枚举值:0:注册时间,1:阅读数,2:最近一次阅读时间"
  539. // @Param SortType query string true "如何排序,是正序还是倒序,0:倒序,1:正序"
  540. // @Success 200 {object} response.TemplateUserListResp
  541. // @router /tradeOrder/export [get]
  542. func (this *OrderController) ExportTradeOrder() {
  543. br := new(models.BaseResponse).Init()
  544. defer func() {
  545. this.Data["json"] = br
  546. this.ServeJSON()
  547. }()
  548. pageSize, _ := this.GetInt("PageSize")
  549. currentIndex, _ := this.GetInt("CurrentIndex")
  550. sortType := this.GetString("SortType")
  551. KeyWord := this.GetString("KeyWord")
  552. DealDate := this.GetString("DealDate")
  553. PaymentWay := this.GetString("PaymentWay")
  554. CreatedDate := this.GetString("CreatedDate")
  555. OrderStatus := this.GetString("OrderStatus")
  556. IsRefund, _ := this.GetBool("IsRefund", false)
  557. var condition string
  558. if pageSize <= 0 {
  559. pageSize = utils.PageSize20
  560. }
  561. if currentIndex <= 0 {
  562. currentIndex = 1
  563. }
  564. if IsRefund {
  565. condition += " AND payment_type ='" + string(models.PaymentTypeRefund) + "'"
  566. } else {
  567. condition += " AND payment_type ='" + string(models.PaymentTypePay) + "'"
  568. }
  569. if KeyWord != "" {
  570. condition += " AND (product_name like '%" + KeyWord + "%' or real_name like '%" + KeyWord + "%' or product_order_id like '%" + KeyWord + "%' or mobile like '%" + KeyWord + "%')"
  571. }
  572. sortCondition := " ORDER BY created_time "
  573. if sortType == "" {
  574. sortType = "DESC"
  575. }
  576. if CreatedDate != "" {
  577. condition += " AND Date(created_time) = '" + CreatedDate + "'"
  578. }
  579. if DealDate != "" {
  580. condition += " AND Date(deal_time) = '" + DealDate + "'"
  581. }
  582. if PaymentWay != "" {
  583. condition += " AND payment_way='" + PaymentWay + "'"
  584. }
  585. if OrderStatus != "" {
  586. switch OrderStatus {
  587. case "pending":
  588. condition += " AND payment_status='pending'"
  589. case "done":
  590. condition += " AND payment_status='done'"
  591. case "failed":
  592. condition += " AND payment_status='failed'"
  593. default:
  594. br.Msg = "无效的支付订单状态"
  595. br.ErrMsg = "无效的支付订单状态:" + OrderStatus
  596. return
  597. }
  598. }
  599. sortCondition = sortCondition + sortType
  600. List, err := models.GetTradeOrderListByCondition(condition, sortCondition)
  601. if err != nil {
  602. br.Msg = "获取支付明细列表失败"
  603. br.ErrMsg = "获取支付明细列表失败,Err:" + err.Error()
  604. return
  605. }
  606. var ListView []models.TradeOrderView
  607. for i := 0; i < len(List); i++ {
  608. order := List[i]
  609. view := models.TradeOrderView{
  610. RealName: order.RealName,
  611. Mobile: fmt.Sprintf("+%s %s", order.AreaCode, order.Mobile),
  612. ProductName: order.ProductName,
  613. Amount: fmt.Sprintf("¥%s", order.Amount),
  614. TransactionID: order.TransactionId,
  615. ProductOrderID: order.ProductOrderId,
  616. PaymentWay: PaymentWayMap[order.PaymentWay],
  617. PaymentAccount: order.PaymentAccount,
  618. MerchantID: order.MerchantId,
  619. CreatedTime: order.CreatedTime.Format(time.DateTime),
  620. }
  621. if order.PaymentStatus == models.PaymentStatusDone {
  622. view.DealTime = order.DealTime.Format(time.DateTime)
  623. }
  624. if IsRefund {
  625. view.PaymentStatus = RefundOrderStatus[order.PaymentStatus]
  626. } else {
  627. view.PaymentStatus = TradeOrderStatus[order.PaymentStatus]
  628. }
  629. ListView = append(ListView, view)
  630. }
  631. year, month, day := time.Now().Date()
  632. yearStr := strconv.Itoa(year)[2:]
  633. if IsRefund {
  634. fileName := fmt.Sprintf("退款明细%s.%d.%d.xlsx", yearStr, month, day)
  635. file, _ := utils.ExportExcel("退款明细", refundCols, ListView)
  636. _ = this.downloadExcelFile(file, fileName)
  637. } else {
  638. fileName := fmt.Sprintf("支付明细%s.%d.%d.xlsx", yearStr, month, day)
  639. file, _ := utils.ExportExcel("支付明细", tradeCols, ListView)
  640. _ = this.downloadExcelFile(file, fileName)
  641. }
  642. br.Ret = 200
  643. br.Success = true
  644. br.Msg = "下载成功"
  645. }
  646. // encodeChineseFilename 将中文文件名编码为 ISO-8859-1
  647. func (this *OrderController) downloadExcelFile(file *excelize.File, filename string) (err error) {
  648. // 对文件名进行 ISO-8859-1 编码
  649. fn := url.QueryEscape(filename)
  650. if filename == fn {
  651. fn = "filename=" + fn
  652. } else {
  653. fn = "filename=" + filename + "; filename*=utf-8''" + fn
  654. }
  655. this.Ctx.ResponseWriter.Header().Set("Access-Control-Expose-Headers", "Content-Disposition")
  656. this.Ctx.ResponseWriter.Header().Set("Content-Disposition", "attachment; "+fn)
  657. this.Ctx.ResponseWriter.Header().Set("Content-Description", "File Transfer")
  658. this.Ctx.ResponseWriter.Header().Set("Content-Type", "application/octet-stream")
  659. this.Ctx.ResponseWriter.Header().Set("Content-Transfer-Encoding", "binary")
  660. this.Ctx.ResponseWriter.Header().Set("Expires", "0")
  661. this.Ctx.ResponseWriter.Header().Set("Cache-Control", "must-revalidate")
  662. this.Ctx.ResponseWriter.Header().Set("Pragma", "public")
  663. this.Ctx.ResponseWriter.Header().Set("File-Name", filename)
  664. // 写入文件
  665. if err = file.Write(this.Ctx.ResponseWriter); err != nil {
  666. utils.FileLog.Error("导出excel文件失败:", err)
  667. http.Error(this.Ctx.ResponseWriter, "导出excel文件失败", http.StatusInternalServerError)
  668. }
  669. return
  670. }
  671. // Refund
  672. // @Title 退款
  673. // @Description 退款
  674. // @Param PageSize query int true "每页数据条数"
  675. // @Param CurrentIndex query int true "当前页页码,从1开始"
  676. // @Param ClassifyIds query string true "二级分类id,可多选用英文,隔开"
  677. // @Param KeyWord query string true "报告标题/创建人"
  678. // @Param SortType query string true "排序方式"
  679. // @Success 200 {object} models.ReportAuthorResp
  680. // @router /refund [post]
  681. func (this *OrderController) Refund() {
  682. br := new(models.BaseResponse).Init()
  683. defer func() {
  684. this.Data["json"] = br
  685. this.ServeJSON()
  686. }()
  687. var req request.RefundReq
  688. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  689. br.Msg = "参数解析失败"
  690. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  691. return
  692. }
  693. if req.ProductOrderNo == "" {
  694. br.Msg = "商品订单号不能为空"
  695. br.ErrMsg = "商品订单号不能为空"
  696. return
  697. }
  698. productOrder, err := models.GetProductOrderByID(req.ProductOrderNo)
  699. if err != nil {
  700. br.Msg = "获取商品订单失败"
  701. br.ErrMsg = "获取商品订单失败,err:" + err.Error()
  702. return
  703. }
  704. if productOrder.Status == models.OrderStatusPending {
  705. br.Msg = "退款失败,"
  706. br.ErrMsg = "退款失败,退款状态异常,当前订单已关闭"
  707. return
  708. }
  709. if productOrder.Status == models.OrderStatusRefund && productOrder.RefundStatus != models.RefundStatusFailed {
  710. br.Msg = "退款失败,"
  711. br.ErrMsg = "退款失败,当前订单退款处理中"
  712. return
  713. }
  714. tradeOrder, err := models.GetTradeOrderByNo(productOrder.TradeNo)
  715. if err != nil {
  716. br.Msg = "退款失败,获取原订单失败"
  717. br.ErrMsg = "退款失败,获取原订单失败,err:" + err.Error()
  718. return
  719. }
  720. if tradeOrder.PaymentType != models.PaymentTypePay {
  721. br.Msg = "退款失败,原订单非支付订单"
  722. br.ErrMsg = "退款失败,原订单非支付订单"
  723. return
  724. }
  725. if tradeOrder.PaymentStatus != models.PaymentStatusDone {
  726. br.Msg = "退款失败,原订单未完成支付"
  727. br.ErrMsg = "退款失败,原订单未完成支付"
  728. return
  729. }
  730. uuidStr := strings.ReplaceAll(uuid.New().String(), "-", "")
  731. key := fmt.Sprintf("refund_lock_%s", productOrder.OrderId)
  732. defer func() {
  733. utils.ReleaseLock(key, uuidStr)
  734. }()
  735. if utils.AcquireLock(key, 10, uuidStr) {
  736. aa := GenerateProductOrderNo()
  737. refundOrder := &models.TradeOrder{
  738. TransactionId: aa,
  739. OrgTransactionId: productOrder.TradeNo,
  740. ProductOrderId: productOrder.OrderId,
  741. PaymentAccount: tradeOrder.PaymentAccount,
  742. PaymentWay: tradeOrder.PaymentWay,
  743. ProductName: productOrder.ProductName,
  744. Mobile: productOrder.Mobile,
  745. AreaCode: productOrder.AreaCode,
  746. RealName: productOrder.RealName,
  747. Amount: tradeOrder.Amount,
  748. Currency: tradeOrder.Currency,
  749. UserId: tradeOrder.UserId,
  750. TemplateUserId: tradeOrder.TemplateUserId,
  751. PaymentType: models.PaymentTypeRefund,
  752. PaymentStatus: models.PaymentStatusPending,
  753. CreatedTime: time.Now(),
  754. }
  755. productOrder.RefundStatus = models.RefundStatusProcessing
  756. productOrder.Status = models.OrderStatusRefund
  757. productOrder.RefundTradeId = refundOrder.TransactionId
  758. productOrder.Remark = req.Remark
  759. productOrder.RefundAmount = tradeOrder.Amount
  760. err = refundOrder.Refund(productOrder)
  761. if err != nil {
  762. br.Msg = "退款失败"
  763. br.ErrMsg = "退款失败,,Err:" + err.Error()
  764. return
  765. }
  766. refundflow := models.RefundDealFlow{
  767. ProductOrderNo: productOrder.OrderId,
  768. RefundOrderNo: refundOrder.TransactionId,
  769. OperatorUserId: this.SysUser.SysUserId,
  770. CreatedTime: time.Now(),
  771. }
  772. //增加一条退款流水
  773. _ = refundflow.Insert()
  774. br.Ret = 200
  775. br.Success = true
  776. br.Msg = "退款处理成功"
  777. } else {
  778. br.Msg = "退款失败,当前订单正在退款中"
  779. br.ErrMsg = "退款失败,当前订单正在退款中"
  780. return
  781. }
  782. }
  783. func GenerateProductOrderNo() string {
  784. timestamp := time.Now().UnixNano() / 1000000 // 毫秒级时间戳
  785. // 生成随机数
  786. rand.New(rand.NewSource(time.Now().UnixNano()))
  787. randomPart := rand.Intn(999999)
  788. // 格式化订单号
  789. orderNumber := fmt.Sprintf("R%d%06d", timestamp, randomPart)
  790. return orderNumber
  791. }
  792. // RefundDetail
  793. // @Title 退款详情
  794. // @Description 退款详情
  795. // @Param PageSize query int true "每页数据条数"
  796. // @Param CurrentIndex query int true "当前页页码,从1开始"
  797. // @Param ClassifyIds query string true "二级分类id,可多选用英文,隔开"
  798. // @Param KeyWord query string true "报告标题/创建人"
  799. // @Param SortType query string true "排序方式"
  800. // @Success 200 {object} models.ReportAuthorResp
  801. // @router /refundDetail [get]
  802. func (this *OrderController) RefundDetail() {
  803. br := new(models.BaseResponse).Init()
  804. defer func() {
  805. this.Data["json"] = br
  806. this.ServeJSON()
  807. }()
  808. ProductOrderNo := this.GetString("ProductOrderNo")
  809. if ProductOrderNo == "" {
  810. br.Msg = "商品订单号不能为空"
  811. br.ErrMsg = "商品订单号不能为空"
  812. return
  813. }
  814. productOrder, err := models.GetProductOrderByID(ProductOrderNo)
  815. if err != nil {
  816. br.Msg = "获取商品订单失败"
  817. br.ErrMsg = "获取商品订单失败,err:" + err.Error()
  818. return
  819. }
  820. if productOrder.Status != models.OrderStatusRefund && productOrder.RefundStatus != models.RefundStatusSuccess {
  821. br.Msg = "当前订单未完成退款"
  822. br.ErrMsg = "当前订单未完成退款"
  823. return
  824. }
  825. refundOrder, err := models.GetTradeOrderByNo(productOrder.RefundTradeId)
  826. if err != nil {
  827. br.Msg = "获取退款订单失败"
  828. br.ErrMsg = "获取退款订单失败,err:" + err.Error()
  829. return
  830. }
  831. refundResp := response.RefundResp{
  832. Account: refundOrder.PaymentAccount,
  833. RealName: productOrder.RealName,
  834. RefundAmount: productOrder.RefundAmount,
  835. RefundFinishTime: productOrder.RefundFinishTime.Format(time.DateTime),
  836. Remark: productOrder.Remark,
  837. }
  838. br.Ret = 200
  839. br.Success = true
  840. br.Data = refundResp
  841. br.Msg = "退款详情获取成功"
  842. return
  843. }