sunsheng 9 months ago
parent
commit
755bdaba3e
5 changed files with 119 additions and 29 deletions
  1. 14 1
      app.yaml
  2. 84 16
      el/excel.go
  3. 18 5
      main.go
  4. 3 3
      model/plan.go
  5. 0 4
      readme.md

+ 14 - 1
app.yaml

@@ -1,2 +1,15 @@
 plans:
-  - "6648181a23830800fd7751fd"
+  - "6648181a23830800fd7751fd"
+  - "664968ac23830800fd775203"
+  - "66498b5423830800fd775205"
+  - "6649535e23830800fd7751ff"
+  - "664989d823830800fd775204"
+  - "6649a02b23830800fd775216"
+  - "6649980723830800fd77520f"
+  - "66499a3423830800fd775211"
+  - "66499e2b23830800fd775215"
+  - "665dbdd623830800fd775416"
+  - "66499ae023830800fd775212"
+  - "6649990123830800fd775210"
+  - "66499c1d23830800fd775213"
+  - "66499cfd23830800fd775214"

+ 84 - 16
el/excel.go

@@ -20,6 +20,7 @@ const (
 	PURCHASE_COLLECTION = "bill-purchase"
 	PRODUCE_COLLECTION  = "bill-produce"
 	PRODUCT_COLLECTION  = "bill-product"
+	CELL_BACKGROUND     = "808080"
 )
 
 var needChangeCol = map[string]string{
@@ -36,6 +37,7 @@ var needChangeCol = map[string]string{
 	"模切": "O",
 	"粘盒": "P",
 	"组装": "Q",
+	"交货": "R",
 }
 
 func MatchString(targetStr string, regexPattern string) bool {
@@ -53,7 +55,7 @@ func UpdateExcel(client *mongo.Client, plans []*model.ProductPlan) {
 
 		for _, comp := range plan.Pack.Components {
 			// ""代表该部件没有该工艺 "〇"代表正在进行的工艺
-			// "8080"背景颜色代表部件所含未进行工艺 √代表已完成工序
+			// "808080"背景颜色代表部件所含未进行工艺 √代表已完成工序
 			compStatus := map[string]string{
 				"部件": " ",
 				"行数": "5",
@@ -81,6 +83,7 @@ func UpdateExcel(client *mongo.Client, plans []*model.ProductPlan) {
 				"粘盒": " ",
 				// 遍历工艺单,工序中包含组装
 				"组装": " ",
+				"交货": " ",
 			}
 			compStatus["部件"] = comp.Name
 			compStatus["行数"] = fmt.Sprintf("%d", row)
@@ -90,6 +93,7 @@ func UpdateExcel(client *mongo.Client, plans []*model.ProductPlan) {
 			tbills := make([]string, 0, len(comp.Stages)) // 结果数组,容量初始化为原数组的长度
 
 			for _, stage := range comp.Stages {
+
 				if len(stage.BillId) > 0 {
 					value := fmt.Sprintf("%d_%s", stage.BillType, stage.BillId)
 					if _, ok := seen[value]; !ok {
@@ -98,6 +102,23 @@ func UpdateExcel(client *mongo.Client, plans []*model.ProductPlan) {
 						// 添加到结果数组
 						tbills = append(tbills, value)
 					}
+				} else {
+					// 产品中填写了这个工序但是没有下单 黑色背景
+					for k := range compStatus {
+						if k == "下单" || k == "交货" || k == "部件" || k == "行数" {
+							continue
+						}
+						// 纸张 只要是采购单就设置纸张状态
+						if stage.Type == 1 {
+							compStatus["纸张"] = CELL_BACKGROUND
+						}
+
+						// 匹配工艺关键字 匹配到就设置状态为黑背景
+						if MatchString(stage.Name, k) {
+							compStatus[k] = CELL_BACKGROUND
+						}
+
+					}
 				}
 
 			}
@@ -105,11 +126,15 @@ func UpdateExcel(client *mongo.Client, plans []*model.ProductPlan) {
 			// 如果tbills为空,说明该部件没有订单
 			if len(tbills) == 0 {
 				// 该部件没有订单,跳过
+				planCompStatus = append(planCompStatus, compStatus)
 				continue
 			}
 
 			// 查询数据库获取bill详细信息
+			// 最后工序订单号
+			lastBillType := tbills[len(tbills)-1]
 			for _, billType := range tbills {
+
 				compStatus["下单"] = "√"
 				bt := strings.Split(billType, "_")[0]
 				billId, _ := primitive.ObjectIDFromHex(strings.Split(billType, "_")[1])
@@ -120,13 +145,25 @@ func UpdateExcel(client *mongo.Client, plans []*model.ProductPlan) {
 					if err != nil {
 						log.Fatal(err)
 					}
-					if bill.Type == "纸张" {
-						compStatus["纸张"] = "〇"
-						if bill.Status == "complete" {
-							compStatus["纸张"] = "√"
-						}
+					// if bill.Type == "纸张" {
+					// 	compStatus["纸张"] = "〇"
+					// 	if bill.Status == "complete" {
+					// 		compStatus["纸张"] = "√"
+					// 	}
 
+					// }
+
+					compStatus["纸张"] = "〇"
+					if bill.Status == "complete" {
+						compStatus["纸张"] = "√"
 					}
+
+					if lastBillType == billType {
+						for _, paper := range bill.Paper {
+							compStatus["交货"] = fmt.Sprintf("%d", paper.ConfirmCount)
+						}
+					}
+
 				}
 				if bt == "2" {
 					// 查询工艺单
@@ -136,8 +173,8 @@ func UpdateExcel(client *mongo.Client, plans []*model.ProductPlan) {
 						log.Fatal(err)
 					}
 					for _, produce := range bill.Produces {
-						for k, _ := range compStatus {
-							if k == "下单" || k == "纸张" {
+						for k := range compStatus {
+							if k == "下单" || k == "纸张" || k == "交货" || k == "部件" || k == "行数" {
 								continue
 							}
 							if MatchString(produce.Name, k) {
@@ -146,6 +183,8 @@ func UpdateExcel(client *mongo.Client, plans []*model.ProductPlan) {
 									compStatus[k] = "√"
 								}
 							}
+
+							compStatus["交货"] = fmt.Sprintf("%d", produce.ConfirmCount)
 						}
 
 					}
@@ -160,8 +199,12 @@ func UpdateExcel(client *mongo.Client, plans []*model.ProductPlan) {
 						log.Fatal(err)
 					}
 					// fmt.Println(bill)
+					if lastBillType == billType {
+						for _, product := range bill.Products {
+							compStatus["交货"] = fmt.Sprintf("%d", product.ConfirmCount)
+						}
+					}
 				}
-
 			}
 
 			planCompStatus = append(planCompStatus, compStatus)
@@ -182,8 +225,8 @@ func UpdateCell(planCompStatus []map[string]string) {
 	sheetName := tmpfile.GetSheetName(excelIndex)
 
 	for _, compStatus := range planCompStatus {
-		fmt.Println("-----------------------------------------------")
-		fmt.Println(compStatus)
+		// fmt.Println("-----------------------------------------------")
+		// fmt.Println(compStatus)
 		row := compStatus["行数"]
 		for colk, col := range needChangeCol {
 			for csk, csv := range compStatus {
@@ -192,19 +235,44 @@ func UpdateCell(planCompStatus []map[string]string) {
 					// fmt.Println(cell)
 
 					// 设置背景
-					if csv == "8080" {
+					if csv == CELL_BACKGROUND {
 						// 设置单元格的值
-						styleId, err := tmpfile.NewStyle(&excelize.Style{
-							Fill: excelize.Fill{
 
-								Color: []string{"8080"},
+						// 定义样式
+						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)
 						}
 
-						err = tmpfile.SetCellStyle(sheetName, cell, cell, styleId)
+						err = tmpfile.SetCellStyle(sheetName, cell, cell, style)
 						if err != nil {
 							log.Fatal(err)
 						}

+ 18 - 5
main.go

@@ -63,13 +63,25 @@ func main() {
 	defer client.Disconnect(ctx)
 
 	collection := client.Database("box-cost").Collection("product-plan")
-	// 构建查询管道
-	pipeline := make([]bson.M, 0)
+
+	var objectIds []primitive.ObjectID
 	for _, planIdstr := range planIds {
 		planId, _ := primitive.ObjectIDFromHex(planIdstr)
-		pipeline = append(pipeline, bson.M{"$match": bson.M{"_id": planId}})
+		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 {
@@ -82,8 +94,9 @@ func main() {
 	if err != nil {
 		log.Fatal(err)
 	}
-	// 读取excel
-	fmt.Println(plans[0].Name)
+
+	fmt.Println("更新中,请稍等...")
+	// 更新excel
 	el.UpdateExcel(client, plans)
 
 }

+ 3 - 3
model/plan.go

@@ -89,9 +89,9 @@ type ComponentStage struct {
 	// 供应商信息
 	SupplierInfo *Supplier `bson:"supplierInfo,omitempty" json:"supplierInfo"`
 
-	BatchCount      int `bson:"batchCount,omitempty" json:"batchCount"`           //拼版数量
-	BatchSizeWidth  int `bson:"batchSizeWidth,omitempty" json:"batchSizeWidth"`   //平板尺寸
-	BatchSizeHeight int `bson:"batchSizeHeight,omitempty" json:"batchSizeHeight"` //平板尺寸
+	BatchCount      float64 `bson:"batchCount,omitempty" json:"batchCount"`           //拼版数量
+	BatchSizeWidth  int     `bson:"batchSizeWidth,omitempty" json:"batchSizeWidth"`   //平板尺寸
+	BatchSizeHeight int     `bson:"batchSizeHeight,omitempty" json:"batchSizeHeight"` //平板尺寸
 
 	Remark     string    `bson:"remark,omitempty" json:"remark"`     //备注
 	Size       string    `bson:"size,omitempty" json:"size"`         //工艺尺寸

+ 0 - 4
readme.md

@@ -1,5 +1 @@
-// 读取表格
 
-	// 数据处理
-
-	// 填写表格