model.template 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // ==========================================================================
  2. // 生成日期:${.table.CreateTime}
  3. // 生成人:${.table.FunctionAuthor}
  4. // ==========================================================================
  5. package ${.table.BusinessName}
  6. import (
  7. "${.table.PackageName}/app/service/cache_service"
  8. "github.com/gogf/gf/container/gset"
  9. "github.com/gogf/gf/errors/gerror"
  10. "github.com/gogf/gf/frame/g"
  11. "github.com/gogf/gf/util/gconv"
  12. )
  13. ${$pk:=""}
  14. ${$pkGoField:=""}
  15. ${range $index, $column := .table.Columns} ${if eq $column.IsPk "1"}
  16. ${$pk = $column.ColumnName}
  17. ${$pkGoField = $column.GoField}
  18. ${end}${end}
  19. // ReqAdd 用于存储新增请求的请求参数
  20. type ReqAdd struct {
  21. ${range $index, $column := .table.Columns}
  22. ${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}
  23. }
  24. // ReqEdit 用于存储修改请求参数
  25. type ReqEdit struct {
  26. ${.table.PkColumn.GoField} ${.table.PkColumn.GoType} `p:"${.table.PkColumn.HtmlField}" v:"required#主键ID不能为空"` ${range $index, $column := .table.Columns} ${if eq $column.IsEdit "1"}
  27. ${$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}
  28. }
  29. // RemoveReq 用于存储删除请求参数
  30. type RemoveReq struct {
  31. Ids []int `p:"ids"` //删除id
  32. }
  33. // ReqSearchList 用于存储查询的请求参数
  34. type ReqSearchList struct { ${range $index, $column := .table.Columns} ${if eq $column.IsQuery "1"}
  35. ${$column.GoField} ${if eq $column.GoType "Time"}*gtime.Time${else}${$column.GoType}${end} `p:"${$column.HtmlField}"` //${$column.ColumnComment} ${end} ${end}
  36. }
  37. // GetByID 根据ID查询记录
  38. func GetByID(id int64) (*Entity, error) {
  39. entity, err := Model.FindOne(id)
  40. if err != nil {
  41. g.Log().Error(err)
  42. return nil, gerror.New("根据ID查询记录出错")
  43. }
  44. if entity == nil {
  45. return nil, gerror.New("根据ID未能查询到记录")
  46. }
  47. return entity, nil
  48. }
  49. // AddSave 添加
  50. func AddSave(req *ReqAdd) error {
  51. entity:= new(Entity)
  52. ${range $index, $column := .table.Columns}
  53. ${if and (eq $column.IsInsert "1") (ne $column.IsPk "1")}
  54. entity.${$column.GoField} = req.${$column.GoField}
  55. ${end}
  56. ${end}
  57. result, err := entity.Insert()
  58. if err != nil {
  59. return err
  60. }
  61. _, err = result.LastInsertId()
  62. if err != nil {
  63. return err
  64. }
  65. return nil
  66. }
  67. // DeleteByIds 删除
  68. func DeleteByIds(Ids []int) error {
  69. _, err := Model.Delete("${.table.PkColumn.ColumnName} in(?)", Ids)
  70. if err != nil {
  71. g.Log().Error(err)
  72. return gerror.New("删除失败")
  73. }
  74. return nil
  75. }
  76. // EditSave 根据ID来修改信息
  77. func EditSave(req *ReqEdit) error {
  78. // 先根据ID来查询要修改的记录
  79. entity, err := GetByID(req.${$pkGoField})
  80. if err != nil {
  81. return err
  82. }
  83. // 修改实体
  84. ${range $index, $column := .table.Columns} ${if eq $column.IsEdit "1"}
  85. entity.${$column.GoField} = req.${$column.GoField}${end} ${end}
  86. _, err = entity.Update()
  87. if err != nil {
  88. g.Log().Error(err)
  89. return gerror.New("修改失败")
  90. }
  91. return nil
  92. }
  93. // GetListSearch 列表查询
  94. func GetListSearch(req *ReqSearchList) (list []*Entity, err error) {
  95. list, err = GetList()
  96. if req !=nil{
  97. filterKey := gset.New(false)
  98. tagWhere := true
  99. for key, entity := range list {
  100. ${range $index, $column := .table.Columns}
  101. ${if eq $column.IsQuery "1" }
  102. if tagWhere && gconv.String(req.${$column.GoField}) != "" && entity.${$column.GoField}!= req.${$column.GoField} {
  103. tagWhere = true
  104. }else{
  105. tagWhere = false
  106. }
  107. ${end}
  108. ${end}
  109. if tagWhere{
  110. filterKey.Add(key)
  111. }
  112. }
  113. searchList := make([]*Entity, 0, len(list))
  114. for key, entity := range list {
  115. if !filterKey.Contains(key) {
  116. searchList = append(searchList, entity)
  117. }
  118. }
  119. list = searchList
  120. }
  121. return
  122. }
  123. // GetList 获取列表
  124. func GetList() (list []*Entity, err error) {
  125. cache := cache_service.New()
  126. //从缓存获取数据
  127. iList := cache.Get("${.table.ModuleName}_${.table.BusinessName}_${.table.TableId}")
  128. if iList != nil {
  129. list = iList.([]*Entity)
  130. return
  131. }
  132. list, err = Model.Order(Columns.${$pkGoField}+" ASC").All()
  133. if err != nil {
  134. g.Log().Error()
  135. err = gerror.New("获取数据失败")
  136. return
  137. }
  138. //缓存数据
  139. cache.Set("${.table.ModuleName}_${.table.BusinessName}_${.table.TableId}", list, 0, "${.table.ModuleName}_${.table.BusinessName}_tag")
  140. return
  141. }