animeic 2 年之前
父節點
當前提交
b74e0ecc53
共有 5 個文件被更改,包括 201 次插入145 次删除
  1. 2 2
      boxcost/api/IExcel.go
  2. 151 120
      boxcost/api/plan.go
  3. 23 22
      boxcost/api/print.go
  4. 24 0
      boxcost/api/utils.go
  5. 1 1
      boxcost/db/model/pack.go

+ 2 - 2
boxcost/api/IExcel.go

@@ -1,10 +1,10 @@
 package api
 
 type IExcel interface {
+	SetSignatures(sign interface{})
 	SetContent(content interface{})
 	SetTitle(title string)
 	SetRow(row int)
 	GetRow() int
-
-	SetSignatures(sign interface{})
+	Draws()
 }

+ 151 - 120
boxcost/api/plan.go

@@ -6,9 +6,11 @@ import (
 	"box-cost/log"
 	"errors"
 	"fmt"
+	"strings"
 	"time"
 
 	"github.com/gin-gonic/gin"
+	"github.com/xuri/excelize/v2"
 	"go.mongodb.org/mongo-driver/bson"
 	"go.mongodb.org/mongo-driver/bson/primitive"
 )
@@ -85,131 +87,160 @@ type SupplierPlanCost struct {
 
 // }
 
-// func DownLoadPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-// 	_planId := c.Query("id")
-// 	compId := c.Query("compId")
-// 	planId, err := primitive.ObjectIDFromHex(_planId)
-// 	if err != nil {
-// 		return nil, errors.New("planId错误")
-// 	}
-// 	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("数据未找到")
-// 	}
-// 	// 获取部件单据
-// 	curComp := &model.PackComponent{}
-// 	for _, comp := range plan.Pack.Components {
-// 		if comp.Id == compId {
-// 			curComp = comp
-// 		}
-// 	}
-// 	if curComp.Id == "" {
-// 		return nil, errors.New("该组件不存在")
-// 	}
+func DownLoadCompBills(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	_planId := c.Query("id")
+	compId := c.Query("compId")
+	planId, err := primitive.ObjectIDFromHex(_planId)
+	if err != nil {
+		return nil, errors.New("planId错误")
+	}
+	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("数据未找到")
+	}
+	// 获取部件单据
+	curComp := &model.PackComponent{}
+	for _, comp := range plan.Pack.Components {
+		if comp.Id == compId {
+			curComp = comp
+		}
+	}
+	if curComp.Id == "" {
+		return nil, errors.New("该组件不存在")
+	}
 
-// 	// 获取bill
-// 	if len(curComp.Stages) == 0 {
-// 		return nil, errors.New("该组件数据不存在")
-// 	}
-// 	f := excelize.NewFile()
-// 	index := f.NewSheet("Sheet1")
-// 	f.SetActiveSheet(index)
-// 	f.SetDefaultFont("宋体")
-// 	companyName := getCompanyName(apictx)
-
-// 	// 获取不同类型的单据id
-// 	set := make(map[int]map[primitive.ObjectID]struct{})
-// 	for _, stage := range curComp.Stages {
-// 		_billId := stage.BillId
-// 		billId, _ := primitive.ObjectIDFromHex(_billId)
-// 		if !billId.IsZero() {
-// 			set[stage.Type] = map[primitive.ObjectID]struct{}{billId: struct{}{}}
-// 		}
-// 	}
+	// 获取bill
+	if len(curComp.Stages) == 0 {
+		return nil, errors.New("该组件数据不存在")
+	}
 
-// 	// row := 0
-// 	// for k, bill := range set {
-// 	// 	for billId, _ := range bill {
-// 	// 		var billExcel IExcel
-// 	// 		collectName := ""
-// 	// 		if k == 1 {
-// 	// 			collectName = repo.CollectionBillPurchase
-// 	// 			billExcel = NewPurchaseBill(f)
-// 	// 		}
-// 	// 		if k == 2 {
-// 	// 			collectName = repo.CollectionBillProduce
-// 	// 			billExcel = NewProduceBill(f)
-// 	// 		}
-// 	// 		if k == 2 {
-// 	// 			collectName = repo.CollectionBillProduct
-// 	// 			billExcel = NewProductBill(f)
-// 	// 		}
-// 	// 		billExcel.SetRow(row)
-
-// 			// purchase := model.PurchaseBill{}
-// 			// found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
-// 			// 	CollectName: repo.CollectionBillPurchase,
-// 			// 	Query:       repo.Map{"_id": billId},
-// 			// }, &purchase)
-// 			// fmt.Println(purchase)
-// 			// if found {
-// 			// 	var billExcel IPurchBill
-// 			// 	if purchase.Process != nil {
-// 			// 		billExcel = NewProcessBill(f)
-// 			// 	}
-
-// 			// 	if len(purchase.Paper) > 0 {
-// 			// 		billExcel = NewPurchaseBill(f)
-// 			// 	}
-// 			// 	// purchaseExcel := NewPurchaseBill(f)
-// 			// 	billExcel.SetContent(&purchase)
-// 			// 	billExcel.SetTitle(fmt.Sprintf("%s原材料采购单", companyName))
-// 			// 	billExcel.SetOffset(offset)
-// 			// 	// billExcel.Title = fmt.Sprintf("%s原材料采购单", companyName)
-// 			// 	//设置对应的数据
-// 			// 	// purchaseExcel.Offset = offset
-// 			// 	billExcel.Draws()
-
-// 			// 	offset += 15
-// 			// }
-// //
-// 		// }
-
-// 		// 采购单
-// 		// if k == 1 {
-
-// 		// }
-// 		// // 加工单
-// 		// if k == 2 {
-// 		// 	for billId, _ := range bill {
-
-// 		// 	}
-
-// 		// }
-// 		// // 成品采购单
-// 		// if k == 3 {
-// 		// 	for billId, _ := range bill {
-
-// 		// 	}
-
-// 		// }
-// 	}
+	// 获取不同类型的单据id
+	// set := make(map[int]map[primitive.ObjectID]struct{})
+	billIds := make([]string, 0)
+	for _, stage := range curComp.Stages {
+		_billId := stage.BillId
 
-// 	c.Header("Content-Type", "application/octet-stream")
-// 	c.Header("Content-Disposition", "attachment; filename="+"bill.xlsx")
-// 	c.Header("Content-Transfer-Encoding", "binary")
+		billId, _ := primitive.ObjectIDFromHex(_billId)
+		if !billId.IsZero() {
+			// set[stage.Type] = map[primitive.ObjectID]struct{}{billId: struct{}{}}
+			billIds = append(billIds, fmt.Sprintf("%d_%s", stage.BillType, stage.BillId))
 
-// 	err = f.Write(c.Writer)
-// 	if err != nil {
-// 		return nil, err
-// 	}
+		}
+	}
+	typeBillIds := removeDuplicationSort(billIds)
+	if len(typeBillIds) < 1 {
+		return nil, errors.New("未找到单据信息")
+	}
+	f := excelize.NewFile()
+	index := f.NewSheet("Sheet1")
+	f.SetActiveSheet(index)
+	f.SetDefaultFont("宋体")
+	companyName := getCompanyName(apictx)
+
+	row := 0
+	for _, tId := range typeBillIds {
+		tidArr := strings.Split(tId, "_")
+
+		var billExcel IExcel
+
+		// 采购
+		billId, _ := primitive.ObjectIDFromHex(tidArr[1])
+		if tidArr[0] == "1" {
+			purchase := model.PurchaseBill{}
+			found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
+				CollectName: repo.CollectionBillPurchase,
+				Query:       repo.Map{"_id": billId},
+			}, &purchase)
+			if found {
+				billExcel = NewPurchaseBill(f)
+				if purchase.Reviewed == 1 {
+					if len(purchase.SignUsers) > 0 {
+						signs := []*model.Signature{}
+						repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
+							CollectName: repo.CollectionSignature,
+							Query:       repo.Map{"_id": bson.M{"$in": purchase.SignUsers}},
+							Sort:        bson.M{"sort": 1}, // 升序
+						}, &signs)
+						billExcel.SetSignatures(signs)
+					}
 
-// 	return nil, nil
-// }
+				}
+				billExcel.SetContent(&purchase)
+				billExcel.SetTitle(fmt.Sprintf("%s原材料采购单", companyName))
+			}
+
+		}
+		// 工艺
+		if tidArr[0] == "2" {
+			produce := model.ProduceBill{}
+			found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
+				CollectName: repo.CollectionBillProduce,
+				Query:       repo.Map{"_id": billId},
+			}, &produce)
+			if found {
+				billExcel = NewProduceBill(f)
+				if produce.Reviewed == 1 {
+					if len(produce.SignUsers) > 0 {
+						signs := []*model.Signature{}
+						repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
+							CollectName: repo.CollectionSignature,
+							Query:       repo.Map{"_id": bson.M{"$in": produce.SignUsers}},
+							Sort:        bson.M{"sort": 1}, // 升序
+						}, &signs)
+						billExcel.SetSignatures(signs)
+					}
+
+				}
+				billExcel.SetContent(&produce)
+				billExcel.SetTitle(fmt.Sprintf("%s加工单", companyName))
+			}
+
+		}
+		// 成品采购
+		if tidArr[0] == "3" {
+			product := model.ProductBill{}
+			found, _ := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
+				CollectName: repo.CollectionBillProduct,
+				Query:       repo.Map{"_id": billId},
+			}, &product)
+			if found {
+				billExcel = NewProduceBill(f)
+				if product.Reviewed == 1 {
+					if len(product.SignUsers) > 0 {
+						signs := []*model.Signature{}
+						repo.RepoDocsSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
+							CollectName: repo.CollectionSignature,
+							Query:       repo.Map{"_id": bson.M{"$in": product.SignUsers}},
+							Sort:        bson.M{"sort": 1}, // 升序
+						}, &signs)
+						billExcel.SetSignatures(signs)
+					}
+
+				}
+				billExcel.SetContent(&product)
+				billExcel.SetTitle(fmt.Sprintf("%s成品采购单", companyName))
+			}
+		}
+		billExcel.SetRow(row)
+		billExcel.Draws()
+		row = billExcel.GetRow() + 3
+	}
+
+	c.Header("Content-Type", "application/octet-stream")
+	c.Header("Content-Disposition", "attachment; filename="+"bill.xlsx")
+	c.Header("Content-Transfer-Encoding", "binary")
+
+	err = f.Write(c.Writer)
+	if err != nil {
+		return nil, err
+	}
+
+	return nil, nil
+}
 
 // 创建生产计划
 func CreateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error) {

+ 23 - 22
boxcost/api/print.go

@@ -39,37 +39,38 @@ func Printr(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	// fmt.Println(endTime.Unix())
 
 	// return "success", nil
-	arr := []string{"a", "b", "a", "c", "c", "b"}
-	newArr := removeDuplication_sort(arr)
+	arr := []string{"a", "a", "a", "b", "a", "a", "a", "c", "c", "b"}
+	newArr := removeDuplicationSort(arr)
 	for k, v := range newArr {
-		fmt.Println("k:%d,v:%s", k, v)
+		fmt.Printf("k:%d,v:%s\n", k, v)
 	}
 	return nil, nil
 
 }
 
-func removeDuplication_sort(arr []string) []string {
-	length := len(arr)
-	if length == 0 {
-		return arr
-	}
+// // 去重相邻元素
+// func removeDuplicationSort(arr []string) []string {
+// 	length := len(arr)
+// 	if length == 0 {
+// 		return arr
+// 	}
 
-	j := 0
-	for i := 1; i < length; i++ {
-		if arr[i] != arr[j] {
-			j++
-			if j < i {
-				swap(arr, i, j)
-			}
-		}
-	}
+// 	j := 0
+// 	for i := 1; i < length; i++ {
+// 		if arr[i] != arr[j] {
+// 			j++
+// 			if j < i {
+// 				swap(arr, i, j)
+// 			}
+// 		}
+// 	}
 
-	return arr[:j+1]
-}
+// 	return arr[:j+1]
+// }
 
-func swap(arr []string, a, b int) {
-	arr[a], arr[b] = arr[b], arr[a]
-}
+// func swap(arr []string, a, b int) {
+// 	arr[a], arr[b] = arr[b], arr[a]
+// }
 
 // // 生成百万数据
 // func GenData(_ *gin.Context, apictx *ApiSession) (interface{}, error) {

+ 24 - 0
boxcost/api/utils.go

@@ -17,6 +17,30 @@ import (
 
 var SignatureDir string = "https://www.3dqueen.cloud/box/v1/boxcost/public/"
 
+// 去重相邻元素
+func removeDuplicationSort(arr []string) []string {
+	length := len(arr)
+	if length == 0 {
+		return arr
+	}
+
+	j := 0
+	for i := 1; i < length; i++ {
+		if arr[i] != arr[j] {
+			j++
+			if j < i {
+				swap(arr, i, j)
+			}
+		}
+	}
+
+	return arr[:j+1]
+}
+
+func swap(arr []string, a, b int) {
+	arr[a], arr[b] = arr[b], arr[a]
+}
+
 func generateSerial(ctx *ApiSession, typeName string) (serial string, err error) {
 	// 获取类型
 	cate := &model.Category{}

+ 1 - 1
boxcost/db/model/pack.go

@@ -81,5 +81,5 @@ type ComponentStage struct {
 	Type       int       `bson:"type,omitempty" json:"type"`         //1-材料 2-工艺 3-成品
 	Group      string    `bson:"group,omitempty" json:"group"`       //工序合标记,相同Group的数据合并成一个单据
 	BillId     string    `bson:"billId,omitempty" json:"billId"`     //订单Id
-	BillType   string    `bson:"billType,omitempty" json:"billType"` //订单type // 1,2,3
+	BillType   int       `bson:"billType,omitempty" json:"billType"` //订单type // 1,2,3
 }