package controllers import ( "encoding/json" "eta/eta_mini_crm_ht/models" "eta/eta_mini_crm_ht/models/request" "eta/eta_mini_crm_ht/models/response" "eta/eta_mini_crm_ht/utils" "fmt" "github.com/google/uuid" "github.com/rdlucklib/rdluck_tools/paging" "github.com/xuri/excelize/v2" "math/rand" "net/http" "net/url" "strconv" "strings" "time" ) var ( productCols = map[string]utils.ExcelColMapping{ "A": {"订单编号", "OrderID"}, "B": {"姓名", "RealName"}, "C": {"手机号", "Mobile"}, "D": {"商品名称", "ProductName"}, "E": {"商品类型", "ProductType"}, "F": {"商品价格", "TotalAmount"}, "G": {"有效期", "ValidDate"}, "H": {"订单状态", "Status"}, "I": {"支付渠道", "PaymentWay"}, "J": {"支付金额", "PaymentAmount"}, "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") TemplateUserId, _ := this.GetInt("TemplateUserId", 0) 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 + "%'or 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 TemplateUserId > 0 { condition += fmt.Sprintf(" AND template_user_id=%d", TemplateUserId) } 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 _, orderItem := range List { view := &models.ProductOrderView{ OrderID: orderItem.OrderId, RealName: orderItem.RealName, Mobile: fmt.Sprintf("+%s %s", orderItem.AreaCode, orderItem.Mobile), ProductType: ProductTypeMap[orderItem.ProductType], ProductName: orderItem.ProductName, TotalAmount: fmt.Sprintf("¥%s", orderItem.TotalAmount), TradeNO: orderItem.TradeNo, RefundAmount: orderItem.RefundAmount, RefundTradeId: orderItem.RefundTradeId, PaymentWay: PaymentWayMap[orderItem.PaymentWay], Status: ProductOrderStatus[orderItem.Status], RefundStatus: RefundStatusMap[orderItem.RefundStatus], Remark: orderItem.Remark, ValidDuration: orderItem.ValidDuration, CreatedTime: orderItem.CreatedTime.Format(time.DateTime), } if orderItem.TradeNo != "" { view.PaymentTime = orderItem.PaymentTime.Format(time.DateTime) tradeOrder, tradeErr := models.GetTradeOrderByNo(orderItem.TradeNo) if tradeErr != nil { utils.FileLog.Error("获取支付订单失败,支付订单号:" + orderItem.TradeNo + ",err:" + tradeErr.Error()) } else { view.PaymentAmount = fmt.Sprintf("¥%s", tradeOrder.Amount) } } if orderItem.Status == models.OrderStatusRefund && orderItem.RefundStatus == models.RefundStatusSuccess { view.RefundFinishTime = orderItem.RefundFinishTime.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 + "%' or product_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 for i := 0; i < len(List); i++ { order := List[i] view := &models.TradeOrderView{ RealName: order.RealName, Mobile: fmt.Sprintf("+%s %s", order.AreaCode, order.Mobile), ProductName: order.ProductName, Amount: fmt.Sprintf("¥%s", order.Amount), TransactionID: order.TransactionId, ProductOrderID: order.ProductOrderId, PaymentWay: PaymentWayMap[order.PaymentWay], PaymentAccount: order.PaymentAccount, MerchantID: order.MerchantId, CreatedTime: order.CreatedTime.Format(time.DateTime), } if order.PaymentStatus == models.PaymentStatusDone { view.DealTime = order.DealTime.Format(time.DateTime) } if IsRefund { view.PaymentStatus = RefundOrderStatus[order.PaymentStatus] } else { view.PaymentStatus = TradeOrderStatus[order.PaymentStatus] } ListView = append(ListView, view) } 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 /productOrder/export [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 _, orderItem := range List { view := models.ProductOrderView{ OrderID: orderItem.OrderId, RealName: orderItem.RealName, Mobile: fmt.Sprintf("+%s %s", orderItem.AreaCode, orderItem.Mobile), ProductType: ProductTypeMap[orderItem.ProductType], ProductName: orderItem.ProductName, TotalAmount: fmt.Sprintf("¥%s", orderItem.TotalAmount), TradeNO: orderItem.TradeNo, RefundAmount: orderItem.RefundAmount, RefundTradeId: orderItem.RefundTradeId, PaymentWay: PaymentWayMap[orderItem.PaymentWay], PaymentTime: orderItem.PaymentTime.Format(time.DateTime), Status: ProductOrderStatus[orderItem.Status], RefundStatus: RefundStatusMap[orderItem.RefundStatus], Remark: orderItem.Remark, CreatedTime: orderItem.CreatedTime.Format(time.DateTime), ValidDuration: orderItem.ValidDuration, } if orderItem.TradeNo != "" { view.PaymentTime = orderItem.PaymentTime.Format(time.DateTime) tradeOrder, tradeErr := models.GetTradeOrderByNo(orderItem.TradeNo) if tradeErr != nil { utils.FileLog.Error("获取支付订单失败,支付订单号:" + orderItem.TradeNo + ",err:" + tradeErr.Error()) } else { view.PaymentAmount = fmt.Sprintf("¥%s", tradeOrder.Amount) } } //if orderItem.Status == models.OrderStatusPaid { // access, accessErr := models.GetAccess(orderItem.ProductId, orderItem.TemplateUserId) // if accessErr != nil { // utils.FileLog.Error("获取用户订阅记录失败,templateUserId:" + string(rune(orderItem.TemplateUserId)) + "productId:" + string(rune(orderItem.ProductId)) + ",err:" + accessErr.Error()) // } else { // if access.ProductType == models.ProductPackage { // view.ValidDuration = fmt.Sprintf("%s~%s", access.BeginDate.Format(time.DateOnly), access.EndDate.Format(time.DateOnly)) // } else { // view.ValidDuration = "永久有效" // } // } //} if orderItem.Status == models.OrderStatusRefund && orderItem.RefundStatus == models.RefundStatusSuccess { view.RefundFinishTime = orderItem.RefundFinishTime.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 /tradeOrder/export [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 + "%' or product_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 for i := 0; i < len(List); i++ { order := List[i] view := models.TradeOrderView{ RealName: order.RealName, Mobile: fmt.Sprintf("+%s %s", order.AreaCode, order.Mobile), ProductName: order.ProductName, Amount: fmt.Sprintf("¥%s", order.Amount), TransactionID: order.TransactionId, ProductOrderID: order.ProductOrderId, PaymentWay: PaymentWayMap[order.PaymentWay], PaymentAccount: order.PaymentAccount, MerchantID: order.MerchantId, CreatedTime: order.CreatedTime.Format(time.DateTime), } if order.PaymentStatus == models.PaymentStatusDone { view.DealTime = order.DealTime.Format(time.DateTime) } if IsRefund { view.PaymentStatus = RefundOrderStatus[order.PaymentStatus] } else { view.PaymentStatus = TradeOrderStatus[order.PaymentStatus] } ListView = append(ListView, view) } 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 } // Refund // @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 /refund [post] func (this *OrderController) Refund() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() var req request.RefundReq if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil { br.Msg = "参数解析失败" br.ErrMsg = "参数解析失败,Err:" + err.Error() return } if req.ProductOrderNo == "" { br.Msg = "商品订单号不能为空" br.ErrMsg = "商品订单号不能为空" return } productOrder, err := models.GetProductOrderByID(req.ProductOrderNo) if err != nil { br.Msg = "获取商品订单失败" br.ErrMsg = "获取商品订单失败,err:" + err.Error() return } if productOrder.Status == models.OrderStatusPending { br.Msg = "退款失败," br.ErrMsg = "退款失败,退款状态异常,当前订单已关闭" return } if productOrder.Status == models.OrderStatusRefund && productOrder.RefundStatus != models.RefundStatusFailed { br.Msg = "退款失败," br.ErrMsg = "退款失败,当前订单退款处理中" return } tradeOrder, err := models.GetTradeOrderByNo(productOrder.TradeNo) if err != nil { br.Msg = "退款失败,获取原订单失败" br.ErrMsg = "退款失败,获取原订单失败,err:" + err.Error() return } if tradeOrder.PaymentType != models.PaymentTypePay { br.Msg = "退款失败,原订单非支付订单" br.ErrMsg = "退款失败,原订单非支付订单" return } if tradeOrder.PaymentStatus != models.PaymentStatusDone { br.Msg = "退款失败,原订单未完成支付" br.ErrMsg = "退款失败,原订单未完成支付" return } uuidStr := strings.ReplaceAll(uuid.New().String(), "-", "") key := fmt.Sprintf("refund_lock_%s", productOrder.OrderId) defer func() { utils.ReleaseLock(key, uuidStr) }() if utils.AcquireLock(key, 10, uuidStr) { aa := GenerateProductOrderNo() refundOrder := &models.TradeOrder{ TransactionId: aa, OrgTransactionId: productOrder.TradeNo, ProductOrderId: productOrder.OrderId, PaymentAccount: tradeOrder.PaymentAccount, PaymentWay: tradeOrder.PaymentWay, ProductName: productOrder.ProductName, Mobile: productOrder.Mobile, AreaCode: productOrder.AreaCode, RealName: productOrder.RealName, Amount: tradeOrder.Amount, Currency: tradeOrder.Currency, UserId: tradeOrder.UserId, TemplateUserId: tradeOrder.TemplateUserId, PaymentType: models.PaymentTypeRefund, PaymentStatus: models.PaymentStatusPending, CreatedTime: time.Now(), } productOrder.RefundStatus = models.RefundStatusProcessing productOrder.Status = models.OrderStatusRefund productOrder.RefundTradeId = refundOrder.TransactionId productOrder.Remark = req.Remark productOrder.RefundAmount = tradeOrder.Amount err = refundOrder.Refund(productOrder) if err != nil { br.Msg = "退款失败" br.ErrMsg = "退款失败,,Err:" + err.Error() return } refundflow := models.RefundDealFlow{ ProductOrderNo: productOrder.OrderId, RefundOrderNo: refundOrder.TransactionId, OperatorUserId: this.SysUser.SysUserId, CreatedTime: time.Now(), } //增加一条退款流水 _ = refundflow.Insert() br.Ret = 200 br.Success = true br.Msg = "退款处理成功" } else { br.Msg = "退款失败,当前订单正在退款中" br.ErrMsg = "退款失败,当前订单正在退款中" return } } func GenerateProductOrderNo() string { timestamp := time.Now().UnixNano() / 1000000 // 毫秒级时间戳 // 生成随机数 rand.New(rand.NewSource(time.Now().UnixNano())) randomPart := rand.Intn(999999) // 格式化订单号 orderNumber := fmt.Sprintf("R%d%06d", timestamp, randomPart) return orderNumber } // RefundDetail // @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 /refundDetail [get] func (this *OrderController) RefundDetail() { br := new(models.BaseResponse).Init() defer func() { this.Data["json"] = br this.ServeJSON() }() ProductOrderNo := this.GetString("ProductOrderNo") if ProductOrderNo == "" { br.Msg = "商品订单号不能为空" br.ErrMsg = "商品订单号不能为空" return } productOrder, err := models.GetProductOrderByID(ProductOrderNo) if err != nil { br.Msg = "获取商品订单失败" br.ErrMsg = "获取商品订单失败,err:" + err.Error() return } if productOrder.Status != models.OrderStatusRefund && productOrder.RefundStatus != models.RefundStatusSuccess { br.Msg = "当前订单未完成退款" br.ErrMsg = "当前订单未完成退款" return } refundOrder, err := models.GetTradeOrderByNo(productOrder.RefundTradeId) if err != nil { br.Msg = "获取退款订单失败" br.ErrMsg = "获取退款订单失败,err:" + err.Error() return } refundResp := response.RefundResp{ Account: refundOrder.PaymentAccount, RealName: productOrder.RealName, RefundAmount: productOrder.RefundAmount, RefundFinishTime: productOrder.RefundFinishTime.Format(time.DateTime), Remark: productOrder.Remark, } br.Ret = 200 br.Success = true br.Data = refundResp br.Msg = "退款详情获取成功" return }