// ========================================================================== // GFast自动生成业务逻辑层相关代码,只生成一次,按需修改,再次生成不会覆盖. // 生成日期:{{.table.CreateTime}} // 生成路径: {{.table.PackageName}}/service/{{.table.BusinessName}}.go // 生成人:{{.table.FunctionAuthor}} // ========================================================================== //// {{$structName := .table.BusinessName | CaseCamelLower}} package service import ( "context" comModel "gfast/app/common/model" "{{.table.PackageName}}/dao" "{{.table.PackageName}}/model" {{if eq .table.TplCategory "tree"}} "github.com/gogf/gf/util/gconv" "gfast/library" {{end}} "github.com/gogf/gf/errors/gerror" "github.com/gogf/gf/frame/g" ) type {{$structName}} struct { } var {{.table.ClassName}} = new({{$structName}}) {{$pk:=""}} {{$pkGoField:=""}} {{$createdAt:=""}} {{$createdAtGoField:=""}} {{range $index, $column := .table.Columns}} {{if eq $column.IsPk "1"}} {{$pk = $column.ColumnName}} {{$pkGoField = $column.GoField}} {{end}} {{if eq $column.ColumnName "created_at"}} {{$createdAt = $column.ColumnName}} {{$createdAtGoField = $column.GoField}} {{end}} {{end}} // GetList 获取任务列表 func (s *{{$structName}}) GetList(req *dao.{{.table.ClassName}}SearchReq) (total, page int, list []*model.{{.table.ClassName}}, err error) { m := dao.{{.table.ClassName}}.Ctx(req.Ctx) {{range $index, $column := .table.Columns}} {{if eq $column.IsQuery "1"}} {{if eq $column.QueryType "LIKE"}} if req.{{$column.GoField}} != "" { m = m.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" like ?", "%"+req.{{$column.GoField}}+"%") } {{end}} {{if eq $column.QueryType "EQ"}} {{if eq $column.GoType "string"}} if req.{{$column.GoField}} != "" { m = m.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" = ?", req.{{$column.GoField}}) } {{else if and (eq $column.GoType "Time") (eq $column.ColumnName "created_at")}} if req.BeginTime != "" { m = m.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" >=", req.BeginTime) } if req.EndTime != "" { m = m.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" <", req.EndTime) } {{else if or (eq $column.GoType "int") (eq $column.GoType "int64") (eq $column.GoType "uint") (eq $column.GoType "uint64") }} if req.{{$column.GoField}} != "" { m = m.Where(dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}+" = ?", req.{{$column.GoField}}) } {{end}} {{end}} {{if and (eq $column.QueryType "BETWEEN") (eq $column.ColumnType "datetime") }} if req.{{$column.GoField}} != nil { 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)) } {{end}} {{end}} {{end}} total, err = m.Count() if err != nil { g.Log().Error(err) err = gerror.New("获取总行数失败") return } {{if ne .table.TplCategory "tree"}} if req.PageNum == 0 { req.PageNum = 1 } page = req.PageNum if req.PageSize == 0 { req.PageSize = comModel.PageSize } order:= "{{$pk}} asc" if req.OrderBy!=""{ order = req.OrderBy } err = m.Page(page, req.PageSize).Order(order).Scan(&list) {{else}} order:= "{{$pk}} asc" if req.OrderBy!=""{ order = req.OrderBy } err = m.Order(order).Scan(&list) {{end}} if err != nil { g.Log().Error(err) err = gerror.New("获取数据失败") } return } // GetInfoById 通过id获取 func (s *{{$structName}}) GetInfoById(ctx context.Context,id int64) (info *model.{{.table.ClassName}}, err error) { if id == 0 { err = gerror.New("参数错误") return } err = dao.{{.table.ClassName}}.Ctx(ctx).Where(dao.{{.table.ClassName}}.Columns.{{$pkGoField}}, id).Scan(&info) if err != nil { g.Log().Error(err) } if info == nil || err != nil { err = gerror.New("获取信息失败") } return } // Add 添加 func (s *{{$structName}}) Add(ctx context.Context,req *dao.{{.table.ClassName}}AddReq) (err error) { _, err = dao.{{.table.ClassName}}.Ctx(ctx).Insert(req) return } // Edit 修改 func (s *{{$structName}}) Edit(ctx context.Context,req *dao.{{.table.ClassName}}EditReq) error { {{ $fieldsEx:= concat "dao." $.table.ClassName ".Columns." $pkGoField }} {{if ne $createdAt ""}} {{$fieldsEx = concat "dao." $.table.ClassName ".Columns." $pkGoField "," "dao." $.table.ClassName ".Columns." $createdAtGoField}} {{end}} _, err := dao.{{.table.ClassName}}.Ctx(ctx).FieldsEx({{$fieldsEx}}).Where(dao.{{.table.ClassName}}.Columns.{{$pkGoField}}, req.{{$pkGoField}}). Update(req) return err } // DeleteByIds 删除 func (s *{{$structName}}) DeleteByIds(ctx context.Context,ids []int) (err error) { if len(ids) == 0 { err = gerror.New("参数错误") return } {{if eq .table.TplCategory "tree"}} ids, err = s.GetChildrenIds(ctx,ids) if err != nil { return } {{end}} _, err = dao.{{.table.ClassName}}.Ctx(ctx).Delete(dao.{{.table.ClassName}}.Columns.{{$pkGoField}}+" in (?)", ids) if err != nil { g.Log().Error(err) err = gerror.New("删除失败") } return } {{range $index,$column:= .table.Columns}} {{if and (HasSuffix $column.ColumnName "status") (eq $column.IsList "1") }} // Change{{$column.GoField}} 修改状态 func (s *{{$structName}}) Change{{$column.GoField}}(ctx context.Context,req *dao.{{$.table.ClassName}}{{$column.GoField}}Req) error { _, err := dao.{{$.table.ClassName}}.Ctx(ctx).WherePri(req.{{$pkGoField}}).Update(g.Map{ dao.{{$.table.ClassName}}.Columns.{{$column.GoField}}: req.{{$column.GoField}}, }) return err } {{end}} {{end}} {{if eq .table.TplCategory "tree"}} // GetChildrenIds 通过ID获取子级ID func (s *{{$structName}})GetChildrenIds(ctx context.Context,ids []int) ([]int, error) { //获取所有 _,_,all, err := s.GetList(&dao.{{.table.ClassName}}SearchReq{PageReq:comModel.PageReq{Ctx: ctx}}) if err != nil { return nil, err } list := make(g.List, len(all)) for k, info := range all { list[k] = gconv.Map(info) } for _, id := range ids { children := library.FindSonByParentId(list, id, "{{.table.TreeParentCode}}", "{{.table.TreeCode}}") for _, cid := range children { ids = append(ids, gconv.Int(cid["{{.table.TreeCode}}"])) } } return ids, nil } {{end}}