123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448 |
- package el
- import (
- "box-cost/model"
- "context"
- "fmt"
- "log"
- "regexp"
- "strings"
- "time"
- "github.com/xuri/excelize/v2"
- "go.mongodb.org/mongo-driver/bson"
- "go.mongodb.org/mongo-driver/bson/primitive"
- "go.mongodb.org/mongo-driver/mongo"
- )
- const (
- EXCEL_TMPLATE_FILE = "tmplate/tmplate.xlsx"
- PURCHASE_COLLECTION = "bill-purchase"
- PRODUCE_COLLECTION = "bill-produce"
- PRODUCT_COLLECTION = "bill-product"
- CELL_BACKGROUND = "808080"
- )
- var needChangeCol = map[string]string{
- "部件": "E",
- "下单": "F",
- "纸张": "G",
- "印刷": "H",
- "覆膜": "I",
- "烫金": "J",
- "丝印": "K",
- "对裱": "L",
- "压纹": "M",
- "裱瓦": "N",
- "模切": "O",
- "粘盒": "P",
- "组装": "Q",
- "交货": "R",
- }
- func MatchString(targetStr string, regexPattern string) bool {
-
- re := regexp.MustCompile(regexPattern)
-
- 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
- planStatusInfos := make([]*PlanStatusInfo, 0)
- planCompStatus := []map[string]string{}
- for _, plan := range plans {
- startRow := row
- for _, comp := range plan.Pack.Components {
-
-
- compStatus := map[string]string{
- "部件": " ",
- "行数": "5",
-
- "下单": " ",
-
- "纸张": " ",
-
- "印刷": " ",
-
- "覆膜": " ",
-
- "烫金": " ",
-
- "丝印": " ",
-
- "对裱": " ",
-
- "压纹": " ",
-
- "裱瓦": " ",
-
- "模切": " ",
-
- "粘盒": " ",
-
- "组装": " ",
- "交货": " ",
- }
- fmt.Println(plan.Name)
- fmt.Println(comp.Name)
- fmt.Println(row)
- fmt.Println("------------------------------------")
- compStatus["部件"] = comp.Name
- compStatus["行数"] = fmt.Sprintf("%d", row)
- row++
-
- seen := make(map[string]bool)
- 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 {
-
- seen[value] = true
-
- 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
- }
- }
- }
- }
-
-
- if len(tbills) == 0 {
-
- planCompStatus = append(planCompStatus, compStatus)
- continue
- }
-
-
- lastBillType := tbills[len(tbills)-1]
- for _, billType := range tbills {
- compStatus["下单"] = "√"
- bt := strings.Split(billType, "_")[0]
- billId, _ := primitive.ObjectIDFromHex(strings.Split(billType, "_")[1])
- if bt == "1" {
-
- bill := &model.PurchaseBill{}
- err := client.Database("box-cost").Collection(PURCHASE_COLLECTION).FindOne(context.Background(), bson.M{"_id": billId}).Decode(bill)
- if err != nil {
- log.Fatal(err)
- }
-
-
-
-
-
-
- compStatus["纸张"] = "〇"
- if bill.Status == "complete" {
- compStatus["纸张"] = "√"
- }
-
-
-
- if lastBillType == billType {
- for _, paper := range bill.Paper {
- compStatus["交货"] = fmt.Sprintf("%d", paper.ConfirmCount)
- }
- }
- }
- if bt == "2" {
-
- bill := &model.ProduceBill{}
- err := client.Database("box-cost").Collection(PRODUCE_COLLECTION).FindOne(context.Background(), bson.M{"_id": billId}).Decode(bill)
- if err != nil {
- log.Fatal(err)
- }
- for _, produce := range bill.Produces {
- for k := range compStatus {
- if k == "下单" || k == "纸张" || k == "交货" || k == "部件" || k == "行数" {
- continue
- }
- if MatchString(produce.Name, k) {
- compStatus[k] = "〇"
- if bill.Status == "complete" {
- compStatus[k] = "√"
- }
- }
-
-
-
-
-
-
- compStatus["交货"] = fmt.Sprintf("%d", produce.ConfirmCount)
- }
- }
- }
-
- if bt == "3" {
-
- bill := &model.ProductBill{}
- err := client.Database("box-cost").Collection(PRODUCT_COLLECTION).FindOne(context.Background(), bson.M{"_id": billId}).Decode(bill)
- if err != nil {
- log.Fatal(err)
- }
-
-
-
-
-
- if lastBillType == billType {
- for _, product := range bill.Products {
- compStatus["交货"] = fmt.Sprintf("%d", product.ConfirmCount)
- }
- }
- }
- }
- planCompStatus = append(planCompStatus, compStatus)
- }
-
-
-
- planStatusInfos = append(planStatusInfos, &PlanStatusInfo{
- PlanName: plan.Name,
-
- PlanUnit: "",
- PlanCount: plan.Total,
- PlanRowStart: startRow,
- PlanRowEnd: row - 1,
- PlanCompStatus: planCompStatus,
- })
- }
-
-
- UpdateCell1(planStatusInfos)
- }
- func UpdateCell(planCompStatus []map[string]string) {
- 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,
- Color: []string{CELL_BACKGROUND},
- },
- })
- if err != nil {
- log.Fatal(err)
- }
- 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)
-
-
- 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)
- }
- }
- 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,
- 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)
-
-
- 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)
- }
- }
|