You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.3 KiB

  1. package couponusers
  2. import (
  3. "CouponBackendGo/api/v1/couponusers"
  4. "CouponBackendGo/internal/model/dto"
  5. "CouponBackendGo/internal/service"
  6. "github.com/gogf/gf/v2/net/ghttp"
  7. "mime/multipart"
  8. )
  9. type CouponUsers struct{}
  10. func NewCouponUsers() *CouponUsers {
  11. return &CouponUsers{}
  12. }
  13. func (c *CouponUsers) GetCouponUsers(r *ghttp.Request) {
  14. var req *couponusers.GetCouponUsersReq
  15. if err := r.Parse(&req); err != nil {
  16. r.Response.WriteJsonExit(dto.Error(err.Error()))
  17. }
  18. result, err := service.CouponUsers().GetCouponUsersByCondition(r.Context(), req.CouponId, req.Jwcode, req.Name)
  19. if err != nil {
  20. r.Response.WriteJsonExit(dto.Error(err.Error()))
  21. }
  22. if result == nil {
  23. r.Response.WriteJsonExit(dto.Error("没有符合条件的用户"))
  24. }
  25. r.Response.WriteJsonExit(dto.SuccessWithData(result))
  26. }
  27. func (c *CouponUsers) DeleteCouponUserByJwcode(r *ghttp.Request) {
  28. var req *couponusers.DelCouponUserByJwcodeReq
  29. if err := r.Parse(&req); err != nil {
  30. r.Response.WriteJsonExit(dto.Error(err.Error()))
  31. }
  32. result, err := service.CouponUsers().DeleteCouponUserByJwcode(r.Context(), req.CouponId, req.Jwcode)
  33. if err != nil {
  34. r.Response.WriteJsonExit(dto.Error(err.Error()))
  35. }
  36. affected, err := result.RowsAffected()
  37. if affected == 0 {
  38. r.Response.WriteJsonExit(dto.Error("删除失败,删除了0条"))
  39. }
  40. r.Response.WriteJsonExit(dto.SuccessWithMsg("删除成功"))
  41. //r.Response.WriteJsonExit(dto.SuccessWithData(result))
  42. }
  43. // 待测试
  44. // 接受前端传来的excel表格,并解析,返回jwcode切片,可以有重复的,在插入时进行验证,如果有重复的,只插入一次
  45. func (c *CouponUsers) InsertJwcodeByExcel(r *ghttp.Request) {
  46. // 从请求中获取文件
  47. file, _, err := r.FormFile("excelFile")
  48. if err != nil {
  49. r.Response.WriteJsonExit(dto.Error(err.Error()))
  50. }
  51. // 关闭文件
  52. defer func(file multipart.File) {
  53. err := file.Close()
  54. if err != nil {
  55. // 处理关闭文件时的错误
  56. r.Response.WriteJsonExit(dto.Error("关闭文件失败"))
  57. }
  58. }(file)
  59. //参数校验,检查文件是否为空
  60. if file == nil {
  61. r.Response.WriteJsonExit(dto.Error("文件为空"))
  62. }
  63. jwcodes, err := service.CouponUsers().InsertJwcodeByExcel(file)
  64. if err != nil {
  65. r.Response.WriteJsonExit(dto.Error(err.Error()))
  66. }
  67. r.Response.WriteJsonExit(dto.SuccessWithData(jwcodes))
  68. }