sunsheng 8 months ago
parent
commit
249a4c315e
3 changed files with 156 additions and 31 deletions
  1. BIN
      box-cost.exe
  2. 147 4
      el/excel.go
  3. 9 27
      main.go

BIN
box-cost.exe


+ 147 - 4
el/excel.go

@@ -47,12 +47,22 @@ func MatchString(targetStr string, regexPattern string) bool {
 	return re.MatchString(targetStr)
 }
 
+type PlanStatusInfo struct {
+	PlanName       string              `json:"planName"`
+	PlanUnit       string              `json:"planUnit"`
+	PlanCount      int                 `json:"planCount"`
+	PlanRowStart   int                 `json:"planRowStart"`
+	PlanRowEnd     int                 `json:"planRowEnd"`
+	PlanCompStatus []map[string]string `json:"planCompStatus"`
+}
+
 func UpdateExcel(client *mongo.Client, plans []*model.ProductPlan) {
 	row := 5
-	planCompStatus := []map[string]string{}
 
+	planStatusInfos := make([]*PlanStatusInfo, 0)
+	planCompStatus := []map[string]string{}
 	for _, plan := range plans {
-
+		startRow := row
 		for _, comp := range plan.Pack.Components {
 			// ""代表该部件没有该工艺 "〇"代表正在进行的工艺
 			// "808080"背景颜色代表部件所含未进行工艺 √代表已完成工序
@@ -85,6 +95,10 @@ func UpdateExcel(client *mongo.Client, plans []*model.ProductPlan) {
 				"组装": " ",
 				"交货": " ",
 			}
+			fmt.Println(plan.Name)
+			fmt.Println(comp.Name)
+			fmt.Println(row)
+			fmt.Println("------------------------------------")
 			compStatus["部件"] = comp.Name
 			compStatus["行数"] = fmt.Sprintf("%d", row)
 			row++
@@ -92,6 +106,13 @@ func UpdateExcel(client *mongo.Client, plans []*model.ProductPlan) {
 			seen := make(map[string]bool)
 			tbills := make([]string, 0, len(comp.Stages)) // 结果数组,容量初始化为原数组的长度
 
+			// ???:1 最后一个工序没下单
+			// isBill := true
+			// lastStage := comp.Stages[len(comp.Stages)-1]
+			// if len(lastStage.BillId) < 24 {
+			// 	isBill = false
+			// }
+
 			for _, stage := range comp.Stages {
 
 				if len(stage.BillId) > 0 {
@@ -159,6 +180,9 @@ func UpdateExcel(client *mongo.Client, plans []*model.ProductPlan) {
 						compStatus["纸张"] = "√"
 					}
 
+					// if !isBill {
+					// 	continue
+					// }
 					if lastBillType == billType {
 						for _, paper := range bill.Paper {
 							compStatus["交货"] = fmt.Sprintf("%d", paper.ConfirmCount)
@@ -187,6 +211,10 @@ func UpdateExcel(client *mongo.Client, plans []*model.ProductPlan) {
 
 							// 直接赋值,如果这个订单是最后一个,则状态覆盖
 							// ???思考:如果最后一个工序没有生成订单的话,是按最后一个工序还是最后一个订单?这里是最后一个订单
+							// ???:1
+							// if !isBill {
+							// 	continue
+							// }
 							compStatus["交货"] = fmt.Sprintf("%d", produce.ConfirmCount)
 						}
 
@@ -203,6 +231,9 @@ func UpdateExcel(client *mongo.Client, plans []*model.ProductPlan) {
 					}
 					// fmt.Println(bill)
 					// ?? 这里需不需要 影响正确数据吗?
+					// if !isBill {
+					// 	continue
+					// }
 					if lastBillType == billType {
 						for _, product := range bill.Products {
 							compStatus["交货"] = fmt.Sprintf("%d", product.ConfirmCount)
@@ -213,10 +244,23 @@ func UpdateExcel(client *mongo.Client, plans []*model.ProductPlan) {
 
 			planCompStatus = append(planCompStatus, compStatus)
 		}
+		// fmt.Println(plan.Name)
+		// fmt.Println("rowstart:", startRow)
+		// fmt.Println("rowend:", row-1)
+		planStatusInfos = append(planStatusInfos, &PlanStatusInfo{
+			PlanName: plan.Name,
+			// !这个规格好像没有
+			PlanUnit:       "",
+			PlanCount:      plan.Total,
+			PlanRowStart:   startRow,
+			PlanRowEnd:     row - 1,
+			PlanCompStatus: planCompStatus,
+		})
 
 	}
-	// fmt.Printf("%#v\n", planCompStatus)
-	UpdateCell(planCompStatus)
+	// fmt.Printf("%#v\n", planStatusInfos[len(planStatusInfos)-1])
+	// UpdateCell(planCompStatus)
+	UpdateCell1(planStatusInfos)
 
 }
 
@@ -303,3 +347,102 @@ func UpdateCell(planCompStatus []map[string]string) {
 		log.Fatal(err)
 	}
 }
+
+func UpdateCell1(planStatusInfos []*PlanStatusInfo) {
+	tmpfile, err := excelize.OpenFile(EXCEL_TMPLATE_FILE)
+	if err != nil {
+		log.Fatal(err)
+	}
+	excelIndex := tmpfile.GetActiveSheetIndex()
+	sheetName := tmpfile.GetSheetName(excelIndex)
+	// 定义样式
+	style, err := tmpfile.NewStyle(&excelize.Style{
+		Border: []excelize.Border{
+			{
+				Type:  "left",
+				Color: "FF000000",
+				Style: 1,
+			},
+			{
+				Type:  "right",
+				Color: "FF000000",
+				Style: 1,
+			},
+			{
+				Type:  "top",
+				Color: "FF000000",
+				Style: 1,
+			},
+			{
+				Type:  "bottom",
+				Color: "FF000000",
+				Style: 1,
+			},
+		},
+		Fill: excelize.Fill{
+			Type:    "pattern",
+			Pattern: 1, // 1 表示实心填充
+			Color:   []string{CELL_BACKGROUND},
+		},
+	})
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	for _, planStatusInfo := range planStatusInfos {
+		// 填充品名/箱规/数量
+		// 品名
+		err = tmpfile.SetCellValue(sheetName, fmt.Sprintf("%s%d", "B", planStatusInfo.PlanRowStart), planStatusInfo.PlanName)
+		if err != nil {
+			log.Fatal(err)
+		}
+		// 数量
+		err = tmpfile.SetCellValue(sheetName, fmt.Sprintf("%s%d", "D", planStatusInfo.PlanRowStart), planStatusInfo.PlanCount)
+		if err != nil {
+			log.Fatal(err)
+		}
+
+		planCompStatus := planStatusInfo.PlanCompStatus
+		for _, compStatus := range planCompStatus {
+
+			row := compStatus["行数"]
+			for colk, col := range needChangeCol {
+				for csk, csv := range compStatus {
+					if colk == csk {
+						cell := fmt.Sprintf("%s%s", col, row)
+						// fmt.Println(cell)
+
+						// 设置背景
+						if csv == CELL_BACKGROUND {
+							// 设置单元格的值
+							err = tmpfile.SetCellStyle(sheetName, cell, cell, style)
+							if err != nil {
+								log.Fatal(err)
+							}
+						} else {
+							// 设置字符
+							err = tmpfile.SetCellValue(sheetName, cell, csv)
+							if err != nil {
+								log.Fatal(err)
+							}
+
+						}
+
+					}
+
+				}
+			}
+
+		}
+	}
+
+	// 获取当前日期
+	date := time.Now().Format("2006年01月02日_150405")
+	fileName := fmt.Sprintf("礼盒加工追踪表_%s.xlsx", date)
+
+	// 以新的文件名保存文件
+	err = tmpfile.SaveAs(fileName)
+	if err != nil {
+		log.Fatal(err)
+	}
+}

+ 9 - 27
main.go

@@ -64,38 +64,20 @@ func main() {
 
 	collection := client.Database("box-cost").Collection("product-plan")
 
-	var objectIds []primitive.ObjectID
+	plans := []*model.ProductPlan{}
 	for _, planIdstr := range planIds {
 		planId, _ := primitive.ObjectIDFromHex(planIdstr)
-		objectIds = append(objectIds, planId)
-	}
-
-	// 创建聚合管道
-	pipeline := mongo.Pipeline{
-		{{Key: "$match", Value: bson.D{{Key: "_id", Value: bson.D{{Key: "$in", Value: objectIds}}}}}},
-	}
-
-	// 构建查询管道
-	// pipeline := make([]bson.M, 0)
-	// for _, planIdstr := range planIds {
-	// 	planId, _ := primitive.ObjectIDFromHex(planIdstr)
-	// 	pipeline = append(pipeline, bson.M{"$match": bson.M{"_id": planId}})
-	// }
-
-	// 执行聚合查询
-	cursor, err := collection.Aggregate(context.Background(), pipeline)
-	if err != nil {
-		log.Fatal(err)
-	}
-	defer cursor.Close(context.Background())
-	// 获取结果
-	plans := make([]*model.ProductPlan, 0)
-	err = cursor.All(context.Background(), &plans)
-	if err != nil {
-		log.Fatal(err)
+		fmt.Println(planIdstr)
+		plan := &model.ProductPlan{}
+		err := collection.FindOne(context.Background(), bson.M{"_id": planId}).Decode(plan)
+		if err != nil {
+			log.Fatal(err)
+		}
+		plans = append(plans, plan)
 	}
 
 	fmt.Println("更新中,请稍等...")
+
 	// 更新excel
 	el.UpdateExcel(client, plans)