package controllers import ( "eta/eta_mini_crm_ht/models" "eta/eta_mini_crm_ht/models/response" "eta/eta_mini_crm_ht/utils" "fmt" "github.com/rdlucklib/rdluck_tools/paging" "github.com/xuri/excelize/v2" "net/http" "net/url" "strconv" "sync" "time" ) var ( productCols = map[string]utils.ExcelColMapping{ "A": {"订单编号", "OrderID"}, "B": {"姓名", "RealName"}, "C": {"手机号", "Mobile"}, "D": {"商品名称", "ProductName"}, "E": {"商品类型", "ProductType"}, "F": {"商品价格", "Price"}, "G": {"有效期", "ValidDate"}, "H": {"订单状态", "Status"}, "I": {"支付渠道", "PaymentWay"}, "J": {"支付金额", "TotalAmount"}, "K": {"售后状态", "RefundStatus"}, "L": {"付款时间", "PaymentTime"}, "M": {"下单时间", "CreatedTime"}, } tradeCols = map[string]utils.ExcelColMapping{ "A": {"支付订单", "TransactionId"}, "B": {"订单编号", "OrderID"}, "C": {"姓名", "RealName"}, "D": {"手机号", "Mobile"}, "E": {"商品名称", "ProductName"}, "F": {"支付金额", "Amount"}, "G": {"支付状态", "PaymentStatus"}, "H": {"支付渠道", "PaymentWay"}, "I": {"支付账号", "PaymentAccount"}, "J": {"收款方", "MerchantId"}, "K": {"完成支付时间", "DealTime"}, "L": {"创建时间", "CreatedTime"}, } refundCols = map[string]utils.ExcelColMapping{ "A": {"退款订单", "TransactionId"}, "B": {"订单编号", "OrderID"}, "C": {"姓名", "RealName"}, "D": {"手机号", "Mobile"}, "E": {"退款金额", "Amount"}, "F": {"退回账号", "PaymentAccount"}, "G": {"退款状态", "PaymentStatus"}, "H": {"完成退款时间", "DealTime"}, "I": {"创建时间", "CreatedTime"}, } ProductOrderStatus = map[models.OrderStatus]string{ "pending": "待支付", "processing": "支付中", "paid": "已支付", "closed": "已关闭", "refund": "已退款", } TradeOrderStatus = map[models.PaymentStatus]string{ "pending": "待支付", "failed": "支付失败", "done": "支付成功", } RefundOrderStatus = map[models.PaymentStatus]string{ "pending": "退款中", "failed": "退款失败", "done": "退款成功", } RefundStatusMap = map[models.RefundStatus]string{ "pending": "待退款", "processing": "退款中", "failed": "退款失败", "success": "退款成功", "canceled": "已取消", } ProductTypeMap = map[models.MerchantProductType]string{ "report": "报告", "video": "视频", "audio": "音频", "package": "套餐", } PaymentWayMap = map[models.PaymentWay]string{ "wechat": "微信", "alipay": "支付宝", } ) type OrderController struct { BaseAuthController } // ProductOrderList // @Title 商品订单列表 // @Description 商品订单列表 // @Param PageSize query int true "每页数据条数" // @Param CurrentIndex query int true "当前页页码,从1开始" // @Param ClassifyIds query string true "二级分类id,可多选用英文,隔开" // @Param KeyWord query string true "报告标题/创建人" // @Param SortType query string true "排序方式" // @Success 200 {object} models.ReportAuthorResp // @router /productOrderList [get] func (this *OrderController) ProductOrderList() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() pageSize, _ := this.GetInt("PageSize") currentIndex, _ := this.GetInt("CurrentIndex") sortType := this.GetString("SortType") KeyWord := this.GetString("KeyWord") PaymentDate := this.GetString("PaymentDate") PaymentWay := this.GetString("PaymentWay") CreatedDate := this.GetString("CreatedDate") ProductType := this.GetString("ProductType") RefundStatus := this.GetString("RefundStatus") OrderStatus := this.GetString("OrderStatus") var condition string if pageSize <= 0 { pageSize = utils.PageSize20 } if currentIndex <= 0 { currentIndex = 1 } if KeyWord != "" { condition += " AND (product_name like '%" + KeyWord + "%' or real_name like '%" + KeyWord + "%' order_id like '%" + KeyWord + "%' or mobile like '%" + KeyWord + "%')" } sortCondition := " ORDER BY created_time " if sortType == "" { sortType = "DESC" } if CreatedDate != "" { condition += " AND Date(created_time) = '" + CreatedDate + "'" } if PaymentDate != "" { condition += " AND Date(payment_time) = '" + PaymentDate + "'" } if PaymentWay != "" { condition += " AND payment_way='" + PaymentWay + "'" } if OrderStatus != "" { switch OrderStatus { case "pending": condition += " AND status='pending'" case "paid": condition += " AND status='paid'" case "closed": condition += " AND status='closed'" case "refund": condition += " AND status='refund'" if RefundStatus != "" { switch RefundStatus { case "pending": condition += " AND refund_status='pending'" case "processing": condition += " AND refund_status='processing'" case "failed": condition += " AND refund_status='failed'" case "success": condition += " AND refund_status='success'" } } default: br.Msg = "无效的订单状态" br.ErrMsg = "无效的订单状态:" + OrderStatus return } } if ProductType != "" { switch ProductType { case "report": condition += " AND product_type='" + string(models.ProductReport) + "'" case "audio": condition += " AND product_type='" + string(models.ProductAudio) + "'" case "video": condition += " AND product_type='" + string(models.ProductVideo) + "'" case "package": condition += " AND product_type='" + string(models.ProductPackage) + "'" default: br.Msg = "无效的产品类型" br.ErrMsg = "无效的产品类型:" + ProductType return } } sortCondition = sortCondition + sortType total, err := models.GetProductOrderCountByCondition(condition) if err != nil { br.Msg = "获取商品列表失败" br.ErrMsg = "获取商品列表失败,Err:" + err.Error() return } startSize := utils.StartIndex(currentIndex, pageSize) List, err := models.GetProductOrderByCondition(condition, sortCondition, startSize, pageSize) if err != nil { br.Msg = "获取商品列表失败" br.ErrMsg = "获取商品列表失败,Err:" + err.Error() return } var ListView []*models.ProductOrderView for _, order := range List { view := &models.ProductOrderView{ OrderID: order.OrderID, RealName: order.RealName, Mobile: fmt.Sprintf("+%s %s", order.AreaCode, order.Mobile), ProductType: ProductTypeMap[order.ProductType], ProductName: order.ProductName, TotalAmount: order.TotalAmount, TradeNO: order.TradeNO, RefundAmount: order.RefundAmount, PaymentWay: PaymentWayMap[order.PaymentWay], PaymentTime: order.PaymentTime.Format(time.DateTime), Status: ProductOrderStatus[order.Status], RefundStatus: RefundStatusMap[order.RefundStatus], RefundFinishTime: order.RefundFinishTime.Format(time.DateTime), Remark: order.Remark, CreatedTime: order.CreatedTime.Format(time.DateTime), } ListView = append(ListView, view) } page := paging.GetPaging(currentIndex, pageSize, total) resp := new(response.ProductOrderListResp) resp.List = ListView resp.Paging = page br.Ret = 200 br.Success = true br.Data = resp br.Msg = "获取成功" } // TradeOrderList // @Title 支付订单列表 // @Description 支付订单列表 // @Param PageSize query int true "每页数据条数" // @Param CurrentIndex query int true "当前页页码,从1开始" // @Param ClassifyIds query string true "二级分类id,可多选用英文,隔开" // @Param KeyWord query string true "报告标题/创建人" // @Param SortType query string true "排序方式" // @Success 200 {object} models.ReportAuthorResp // @router /tradeOrderList [get] func (this *OrderController) TradeOrderList() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() pageSize, _ := this.GetInt("PageSize") currentIndex, _ := this.GetInt("CurrentIndex") sortType := this.GetString("SortType") KeyWord := this.GetString("KeyWord") DealDate := this.GetString("DealDate") PaymentWay := this.GetString("PaymentWay") CreatedDate := this.GetString("CreatedDate") OrderStatus := this.GetString("OrderStatus") IsRefund, _ := this.GetBool("IsRefund", false) var condition string if pageSize <= 0 { pageSize = utils.PageSize20 } if currentIndex <= 0 { currentIndex = 1 } if IsRefund { condition += " AND payment_type ='" + string(models.PaymentTypeRefund) + "'" } else { condition += " AND payment_type ='" + string(models.PaymentTypePay) + "'" } if KeyWord != "" { condition += " AND (product_name like '%" + KeyWord + "%' or real_name like '%" + KeyWord + "%' order_id like '%" + KeyWord + "%' or mobile like '%" + KeyWord + "%')" } sortCondition := " ORDER BY created_time " if sortType == "" { sortType = "DESC" } if CreatedDate != "" { condition += " AND Date(created_time) = '" + CreatedDate + "'" } if DealDate != "" { condition += " AND Date(deal_time) = '" + DealDate + "'" } if PaymentWay != "" { condition += " AND payment_way='" + PaymentWay + "'" } if OrderStatus != "" { switch OrderStatus { case "pending": condition += " AND payment_status='pending'" case "done": condition += " AND payment_status='done'" case "failed": condition += " AND payment_status='failed'" default: br.Msg = "无效的支付订单状态" br.ErrMsg = "无效的支付订单状态:" + OrderStatus return } } sortCondition = sortCondition + sortType total, err := models.GetTradeOrderCountByCondition(condition) if err != nil { br.Msg = "获取支付明细列表失败" br.ErrMsg = "获取支付明细列表失败,Err:" + err.Error() return } startSize := utils.StartIndex(currentIndex, pageSize) List, err := models.GetTradeOrderByCondition(condition, sortCondition, startSize, pageSize) if err != nil { br.Msg = "获取支付明细列表失败" br.ErrMsg = "获取支付明细列表失败,Err:" + err.Error() return } var ListView []*models.TradeOrderView var wg sync.WaitGroup wg.Add(len(List)) for i := 0; i < len(List); i++ { go func(order *models.TradeOrder) { defer wg.Done() productOrder, pdErr := models.GetProductOrderByID(order.ProductOrderId) if pdErr != nil { utils.FileLog.Error("获取商品订单信息失败,Err:" + pdErr.Error()) } view := &models.TradeOrderView{ RealName: productOrder.RealName, Mobile: fmt.Sprintf("+%s %s", productOrder.AreaCode, productOrder.Mobile), ProductName: productOrder.ProductName, Amount: order.Amount, TransactionID: order.TransactionId, ProductOrderID: order.ProductOrderId, PaymentWay: PaymentWayMap[order.PaymentWay], PaymentAccount: order.PaymentAccount, MerchantID: order.MerchantId, DealTime: order.DealTime.Format(time.DateTime), CreatedTime: order.CreatedTime.Format(time.DateTime), } if IsRefund { view.PaymentStatus = RefundOrderStatus[order.PaymentStatus] } else { view.PaymentStatus = TradeOrderStatus[order.PaymentStatus] } ListView = append(ListView, view) }(List[i]) } wg.Wait() page := paging.GetPaging(currentIndex, pageSize, total) resp := new(response.TradeOrderListResp) resp.List = ListView resp.Paging = page br.Ret = 200 br.Success = true br.Data = resp br.Msg = "获取成功" } // ExportProductOrder // @Title 临时用户列表 // @Description 临时用户列表 // @Param PageSize query int true "每页数据条数" // @Param CurrentIndex query int true "当前页页码,从1开始" // @Param Keyword query string false "手机号" // @Param SortParam query string false "排序字段参数,用来排序的字段, 枚举值:0:注册时间,1:阅读数,2:最近一次阅读时间" // @Param SortType query string true "如何排序,是正序还是倒序,0:倒序,1:正序" // @Success 200 {object} response.TemplateUserListResp // @router /exportProductOrder [get] func (this *OrderController) ExportProductOrder() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() pageSize, _ := this.GetInt("PageSize") currentIndex, _ := this.GetInt("CurrentIndex") sortType := this.GetString("SortType") KeyWord := this.GetString("KeyWord") PaymentDate := this.GetString("PaymentDate") PaymentWay := this.GetString("PaymentWay") CreatedDate := this.GetString("CreatedDate") ProductType := this.GetString("ProductType") RefundStatus := this.GetString("RefundStatus") OrderStatus := this.GetString("OrderStatus") var condition string if pageSize <= 0 { pageSize = utils.PageSize20 } if currentIndex <= 0 { currentIndex = 1 } if KeyWord != "" { condition += " AND (product_name like '%" + KeyWord + "%' or real_name like '%" + KeyWord + "%' order_id like '%" + KeyWord + "%' or mobile like '%" + KeyWord + "%')" } sortCondition := " ORDER BY created_time " if sortType == "" { sortType = "DESC" } if CreatedDate != "" { condition += " AND Date(created_time) = '" + CreatedDate + "'" } if PaymentDate != "" { condition += " AND Date(payment_time) = '" + PaymentDate + "'" } if PaymentWay != "" { condition += " AND payment_way='" + PaymentWay + "'" } if OrderStatus != "" { switch OrderStatus { case "pending": condition += " AND status='pending'" case "paid": condition += " AND status='paid'" case "closed": condition += " AND status='closed'" case "refund": condition += " AND status='refund'" if RefundStatus != "" { switch RefundStatus { case "pending": condition += " AND refund_status='pending'" case "processing": condition += " AND refund_status='processing'" case "failed": condition += " AND refund_status='failed'" case "success": condition += " AND refund_status='success'" } } default: br.Msg = "无效的订单状态" br.ErrMsg = "无效的订单状态:" + OrderStatus return } } if ProductType != "" { switch ProductType { case "report": condition += " AND product_type='" + string(models.ProductReport) + "'" case "audio": condition += " AND product_type='" + string(models.ProductAudio) + "'" case "video": condition += " AND product_type='" + string(models.ProductVideo) + "'" case "package": condition += " AND product_type='" + string(models.ProductPackage) + "'" default: br.Msg = "无效的产品类型" br.ErrMsg = "无效的产品类型:" + ProductType return } } sortCondition = sortCondition + sortType List, err := models.GetProductOrderListByCondition(condition, sortCondition) if err != nil { br.Msg = "导出商品订单失败" br.ErrMsg = "导出商品订单失败,Err:" + err.Error() return } var ListView []*models.ProductOrderView for _, order := range List { view := &models.ProductOrderView{ OrderID: order.OrderID, RealName: order.RealName, Mobile: fmt.Sprintf("+%s %s", order.AreaCode, order.Mobile), ProductType: ProductTypeMap[order.ProductType], ProductName: order.ProductName, TotalAmount: order.TotalAmount, TradeNO: order.TradeNO, RefundAmount: order.RefundAmount, PaymentWay: PaymentWayMap[order.PaymentWay], PaymentTime: order.PaymentTime.Format(time.DateTime), Status: ProductOrderStatus[order.Status], RefundStatus: RefundStatusMap[order.RefundStatus], RefundFinishTime: order.RefundFinishTime.Format(time.DateTime), Remark: order.Remark, CreatedTime: order.CreatedTime.Format(time.DateTime), } ListView = append(ListView, view) } year, month, day := time.Now().Date() yearStr := strconv.Itoa(year)[2:] fileName := fmt.Sprintf("商品订单%s.%d.%d.xlsx", yearStr, month, day) file, err := utils.ExportExcel("商品订单", productCols, ListView) _ = this.downloadExcelFile(file, fileName) br.Ret = 200 br.Success = true br.Msg = "下载成功" } // ExportTradeOrder // @Title 临时用户列表 // @Description 临时用户列表 // @Param PageSize query int true "每页数据条数" // @Param CurrentIndex query int true "当前页页码,从1开始" // @Param Keyword query string false "手机号" // @Param SortParam query string false "排序字段参数,用来排序的字段, 枚举值:0:注册时间,1:阅读数,2:最近一次阅读时间" // @Param SortType query string true "如何排序,是正序还是倒序,0:倒序,1:正序" // @Success 200 {object} response.TemplateUserListResp // @router /exportTradeOrder [get] func (this *OrderController) ExportTradeOrder() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() pageSize, _ := this.GetInt("PageSize") currentIndex, _ := this.GetInt("CurrentIndex") sortType := this.GetString("SortType") KeyWord := this.GetString("KeyWord") DealDate := this.GetString("DealDate") PaymentWay := this.GetString("PaymentWay") CreatedDate := this.GetString("CreatedDate") OrderStatus := this.GetString("OrderStatus") IsRefund, _ := this.GetBool("IsRefund", false) var condition string if pageSize <= 0 { pageSize = utils.PageSize20 } if currentIndex <= 0 { currentIndex = 1 } if IsRefund { condition += " AND payment_type ='" + string(models.PaymentTypeRefund) + "'" } else { condition += " AND payment_type ='" + string(models.PaymentTypePay) + "'" } if KeyWord != "" { condition += " AND (product_name like '%" + KeyWord + "%' or real_name like '%" + KeyWord + "%' order_id like '%" + KeyWord + "%' or mobile like '%" + KeyWord + "%')" } sortCondition := " ORDER BY created_time " if sortType == "" { sortType = "DESC" } if CreatedDate != "" { condition += " AND Date(created_time) = '" + CreatedDate + "'" } if DealDate != "" { condition += " AND Date(deal_time) = '" + DealDate + "'" } if PaymentWay != "" { condition += " AND payment_way='" + PaymentWay + "'" } if OrderStatus != "" { switch OrderStatus { case "pending": condition += " AND payment_status='pending'" case "done": condition += " AND payment_status='done'" case "failed": condition += " AND payment_status='failed'" default: br.Msg = "无效的支付订单状态" br.ErrMsg = "无效的支付订单状态:" + OrderStatus return } } sortCondition = sortCondition + sortType List, err := models.GetTradeOrderListByCondition(condition, sortCondition) if err != nil { br.Msg = "获取支付明细列表失败" br.ErrMsg = "获取支付明细列表失败,Err:" + err.Error() return } var ListView []*models.TradeOrderView var wg sync.WaitGroup wg.Add(len(List)) for i := 0; i < len(List); i++ { go func(order *models.TradeOrder) { defer wg.Done() productOrder, pdErr := models.GetProductOrderByID(order.ProductOrderId) if pdErr != nil { utils.FileLog.Error("获取商品订单信息失败,Err:" + pdErr.Error()) } view := &models.TradeOrderView{ RealName: productOrder.RealName, Mobile: fmt.Sprintf("+%s %s", productOrder.AreaCode, productOrder.Mobile), ProductName: productOrder.ProductName, Amount: order.Amount, TransactionID: order.TransactionId, ProductOrderID: order.ProductOrderId, PaymentWay: PaymentWayMap[order.PaymentWay], PaymentAccount: order.PaymentAccount, MerchantID: order.MerchantId, DealTime: order.DealTime.Format(time.DateTime), CreatedTime: order.CreatedTime.Format(time.DateTime), } if IsRefund { view.PaymentStatus = RefundOrderStatus[order.PaymentStatus] } else { view.PaymentStatus = TradeOrderStatus[order.PaymentStatus] } ListView = append(ListView, view) }(List[i]) } wg.Wait() year, month, day := time.Now().Date() yearStr := strconv.Itoa(year)[2:] if IsRefund { fileName := fmt.Sprintf("退款明细%s.%d.%d.xlsx", yearStr, month, day) file, _ := utils.ExportExcel("退款明细", refundCols, ListView) _ = this.downloadExcelFile(file, fileName) } else { fileName := fmt.Sprintf("支付明细%s.%d.%d.xlsx", yearStr, month, day) file, _ := utils.ExportExcel("支付明细", tradeCols, ListView) _ = this.downloadExcelFile(file, fileName) } br.Ret = 200 br.Success = true br.Msg = "下载成功" } // encodeChineseFilename 将中文文件名编码为 ISO-8859-1 func (this *OrderController) downloadExcelFile(file *excelize.File, filename string) (err error) { // 对文件名进行 ISO-8859-1 编码 fn := url.QueryEscape(filename) if filename == fn { fn = "filename=" + fn } else { fn = "filename=" + filename + "; filename*=utf-8''" + fn } this.Ctx.ResponseWriter.Header().Set("Access-Control-Expose-Headers", "Content-Disposition") this.Ctx.ResponseWriter.Header().Set("Content-Disposition", "attachment; "+fn) this.Ctx.ResponseWriter.Header().Set("Content-Description", "File Transfer") this.Ctx.ResponseWriter.Header().Set("Content-Type", "application/octet-stream") this.Ctx.ResponseWriter.Header().Set("Content-Transfer-Encoding", "binary") this.Ctx.ResponseWriter.Header().Set("Expires", "0") this.Ctx.ResponseWriter.Header().Set("Cache-Control", "must-revalidate") this.Ctx.ResponseWriter.Header().Set("Pragma", "public") this.Ctx.ResponseWriter.Header().Set("File-Name", filename) // 写入文件 if err = file.Write(this.Ctx.ResponseWriter); err != nil { utils.FileLog.Error("导出excel文件失败:", err) http.Error(this.Ctx.ResponseWriter, "导出excel文件失败", http.StatusInternalServerError) } return }