package mongo import ( "context" "finclip-app-manager/domain/entity" "finclip-app-manager/domain/entity/proto/apiproto" "finclip-app-manager/domain/repository" mgo "gitlab.finogeeks.club/finclip-backend-v2/finclip-mgo" "gitlab.finogeeks.club/finclip-backend-v2/finclip-mgo/bson" ) var _ repository.IMenuInfoRepo = new(MenuInfoByMongoRepo) type MenuInfoByMongoRepo struct { } type MenuInfoMongo struct { TraceId string `bson:"trace_id" json:"traceId"` Name string `bson:"name" json:"name"` InfoId string `bson:"info_id" json:"infoId"` ImageUrl string `bson:"image_url" json:"imageUrl"` SortNum int `bson:"sort_num" json:"sortNum"` CreateTime int64 `bson:"create_time" json:"createTime"` UpdateTime int64 `bson:"update_time" json:"updateTime"` UpdateOperator string `bson:"update_operator" json:"updateOperator"` } func (b MenuInfoByMongoRepo) GetInfoByTraceId(ctx context.Context, id string) (*entity.MenuInfo, error) { menuInfoMongo := MenuInfoMongo{} err := menuInfoTable.GetOne(ctx, bson.M{"trace_id": id}, &menuInfoMongo) if err != nil { if NotFound(err) { log.Errorf("GetInfoByTraceId err:%s", err.Error()) return &entity.MenuInfo{}, entity.NotFoundErr } } result := convertMenuInfoMongoToMenuInfo(menuInfoMongo) return &result, err } func (b MenuInfoByMongoRepo) GetAllMenuList(ctx context.Context, sort []string) ([]entity.MenuInfo, int, error) { menuInfoMongos := make([]MenuInfoMongo, 0) total, err := menuInfoTable.GetAll(ctx, bson.M{}, sort, &menuInfoMongos) if b.NotFound(err) { return []entity.MenuInfo{}, 0, nil } menuInfos := make([]entity.MenuInfo, 0) for _, menuInfoMongo := range menuInfoMongos { menuInfos = append(menuInfos, convertMenuInfoMongoToMenuInfo(menuInfoMongo)) } return menuInfos, total, err } func (b MenuInfoByMongoRepo) Add(ctx context.Context, info *entity.MenuInfo) error { //if err := repository.Lock(ctx, 1*time.Minute); err != nil { // return err //} //defer repository.Unlock(ctx) count, err := menuInfoTable.Count(ctx, bson.M{"$or": []bson.M{ {"info_id": info.InfoId}, {"name": info.Name}, }}) if err != nil { return err } if count > 0 { return entity.MenuIdOrNameExiErr } nextSortNum, err := b.GetNextSortNum(ctx) if err != nil { return err } info.SortNum = nextSortNum return menuInfoTable.Insert(ctx, convertMenuInfoToMenuInfoMongo(info)) } func (b MenuInfoByMongoRepo) GetNextSortNum(ctx context.Context) (int, error) { latestInfo := new(MenuInfoMongo) err := menuInfoTable.GetSortOne(ctx, bson.M{}, []string{"-sort_num"}, latestInfo) if b.NotFound(err) { return 1, nil } return latestInfo.SortNum + 1, nil } func (b MenuInfoByMongoRepo) DelByTraceId(ctx context.Context, id string) error { return menuInfoTable.Del(ctx, bson.M{"trace_id": id}) } func (b MenuInfoByMongoRepo) UpdateInfo(ctx context.Context, id string, info *entity.MenuInfo) error { //if err := repository.Lock(ctx, 1*time.Minute); err != nil { // return err //} //defer repository.Unlock(ctx) menuInfoMongos := make([]MenuInfoMongo, 0) _, err := menuInfoTable.GetAll(ctx, bson.M{"$or": []bson.M{ {"info_id": info.InfoId}, {"name": info.Name}, }}, []string{}, &menuInfoMongos) if err != nil && !b.NotFound(err) { return err } for _, m := range menuInfoMongos { if m.TraceId != id { return entity.MenuIdOrNameExiErr } } return menuInfoTable.UpdateOne(ctx, bson.M{"trace_id": id}, info) } func (b MenuInfoByMongoRepo) AllCount(ctx context.Context) (int, error) { return menuInfoTable.Count(ctx, bson.M{}) } func (b MenuInfoByMongoRepo) Sort(ctx context.Context, req *apiproto.MenuSortReq) error { //if err := repository.Lock(ctx, 1*time.Minute); err != nil { // return err //} //defer repository.Unlock(ctx) var resError error for _, v := range req.List { err := menuInfoTable.UpdateData(bson.M{"trace_id": v.TraceId}, bson.M{"$set": bson.M{"sort_num": v.SortNum}}) if err != nil { err = err } } return resError } func (b MenuInfoByMongoRepo) NotFound(err error) bool { return err == mgo.ErrNotFound } func convertMenuInfoMongoToMenuInfo(menuInfoMongo MenuInfoMongo) entity.MenuInfo { result := entity.MenuInfo{} result.TraceId = menuInfoMongo.TraceId result.Name = menuInfoMongo.Name result.InfoId = menuInfoMongo.InfoId result.ImageUrl = menuInfoMongo.ImageUrl result.SortNum = menuInfoMongo.SortNum result.CreateTime = menuInfoMongo.CreateTime result.UpdateTime = menuInfoMongo.UpdateTime result.UpdateOperator = menuInfoMongo.UpdateOperator return result } func convertMenuInfoToMenuInfoMongo(menuInfo *entity.MenuInfo) MenuInfoMongo { result := MenuInfoMongo{} result.TraceId = menuInfo.TraceId result.Name = menuInfo.Name result.InfoId = menuInfo.InfoId result.ImageUrl = menuInfo.ImageUrl result.SortNum = menuInfo.SortNum result.CreateTime = menuInfo.CreateTime result.UpdateTime = menuInfo.UpdateTime result.UpdateOperator = menuInfo.UpdateOperator return result }