浏览代码

add plan track api

sunsheng 7 月之前
父节点
当前提交
bbf39e38ed
共有 4 个文件被更改,包括 159 次插入1 次删除
  1. 132 0
      boxcost/api/plan-process-track.go
  2. 3 1
      boxcost/api/router.go
  3. 23 0
      boxcost/db/model/plan-track.go
  4. 1 0
      boxcost/db/repo/repo.go

+ 132 - 0
boxcost/api/plan-process-track.go

@@ -12,6 +12,7 @@ import (
 
 	"github.com/gin-gonic/gin"
 	"github.com/xuri/excelize/v2"
+	"go.mongodb.org/mongo-driver/bson"
 	"go.mongodb.org/mongo-driver/bson/primitive"
 )
 
@@ -19,6 +20,137 @@ type PlanIds struct {
 	Ids []primitive.ObjectID `json:"ids"`
 }
 
+var PlanSearchFields = []string{"_id", "name", "createUser", "total", "status", "totalPrice", "updateTime", "createTime"}
+
+// 计划追踪管理
+func PlanTrack(r *GinRouter) {
+
+	// 新增计划追踪
+	r.POSTJWT("/planTrack/create", CreatePlanTrack)
+
+	// 获取计划追踪信息
+	r.GETJWT("/planTrack/detail/:id", GetPlanTrack)
+
+	// 获取计划追踪列表
+	r.GETJWT("/planTrack/list", GetPlanTracks)
+
+	// 计划追踪列表
+	r.POSTJWT("/planTrack/update", UpdatePlanTrack)
+
+	r.POSTJWT("/planTrack/delete/:id", DelPlanTrack)
+}
+
+func CreatePlanTrack(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+
+	var PlanTrack model.PlanTrack
+
+	err := c.ShouldBindJSON(&PlanTrack)
+	if err != nil {
+		return nil, errors.New("参数错误!")
+	}
+	ctx := apictx.CreateRepoCtx()
+
+	if len(PlanTrack.Name) < 1 {
+		return nil, errors.New("计划追踪名为空")
+	}
+	if len(PlanTrack.PlanIds) < 1 {
+		return nil, errors.New("计划追踪id为空")
+	}
+
+	PlanTrack.CreateTime = time.Now()
+	PlanTrack.UpdateTime = time.Now()
+
+	result, err := repo.RepoAddDoc(ctx, repo.CollectionPlanTrack, &PlanTrack)
+	return result, err
+}
+
+// 获取计划追踪信息
+func GetPlanTrack(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	PlanTrackId := c.Param("id")
+	id, err := primitive.ObjectIDFromHex(PlanTrackId)
+	if err != nil {
+		return nil, errors.New("非法id")
+	}
+	var PlanTrack model.PlanTrack
+	found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
+		CollectName: repo.CollectionPlanTrack,
+		Query:       repo.Map{"_id": id},
+	}, &PlanTrack)
+	if !found || err != nil {
+		log.Info(err)
+		return nil, errors.New("数据未找到")
+	}
+	userId, _ := primitive.ObjectIDFromHex(apictx.User.ID)
+	// 获取用户信息
+	userInfo, err := getUserById(apictx, userId)
+	if err != nil {
+		return nil, errors.New("用户信息获取失败")
+	}
+	PlanTrack.UserInfo = userInfo
+	// 查询包含计划的详细信息
+	for _, planId := range PlanTrack.PlanIds {
+		plan := &model.ProductPlan{}
+		_, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
+			CollectName: repo.CollectionProductPlan,
+			Query:       repo.Map{"_id": planId},
+			Project:     PlanSearchFields,
+		}, plan)
+		if err != nil {
+			log.Info(err)
+			continue
+		}
+		PlanTrack.Plans = append(PlanTrack.Plans, plan)
+	}
+
+	return PlanTrack, nil
+}
+
+// 获取计划追踪列表
+func GetPlanTracks(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+
+	page, size, query := UtilQueryPageSize(c)
+
+	if _name, ok := query["name"]; ok {
+		delete(query, "name")
+		query["name"] = bson.M{"$regex": _name.(string)}
+	}
+	option := &repo.PageSearchOptions{
+		CollectName: repo.CollectionPlanTrack,
+		Query:       query,
+		Page:        page,
+		Size:        size,
+		Sort:        bson.M{"createTime": -1},
+	}
+	return repo.RepoPageSearch(apictx.CreateRepoCtx(), option)
+}
+
+func UpdatePlanTrack(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	var pt model.PlanTrack
+	err := c.ShouldBindJSON(&pt)
+	if err != nil {
+		return nil, errors.New("参数错误")
+	}
+	if pt.Id == primitive.NilObjectID {
+		return nil, errors.New("id的为空")
+	}
+	pt.UpdateTime = time.Now()
+	return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionPlanTrack, pt.Id.Hex(), &pt, &repo.RecordLogReq{
+		Path:     c.Request.URL.Path,
+		UserId:   apictx.User.ID,
+		TargetId: pt.Id.Hex(),
+	})
+}
+
+// 删除计划追踪
+func DelPlanTrack(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	PlanTrackId := c.Param("id")
+	if PlanTrackId == "" {
+		return nil, errors.New("id为空")
+	}
+
+	return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionPlanTrack, PlanTrackId)
+}
+
 func DownloadPlanTrack(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	var form PlanIds
 

+ 3 - 1
boxcost/api/router.go

@@ -20,7 +20,9 @@ func RegRouters(svc *Service) {
 	boxcost.GET("/callback", callback)
 	boxcost.POSTJWT("/diffUpdatePlanTest", DiffUpdatePlanTest)
 	boxcost.GET("/printDiff", PrintDiff)
-	boxcost.POST("/download/plan/track", DownloadPlanTrack)
+	boxcost.POSTJWT("/download/plan/track", DownloadPlanTrack)
+
+	PlanTrack(boxcost)
 
 	// 材料管理
 	Material(boxcost)

+ 23 - 0
boxcost/db/model/plan-track.go

@@ -0,0 +1,23 @@
+package model
+
+import (
+	"time"
+
+	"go.mongodb.org/mongo-driver/bson/primitive"
+)
+
+type PlanTrack struct {
+	Id     primitive.ObjectID `bson:"id,omitempty" json:"id"`
+	UserId primitive.ObjectID `bson:"userId,omitempty" json:"userId"`
+	//名字
+	Name string `bson:"name,omitempty" json:"name"`
+	//规格
+	PlanIds []string `bson:"planIds,omitempty" json:"planIds"`
+
+	// 查询时动态返回给前端的数据 数据库不记录
+	UserInfo interface{}    `bson:"userInfo,omitempty" json:"userInfo"`
+	Plans    []*ProductPlan `bson:"plans,omitempty" json:"plans"`
+
+	CreateTime time.Time `bson:"createTime,omitempty" json:"createTime"`
+	UpdateTime time.Time `bson:"updateTime,omitempty" json:"updateTime"`
+}

+ 1 - 0
boxcost/db/repo/repo.go

@@ -42,6 +42,7 @@ const (
 	// 更改日志记录
 	CollectionLogs        = "logs"
 	CollectionRequestLogs = "request-logs"
+	CollectionPlanTrack   = "plan-track"
 )
 
 type Map map[string]interface{}