101 lines
2.6 KiB
Go
101 lines
2.6 KiB
Go
package biz
|
|
|
|
import (
|
|
"ai_scheduler/internal/data/mongo_model"
|
|
"ai_scheduler/internal/entitys"
|
|
"ai_scheduler/internal/pkg"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"context"
|
|
|
|
"go.mongodb.org/mongo-driver/bson"
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
)
|
|
|
|
type AdviceProjectBiz struct {
|
|
AdvicerProjectMongo *mongo_model.AdvicerProjectMongo
|
|
mongo *pkg.Mongo
|
|
}
|
|
|
|
func NewAdviceProjectBiz(
|
|
advicerProjectMongo *mongo_model.AdvicerProjectMongo,
|
|
mongo *pkg.Mongo,
|
|
) *AdviceProjectBiz {
|
|
return &AdviceProjectBiz{
|
|
AdvicerProjectMongo: advicerProjectMongo,
|
|
mongo: mongo,
|
|
}
|
|
}
|
|
|
|
func (a *AdviceProjectBiz) Add(ctx context.Context, param *entitys.AdvicerProjectAddReq) (err error) {
|
|
|
|
_, err = a.mongo.Co(a.AdvicerProjectMongo).InsertOne(ctx, &mongo_model.AdvicerProjectMongo{
|
|
ProjectId: param.ProjectId,
|
|
ProjectInfo: param.ProjectInfo,
|
|
RegionValue: param.RegionValue,
|
|
CompetitionComparison: param.CompetitionComparison,
|
|
CoreSellingPoints: param.CoreSellingPoints,
|
|
SupportingFacilities: param.SupportingFacilities,
|
|
DeveloperBacking: param.DeveloperBacking,
|
|
LastUpdateTime: time.Now(),
|
|
})
|
|
|
|
return err
|
|
}
|
|
|
|
func (a *AdviceProjectBiz) Update(ctx context.Context, param *entitys.AdvicerrProjectUpdateReq) (err error) {
|
|
filter := bson.M{}
|
|
if len(param.Id) == 0 {
|
|
return errors.New("ID不能为空")
|
|
}
|
|
objectID, err := primitive.ObjectIDFromHex(param.Id)
|
|
if err != nil {
|
|
return fmt.Errorf("ID转换失败: %w", err)
|
|
}
|
|
filter["_id"] = objectID
|
|
update := bson.M{
|
|
"$set": &mongo_model.AdvicerProjectMongo{
|
|
ProjectId: param.ProjectId,
|
|
RegionValue: param.RegionValue,
|
|
CompetitionComparison: param.CompetitionComparison,
|
|
CoreSellingPoints: param.CoreSellingPoints,
|
|
SupportingFacilities: param.SupportingFacilities,
|
|
DeveloperBacking: param.DeveloperBacking,
|
|
LastUpdateTime: time.Now(),
|
|
},
|
|
}
|
|
res := a.mongo.Co(a.AdvicerProjectMongo).FindOneAndUpdate(ctx, filter, update)
|
|
return res.Err()
|
|
}
|
|
|
|
func (a *AdviceProjectBiz) Info(ctx context.Context, param *entitys.AdvicerProjectInfoReq) (info mongo_model.AdvicerProjectMongo, err error) {
|
|
filter := bson.M{}
|
|
|
|
if param.ProjectId != 0 {
|
|
filter["projectId"] = param.ProjectId
|
|
}
|
|
|
|
// 2. _id 条件
|
|
if len(param.Id) != 0 {
|
|
objectID, err := primitive.ObjectIDFromHex(param.Id)
|
|
if err != nil {
|
|
return info, fmt.Errorf("ID转换失败: %w", err)
|
|
}
|
|
filter["_id"] = objectID
|
|
}
|
|
|
|
res := a.mongo.Co(a.AdvicerProjectMongo).FindOne(ctx, filter)
|
|
if res.Err() != nil {
|
|
return info, res.Err()
|
|
}
|
|
// 遍历结果
|
|
|
|
if err := res.Decode(&info); err != nil {
|
|
return info, err
|
|
}
|
|
|
|
return info, nil
|
|
}
|