package mysql import ( "context" "finclip-app-manager/domain/entity" "finclip-app-manager/domain/repository" "gorm.io/gorm" ) var _ repository.IBundleRepo = new(BundleByMysqlRepo) type BundleByMysqlRepo struct { } type BundleMysql 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:secert"` 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:tinyint(1);default:0;comment:是否禁用"` //是否禁用 0:可用 1:禁用 } func (BundleMysql) TableName() string { return "bundle" } func (b BundleByMysqlRepo) Insert(ctx context.Context, bundles []entity.Bundle) error { return DB.Transaction(func(tx *gorm.DB) error { var err error for _, v := range bundles { info := convertBundleToBundleMysql(v) err = DB.WithContext(ctx).Model(&BundleMysql{}).Debug().Create(&info).Error } return err }) } func (b BundleByMysqlRepo) GetListByBundleId(ctx context.Context, ids []string, pageNo int, pageSize int) (int, []entity.Bundle, error) { query := "binary bundle_id in ?" args := []interface{}{ids} bundleMysqls := make([]BundleMysql, 0) err := DB.Model(&BundleMysql{}).Where(query, args...).Offset((pageNo - 1) * pageSize). Limit(pageSize).Find(&bundleMysqls).Error if b.NotFound(err) { return 0, []entity.Bundle{}, nil } var count int64 err = DB.Model(&BundleMysql{}).Count(&count).Error if err != nil { return 0, []entity.Bundle{}, nil } bundles := make([]entity.Bundle, 0) for _, bundleMysql := range bundleMysqls { bundles = append(bundles, convertBundleMysqlToBundle(bundleMysql)) } return int(count), bundles, err } func (b BundleByMysqlRepo) GetListByBundleIds(ctx context.Context, ids []string) ([]entity.Bundle, error) { bundles := make([]BundleMysql, 0) err := DB.WithContext(ctx).Debug(). Model(&BundleMysql{}). Where("binary bundle_id IN (?)", ids).Find(&bundles).Error if err != nil { if b.NotFound(err) { return []entity.Bundle{}, nil } } result := make([]entity.Bundle, 0) for _, v := range bundles { result = append(result, convertBundleMysqlToBundle(v)) } return result, err } func (b BundleByMysqlRepo) ExistBundleId(ctx context.Context, bundleId string) (bool, error) { var ( total int64 err error ) err = DB.Model(&BundleMysql{}).Where("binary bundle_id=?", bundleId).Count(&total).Error if err != nil { return false, err } return total > 0, nil } func (b BundleByMysqlRepo) GetInfoByBundleId(ctx context.Context, bundleId string) (entity.Bundle, error) { info := BundleMysql{} err := DB.Debug().Model(&BundleMysql{}).Where("binary bundle_id=?", bundleId).First(&info).Error if err != nil { return entity.Bundle{}, err } return convertBundleMysqlToBundle(info), err } func (b BundleByMysqlRepo) ListAllBundleInfos(ctx context.Context, searchText string, selectType, pageNo int, pageSize int) (int, []entity.Bundle, error) { var ( err error total int64 list = make([]BundleMysql, 0) ) query := "" args := []interface{}{} if searchText != "" { query += "bundle_id LIKE ? " args = []interface{}{searchText} } if selectType == 1 { if query == "" { query += " is_forbidden=? " } else { query += " and is_forbidden=? " } args = append(args, 0) } if selectType == 2 { if query == "" { query += " is_forbidden=? " } else { query += " and is_forbidden=? " } args = append(args, 1) } /*if searchText != "" { err = DB.WithContext(ctx).Model(&BundleMysql{}). Where("bundle_id LIKE ?", genLike(searchText)). Count(&total). Order("created_at DESC"). Offset(genOffset(pageNo, pageSize)).Limit(pageSize). Find(&list).Error }*/ err = DB.WithContext(ctx).Model(&BundleMysql{}).Debug().Where(query, args...). Count(&total). Order("created_at DESC"). Offset(genOffset(pageNo, pageSize)).Limit(pageSize). Find(&list).Error if err != nil { return 0, nil, err } result := make([]entity.Bundle, 0) for _, v := range list { temp := v result = append(result, convertBundleMysqlToBundle(temp)) } return int(total), result, nil } func (b BundleByMysqlRepo) AllCount(ctx context.Context) (int, error) { var total int64 err := DB.Model(&BundleMysql{}).Where("is_forbidden=?", 0).Count(&total).Error return int(total), err } func (b BundleByMysqlRepo) UpdateBundleForbidden(ctx context.Context, bundleId string, IsForbidden int) error { params := map[string]interface{}{ "is_forbidden": IsForbidden, } return DB.Model(&BundleMysql{}).Where("binary bundle_id=?", bundleId).Updates(params).Error } func (b BundleByMysqlRepo) UpdateBundlePlatform(ctx context.Context, bundleId, remark string) error { params := map[string]interface{}{ "remark": remark, } return DB.Model(&BundleMysql{}).Where("binary bundle_id=?", bundleId).Updates(params).Error } func (b BundleByMysqlRepo) NotFound(err error) bool { return err == gorm.ErrRecordNotFound } func convertBundleToBundleMysql(bundleInfo entity.Bundle) BundleMysql { result := BundleMysql{} 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 convertBundleMysqlToBundle(bundleMysql BundleMysql) entity.Bundle { result := entity.Bundle{} result.BundleID = bundleMysql.BundleID result.Remark = bundleMysql.Remark result.SDKKey = bundleMysql.SDKKey result.SDKID = bundleMysql.SDKID result.IsFirstCreate = bundleMysql.IsFirstCreate result.CreatedAt = bundleMysql.CreatedAt result.CreatedAccount = bundleMysql.CreatedAccount result.CreatedBy = bundleMysql.CreatedBy result.IsForbidden = bundleMysql.IsForbidden return result }