type.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package aftersale
  2. import (
  3. "zhiyuan/pkg/app"
  4. "zhiyuan/pkg/utils"
  5. "zhiyuan/services/aftersale"
  6. "zhiyuan/services/form"
  7. "github.com/gin-gonic/gin"
  8. )
  9. func TypeList(c *gin.Context) {
  10. isTree := utils.ToInt(c.Query("tree"))
  11. if isTree == 1 {
  12. TypeListTree(c)
  13. return
  14. }
  15. page := app.HandlePageNum(c)
  16. where := make(map[string]interface{})
  17. total, err := aftersale.CountType(where)
  18. if err != nil {
  19. app.Error(c, err.Error())
  20. return
  21. }
  22. types, err := aftersale.GetTypeList(where, []string{"id", "type_name"}, page, nil)
  23. if err != nil {
  24. app.Error(c, err.Error())
  25. return
  26. }
  27. data := gin.H{
  28. "list": types,
  29. "total": total,
  30. "limit": page.PageSize,
  31. }
  32. app.Success(c, data)
  33. }
  34. func TypeListTree(c *gin.Context) {
  35. auths, err := aftersale.GetTypeList(nil, []string{"id", "type_name", "pid", "warranty_period", "repair_days", "`show`"}, app.Page{}, nil)
  36. if err != nil {
  37. app.Error(c, err.Error())
  38. return
  39. }
  40. app.Success(c, utils.GenTree(auths, "id", "pid", "children", 0))
  41. }
  42. func TypeAdd(c *gin.Context) {
  43. var form form.ASTypeAdd
  44. if app.Bind(c, &form) != nil {
  45. return
  46. }
  47. if id, err := aftersale.AddType(form); err != nil {
  48. app.Error(c, err.Error())
  49. return
  50. } else {
  51. app.Success(c, gin.H{"id": id})
  52. }
  53. }
  54. func TypeEdit(c *gin.Context) {
  55. id := utils.StrTo(c.Param("id")).MustInt()
  56. if id <= 0 {
  57. app.Error(c, "类型id有误")
  58. return
  59. }
  60. var form form.ASTypeAdd
  61. if app.Bind(c, &form) != nil {
  62. return
  63. }
  64. if _, err := aftersale.EditType(form, id); err != nil {
  65. app.ErrorMsg(c, err.Error(), nil)
  66. return
  67. }
  68. app.Success(c, nil)
  69. }