model.template 5.6 KB

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