service.template 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. {{$gjson:=false}}
  11. {{range $index, $column := .table.Columns}}
  12. {{if eq $column.HtmlType "images" "file" "files"}}
  13. {{$gjson = true}}
  14. {{end}}
  15. {{end}}
  16. import (
  17. "context"
  18. comModel "gfast/app/common/model"
  19. "{{.table.PackageName}}/dao"
  20. "{{.table.PackageName}}/model"
  21. {{if eq .table.TplCategory "tree"}}
  22. "github.com/gogf/gf/util/gconv"
  23. "gfast/library"
  24. {{end}}
  25. {{if $gjson}}
  26. "github.com/gogf/gf/encoding/gjson"
  27. {{end}}
  28. "github.com/gogf/gf/errors/gerror"
  29. "github.com/gogf/gf/frame/g"
  30. )
  31. type {{$structName}} struct {
  32. }
  33. var {{.table.ClassName}} = new({{$structName}})
  34. {{$pk:=""}}
  35. {{$pkGoField:=""}}
  36. {{$createdAt:=""}}
  37. {{$createdAtGoField:=""}}
  38. {{range $index, $column := .table.Columns}}
  39. {{if eq $column.IsPk "1"}}
  40. {{$pk = $column.ColumnName}}
  41. {{$pkGoField = $column.GoField}}
  42. {{end}}
  43. {{if eq $column.ColumnName "created_at"}}
  44. {{$createdAt = $column.ColumnName}}
  45. {{$createdAtGoField = $column.GoField}}
  46. {{end}}
  47. {{end}}
  48. // GetList 获取列表
  49. func (s *{{$structName}}) GetList(req *dao.{{.table.ClassName}}SearchReq) (total, page int, list []*dao.{{.table.ClassName}}ListRes, err error) {
  50. m := dao.{{.table.ClassName}}.Ctx(req.Ctx)
  51. {{range $index, $column := .table.Columns}} {{if eq $column.IsQuery "1"}}
  52. {{if eq $column.QueryType "LIKE"}}
  53. if req.{{$column.GoField}} != "" {
  54. m = m.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" like ?", "%"+req.{{$column.GoField}}+"%")
  55. } {{end}}
  56. {{if eq $column.QueryType "EQ"}} {{if eq $column.GoType "string"}}
  57. if req.{{$column.GoField}} != "" {
  58. m = m.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" = ?", req.{{$column.GoField}})
  59. }
  60. {{else if and (eq $column.GoType "Time") (eq $column.ColumnName "created_at")}}
  61. if req.BeginTime != "" {
  62. m = m.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" >=", req.BeginTime)
  63. }
  64. if req.EndTime != "" {
  65. m = m.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" <", req.EndTime)
  66. }
  67. {{else if or (eq $column.GoType "int") (eq $column.GoType "int64") (eq $column.GoType "uint") (eq $column.GoType "uint64") }}
  68. if req.{{$column.GoField}} != "" {
  69. m = m.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" = ?", req.{{$column.GoField}})
  70. }
  71. {{end}} {{end}}
  72. {{if and (eq $column.QueryType "BETWEEN") (eq $column.ColumnType "datetime") }}
  73. if req.{{$column.GoField}} != nil {
  74. 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))
  75. }
  76. {{end}}
  77. {{end}}
  78. {{end}}
  79. total, err = m.Count()
  80. if err != nil {
  81. g.Log().Error(err)
  82. err = gerror.New("获取总行数失败")
  83. return
  84. }
  85. {{if ne .table.TplCategory "tree"}}
  86. if req.PageNum == 0 {
  87. req.PageNum = 1
  88. }
  89. page = req.PageNum
  90. if req.PageSize == 0 {
  91. req.PageSize = comModel.PageSize
  92. }
  93. order:= "{{$pk}} asc"
  94. if req.OrderBy!=""{
  95. order = req.OrderBy
  96. }
  97. var res []*model.{{.table.ClassName}}
  98. err = m.Fields(dao.{{.table.ClassName}}ListRes{}).Page(page, req.PageSize).Order(order).Scan(&res)
  99. {{else}}
  100. order:= "{{$pk}} asc"
  101. if req.OrderBy!=""{
  102. order = req.OrderBy
  103. }
  104. var res []*model.{{.table.ClassName}}
  105. err = m.Fields(dao.{{.table.ClassName}}ListRes{}).Order(order).Scan(&res)
  106. {{end}}
  107. if err != nil {
  108. g.Log().Error(err)
  109. err = gerror.New("获取数据失败")
  110. }
  111. list = make([]*dao.{{.table.ClassName}}ListRes,len(res))
  112. for k,v:=range res{
  113. {{range $index, $column := .table.Columns}}
  114. {{if and (eq $column.IsList "1") (eq $column.HtmlType "images" "file" "files")}}
  115. {{$column.HtmlField}}:= ([]*comModel.UpFile)(nil)
  116. err = gjson.DecodeTo(v.{{$column.GoField}},&{{$column.HtmlField}})
  117. if err!=nil{
  118. return
  119. }
  120. {{end}}
  121. {{end}}
  122. list[k] = &dao.{{.table.ClassName}}ListRes{
  123. {{range $index, $column := .table.Columns}}
  124. {{if eq $column.IsList "1"}}
  125. {{if eq $column.HtmlType "images" "file" "files"}}
  126. {{$column.GoField}} : {{$column.HtmlField}},
  127. {{else}}
  128. {{$column.GoField}} : v.{{$column.GoField}},
  129. {{end}}
  130. {{end}}
  131. {{end}}
  132. }
  133. }
  134. return
  135. }
  136. // GetInfoById 通过id获取
  137. func (s *{{$structName}}) GetInfoById(ctx context.Context,id int64) (info *dao.{{.table.ClassName}}InfoRes, err error) {
  138. if id == 0 {
  139. err = gerror.New("参数错误")
  140. return
  141. }
  142. var data *model.{{.table.ClassName}}
  143. err = dao.{{.table.ClassName}}.Ctx(ctx).Where(dao.{{.table.ClassName}}.Columns.{{$pkGoField}}, id).Scan(&data)
  144. if err != nil {
  145. g.Log().Error(err)
  146. }
  147. if data == nil || err != nil {
  148. err = gerror.New("获取信息失败")
  149. }
  150. {{range $index, $column := .table.Columns}}
  151. {{if and (eq $column.IsList "1") (eq $column.HtmlType "images" "file" "files")}}
  152. {{$column.HtmlField}}:= ([]*comModel.UpFile)(nil)
  153. err = gjson.DecodeTo(data.{{$column.GoField}},&{{$column.HtmlField}})
  154. if err!=nil{
  155. return
  156. }
  157. {{end}}
  158. {{end}}
  159. info = &dao.{{.table.ClassName}}InfoRes{
  160. {{range $index, $column := .table.Columns}}
  161. {{if and (eq $column.IsList "1") (eq $column.HtmlType "images" "file" "files")}}
  162. {{$column.GoField}} : {{$column.HtmlField}},
  163. {{else}}
  164. {{$column.GoField}} : data.{{$column.GoField}},
  165. {{end}}
  166. {{end}}
  167. }
  168. return
  169. }
  170. // Add 添加
  171. func (s *{{$structName}}) Add(ctx context.Context,req *dao.{{.table.ClassName}}AddReq) (err error) {
  172. _, err = dao.{{.table.ClassName}}.Ctx(ctx).Insert(req)
  173. return
  174. }
  175. // Edit 修改
  176. func (s *{{$structName}}) Edit(ctx context.Context,req *dao.{{.table.ClassName}}EditReq) error {
  177. {{ $fieldsEx:= concat "dao." $.table.ClassName ".Columns." $pkGoField }}
  178. {{if ne $createdAt ""}}
  179. {{$fieldsEx = concat "dao." $.table.ClassName ".Columns." $pkGoField "," "dao." $.table.ClassName ".Columns." $createdAtGoField}}
  180. {{end}}
  181. _, err := dao.{{.table.ClassName}}.Ctx(ctx).FieldsEx({{$fieldsEx}}).Where(dao.{{.table.ClassName}}.Columns.{{$pkGoField}}, req.{{$pkGoField}}).
  182. Update(req)
  183. return err
  184. }
  185. // DeleteByIds 删除
  186. func (s *{{$structName}}) DeleteByIds(ctx context.Context,ids []int) (err error) {
  187. if len(ids) == 0 {
  188. err = gerror.New("参数错误")
  189. return
  190. }
  191. {{if eq .table.TplCategory "tree"}}
  192. ids, err = s.GetChildrenIds(ctx,ids)
  193. if err != nil {
  194. return
  195. }
  196. {{end}}
  197. _, err = dao.{{.table.ClassName}}.Ctx(ctx).Delete(dao.{{.table.ClassName}}.Columns.{{$pkGoField}}+" in (?)", ids)
  198. if err != nil {
  199. g.Log().Error(err)
  200. err = gerror.New("删除失败")
  201. }
  202. return
  203. }
  204. {{range $index,$column:= .table.Columns}}
  205. {{if and (HasSuffix $column.ColumnName "status") (eq $column.IsList "1") }}
  206. // Change{{$column.GoField}} 修改状态
  207. func (s *{{$structName}}) Change{{$column.GoField}}(ctx context.Context,req *dao.{{$.table.ClassName}}{{$column.GoField}}Req) error {
  208. _, err := dao.{{$.table.ClassName}}.Ctx(ctx).WherePri(req.{{$pkGoField}}).Update(g.Map{
  209. dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}: req.{{$column.GoField}},
  210. })
  211. return err
  212. }
  213. {{end}}
  214. {{end}}
  215. {{if eq .table.TplCategory "tree"}}
  216. // GetChildrenIds 通过ID获取子级ID
  217. func (s *{{$structName}})GetChildrenIds(ctx context.Context,ids []int) ([]int, error) {
  218. //获取所有
  219. _,_,all, err := s.GetList(&dao.{{.table.ClassName}}SearchReq{PageReq:comModel.PageReq{Ctx: ctx}})
  220. if err != nil {
  221. return nil, err
  222. }
  223. list := make(g.List, len(all))
  224. for k, info := range all {
  225. list[k] = gconv.Map(info)
  226. }
  227. for _, id := range ids {
  228. children := library.FindSonByParentId(list, id, "{{.table.TreeParentCode}}", "{{.table.TreeCode}}")
  229. for _, cid := range children {
  230. ids = append(ids, gconv.Int(cid["{{.table.TreeCode}}"]))
  231. }
  232. }
  233. return ids, nil
  234. }
  235. {{end}}