service.template 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. {{if eq .table.TplCategory "tree"}}
  124. {{range $index, $column := .table.Columns}}
  125. {{if or (eq $column.HtmlField $.table.TreeCode) (eq $column.HtmlField $.table.TreeParentCode) (eq $column.HtmlField $.table.TreeName) }}
  126. {{$column.GoField}} : v.{{$column.GoField}},
  127. {{end}}
  128. {{end}}
  129. {{range $index, $column := .table.Columns}}
  130. {{if and (eq $column.IsList "1") (ne $column.HtmlField $.table.TreeCode) (ne $column.HtmlField $.table.TreeParentCode) (ne $column.HtmlField $.table.TreeName) }}
  131. {{if eq $column.HtmlType "images" "file" "files"}}
  132. {{$column.GoField}} : {{$column.HtmlField}},
  133. {{else}}
  134. {{$column.GoField}} : v.{{$column.GoField}},
  135. {{end}}
  136. {{end}}
  137. {{end}}
  138. {{else}}
  139. {{range $index, $column := .table.Columns}}
  140. {{if or (eq $column.IsList "1") (eq $column.IsPk "1")}}
  141. {{if eq $column.HtmlType "images" "file" "files"}}
  142. {{$column.GoField}} : {{$column.HtmlField}},
  143. {{else}}
  144. {{$column.GoField}} : v.{{$column.GoField}},
  145. {{end}}
  146. {{end}}
  147. {{end}}
  148. {{end}}
  149. }
  150. }
  151. return
  152. }
  153. // GetInfoById 通过id获取
  154. func (s *{{$structName}}) GetInfoById(ctx context.Context,id {{$.table.PkColumn.GoType}}) (info *dao.{{.table.ClassName}}InfoRes, err error) {
  155. if id == 0 {
  156. err = gerror.New("参数错误")
  157. return
  158. }
  159. var data *model.{{.table.ClassName}}
  160. err = dao.{{.table.ClassName}}.Ctx(ctx).Where(dao.{{.table.ClassName}}.Columns.{{$pkGoField}}, id).Scan(&data)
  161. if err != nil {
  162. g.Log().Error(err)
  163. }
  164. if data == nil || err != nil {
  165. err = gerror.New("获取信息失败")
  166. return
  167. }
  168. {{range $index, $column := .table.Columns}}
  169. {{if eq $column.HtmlType "images" "file" "files"}}
  170. {{$column.HtmlField}}:= ([]*comModel.UpFile)(nil)
  171. err = gjson.DecodeTo(data.{{$column.GoField}},&{{$column.HtmlField}})
  172. if err!=nil{
  173. return
  174. }
  175. {{end}}
  176. {{end}}
  177. info = &dao.{{.table.ClassName}}InfoRes{
  178. {{range $index, $column := .table.Columns}}
  179. {{if eq $column.HtmlType "images" "file" "files"}}
  180. {{$column.GoField}} : {{$column.HtmlField}},
  181. {{else}}
  182. {{$column.GoField}} : data.{{$column.GoField}},
  183. {{end}}
  184. {{end}}
  185. }
  186. return
  187. }
  188. // Add 添加
  189. func (s *{{$structName}}) Add(ctx context.Context,req *dao.{{.table.ClassName}}AddReq) (err error) {
  190. _, err = dao.{{.table.ClassName}}.Ctx(ctx).Insert(req)
  191. return
  192. }
  193. // Edit 修改
  194. func (s *{{$structName}}) Edit(ctx context.Context,req *dao.{{.table.ClassName}}EditReq) error {
  195. {{ $fieldsEx:= concat "dao." $.table.ClassName ".Columns." $pkGoField }}
  196. {{if ne $createdAt ""}}
  197. {{$fieldsEx = concat "dao." $.table.ClassName ".Columns." $pkGoField "," "dao." $.table.ClassName ".Columns." $createdAtGoField}}
  198. {{end}}
  199. _, err := dao.{{.table.ClassName}}.Ctx(ctx).FieldsEx({{$fieldsEx}}).Where(dao.{{.table.ClassName}}.Columns.{{$pkGoField}}, req.{{$pkGoField}}).
  200. Update(req)
  201. return err
  202. }
  203. // DeleteByIds 删除
  204. func (s *{{$structName}}) DeleteByIds(ctx context.Context,ids []int) (err error) {
  205. if len(ids) == 0 {
  206. err = gerror.New("参数错误")
  207. return
  208. }
  209. {{if eq .table.TplCategory "tree"}}
  210. ids, err = s.GetChildrenIds(ctx,ids)
  211. if err != nil {
  212. return
  213. }
  214. {{end}}
  215. _, err = dao.{{.table.ClassName}}.Ctx(ctx).Delete(dao.{{.table.ClassName}}.Columns.{{$pkGoField}}+" in (?)", ids)
  216. if err != nil {
  217. g.Log().Error(err)
  218. err = gerror.New("删除失败")
  219. }
  220. return
  221. }
  222. {{range $index,$column:= .table.Columns}}
  223. {{if and (HasSuffix $column.ColumnName "status") (eq $column.IsList "1") }}
  224. // Change{{$column.GoField}} 修改状态
  225. func (s *{{$structName}}) Change{{$column.GoField}}(ctx context.Context,req *dao.{{$.table.ClassName}}{{$column.GoField}}Req) error {
  226. _, err := dao.{{$.table.ClassName}}.Ctx(ctx).WherePri(req.{{$pkGoField}}).Update(g.Map{
  227. dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}: req.{{$column.GoField}},
  228. })
  229. return err
  230. }
  231. {{end}}
  232. {{end}}
  233. {{if eq .table.TplCategory "tree"}}
  234. // GetChildrenIds 通过ID获取子级ID
  235. func (s *{{$structName}})GetChildrenIds(ctx context.Context,ids []int) ([]int, error) {
  236. //获取所有
  237. _,_,all, err := s.GetList(&dao.{{.table.ClassName}}SearchReq{PageReq:comModel.PageReq{Ctx: ctx}})
  238. if err != nil {
  239. return nil, err
  240. }
  241. list := make(g.List, len(all))
  242. for k, info := range all {
  243. list[k] = gconv.Map(info)
  244. }
  245. for _, id := range ids {
  246. children := library.FindSonByParentId(list, id, "{{.table.TreeParentCode}}", "{{.table.TreeCode}}")
  247. for _, cid := range children {
  248. ids = append(ids, gconv.Int(cid["{{.table.TreeCode}}"]))
  249. }
  250. }
  251. return ids, nil
  252. }
  253. {{end}}