extend.template 5.5 KB

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