service.template 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. if req != nil {
  52. {{range $index, $column := .table.Columns}} {{if eq $column.IsQuery "1"}}
  53. {{if eq $column.QueryType "LIKE"}}
  54. if req.{{$column.GoField}} != "" {
  55. m = m.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" like ?", "%"+req.{{$column.GoField}}+"%")
  56. } {{end}}
  57. {{if eq $column.QueryType "EQ"}} {{if eq $column.GoType "string"}}
  58. if req.{{$column.GoField}} != "" {
  59. m = m.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" = ?", req.{{$column.GoField}})
  60. }
  61. {{else if and (eq $column.GoType "Time") (eq $column.ColumnName "created_at")}}
  62. if req.BeginTime != "" {
  63. m = m.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" >=", req.BeginTime)
  64. }
  65. if req.EndTime != "" {
  66. m = m.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" <", req.EndTime)
  67. }
  68. {{else if or (eq $column.GoType "int") (eq $column.GoType "int64") (eq $column.GoType "uint") (eq $column.GoType "uint64") }}
  69. if req.{{$column.GoField}} != "" {
  70. m = m.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" = ?", req.{{$column.GoField}})
  71. }
  72. {{end}} {{end}}
  73. {{if and (eq $column.QueryType "BETWEEN") (eq $column.ColumnType "datetime") }}
  74. if req.{{$column.GoField}} != nil {
  75. 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))
  76. }
  77. {{end}}
  78. {{end}}
  79. {{end}}
  80. }
  81. total, err = m.Count()
  82. if err != nil {
  83. g.Log().Error(err)
  84. err = gerror.New("获取总行数失败")
  85. return
  86. }
  87. {{if ne .table.TplCategory "tree"}}
  88. if req.PageNum == 0 {
  89. req.PageNum = 1
  90. }
  91. page = req.PageNum
  92. if req.PageSize == 0 {
  93. req.PageSize = comModel.PageSize
  94. }
  95. order:= "{{$pk}} asc"
  96. if req.OrderBy!=""{
  97. order = req.OrderBy
  98. }
  99. var res []*model.{{.table.ClassName}}
  100. err = m.Fields(dao.{{.table.ClassName}}ListRes{}).Page(page, req.PageSize).Order(order).Scan(&res)
  101. {{else}}
  102. order:= "{{$pk}} asc"
  103. if req.OrderBy!=""{
  104. order = req.OrderBy
  105. }
  106. var res []*model.{{.table.ClassName}}
  107. err = m.Fields(dao.{{.table.ClassName}}ListRes{}).Order(order).Scan(&res)
  108. {{end}}
  109. if err != nil {
  110. g.Log().Error(err)
  111. err = gerror.New("获取数据失败")
  112. }
  113. list = make([]*dao.{{.table.ClassName}}ListRes,len(res))
  114. for k,v:=range res{
  115. {{range $index, $column := .table.Columns}}
  116. {{if and (eq $column.IsList "1") (eq $column.HtmlType "images" "file" "files")}}
  117. {{$column.HtmlField}}:= ([]*comModel.UpFile)(nil)
  118. err = gjson.DecodeTo(v.{{$column.GoField}},&{{$column.HtmlField}})
  119. if err!=nil{
  120. return
  121. }
  122. {{end}}
  123. {{end}}
  124. list[k] = &dao.{{.table.ClassName}}ListRes{
  125. {{range $index, $column := .table.Columns}}
  126. {{if and (eq $column.IsList "1") (eq $column.HtmlType "images" "file" "files")}}
  127. {{$column.GoField}} : {{$column.HtmlField}},
  128. {{else}}
  129. {{$column.GoField}} : v.{{$column.GoField}},
  130. {{end}}
  131. {{end}}
  132. }
  133. }
  134. return
  135. }
  136. // GetInfoById 通过id获取
  137. func (s *{{$structName}}) GetInfoById(id int64,ctx context.Context) (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(req *dao.{{.table.ClassName}}AddReq,ctx context.Context) (err error) {
  172. _, err = dao.{{.table.ClassName}}.Ctx(ctx).Insert(req)
  173. return
  174. }
  175. // Edit 修改
  176. func (s *{{$structName}}) Edit(req *dao.{{.table.ClassName}}EditReq,ctx context.Context) 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(ids []int,ctx context.Context) (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(ids,ctx)
  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}}(req *dao.{{$.table.ClassName}}{{$column.GoField}}Req,ctx context.Context) 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(ids []int,ctx context.Context) ([]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}}