package mysql import ( "context" "encoding/json" "errors" "finclip-app-manager/domain/entity" "finclip-app-manager/domain/repository" "finclip-app-manager/infrastructure/logger" "gorm.io/gorm" "time" ) var _ repository.IQrCodeInfoRepo = new(QrCodeInfoByMysqlRepo) type QrCodeInfoByMysqlRepo struct { } type QrCodeInfoMysql struct { //Id uint `bson:"id" gorm:"primary_key;column:id" sql:"auto_increment;primary_key;unique"` ID uint64 `json:"id" gorm:"primary_key;column:id;type:BIGINT(16) AUTO_INCREMENT;comment:'自增id'" sql:"auto_increment;primary_key"` Type string `json:"type" gorm:"column:type;type:varchar(64);comment:二维码的类型"` Uuid string `json:"uuid" gorm:"column:uuid;type:varchar(64);comment:标识该二维码"` AppId string `json:"appId" gorm:"column:app_id;type:varchar(64);comment:小程序Id"` Sequence int `json:"sequence" gorm:"column:sequence;type:int(6);comment:小程序序列号"` ApiServer string `json:"apiServer" gorm:"column:api_server;type:varchar(256);comment:小程序apiServer"` CodeId string `json:"codeId" gorm:"column:code_id;type:varchar(64);comment:标识某个编译版本的id"` PathAndQuery string `json:"pathAndQuery" gorm:"column:path_and_query;type:varchar(512);comment:小程序启动参数"` ExpireTime int64 `json:"expireTime" gorm:"column:expire_time;type:BIGINT(16);comment:过期时间"` CreateTime int64 `json:"createTime" gorm:"column:create_time;type:BIGINT(16);comment:创建时间"` UpdateTime int64 `json:"updateTime" gorm:"column:update_time;type:BIGINT(16);comment:更新时间"` DeleteTime int64 `json:"deleteTime" gorm:"column:delete_time;type:BIGINT(16);comment:删除时间"` DebugInfo string `json:"debugInfo" gorm:"column:debug_info;type:text;comment:扩展数据"` } func (QrCodeInfoMysql) TableName() string { return "qr_code_info" } func (q QrCodeInfoByMysqlRepo) Insert(ctx context.Context, info *entity.QrCodeInfo) error { sqlInfo := convertQrCodeInfoToQrCodeInfoMysql(info) return DB.Model(&QrCodeInfoMysql{}).Create(&sqlInfo).Error } func (q QrCodeInfoByMysqlRepo) GenInfo(ctx context.Context, info *entity.QrCodeInfo) error { switch info.Type { case entity.QrCodeTypeReview: query := "type = ? and app_id = ? and sequence = ?" args := []interface{}{info.Type, info.AppId, info.Sequence} var count int64 err := DB.Model(&QrCodeInfoMysql{}).Where(query, args...).Count(&count).Error if err != nil { return err } //如果存在就不再进行插入 if count > 0 { return nil } case entity.QrCodeTypeRelease: query := "type = ? and app_id = ?" args := []interface{}{info.Type, info.AppId} var count int64 err := DB.Model(&QrCodeInfoMysql{}).Where(query, args...).Count(&count).Error if err != nil { return err } //如果已经存在,更新就好 if count > 0 { query := "type = ? and app_id = ?" args := []interface{}{info.Type, info.AppId} return DB.Model(&QrCodeInfoMysql{}).Where(query, args...).Update("update_time", time.Now().UnixNano()/1e6).Error } case entity.QrCodeTypeTrial: query := "type = ? and app_id = ?" args := []interface{}{info.Type, info.AppId} var count int64 err := DB.Model(&QrCodeInfoMysql{}).Where(query, args...).Count(&count).Error if err != nil { return err } if count > 0 { query := "type = ? and app_id = ?" args := []interface{}{info.Type, info.AppId} return DB.Model(&QrCodeInfoMysql{}).Where(query, args...).Updates(map[string]interface{}{"path_and_query": "", "code_id": info.CodeId, "update_time": time.Now().UnixNano() / 1e6}).Error } case entity.QrCodeTypeTemporary: //直接插入 default: return errors.New("info type err") } //不存在就插入 sqlInfo := convertQrCodeInfoToQrCodeInfoMysql(info) return DB.Debug().Model(&QrCodeInfoMysql{}).Create(&sqlInfo).Error } func (q QrCodeInfoByMysqlRepo) GetInfoByUuid(ctx context.Context, uuid string) (*entity.QrCodeInfo, error) { query := "uuid = ?" args := []interface{}{uuid} qrCodeInfoMysql := QrCodeInfoMysql{} err := DB.Model(&QrCodeInfoMysql{}).Where(query, args...).First(&qrCodeInfoMysql).Error if err != nil { return nil, err } result := convertQrCodeInfoMysqlToQrCodeInfo(qrCodeInfoMysql) return &result, err } func (q QrCodeInfoByMysqlRepo) GetInfoByCodeId(ctx context.Context, codeId string) (*entity.QrCodeInfo, error) { query := "code_id = ?" args := []interface{}{codeId} qrCodeInfoMysql := QrCodeInfoMysql{} err := DB.Model(&QrCodeInfoMysql{}).Where(query, args...).First(&qrCodeInfoMysql).Error if err != nil { return nil, err } result := convertQrCodeInfoMysqlToQrCodeInfo(qrCodeInfoMysql) return &result, err } func (q QrCodeInfoByMysqlRepo) GetReviewInfo(ctx context.Context, appId string, seq int) (*entity.QrCodeInfo, error) { query := "type = ? and app_id = ? and sequence = ?" args := []interface{}{entity.QrCodeTypeReview, appId, seq} qrCodeInfoMysql := QrCodeInfoMysql{} err := DB.Model(&QrCodeInfoMysql{}).Where(query, args...).First(&qrCodeInfoMysql).Error if err != nil { return nil, err } result := convertQrCodeInfoMysqlToQrCodeInfo(qrCodeInfoMysql) return &result, err } func (q QrCodeInfoByMysqlRepo) GetReleaseInfo(ctx context.Context, appId string) (*entity.QrCodeInfo, error) { query := "type = ? and app_id = ?" args := []interface{}{entity.QrCodeTypeRelease, appId} qrCodeInfoMysql := QrCodeInfoMysql{} err := DB.Model(&QrCodeInfoMysql{}).Where(query, args...).First(&qrCodeInfoMysql).Error if err != nil { return nil, err } result := convertQrCodeInfoMysqlToQrCodeInfo(qrCodeInfoMysql) return &result, err } func (q QrCodeInfoByMysqlRepo) GetTrialInfo(ctx context.Context, appId string) (*entity.QrCodeInfo, error) { query := "type = ? and app_id = ?" args := []interface{}{entity.QrCodeTypeTrial, appId} qrCodeInfoMysql := QrCodeInfoMysql{} err := DB.Model(&QrCodeInfoMysql{}).Where(query, args...).First(&qrCodeInfoMysql).Error if err != nil { return nil, err } result := convertQrCodeInfoMysqlToQrCodeInfo(qrCodeInfoMysql) return &result, err } func (q QrCodeInfoByMysqlRepo) GetTemporaryInfo(ctx context.Context, appId string, seq int) (*entity.QrCodeInfo, error) { query := "type = ? and app_id = ? and sequence = ?" args := []interface{}{entity.QrCodeTypeTemporary, appId, seq} qrCodeInfoMysql := QrCodeInfoMysql{} err := DB.Model(&QrCodeInfoMysql{}).Where(query, args...).First(&qrCodeInfoMysql).Error if err != nil { return nil, err } result := convertQrCodeInfoMysqlToQrCodeInfo(qrCodeInfoMysql) return &result, err } func (q QrCodeInfoByMysqlRepo) GetRemoteDebugInfo(ctx context.Context, appId string, seq int) (*entity.QrCodeInfo, error) { query := "type = ? and app_id = ? and sequence = ?" args := []interface{}{entity.QrCodeTypeRomoteDebug, appId, seq} qrCodeInfoMysql := QrCodeInfoMysql{} err := DB.Model(&QrCodeInfoMysql{}).Where(query, args...).First(&qrCodeInfoMysql).Error if err != nil { return nil, err } result := convertQrCodeInfoMysqlToQrCodeInfo(qrCodeInfoMysql) return &result, err } func (q QrCodeInfoByMysqlRepo) UpdateTrialStartParams(ctx context.Context, codeId string, p entity.AppStartParams) error { query := "type = ? and code_id = ?" args := []interface{}{entity.QrCodeTypeTrial, codeId} return DB.Model(&QrCodeInfoMysql{}).Where(query, args...).Update("path_and_query", p.PathAndQuery).Error } func (q QrCodeInfoByMysqlRepo) UpdateStartParamsByUuid(ctx context.Context, uuid string, p entity.AppStartParams) error { query := "uuid = ?" args := []interface{}{uuid} return DB.Model(&QrCodeInfoMysql{}).Where(query, args...).Update("path_and_query", p.PathAndQuery).Error } func (q QrCodeInfoByMysqlRepo) UpdateApiServer(ctx context.Context, uuid string, apiServer string) error { query := "uuid = ?" args := []interface{}{uuid} return DB.Model(&QrCodeInfoMysql{}).Where(query, args...).Update("api_server", apiServer).Error } func (q QrCodeInfoByMysqlRepo) UpdateInfo(ctx context.Context, uuid string, upInfo map[string]interface{}) error { query := "uuid = ?" args := []interface{}{uuid} return DB.Model(&QrCodeInfoMysql{}).Where(query, args...).UpdateColumns(upInfo).Error } func (q QrCodeInfoByMysqlRepo) GenReviewQrCodeInfo(ctx context.Context, info *entity.QrCodeInfo) error { return nil } func (q QrCodeInfoByMysqlRepo) NotFound(err error) bool { return err == gorm.ErrRecordNotFound } func convertQrCodeInfoToQrCodeInfoMysql(qrCodeInfo *entity.QrCodeInfo) QrCodeInfoMysql { result := QrCodeInfoMysql{} result.Type = qrCodeInfo.Type result.Uuid = qrCodeInfo.Uuid result.AppId = qrCodeInfo.AppId result.Sequence = qrCodeInfo.Sequence result.ApiServer = qrCodeInfo.ApiServer result.CodeId = qrCodeInfo.CodeId result.PathAndQuery = qrCodeInfo.StartParams.PathAndQuery result.ExpireTime = qrCodeInfo.ExpireTime result.CreateTime = qrCodeInfo.CreateTime result.UpdateTime = qrCodeInfo.UpdateTime result.DeleteTime = qrCodeInfo.DeleteTime b, err := json.Marshal(qrCodeInfo.DebugInfo) if err != nil { logger.GetLogger().Errorf("convertQrCodeInfoToQrCodeInfoMysql json.Marshal err:%s", err.Error()) result.DebugInfo = "{}" } else { result.DebugInfo = string(b) } return result } func convertQrCodeInfoMysqlToQrCodeInfo(qrCodeInfoMysql QrCodeInfoMysql) entity.QrCodeInfo { result := entity.QrCodeInfo{} appStartParams := entity.AppStartParams{ PathAndQuery: qrCodeInfoMysql.PathAndQuery, } result.Type = qrCodeInfoMysql.Type result.Uuid = qrCodeInfoMysql.Uuid result.AppId = qrCodeInfoMysql.AppId result.Sequence = qrCodeInfoMysql.Sequence result.ApiServer = qrCodeInfoMysql.ApiServer result.CodeId = qrCodeInfoMysql.CodeId result.StartParams = appStartParams result.ExpireTime = qrCodeInfoMysql.ExpireTime result.CreateTime = qrCodeInfoMysql.CreateTime result.UpdateTime = qrCodeInfoMysql.UpdateTime result.DeleteTime = qrCodeInfoMysql.DeleteTime debugInfo := make(map[string]interface{}) err := json.Unmarshal([]byte(qrCodeInfoMysql.DebugInfo), &debugInfo) if err != nil { logger.GetLogger().Errorf("convertQrCodeInfoMysqlToQrCodeInfo json.Unmarshal err:%s", err.Error()) } result.DebugInfo = debugInfo return result }