116 lines
3.3 KiB
Go
116 lines
3.3 KiB
Go
|
|
package data
|
||
|
|
|
||
|
|
import (
|
||
|
|
consts "cid/consts/public"
|
||
|
|
dto "cid/model/dto/data"
|
||
|
|
entity "cid/model/entity/data"
|
||
|
|
"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 Platform = new(platformDao)
|
||
|
|
|
||
|
|
type platformDao struct{}
|
||
|
|
|
||
|
|
// Insert 插入平台
|
||
|
|
func (d *platformDao) Insert(ctx context.Context, req *dto.CreatePlatformReq) (id int64, err error) {
|
||
|
|
var res *entity.Platform
|
||
|
|
if err = gconv.Struct(req, &res); err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
r, err := gfdb.DB(ctx).Model(ctx, consts.PlatformTable).Data(&res).Insert()
|
||
|
|
if err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
return r.LastInsertId()
|
||
|
|
}
|
||
|
|
|
||
|
|
// Update 更新平台
|
||
|
|
func (d *platformDao) Update(ctx context.Context, req *dto.UpdatePlatformReq) (rows int64, err error) {
|
||
|
|
r, err := gfdb.DB(ctx).Model(ctx, consts.PlatformTable).Data(&req).OmitEmpty().Where(entity.PlatformCols.Id, req.Id).Update()
|
||
|
|
if err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
return r.RowsAffected()
|
||
|
|
}
|
||
|
|
|
||
|
|
// Delete 删除平台
|
||
|
|
func (d *platformDao) Delete(ctx context.Context, req *dto.DeletePlatformReq) (rows int64, err error) {
|
||
|
|
r, err := gfdb.DB(ctx).Model(ctx, consts.PlatformTable).Where(entity.PlatformCols.Id, req.Id).Delete()
|
||
|
|
if err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
return r.RowsAffected()
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetOne 获取单个平台
|
||
|
|
func (d *platformDao) GetOne(ctx context.Context, req *dto.GetPlatformReq) (res *entity.Platform, err error) {
|
||
|
|
r, err := gfdb.DB(ctx).Model(ctx, consts.PlatformTable).Where(entity.PlatformCols.Id, req.Id).One()
|
||
|
|
if err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
err = r.Struct(&res)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
// Count 获取平台数量
|
||
|
|
func (d *platformDao) Count(ctx context.Context, req *dto.ListPlatformReq) (count int, err error) {
|
||
|
|
return d.buildListFilter(ctx, req).Count()
|
||
|
|
}
|
||
|
|
|
||
|
|
// List 获取平台列表
|
||
|
|
func (d *platformDao) List(ctx context.Context, req *dto.ListPlatformReq) (res []entity.Platform, total int, err error) {
|
||
|
|
model := d.buildListFilter(ctx, req)
|
||
|
|
model.OrderDesc(entity.PlatformCols.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 *platformDao) buildListFilter(ctx context.Context, req *dto.ListPlatformReq) *gdb.Model {
|
||
|
|
model := gfdb.DB(ctx).Model(ctx, consts.PlatformTable).Model
|
||
|
|
if !g.IsEmpty(req.Keyword) {
|
||
|
|
model.WhereLike(entity.PlatformCols.Name, "%"+req.Keyword+"%")
|
||
|
|
}
|
||
|
|
model.Where(entity.PlatformCols.Name, req.Name)
|
||
|
|
model.Where(entity.PlatformCols.Type, req.Type)
|
||
|
|
model.Where(entity.PlatformCols.Status, req.Status)
|
||
|
|
model.OmitEmptyWhere()
|
||
|
|
return model
|
||
|
|
}
|
||
|
|
|
||
|
|
// UpdateStatus 更新平台状态
|
||
|
|
func (d *platformDao) UpdateStatus(ctx context.Context, id int64, status string) (rows int64, err error) {
|
||
|
|
r, err := gfdb.DB(ctx).Model(ctx, consts.PlatformTable).
|
||
|
|
Data(map[string]interface{}{"status": status}).
|
||
|
|
Where(entity.PlatformCols.Id, id).
|
||
|
|
Update()
|
||
|
|
if err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
return r.RowsAffected()
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetByType 根据类型获取平台
|
||
|
|
func (d *platformDao) GetByType(ctx context.Context, platformType string) (res *entity.Platform, err error) {
|
||
|
|
r, err := gfdb.DB(ctx).Model(ctx, consts.PlatformTable).
|
||
|
|
Where(entity.PlatformCols.Type, platformType).
|
||
|
|
One()
|
||
|
|
if err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
err = r.Struct(&res)
|
||
|
|
return
|
||
|
|
}
|