2022-05-17 20:23:08 +08:00
|
|
|
package persistence
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2022-09-09 18:26:08 +08:00
|
|
|
"mayfly-go/internal/mongo/domain/entity"
|
|
|
|
|
"mayfly-go/internal/mongo/domain/repository"
|
2022-06-02 17:41:11 +08:00
|
|
|
"mayfly-go/pkg/biz"
|
|
|
|
|
"mayfly-go/pkg/model"
|
2022-10-26 20:49:29 +08:00
|
|
|
"mayfly-go/pkg/utils"
|
|
|
|
|
"strings"
|
2022-05-17 20:23:08 +08:00
|
|
|
)
|
|
|
|
|
|
2022-09-09 18:26:08 +08:00
|
|
|
type mongoRepoImpl struct{}
|
2022-05-17 20:23:08 +08:00
|
|
|
|
2022-09-09 18:26:08 +08:00
|
|
|
func newMongoRepo() repository.Mongo {
|
|
|
|
|
return new(mongoRepoImpl)
|
|
|
|
|
}
|
2022-05-17 20:23:08 +08:00
|
|
|
|
|
|
|
|
// 分页获取数据库信息列表
|
2023-06-01 12:31:32 +08:00
|
|
|
func (d *mongoRepoImpl) GetList(condition *entity.MongoQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) *model.PageResult {
|
2022-10-26 20:49:29 +08:00
|
|
|
sql := "SELECT d.* FROM t_mongo d WHERE 1=1 "
|
|
|
|
|
|
|
|
|
|
if len(condition.TagIds) > 0 {
|
|
|
|
|
sql = sql + " AND d.tag_id IN " + fmt.Sprintf("(%s)", strings.Join(utils.NumberArr2StrArr(condition.TagIds), ","))
|
2022-05-17 20:23:08 +08:00
|
|
|
}
|
2022-10-31 18:39:52 +08:00
|
|
|
|
2023-06-01 12:31:32 +08:00
|
|
|
values := make([]any, 0)
|
2022-10-26 20:49:29 +08:00
|
|
|
if condition.TagPathLike != "" {
|
2022-10-31 18:39:52 +08:00
|
|
|
values = append(values, condition.TagPathLike+"%")
|
|
|
|
|
sql = sql + " AND d.tag_path LIKE ?"
|
2022-05-17 20:23:08 +08:00
|
|
|
}
|
2022-10-29 20:08:15 +08:00
|
|
|
sql = sql + " ORDER BY d.tag_path"
|
2022-10-31 18:39:52 +08:00
|
|
|
return model.GetPageBySql(sql, pageParam, toEntity, values...)
|
2022-05-17 20:23:08 +08:00
|
|
|
}
|
|
|
|
|
|
2022-10-26 20:49:29 +08:00
|
|
|
func (d *mongoRepoImpl) Count(condition *entity.MongoQuery) int64 {
|
2023-06-01 12:31:32 +08:00
|
|
|
where := make(map[string]any)
|
2022-10-26 20:49:29 +08:00
|
|
|
if len(condition.TagIds) > 0 {
|
|
|
|
|
where["tag_id"] = condition.TagIds
|
|
|
|
|
}
|
|
|
|
|
if condition.TagId != 0 {
|
|
|
|
|
where["tag_id"] = condition.TagId
|
|
|
|
|
}
|
|
|
|
|
return model.CountByMap(new(entity.Mongo), where)
|
2022-05-17 20:23:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据条件获取
|
2022-09-09 18:26:08 +08:00
|
|
|
func (d *mongoRepoImpl) Get(condition *entity.Mongo, cols ...string) error {
|
2022-05-17 20:23:08 +08:00
|
|
|
return model.GetBy(condition, cols...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据id获取
|
2022-09-09 18:26:08 +08:00
|
|
|
func (d *mongoRepoImpl) GetById(id uint64, cols ...string) *entity.Mongo {
|
2022-05-17 20:23:08 +08:00
|
|
|
db := new(entity.Mongo)
|
|
|
|
|
if err := model.GetById(db, id, cols...); err != nil {
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return db
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-09 18:26:08 +08:00
|
|
|
func (d *mongoRepoImpl) Insert(db *entity.Mongo) {
|
2022-05-17 20:23:08 +08:00
|
|
|
biz.ErrIsNil(model.Insert(db), "新增mongo信息失败")
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-09 18:26:08 +08:00
|
|
|
func (d *mongoRepoImpl) Update(db *entity.Mongo) {
|
2022-05-17 20:23:08 +08:00
|
|
|
biz.ErrIsNil(model.UpdateById(db), "更新mongo信息失败")
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-09 18:26:08 +08:00
|
|
|
func (d *mongoRepoImpl) Delete(id uint64) {
|
2022-05-17 20:23:08 +08:00
|
|
|
model.DeleteById(new(entity.Mongo), id)
|
|
|
|
|
}
|