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

1688 lines
63 KiB
Go
Raw Permalink Normal View History

2023-10-31 14:07:26 +08:00
package mysql
import (
"context"
"errors"
"finclip-app-manager/domain/entity"
"finclip-app-manager/domain/entity/proto/apiproto"
"finclip-app-manager/domain/repository"
"finclip-app-manager/infrastructure/config"
"finclip-app-manager/infrastructure/db/entity/sql"
"finclip-app-manager/infrastructure/utility"
"fmt"
"time"
"gorm.io/gorm"
)
var _ repository.IBindingRepo = new(BindingByMysqlRepo)
type BindingByMysqlRepo struct {
}
func NewBindingByMysqlRepo() *BindingByMysqlRepo {
return &BindingByMysqlRepo{}
}
type Bundle struct {
Id uint64 `json:"id" gorm:"primary_key;column:id;type:BIGINT(16) AUTO_INCREMENT;comment:'自增id'" sql:"auto_increment;primary_key"`
BundleID string `json:"bundleId" gorm:"column:bundle_id;type:varchar(512);default:'';comment:bundleId"`
Remark string `json:"remark" gorm:"column:remark;type:varchar(512);default:'';comment:类型"`
SDKKey string `json:"SDKKey" gorm:"column:sdk_key;type:varchar(512);default:'';comment:sdkKey"`
SDKID string `json:"SDKID" gorm:"column:sdk_id;type:varchar(64);default:'';comment:sdkid"`
IsFirstCreate bool `json:"isFirstCreate" gorm:"column:is_first_create;type:bool;default:false;comment:是否第一次创建"` //是否第一次创建
CreatedAt int64 `json:"createdAt" gorm:"column:created_at;type:BIGINT(16);default:0;comment:创建时间"`
CreatedAccount string `json:"createdAccount" bson:"created_account" gorm:"column:created_account;type:varchar(64);default:'';comment:创建账号"`
CreatedBy string `json:"createdBy" bson:"created_by" gorm:"column:created_by;type:varchar(64);default:'';comment:创建人"`
IsForbidden int `json:"isForbidden" bson:"is_forbidden" gorm:"column:is_forbidden;type:BIGINT(16);default:0;comment:是否禁用"` //是否禁用 0:可用 1:禁用
}
func (Bundle) TableName() string {
return "bundle"
}
type BundleInfo struct {
Id uint64 `json:"id" gorm:"primary_key;column:id;type:BIGINT(16) AUTO_INCREMENT;comment:'自增id'" sql:"auto_increment;primary_key"`
BindingId string `json:"bindingId" gorm:"column:binding_id;key;type:varchar(64);default:'';comment:应用Id"`
GroupID string `json:"groupId" gorm:"column:group_id;key;type:varchar(64);default:'';comment:企业ID"`
BundleID string `json:"bundleId" gorm:"column:bundle_id;type:varchar(512);default:'';comment:bundleId"`
Remark string `json:"remark" gorm:"column:remark;type:varchar(512);default:'';comment:类型"`
SDKKey string `json:"SDKKey" gorm:"column:sdk_key;type:varchar(512);default:'';comment:sdkKey"`
SDKID string `json:"SDKID" gorm:"column:sdk_id;type:varchar(64);default:'';comment:sdkid"`
IsFirstCreate bool `json:"isFirstCreate" gorm:"column:is_first_create;type:bool;default:false;comment:是否第一次创建"` //是否第一次创建
CreatedAt int64 `json:"createdAt" gorm:"column:created_at;type:BIGINT(16);default:0;comment:创建时间"`
CreatedAccount string `json:"createdAccount" gorm:"column:created_account;type:varchar(64);default:'';comment:创建账号"`
CreatedBy string `json:"createdBy" gorm:"column:created_by;type:varchar(64);default:'';comment:创建人"`
IsForbidden int `json:"isForbidden" gorm:"column:is_forbidden;type:BIGINT(16);default:0;comment:是否禁用"` //是否禁用 0:未禁用 1:禁用
IsReview int `json:"isReview" gorm:"column:is_review;type:tinyint;default:0;comment:是否审核应用"` //是否禁用 0:未禁用 1:禁用
}
func (BundleInfo) TableName() string {
return "bundle_info"
}
type BindingMysql struct {
Id uint64 `json:"id" gorm:"primary_key;column:id;type:BIGINT(16) AUTO_INCREMENT;comment:'自增id'" sql:"auto_increment;primary_key"`
BindingID string `json:"bindingID" gorm:"column:binding_id;unique;type:varchar(64);comment:'应用的id'"`
Name string `json:"name" gorm:"column:name;type:varchar(64);default:'';comment:'应用名称'"`
//BundleInfos string `json:"bundleInfos" gorm:"column:bundle_infos;type:text;comment:'bundle ids'"`
CreatedInfoBy string `json:"createdInfoBy" gorm:"column:created_info_by;type:varchar(64);default:'';comment:''"`
CreatedInfoAt int64 `json:"createdInfoAt" gorm:"column:created_info_at;type:BIGINT(16);default:0;comment:''"`
CreatedInfoAccount string `json:"createdInfoAccount" gorm:"column:created_info_account;type:varchar(64);default:'';comment:''"`
GroupID string `json:"groupId" gorm:"column:group_id;type:varchar(64);default:'';comment:'企业ID'"`
GroupName string `json:"groupName" gorm:"column:group_name;type:varchar(64);default:'';comment:'企业名称(为了查询)'"`
CooperateStatusValue string `json:"cooperateStatusValue" gorm:"column:cooperate_status_value;type:varchar(256);default:'';comment:''"`
CooperateStatusReason string `json:"cooperateStatusReason" gorm:"column:cooperate_status_reason;type:varchar(256);default:'';comment:''"`
CooperateStatusLastUpdated int64 `json:"cooperateStatusLastUpdated" gorm:"column:cooperate_status_last_updated;type:BIGINT(16);default:0;comment:''"`
CooperateStatusModifiedBy string `json:"cooperateStatusModifiedBy" gorm:"column:cooperate_status_modified_by;type:varchar(256);default:0;comment:''"`
CooperateValidStatusReason string `json:"cooperateValidStatusReason" gorm:"column:cooperate_valid_status_reson;type:varchar(256);default:'';comment:''"`
CooperateValidStatusLastUpdated int64 `json:"cooperateValidStatusLastUpdated" gorm:"column:cooperate_valid_status_last_updated;type:BIGINT(16);default:0;comment:''"`
CooperateValidStatusModifiedBy string `json:"cooperateValidStatusModifiedBy" gorm:"column:cooperate_valid_status_modified_by;type:varchar(256);default:0;comment:''"`
CooperateInvalidStatusReason string `json:"cooperateInvalidStatusReason" gorm:"column:cooperate_invalid_status_reson;type:varchar(256);default:'';comment:''"`
CooperateInvalidStatusLastUpdated int64 `json:"cooperateInvalidStatusLastUpdated" gorm:"column:cooperate_invalid_status_last_updated;type:BIGINT(16);default:0;comment:''"`
CooperateInvalidStatusModifiedBy string `json:"cooperateInvalidStatusModifiedBy" gorm:"column:cooperate_invalid_status_modified_by;type:varchar(256);default:0;comment:''"`
BundleInfos []BundleInfo `json:"bundleInfos" gorm:"ForeignKey:binding_id;AssociationForeignKey:binding_id"`
AppInfos []LinkAppInfo `json:"appInfos" gorm:"ForeignKey:binding_id;AssociationForeignKey:binding_id"`
//sAppInfos string `json:"appInfos" gorm:"column:app_infos;type:text;comment:'appid 的信息'"`
Owner string `json:"owner" gorm:"column:owner;type:varchar(64);default:'';comment:'所属企业'"`
Expire int64 `json:"expire" gorm:"column:expire;type:BIGINT(16);default:0;comment:''"`
ApiServer string `json:"apiServer" gorm:"column:api_server;type:varchar(256);default:'';comment:'子域名'"`
PlatForm int `json:"platform" gorm:"column:platform;type:tinyint;comment:'来源平台'"`
AutoBind int `json:"autoBind" gorm:"column:auto_bind;type:tinyint;comment:'自动绑定'"`
HiddenBundle int `json:"hiddenBundle" gorm:"column:hidden_bundle;type:tinyint;comment:'隐藏bundle信息'"`
FromBindingID string `json:"fromBindingId" gorm:"column:from_binding_id;type:varchar(64);default:'';comment:'来源bindingid'"`
}
type BindingMysqlV2 struct {
Id uint64 `json:"id" gorm:"primary_key;column:id;type:BIGINT(16) AUTO_INCREMENT;comment:'自增id'" sql:"auto_increment;primary_key"`
BindingID string `json:"bindingID" gorm:"column:binding_id;unique;type:varchar(64);comment:'应用的id'"`
Name string `json:"name" gorm:"column:name;type:varchar(64);default:'';comment:'应用名称'"`
CreatedInfoBy string `json:"createdInfoBy" gorm:"column:created_info_by;type:varchar(64);default:'';comment:''"`
CreatedInfoAt int64 `json:"createdInfoAt" gorm:"column:created_info_at;type:BIGINT(16);default:0;comment:''"`
CreatedInfoAccount string `json:"createdInfoAccount" gorm:"column:created_info_account;type:varchar(64);default:'';comment:''"`
GroupID string `json:"groupId" gorm:"column:group_id;type:varchar(64);default:'';comment:'企业ID'"`
GroupName string `json:"groupName" gorm:"column:group_name;type:varchar(64);default:'';comment:'企业名称(为了查询)'"`
CooperateStatusValue string `json:"cooperateStatusValue" gorm:"column:cooperate_status_value;type:varchar(256);default:'';comment:''"`
CooperateStatusReason string `json:"cooperateStatusReason" gorm:"column:cooperate_status_reason;type:varchar(256);default:'';comment:''"`
CooperateStatusLastUpdated int64 `json:"cooperateStatusLastUpdated" gorm:"column:cooperate_status_last_updated;type:BIGINT(16);default:0;comment:''"`
CooperateStatusModifiedBy string `json:"cooperateStatusModifiedBy" gorm:"column:cooperate_status_modified_by;type:varchar(256);default:0;comment:''"`
CooperateValidStatusReason string `json:"cooperateValidStatusReason" gorm:"column:cooperate_valid_status_reson;type:varchar(256);default:'';comment:''"`
CooperateValidStatusLastUpdated int64 `json:"cooperateValidStatusLastUpdated" gorm:"column:cooperate_valid_status_last_updated;type:BIGINT(16);default:0;comment:''"`
CooperateValidStatusModifiedBy string `json:"cooperateValidStatusModifiedBy" gorm:"column:cooperate_valid_status_modified_by;type:varchar(256);default:0;comment:''"`
CooperateInvalidStatusReason string `json:"cooperateInvalidStatusReason" gorm:"column:cooperate_invalid_status_reson;type:varchar(256);default:'';comment:''"`
CooperateInvalidStatusLastUpdated int64 `json:"cooperateInvalidStatusLastUpdated" gorm:"column:cooperate_invalid_status_last_updated;type:BIGINT(16);default:0;comment:''"`
CooperateInvalidStatusModifiedBy string `json:"cooperateInvalidStatusModifiedBy" gorm:"column:cooperate_invalid_status_modified_by;type:varchar(256);default:0;comment:''"`
Owner string `json:"owner" gorm:"column:owner;type:varchar(64);default:'';comment:'所属企业'"`
Expire int64 `json:"expire" gorm:"column:expire;type:BIGINT(16);default:0;comment:''"`
ApiServer string `json:"apiServer" gorm:"column:api_server;type:varchar(256);default:'';comment:'子域名'"`
PlatForm int `json:"platform" gorm:"column:platform;type:tinyint;comment:'来源平台'"`
AutoBind int `json:"autoBind" gorm:"column:auto_bind;type:tinyint;comment:'自动绑定'"`
HiddenBundle int `json:"hiddenBundle" gorm:"column:hidden_bundle;type:tinyint;comment:'隐藏bundle信息'"`
FromBindingID string `json:"fromBindingId" gorm:"column:from_binding_id;type:varchar(64);default:'';comment:'来源bindingid'"`
}
type LinkAppInfo struct {
Id uint64 `gorm:"primary_key;column:id;type:BIGINT(16) AUTO_INCREMENT;comment:'自增id'" sql:"auto_increment;primary_key"`
BindingId string `gorm:"column:binding_id;key;type:varchar(64);comment:'关联应用的id'"`
AppId string `gorm:"column:app_id;key;type:varchar(64);comment:'小程序id'"`
AssociatedAt int64 `gorm:"column:associated_at;type:BIGINT(16);default:0;comment:'关联时间'"`
AssociatedBy string `gorm:"column:associated_by;type:varchar(64);default:'';comment:'关联人'"`
}
func (LinkAppInfo) TableName() string {
return "link_app_infos"
}
func (BindingMysql) TableName() string {
return "binding"
}
func (BindingMysqlV2) TableName() string {
return "binding"
}
func (b BindingByMysqlRepo) GetInfo(ctx context.Context, bindingId string) (*entity.Binding, error) {
info := BindingMysql{}
err := DB.Model(&BindingMysql{}).WithContext(ctx).
Where("binding_id=?", bindingId).
//Preload("BundleInfos").
//Preload("AppInfos").
First(&info).Error
bundleInfos, linkAppInfos, _ := b.GetBundleInfoAndLinkAppInfo(ctx, info.BindingID)
if len(linkAppInfos) > 0 {
info.AppInfos = linkAppInfos
}
if len(bundleInfos) > 0 {
info.BundleInfos = bundleInfos
}
result := convertBindingMysqlToBinding(info)
return &result, err
}
func (b BindingByMysqlRepo) GetByGroupIdAndName(ctx context.Context, groupId string, name string, isAdmin bool) (*entity.Binding, error) {
query := "group_id = ? and name = ? "
args := []interface{}{groupId, name}
if isAdmin {
query += "and platform = ? "
args = append(args, entity.BINGING_PLATFORM_OPER)
} else {
query += "and platform = ? "
args = append(args, entity.BINGING_PLATFORM_ORGAN)
}
bindingMysql := BindingMysql{}
err := DB.Model(&BindingMysql{}).Where(query, args...).
//Preload("BundleInfos").Preload("AppInfos").
First(&bindingMysql).Error
if err != nil {
return nil, err
}
bundleInfos, linkAppInfos, _ := b.GetBundleInfoAndLinkAppInfo(ctx, bindingMysql.BindingID)
if len(linkAppInfos) > 0 {
bindingMysql.AppInfos = linkAppInfos
}
if len(bundleInfos) > 0 {
bindingMysql.BundleInfos = bundleInfos
}
result := convertBindingMysqlToBinding(bindingMysql)
return &result, err
}
func (b BindingByMysqlRepo) GetByApiServer(ctx context.Context, apiServer string) (entity.Binding, error) {
query := "api_server = ?"
args := []interface{}{apiServer}
res := entity.Binding{}
bindingMysql := BindingMysql{}
err := DB.Model(&BindingMysql{}).Where(query, args...).First(&bindingMysql).Error
if err != nil {
return res, err
}
query = "binding_id = ?"
args = []interface{}{bindingMysql.BindingID}
bundleInfoMysqls := make([]BundleInfo, 0)
err = DB.Model(&BindingMysql{}).Where(query, args...).Find(&bundleInfoMysqls).Error
if err != nil {
return res, err
}
bundleInfos, linkAppInfos, _ := b.GetBundleInfoAndLinkAppInfo(ctx, bindingMysql.BindingID)
if len(linkAppInfos) > 0 {
bindingMysql.AppInfos = linkAppInfos
}
if len(bundleInfos) > 0 {
bindingMysql.BundleInfos = bundleInfos
}
result := convertBindingMysqlToBinding(bindingMysql)
return result, err
}
func (b BindingByMysqlRepo) GetInfoByParams(ctx context.Context, sdkKey, organId, appId string) (*entity.Binding, error) {
db := DB.Model(&BindingMysql{})
//db = db.Preload("BundleInfos").Preload("AppInfos")
db.Where("bundle_info.sdk_key like ?", sdkKey)
if appId != "" {
db.Where("link_app_infos.app_id like ?", appId)
}
if organId != "" {
db.Where("binding.group_id = ?", organId)
}
bindingMysql := BindingMysql{}
err := db.
Select("DISTINCT(binding.id), binding.*").
Joins("left join bundle_info on binding.binding_id = bundle_info.binding_id").
Joins("left join link_app_infos on binding.binding_id = link_app_infos.binding_id").
First(&bindingMysql).Error
bundleInfos, linkAppInfos, _ := b.GetBundleInfoAndLinkAppInfo(ctx, bindingMysql.BindingID)
if len(linkAppInfos) > 0 {
bindingMysql.AppInfos = linkAppInfos
}
if len(bundleInfos) > 0 {
bindingMysql.BundleInfos = bundleInfos
}
binding := convertBindingMysqlToBinding(bindingMysql)
return &binding, err
}
func (b BindingByMysqlRepo) GetInfoBySdkKeyOrganId(ctx context.Context, organId, sdkKey string) (*entity.Binding, error) {
query := "group_id = ? and sdk_key = ?"
args := []interface{}{organId, sdkKey}
bundleInfoMysql := BundleInfo{}
err := DB.Model(&BundleInfo{}).Where(query, args...).First(&bundleInfoMysql).Error
if err != nil {
return nil, err
}
return b.GetInfo(ctx, bundleInfoMysql.BindingId)
}
func (b BindingByMysqlRepo) GetAllSdkKey(ctx context.Context, organId string) ([]string, error) {
query := "group_id = ?"
args := []interface{}{organId}
bundleInfoMysqls := make([]BundleInfo, 0)
err := DB.Model(&BundleInfo{}).Where(query, args...).Find(&bundleInfoMysqls).Error
if err != nil {
return nil, err
}
result := make([]string, 0)
for _, v := range bundleInfoMysqls {
result = append(result, v.SDKKey)
}
return result, err
}
func (b BindingByMysqlRepo) GetBindListBySdkKey(ctx context.Context, sdkKey string) ([]entity.Binding, error) {
query := "sdk_key = ?"
args := []interface{}{sdkKey}
bundleInfoMysqls := make([]BundleInfo, 0)
err := DB.Model(&BundleInfo{}).Where(query, args...).Order("created_at").Find(&bundleInfoMysqls).Error
if err != nil {
return nil, err
}
bindings := make([]entity.Binding, 0)
for _, bundleInfoMysql := range bundleInfoMysqls {
binding, _ := b.GetInfo(ctx, bundleInfoMysql.BindingId)
bindings = append(bindings, *binding)
}
return bindings, err
}
func (b BindingByMysqlRepo) SdkKeyExi(ctx context.Context, sdkKey string) (bool, error) {
if _, ok := entity.SdkExiCache.Get(sdkKey); ok {
return true, nil
}
var count int64
err := DB.Model(&BundleInfo{}).
Where("sdk_key=?", sdkKey).
Count(&count).Error
if err != nil {
return false, err
}
if count > 0 {
entity.SdkExiCache.Set(sdkKey, true, -1)
return true, err
}
return false, err
}
func (b BindingByMysqlRepo) Insert(ctx context.Context, bind *entity.Binding) error {
info := convertBindingToBindingMysql(bind)
infoV2 := convertBindingMysqlToBindingMysqlV2(&info)
err := DB.WithContext(ctx).Model(&BindingMysqlV2{}).Create(&infoV2).Error
if err != nil {
return err
}
if len(info.BundleInfos) > 0 {
for _, v := range info.BundleInfos {
DB.Model(&BundleInfo{}).Create(&v)
}
}
if len(info.AppInfos) > 0 {
for _, v := range info.AppInfos {
DB.Model(&LinkAppInfo{}).Create(&v)
}
}
return nil
}
func (b BindingByMysqlRepo) Count(ctx context.Context) (int, error) {
query := ""
args := []interface{}{}
var count int64
err := DB.Model(&BindingMysql{}).Where(query, args...).Count(&count).Error
return int(count), err
}
func (b BindingByMysqlRepo) GetCountByStatus(ctx context.Context, status string, platform int) (int, error) {
query := "cooperate_status_value = ?"
args := []interface{}{"Valid"}
/*switch platform {
case entity.BINGING_PLATFORM_OPER:
query += " and platform = ? and from_binding_id = ?"
args = append(args, platform)
args = append(args, "")
case entity.BINGING_PLATFORM_ORGAN:
query += " and platform = ?"
args = append(args, platform)
default:
query += " and ( ( platform = ? and from_binding_id = ? ) or ( platform = ? ) )"
args = append(args, entity.BINGING_PLATFORM_OPER)
args = append(args, "")
args = append(args, entity.BINGING_PLATFORM_ORGAN)
}*/
query += " and ( ( platform = ? and group_id = ? ) or ( platform = ? ) )"
args = append(args, entity.BINGING_PLATFORM_OPER)
args = append(args, "")
args = append(args, entity.BINGING_PLATFORM_ORGAN)
var count int64
err := DB.Model(&BindingMysql{}).Where(query, args...).Count(&count).Error
return int(count), err
}
func (b BindingByMysqlRepo) GetBundleIdNum(ctx context.Context) (int, error) {
query := "is_forbidden = ?"
args := []interface{}{0}
var count int64
err := DB.Model(&BundleInfo{}).Where(query, args...).Count(&count).Error
return int(count), err
}
func (b BindingByMysqlRepo) GetBundleIdLimitHand(ctx context.Context, groupId string) (int, error) {
var total int64
err := DB.Model(&BundleInfo{}).
Select("select * from bundle_info").Joins("left join binding on binding.binding_id = bundle_info.binding_id").
Where("binding.group_id = ? and bundle_info.is_forbidden = ?", groupId, 0).
Count(&total).Error
return int(total), err
}
func (b BindingByMysqlRepo) GetBundleLimit(ctx context.Context) (int, error) {
/*query := ""
args := []interface{}{}*/
query := "is_forbidden = ?"
args := []interface{}{0}
var count int64
err := DB.Model(&Bundle{}).Where(query, args...).Count(&count).Error
return int(count), err
}
func (b BindingByMysqlRepo) GetBindingByBindingId(ctx context.Context, bindingId string) (*entity.Binding, error) {
bindingMysql := BindingMysql{}
err := DB.Model(&BindingMysql{}).
Where("binding_id = ?", bindingId).
//Preload("BundleInfos").Preload("AppInfos").
First(&bindingMysql).Error
if err != nil {
return nil, err
}
bundleInfos, linkAppInfos, _ := b.GetBundleInfoAndLinkAppInfo(ctx, bindingMysql.BindingID)
if len(linkAppInfos) > 0 {
bindingMysql.AppInfos = linkAppInfos
}
if len(bundleInfos) > 0 {
bindingMysql.BundleInfos = bundleInfos
}
result := convertBindingMysqlToBinding(bindingMysql)
return &result, err
}
func (b BindingByMysqlRepo) GetBindingByGroupIdAndBindingId(ctx context.Context, groupId string, bindingId string) (*entity.Binding, error) {
info := BindingMysql{}
err := DB.Model(&BindingMysql{}).WithContext(ctx).
Where("group_id = ? and binding_id = ?", []interface{}{groupId}, []interface{}{bindingId}).
//Preload("BundleInfos").
//Preload("AppInfos").
First(&info).Error
bundleInfos, linkAppInfos, _ := b.GetBundleInfoAndLinkAppInfo(ctx, info.BindingID)
if len(linkAppInfos) > 0 {
info.AppInfos = linkAppInfos
}
if len(bundleInfos) > 0 {
info.BundleInfos = bundleInfos
}
result := convertBindingMysqlToBinding(info)
return &result, err
}
func (b BindingByMysqlRepo) GetBindingByGroupIdAndSdkKey(ctx context.Context, groupId string, sdkKey string) (*entity.Binding, error) {
query := "group_id = ? and sdk_key = ?"
args := []interface{}{groupId, sdkKey}
bundleInfoMysql := BundleInfo{}
err := DB.Model(&BundleInfo{}).Where(query, args...).First(&bundleInfoMysql).Error
if err != nil {
return nil, err
}
return b.GetInfo(ctx, bundleInfoMysql.BindingId)
}
func (b BindingByMysqlRepo) UpdateBundleInfosByGroupIdAndBindId(ctx context.Context, groupId string, bindingId string, infos []entity.BundleInfo) error {
return DB.Transaction(func(tx *gorm.DB) error {
var err error
for _, v := range infos {
query := "group_id = ? and binding_id = ?"
args := []interface{}{groupId, bindingId}
err = tx.Model(&BundleInfo{}).
Where(query, args...).
Updates(convertBundleInfoToBundleInfoMysql(bindingId, groupId, v)).Error
}
return err
})
}
func (b BindingByMysqlRepo) AppendBundles(ctx context.Context, bindingId, groupId string, bundles []entity.BundleInfo) error {
return DB.Transaction(func(tx *gorm.DB) error {
var err error
for _, v := range bundles {
item := convertBundleInfoToBundleInfoMysql(bindingId, groupId, v)
if config.Cfg.PublishEnv == entity.ENV_PRIVATE {
//私有化环境中如果要变为启用状态要先判断该bundleId运营端是否已启用
if v.IsForbidden == 0 {
info := BundleMysql{}
err := DB.Debug().Model(&BundleMysql{}).Where("binary bundle_id=?", v.BundleID).First(&info).Error
if err != nil {
return err
}
if info.IsForbidden == 1 {
return errors.New(utility.FS_BIND_IS_FORBIDDEN)
}
}
}
if err = tx.Model(&BundleInfo{}).Create(&item).Error; err != nil {
return err
}
}
return nil
})
}
func (b BindingByMysqlRepo) UpdateBundleIsView(ctx context.Context, reviews []apiproto.BundleReviewItem, isReview int) error {
return DB.Transaction(func(tx *gorm.DB) error {
var err error
for _, review := range reviews {
query := "binding_id = ? and binary bundle_id= ?"
args := []interface{}{review.BindingId, review.BundleId}
params := map[string]interface{}{
"is_review": isReview,
}
err = tx.Model(&BundleInfo{}).Debug().
Where(query, args...).
Updates(params).Error
}
return err
})
}
func (b BindingByMysqlRepo) ListReviewBundle(ctx context.Context, pageSize int, pageNo int, searchText string, isReview int) (int64, []entity.ReviewBundleInfo, error) {
query := "binding.platform = ? AND binding.from_binding_id = ? AND bundle_info.is_review = ?"
args := []interface{}{entity.BINGING_PLATFORM_OPER, "", isReview}
if searchText != "" {
query += " AND (binding.name LIKE ? OR bundle_info.bundle_id LIKE ?) "
args = append(args, genLike(searchText))
args = append(args, genLike(searchText))
}
list := []entity.ReviewBundleInfo{}
var total int64
var err error
DB.Debug().Model(&BindingMysql{}).
Joins("LEFT JOIN bundle_info ON binding.binding_id = bundle_info.binding_id").
Where(query, args...).
Count(&total)
if pageSize == 0 && pageNo == 0 {
err = DB.Debug().Model(&BindingMysql{}).
Select("binding.binding_id as binding_id, binding.created_info_at as created_info_at, binding.name as name, binding.cooperate_status_value as status, bundle_info.bundle_id as bundle_id, bundle_info.remark as remark, bundle_info.sdk_key as sdk_key, bundle_info.sdk_id as sdk_id, bundle_info.is_forbidden as is_forbidden, bundle_info.is_review as is_review").
Joins("LEFT JOIN bundle_info ON binding.binding_id = bundle_info.binding_id").
Order("binding.created_info_at desc").
Where(query, args...).
Scan(&list).Error
} else {
err = DB.Debug().Model(&BindingMysql{}).
Select("binding.binding_id as binding_id, binding.created_info_at as created_info_at, binding.name as name, binding.cooperate_status_value as status, bundle_info.bundle_id as bundle_id, bundle_info.remark as remark, bundle_info.sdk_key as sdk_key, bundle_info.sdk_id as sdk_id, bundle_info.is_forbidden as is_forbidden, bundle_info.is_review as is_review").
Joins("LEFT JOIN bundle_info ON binding.binding_id = bundle_info.binding_id").
Order("binding.created_info_at desc").
Where(query, args...).
Offset((pageNo - 1) * pageSize).
Limit(pageSize).
Scan(&list).Error
}
return total, list, err
}
func (b BindingByMysqlRepo) CheckReviewBySdkKey(ctx context.Context, sdkKey string) bool {
query := "binding.platform = ? AND binding.from_binding_id = ? AND binding.cooperate_status_value = ? AND bundle_info.is_review = ? and bundle_info.sdk_key = ? AND bundle_info.is_forbidden = ?"
args := []interface{}{entity.BINGING_PLATFORM_OPER, "", entity.StBindValid, 1, sdkKey, 0}
var total int64
DB.Debug().Model(&BindingMysql{}).
Joins("LEFT JOIN bundle_info ON binding.binding_id = bundle_info.binding_id").
Where(query, args...).
Count(&total)
return total > 0
}
func (b BindingByMysqlRepo) GetReviewBindBySdkKey(ctx context.Context, sdkKey string) (*entity.ReviewBundleInfo, error) {
query := "binding.platform = ? AND binding.from_binding_id = ? AND bundle_info.sdk_key = ?"
args := []interface{}{entity.BINGING_PLATFORM_OPER, "", sdkKey}
list := []entity.ReviewBundleInfo{}
err := DB.Debug().Model(&BindingMysql{}).
Select("binding.binding_id as binding_id, binding.created_info_at as created_info_at, binding.name as name, binding.cooperate_status_value as status, bundle_info.bundle_id as bundle_id, bundle_info.remark as remark, bundle_info.sdk_key as sdk_key, bundle_info.sdk_id as sdk_id, bundle_info.is_forbidden as is_forbidden, bundle_info.is_review as is_review").
Joins("LEFT JOIN bundle_info ON binding.binding_id = bundle_info.binding_id").
Order("binding.created_info_at desc").
Where(query, args...).
Scan(&list).Error
if err != nil {
return nil, err
}
if len(list) > 0 {
return &list[0], nil
} else {
return nil, nil
}
}
func (b BindingByMysqlRepo) UpdateBundles(ctx context.Context, bindingId, groupId string, bundles []entity.BundleInfo) error {
return DB.Transaction(func(tx *gorm.DB) error {
var err error
for _, v := range bundles {
if config.Cfg.PublishEnv == entity.ENV_PRIVATE {
//私有化环境中如果要变为启用状态要先判断该bundleId运营端是否已启用
if v.IsForbidden == 0 {
info := BundleMysql{}
err := DB.Debug().Model(&BundleMysql{}).Where("binary bundle_id=?", v.BundleID).First(&info).Error
if err != nil {
return err
}
if info.IsForbidden == 1 {
return errors.New(utility.FS_BIND_IS_FORBIDDEN)
}
}
}
query := "binding_id = ? and binary bundle_id= ?"
args := []interface{}{bindingId, v.BundleID}
params := map[string]interface{}{
"is_forbidden": v.IsForbidden,
"remark": v.Remark,
}
err = tx.Model(&BundleInfo{}).Debug().
Where(query, args...).
Updates(params).Error
}
return err
})
}
func (b BindingByMysqlRepo) GetByBindIdList(ctx context.Context, ids []string) ([]entity.Binding, error) {
query := "binding_id in ?"
args := []interface{}{ids}
bindingMysqls := make([]BindingMysql, 0)
err := DB.Model(&BindingMysql{}).Where(query, args...).Find(&bindingMysqls).Error
if err != nil {
return nil, err
}
bindings := make([]entity.Binding, 0)
for _, bindingMysql := range bindingMysqls {
query = "binding_id = ?"
args = []interface{}{bindingMysql.BindingID}
bundleInfoMysqls := make([]BundleInfo, 0)
err = DB.Model(&BundleInfo{}).Where(query, args...).Find(&bundleInfoMysqls).Error
if err != nil {
return nil, err
}
bindingMysql.BundleInfos = bundleInfoMysqls
bundleInfos, linkAppInfos, _ := b.GetBundleInfoAndLinkAppInfo(ctx, bindingMysql.BindingID)
if len(linkAppInfos) > 0 {
bindingMysql.AppInfos = linkAppInfos
}
if len(bundleInfos) > 0 {
bindingMysql.BundleInfos = bundleInfos
}
bindings = append(bindings, convertBindingMysqlToBinding(bindingMysql))
}
return bindings, err
}
func (b BindingByMysqlRepo) GetBindListByGroupId(ctx context.Context, groupId string, pageSize int, pageNo int) ([]entity.Binding, int, error) {
bindingMysqls := make([]BindingMysql, 0)
var total int64
err := DB.Model(&BindingMysql{}).WithContext(ctx).
Where("group_id = ? and platform = 0", groupId).
//Preload("BundleInfos").
//Preload("AppInfos").
Order("created_info_at desc").
Offset((pageNo - 1) * pageSize).
Limit(pageSize).
Find(&bindingMysqls).Error
DB.Model(&BindingMysql{}).WithContext(ctx).
Where("group_id = ? and platform = 0", groupId).Count(&total)
bindings := make([]entity.Binding, 0)
for _, bindingMysql := range bindingMysqls {
bundleInfos, linkAppInfos, _ := b.GetBundleInfoAndLinkAppInfo(ctx, bindingMysql.BindingID)
if len(linkAppInfos) > 0 {
bindingMysql.AppInfos = linkAppInfos
}
if len(bundleInfos) > 0 {
bindingMysql.BundleInfos = bundleInfos
}
bindings = append(bindings, convertBindingMysqlToBinding(bindingMysql))
}
return bindings, int(total), err
}
func (b BindingByMysqlRepo) AppendApps(ctx context.Context, bindingId string, apps []entity.AppInfo) error {
for _, app := range apps {
linkApp := LinkAppInfo{
BindingId: bindingId,
AppId: app.AppID,
AssociatedAt: app.AssociatedAt,
AssociatedBy: app.AssociatedBy,
}
DB.Model(&LinkAppInfo{}).Create(&linkApp)
}
return nil
}
func (b BindingByMysqlRepo) RemoveApps(ctx context.Context, bindingId string, appIds []string) error {
err := DB.Where("binding_id = ? AND app_id IN ?", bindingId, appIds).Delete(&LinkAppInfo{}).Error
if err != nil {
log.Errorf("RemoveApps delete binding:%s LinkAppInfo err:%s", bindingId, err.Error())
} else {
log.Infof("RemoveApps delete binding:%s LinkAppInfo succ", bindingId)
}
return err
}
func (b BindingByMysqlRepo) UpdateByBindId(ctx context.Context, bindingId string, bind *entity.Binding) error {
info := convertBindingToBindingMysql(bind)
data := make(map[string]interface{})
data["name"] = info.Name
data["cooperate_status_value"] = info.CooperateStatusValue
data["cooperate_status_reason"] = info.CooperateStatusReason
data["cooperate_status_last_updated"] = info.CooperateStatusLastUpdated
data["cooperate_status_modified_by"] = info.CooperateStatusModifiedBy
data["cooperate_valid_status_reson"] = info.CooperateValidStatusReason
data["cooperate_valid_status_last_updated"] = info.CooperateValidStatusLastUpdated
data["cooperate_valid_status_modified_by"] = info.CooperateValidStatusModifiedBy
data["cooperate_invalid_status_reson"] = info.CooperateInvalidStatusReason
data["cooperate_invalid_status_last_updated"] = info.CooperateInvalidStatusLastUpdated
data["cooperate_invalid_status_modified_by"] = info.CooperateInvalidStatusModifiedBy
data["owner"] = info.Owner
data["expire"] = info.Expire
data["api_server"] = info.ApiServer
//DB.Delete(&BundleInfo{}, "binding_id=?", bindingId)
//DB.Delete(&LinkAppInfo{}, "binding_id=?", bindingId)
err := DB.Where("binding_id=?", bindingId).Delete(&BundleInfo{}).Error
if err != nil {
log.Errorf("UpdateByBindId delete binding:%s BundleInfo err:%s", bindingId, err.Error())
} else {
log.Infof("UpdateByBindId delete binding:%s BundleInfo succ", bindingId)
}
err = DB.Where("binding_id=?", bindingId).Delete(&LinkAppInfo{}).Error
if err != nil {
log.Errorf("UpdateByBindId delete binding:%s LinkAppInfo err:%s", bindingId, err.Error())
} else {
log.Infof("UpdateByBindId delete binding:%s LinkAppInfo succ", bindingId)
}
for _, v := range info.AppInfos {
DB.Model(&LinkAppInfo{}).Create(&v)
}
for _, v := range info.BundleInfos {
DB.Model(&BundleInfo{}).Create(&v)
}
return DB.Model(&BindingMysql{}).Where("binding_id=?", bindingId).Updates(data).Error
}
func (b BindingByMysqlRepo) UpdateExpire(ctx context.Context, bindingId string, expire int64) error {
return DB.WithContext(ctx).Model(&sql.AppVersion{}).Where("binding_id=? ", bindingId).Update("expire", expire).Error
}
func (b BindingByMysqlRepo) UpdateBindingInfo(ctx context.Context, bindingId string, info map[string]interface{}, platform int) error {
if platform == entity.BINGING_PLATFORM_OPER {
query := "binding_id = ? "
args := []interface{}{bindingId}
err := DB.Model(&BindingMysqlV2{}).Debug().
Where(query, args...).
Updates(info).Error
if err != nil {
return err
}
query = "from_binding_id = ? "
args = []interface{}{bindingId}
return DB.Model(&BindingMysqlV2{}).Debug().
Where(query, args...).
Updates(info).Error
} else {
query := "binding_id = ? "
args := []interface{}{bindingId}
return DB.Model(&BindingMysqlV2{}).Debug().
Where(query, args...).
Updates(info).Error
}
}
func (b BindingByMysqlRepo) UpdateBindingGroupName(ctx context.Context, groupId, groupName string) error {
info := make(map[string]interface{})
info["group_name"] = groupName
query := "group_id = ? "
args := []interface{}{groupId}
return DB.Model(&BindingMysqlV2{}).Debug().
Where(query, args...).
Updates(info).Error
}
func (b BindingByMysqlRepo) BundleIdCount(ctx context.Context, groupId string) (int, error) {
if groupId == "" {
query := "is_forbidden = 0"
args := make([]interface{}, 0)
var count int64
err := DB.Model(&BundleInfo{}).Where(query, args...).Count(&count).Error
return int(count), err
} else {
query := "group_id = ? and is_forbidden = 0"
args := []interface{}{groupId}
var count int64
err := DB.Model(&BundleInfo{}).Where(query, args...).Count(&count).Error
return int(count), err
}
}
func (b BindingByMysqlRepo) GetBundleByGroupIdAndBundleId(ctx context.Context, groupId string, bundleId string) (*entity.Binding, error) {
bindingMysql := BindingMysql{}
err := DB.Model(&BindingMysql{}).
Select("binding.*").Joins("left join bundle_info on binding.binding_id = bundle_info.binding_id").
Where("binding.group_id = ? and binary bundle_info.bundle_id = ?", []interface{}{groupId}, []interface{}{bundleId}).
First(&bindingMysql).Error
bundleInfos, linkAppInfos, _ := b.GetBundleInfoAndLinkAppInfo(ctx, bindingMysql.BindingID)
if len(linkAppInfos) > 0 {
bindingMysql.AppInfos = linkAppInfos
}
if len(bundleInfos) > 0 {
bindingMysql.BundleInfos = bundleInfos
}
binding := convertBindingMysqlToBinding(bindingMysql)
return &binding, err
//query := "group_id = ? and bundle_id = ?"
//args := []interface{}{groupId, bundleId}
//
//bundleInfoMysql := BundleInfo{}
//err := DB.Model(&BundleInfo{}).Where(query, args...).First(&bundleInfoMysql).Error
//if err != nil {
// return nil, err
//}
//
//return b.GetInfo(ctx, bundleInfoMysql.BindingId)
}
func (b BindingByMysqlRepo) GetbundlesByBundleId(ctx context.Context, bundleId string) (*entity.Binding, error) {
query := "binary bundle_id = ?"
args := []interface{}{bundleId}
bundleInfoMysql := BundleInfo{}
err := DB.Model(&BundleInfo{}).Where(query, args...).First(&bundleInfoMysql).Error
if err != nil {
return nil, err
}
return b.GetInfo(ctx, bundleInfoMysql.BindingId)
}
type BindingsResult struct {
BindingId string `json:"bindingId" gorm:"column:binding_id"`
}
func (b BindingByMysqlRepo) GetBindingsBySearch(ctx context.Context, pageSize int, pageNo int, sort string, searchText string, searchFields string, cooperateStatus string, platform int) ([]entity.Binding, int, error) {
log.Infof("ListBindings req,groupId:[%s],searchText:[%s],pageNo:[%d],pageSize:[%d]", searchText, pageNo, pageSize)
var sortColumn string
switch sort {
case "-created":
sortColumn = "binding.created_info_at DESC" //按创建时间排序
case "-expire":
sortColumn = "binding.expire DESC" //按过期时间倒序排序
case "expire":
sortColumn = "binding.expire ASC" //按过期时间正序排序
default:
sortColumn = "binding.created_info_at DESC" //按创建时间倒序排序
}
list := make([]BindingMysql, 0)
var total int64
if searchText == "" {
if cooperateStatus != "all" {
query := "cooperate_status_value = ?"
queryList := []interface{}{cooperateStatus}
switch platform {
case entity.BINGING_PLATFORM_OPER:
query += " AND platform = ? and group_id = ?"
queryList = append(queryList, platform)
queryList = append(queryList, "")
case entity.BINGING_PLATFORM_ORGAN:
query += " AND platform = ?"
queryList = append(queryList, platform)
default:
query += " AND ( ( platform = ? AND group_id = ? ) OR ( platform = ? ) )"
queryList = append(queryList, entity.BINGING_PLATFORM_OPER)
queryList = append(queryList, "")
queryList = append(queryList, entity.BINGING_PLATFORM_ORGAN)
}
err := DB.
Where(query, queryList...).
//Preload("BundleInfos").
Order(sortColumn).
Offset(genOffset(pageNo, pageSize)).Limit(pageSize).
Find(&list).Error
if err != nil {
return nil, 0, err
}
err = DB.Model(&BindingMysql{}).
Where(query, queryList...).
Count(&total).Error
if err != nil {
return nil, 0, err
}
} else {
query := ""
queryList := []interface{}{}
switch platform {
case entity.BINGING_PLATFORM_OPER:
query += "platform = ? and group_id = ?"
queryList = append(queryList, platform)
queryList = append(queryList, "")
case entity.BINGING_PLATFORM_ORGAN:
query += "platform = ?"
queryList = append(queryList, platform)
default:
query += "( platform = ? AND group_id = ? ) OR ( platform = ? )"
queryList = append(queryList, entity.BINGING_PLATFORM_OPER)
queryList = append(queryList, "")
queryList = append(queryList, entity.BINGING_PLATFORM_ORGAN)
}
err := DB.
Where(query, queryList...).
//Preload("BundleInfos").
Order(sortColumn).
Offset(genOffset(pageNo, pageSize)).Limit(pageSize).
Find(&list).Error
if err != nil {
return nil, 0, err
}
err = DB.Model(&BindingMysql{}).
Where(query, queryList...).
Count(&total).Error
if err != nil {
return nil, 0, err
}
}
} else {
query := ""
queryList := []interface{}{}
if cooperateStatus != "all" {
query = "(binding.name LIKE ? OR binding.group_name LIKE ? OR bundle_info.bundle_id LIKE ?) AND binding.cooperate_status_value = ? "
s := genLike(searchText)
queryList = []interface{}{s, s, s, cooperateStatus}
switch platform {
case entity.BINGING_PLATFORM_OPER:
query += " AND binding.platform = ? and binding.group_id = ?"
queryList = append(queryList, platform)
queryList = append(queryList, "")
case entity.BINGING_PLATFORM_ORGAN:
query += " AND binding.platform = ?"
queryList = append(queryList, platform)
default:
query += " AND ( ( binding.platform = ? AND binding.group_id = ? ) OR ( binding.platform = ? ) )"
queryList = append(queryList, entity.BINGING_PLATFORM_OPER)
queryList = append(queryList, "")
queryList = append(queryList, entity.BINGING_PLATFORM_ORGAN)
}
} else {
query = "(binding.name LIKE ? OR binding.group_name LIKE ? OR bundle_info.bundle_id LIKE ?) "
s := genLike(searchText)
queryList = []interface{}{s, s, s}
switch platform {
case entity.BINGING_PLATFORM_OPER:
query += " AND binding.platform = ? and binding.group_id = ?"
queryList = append(queryList, platform)
queryList = append(queryList, "")
case entity.BINGING_PLATFORM_ORGAN:
query += " AND binding.platform = ?"
queryList = append(queryList, platform)
default:
query += " AND ( ( binding.platform = ? AND binding.group_id = ? ) OR ( binding.platform = ? ) )"
queryList = append(queryList, entity.BINGING_PLATFORM_OPER)
queryList = append(queryList, "")
queryList = append(queryList, entity.BINGING_PLATFORM_ORGAN)
}
}
resultArr := []BindingsResult{}
/*err := mysql.MysqlDB.Table(dao.SDK_DATA_OPEN_INFO_C_NAME).Debug().Select("sum(app_open_count) as app_open_count, sum(total_app_stay_time) as total_app_stay_time, sum(app_close_count) as app_close_count, day_time").
Where(query, args...).Group("day_time").Order("day_time").
Scan(&resultArr).Error*/
err := DB.Debug().Model(&BindingMysql{}).
Joins("LEFT JOIN `bundle_info` ON `binding`.`binding_id` = `bundle_info`.`binding_id` ").
Select("binding.binding_id").
Where(query, queryList...).
Group("binding.binding_id").
Scan(&resultArr).Error
/*err := DB.Debug().Model(&BindingMysql{}).Preload("BundleInfos").
Joins("LEFT JOIN `bundle_info` `BundleInfos` ON `binding`.`binding_id` = `BundleInfos`.`binding_id` ").
Select("binding.*,GROUP_CONCAT(BundleInfos.bundle_id) AS `bundle_ids`").
Group("binding.binding_id").
Having(query, queryList...).
Order(sortColumn).
Offset(genOffset(pageNo, pageSize)).Limit(pageSize).
Find(&list).Error*/
if err != nil {
return nil, 0, err
}
fmt.Printf("resultArr = %s\n", utility.InterfaceToJsonString(resultArr))
/*t := DB.Model(&BindingMysql{}).Joins("LEFT JOIN `bundle_info` `BundleInfos` ON `binding`.`binding_id` = `BundleInfos`.`binding_id`").
Select("binding.*,GROUP_CONCAT(BundleInfos.bundle_id) AS `bundle_ids`").
Group("binding.binding_id").
Having(query, queryList...)
err = DB.Debug().Table("(?) t", t).Count(&total).Error*/
t := DB.Model(&BindingMysql{}).Joins("LEFT JOIN `bundle_info` ON `binding`.`binding_id` = `bundle_info`.`binding_id`").
Select("binding.binding_id").
Where(query, queryList...).
Group("binding.binding_id")
err = DB.Debug().Table("(?) t", t).Count(&total).Error
if err != nil {
return nil, 0, err
}
bindingIds := []string{}
for _, id := range resultArr {
bindingIds = append(bindingIds, id.BindingId)
}
query = "binding_id in (?)"
queryList = []interface{}{bindingIds}
err = DB.Model(&BindingMysql{}).
//Preload("BundleInfos").
Where(query, queryList...).
Order(sortColumn).
Offset(genOffset(pageNo, pageSize)).Limit(pageSize).
Find(&list).Error
if err != nil {
return nil, 0, err
}
}
bindings := make([]entity.Binding, 0)
for _, v := range list {
bundleInfos, linkAppInfos, _ := b.GetBundleInfoAndLinkAppInfo(ctx, v.BindingID)
if len(linkAppInfos) > 0 {
v.AppInfos = linkAppInfos
}
if len(bundleInfos) > 0 {
v.BundleInfos = bundleInfos
}
bindings = append(bindings, convertBindingMysqlToBinding(v))
}
return bindings, int(total), nil
}
func (b BindingByMysqlRepo) GetBindingsByAppId(ctx context.Context, pageSize int, pageNo int, appId string) ([]entity.Binding, int, error) {
list := make([]BindingMysql, 0)
var total int64
err := DB.Debug().Model(&BindingMysql{}).
//Preload("BundleInfos").Preload("AppInfos").
Where("link_app_infos.app_id = ?", appId).
Select("DISTINCT(binding.id), binding.*").
Joins("left join bundle_info on binding.binding_id = bundle_info.binding_id").
Joins("left join link_app_infos on binding.binding_id = link_app_infos.binding_id").
Order("binding.created_info_at DESC").
Offset(genOffset(pageNo, pageSize)).Limit(pageSize).
Find(&list).Error
if err != nil {
return nil, 0, err
}
err = DB.Debug().Model(&BindingMysql{}).
//Preload("BundleInfos").Preload("AppInfos").
Where("link_app_infos.app_id = ?", appId).
Select("DISTINCT(binding.id), binding.*").
Joins("left join bundle_info on binding.binding_id = bundle_info.binding_id").
Joins("left join link_app_infos on binding.binding_id = link_app_infos.binding_id").
Order("binding.created_info_at DESC").
Offset(genOffset(pageNo, pageSize)).Limit(pageSize).
Count(&total).Error
if err != nil {
return nil, 0, err
}
bindings := make([]entity.Binding, 0)
for _, v := range list {
bundleInfos, linkAppInfos, _ := b.GetBundleInfoAndLinkAppInfo(ctx, v.BindingID)
if len(linkAppInfos) > 0 {
v.AppInfos = linkAppInfos
}
if len(bundleInfos) > 0 {
v.BundleInfos = bundleInfos
}
bindings = append(bindings, convertBindingMysqlToBinding(v))
}
return bindings, int(total), nil
}
func (b BindingByMysqlRepo) GetDevListBinding(ctx context.Context, pageSize int, pageNo int, sort string, searchText string, pullType string, groupId string, bindStatus string, platform int) ([]entity.Binding, int, error) {
bindingMysqls := make([]BindingMysql, 0)
var sortColumn string
switch sort {
case "-created":
sortColumn = "created_info_at DESC" //按创建时间排序
case "-expire":
sortColumn = "expire DESC" //按过期时间倒序排序
case "expire":
sortColumn = "expire ASC" //按过期时间正序排序
default:
sortColumn = "created_info_at DESC" //按创建时间倒序排序
}
query := "group_id = ? "
args := []interface{}{groupId}
switch pullType {
case "recently-expire":
beginData := time.Now().UnixNano() / 1e6
endData := time.Now().AddDate(0, 0, 30).UnixNano() / 1e6
query = query + "AND expire > ? and expire < ? "
args = append(args, beginData, endData)
}
switch bindStatus {
case "Valid":
query = query + "AND cooperate_status_value = ? "
args = append(args, entity.StBindValid)
case "Invalid":
query = query + "AND cooperate_status_value = ? "
args = append(args, entity.StBindInvalid)
}
if searchText != "" {
query += "AND name LIKE ? "
args = append(args, "%"+searchText+"%")
}
switch platform {
case entity.BINGING_PLATFORM_OPER:
query += "AND platform = ? "
args = append(args, platform)
case entity.BINGING_PLATFORM_ORGAN:
query += "AND platform = ? "
args = append(args, platform)
default:
query += "AND ( ( platform = ? ) OR ( platform = ? ) ) "
args = append(args, entity.BINGING_PLATFORM_OPER)
args = append(args, entity.BINGING_PLATFORM_ORGAN)
}
var total int64
err := DB.Model(&BindingMysql{}).
Where(query, args...).
//Preload("BundleInfos").
//Preload("AppInfos").
Count(&total).
Order(sortColumn).
Offset((pageNo - 1) * pageSize).Limit(pageSize).
Find(&bindingMysqls).Error
if err != nil {
return nil, 0, err
}
bindings := make([]entity.Binding, 0)
for _, bindingMysql := range bindingMysqls {
bundleInfos, linkAppInfos, _ := b.GetBundleInfoAndLinkAppInfo(ctx, bindingMysql.BindingID)
if len(linkAppInfos) > 0 {
bindingMysql.AppInfos = linkAppInfos
}
if len(bundleInfos) > 0 {
bindingMysql.BundleInfos = bundleInfos
}
bindings = append(bindings, convertBindingMysqlToBinding(bindingMysql))
}
return bindings, int(total), err
}
func (b BindingByMysqlRepo) GetBundleInfoAndLinkAppInfo(ctx context.Context, bindingId string) ([]BundleInfo, []LinkAppInfo, error) {
tmpQuery := "binding_id = ?"
tmpArgs := []interface{}{bindingId}
var bundleInfos []BundleInfo
err := DB.Model(&BundleInfo{}).Where(tmpQuery, tmpArgs...).Find(&bundleInfos).Error
var linkAppInfos []LinkAppInfo
err = DB.Model(&LinkAppInfo{}).Where(tmpQuery, tmpArgs...).Find(&linkAppInfos).Error
return bundleInfos, linkAppInfos, err
}
func (b BindingByMysqlRepo) GetBindingBySdkKey(ctx context.Context, sdkKey string) (*entity.Binding, error) {
query := "sdk_key = ?"
args := []interface{}{sdkKey}
bundleInfoMysql := BundleInfo{}
err := DB.Model(&BundleInfo{}).Where(query, args...).First(&bundleInfoMysql).Error
if err != nil {
return nil, err
}
return b.GetInfo(ctx, bundleInfoMysql.BindingId)
}
func (b BindingByMysqlRepo) ListAutoBindAppBinding(ctx context.Context, groupId string) ([]string, error) {
query := "auto_bind = ? and platform = ? and group_id = ?"
args := []interface{}{1, entity.BINGING_PLATFORM_OPER, groupId}
bindingMysqls := make([]BindingMysql, 0)
err := DB.Model(&BindingMysql{}).Where(query, args...).Find(&bindingMysqls).Error
if err != nil {
return nil, err
}
result := []string{}
for _, binding := range bindingMysqls {
result = append(result, binding.BindingID)
}
return result, nil
}
func (b BindingByMysqlRepo) ListCopyOperBinding(ctx context.Context, bindingId string) ([]string, []entity.Binding, error) {
query := "platform = ? and from_binding_id = ?"
args := []interface{}{entity.BINGING_PLATFORM_OPER, bindingId}
bindingMysqls := make([]BindingMysql, 0)
err := DB.Model(&BindingMysql{}).Where(query, args...).Find(&bindingMysqls).Error
if err != nil {
return nil, nil, err
}
bindings := make([]entity.Binding, 0)
result := []string{}
for _, binding := range bindingMysqls {
result = append(result, binding.BindingID)
bindings = append(bindings, convertBindingMysqlToBinding(binding))
}
return result, bindings, nil
}
func (b BindingByMysqlRepo) ListBindings(ctx context.Context, groupId string, searchText string, pageNo int, pageSize int) ([]entity.Binding, int, error) {
log.Infof("ListBindings req,groupId:[%s],searchText:[%s],pageNo:[%d],pageSize:[%d]", searchText, groupId, pageNo, pageSize)
var (
sortColumn = "binding.created_info_at DESC"
list = make([]BindingMysql, 0)
total int64
)
if searchText == "" {
if groupId != "" {
err := DB.Debug().
Where("binding.group_id=?", groupId).
Order(sortColumn).
Offset(genOffset(pageNo, pageSize)).Limit(pageSize).
Find(&list).Error
if err != nil {
return nil, 0, err
}
err = DB.Model(&BindingMysql{}).
Where("binding.group_id=?", groupId).
Count(&total).Error
if err != nil {
return nil, 0, err
}
} else {
err := DB.Debug().
Where("binding.group_id <> ?", "").
//Preload("BundleInfos").
Order(sortColumn).
Offset(genOffset(pageNo, pageSize)).Limit(pageSize).
Find(&list).Error
if err != nil {
return nil, 0, err
}
err = DB.Model(&BindingMysql{}).
Where("binding.group_id <> ?", "").
Count(&total).Error
if err != nil {
return nil, 0, err
}
}
} else {
query := "(binding.name LIKE ? OR binding.group_name LIKE ? OR bundle_ids LIKE ?) "
s := genLike(searchText)
queryList := []interface{}{s, s, s}
if groupId != "" {
query += " AND binding.group_id=? "
queryList = append(queryList, groupId)
} else {
query += " AND binding.group_id <> ? "
queryList = append(queryList, "")
}
err := DB.Debug().Model(&BindingMysql{}).
//Select("select * from binding").
//Preload("BundleInfos").
Joins("LEFT JOIN `bundle_info` ON `binding`.`binding_id` = `bundle_info`.`binding_id` ").
Where("binding.name LIKE ? OR binding.group_name LIKE ? OR bundle_info.bundle_id LIKE ?", s, s, s).
//Select("binding.*,GROUP_CONCAT(bundle_id.bundle_id) AS `bundle_ids`").
Group("binding.id").
//Having(query, queryList...).
Order(sortColumn).
Offset(genOffset(pageNo, pageSize)).Limit(pageSize).
Find(&list).Error
if err != nil {
return nil, 0, err
}
t := DB.Model(&BindingMysql{}).
Joins("LEFT JOIN `bundle_info` ON `binding`.`binding_id` = `bundle_info`.`binding_id`").
//Select("binding.*,GROUP_CONCAT(bundle_info.bundle_id) AS `bundle_ids`").
Group("binding.id").
Where("binding.name LIKE ? OR binding.group_name LIKE ? OR bundle_info.bundle_id LIKE ?", s, s, s)
//Having(query, queryList...)
err = DB.Debug().Table("(?) t", t).Count(&total).Error
if err != nil {
return nil, 0, err
}
}
bindings := make([]entity.Binding, 0)
for _, v := range list {
bundleInfos, linkAppInfos, _ := b.GetBundleInfoAndLinkAppInfo(ctx, v.BindingID)
if len(linkAppInfos) > 0 {
v.AppInfos = linkAppInfos
}
if len(bundleInfos) > 0 {
v.BundleInfos = bundleInfos
}
bindings = append(bindings, convertBindingMysqlToBinding(v))
}
return bindings, int(total), nil
//
//db := DB.Model(&BindingMysql{})
//if groupId != "" {
// db = db.Where("binding.group_id = ?", groupId)
//}
//
//if searchText != "" {
// db = db.Where("binding.name LIKE ?", searchText).
// Or("binding.group_name LIKE ?", searchText).
// Or("bundle_info.bundle_id LIKE ?", searchText)
//}
//
//var total int
//bindingMysqls := make([]BindingMysql, 0)
//db = db.
// Select("DISTINCT(binding.id), binding.*").
// Joins("LEFT JOIN bundle_info on binding.binding_id = bundle_info.binding_id").
// Joins("LEFT JOIN link_app_infos on binding.binding_id = link_app_infos.binding_id").
// Find(&bindingMysqls)
//total = len(bindingMysqls)
//
//err := db.Order("created_info_at desc").Offset((pageNo - 1) * pageSize).Limit(pageSize).Find(&bindingMysqls).Error
//if err != nil {
// return nil, 0, err
//}
//bindings := make([]entity.Binding, 0)
//for _, bindingMysql := range bindingMysqls {
// bindings = append(bindings, convertBindingMysqlToBinding(bindingMysql))
//}
//return bindings, int(total), nil
}
func (b BindingByMysqlRepo) GetAllAssBinds(ctx context.Context, appId string) ([]entity.Binding, error) {
//bindings := make([]BindingMongo, 0)
//err := bindingTable.OnlyGetAll(ctx, bson.M{"app_infos.app_id": appId}, []string{}, &bindings)
//if err != nil {
// return nil, err
//}
//result := make([]entity.Binding, 0)
//for _, bindingMongo := range bindings {
// result = append(result, convertBindingMongoToBinding(bindingMongo))
//}
//return result, nil
list := make([]BindingMysql, 0)
err := DB.Debug().Model(&BindingMysql{}).
//Preload("BundleInfos").Preload("AppInfos").
Where("link_app_infos.app_id = ?", appId).
Select("DISTINCT(binding.id), binding.*").
Joins("left join bundle_info on binding.binding_id = bundle_info.binding_id").
Joins("left join link_app_infos on binding.binding_id = link_app_infos.binding_id").
Find(&list).Error
if err != nil {
return nil, err
}
bindings := make([]entity.Binding, 0)
for _, v := range list {
bundleInfos, linkAppInfos, _ := b.GetBundleInfoAndLinkAppInfo(ctx, v.BindingID)
if len(linkAppInfos) > 0 {
v.AppInfos = linkAppInfos
}
if len(bundleInfos) > 0 {
v.BundleInfos = bundleInfos
}
bindings = append(bindings, convertBindingMysqlToBinding(v))
}
return bindings, nil
}
func (b BindingByMysqlRepo) UpdateRelatedBindingCooperate(ctx context.Context, bindingId string, status entity.Status, specificStatus entity.SpecificStatus, cooperate bool) error {
query := "from_binding_id = ? and platform = ?"
args := []interface{}{bindingId, entity.BINGING_PLATFORM_OPER}
params := map[string]interface{}{}
if cooperate {
params = map[string]interface{}{
"cooperate_status_value": status.Value,
"cooperate_status_last_updated": status.LastUpdated,
"cooperate_status_modified_by": status.ModifiedBy,
"cooperate_valid_status_last_updated": specificStatus.LastUpdated,
"cooperate_valid_status_modified_by": specificStatus.ModifiedBy,
}
} else {
params = map[string]interface{}{
"cooperate_status_value": status.Value,
"cooperate_status_last_updated": status.LastUpdated,
"cooperate_status_modified_by": status.ModifiedBy,
"cooperate_invalid_status_last_updated": specificStatus.LastUpdated,
"cooperate_invalid_status_modified_by": specificStatus.ModifiedBy,
}
}
err := DB.Model(&BindingMysqlV2{}).Debug().
Where(query, args...).
Updates(params).Error
if err != nil {
return err
}
return nil
}
func (b BindingByMysqlRepo) UpdateBundleIdIsForbidden(ctx context.Context, bundleId string) error {
query := "binary bundle_id= ?"
args := []interface{}{bundleId}
params := map[string]interface{}{
"is_forbidden": 1,
}
err := DB.Model(&BundleInfo{}).Debug().
Where(query, args...).
Updates(params).Error
if err != nil {
return err
}
return nil
}
func (b BindingByMysqlRepo) UpdateBundleIdPlatform(ctx context.Context, bundleId, platform string) error {
query := "binary bundle_id= ?"
args := []interface{}{bundleId}
params := map[string]interface{}{
"remark": platform,
}
err := DB.Model(&BundleInfo{}).Debug().
Where(query, args...).
Updates(params).Error
if err != nil {
return err
}
return nil
}
func (b BindingByMysqlRepo) NotFound(err error) bool {
return err == gorm.ErrRecordNotFound || errors.Is(err, gorm.ErrRecordNotFound)
}
func (b BindingByMysqlRepo) UpdateBundleBindingId(ctx context.Context, bundleId, bindingId, toBindingId string) error {
query := "binding_id = ? and bundle_id= ?"
args := []interface{}{bindingId, bundleId}
params := map[string]interface{}{
"binding_id": toBindingId,
}
err := DB.Model(&BundleInfo{}).Debug().
Where(query, args...).
Updates(params).Error
if err != nil {
return err
}
return nil
}
func convertBindingMysqlToBinding(info BindingMysql) entity.Binding {
result := entity.Binding{}
bundleInfos := make([]entity.BundleInfo, 0)
for _, v := range info.BundleInfos {
item := entity.BundleInfo{
//Id: v.Id,
BundleID: v.BundleID,
Remark: v.Remark,
SDKKey: v.SDKKey,
SDKID: v.SDKID,
IsFirstCreate: v.IsFirstCreate,
CreatedAt: v.CreatedAt,
CreatedAccount: v.CreatedAccount,
CreatedBy: v.CreatedBy,
IsForbidden: v.IsForbidden,
IsReview: v.IsReview,
}
bundleInfos = append(bundleInfos, item)
}
createInfo := entity.CreatedInfo{
CreatedBy: info.CreatedInfoBy,
CreatedAt: info.CreatedInfoAt,
CreatedAccount: info.CreatedInfoAccount,
}
cooperateStatus := entity.Status{
Value: info.CooperateStatusValue,
Reason: info.CooperateStatusReason,
LastUpdated: info.CooperateStatusLastUpdated,
ModifiedBy: info.CooperateStatusModifiedBy,
}
cooperateValidStatus := entity.SpecificStatus{
Reason: info.CooperateValidStatusReason,
LastUpdated: info.CooperateValidStatusLastUpdated,
ModifiedBy: info.CooperateValidStatusModifiedBy,
}
cooperateInvalidStatus := entity.SpecificStatus{
Reason: info.CooperateInvalidStatusReason,
LastUpdated: info.CooperateInvalidStatusLastUpdated,
ModifiedBy: info.CooperateInvalidStatusModifiedBy,
}
//var appInfos []entity.AppInfo
//json.Unmarshal([]byte(bindingMysql.AppInfos), &appInfos)
result.BindingID = info.BindingID
result.Name = info.Name
result.BundleInfos = bundleInfos
result.CreatedInfo = createInfo
result.GroupID = info.GroupID
result.GroupName = info.GroupName
result.CooperateStatus = cooperateStatus
result.CooperateValidStatus = cooperateValidStatus
result.CooperateInvalidStatus = cooperateInvalidStatus
//result.AppInfos = appInfos
result.Owner = info.Owner
result.Expire = info.Expire
result.ApiServer = info.ApiServer
result.AppInfos = make([]entity.AppInfo, 0)
for _, v := range info.AppInfos {
item := entity.AppInfo{
//Id: v.Id,
AppID: v.AppId,
AssociatedAt: v.AssociatedAt,
AssociatedBy: v.AssociatedBy,
}
result.AppInfos = append(result.AppInfos, item)
}
result.GroupID = info.GroupID
result.PlatForm = info.PlatForm
result.AutoBind = info.AutoBind
result.HiddenBundle = info.HiddenBundle
result.FromBindingID = info.FromBindingID
return result
}
func convertBindingToBindingMysql(binding *entity.Binding) BindingMysql {
result := BindingMysql{}
result.BindingID = binding.BindingID
result.Name = binding.Name
result.CreatedInfoBy = binding.CreatedInfo.CreatedBy
result.CreatedInfoAt = binding.CreatedInfo.CreatedAt
result.CreatedInfoAccount = binding.CreatedInfo.CreatedAccount
result.GroupID = binding.GroupID
result.GroupName = binding.GroupName
result.CooperateStatusValue = binding.CooperateStatus.Value
result.CooperateStatusReason = binding.CooperateStatus.Reason
result.CooperateStatusLastUpdated = binding.CooperateStatus.LastUpdated
result.CooperateStatusModifiedBy = binding.CooperateStatus.ModifiedBy
result.CooperateValidStatusReason = binding.CooperateValidStatus.Reason
result.CooperateValidStatusLastUpdated = binding.CooperateValidStatus.LastUpdated
result.CooperateValidStatusModifiedBy = binding.CooperateValidStatus.ModifiedBy
result.CooperateInvalidStatusReason = binding.CooperateInvalidStatus.Reason
result.CooperateInvalidStatusLastUpdated = binding.CooperateInvalidStatus.LastUpdated
result.CooperateInvalidStatusModifiedBy = binding.CooperateInvalidStatus.ModifiedBy
result.AppInfos = make([]LinkAppInfo, 0)
for _, v := range binding.AppInfos {
item := LinkAppInfo{
//Id: v.Id,
BindingId: binding.BindingID,
AppId: v.AppID,
AssociatedAt: v.AssociatedAt,
AssociatedBy: v.AssociatedBy,
}
result.AppInfos = append(result.AppInfos, item)
}
result.BundleInfos = make([]BundleInfo, 0)
for _, v := range binding.BundleInfos {
item := BundleInfo{
//Id: v.Id,
BindingId: binding.BindingID,
GroupID: binding.GroupID,
BundleID: v.BundleID,
Remark: v.Remark,
SDKKey: v.SDKKey,
SDKID: v.SDKID,
IsFirstCreate: v.IsFirstCreate,
CreatedAt: v.CreatedAt,
CreatedAccount: v.CreatedAccount,
CreatedBy: v.CreatedBy,
IsForbidden: v.IsForbidden,
IsReview: v.IsReview,
}
result.BundleInfos = append(result.BundleInfos, item)
}
result.Owner = binding.Owner
result.Expire = binding.Expire
result.ApiServer = binding.ApiServer
result.GroupID = binding.GroupID
result.PlatForm = binding.PlatForm
result.AutoBind = binding.AutoBind
result.HiddenBundle = binding.HiddenBundle
result.FromBindingID = binding.FromBindingID
return result
}
func convertBindingMysqlToBindingMysqlV2(binding *BindingMysql) BindingMysqlV2 {
result := BindingMysqlV2{}
result.BindingID = binding.BindingID
result.Name = binding.Name
result.CreatedInfoBy = binding.CreatedInfoBy
result.CreatedInfoAt = binding.CreatedInfoAt
result.CreatedInfoAccount = binding.CreatedInfoAccount
result.GroupID = binding.GroupID
result.GroupName = binding.GroupName
result.CooperateStatusValue = binding.CooperateStatusValue
result.CooperateStatusReason = binding.CooperateStatusReason
result.CooperateStatusLastUpdated = binding.CooperateStatusLastUpdated
result.CooperateStatusModifiedBy = binding.CooperateStatusModifiedBy
result.CooperateValidStatusReason = binding.CooperateValidStatusReason
result.CooperateValidStatusLastUpdated = binding.CooperateValidStatusLastUpdated
result.CooperateValidStatusModifiedBy = binding.CooperateValidStatusModifiedBy
result.CooperateInvalidStatusReason = binding.CooperateInvalidStatusReason
result.CooperateInvalidStatusLastUpdated = binding.CooperateInvalidStatusLastUpdated
result.CooperateInvalidStatusModifiedBy = binding.CooperateInvalidStatusModifiedBy
result.Owner = binding.Owner
result.Expire = binding.Expire
result.ApiServer = binding.ApiServer
result.GroupID = binding.GroupID
result.PlatForm = binding.PlatForm
result.AutoBind = binding.AutoBind
result.HiddenBundle = binding.HiddenBundle
result.FromBindingID = binding.FromBindingID
return result
}
func convertBundleInfoToBundleInfoMysql(bindId, groupId string, bundleInfo entity.BundleInfo) BundleInfo {
result := BundleInfo{}
result.BindingId = bindId
result.GroupID = groupId
result.BundleID = bundleInfo.BundleID
result.Remark = bundleInfo.Remark
result.SDKKey = bundleInfo.SDKKey
result.SDKID = bundleInfo.SDKID
result.IsFirstCreate = bundleInfo.IsFirstCreate
result.CreatedAt = bundleInfo.CreatedAt
result.CreatedAccount = bundleInfo.CreatedAccount
result.CreatedBy = bundleInfo.CreatedBy
result.IsForbidden = bundleInfo.IsForbidden
return result
}
func convertBundleInfoMysqlToBundleInfo(bundleInfoMysql BundleInfo) entity.BundleInfo {
result := entity.BundleInfo{}
result.BundleID = bundleInfoMysql.BundleID
result.Remark = bundleInfoMysql.Remark
result.SDKKey = bundleInfoMysql.SDKKey
result.SDKID = bundleInfoMysql.SDKID
result.IsFirstCreate = bundleInfoMysql.IsFirstCreate
result.CreatedAt = bundleInfoMysql.CreatedAt
result.CreatedAccount = bundleInfoMysql.CreatedAccount
result.CreatedBy = bundleInfoMysql.CreatedBy
return result
}