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.

105 lines
4.2 KiB

  1. package service
  2. import (
  3. "gin-vue-admin/global"
  4. "gin-vue-admin/model"
  5. "gin-vue-admin/model/request"
  6. )
  7. // @title Create{{.StructName}}
  8. // @description create a {{.StructName}}
  9. // @param {{.Abbreviation}} model.{{.StructName}}
  10. // @auth (2020/04/05 20:22)
  11. // @return err error
  12. func Create{{.StructName}}({{.Abbreviation}} model.{{.StructName}}) (err error) {
  13. err = global.GVA_DB.Create(&{{.Abbreviation}}).Error
  14. return err
  15. }
  16. // @title Delete{{.StructName}}
  17. // @description delete a {{.StructName}}
  18. // @auth (2020/04/05 20:22)
  19. // @param {{.Abbreviation}} model.{{.StructName}}
  20. // @return error
  21. func Delete{{.StructName}}({{.Abbreviation}} model.{{.StructName}}) (err error) {
  22. err = global.GVA_DB.Delete({{.Abbreviation}}).Error
  23. return err
  24. }
  25. // @title Delete{{.StructName}}ByIds
  26. // @description delete {{.StructName}}s
  27. // @auth (2020/04/05 20:22)
  28. // @param {{.Abbreviation}} model.{{.StructName}}
  29. // @return error
  30. func Delete{{.StructName}}ByIds(ids request.IdsReq) (err error) {
  31. err = global.GVA_DB.Delete(&[]model.{{.StructName}}{},"id in (?)",ids.Ids).Error
  32. return err
  33. }
  34. // @title Update{{.StructName}}
  35. // @description update a {{.StructName}}
  36. // @param {{.Abbreviation}} *model.{{.StructName}}
  37. // @auth (2020/04/05 20:22)
  38. // @return error
  39. func Update{{.StructName}}({{.Abbreviation}} *model.{{.StructName}}) (err error) {
  40. err = global.GVA_DB.Save({{.Abbreviation}}).Error
  41. return err
  42. }
  43. // @title Get{{.StructName}}
  44. // @description get the info of a {{.StructName}}
  45. // @auth (2020/04/05 20:22)
  46. // @param id uint
  47. // @return error
  48. // @return {{.StructName}} {{.StructName}}
  49. func Get{{.StructName}}(id uint) (err error, {{.Abbreviation}} model.{{.StructName}}) {
  50. err = global.GVA_DB.Where("id = ?", id).First(&{{.Abbreviation}}).Error
  51. return
  52. }
  53. // @title Get{{.StructName}}InfoList
  54. // @description get {{.StructName}} list by pagination, 分页获取{{.StructName}}
  55. // @auth (2020/04/05 20:22)
  56. // @param info PageInfo
  57. // @return error
  58. func Get{{.StructName}}InfoList(info request.{{.StructName}}Search) (err error, list interface{}, total int) {
  59. limit := info.PageSize
  60. offset := info.PageSize * (info.Page - 1)
  61. // 创建db
  62. db := global.GVA_DB.Model(&model.{{.StructName}}{})
  63. var {{.Abbreviation}}s []model.{{.StructName}}
  64. // 如果有条件搜索 下方会自动创建搜索语句
  65. {{- range .Fields}}
  66. {{- if .FieldSearchType}}
  67. {{- if eq .FieldType "string" }}
  68. if info.{{.FieldName}} != "" {
  69. db = db.Where("`{{.ColumnName}}` {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+ {{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }})
  70. }
  71. {{- else if eq .FieldType "bool" }}
  72. if info.{{.FieldName}} != nil {
  73. db = db.Where("`{{.ColumnName}}` {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }})
  74. }
  75. {{- else if eq .FieldType "int" }}
  76. if info.{{.FieldName}} != 0 {
  77. db = db.Where("`{{.ColumnName}}` {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }})
  78. }
  79. {{- else if eq .FieldType "float64" }}
  80. if info.{{.FieldName}} != 0 {
  81. db = db.Where("`{{.ColumnName}}` {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }})
  82. }
  83. {{- else if eq .FieldType "time.Time" }}
  84. if !info.{{.FieldName}}.IsZero() {
  85. db = db.Where("`{{.ColumnName}}` {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }})
  86. }
  87. {{- end }}
  88. {{- end }}
  89. {{- end }}
  90. err = db.Count(&total).Error
  91. err = db.Limit(limit).Offset(offset).Find(&{{.Abbreviation}}s).Error
  92. return err, {{.Abbreviation}}s, total
  93. }