package data import ( "context" consts "erp/consts/public" dto "erp/model/dto/data" entity "erp/model/entity/data" "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 LiveAccount = new(liveAccountDao) type liveAccountDao struct{} // Insert 插入直播账号 func (d *liveAccountDao) Insert(ctx context.Context, req *dto.CreateLiveAccountReq) (id int64, err error) { var res *entity.LiveAccount if err = gconv.Struct(req, &res); err != nil { return } r, err := gfdb.DB(ctx).Model(ctx, consts.LiveAccountTable).Data(&res).Insert() if err != nil { return } return r.LastInsertId() } // Update 更新直播账号 func (d *liveAccountDao) Update(ctx context.Context, req *dto.UpdateLiveAccountReq) (rows int64, err error) { r, err := gfdb.DB(ctx).Model(ctx, consts.LiveAccountTable).Data(&req).OmitEmpty().Where(entity.LiveAccountCols.Id, req.Id).Update() if err != nil { return } return r.RowsAffected() } // Delete 删除直播账号 func (d *liveAccountDao) Delete(ctx context.Context, req *dto.DeleteLiveAccountReq) (rows int64, err error) { r, err := gfdb.DB(ctx).Model(ctx, consts.LiveAccountTable).Where(entity.LiveAccountCols.Id, req.Id).Delete() if err != nil { return } return r.RowsAffected() } // GetOne 获取单个直播账号 func (d *liveAccountDao) GetOne(ctx context.Context, req *dto.GetLiveAccountReq) (res *entity.LiveAccount, err error) { r, err := gfdb.DB(ctx).Model(ctx, consts.LiveAccountTable).Where(entity.LiveAccountCols.Id, req.Id).One() if err != nil { return } err = r.Struct(&res) return } // Count 获取直播账号数量 func (d *liveAccountDao) Count(ctx context.Context, req *dto.ListLiveAccountReq) (count int, err error) { return d.buildListFilter(ctx, req).Count() } // List 获取直播账号列表 func (d *liveAccountDao) List(ctx context.Context, req *dto.ListLiveAccountReq) (res []entity.LiveAccount, total int, err error) { model := d.buildListFilter(ctx, req) model.OrderDesc(entity.LiveAccountCols.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 *liveAccountDao) buildListFilter(ctx context.Context, req *dto.ListLiveAccountReq) *gdb.Model { model := gfdb.DB(ctx).Model(ctx, consts.LiveAccountTable).Model if !g.IsEmpty(req.Keyword) { model.WhereLike(entity.LiveAccountCols.Platform, "%"+req.Keyword+"%"). WhereOrLike(entity.LiveAccountCols.AccountName, "%"+req.Keyword+"%"). WhereOrLike(entity.LiveAccountCols.AccountId, "%"+req.Keyword+"%") } model.Where(entity.LiveAccountCols.Platform, req.Platform) model.Where(entity.LiveAccountCols.AccountName, req.AccountName) model.Where(entity.LiveAccountCols.AccountId, req.AccountId) if req.Status != nil { model.Where(entity.LiveAccountCols.Status, *req.Status) } model.OmitEmptyWhere() return model } // UpdateStatus 更新直播账号状态 func (d *liveAccountDao) UpdateStatus(ctx context.Context, id int64, status int) (rows int64, err error) { r, err := gfdb.DB(ctx).Model(ctx, consts.LiveAccountTable). Data(map[string]interface{}{"status": status}). Where(entity.LiveAccountCols.Id, id). Update() if err != nil { return } return r.RowsAffected() } // GetByAccountId 根据账号ID获取直播账号 func (d *liveAccountDao) GetByAccountId(ctx context.Context, accountId string) (res *entity.LiveAccount, err error) { r, err := gfdb.DB(ctx).Model(ctx, consts.LiveAccountTable). Where(entity.LiveAccountCols.AccountId, accountId). One() if err != nil { return } err = r.Struct(&res) return }