order.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. "sync"
  9. "time"
  10. )
  11. var (
  12. ProductOrderStatus = map[models.OrderStatus]string{
  13. "pending": "待支付",
  14. "processing": "支付中",
  15. "paid": "已支付",
  16. "closed": "已关闭",
  17. "refund": "已退款",
  18. }
  19. TradeOrderStatus = map[models.PaymentStatus]string{
  20. "pending": "待支付",
  21. "failed": "支付失败",
  22. "done": "支付成功",
  23. }
  24. RefundOrderStatus = map[models.PaymentStatus]string{
  25. "pending": "退款中",
  26. "failed": "退款失败",
  27. "done": "退款成功",
  28. }
  29. RefundStatusMap = map[models.RefundStatus]string{
  30. "pending": "待退款",
  31. "processing": "退款中",
  32. "failed": "退款失败",
  33. "success": "退款成功",
  34. "canceled": "已取消",
  35. }
  36. ProductTypeMap = map[models.MerchantProductType]string{
  37. "report": "报告",
  38. "video": "视频",
  39. "audio": "音频",
  40. "package": "套餐",
  41. }
  42. PaymentWayMap = map[models.PaymentWay]string{
  43. "wechat": "微信",
  44. "alipay": "支付宝",
  45. }
  46. )
  47. type OrderController struct {
  48. BaseAuthController
  49. }
  50. // ProductOrderList
  51. // @Title 商品订单列表
  52. // @Description 商品订单列表
  53. // @Param PageSize query int true "每页数据条数"
  54. // @Param CurrentIndex query int true "当前页页码,从1开始"
  55. // @Param ClassifyIds query string true "二级分类id,可多选用英文,隔开"
  56. // @Param KeyWord query string true "报告标题/创建人"
  57. // @Param SortType query string true "排序方式"
  58. // @Success 200 {object} models.ReportAuthorResp
  59. // @router /productOrderList [get]
  60. func (this *OrderController) ProductOrderList() {
  61. br := new(models.BaseResponse).Init()
  62. defer func() {
  63. this.Data["json"] = br
  64. this.ServeJSON()
  65. }()
  66. pageSize, _ := this.GetInt("PageSize")
  67. currentIndex, _ := this.GetInt("CurrentIndex")
  68. sortType := this.GetString("SortType")
  69. KeyWord := this.GetString("KeyWord")
  70. PaymentDate := this.GetString("PaymentDate")
  71. PaymentWay := this.GetString("PaymentWay")
  72. CreatedDate := this.GetString("CreatedDate")
  73. ProductType := this.GetString("ProductType")
  74. RefundStatus := this.GetString("RefundStatus")
  75. OrderStatus := this.GetString("OrderStatus")
  76. var condition string
  77. if pageSize <= 0 {
  78. pageSize = utils.PageSize20
  79. }
  80. if currentIndex <= 0 {
  81. currentIndex = 1
  82. }
  83. if KeyWord != "" {
  84. condition += " AND (product_name like '%" + KeyWord + "%' or real_name like '%" + KeyWord + "%' order_id like '%" + KeyWord + "%' or mobile like '%" + KeyWord + "%')"
  85. }
  86. sortCondition := " ORDER BY created_time "
  87. if sortType == "" {
  88. sortType = "DESC"
  89. }
  90. if CreatedDate != "" {
  91. condition += " AND Date(created_time) = '" + CreatedDate + "'"
  92. }
  93. if PaymentDate != "" {
  94. condition += " AND Date(payment_time) = '" + PaymentDate + "'"
  95. }
  96. if PaymentWay != "" {
  97. condition += " AND payment_way='" + PaymentWay + "'"
  98. }
  99. if OrderStatus != "" {
  100. switch OrderStatus {
  101. case "pending":
  102. condition += " AND status='pending'"
  103. case "paid":
  104. condition += " AND status='paid'"
  105. case "closed":
  106. condition += " AND status='closed'"
  107. case "refund":
  108. condition += " AND status='refund'"
  109. if RefundStatus != "" {
  110. switch RefundStatus {
  111. case "pending":
  112. condition += " AND refund_status='pending'"
  113. case "processing":
  114. condition += " AND refund_status='processing'"
  115. case "failed":
  116. condition += " AND refund_status='failed'"
  117. case "success":
  118. condition += " AND refund_status='success'"
  119. }
  120. }
  121. default:
  122. br.Msg = "无效的订单状态"
  123. br.ErrMsg = "无效的订单状态:" + OrderStatus
  124. return
  125. }
  126. }
  127. if ProductType != "" {
  128. switch ProductType {
  129. case "report":
  130. condition += " AND product_type='" + string(models.ProductReport) + "'"
  131. case "audio":
  132. condition += " AND product_type='" + string(models.ProductAudio) + "'"
  133. case "video":
  134. condition += " AND product_type='" + string(models.ProductVideo) + "'"
  135. case "package":
  136. condition += " AND product_type='" + string(models.ProductPackage) + "'"
  137. default:
  138. br.Msg = "无效的产品类型"
  139. br.ErrMsg = "无效的产品类型:" + ProductType
  140. return
  141. }
  142. }
  143. sortCondition = sortCondition + sortType
  144. total, err := models.GetProductOrderCountByCondition(condition)
  145. if err != nil {
  146. br.Msg = "获取商品列表失败"
  147. br.ErrMsg = "获取商品列表失败,Err:" + err.Error()
  148. return
  149. }
  150. startSize := utils.StartIndex(currentIndex, pageSize)
  151. List, err := models.GetProductOrderByCondition(condition, sortCondition, startSize, pageSize)
  152. if err != nil {
  153. br.Msg = "获取商品列表失败"
  154. br.ErrMsg = "获取商品列表失败,Err:" + err.Error()
  155. return
  156. }
  157. var ListView []*models.ProductOrderView
  158. for _, order := range List {
  159. view := &models.ProductOrderView{
  160. OrderID: order.OrderID,
  161. RealName: order.RealName,
  162. Mobile: fmt.Sprintf("+%s %s", order.AreaCode, order.Mobile),
  163. ProductType: ProductTypeMap[order.ProductType],
  164. ProductName: order.ProductName,
  165. TotalAmount: order.TotalAmount,
  166. TradeNO: order.TradeNO,
  167. RefundAmount: order.RefundAmount,
  168. PaymentWay: PaymentWayMap[order.PaymentWay],
  169. PaymentTime: order.PaymentTime.Format(time.DateTime),
  170. Status: ProductOrderStatus[order.Status],
  171. RefundStatus: RefundStatusMap[order.RefundStatus],
  172. RefundFinishTime: order.RefundFinishTime.Format(time.DateTime),
  173. Remark: order.Remark,
  174. CreatedTime: order.CreatedTime.Format(time.DateTime),
  175. }
  176. ListView = append(ListView, view)
  177. }
  178. page := paging.GetPaging(currentIndex, pageSize, total)
  179. resp := new(response.ProductOrderListResp)
  180. resp.List = ListView
  181. resp.Paging = page
  182. br.Ret = 200
  183. br.Success = true
  184. br.Data = resp
  185. br.Msg = "获取成功"
  186. }
  187. // TradeOrderList
  188. // @Title 支付订单列表
  189. // @Description 支付订单列表
  190. // @Param PageSize query int true "每页数据条数"
  191. // @Param CurrentIndex query int true "当前页页码,从1开始"
  192. // @Param ClassifyIds query string true "二级分类id,可多选用英文,隔开"
  193. // @Param KeyWord query string true "报告标题/创建人"
  194. // @Param SortType query string true "排序方式"
  195. // @Success 200 {object} models.ReportAuthorResp
  196. // @router /tradeOrderList [get]
  197. func (this *OrderController) TradeOrderList() {
  198. br := new(models.BaseResponse).Init()
  199. defer func() {
  200. this.Data["json"] = br
  201. this.ServeJSON()
  202. }()
  203. pageSize, _ := this.GetInt("PageSize")
  204. currentIndex, _ := this.GetInt("CurrentIndex")
  205. sortType := this.GetString("SortType")
  206. KeyWord := this.GetString("KeyWord")
  207. DealDate := this.GetString("DealDate")
  208. PaymentWay := this.GetString("PaymentWay")
  209. CreatedDate := this.GetString("CreatedDate")
  210. OrderStatus := this.GetString("OrderStatus")
  211. IsRefund, _ := this.GetBool("IsRefund", false)
  212. var condition string
  213. if pageSize <= 0 {
  214. pageSize = utils.PageSize20
  215. }
  216. if currentIndex <= 0 {
  217. currentIndex = 1
  218. }
  219. if IsRefund {
  220. condition += " AND payment_type ='" + string(models.PaymentTypeRefund) + "'"
  221. } else {
  222. condition += " AND payment_type ='" + string(models.PaymentTypePay) + "'"
  223. }
  224. if KeyWord != "" {
  225. condition += " AND (product_name like '%" + KeyWord + "%' or real_name like '%" + KeyWord + "%' order_id like '%" + KeyWord + "%' or mobile like '%" + KeyWord + "%')"
  226. }
  227. sortCondition := " ORDER BY created_time "
  228. if sortType == "" {
  229. sortType = "DESC"
  230. }
  231. if CreatedDate != "" {
  232. condition += " AND Date(created_time) = '" + CreatedDate + "'"
  233. }
  234. if DealDate != "" {
  235. condition += " AND Date(deal_time) = '" + DealDate + "'"
  236. }
  237. if PaymentWay != "" {
  238. condition += " AND payment_way='" + PaymentWay + "'"
  239. }
  240. if OrderStatus != "" {
  241. switch OrderStatus {
  242. case "pending":
  243. condition += " AND payment_status='pending'"
  244. case "done":
  245. condition += " AND payment_status='done'"
  246. case "failed":
  247. condition += " AND payment_status='failed'"
  248. default:
  249. br.Msg = "无效的支付订单状态"
  250. br.ErrMsg = "无效的支付订单状态:" + OrderStatus
  251. return
  252. }
  253. }
  254. sortCondition = sortCondition + sortType
  255. total, err := models.GetTradeOrderCountByCondition(condition)
  256. if err != nil {
  257. br.Msg = "获取支付明细列表失败"
  258. br.ErrMsg = "获取支付明细列表失败,Err:" + err.Error()
  259. return
  260. }
  261. startSize := utils.StartIndex(currentIndex, pageSize)
  262. List, err := models.GetTradeOrderByCondition(condition, sortCondition, startSize, pageSize)
  263. if err != nil {
  264. br.Msg = "获取支付明细列表失败"
  265. br.ErrMsg = "获取支付明细列表失败,Err:" + err.Error()
  266. return
  267. }
  268. var ListView []*models.TradeOrderView
  269. var wg sync.WaitGroup
  270. wg.Add(len(List))
  271. for i := 0; i < len(List); i++ {
  272. go func(order *models.TradeOrder) {
  273. defer wg.Done()
  274. productOrder, pdErr := models.GetProductOrderByID(order.ProductOrderId)
  275. if pdErr != nil {
  276. utils.FileLog.Error("获取商品订单信息失败,Err:" + pdErr.Error())
  277. }
  278. view := &models.TradeOrderView{
  279. RealName: productOrder.RealName,
  280. Mobile: fmt.Sprintf("+%s %s", productOrder.AreaCode, productOrder.Mobile),
  281. ProductName: productOrder.ProductName,
  282. Amount: order.Amount,
  283. TransactionID: order.TransactionId,
  284. ProductOrderID: order.ProductOrderId,
  285. PaymentWay: PaymentWayMap[order.PaymentWay],
  286. PaymentAccount: order.PaymentAccount,
  287. MerchantID: order.MerchantId,
  288. DealTime: order.DealTime.Format(time.DateTime),
  289. CreatedTime: order.CreatedTime.Format(time.DateTime),
  290. }
  291. if IsRefund {
  292. view.PaymentStatus = RefundOrderStatus[order.PaymentStatus]
  293. } else {
  294. view.PaymentStatus = TradeOrderStatus[order.PaymentStatus]
  295. }
  296. ListView = append(ListView, view)
  297. }(List[i])
  298. }
  299. wg.Wait()
  300. page := paging.GetPaging(currentIndex, pageSize, total)
  301. resp := new(response.TradeOrderListResp)
  302. resp.List = ListView
  303. resp.Paging = page
  304. br.Ret = 200
  305. br.Success = true
  306. br.Data = resp
  307. br.Msg = "获取成功"
  308. }