service.template 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // ==========================================================================
  2. // GFast自动生成业务逻辑层相关代码,只生成一次,按需修改,再次生成不会覆盖.
  3. // 生成日期:{{.table.CreateTime}}
  4. // 生成路径: {{.table.PackageName}}/service/{{.table.BusinessName}}.go
  5. // 生成人:{{.table.FunctionAuthor}}
  6. // ==========================================================================
  7. ////
  8. {{$structName := .table.BusinessName | CaseCamelLower}}
  9. package service
  10. import (
  11. "context"
  12. comModel "gfast/app/common/model"
  13. "{{.table.PackageName}}/dao"
  14. "{{.table.PackageName}}/model"
  15. {{if eq .table.TplCategory "tree"}}
  16. "github.com/gogf/gf/util/gconv"
  17. "gfast/library"
  18. {{end}}
  19. "github.com/gogf/gf/errors/gerror"
  20. "github.com/gogf/gf/frame/g"
  21. )
  22. type {{$structName}} struct {
  23. }
  24. var {{.table.ClassName}} = new({{$structName}})
  25. {{$pk:=""}}
  26. {{$pkGoField:=""}}
  27. {{$createdAt:=""}}
  28. {{$createdAtGoField:=""}}
  29. {{range $index, $column := .table.Columns}}
  30. {{if eq $column.IsPk "1"}}
  31. {{$pk = $column.ColumnName}}
  32. {{$pkGoField = $column.GoField}}
  33. {{end}}
  34. {{if eq $column.ColumnName "created_at"}}
  35. {{$createdAt = $column.ColumnName}}
  36. {{$createdAtGoField = $column.GoField}}
  37. {{end}}
  38. {{end}}
  39. // GetList 获取任务列表
  40. func (s *{{$structName}}) GetList(req *dao.{{.table.ClassName}}SearchReq) (total, page int, list []*model.{{.table.ClassName}}, err error) {
  41. m := dao.{{.table.ClassName}}.Ctx(req.Ctx)
  42. {{range $index, $column := .table.Columns}} {{if eq $column.IsQuery "1"}}
  43. {{if eq $column.QueryType "LIKE"}}
  44. if req.{{$column.GoField}} != "" {
  45. m = m.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" like ?", "%"+req.{{$column.GoField}}+"%")
  46. } {{end}}
  47. {{if eq $column.QueryType "EQ"}} {{if eq $column.GoType "string"}}
  48. if req.{{$column.GoField}} != "" {
  49. m = m.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" = ?", req.{{$column.GoField}})
  50. }
  51. {{else if and (eq $column.GoType "Time") (eq $column.ColumnName "created_at")}}
  52. if req.BeginTime != "" {
  53. m = m.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" >=", req.BeginTime)
  54. }
  55. if req.EndTime != "" {
  56. m = m.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" <", req.EndTime)
  57. }
  58. {{else if or (eq $column.GoType "int") (eq $column.GoType "int64") (eq $column.GoType "uint") (eq $column.GoType "uint64") }}
  59. if req.{{$column.GoField}} != "" {
  60. m = m.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" = ?", req.{{$column.GoField}})
  61. }
  62. {{end}} {{end}}
  63. {{if and (eq $column.QueryType "BETWEEN") (eq $column.ColumnType "datetime") }}
  64. if req.{{$column.GoField}} != nil {
  65. m = m.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" >= ? AND "+dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" < ?", req.{{$column.GoField}}, req.{{$column.GoField}}.Add(gtime.D))
  66. }
  67. {{end}}
  68. {{end}}
  69. {{end}}
  70. total, err = m.Count()
  71. if err != nil {
  72. g.Log().Error(err)
  73. err = gerror.New("获取总行数失败")
  74. return
  75. }
  76. {{if ne .table.TplCategory "tree"}}
  77. if req.PageNum == 0 {
  78. req.PageNum = 1
  79. }
  80. page = req.PageNum
  81. if req.PageSize == 0 {
  82. req.PageSize = comModel.PageSize
  83. }
  84. order:= "{{$pk}} asc"
  85. if req.OrderBy!=""{
  86. order = req.OrderBy
  87. }
  88. err = m.Page(page, req.PageSize).Order(order).Scan(&list)
  89. {{else}}
  90. order:= "{{$pk}} asc"
  91. if req.OrderBy!=""{
  92. order = req.OrderBy
  93. }
  94. err = m.Order(order).Scan(&list)
  95. {{end}}
  96. if err != nil {
  97. g.Log().Error(err)
  98. err = gerror.New("获取数据失败")
  99. }
  100. return
  101. }
  102. // GetInfoById 通过id获取
  103. func (s *{{$structName}}) GetInfoById(ctx context.Context,id int64) (info *model.{{.table.ClassName}}, err error) {
  104. if id == 0 {
  105. err = gerror.New("参数错误")
  106. return
  107. }
  108. err = dao.{{.table.ClassName}}.Ctx(ctx).Where(dao.{{.table.ClassName}}.Columns.{{$pkGoField}}, id).Scan(&info)
  109. if err != nil {
  110. g.Log().Error(err)
  111. }
  112. if info == nil || err != nil {
  113. err = gerror.New("获取信息失败")
  114. }
  115. return
  116. }
  117. // Add 添加
  118. func (s *{{$structName}}) Add(ctx context.Context,req *dao.{{.table.ClassName}}AddReq) (err error) {
  119. _, err = dao.{{.table.ClassName}}.Ctx(ctx).Insert(req)
  120. return
  121. }
  122. // Edit 修改
  123. func (s *{{$structName}}) Edit(ctx context.Context,req *dao.{{.table.ClassName}}EditReq) error {
  124. {{ $fieldsEx:= concat "dao." $.table.ClassName ".Columns." $pkGoField }}
  125. {{if ne $createdAt ""}}
  126. {{$fieldsEx = concat "dao." $.table.ClassName ".Columns." $pkGoField "," "dao." $.table.ClassName ".Columns." $createdAtGoField}}
  127. {{end}}
  128. _, err := dao.{{.table.ClassName}}.Ctx(ctx).FieldsEx({{$fieldsEx}}).Where(dao.{{.table.ClassName}}.Columns.{{$pkGoField}}, req.{{$pkGoField}}).
  129. Update(req)
  130. return err
  131. }
  132. // DeleteByIds 删除
  133. func (s *{{$structName}}) DeleteByIds(ctx context.Context,ids []int) (err error) {
  134. if len(ids) == 0 {
  135. err = gerror.New("参数错误")
  136. return
  137. }
  138. {{if eq .table.TplCategory "tree"}}
  139. ids, err = s.GetChildrenIds(ctx,ids)
  140. if err != nil {
  141. return
  142. }
  143. {{end}}
  144. _, err = dao.{{.table.ClassName}}.Ctx(ctx).Delete(dao.{{.table.ClassName}}.Columns.{{$pkGoField}}+" in (?)", ids)
  145. if err != nil {
  146. g.Log().Error(err)
  147. err = gerror.New("删除失败")
  148. }
  149. return
  150. }
  151. {{range $index,$column:= .table.Columns}}
  152. {{if and (HasSuffix $column.ColumnName "status") (eq $column.IsList "1") }}
  153. // Change{{$column.GoField}} 修改状态
  154. func (s *{{$structName}}) Change{{$column.GoField}}(ctx context.Context,req *dao.{{$.table.ClassName}}{{$column.GoField}}Req) error {
  155. _, err := dao.{{$.table.ClassName}}.Ctx(ctx).WherePri(req.{{$pkGoField}}).Update(g.Map{
  156. dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}: req.{{$column.GoField}},
  157. })
  158. return err
  159. }
  160. {{end}}
  161. {{end}}
  162. {{if eq .table.TplCategory "tree"}}
  163. // GetChildrenIds 通过ID获取子级ID
  164. func (s *{{$structName}})GetChildrenIds(ctx context.Context,ids []int) ([]int, error) {
  165. //获取所有
  166. _,_,all, err := s.GetList(&dao.{{.table.ClassName}}SearchReq{PageReq:comModel.PageReq{Ctx: ctx}})
  167. if err != nil {
  168. return nil, err
  169. }
  170. list := make(g.List, len(all))
  171. for k, info := range all {
  172. list[k] = gconv.Map(info)
  173. }
  174. for _, id := range ids {
  175. children := library.FindSonByParentId(list, id, "{{.table.TreeParentCode}}", "{{.table.TreeCode}}")
  176. for _, cid := range children {
  177. ids = append(ids, gconv.Int(cid["{{.table.TreeCode}}"]))
  178. }
  179. }
  180. return ids, nil
  181. }
  182. {{end}}