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