model.template 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // ==========================================================================
  2. // 生成日期:{{.table.CreateTime}}
  3. // 生成人:{{.table.FunctionAuthor}}
  4. // ==========================================================================
  5. package {{.table.BusinessName}}
  6. import (
  7. "github.com/gogf/gf/errors/gerror"
  8. "github.com/gogf/gf/frame/g"
  9. )
  10. {{$pk:=""}}
  11. {{$pkGoField:=""}}
  12. {{range $index, $column := .table.Columns}} {{if eq $column.IsPk "1"}}
  13. {{$pk = $column.ColumnName}}
  14. {{$pkGoField = $column.GoField}}
  15. {{end}}{{end}}
  16. // AddReq 用于存储新增请求的请求参数
  17. type AddReq struct {
  18. {{range $index, $column := .table.Columns}}
  19. {{if and (eq $column.IsInsert "1") (ne $column.IsPk "1")}} {{$column.GoField}} {{if eq $column.GoType "Time"}}*gtime.Time{{else}}{{$column.GoType}}{{end}} `p:"{{$column.HtmlField}}" {{if eq $column.IsRequired "1"}}v:"required#{{$column.ColumnComment}}不能为空"{{end}}` {{end}} {{end}}
  20. }
  21. // EditReq 用于存储修改请求参数
  22. type EditReq struct {
  23. {{.table.PkColumn.GoField}} {{.table.PkColumn.GoType}} `p:"{{.table.PkColumn.HtmlField}}" v:"required#主键ID不能为空"` {{range $index, $column := .table.Columns}} {{if eq $column.IsEdit "1"}}
  24. {{$column.GoField}} {{if eq $column.GoType "Time"}}*gtime.Time{{else}}{{$column.GoType}}{{end}} `p:"{{$column.HtmlField}}" {{if eq $column.IsRequired "1"}}v:"required#{{$column.ColumnComment}}不能为空"{{end}}` {{end}} {{end}}
  25. }
  26. // RemoveReq 用于存储删除请求参数
  27. type RemoveReq struct {
  28. Ids []int `p:"ids"` //删除id
  29. }
  30. // SelectPageReq 用于存储分页查询的请求参数
  31. type SelectPageReq struct { {{range $index, $column := .table.Columns}} {{if eq $column.IsQuery "1"}}
  32. {{$column.GoField}} {{if eq $column.GoType "Time"}}*gtime.Time{{else}}{{$column.GoType}}{{end}} `p:"{{$column.HtmlField}}"` //{{$column.ColumnComment}} {{end}} {{end}}
  33. BeginTime string `p:"beginTime"` //开始时间
  34. EndTime string `p:"endTime"` //结束时间
  35. PageNum int `p:"pageNum"` //当前页码
  36. PageSize int `p:"pageSize"` //每页数
  37. }
  38. // GetByID 根据ID查询记录
  39. func GetByID(id int64) (*Entity, error) {
  40. entity, err := Model.FindOne(id)
  41. if err != nil {
  42. g.Log().Error(err)
  43. return nil, gerror.New("根据ID查询记录出错")
  44. }
  45. if entity == nil {
  46. return nil, gerror.New("根据ID未能查询到记录")
  47. }
  48. return entity, nil
  49. }
  50. // AddSave 添加
  51. func AddSave(req *AddReq) error {
  52. entity:= new(Entity)
  53. {{range $index, $column := .table.Columns}} {{if eq $column.IsInsert "1"}}
  54. entity.{{$column.GoField}} = req.{{$column.GoField}}{{end}} {{end}}
  55. result, err := Model.Save(entity)
  56. if err != nil {
  57. return err
  58. }
  59. _, err = result.LastInsertId()
  60. if err != nil {
  61. return err
  62. }
  63. return nil
  64. }
  65. // DeleteByIds 删除
  66. func DeleteByIds(Ids []int) error {
  67. _, err := Model.Delete("{{.table.PkColumn.ColumnName}} in(?)", Ids)
  68. if err != nil {
  69. g.Log().Error(err)
  70. return gerror.New("删除失败")
  71. }
  72. return nil
  73. }
  74. // EditSave 根据ID来修改信息
  75. func EditSave(req *EditReq) error {
  76. // 先根据ID来查询要修改的记录
  77. entity, err := GetByID(req.{{$pkGoField}})
  78. if err != nil {
  79. return err
  80. }
  81. // 修改实体
  82. {{range $index, $column := .table.Columns}} {{if eq $column.IsEdit "1"}}
  83. entity.{{$column.GoField}} = req.{{$column.GoField}}{{end}} {{end}}
  84. _, err = Model.Save(entity)
  85. if err != nil {
  86. g.Log().Error(err)
  87. return gerror.New("修改失败")
  88. }
  89. return nil
  90. }
  91. // SelectListByPage 分页查询,返回值total总记录数,page当前页
  92. func SelectListByPage(req *SelectPageReq) (total int, page int64, list []*Entity, err error) {
  93. model := Model
  94. if req != nil {
  95. {{range $index, $column := .table.Columns}} {{if eq $column.IsQuery "1"}}
  96. {{if eq $column.QueryType "LIKE"}}
  97. if req.{{$column.GoField}} != "" {
  98. model = model.Where("{{$column.ColumnName}} like ?", "%"+req.{{$column.GoField}}+"%")
  99. } {{else if eq $column.QueryType "EQ"}} {{if eq $column.GoType "string"}}
  100. if req.{{$column.GoField}} != "" {
  101. model = model.Where("{{$column.ColumnName}} = ?", req.{{$column.GoField}})
  102. } {{else if eq $column.GoType "int" "int64"}}
  103. if req.{{$column.GoField}} != 0 {
  104. model = model.Where("{{$column.ColumnName}} = ?", req.{{$column.GoField}})
  105. }
  106. {{end}} {{end}} {{end}} {{end}}
  107. }
  108. // 查询总记录数(总行数)
  109. total, err = model.Count()
  110. if err != nil {
  111. g.Log().Error(err)
  112. err = gerror.New("获取总记录数失败")
  113. return
  114. }
  115. if req.PageNum == 0 {
  116. req.PageNum = 1
  117. }
  118. page = req.PageNum
  119. if req.PageSize == 0 {
  120. req.PageSize = 10
  121. }
  122. // 分页排序查询
  123. list, err = model.Page(int(page), int(req.PageSize)).Order("{{$pk}} asc").All()
  124. if err != nil {
  125. g.Log().Error(err)
  126. err = gerror.New("分页查询失败")
  127. return
  128. }
  129. return
  130. }
  131. // SelectListAll 获取所有数据
  132. func SelectListAll(req *SelectPageReq) (list []*Entity, err error) {
  133. model := Model
  134. if req != nil {
  135. {{range $index, $column := .table.Columns}} {{if eq $column.IsQuery "1"}}
  136. {{if eq $column.QueryType "LIKE"}}
  137. if req.{{$column.GoField}} != "" {
  138. model.Where("{{$column.ColumnName}} like ?", "%"+req.{{$column.GoField}}+"%")
  139. } {{else if eq $column.QueryType "EQ"}} {{if eq $column.GoType "string"}}
  140. if req.{{$column.GoField}} != "" {
  141. model.Where("{{$column.ColumnName}} = ?", req.{{$column.GoField}})
  142. } {{else if eq $column.GoType "int" "int64"}}
  143. if req.{{$column.GoField}} != 0 {
  144. model.Where("{{$column.ColumnName}} = ?", req.{{$column.GoField}})
  145. }
  146. {{end}} {{end}} {{end}} {{end}}
  147. }
  148. // 查询
  149. list, err = model.Order("{{$pk}} asc").All()
  150. if err != nil {
  151. g.Log().Error(err)
  152. err = gerror.New("查询失败")
  153. return
  154. }
  155. return
  156. }