package mongo import ( "context" "errors" "finclip-app-manager/domain/entity" "finclip-app-manager/domain/repository" "finclip-app-manager/infrastructure/utility" "finclip-app-manager/infrastructure/utils" mgo "gitlab.finogeeks.club/finclip-backend-v2/finclip-mgo" "gitlab.finogeeks.club/finclip-backend-v2/finclip-mgo/bson" "time" ) var _ repository.IQrCodeInfoRepo = new(QrCodeInfoByMongoRepo) type QrCodeInfoByMongoRepo struct { } type QrCodeInfoMongo struct { Type string `json:"type" bson:"type"` //二维码的类型 Uuid string `json:"uuid" bson:"uuid"` //标识该二维码 AppId string `json:"appId" bson:"app_id"` //小程序Id Sequence int `json:"sequence" bson:"sequence"` //小程序序列号 ApiServer string `json:"apiServer" bson:"api_server"` //小程序apiServer CodeId string `json:"codeId" bson:"code_id"` //标识某个编译版本的id PathAndQuery string `json:"pathAndQuery" bson:"path_and_query"` //小程序启动参数 ExpireTime int64 `json:"expireTime" bson:"expire_time"` //过期时间 CreateTime int64 `json:"createTime" bson:"create_time"` //创建时间 UpdateTime int64 `json:"updateTime" bson:"Update_time"` //更新时间 DeleteTime int64 `json:"deleteTime" bson:"Delete_time"` //删除时间-暂不用 DebugInfo map[string]interface{} `json:"debugInfo" bson:"debug_info"` //拓展数据 } func (q QrCodeInfoByMongoRepo) Insert(ctx context.Context, info *entity.QrCodeInfo) error { return qrCodeInfoTable.Insert(ctx, convertQrCodeInfoToQrCodeInfoMongo(info)) } func (q QrCodeInfoByMongoRepo) GenInfo(ctx context.Context, info *entity.QrCodeInfo) error { switch info.Type { case entity.QrCodeTypeReview: count, err := qrCodeInfoTable.Count(ctx, bson.M{"type": info.Type, "app_id": info.AppId, "sequence": info.Sequence}) if err != nil { return err } //如果存在就不再进行插入 if count > 0 { return nil } case entity.QrCodeTypeRelease: count, err := qrCodeInfoTable.Count(ctx, bson.M{"type": info.Type, "app_id": info.AppId}) if err != nil { return err } //如果已经存在,更新就好 if count > 0 { return qrCodeInfoTable.UpdateData(bson.M{"type": info.Type, "app_id": info.AppId}, bson.M{"$set": bson.M{"update_time": time.Now().UnixNano() / 1e6}}) } case entity.QrCodeTypeTrial: count, err := qrCodeInfoTable.Count(ctx, bson.M{"type": info.Type, "app_id": info.AppId}) if err != nil { return err } if count > 0 { upInfo := bson.M{"$set": bson.M{"path_and_query": "", "code_id": info.CodeId, "update_time": utils.GetNowMs()}} return qrCodeInfoTable.UpdateData(bson.M{"type": info.Type, "app_id": info.AppId}, upInfo) } case entity.QrCodeTypeTemporary: //直接插入 default: return errors.New("info type err") } //不存在就插入 return qrCodeInfoTable.Insert(ctx, convertQrCodeInfoToQrCodeInfoMongo(info)) } func (q QrCodeInfoByMongoRepo) GetInfoByUuid(ctx context.Context, uuid string) (*entity.QrCodeInfo, error) { qrCodeInfoMongo := QrCodeInfoMongo{} err := qrCodeInfoTable.GetOne(ctx, bson.M{"uuid": uuid}, &qrCodeInfoMongo) log.Infof(utility.InterfaceToJsonString(qrCodeInfoMongo)) if err != nil { return nil, err } result := convertQrCodeInfoMongoToQrCodeInfo(qrCodeInfoMongo) return &result, err } func (q QrCodeInfoByMongoRepo) GetInfoByCodeId(ctx context.Context, codeId string) (*entity.QrCodeInfo, error) { qrCodeInfoMongo := QrCodeInfoMongo{} err := qrCodeInfoTable.GetOne(ctx, bson.M{"code_id": codeId}, &qrCodeInfoMongo) if err != nil { return nil, err } result := convertQrCodeInfoMongoToQrCodeInfo(qrCodeInfoMongo) return &result, err } func (q QrCodeInfoByMongoRepo) GetReviewInfo(ctx context.Context, appId string, seq int) (*entity.QrCodeInfo, error) { qrCodeInfoMongo := QrCodeInfoMongo{} err := qrCodeInfoTable.GetOne(ctx, bson.M{"type": entity.QrCodeTypeReview, "app_id": appId, "sequence": seq}, &qrCodeInfoMongo) if err != nil { return nil, err } result := convertQrCodeInfoMongoToQrCodeInfo(qrCodeInfoMongo) return &result, err } func (q QrCodeInfoByMongoRepo) GetReleaseInfo(ctx context.Context, appId string) (*entity.QrCodeInfo, error) { qrCodeInfoMongo := QrCodeInfoMongo{} err := qrCodeInfoTable.GetOne(ctx, bson.M{"type": entity.QrCodeTypeRelease, "app_id": appId}, &qrCodeInfoMongo) if err != nil { return nil, err } result := convertQrCodeInfoMongoToQrCodeInfo(qrCodeInfoMongo) return &result, err } func (q QrCodeInfoByMongoRepo) GetTrialInfo(ctx context.Context, appId string) (*entity.QrCodeInfo, error) { qrCodeInfoMongo := QrCodeInfoMongo{} err := qrCodeInfoTable.GetOne(ctx, bson.M{"type": entity.QrCodeTypeTrial, "app_id": appId}, &qrCodeInfoMongo) if err != nil { return nil, err } result := convertQrCodeInfoMongoToQrCodeInfo(qrCodeInfoMongo) return &result, err } func (q QrCodeInfoByMongoRepo) GetTemporaryInfo(ctx context.Context, appId string, seq int) (*entity.QrCodeInfo, error) { qrCodeInfoMongo := QrCodeInfoMongo{} err := qrCodeInfoTable.GetOne(ctx, bson.M{"type": entity.QrCodeTypeTemporary, "app_id": appId}, &qrCodeInfoMongo) if err != nil { return nil, err } result := convertQrCodeInfoMongoToQrCodeInfo(qrCodeInfoMongo) return &result, err } func (q QrCodeInfoByMongoRepo) GetRemoteDebugInfo(ctx context.Context, appId string, seq int) (*entity.QrCodeInfo, error) { qrCodeInfoMongo := QrCodeInfoMongo{} err := qrCodeInfoTable.GetOne(ctx, bson.M{"type": entity.QrCodeTypeRomoteDebug, "app_id": appId, "sequence": seq}, &qrCodeInfoMongo) if err != nil { return nil, err } result := convertQrCodeInfoMongoToQrCodeInfo(qrCodeInfoMongo) return &result, err } func (q QrCodeInfoByMongoRepo) UpdateTrialStartParams(ctx context.Context, codeId string, p entity.AppStartParams) error { return qrCodeInfoTable.UpdateData(bson.M{"type": entity.QrCodeTypeTrial, "code_id": codeId}, bson.M{"$set": bson.M{"path_and_query": p.PathAndQuery}}) } func (q QrCodeInfoByMongoRepo) UpdateStartParamsByUuid(ctx context.Context, uuid string, p entity.AppStartParams) error { return qrCodeInfoTable.UpdateData(bson.M{"uuid": uuid}, bson.M{"$set": bson.M{"path_and_query": p.PathAndQuery}}) } func (q QrCodeInfoByMongoRepo) UpdateApiServer(ctx context.Context, uuid string, apiServer string) error { return qrCodeInfoTable.UpdateData(bson.M{"uuid": uuid}, bson.M{"$set": bson.M{"api_server": apiServer}}) } func (q QrCodeInfoByMongoRepo) UpdateInfo(ctx context.Context, uuid string, upInfo map[string]interface{}) error { params := make(map[string]interface{}) if t, ok := upInfo["expire_time"]; ok { params["expire_time"] = t } return qrCodeInfoTable.UpdateData(bson.M{"uuid": uuid}, bson.M{"$set": params}) } func (q QrCodeInfoByMongoRepo) GenReviewQrCodeInfo(ctx context.Context, info *entity.QrCodeInfo) error { return nil } func (q QrCodeInfoByMongoRepo) NotFound(err error) bool { return err == mgo.ErrNotFound } func convertQrCodeInfoToQrCodeInfoMongo(qrCodeInfo *entity.QrCodeInfo) QrCodeInfoMongo { result := QrCodeInfoMongo{} 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 result.DebugInfo = qrCodeInfo.DebugInfo return result } func convertQrCodeInfoMongoToQrCodeInfo(qrCodeInfoMongo QrCodeInfoMongo) entity.QrCodeInfo { result := entity.QrCodeInfo{} appStartParams := entity.AppStartParams{ PathAndQuery: qrCodeInfoMongo.PathAndQuery, } result.Type = qrCodeInfoMongo.Type result.Uuid = qrCodeInfoMongo.Uuid result.AppId = qrCodeInfoMongo.AppId result.Sequence = qrCodeInfoMongo.Sequence result.ApiServer = qrCodeInfoMongo.ApiServer result.CodeId = qrCodeInfoMongo.CodeId result.StartParams = appStartParams result.ExpireTime = qrCodeInfoMongo.ExpireTime result.CreateTime = qrCodeInfoMongo.CreateTime result.UpdateTime = qrCodeInfoMongo.UpdateTime result.DeleteTime = qrCodeInfoMongo.DeleteTime result.DebugInfo = qrCodeInfoMongo.DebugInfo return result }