package app import ( consts "cid/consts/public" dto "cid/model/dto/app" entity "cid/model/entity/app" "context" "gitea.com/red-future/common/db/gfdb" "github.com/gogf/gf/v2/database/gdb" "github.com/gogf/gf/v2/frame/g" "github.com/gogf/gf/v2/util/gconv" ) var Application = new(applicationDao) type applicationDao struct{} // Insert 插入应用 func (d *applicationDao) Insert(ctx context.Context, req *dto.CreateApplicationReq) (id int64, err error) { var res *entity.Application if err = gconv.Struct(req, &res); err != nil { return } r, err := gfdb.DB(ctx).Model(ctx, consts.ApplicationTable).Data(&res).Insert() if err != nil { return } return r.LastInsertId() } // Update 更新应用 func (d *applicationDao) Update(ctx context.Context, req *dto.UpdateApplicationReq) (rows int64, err error) { r, err := gfdb.DB(ctx).Model(ctx, consts.ApplicationTable).Data(&req).OmitEmpty().Where(entity.ApplicationCols.Id, req.Id).Update() if err != nil { return } return r.RowsAffected() } // Delete 删除应用 func (d *applicationDao) Delete(ctx context.Context, req *dto.DeleteApplicationReq) (rows int64, err error) { r, err := gfdb.DB(ctx).Model(ctx, consts.ApplicationTable).Where(entity.ApplicationCols.Id, req.Id).Delete() if err != nil { return } return r.RowsAffected() } // GetOne 获取单个应用 func (d *applicationDao) GetOne(ctx context.Context, req *dto.GetApplicationReq) (res *entity.Application, err error) { r, err := gfdb.DB(ctx).Model(ctx, consts.ApplicationTable).Where(entity.ApplicationCols.Id, req.Id).One() if err != nil { return } err = r.Struct(&res) return } // Count 获取应用数量 func (d *applicationDao) Count(ctx context.Context, req *dto.ListApplicationReq) (count int, err error) { return d.buildListFilter(ctx, req).Count() } // List 获取应用列表 func (d *applicationDao) List(ctx context.Context, req *dto.ListApplicationReq) (res []entity.Application, total int, err error) { model := d.buildListFilter(ctx, req) model.OrderDesc(entity.ApplicationCols.CreatedAt) if req.Page != nil { model.Page(int(req.Page.PageNum), int(req.Page.PageSize)) } r, total, err := model.AllAndCount(false) if err != nil { return } err = r.Structs(&res) return } // buildListFilter 构建列表查询的过滤条件 func (d *applicationDao) buildListFilter(ctx context.Context, req *dto.ListApplicationReq) *gdb.Model { model := gfdb.DB(ctx).Model(ctx, consts.ApplicationTable).Model if !g.IsEmpty(req.Keyword) { model.WhereLike(entity.ApplicationCols.Name, "%"+req.Keyword+"%") model.WhereOrLike(entity.ApplicationCols.AppCode, "%"+req.Keyword+"%") } model.Where(entity.ApplicationCols.Name, req.Name) model.Where(entity.ApplicationCols.AppCode, req.AppCode) model.Where(entity.ApplicationCols.Type, req.Type) model.Where(entity.ApplicationCols.Status, req.Status) model.OmitEmptyWhere() return model } // UpdateStatus 更新应用状态 func (d *applicationDao) UpdateStatus(ctx context.Context, id int64, status string) (rows int64, err error) { r, err := gfdb.DB(ctx).Model(ctx, consts.ApplicationTable). Data(map[string]interface{}{"status": status}). Where(entity.ApplicationCols.Id, id). Update() if err != nil { return } return r.RowsAffected() } // GetByAppCode 根据应用编码获取应用 func (d *applicationDao) GetByAppCode(ctx context.Context, appCode string) (res *entity.Application, err error) { r, err := gfdb.DB(ctx).Model(ctx, consts.ApplicationTable). Where(entity.ApplicationCols.AppCode, appCode). One() if err != nil { return } err = r.Struct(&res) return }