service.go.tpl 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package autocode
  2. import (
  3. "github.com/flipped-aurora/gin-vue-admin/server/global"
  4. "github.com/flipped-aurora/gin-vue-admin/server/model/autocode"
  5. "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
  6. autoCodeReq "github.com/flipped-aurora/gin-vue-admin/server/model/autocode/request"
  7. )
  8. type {{.StructName}}Service struct {
  9. }
  10. // Create{{.StructName}} 创建{{.StructName}}记录
  11. // Author [piexlmax](https://github.com/piexlmax)
  12. func ({{.Abbreviation}}Service *{{.StructName}}Service) Create{{.StructName}}({{.Abbreviation}} autocode.{{.StructName}}) (err error) {
  13. err = global.GVA_DB.Create(&{{.Abbreviation}}).Error
  14. return err
  15. }
  16. // Delete{{.StructName}} 删除{{.StructName}}记录
  17. // Author [piexlmax](https://github.com/piexlmax)
  18. func ({{.Abbreviation}}Service *{{.StructName}}Service)Delete{{.StructName}}({{.Abbreviation}} autocode.{{.StructName}}) (err error) {
  19. err = global.GVA_DB.Delete(&{{.Abbreviation}}).Error
  20. return err
  21. }
  22. // Delete{{.StructName}}ByIds 批量删除{{.StructName}}记录
  23. // Author [piexlmax](https://github.com/piexlmax)
  24. func ({{.Abbreviation}}Service *{{.StructName}}Service)Delete{{.StructName}}ByIds(ids request.IdsReq) (err error) {
  25. err = global.GVA_DB.Delete(&[]autocode.{{.StructName}}{},"id in ?",ids.Ids).Error
  26. return err
  27. }
  28. // Update{{.StructName}} 更新{{.StructName}}记录
  29. // Author [piexlmax](https://github.com/piexlmax)
  30. func ({{.Abbreviation}}Service *{{.StructName}}Service)Update{{.StructName}}({{.Abbreviation}} autocode.{{.StructName}}) (err error) {
  31. err = global.GVA_DB.Save(&{{.Abbreviation}}).Error
  32. return err
  33. }
  34. // Get{{.StructName}} 根据id获取{{.StructName}}记录
  35. // Author [piexlmax](https://github.com/piexlmax)
  36. func ({{.Abbreviation}}Service *{{.StructName}}Service)Get{{.StructName}}(id uint) (err error, {{.Abbreviation}} autocode.{{.StructName}}) {
  37. err = global.GVA_DB.Where("id = ?", id).First(&{{.Abbreviation}}).Error
  38. return
  39. }
  40. // Get{{.StructName}}InfoList 分页获取{{.StructName}}记录
  41. // Author [piexlmax](https://github.com/piexlmax)
  42. func ({{.Abbreviation}}Service *{{.StructName}}Service)Get{{.StructName}}InfoList(info autoCodeReq.{{.StructName}}Search) (err error, list interface{}, total int64) {
  43. limit := info.PageSize
  44. offset := info.PageSize * (info.Page - 1)
  45. // 创建db
  46. db := global.GVA_DB.Model(&autocode.{{.StructName}}{})
  47. var {{.Abbreviation}}s []autocode.{{.StructName}}
  48. // 如果有条件搜索 下方会自动创建搜索语句
  49. {{- range .Fields}}
  50. {{- if .FieldSearchType}}
  51. {{- if eq .FieldType "string" }}
  52. if info.{{.FieldName}} != "" {
  53. db = db.Where("`{{.ColumnName}}` {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+ {{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }})
  54. }
  55. {{- else if eq .FieldType "bool" }}
  56. if info.{{.FieldName}} != nil {
  57. db = db.Where("`{{.ColumnName}}` {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }})
  58. }
  59. {{- else if eq .FieldType "int" }}
  60. if info.{{.FieldName}} != nil {
  61. db = db.Where("`{{.ColumnName}}` {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }})
  62. }
  63. {{- else if eq .FieldType "float64" }}
  64. if info.{{.FieldName}} != nil {
  65. db = db.Where("`{{.ColumnName}}` {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }})
  66. }
  67. {{- else if eq .FieldType "time.Time" }}
  68. if info.{{.FieldName}} != nil {
  69. db = db.Where("`{{.ColumnName}}` {{.FieldSearchType}} ?",{{if eq .FieldSearchType "LIKE"}}"%"+{{ end }}info.{{.FieldName}}{{if eq .FieldSearchType "LIKE"}}+"%"{{ end }})
  70. }
  71. {{- end }}
  72. {{- end }}
  73. {{- end }}
  74. err = db.Count(&total).Error
  75. err = db.Limit(limit).Offset(offset).Find(&{{.Abbreviation}}s).Error
  76. return err, {{.Abbreviation}}s, total
  77. }