animeic 1 year ago
parent
commit
6ade0fff43
1 changed files with 64 additions and 5 deletions
  1. 64 5
      boxcost/api/plan.go

+ 64 - 5
boxcost/api/plan.go

@@ -38,7 +38,7 @@ func ProductPlan(r *GinRouter) {
 	r.POST("/plan/update", UpdateProductPlan)
 
 	// 删除生产计划
-	r.POST("/plan/delete/:id", DelProductPlan)
+	r.POSTJWT("/plan/delete/:id", DelProductPlan)
 
 	// 下载部件单据
 	// r.GET("/bill/plan/download", DownLoadCompBills)
@@ -1162,10 +1162,69 @@ func UpdateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error)
 
 // 删除生产计划
 func DelProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-	planId := c.Param("id")
-	if planId == "" {
-		return nil, errors.New("id为空")
+	if apictx.User == nil {
+		return nil, errors.New("用户错误,请重新登录")
+	}
+	userId, _ := primitive.ObjectIDFromHex(apictx.User.Parent)
+	if userId.IsZero() {
+		return nil, errors.New("用户错误,请重新登录")
+	}
+	_planId := c.Param("id")
+	planId, _ := primitive.ObjectIDFromHex(_planId)
+	if planId.IsZero() {
+		return nil, errors.New("计划id错误")
+	}
+	plan := model.ProductPlan{}
+	found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
+		CollectName: repo.CollectionProductPlan,
+		Query:       repo.Map{"_id": planId},
+	}, &plan)
+	if !found || err != nil {
+		return nil, errors.New("计划数据未找到")
 	}
 
-	return repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, planId)
+	res, err := repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, _planId)
+
+	// 删除计划对应订单
+	if err == nil {
+
+		// 获取所有stages单据id
+		billIds := make([]string, 0)
+		for _, comp := range plan.Pack.Components {
+			if comp.Id == "" || len(comp.Stages) == 0 {
+				continue
+			}
+			for _, stage := range comp.Stages {
+				billId, _ := primitive.ObjectIDFromHex(stage.BillId)
+				if !billId.IsZero() {
+					billIds = append(billIds, fmt.Sprintf("%d_%s", stage.BillType, stage.BillId))
+				}
+			}
+
+		}
+		// 去重单据号
+		typeBillIds := removeDuplicationSort(billIds)
+		if len(typeBillIds) < 1 {
+			return res, err
+		}
+
+		for _, tId := range typeBillIds {
+			tidArr := strings.Split(tId, "_")
+			// 采购
+			if tidArr[0] == "1" {
+				repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionBillPurchase, tidArr[1])
+			}
+			// 工艺
+			if tidArr[0] == "2" {
+				repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduce, tidArr[1])
+			}
+			// 成品采购
+			if tidArr[0] == "3" {
+				repo.RepoDeleteDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduct, tidArr[1])
+			}
+
+		}
+
+	}
+	return res, err
 }