|
@@ -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
|
|
|
|