model.template 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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.ColumnName}" ${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.ColumnName}" 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.ColumnName}" ${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.ColumnName}"` //${$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. } ${end}
  101. ${if eq $column.QueryType "EQ"} ${if eq $column.GoType "string"}
  102. if req.${$column.GoField} != "" {
  103. model = model.Where("${$column.ColumnName} = ?", req.${$column.GoField})
  104. }
  105. ${else if eq $column.GoType "Time"}
  106. if req.${$column.GoField} != nil {
  107. model = model.Where("${$column.ColumnName} = ?", req.${$column.GoField})
  108. }
  109. ${else if or (eq $column.GoType "int") (eq $column.GoType "int64") (eq $column.GoType "uint") (eq $column.GoType "uint64") }
  110. if req.${$column.GoField} != 0 {
  111. model = model.Where("${$column.ColumnName} = ?", req.${$column.GoField})
  112. }
  113. ${end} ${end}
  114. ${if and (eq $column.QueryType "BETWEEN") (eq $column.ColumnType "datetime") }
  115. if req.${$column.GoField} != nil {
  116. model = model.Where("${$column.ColumnName} >= ? AND ${$column.ColumnName} < ?", req.${$column.GoField}, req.${$column.GoField}.Add(gtime.D))
  117. }
  118. ${end}
  119. ${end} ${end}
  120. }
  121. // 查询总记录数(总行数)
  122. total, err = model.Count()
  123. if err != nil {
  124. g.Log().Error(err)
  125. err = gerror.New("获取总记录数失败")
  126. return
  127. }
  128. if req.PageNum == 0 {
  129. req.PageNum = 1
  130. }
  131. page = req.PageNum
  132. if req.PageSize == 0 {
  133. req.PageSize = 10
  134. }
  135. // 分页排序查询
  136. list, err = model.Page(int(page), int(req.PageSize)).Order("${$pk} desc").All()
  137. if err != nil {
  138. g.Log().Error(err)
  139. err = gerror.New("分页查询失败")
  140. return
  141. }
  142. return
  143. }
  144. // SelectListAll 获取所有数据
  145. func SelectListAll(req *SelectPageReq) (list []*Entity, err error) {
  146. model := Model
  147. if req != nil {
  148. ${range $index, $column := .table.Columns} ${if eq $column.IsQuery "1"}
  149. ${if eq $column.QueryType "LIKE"}
  150. if req.${$column.GoField} != "" {
  151. model.Where("${$column.ColumnName} like ?", "%"+req.${$column.GoField}+"%")
  152. } ${else if eq $column.QueryType "EQ"} ${if eq $column.GoType "string"}
  153. if req.${$column.GoField} != "" {
  154. model.Where("${$column.ColumnName} = ?", req.${$column.GoField})
  155. } ${else if eq $column.GoType "int" "int64"}
  156. if req.${$column.GoField} != 0 {
  157. model.Where("${$column.ColumnName} = ?", req.${$column.GoField})
  158. }
  159. ${end} ${end} ${end} ${end}
  160. }
  161. // 查询
  162. list, err = model.Order("${$pk} desc").All()
  163. if err != nil {
  164. g.Log().Error(err)
  165. err = gerror.New("查询失败")
  166. return
  167. }
  168. return
  169. }