finclip-app-manager/infrastructure/db/repo/mysql/app_build_info.go

200 lines
6.0 KiB
Go
Raw Normal View History

2023-10-31 14:07:26 +08:00
package mysql
import (
"context"
"finclip-app-manager/domain/entity"
"finclip-app-manager/domain/repository"
"finclip-app-manager/infrastructure/db/entity/sql"
"gorm.io/gorm"
)
var _ repository.IAppBuildInfoRepo = new(AppBuildInfoMysqlRepo)
type AppBuildInfoMysqlRepo struct {
}
func NewAppBuildInfoMysqlRepo() *AppBuildInfoMysqlRepo {
return &AppBuildInfoMysqlRepo{}
}
func (a *AppBuildInfoMysqlRepo) Insert(ctx context.Context, item entity.AppBuildInfo) error {
err := DB.Model(&sql.AppBuildInfo{}).Create(tr.CovertBuildInfoToSql(&item)).Error
return err
}
func (a *AppBuildInfoMysqlRepo) GetInfoById(ctx context.Context, id string) (*entity.AppBuildInfo, error) {
query := "trace_id=?"
args := []interface{}{id}
info := sql.AppBuildInfo{}
err := DB.Model(&sql.AppBuildInfo{}).
Where(query, args...).
First(&info).Error
if err != nil {
return nil, err
}
return tr.CovertBuildInfoToEntity(&info), err
}
func (a *AppBuildInfoMysqlRepo) GetInfoByBuildInfoId(ctx context.Context, buildInfoId string) (*entity.AppBuildInfo, error) {
query := "build_info_id=?"
args := []interface{}{buildInfoId}
info := sql.AppBuildInfo{}
err := DB.Model(&sql.AppBuildInfo{}).
Where(query, args...).
First(&info).Error
if err != nil {
return nil, err
}
return tr.CovertBuildInfoToEntity(&info), err
}
func (a *AppBuildInfoMysqlRepo) GetInfoByBuildId(ctx context.Context, id string) (*entity.AppBuildInfo, error) {
query := "build_info_id=?"
args := []interface{}{id}
info := sql.AppBuildInfo{}
err := DB.Model(&sql.AppBuildInfo{}).
Where(query, args...).
First(&info).Error
if err != nil {
return nil, err
}
return tr.CovertBuildInfoToEntity(&info), err
}
func (a *AppBuildInfoMysqlRepo) GetLatestInfoByAppId(ctx context.Context, appId string) (*entity.AppBuildInfo, error) {
query := "app_id=?"
args := []interface{}{appId}
info := sql.AppBuildInfo{}
err := DB.Model(&sql.AppBuildInfo{}).
Where(query, args...).
Order("create_time DESC").
First(&info).Error
if err != nil {
return nil, err
}
return tr.CovertBuildInfoToEntity(&info), nil
}
func (a *AppBuildInfoMysqlRepo) GetTrialInfoByAppId(ctx context.Context, appId string) (*entity.AppBuildInfo, error) {
query := "app_id=? and source=?"
args := []interface{}{appId, entity.APP_BUILD_SOURCE_TRIAL}
info := sql.AppBuildInfo{}
err := DB.Model(&sql.AppBuildInfo{}).Where(query, args...).First(&info).Error
if err != nil {
return nil, err
}
return tr.CovertBuildInfoToEntity(&info), nil
}
func (a *AppBuildInfoMysqlRepo) GetTrialInfoById(ctx context.Context, id string) (*entity.AppBuildInfo, error) {
query := "trace_id=? and source=?"
args := []interface{}{id, entity.APP_BUILD_SOURCE_TRIAL}
info := sql.AppBuildInfo{}
err := DB.Model(&sql.AppBuildInfo{}).
Where(query, args...).First(&info).Error
if err != nil {
return nil, err
}
return tr.CovertBuildInfoToEntity(&info), nil
}
func (a *AppBuildInfoMysqlRepo) AddTrial(ctx context.Context, id string) error {
query := "trace_id=?"
args := []interface{}{id}
return DB.Model(&sql.AppBuildInfo{}).Where(query, args...).Update("source", entity.APP_BUILD_SOURCE_TRIAL).Error
}
func (a *AppBuildInfoMysqlRepo) CancelTrial(ctx context.Context, id string) error {
query := "trace_id=?"
args := []interface{}{id}
data := make(map[string]interface{})
data["source"] = entity.APP_BUILD_SOURCE_BUILD //零值字段
data["path_and_query"] = ""
return DB.Model(&sql.AppBuildInfo{}).Where(query, args...).Updates(data).Error
}
func (a *AppBuildInfoMysqlRepo) UpdateTrialStartParams(ctx context.Context, id string, params entity.AppStartParams) error {
query := "trace_id=?"
args := []interface{}{id}
return DB.Model(&sql.AppBuildInfo{}).Where(query, args...).Update("path_and_query", params.PathAndQuery).Error
}
func (a *AppBuildInfoMysqlRepo) UpdateTrialPath(ctx context.Context, id, path string) error {
query := "trace_id=?"
args := []interface{}{id}
return DB.Model(&sql.AppBuildInfo{}).Where(query, args...).Update("path", path).Error
}
func (a *AppBuildInfoMysqlRepo) UpdateOneStatus(ctx context.Context, id string, status bool) error {
query := "trace_id=?"
args := []interface{}{id}
return DB.Model(&sql.AppBuildInfo{}).Where(query, args...).Update("status", status).Error
}
func (a *AppBuildInfoMysqlRepo) GetList(ctx context.Context, appId string, pageNo, pageSize int) (int64, []entity.AppBuildInfo, error) {
query := "app_id =? "
args := []interface{}{appId}
sort := "create_time DESC"
var total int64
list := make([]sql.AppBuildInfo, 0)
err := DB.Model(&sql.AppBuildInfo{}).Debug().
Where(query, args...).Count(&total).
Order(sort).
Offset(genOffset(pageNo, pageSize)).Limit(pageSize).
Find(&list).Error
if err != nil {
return 0, nil, err
}
result := make([]entity.AppBuildInfo, 0)
for _, v := range list {
temp := v
result = append(result, *tr.CovertBuildInfoToEntity(&temp))
}
return total, result, nil
}
func (a *AppBuildInfoMysqlRepo) GetAll(ctx context.Context) ([]entity.AppBuildInfo, error) {
list := make([]sql.AppBuildInfo, 0)
err := DB.Model(&sql.AppBuildInfo{}).Debug().
Find(&list).Error
if err != nil {
return nil, err
}
result := make([]entity.AppBuildInfo, 0)
for _, v := range list {
temp := v
result = append(result, *tr.CovertBuildInfoToEntity(&temp))
}
return result, nil
}
func (a *AppBuildInfoMysqlRepo) GetAppBuilds(ctx context.Context, groupID, appId string, pageSize, pageNo int) (int64, []entity.AppBuildInfo, error) {
query := "app_id =? and group_id =?"
args := []interface{}{appId, groupID}
sort := "create_time desc"
var total int64
list := make([]sql.AppBuildInfo, 0)
err := DB.Model(&sql.AppBuildInfo{}).Debug().
Where(query, args...).Count(&total).
Order(sort).
Offset(genOffset(pageNo, pageSize)).Limit(pageSize).
Find(&list).Error
if err != nil {
return 0, nil, err
}
result := make([]entity.AppBuildInfo, 0)
for _, v := range list {
temp := v
result = append(result, *tr.CovertBuildInfoToEntity(&temp))
}
return total, result, nil
}
func (a *AppBuildInfoMysqlRepo) NotFound(err error) bool {
return err == gorm.ErrRecordNotFound
}