model.template 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. type RemoveReq struct {
  30. Ids []int `p:"ids"` //删除id
  31. }
  32. // ReqSearchList 用于存储查询的请求参数
  33. type ReqSearchList struct { {{range $index, $column := .table.Columns}} {{if eq $column.IsQuery "1"}}
  34. {{$column.GoField}} {{if eq $column.GoType "Time"}}*gtime.Time{{else}}{{$column.GoType}}{{end}} `p:"{{$column.HtmlField}}"` //{{$column.ColumnComment}} {{end}} {{end}}
  35. }
  36. // GetPlugAdByID 根据ID查询记录
  37. func GetByID(id int64) (*Entity, error) {
  38. entity, err := Model.FindOne(id)
  39. if err != nil {
  40. g.Log().Error(err)
  41. err = gerror.New("根据ID查询记录出错")
  42. }
  43. if entity == nil {
  44. err = gerror.New("根据ID未能查询到记录")
  45. }
  46. return entity, nil
  47. }
  48. // AddSave 添加
  49. func AddSave(req *ReqAdd) error {
  50. entity:= new(Entity)
  51. {{range $index, $column := .table.Columns}}
  52. {{if and (eq $column.IsInsert "1") (ne $column.IsPk "1")}}
  53. entity.{{$column.GoField}} = req.{{$column.GoField}}
  54. {{end}}
  55. {{end}}
  56. result, err := entity.Insert()
  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. // 删除
  67. func DeleteByIds(Ids []int) 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. // 根据ID来修改信息
  76. func EditSave(req *ReqEdit) 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 = entity.Update()
  86. if err != nil {
  87. g.Log().Error(err)
  88. return gerror.New("修改失败")
  89. }
  90. return nil
  91. }
  92. // 列表查询
  93. func GetListSearch(req *ReqSearchList) (list []*Entity, err error) {
  94. list, err = GetList()
  95. if req !=nil{
  96. filterKey := gset.New(false)
  97. tagWhere := true
  98. for key, entity := range list {
  99. {{range $index, $column := .table.Columns}}
  100. {{if eq $column.IsQuery "1" }}
  101. if tagWhere && gconv.String(req.{{$column.GoField}}) != "" && entity.{{$column.GoField}}!= req.{{$column.GoField}} {
  102. tagWhere = true
  103. }else{
  104. tagWhere = false
  105. }
  106. {{end}}
  107. {{end}}
  108. if tagWhere{
  109. filterKey.Add(key)
  110. }
  111. }
  112. searchList := make([]*Entity, 0, len(list))
  113. for key, entity := range list {
  114. if !filterKey.Contains(key) {
  115. searchList = append(searchList, entity)
  116. }
  117. }
  118. list = searchList
  119. }
  120. return
  121. }
  122. func GetList() (list []*Entity, err error) {
  123. cache := cache_service.New()
  124. //从缓存获取数据
  125. iList := cache.Get("{{.table.ModuleName}}_{{.table.BusinessName}}_{{.table.TableId}}")
  126. if iList != nil {
  127. list = iList.([]*Entity)
  128. return
  129. }
  130. list, err = Model.Order(Columns.{{$pkGoField}}+" ASC").All()
  131. if err != nil {
  132. g.Log().Error()
  133. err = gerror.New("获取数据失败")
  134. return
  135. }
  136. //缓存数据
  137. cache.Set("{{.table.ModuleName}}_{{.table.BusinessName}}_{{.table.TableId}}", list, 0, "{{.table.ModuleName}}_{{.table.BusinessName}}_tag")
  138. return
  139. }