package mysql import ( "context" "encoding/json" "finclip-app-manager/domain/entity" "finclip-app-manager/domain/entity/proto/apiproto" "finclip-app-manager/infrastructure/db/entity/sql" "finclip-app-manager/infrastructure/utils" "strings" "time" "gorm.io/gorm" ) type AppRepo struct { } func NewAppRepo() *AppRepo { return &AppRepo{} } func (ar *AppRepo) AppCount(ctx context.Context, organId string) (int, error) { var ( total int64 err error ) if organId != "" { err = DB.Model(&sql.App{}).Where("organ_id=? and is_forbidden=?", organId, 0).Count(&total).Error } else { err = DB.Model(&sql.App{}).Where("is_forbidden=?", 0).Count(&total).Error } return int(total), err } func (ar *AppRepo) InsertApp(ctx context.Context, info entity.App) error { sqlInfo := tr.CovertAppToSql(&info) sqlInfoV2 := tr.CovertAppToAppV2(sqlInfo) err := DB.WithContext(ctx).Model(&sql.AppV2{}).Create(sqlInfoV2).Error if err != nil { return err } return DB.WithContext(ctx).Model(&sql.AppStatusInfo{}).Create(&sqlInfo.StatusInfo).Error } func (ar *AppRepo) GetAppInfo(ctx context.Context, appId string) (entity.App, error) { info := sql.App{} err := DB.WithContext(ctx). //Joins("StatusInfo"). Where("apps.app_id=?", appId). First(&info).Error if err != nil { return entity.App{}, err } statusInfo, _ := ar.GetAppStatusInfo(ctx, appId) info.StatusInfo = statusInfo return *tr.CovertAppToEntity(&info), nil } func (ar *AppRepo) UpdateApp(ctx context.Context, info entity.App) error { sqlInfo := tr.CovertAppToSql(&info) data := make(map[string]interface{}) data["app_class"] = sqlInfo.AppClass data["app_tag"] = sqlInfo.AppTag data["app_type"] = sqlInfo.AppType data["name"] = sqlInfo.Name data["logo"] = sqlInfo.Logo data["desc"] = sqlInfo.Desc data["detail_desc"] = sqlInfo.DetailDesc data["class"] = sqlInfo.Class data["created_by"] = sqlInfo.CreatedBy data["developer_id"] = sqlInfo.DeveloperId data["group_id"] = sqlInfo.GroupId data["is_rollback"] = sqlInfo.IsRollback data["ext"] = sqlInfo.Ext data["is_forbidden"] = sqlInfo.IsForbidden data["privacy_setting_type"] = sqlInfo.PrivacySettingType data["expire"] = sqlInfo.Expire data["create_time"] = sqlInfo.CreateTime data["update_time"] = sqlInfo.UpdateTime return DB.Model(&sql.App{}).Where("app_id=?", info.AppID).Updates(data).Error } func (ar *AppRepo) GetAppStatusInfo(ctx context.Context, appId string) (sql.AppStatusInfo, error) { tmpQuery := "app_id = ?" tmpArgs := []interface{}{appId} var appStatusInfo sql.AppStatusInfo err := DB.Model(&sql.AppStatusInfo{}).Where(tmpQuery, tmpArgs...).First(&appStatusInfo).Error return appStatusInfo, err } func (ar *AppRepo) GetAppBuildInfoAndAppVersionStatusInfo(ctx context.Context, buildInfoId string, appId string, sequence int) (sql.AppBuildInfo, sql.AppVersionStatusInfo, error) { tmpQuery := "trace_id = ? or build_info_id = ?" tmpArgs := []interface{}{buildInfoId, buildInfoId} var appBuildInfo sql.AppBuildInfo err := DB.Model(&sql.AppBuildInfo{}).Where(tmpQuery, tmpArgs...).First(&appBuildInfo).Error tmpQuery = "app_id = ? AND sequence = ?" tmpArgs = []interface{}{appId, sequence} var appVersionStatusInfo sql.AppVersionStatusInfo err = DB.Model(&sql.AppVersionStatusInfo{}).Where(tmpQuery, tmpArgs...).First(&appVersionStatusInfo).Error return appBuildInfo, appVersionStatusInfo, err } func (ar *AppRepo) GetAppBuildInfoAndAppVersionStatusInfoV2(ctx context.Context, appId, appVer string, sequence int) (sql.AppBuildInfo, sql.AppVersionStatusInfo, error) { tmpQuery := "app_id = ? and version = ? and buildStatus = ?" tmpArgs := []interface{}{appId, appVer, "success"} var appBuildInfo sql.AppBuildInfo var appVersionStatusInfo sql.AppVersionStatusInfo var buildInfo sql.BuildInfo err := DB.Model(&sql.BuildInfo{}).Where(tmpQuery, tmpArgs...). Order("create_time DESC"). First(&buildInfo).Error if err != nil { return appBuildInfo, appVersionStatusInfo, err } tmpQuery = "app_id = ? AND sequence = ?" tmpArgs = []interface{}{appId, sequence} err = DB.Model(&sql.AppVersionStatusInfo{}).Where(tmpQuery, tmpArgs...).First(&appVersionStatusInfo).Error appBuildInfo.AppId = appId appBuildInfo.UserId = buildInfo.UserId appBuildInfo.CreatedBy = buildInfo.Username appBuildInfo.Status = true appBuildInfo.Version = appVer appBuildInfo.VersionDescription = buildInfo.VersionRemark appBuildInfo.Name = "app.zip" appBuildInfo.Url = buildInfo.FileUrl appBuildInfo.EncryptedUrl = buildInfo.EncryptedUrl appBuildInfo.Packages = buildInfo.Packages appBuildInfo.EncryptPackages = buildInfo.EncryptPackage appBuildInfo.CreateTime = buildInfo.CreateTime appBuildInfo.UpdateTime = buildInfo.UpdateTime return appBuildInfo, appVersionStatusInfo, err } func (ar *AppRepo) GetAppVerInfo(ctx context.Context, appId string, seq int) (entity.AppVersion, error) { sqlInfo := sql.AppVersion{} err := DB.WithContext(ctx). Where("app_versions.app_id=? AND app_versions.sequence=?", appId, seq). //Joins("StatusInfo"). //Joins("BuildInfo"). Find(&sqlInfo).Error if sqlInfo.AppId == "" { err = gorm.ErrRecordNotFound } if sqlInfo.BuildInfoId != "" { appBuildInfo, appVersionStatusInfo, _ := ar.GetAppBuildInfoAndAppVersionStatusInfo(ctx, sqlInfo.BuildInfoId, sqlInfo.AppId, sqlInfo.Sequence) sqlInfo.BuildInfo = appBuildInfo sqlInfo.StatusInfo = appVersionStatusInfo } else { appBuildInfo, appVersionStatusInfo, _ := ar.GetAppBuildInfoAndAppVersionStatusInfoV2(ctx, sqlInfo.AppId, sqlInfo.Version, sqlInfo.Sequence) sqlInfo.BuildInfo = appBuildInfo sqlInfo.StatusInfo = appVersionStatusInfo } return *tr.CovertAppVerToEntity(&sqlInfo), err } func (ar *AppRepo) GetOnlineAppVer(ctx context.Context, appId string) (entity.AppVersion, error) { sqlInfo := sql.AppVersion{} /*err := DB.WithContext(ctx). Joins("StatusInfo"). Joins("BuildInfo"). Where("StatusInfo.app_id=? AND StatusInfo.status_value=?", appId, entity.StPublished). First(&sqlInfo).Error*/ statusInfo := sql.AppVersionStatusInfo{} err := DB.WithContext(ctx). Where("app_version_status_infos.app_id=? AND app_version_status_infos.status_value=?", appId, entity.StPublished). First(&statusInfo).Error if err != nil { return *tr.CovertAppVerToEntity(&sqlInfo), err } err = DB.WithContext(ctx). Where("app_versions.app_id=? AND app_versions.sequence=?", appId, statusInfo.Sequence). First(&sqlInfo).Error if err != nil { return *tr.CovertAppVerToEntity(&sqlInfo), err } sqlInfo.StatusInfo = statusInfo if sqlInfo.BuildInfoId != "" { appBuildInfo, _, _ := ar.GetAppBuildInfoAndAppVersionStatusInfo(ctx, sqlInfo.BuildInfoId, sqlInfo.AppId, sqlInfo.Sequence) sqlInfo.BuildInfo = appBuildInfo } else { appBuildInfo, _, _ := ar.GetAppBuildInfoAndAppVersionStatusInfoV2(ctx, sqlInfo.AppId, sqlInfo.Version, sqlInfo.Sequence) sqlInfo.BuildInfo = appBuildInfo } return *tr.CovertAppVerToEntity(&sqlInfo), err } func (ar *AppRepo) GetLatestPubAppVer(ctx context.Context, appId string) (entity.AppVersion, error) { sqlInfo := sql.AppVersion{} /*err := DB.WithContext(ctx). Joins("StatusInfo"). Joins("BuildInfo"). Where("StatusInfo.app_id=? AND StatusInfo.published_update_time<>0", appId). Order("StatusInfo.published_update_time DESC"). First(&sqlInfo).Error*/ statusInfo := sql.AppVersionStatusInfo{} err := DB.WithContext(ctx). Where("app_version_status_infos.app_id=? AND app_version_status_infos.published_update_time<>0", appId). Order("app_version_status_infos.published_update_time DESC"). First(&statusInfo).Error if err != nil { return *tr.CovertAppVerToEntity(&sqlInfo), err } err = DB.WithContext(ctx). Where("app_versions.app_id=? AND app_versions.sequence=?", appId, statusInfo.Sequence). First(&sqlInfo).Error if err != nil { return *tr.CovertAppVerToEntity(&sqlInfo), err } sqlInfo.StatusInfo = statusInfo if sqlInfo.BuildInfoId != "" { appBuildInfo, _, _ := ar.GetAppBuildInfoAndAppVersionStatusInfo(ctx, sqlInfo.BuildInfoId, sqlInfo.AppId, sqlInfo.Sequence) sqlInfo.BuildInfo = appBuildInfo } else { appBuildInfo, _, _ := ar.GetAppBuildInfoAndAppVersionStatusInfoV2(ctx, sqlInfo.AppId, sqlInfo.Version, sqlInfo.Sequence) sqlInfo.BuildInfo = appBuildInfo } return *tr.CovertAppVerToEntity(&sqlInfo), err } func (ar *AppRepo) UpdateAppVerAppName(ctx context.Context, appId, appName string) error { data := make(map[string]interface{}) data["name"] = appName return DB.Model(&sql.AppVersionV2{}).Where("app_id=?", appId).Updates(data).Error } func (ar *AppRepo) GetNowPubAppVer(ctx context.Context, appId string) (entity.AppVersion, error) { sqlInfo := sql.AppVersion{} /*err := DB.WithContext(ctx). Joins("StatusInfo"). Joins("BuildInfo"). Where("StatusInfo.app_id=? AND StatusInfo.status_value=?", appId, entity.StPublished). Order("sequence DESC"). First(&sqlInfo).Error*/ statusInfo := sql.AppVersionStatusInfo{} err := DB.WithContext(ctx). Where("app_version_status_infos.app_id=? AND app_version_status_infos.status_value=?", appId, entity.StPublished). Order("app_version_status_infos.sequence DESC"). First(&statusInfo).Error if err != nil { return *tr.CovertAppVerToEntity(&sqlInfo), err } err = DB.WithContext(ctx). Where("app_versions.app_id=? AND app_versions.sequence=?", appId, statusInfo.Sequence). First(&sqlInfo).Error if err != nil { return *tr.CovertAppVerToEntity(&sqlInfo), err } sqlInfo.StatusInfo = statusInfo appBuildInfo, _, _ := ar.GetAppBuildInfoAndAppVersionStatusInfo(ctx, sqlInfo.BuildInfoId, sqlInfo.AppId, sqlInfo.Sequence) sqlInfo.BuildInfo = appBuildInfo return *tr.CovertAppVerToEntity(&sqlInfo), err } func (ar *AppRepo) GetLatestReviewAppVer(ctx context.Context, appId string) (entity.AppVersion, error) { sqlInfo := sql.AppVersion{} /*err := DB.WithContext(ctx). Joins("StatusInfo"). Joins("BuildInfo"). Where("app_versions.app_id=? AND StatusInfo.publishing_update_time <> 0", appId). Order("app_versions.sequence DESC"). First(&sqlInfo).Error*/ statusInfo := sql.AppVersionStatusInfo{} err := DB.WithContext(ctx). Where("app_version_status_infos.app_id=? AND app_version_status_infos.publishing_update_time <> 0", appId). Order("app_version_status_infos.publishing_update_time DESC"). First(&statusInfo).Error if err != nil { return *tr.CovertAppVerToEntity(&sqlInfo), err } err = DB.WithContext(ctx). Where("app_versions.app_id=?", appId). Order("app_versions.sequence DESC"). First(&sqlInfo).Error if err != nil { return *tr.CovertAppVerToEntity(&sqlInfo), err } sqlInfo.StatusInfo = statusInfo if sqlInfo.BuildInfoId != "" { appBuildInfo, _, _ := ar.GetAppBuildInfoAndAppVersionStatusInfo(ctx, sqlInfo.BuildInfoId, sqlInfo.AppId, sqlInfo.Sequence) sqlInfo.BuildInfo = appBuildInfo } else { appBuildInfo, _, _ := ar.GetAppBuildInfoAndAppVersionStatusInfoV2(ctx, sqlInfo.AppId, sqlInfo.Version, sqlInfo.Sequence) sqlInfo.BuildInfo = appBuildInfo } return *tr.CovertAppVerToEntity(&sqlInfo), err } func (ar *AppRepo) GetAllPermitGrayPubVers(ctx context.Context, appId string, maxSeq int) ([]entity.AppVersion, error) { statusList := make([]sql.AppVersionStatusInfo, 0) /*err := DB.WithContext(ctx). Joins("StatusInfo"). Where("StatusInfo.app_id=? AND StatusInfo.status_value=? AND StatusInfo.sequence>?", appId, entity.StPublishApproved, maxSeq). Find(&list).Error*/ err := DB.WithContext(ctx). Where("app_version_status_infos.app_id=? AND app_version_status_infos.status_value=? AND app_version_status_infos.sequence>?", appId, entity.StPublishApproved, maxSeq). Find(&statusList).Error if err != nil { return nil, err } result := make([]entity.AppVersion, 0) for _, v := range statusList { sqlInfo := sql.AppVersion{} err = DB.WithContext(ctx). Where("app_versions.app_id=? AND app_versions.sequence=?", appId, v.Sequence). First(&sqlInfo).Error if err == nil { result = append(result, *tr.CovertAppVerToEntity(&sqlInfo)) } } return result, nil } func (ar *AppRepo) GetMaxSeqAppVer(ctx context.Context, appId string) (entity.AppVersion, error) { sqlInfo := sql.AppVersion{} err := DB.WithContext(ctx). //Joins("StatusInfo"). //Joins("BuildInfo"). Where("app_versions.app_id=?", appId). Order("sequence DESC"). First(&sqlInfo).Error if err != nil { return *tr.CovertAppVerToEntity(&sqlInfo), err } appBuildInfo, statusInfo, _ := ar.GetAppBuildInfoAndAppVersionStatusInfo(ctx, sqlInfo.BuildInfoId, sqlInfo.AppId, sqlInfo.Sequence) sqlInfo.BuildInfo = appBuildInfo sqlInfo.StatusInfo = statusInfo return *tr.CovertAppVerToEntity(&sqlInfo), err } func (ar *AppRepo) SubmitApp(ctx context.Context, req entity.SubmitAppReq, expire int64, userId string) error { nowMaxSeq := 0 maxAppVerInfo, err := ar.GetMaxSeqAppVer(ctx, req.AppId) if err == nil { nowMaxSeq = maxAppVerInfo.Sequence } else { if !DbNotFound(err) { return err } } buildInfo, err := NewAppBuildInfoMysqlRepo().GetInfoById(ctx, req.BuildId) if DbNotFound(err) { buildInfo, err = NewAppBuildInfoMysqlRepo().GetInfoByBuildId(ctx, req.BuildId) if err != nil { return err } } appInfo, err := ar.GetAppInfo(ctx, req.AppId) if err != nil { return err } now := utils.GetNowMs() appVerInfo := sql.AppVersion{ AppId: appInfo.AppID, Sequence: nowMaxSeq + 1, Name: appInfo.Name, Logo: appInfo.Logo, Version: buildInfo.Version, Desc: appInfo.CoreDescription, DetailDesc: appInfo.CustomData.DetailDescription, Class: appInfo.AppClass, Tag: strings.Join(appInfo.AppTag, ","), AppType: appInfo.AppType, DeveloperId: userId, GroupId: appInfo.GroupID, AutoPub: req.NeedAutoPub, InGrayRelease: false, BuildInfoId: req.BuildId, StatusInfo: sql.AppVersionStatusInfo{ AppId: appInfo.AppID, Sequence: nowMaxSeq + 1, StatusValue: entity.StPublishing, StatusReason: "", StatusUpdateTime: now, StatusUpdater: req.Account, PublishingReason: "", PublishingUpdateTime: now, PublishingUpdater: req.Account, PublishedReason: "", PublishedUpdateTime: 0, PublishedUpdater: "", UnpublishedReason: "", UnpublishedUpdateTime: 0, UnpublishedUpdater: "", UnpublishedType: "", ApprovalReason: "", ApprovalUpdateTime: 0, ApprovalUpdater: "", ActionReason: "", ActionUpdater: req.Account, ActionUpdateTime: now, PublishingApprovalReason: "", PublishingApprovalUpdateTime: 0, PublishingApprovalUpdater: "", CreateTime: now, }, CreatedBy: req.Account, IsRollback: false, ExpireTime: expire, CreateTime: now, TestInfo: "", } testInfoByte, _ := json.Marshal(req.TestInfo) appVerInfo.TestInfo = string(testInfoByte) appVerInfoV2 := tr.CovertAppVerToAppVerV2(&appVerInfo) err = DB.WithContext(ctx).Model(&sql.AppVersionV2{}).Create(appVerInfoV2).Error if err != nil { return err } return DB.WithContext(ctx).Model(&sql.AppVersionStatusInfo{}).Create(&appVerInfo.StatusInfo).Error } func (ar *AppRepo) WithdrawPubApp(ctx context.Context, appId string, seq int, account string) error { now := utils.GetNowMs() params := map[string]interface{}{ "status_value": entity.StPublishWithdrawed, "status_update_time": now, "status_updater": account, } return DB.WithContext(ctx).Model(&sql.AppVersionStatusInfo{}).Where("app_id=? AND sequence=?", appId, seq).Updates(params).Error } /* //结束所有低版本的灰度 err := appVerTable.UpdateAll(ctx, bson.M{"appId": appId, "sequence": bson.M{"$lte": seq}}, bson.M{"$set": bson.M{"inGrayRelease": false}}) if err != nil { return err } now := time.Now().UnixNano() / 1e6 //下架之前上架的小程序版本 oldAppVer, err := ar.GetNowPubAppVer(ctx, appId) if err != nil && !NotFound(err) { return err } if !NotFound(err) { unpublishedStatus := mongo.UnpublishedStatus{ LastUpdated: now, ModifiedBy: account, Type: entity.TypeUnpublishedDueToNewSeq, // Mark the type. } //if !isDev { // unpublishedStatus.Type = entity.TypeUnpublishedByAdmin //} unPubUpInfo := map[string]interface{}{ "unpublishedStatus": unpublishedStatus, "status": mongo.Status{ Value: entity.StUnpublished, LastUpdated: now, ModifiedBy: account, }, } err = appVerTable.UpdateOne(ctx, bson.M{"appId": oldAppVer.AppID, "sequence": oldAppVer.Sequence}, bson.M{"$set": unPubUpInfo}) if err != nil { return err } } //上架当前小程序版本 upInfo := make(map[string]interface{}) upInfo["status"] = mongo.Status{ Value: entity.StPublished, Reason: "", LastUpdated: now, ModifiedBy: account, } upInfo["publishedStatus"] = mongo.SpecificStatus{ Reason: "", LastUpdated: now, ModifiedBy: account, } upInfo["actionStatus"] = mongo.SpecificStatus{ LastUpdated: now, ModifiedBy: account, } //修改小程序元数据信息 err = appTable.UpdateOne(ctx, bson.M{"appId": appId}, bson.M{"$set": upInfo}) if err != nil { return err } upInfo["isRollback"] = isRollback err = appVerTable.UpdateOne(ctx, bson.M{"appId": appId, "sequence": seq}, bson.M{"$set": upInfo}) if err != nil { return err } return nil */ func (ar *AppRepo) PubApp(ctx context.Context, appId string, seq int, account string, isDev, isRollback bool) error { now := utils.GetNowMs() oldAppVer, oldPubAppErr := ar.GetNowPubAppVer(ctx, appId) if oldPubAppErr != nil && !DbNotFound(oldPubAppErr) { return oldPubAppErr } return DB.WithContext(ctx).Transaction(func(tx *gorm.DB) error { var err error //下架之前上架的小程序版本 if !DbNotFound(oldPubAppErr) { updateInfos := map[string]interface{}{ "unpublished_update_time": now, "unpublished_updater": account, "unpublished_type": entity.TypeUnpublishedDueToNewSeq, "status_value": entity.StUnpublished, "status_update_time": now, "status_updater": account, } //if !isDev { // updateInfos["unpublished_type"] = entity.TypeUnpublishedByAdmin //} err = tx.WithContext(ctx). Model(&sql.AppVersionStatusInfo{}). Where("app_id=? AND sequence=?", oldAppVer.AppID, oldAppVer.Sequence). Updates(updateInfos).Error if err != nil { return err } } //结束所有低版本的灰度 err = tx.WithContext(ctx). Model(&sql.AppVersion{}). Where("app_id=? AND sequence<=?", appId, seq). Update("in_gray_release", false).Error if err != nil { return err } //上架当前小程序版本 upInfo := map[string]interface{}{ "status_value": entity.StPublished, "status_update_time": now, "status_updater": account, "published_reason": "", "published_update_time": now, "published_updater": account, "action_update_time": now, "action_updater": account, } err = tx.WithContext(ctx). Model(&sql.AppStatusInfo{}). Where("app_id=?", appId). Updates(upInfo).Error if err != nil { return err } err = tx.WithContext(ctx). Model(&sql.AppVersionStatusInfo{}). Where("app_id=? AND sequence=?", appId, seq). Updates(upInfo).Error if err != nil { return err } if isRollback { err = tx.WithContext(ctx). Model(&sql.AppVersionV2{}). Where("app_id=? AND sequence=?", appId, seq). Update("is_rollback", isRollback).Error if err != nil { return err } } return nil }) } func (ar *AppRepo) ListApps(ctx context.Context, groupId string, pageNo, pageSize int, searchText string, sortType, pullType string) (int, []entity.App, error) { var ( list []sql.App sort string query string total int64 queryList []interface{} err error ) switch sortType { case "created": sort = "apps.create_time" case "-created": sort = "apps.create_time DESC" case "updated": sort = "app_status_infos.status_update_time" case "-updated": sort = "app_status_infos.status_update_time DESC" default: sort = "app_status_infos.status_update_time DESC" } if groupId != "" { query = genQuery(query, "AND", "apps.group_id=? ") queryList = append(queryList, groupId) } switch pullType { case "published": //已上架 query = genQuery(query, "AND", "app_status_infos.status_value=? ") queryList = append(queryList, entity.StPublished) case "inDevelopment": //未上架 query = genQuery(query, "AND", "app_status_infos.status_value=? ") queryList = append(queryList, entity.StInDevelopment) case "unPublished": //已下架 query = genQuery(query, "AND", "app_status_infos.status_value=? ") queryList = append(queryList, entity.StUnpublished) } if searchText != "" { s := "%" + searchText + "%" query = genQuery(query, "AND", "( apps.app_id LIKE ? OR apps.name LIKE ? )") queryList = append(queryList, s, s) } if query != "" { err = DB.WithContext(ctx).Debug().Model(&sql.App{}). //Joins("StatusInfo"). Joins("left join app_status_infos on apps.app_id = app_status_infos.app_id"). Where(query, queryList...). Order(sort). Offset(genOffset(pageNo, pageSize)).Limit(pageSize). Find(&list).Error } else { if sortType == "updated" || sortType == "-updated" { err = DB.WithContext(ctx).Debug().Model(&sql.App{}). Joins("left join app_status_infos on apps.app_id = app_status_infos.app_id"). Order(sort). Offset(genOffset(pageNo, pageSize)).Limit(pageSize). Find(&list).Error } else { err = DB.WithContext(ctx).Debug().Model(&sql.App{}). //Joins("StatusInfo"). Order(sort). Offset(genOffset(pageNo, pageSize)).Limit(pageSize). Find(&list).Error } } if err != nil { return 0, nil, err } if query != "" { err = DB.WithContext(ctx).Model(&sql.App{}). Joins("left join app_status_infos on apps.app_id = app_status_infos.app_id"). Where(query, queryList...). Count(&total).Error } else { err = DB.WithContext(ctx).Model(&sql.App{}). Count(&total).Error } if err != nil { return 0, nil, err } result := make([]entity.App, 0) for _, v := range list { statusInfo, _ := ar.GetAppStatusInfo(ctx, v.AppId) v.StatusInfo = statusInfo temp := v result = append(result, *tr.CovertAppToEntity(&temp)) } return int(total), result, err } func (ar *AppRepo) ListAppVers(ctx context.Context, pageNo, pageSize int, searchText string, t string, groupId string) (int, []entity.AppVersion, error) { var ( list []sql.AppVersion sort string query string total int64 queryList []interface{} err error ) sort = "app_version_status_infos.publishing_update_time DESC" switch t { case "pendingReview": //待审核 query += "app_version_status_infos.status_value IN (?) " queryList = append(queryList, []string{entity.StPublishing, entity.StUnpublishing}) case "reviewed": //已审核 query += "app_version_status_infos.status_value NOT IN (?) " queryList = append(queryList, []string{entity.StPublishing, entity.StPublishWithdrawed}) case "revoked": //已撤销 query += "app_version_status_infos.status_value IN (?)" queryList = append(queryList, []string{entity.StPublishWithdrawed}) } if searchText != "" { query = genQuery(query, "AND", "(app_versions.name LIKE ? OR app_versions.app_id LIKE ? )") //query += "(app_versions.name LIKE ? OR app_versions.app_id LIKE ?)" queryList = append(queryList, "%"+searchText+"%", "%"+searchText+"%") } if groupId != "" { query = genQuery(query, "AND", "app_versions.group_id = ?") queryList = append(queryList, groupId) } if query != "" { err = DB.Debug().Model(&sql.AppVersion{}). Joins("left join app_version_status_infos on concat(app_versions.app_id,app_versions.sequence)=concat(app_version_status_infos.app_id,app_version_status_infos.sequence)"). Where(query, queryList...). Order(sort). Offset(genOffset(pageNo, pageSize)).Limit(pageSize). Find(&list).Error } else { err = DB.Debug().Model(&sql.AppVersion{}). Joins("left join app_version_status_infos on concat(app_versions.app_id,app_versions.sequence)=concat(app_version_status_infos.app_id,app_version_status_infos.sequence)"). Order(sort). Offset(genOffset(pageNo, pageSize)).Limit(pageSize). Find(&list).Error } /*if query != "" { err = DB.WithContext(ctx). Model(&sql.AppVersion{}). Order(sort). Joins("StatusInfo"). Joins("BuildInfo"). Where(query, queryList...). Offset(genOffset(pageNo, pageSize)).Limit(pageSize). Find(&list).Error } else { err = DB.WithContext(ctx). Model(&sql.AppVersion{}). Order(sort). Joins("StatusInfo"). Joins("BuildInfo"). Offset(genOffset(pageNo, pageSize)).Limit(pageSize). Find(&list).Error }*/ if err != nil { return 0, nil, err } if query != "" { err = DB.WithContext(ctx). Model(&sql.AppVersion{}). //Joins("StatusInfo"). //Joins("BuildInfo"). Joins("left join app_version_status_infos on concat(app_versions.app_id,app_versions.sequence)=concat(app_version_status_infos.app_id,app_version_status_infos.sequence)"). Where(query, queryList...). Count(&total).Error } else { err = DB.WithContext(ctx). Model(&sql.AppVersion{}). //Joins("StatusInfo"). //Joins("BuildInfo"). Count(&total).Error } result := make([]entity.AppVersion, 0) for _, v := range list { appBuildInfo, statusInfo, _ := ar.GetAppBuildInfoAndAppVersionStatusInfo(ctx, v.BuildInfoId, v.AppId, v.Sequence) v.BuildInfo = appBuildInfo v.StatusInfo = statusInfo temp := v result = append(result, *tr.CovertAppVerToEntity(&temp)) } return int(total), result, err } func (ar *AppRepo) UnpubApp(ctx context.Context, appId string, seq int, account, reason string, isDev bool) error { var err error unpublishedType := "" if isDev { unpublishedType = entity.TypeUnpublishedByDev } else { unpublishedType = entity.TypeUnpublishedByAdmin } now := time.Now().UnixNano() / 1e6 params := map[string]interface{}{ "status_value": entity.StUnpublished, "status_updater": account, "status_update_time": now, "status_reason": reason, "unpublished_updater": account, "unpublished_update_time": now, "unpublished_reason": reason, "action_updater": account, "action_update_time": now, "action_reason": reason, } return DB.WithContext(ctx).Transaction(func(tx *gorm.DB) error { //更新app err = tx.Model(&sql.AppStatusInfo{}). Where("app_id=?", appId). Updates(params).Error if err != nil { return err } //更新app_version params["unpublished_type"] = unpublishedType err = tx.Model(&sql.AppVersionStatusInfo{}). Where("app_id=? AND sequence=?", appId, seq).Updates(params).Error if err != nil { return err } return nil }) } func (ar *AppRepo) ListAppVersByAppId(ctx context.Context, appId string, pageNo, pageSize int) (int, []entity.AppVersion, error) { var ( list = make([]sql.AppVersion, 0) total int64 err error ) err = DB.WithContext(ctx). //Joins("StatusInfo"). //Joins("BuildInfo"). Where("app_versions.app_id=?", appId). Order("sequence DESC"). Offset(genOffset(pageNo, pageSize)).Limit(pageSize). Find(&list).Error if err != nil { return 0, nil, err } err = DB.WithContext(ctx).Model(&sql.AppVersion{}). Where("app_id=?", appId). Count(&total).Error if err != nil { return 0, nil, err } result := make([]entity.AppVersion, 0) for _, v := range list { appBuildInfo, statusInfo, _ := ar.GetAppBuildInfoAndAppVersionStatusInfo(ctx, v.BuildInfoId, v.AppId, v.Sequence) v.BuildInfo = appBuildInfo v.StatusInfo = statusInfo temp := v result = append(result, *tr.CovertAppVerToEntity(&temp)) } return int(total), result, err } func (ar *AppRepo) GetAppReviews(ctx context.Context, req apiproto.AdminGetAppReviewsReq) (int, []entity.AppVersion, error) { return 0, nil, nil } func (ar *AppRepo) GetAllPublishedVerList(ctx context.Context, appId string) ([]entity.AppVersion, int, error) { var ( //list = make([]sql.AppVersion, 0) statusList = make([]sql.AppVersionStatusInfo, 0) total int64 err error ) /*err = DB.WithContext(ctx). Joins("StatusInfo"). Joins("BuildInfo"). Where("app_versions.app_id=? AND StatusInfo.status_value=?", appId, entity.StPublished). Order("sequence DESC"). Find(&list).Error*/ err = DB.WithContext(ctx). Where("app_version_status_infos.app_id=? AND app_version_status_infos.status_value=?", appId, entity.StPublished). Order("sequence DESC"). Find(&statusList).Error if err != nil { return nil, 0, err } result := make([]entity.AppVersion, 0) for _, v := range statusList { sqlInfo := sql.AppVersion{} err = DB.WithContext(ctx). Where("app_versions.app_id=? AND app_versions.sequence=?", appId, v.Sequence). First(&sqlInfo).Error if err == nil { appBuildInfo, statusInfo, _ := ar.GetAppBuildInfoAndAppVersionStatusInfo(ctx, sqlInfo.BuildInfoId, sqlInfo.AppId, sqlInfo.Sequence) sqlInfo.BuildInfo = appBuildInfo sqlInfo.StatusInfo = statusInfo result = append(result, *tr.CovertAppVerToEntity(&sqlInfo)) } } return result, int(total), err } func (ar *AppRepo) GetPublishedAppList(ctx context.Context, pageNo, pageSize int, searchText string) ([]entity.AppVersion, int, error) { var ( err error total int64 list = make([]sql.AppVersion, 0) ) if searchText != "" { s := genLike(searchText) /*err = DB.WithContext(ctx).Model(&sql.AppVersion{}). Joins("StatusInfo"). Joins("BuildInfo"). Where("StatusInfo.status_value=? AND app_versions.name LIKE ? OR app_versions.app_id LIKE ?", entity.StPublished, s, s). Find(&list).Error*/ err = DB.WithContext(ctx).Model(&sql.AppVersion{}). Joins("left join app_version_status_infos on concat(app_versions.app_id,app_versions.sequence)=concat(app_version_status_infos.app_id,app_version_status_infos.sequence)"). Where("app_version_status_infos.status_value=? AND (app_versions.name LIKE ? OR app_versions.app_id LIKE ?)", entity.StPublished, s, s). Order("app_versions.create_time desc"). Offset(genOffset(pageNo, pageSize)).Limit(pageSize). Find(&list).Error } else { /*err = DB.WithContext(ctx).Model(&sql.AppVersion{}). Joins("StatusInfo"). Joins("BuildInfo"). Where("StatusInfo.status_value=?", entity.StPublished). Find(&list).Error*/ err = DB.WithContext(ctx).Model(&sql.AppVersion{}). Joins("left join app_version_status_infos on concat(app_versions.app_id,app_versions.sequence)=concat(app_version_status_infos.app_id,app_version_status_infos.sequence)"). Where("app_version_status_infos.status_value=?", entity.StPublished). Order("app_versions.create_time desc"). Offset(genOffset(pageNo, pageSize)).Limit(pageSize). Find(&list).Error } if err != nil { return nil, 0, err } if searchText != "" { s := genLike(searchText) /*err = DB.WithContext(ctx).Model(&sql.AppVersion{}). Joins("StatusInfo"). Where("StatusInfo.status_value=? AND app_versions.name LIKE ? OR app_versions.app_id LIKE ?", entity.StPublished, s, s). Count(&total).Error*/ err = DB.WithContext(ctx).Model(&sql.AppVersion{}). Joins("left join app_version_status_infos on concat(app_versions.app_id,app_versions.sequence)=concat(app_version_status_infos.app_id,app_version_status_infos.sequence)"). Where("app_version_status_infos.status_value=? AND app_versions.name LIKE ? OR app_versions.app_id LIKE ?", entity.StPublished, s, s). Count(&total).Error } else { /*err = DB.WithContext(ctx).Model(&sql.AppVersion{}). Joins("StatusInfo"). Where("StatusInfo.status_value=?", entity.StPublished). Count(&total).Error*/ err = DB.WithContext(ctx).Model(&sql.AppVersion{}). Joins("left join app_version_status_infos on concat(app_versions.app_id,app_versions.sequence)=concat(app_version_status_infos.app_id,app_version_status_infos.sequence)"). Where("app_version_status_infos.status_value=?", entity.StPublished). Count(&total).Error } if err != nil { return nil, 0, err } result := make([]entity.AppVersion, 0) for _, v := range list { temp := v result = append(result, *tr.CovertAppVerToEntity(&temp)) } return result, int(total), nil } func (ar *AppRepo) GetAllVers(ctx context.Context, appId string) ([]entity.AppVersion, int, error) { var ( list = make([]sql.AppVersion, 0) total int64 err error ) err = DB.WithContext(ctx). //Joins("StatusInfo"). //Joins("BuildInfo"). Where("app_versions.app_id=?", appId). Order("app_versions.sequence DESC"). Find(&list).Error if err != nil { return nil, 0, err } result := make([]entity.AppVersion, 0) for _, v := range list { appBuildInfo, statusInfo, _ := ar.GetAppBuildInfoAndAppVersionStatusInfo(ctx, v.BuildInfoId, v.AppId, v.Sequence) v.BuildInfo = appBuildInfo v.StatusInfo = statusInfo temp := v result = append(result, *tr.CovertAppVerToEntity(&temp)) } return result, int(total), err } func (ar *AppRepo) GetAppsByAppIds(ctx context.Context, appIds []string) ([]entity.App, error) { list := make([]sql.App, 0) err := DB.WithContext(ctx). Model(&sql.App{}). Where("apps.app_id IN (?)", appIds). //Joins("StatusInfo"). Order("apps.create_time DESC"). Find(&list).Error if err != nil { return nil, err } result := make([]entity.App, 0) for _, v := range list { statusInfo, _ := ar.GetAppStatusInfo(ctx, v.AppId) v.StatusInfo = statusInfo temp := v result = append(result, *tr.CovertAppToEntity(&temp)) } return result, nil } func (ar *AppRepo) GetAppsByGroupIds(ctx context.Context, groupIds []string) (int, []entity.App, error) { list := make([]sql.App, 0) var total int64 err := DB.WithContext(ctx).Model(&sql.App{}). Where("group_id IN (?)", groupIds). Count(&total). Order("create_time DESC"). Find(&list).Error if err != nil { return 0, nil, err } result := make([]entity.App, 0) for _, v := range list { statusInfo, _ := ar.GetAppStatusInfo(ctx, v.AppId) v.StatusInfo = statusInfo temp := v result = append(result, *tr.CovertAppToEntity(&temp)) } return int(total), result, nil } func (ar *AppRepo) GetAppsToBinding(ctx context.Context, bindingId string, pageNo, pageSize int, searchText string) (int, []entity.App, error) { br := NewBindingByMysqlRepo() bindingInfo, err := br.GetBindingByBindingId(ctx, bindingId) if err != nil { return 0, nil, err } //获取应用关联的小程序Id assAppIdList := make([]string, 0) for _, v := range bindingInfo.AppInfos { assAppIdList = append(assAppIdList, v.AppID) } query := "apps.group_id=? " queryList := []interface{}{bindingInfo.GroupID} if len(assAppIdList) != 0 { query = genQuery(query, "AND", "apps.app_id NOT IN (?) ") queryList = append(queryList, assAppIdList) } if searchText != "" { query = genQuery(query, "AND", "apps.name LIKE ?") queryList = append(queryList, "%"+searchText+"%") } list := make([]sql.App, 0) var total int64 err = DB.WithContext(ctx). Model(&sql.App{}). Where(query, queryList...). Count(&total). Find(&list).Error if err != nil { return 0, nil, err } err = DB.WithContext(ctx). Model(&sql.App{}). //Joins("StatusInfo"). Where(query, queryList...). Order("apps.create_time DESC"). Offset(genOffset(pageNo, pageSize)).Limit(pageSize). Find(&list).Error if err != nil { return 0, nil, err } result := make([]entity.App, 0) for _, v := range list { statusInfo, _ := ar.GetAppStatusInfo(ctx, v.AppId) v.StatusInfo = statusInfo temp := v result = append(result, *tr.CovertAppToEntity(&temp)) } return int(total), result, nil } func (ar *AppRepo) GetLinkAppsByBindingId(ctx context.Context, bindingId string, pageNo, pageSize int, searchText string) (int, []entity.App, error) { br := NewBindingByMysqlRepo() bindingInfo, err := br.GetBindingByBindingId(ctx, bindingId) if err != nil { return 0, nil, err } //获取应用关联的小程序Id assAppIdList := make([]string, 0) for _, v := range bindingInfo.AppInfos { assAppIdList = append(assAppIdList, v.AppID) } list := make([]sql.App, 0) var total int64 err = DB.WithContext(ctx).Model(&sql.App{}). Where("group_id=? AND app_id IN (?)", bindingInfo.GroupID, assAppIdList). Count(&total). Order("create_time DESC"). Offset(genOffset(pageNo, pageSize)).Limit(pageSize). Find(&list).Error if err != nil { return 0, nil, err } result := make([]entity.App, 0) for _, v := range list { statusInfo, _ := ar.GetAppStatusInfo(ctx, v.AppId) v.StatusInfo = statusInfo temp := v result = append(result, *tr.CovertAppToEntity(&temp)) } return int(total), result, nil } func (ar *AppRepo) GetLinkAppsBySDKKey(ctx context.Context, sdkKey string, pageNo, pageSize int) (int, []entity.App, error) { br := NewBindingByMysqlRepo() //bindingInfo, err := br.GetBindingByBindingId(ctx, bindingId) bindingInfos, err := br.GetBindListBySdkKey(ctx, sdkKey) if err != nil { return 0, nil, err } //获取应用关联的小程序Id assAppIdList := make([]string, 0) for _, v := range bindingInfos { for _, y := range v.AppInfos { assAppIdList = append(assAppIdList, y.AppID) } } list := make([]sql.App, 0) var total int64 err = DB.WithContext(ctx).Model(&sql.App{}). Joins("left join app_status_infos on apps.app_id = app_status_infos.app_id"). Where("app_status_infos.status_value=? and apps.app_id IN (?)", entity.StPublished, assAppIdList). Count(&total). Order("create_time DESC"). Offset(genOffset(pageNo, pageSize)).Limit(pageSize). Find(&list).Error if err != nil { return 0, nil, err } result := make([]entity.App, 0) for _, v := range list { statusInfo, _ := ar.GetAppStatusInfo(ctx, v.AppId) v.StatusInfo = statusInfo temp := v result = append(result, *tr.CovertAppToEntity(&temp)) } return int(total), result, nil } func (ar *AppRepo) AdminGetLinkApps(ctx context.Context, pageNo, pageSize int, searchText string) (int, []entity.AdminGetLinkAppsRspItem, error) { type temp struct { AppId string `gorm:"column:app_id"` AppName string `gorm:"column:app_name"` AssTime int64 `gorm:"column:ass_time"` BindingId string `gorm:"column:binding_id"` GroupName string `gorm:"column:group_name"` BindingName string `gorm:"column:binding_name"` } var ( err error list = make([]temp, 0) total int64 ) // 和mongo保持一致,mongo 0 代表的第一页,客户端的pageNo也是从0开始的 pageNo = pageNo + 1 if searchText != "" { s := genLike(searchText) err = DB.WithContext(ctx). Table("link_app_infos l"). Joins("JOIN apps a ON l.app_id=a.app_id"). Joins("JOIN binding b ON l.binding_id=b.binding_id"). Joins("JOIN app_status_infos c ON l.app_id=c.app_id"). Where("c.status_value =? and (a.name LIKE ? OR b.name LIKE ? OR b.group_name LIKE ? )", entity.StPublished, s, s, s). Select("l.app_id AS app_id,a.name AS app_name,l.associated_at AS ass_time," + "l.binding_id AS binding_id,b.group_name AS group_name,b.name AS binding_name"). Order("l.associated_at DESC"). Offset(genOffset(pageNo, pageSize)).Limit(pageSize). Find(&list).Error } else { err = DB.WithContext(ctx). Table("link_app_infos l"). Joins("JOIN apps a ON l.app_id=a.app_id"). Joins("JOIN binding b ON l.binding_id=b.binding_id"). Joins("JOIN app_status_infos c ON l.app_id=c.app_id"). Where("c.status_value =? ", entity.StPublished). Select("l.app_id AS app_id,a.name AS app_name,l.associated_at AS ass_time," + "l.binding_id AS binding_id,b.group_name AS group_name,b.name AS binding_name"). Order("l.associated_at DESC"). Offset(genOffset(pageNo, pageSize)).Limit(pageSize). Find(&list).Error } if err != nil { return 0, nil, err } if searchText != "" { s := genLike(searchText) err = DB.WithContext(ctx). Table("link_app_infos l"). Joins("JOIN apps a ON l.app_id=a.app_id"). Joins("JOIN binding b ON l.binding_id=b.binding_id"). Joins("JOIN app_status_infos c ON l.app_id=c.app_id"). Where("c.status_value =? and (a.name LIKE ? OR b.name LIKE ? OR b.group_name LIKE ? )", entity.StPublished, s, s, s). Count(&total).Error } else { err = DB.WithContext(ctx). Table("link_app_infos l"). Joins("JOIN app_status_infos c ON l.app_id=c.app_id"). Where("c.status_value =? ", entity.StPublished). Count(&total).Error } if err != nil { return 0, nil, err } result := make([]entity.AdminGetLinkAppsRspItem, 0) for _, v := range list { item := entity.AdminGetLinkAppsRspItem{} item.Id = v.BindingId item.AppIdDetail.AppId = v.AppId item.AppIdDetail.Name = v.AppName item.AppIdDetail.Sequence = 0 item.AppInfos.AssociatedAt = v.AssTime item.BindingId = v.BindingId item.GroupName = v.GroupName item.Name = v.BindingName result = append(result, item) } return int(total), result, nil } func (ar *AppRepo) ApproveApp(ctx context.Context, appId string, seq int, account, reason string, isPass bool) error { now := utils.GetNowMs() params := map[string]interface{}{ "status_update_time": now, "status_updater": account, "status_reason": reason, "publishing_approval_update_time": now, "publishing_approval_updater": account, "publishing_approval_reason": reason, "approval_update_time": now, "approval_updater": account, "approval_reason": reason, } if isPass { params["status_value"] = entity.StPublishApproved } else { params["status_value"] = entity.StPublishRejected } return DB.WithContext(ctx).Model(&sql.AppVersionStatusInfo{}). Where("app_id=? AND sequence=?", appId, seq). Updates(params).Error } func (ar *AppRepo) GetRollbackAbleList(ctx context.Context, groupId, appId string, latestPubSeq int) ([]entity.AppVersion, error) { appVers := make([]sql.AppVersion, 0) limitVersion := 5 // 最近5个版本, 不是最近下架的5个版本 /*query := "app_versions.app_id=? AND app_versions.group_id=? AND " + "StatusInfo.app_id=? AND StatusInfo.status_value=? AND " + "StatusInfo.sequence>=? AND StatusInfo.sequence=? AND app_version_status_infos.sequence 0 { query = genQuery(query, "AND", "create_time>? ") queryList = append(queryList, startTime) } if endTime > 0 { query = genQuery(query, "AND", "create_time<=? ") queryList = append(queryList, endTime) } switch isForbidden { case entity.APP_FORBIDDEN_NOT_STATUS, entity.APP_FORBIDDEN_IS_STATUS: query = genQuery(query, "AND", "is_forbidden=? ") queryList = append(queryList, isForbidden) } if query != "" { err = DB.WithContext(ctx). Model(&sql.App{}). Where(query, queryList...). Count(&total).Error } else { err = DB.WithContext(ctx). Model(&sql.App{}). Count(&total).Error } return int(total), err } func (ar *AppRepo) GetPublishedStatistics(ctx context.Context, startTime, endTime int64, groupId string) (int, error) { var ( query = "" queryList = make([]interface{}, 0) err error total int64 ) query += "status_value=? " queryList = append(queryList, entity.StPublished) if groupId != "" { query += "group_id = ? " queryList = append(queryList, groupId) } if startTime > 0 { query = genQuery(query, "AND", "published_update_time>? ") queryList = append(queryList, startTime) } if endTime > 0 { query = genQuery(query, "AND", "published_update_time<=? ") queryList = append(queryList, endTime) } if query != "" { err = DB.WithContext(ctx).Model(&sql.AppStatusInfo{}). Where(query, queryList...). Count(&total).Error } else { err = DB.WithContext(ctx).Model(&sql.AppStatusInfo{}).Count(&total).Error } return int(total), err } //GetSubmittedStatistics 累计审核的小程序----只要小程序被审核过都算 func (ar *AppRepo) GetSubmittedStatistics(ctx context.Context, startTime, endTime int64, groupId string, distinct bool) (int, error) { var ( query = "" queryList = make([]interface{}, 0) err error total int64 ) query += "status_value<>? " queryList = append(queryList, entity.StInDevelopment) if groupId != "" { query += "AND group_id = ? " queryList = append(queryList, groupId) } if startTime > 0 { query = genQuery(query, "AND", "publishing_update_time>? ") queryList = append(queryList, startTime) } if endTime > 0 { query = genQuery(query, "AND", "publishing_update_time<=? ") queryList = append(queryList, endTime) } if distinct { if query != "" { err = DB.WithContext(ctx).Model(&sql.AppVersionStatusInfo{}). Where(query, queryList...). Select("count(distinct app_id)"). Count(&total).Error } else { err = DB.WithContext(ctx).Model(&sql.AppVersionStatusInfo{}). Select("count(distinct app_id)"). Count(&total).Error } } else { if query != "" { err = DB.WithContext(ctx).Model(&sql.AppVersionStatusInfo{}). Where(query, queryList...). Count(&total).Error } else { err = DB.WithContext(ctx).Model(&sql.AppVersionStatusInfo{}). Count(&total).Error } } return int(total), err } //审核通过小程序 func (ar *AppRepo) GetApprovedStatistics(ctx context.Context, startTime, endTime int64, groupId string, distinct bool) (int, error) { var ( query = "" queryList = make([]interface{}, 0) err error total int64 ) query += "status_value IN (?) " queryList = append(queryList, []string{entity.StPublishApproved, entity.StPublished, entity.StUnpublishing, entity.StUnpublishApproved, entity.StUnpublishRejected, entity.StUnpublished}) if groupId != "" { query += "AND group_id = ? " queryList = append(queryList, groupId) } if startTime > 0 { query = genQuery(query, "AND", "published_update_time>? ") queryList = append(queryList, startTime) } if endTime > 0 { query = genQuery(query, "AND", "published_update_time<=? ") queryList = append(queryList, endTime) } if distinct { if query != "" { err = DB.WithContext(ctx).Debug(). Model(&sql.AppVersionStatusInfo{}). Where(query, queryList...). Select("COUNT(DISTINCT app_id)"). Count(&total).Error } else { err = DB.WithContext(ctx).Debug(). Model(&sql.AppVersionStatusInfo{}). Select("COUNT(DISTINCT app_id)"). Count(&total).Error } } else { if query != "" { err = DB.WithContext(ctx).Model(&sql.AppVersionStatusInfo{}).Where(query, queryList...).Count(&total).Error } else { err = DB.WithContext(ctx).Model(&sql.AppVersionStatusInfo{}).Count(&total).Error } } return int(total), err } func (ar *AppRepo) GetAppClassPer(ctx context.Context, status string) ([]entity.AppClassPerRsp, error) { //先获取所有的class _, typeConfigList, err := NewTypeConfigByMysqlRepo().GetAll(ctx) if err != nil { return nil, err } type TempRsp struct { AppClass string `gorm:"column:app_class"` Count int `gorm:"column:count"` } rspList := make([]TempRsp, 0) if status == "published" { err = DB.WithContext(ctx).Model(&sql.App{}). Joins("StatusInfo"). Where("StatusInfo.status_value = ?", entity.StPublished). Select("apps.app_class as app_class ,count(apps.class) as count"). Group("app_class").Find(&rspList).Error if err != nil { return nil, err } } else { err = DB.WithContext(ctx).Model(&sql.App{}). Select("app_class,count(*) as count"). Group("app_class").Find(&rspList).Error if err != nil { return nil, err } } typeConfigListMap := make(map[string]string) for _, v := range typeConfigList { typeConfigListMap[v.Value] = v.CustomData.Chinese } result := make([]entity.AppClassPerRsp, 0) for _, v := range rspList { item := entity.AppClassPerRsp{ Class: v.AppClass, Count: v.Count, } if name, ok := typeConfigListMap[v.AppClass]; ok { item.Name = []string{name} } result = append(result, item) } return result, err } func (ar *AppRepo) GetAppVerLimitByIdentity(ctx context.Context, t, appId, identity string, limit int) ([]entity.AppVersion, error) { var ( list = make([]sql.AppVersion, 0) query = "" err error ) query = "app_build_infos.file_md5=?" if t == "sha256" { query = "app_build_infos.encrypted_file_sha256=?" } err = DB.WithContext(ctx). //Joins("BuildInfo"). //Joins("StatusInfo"). Joins("left join app_build_infos on app_versions.build_info_id = app_build_infos.trace_id"). Where(query, identity). Order("sequence DESC"). Limit(limit).Find(&list).Error if err != nil { return nil, err } result := make([]entity.AppVersion, 0) for _, v := range list { appBuildInfo, statusInfo, _ := ar.GetAppBuildInfoAndAppVersionStatusInfo(ctx, v.BuildInfoId, v.AppId, v.Sequence) v.BuildInfo = appBuildInfo v.StatusInfo = statusInfo temp := v result = append(result, *tr.CovertAppVerToEntity(&temp)) } return result, nil } func (ar *AppRepo) GetGrayStatisticsVerList(ctx context.Context, appId string, seq int, begin, end int64) (int, []entity.AppVersion, error) { var ( list = make([]sql.AppVersion, 0) total int64 query = "" queryList = make([]interface{}, 0) err error ) query = "app_versions.app_id=? AND app_versions.sequence<=? " queryList = append(queryList, appId, seq) if end == 0 { query += "AND (app_version_status_infos.status_value=? OR (app_version_status_infos.status_value=? AND app_version_status_infos.unpublished_update_time >=?) ) " queryList = append(queryList, entity.StPublished, entity.StUnpublished, begin) } else { query += "AND ( (app_version_status_infos.status_value=? AND app_version_status_infos.published_update_time<=?) OR " + "(app_version_status_infos.status_value=? AND ((app_version_status_infos.published_update_time >=? AND app_version_status_infos.published_update_time<=?) " + "OR (app_version_status_infos.unpublished_update_time>=? AND app_version_status_infos.unpublished_update_time<=?)))) " queryList = append(queryList, entity.StPublished, end, entity.StUnpublished, begin, end, begin, end) } err = DB.WithContext(ctx).Model(&sql.AppVersion{}).Debug(). //Joins("BuildInfo"). //Joins("StatusInfo"). Joins("left join app_version_status_infos on concat(app_versions.app_id,app_versions.sequence)=concat(app_version_status_infos.app_id,app_version_status_infos.sequence)"). Where(query, queryList...). Order("app_versions.sequence ASC"). Find(&list).Error if err != nil { return 0, nil, err } err = DB.WithContext(ctx).Model(&sql.AppVersion{}). //Joins("BuildInfo"). //Joins("StatusInfo"). Joins("left join app_version_status_infos on concat(app_versions.app_id,app_versions.sequence)=concat(app_version_status_infos.app_id,app_version_status_infos.sequence)"). Where(query, queryList...). Count(&total).Error if err != nil { return 0, nil, err } result := make([]entity.AppVersion, 0) for _, v := range list { appBuildInfo, statusInfo, _ := ar.GetAppBuildInfoAndAppVersionStatusInfo(ctx, v.BuildInfoId, v.AppId, v.Sequence) v.BuildInfo = appBuildInfo v.StatusInfo = statusInfo temp := v result = append(result, *tr.CovertAppVerToEntity(&temp)) } return int(total), result, nil } func (ar *AppRepo) GetAppClassList(ctx context.Context) ([]entity.CItem, error) { apps := make([]sql.App, 0) err := DB.Model(&sql.App{}). Select("apps.app_class").Group("apps.app_class"). Find(&apps).Error if err != nil { return make([]entity.CItem, 0), err } cItems := make([]entity.CItem, 0) for _, v := range apps { cItem := entity.CItem{ Id: v.AppClass, } cItems = append(cItems, cItem) } return cItems, nil } func (ar *AppRepo) GetAppTagList(ctx context.Context) ([]entity.TagItem, error) { apps := make([]sql.App, 0) err := DB.Model(&sql.App{}). Select("apps.app_tag").Group("apps.app_tag"). Find(&apps).Error if err != nil { return make([]entity.TagItem, 0), err } tagItem := make([]entity.TagItem, 0) for _, v := range apps { var id []string appTags := strings.Split(v.AppTag, ",") for _, x := range appTags { id = append(id, x) } tag := entity.TagItem{ Id: id, } tagItem = append(tagItem, tag) } return tagItem, nil } func (ar *AppRepo) GetAppsBySearchText(ctx context.Context, searchText string) ([]entity.App, error) { list := make([]sql.App, 0) err := DB.WithContext(ctx).Model(&sql.App{}). Where("app_id LIKE ? OR name LIKE ?", "%"+searchText+"%", "%"+searchText+"%"). Find(&list).Error if err != nil { return nil, err } result := make([]entity.App, 0) for _, v := range list { /*statusInfo, _ := ar.GetAppStatusInfo(ctx, v.AppId) v.StatusInfo = statusInfo*/ temp := v result = append(result, *tr.CovertAppToEntity(&temp)) } return result, nil } func (ar *AppRepo) GetAppByCreator(ctx context.Context, phone string) ([]entity.App, error) { list := make([]sql.App, 0) err := DB.WithContext(ctx).Model(&sql.App{}). Where("created_by = ? ", phone). Find(&list).Error if err != nil { return nil, err } result := make([]entity.App, 0) for _, v := range list { /*statusInfo, _ := ar.GetAppStatusInfo(ctx, v.AppId) v.StatusInfo = statusInfo*/ temp := v result = append(result, *tr.CovertAppToEntity(&temp)) } return result, nil }