animeic před 2 roky
rodič
revize
8ad81eebdd
2 změnil soubory, kde provedl 61 přidání a 301 odebrání
  1. 36 31
      boxcost/api/plan-cost-excel.go
  2. 25 270
      boxcost/api/print.go

+ 36 - 31
boxcost/api/plan-cost-excel.go

@@ -27,7 +27,7 @@ func (b *PlanCostExcel) drawTitle() error {
 	// tileIndex := b.Offset + 1
 	b.Row++
 	startCell := fmt.Sprintf("A%d", b.Row)
-	err := b.Excel.MergeCell(b.SheetName, startCell, fmt.Sprintf("M%d", b.Row))
+	err := b.Excel.MergeCell(b.SheetName, startCell, fmt.Sprintf("N%d", b.Row))
 	if err != nil {
 		return err
 	}
@@ -130,18 +130,19 @@ func (b *PlanCostExcel) drawTableTitle() error {
 	drawCol("A", "产品部件名称")
 	drawCol2("B", "C", "类型/项目", "类型", "项目")
 	drawCol("D", "供应商名称")
-	drawCol3("E", "F", "G", "规格", "厚度(纸克)", "长", "宽")
-	drawCol("H", "单位")
-	drawCol("I", "下单数量")
-	drawCol("J", "实际数量")
-	drawCol("K", "单价") // ??? 下单单价
-	drawCol("L", "预算金额")
-	drawCol("M", "实际金额")
+	drawCol("E", "(单价1")
+	drawCol3("F", "G", "H", "规格", "厚度(纸克)", "长", "宽")
+	drawCol("I", "单位")
+	drawCol("J", "下单数量")
+	drawCol("K", "实际数量")
+	drawCol("L", "单价") // ??? 下单单价
+	drawCol("M", "预算金额")
+	drawCol("N", "实际金额")
 	return nil
 }
 
 func (b *PlanCostExcel) drawRow(rowIndex int, values ...string) {
-	charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M"}
+	charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N"}
 	for i, c := range charas {
 		v := ""
 		if i < len(values) {
@@ -208,8 +209,9 @@ func (b *PlanCostExcel) drawSupplierContent() error {
 							perRealPrice += stage.OrderPrice * float64(stage.ConfirmCount)
 							realPrice := fmt.Sprintf("%.3f", stage.OrderPrice*float64(stage.ConfirmCount))
 							b.FormatToEmpty(&realPrice)
+							unit := stage.Unit
 							if stage.Unit == "吨" || stage.Unit == "平方米" {
-								stage.Unit = "张"
+								unit = "张"
 							}
 
 							// 生成单据时已billType为准
@@ -226,7 +228,7 @@ func (b *PlanCostExcel) drawSupplierContent() error {
 							if stage.Type == 3 {
 								stageType = "成品采购"
 							}
-							b.drawRow(b.Row, "", stageType, stage.Name, "", stage.Norm, matHeigth, matWidth, stage.Unit, orderCount, realCount, price, budgetPrice, realPrice)
+							b.drawRow(b.Row, "", stageType, stage.Name, "", fmt.Sprintf("%.3f元/%s", stage.Price, stage.Unit), stage.Norm, matHeigth, matWidth, unit, orderCount, realCount, price, budgetPrice, realPrice)
 							cates[stage.SupplierInfo.Name] = append(cates[stage.SupplierInfo.Name], b.Row)
 							b.Row++
 						}
@@ -267,24 +269,24 @@ func (b *PlanCostExcel) drawSupplierContent() error {
 
 	// 生产汇总金额
 	startACell := fmt.Sprintf("%s%d", "A", b.Row)
-	endKCell := fmt.Sprintf("%s%d", "K", b.Row)
-	LCell := fmt.Sprintf("%s%d", "L", b.Row)
+	endLCell := fmt.Sprintf("%s%d", "L", b.Row)
 	MCell := fmt.Sprintf("%s%d", "M", b.Row)
-	b.Excel.MergeCell(b.SheetName, startACell, endKCell)
-	b.Excel.SetCellStyle(b.SheetName, startACell, endKCell, b.AlignCenterStyle)
+	NCell := fmt.Sprintf("%s%d", "N", b.Row)
+	b.Excel.MergeCell(b.SheetName, startACell, endLCell)
+	b.Excel.SetCellStyle(b.SheetName, startACell, endLCell, b.AlignCenterStyle)
 	b.Excel.SetCellValue(b.SheetName, startACell, fmt.Sprintf("【%s】生产计划汇总金额", supplier))
 
 	// 生产预算汇总
-	b.Excel.SetCellStyle(b.SheetName, LCell, LCell, b.AlignCenterStyle)
+	b.Excel.SetCellStyle(b.SheetName, MCell, MCell, b.AlignCenterStyle)
 	planOrderRealPrice := fmt.Sprintf("%.3f", totalBudgetPrice)
 	b.FormatToEmpty(&planOrderRealPrice)
-	b.Excel.SetCellValue(b.SheetName, LCell, planOrderRealPrice)
+	b.Excel.SetCellValue(b.SheetName, MCell, planOrderRealPrice)
 
 	// 生产实际汇总
-	b.Excel.SetCellStyle(b.SheetName, MCell, MCell, b.AlignCenterStyle)
+	b.Excel.SetCellStyle(b.SheetName, NCell, NCell, b.AlignCenterStyle)
 	planRealPrice := fmt.Sprintf("%.3f", totalRealPrice)
 	b.FormatToEmpty(&planRealPrice)
-	b.Excel.SetCellValue(b.SheetName, MCell, planRealPrice)
+	b.Excel.SetCellValue(b.SheetName, NCell, planRealPrice)
 
 	return nil
 }
@@ -327,9 +329,11 @@ func (b *PlanCostExcel) drawAllContent() error {
 					perRealPrice += stage.OrderPrice * float64(stage.ConfirmCount)
 					realPrice := fmt.Sprintf("%.3f", stage.OrderPrice*float64(stage.ConfirmCount))
 					b.FormatToEmpty(&realPrice)
+					unit := stage.Unit
 					if stage.Unit == "吨" || stage.Unit == "平方米" {
-						stage.Unit = "张"
+						unit = "张"
 					}
+
 					supplierName := ""
 					if stage.SupplierInfo != nil {
 						supplierName = stage.SupplierInfo.Name
@@ -347,7 +351,7 @@ func (b *PlanCostExcel) drawAllContent() error {
 					if stage.Type == 3 {
 						stageType = "成品采购"
 					}
-					b.drawRow(b.Row, "", stageType, stage.Name, supplierName, stage.Norm, matHeigth, matWidth, stage.Unit, orderCount, realCount, price, budgetPrice, realPrice)
+					b.drawRow(b.Row, "", stageType, stage.Name, supplierName, fmt.Sprintf("%.3f元/%s", stage.Price, stage.Unit), stage.Norm, matHeigth, matWidth, unit, orderCount, realCount, price, budgetPrice, realPrice)
 					if stage.SupplierInfo != nil {
 						cates[stage.SupplierInfo.Name] = append(cates[stage.SupplierInfo.Name], b.Row)
 
@@ -383,24 +387,24 @@ func (b *PlanCostExcel) drawAllContent() error {
 		}
 	}
 	startACell := fmt.Sprintf("%s%d", "A", b.Row)
-	endKCell := fmt.Sprintf("%s%d", "K", b.Row)
-	LCell := fmt.Sprintf("%s%d", "L", b.Row)
+	endLCell := fmt.Sprintf("%s%d", "L", b.Row)
 	MCell := fmt.Sprintf("%s%d", "M", b.Row)
-	b.Excel.MergeCell(b.SheetName, startACell, endKCell)
-	b.Excel.SetCellStyle(b.SheetName, startACell, endKCell, b.AlignCenterStyle)
+	NCell := fmt.Sprintf("%s%d", "N", b.Row)
+	b.Excel.MergeCell(b.SheetName, startACell, endLCell)
+	b.Excel.SetCellStyle(b.SheetName, startACell, endLCell, b.AlignCenterStyle)
 	b.Excel.SetCellValue(b.SheetName, startACell, "生产计划汇总金额")
 
 	// 生产预算汇总
-	b.Excel.SetCellStyle(b.SheetName, LCell, LCell, b.AlignCenterStyle)
+	b.Excel.SetCellStyle(b.SheetName, MCell, MCell, b.AlignCenterStyle)
 	planOrderRealPrice := fmt.Sprintf("%.3f", totalBudgetPrice)
 	b.FormatToEmpty(&planOrderRealPrice)
-	b.Excel.SetCellValue(b.SheetName, LCell, planOrderRealPrice)
+	b.Excel.SetCellValue(b.SheetName, MCell, planOrderRealPrice)
 
 	// 生产实际汇总
-	b.Excel.SetCellStyle(b.SheetName, MCell, MCell, b.AlignCenterStyle)
+	b.Excel.SetCellStyle(b.SheetName, NCell, NCell, b.AlignCenterStyle)
 	planRealPrice := fmt.Sprintf("%.3f", totalRealPrice)
 	b.FormatToEmpty(&planRealPrice)
-	b.Excel.SetCellValue(b.SheetName, MCell, planRealPrice)
+	b.Excel.SetCellValue(b.SheetName, NCell, planRealPrice)
 
 	return nil
 }
@@ -442,8 +446,9 @@ func NewPlanCostExcel(f *excelize.File) *PlanCostExcel {
 	f.SetColWidth(b.SheetName, "A", "B", 12)
 	f.SetColWidth(b.SheetName, "C", "C", 16)
 	f.SetColWidth(b.SheetName, "D", "D", 25)
-	f.SetColWidth(b.SheetName, "E", "E", 16)
-	f.SetColWidth(b.SheetName, "F", "M", 10)
+	f.SetColWidth(b.SheetName, "E", "E", 15)
+	f.SetColWidth(b.SheetName, "F", "F", 16)
+	f.SetColWidth(b.SheetName, "F", "N", 12)
 	f.SetPageMargins(b.SheetName, excelize.PageMarginTop(0), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
 	return b
 }

+ 25 - 270
boxcost/api/print.go

@@ -1,284 +1,39 @@
 package api
 
 import (
+	"fmt"
+	"sync"
+	"time"
+
 	"github.com/gin-gonic/gin"
 )
 
-// import (
-// 	"box-cost/db/model"
-// 	"box-cost/db/repo"
-// 	randc "crypto/rand"
-// 	"encoding/json"
-// 	"fmt"
-// 	"log"
-// 	"math/big"
-// 	"math/rand"
-// 	"sort"
-// 	"sync"
-// 	"time"
-// 	"unsafe"
-
-// 	"github.com/gin-gonic/gin"
-// 	"go.mongodb.org/mongo-driver/bson"
-// 	"go.mongodb.org/mongo-driver/mongo"
-// )
-
 func Printr(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	c1 := make(chan int)
+	var wg sync.WaitGroup
 
-	// https://sku3d-test.obs.cn-east-3.myhuaweicloud.com/image/jpg/1675757991547cnt7X9_2.jpg zhang
-	// https://sku3d-test.obs.cn-east-3.myhuaweicloud.com/image/jpg/1675758027305uLKtda_1.jpg zhu
-	// loc, _ := time.LoadLocation("Local")
-	// date := time.Now().Format("2006-01-02")
-	// startDate := date + " 00:00:00"
-	// startTime, _ := time.ParseInLocation("2006-01-02 15:04:05", startDate, loc)
-	// endDate := date + " 23:59:59"
-	// endTime, _ := time.ParseInLocation("2006-01-02 15:04:05", endDate, loc)
-	// fmt.Println(startTime.Unix())
-	// fmt.Println(endTime.Unix())
+	for i := 0; i < 3; i++ {
+		wg.Add(1)
+		go work(&wg, c1, i)
+	}
 
-	// print
-	// return "success", nil
+	go func() {
+		wg.Wait()
+		close(c1)
+	}()
+
+	for n := range c1 {
+		fmt.Println(n)
+	}
 
-	// 去重
-	// arr := []string{"a", "a", "a", "b", "a", "a", "a", "c", "c", "b"}
-	// newArr := removeDuplicationSort(arr)
-	// for k, v := range newArr {
-	// 	fmt.Printf("k:%d,v:%s\n", k, v)
-	// }
 	return nil, nil
 
 }
 
-// buf, _ := f.WriteToBuffer()
-// 		res, err := excelToPdf(buf, apictx.Svc.Conf.PdfApiAddr)
-// 		if err != nil {
-// 			return nil, errors.New("转化pdf失败")
-// 		}
-// 		body := res.Body
-// 		byteData, err := io.ReadAll(body)
-// 		if err != nil {
-// 			return nil, err
-// 		}
-// 		defer res.Body.Close()
-// 		// os.Mkdir("tmp", 0666)
-// 		if err := os.WriteFile("aaa.pdf", byteData, 0666); err != nil {
-// 			log.Fatal(err)
-// 		}
-
-// // 去重相邻元素
-// 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 GenData(_ *gin.Context, apictx *ApiSession) (interface{}, error) {
-// 	var wg sync.WaitGroup
-// 	for i := 0; i < 100; i++ {
-// 		wg.Add(1)
-// 		go insertData(apictx, &wg)
-// 	}
-// 	wg.Wait()
-
-// 	return true, nil
-// }
-
-// // 聚合查询百万数据
-// func SearchData(_ *gin.Context, apictx *ApiSession) (interface{}, error) {
-// 	dataStatResult := make(chan bson.M)
-// 	var output ResultSlice
-// 	var wg sync.WaitGroup
-// 	// 82个gorutine 执行聚合
-// 	for i := 18; i < 100; i++ {
-// 		wg.Add(1)
-// 		go DataAggregate(apictx, i, dataStatResult, &wg)
-// 	}
-
-// 	// 从管道中获取聚合结果 保存在数组中
-// 	for value := range dataStatResult {
-// 		output = append(output, OutPut{
-// 			Age:       value["age"].(int32),
-// 			Sex:       value["sex"].(int32),
-// 			Total:     value["total"].(int32),
-// 			AvgSalary: value["avgSalary"].(float64),
-// 		})
-// 		if len(output) == 164 { // 如果大于164,不会跳出;如果小于164,wg.done != wg.add ,wg.wait阻塞
-// 			break
-// 		}
-// 	}
-
-// 	wg.Wait()
-// 	//倒序排列
-// 	sort.Sort(output)
-// 	for _, v := range output {
-// 		result, err := json.Marshal(&v)
-// 		if err != nil {
-// 			fmt.Println("json.marshal failed, err:", err)
-// 			return nil, err
-// 		}
-// 		fmt.Println(string(result))
-// 	}
-// 	return output, nil
-// }
-
-// func insertData(apictx *ApiSession, wg *sync.WaitGroup) {
-// 	collectName := "aggregate-test"
-// 	rowNum := 10000
-// 	for i := 0; i < rowNum; i++ {
-// 		repo.RepoAddDoc(apictx.CreateRepoCtx(), collectName, &model.AggregateTest{
-// 			Name:   randName(6),
-// 			Age:    randAge(),
-// 			Sex:    randSex(),
-// 			Salary: randSalary(),
-// 		})
-
-// 	}
-// 	wg.Done()
-// }
-
-// func randSex() *int {
-// 	result, _ := randc.Int(randc.Reader, big.NewInt(2))
-// 	sex := int(result.Int64())
-// 	return &sex
-// }
-// func randAge() int {
-// 	result, _ := randc.Int(randc.Reader, big.NewInt(82))
-// 	return int(result.Int64() + 18)
-// }
-
-// func randSalary() int {
-// 	result, _ := randc.Int(randc.Reader, big.NewInt(10000))
-
-// 	return int(result.Int64() + 2000)
-// }
-
-// const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
-
-// var src = rand.NewSource(time.Now().UnixNano())
-
-// const (
-// 	// 6 bits to represent a letter index
-// 	letterIdBits = 6
-// 	// All 1-bits as many as letterIdBits
-// 	letterIdMask = 1<<letterIdBits - 1
-// 	letterIdMax  = 63 / letterIdBits
-// )
-
-// func randName(n int) string {
-// 	b := make([]byte, n)
-// 	// A rand.Int63() generates 63 random bits, enough for letterIdMax letters!
-// 	for i, cache, remain := n-1, src.Int63(), letterIdMax; i >= 0; {
-// 		if remain == 0 {
-// 			cache, remain = src.Int63(), letterIdMax
-// 		}
-// 		if idx := int(cache & letterIdMask); idx < len(letters) {
-// 			b[i] = letters[idx]
-// 			i--
-// 		}
-// 		cache >>= letterIdBits
-// 		remain--
-// 	}
-// 	return *(*string)(unsafe.Pointer(&b))
-// }
-
-// func genPipeline(age int) (bson.D, bson.D, bson.D) {
-
-// 	matchStage := bson.D{
-// 		{"$match", bson.D{
-// 			{"age",
-// 				bson.D{
-// 					{"$eq", age},
-// 				}},
-// 		}},
-// 	}
-// 	groupStage := bson.D{
-// 		{"$group", bson.D{
-// 			{"_id", bson.D{
-// 				{"age", "$age"},
-// 				{"sex", "$sex"},
-// 			}},
-// 			{"age", bson.D{
-// 				{"$first", "$age"},
-// 			}},
-// 			{"sex", bson.D{
-// 				{"$first", "$sex"},
-// 			}},
-// 			{"total", bson.D{
-// 				{"$sum", 1},
-// 			}},
-// 			{"avgSalary", bson.D{
-// 				{"$avg", "$salary"},
-// 			}},
-// 		}},
-// 	}
-// 	projectStage := bson.D{
-// 		{"$project", bson.D{
-// 			{"_id", 0},
-// 			{"age", 1},
-// 			{"sex", 1},
-// 			{"total", 1},
-// 			{"avgSalary", 1},
-// 		}},
-// 	}
-
-// 	return matchStage, groupStage, projectStage
-
-// }
-
-// func DataAggregate(apictx *ApiSession, age int, resultChan chan bson.M, wg *sync.WaitGroup) {
-// 	matchStage, groupStage, projectStage := genPipeline(age)
-
-// 	// opts := options.Aggregate().SetMaxTime(15 * time.Second)
-// 	cursor, err := apictx.Svc.Mongo.GetCollection("aggregate-test").Aggregate(apictx.CreateRepoCtx().Ctx, mongo.Pipeline{matchStage, groupStage, projectStage})
-// 	if err != nil {
-// 		log.Println(err)
-// 	}
-// 	//打印文档内容
-// 	var results []bson.M
-// 	if err = cursor.All(apictx.CreateRepoCtx().Ctx, &results); err != nil {
-// 		log.Println(err)
-// 	}
-// 	log.Printf("%#v\n", results)
-// 	for _, result := range results {
-// 		resultChan <- result
-// 	}
-
-// 	wg.Done()
-// }
-
-// type OutPut struct {
-// 	Age       int32   `json:"age"`
-// 	Sex       int32   `json:"sex"`
-// 	Total     int32   `json:"total"`
-// 	AvgSalary float64 `json:"avg_salary"`
-// }
-
-// type ResultSlice []OutPut
-
-// func (a ResultSlice) Len() int { // 重写 Len() 方法
-// 	return len(a)
-// }
-// func (a ResultSlice) Swap(i, j int) { // 重写 Swap() 方法
-// 	a[i], a[j] = a[j], a[i]
-// }
-// func (a ResultSlice) Less(i, j int) bool { // 重写 Less() 方法, 从大到小排序
-// 	return a[j].Age < a[i].Age
-// }
+func work(wg *sync.WaitGroup, c1 chan<- int, n int) {
+	if n == 1 {
+		time.Sleep(1 * time.Second)
+	}
+	c1 <- n
+	wg.Done()
+}