package api import ( "fmt" "strings" "github.com/xuri/excelize/v2" ) // 生产成本表 type SummarySampleExcel struct { Row int Title string Excel *excelize.File SheetName string AlignCenterStyle int Content *SupplierPlanSummary } func (b *SummarySampleExcel) drawTitle() error { b.Row++ startCell := fmt.Sprintf("A%d", b.Row) err := b.Excel.MergeCell(b.SheetName, startCell, fmt.Sprintf("G%d", b.Row)) if err != nil { return err } style, err := b.Excel.NewStyle(&excelize.Style{ Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true}, Font: &excelize.Font{Bold: true, Size: 18}}) if err != nil { return err } err = b.Excel.SetCellStyle(b.SheetName, startCell, startCell, style) if err != nil { return err } b.Excel.SetRowHeight(b.SheetName, b.Row, 23) b.Excel.SetCellValue(b.SheetName, startCell, "追踪表") return nil } func (b *SummarySampleExcel) drawTableTitle() error { b.Row++ var drawCol = func(prefix string, value string) error { left1Cell := fmt.Sprintf("%s%d", prefix, b.Row) left2Cell := fmt.Sprintf("%s%d", prefix, b.Row+1) err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell) if err != nil { return err } err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle) if err != nil { return err } return b.Excel.SetCellValue(b.SheetName, left1Cell, value) } drawCol("A", "序号") drawCol("B", "产品名称") drawCol("C", "产品部件名称") drawCol("D", "下单日期") drawCol("E", "交货日期") drawCol("F", "工序") drawCol("G", "备注") return nil } func (b *SummarySampleExcel) drawRow(rowIndex int, values ...string) { charas := []string{"A", "B", "C", "D", "E", "F", "G"} for i, c := range charas { v := "" if i < len(values) { v = values[i] } b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v) val2Cel := fmt.Sprintf("%s%d", c, rowIndex) b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle) if c == "F" { border := []excelize.Border{ {Type: "top", Style: 1, Color: "000000"}, {Type: "left", Style: 1, Color: "000000"}, {Type: "right", Style: 1, Color: "000000"}, {Type: "bottom", Style: 1, Color: "000000"}, } styleLeft, _ := b.Excel.NewStyle(&excelize.Style{ Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true}, Font: &excelize.Font{Size: 11}, Border: border}) b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, styleLeft) } b.Excel.SetRowHeight(b.SheetName, rowIndex, 32) } } func (b *SummarySampleExcel) drawAllContent() error { b.Row += 2 // summaryPlans index := 0 for _, splan := range b.Content.Plans { index++ planStartRow := b.Row plan := splan.Plan comps := plan.Pack.Components if len(comps) > 0 { for _, comp := range comps { if len(comp.Stages) > 0 { startRow := b.Row cates := map[string][]int{} for _, stage := range comp.Stages { // 状态 stageStatus := "" statusMark := "【x】" if len(stage.BillId) < 1 { stageStatus = "未生成订单" } else { if splan.State[stage.BillId] == "created" { stageStatus = "进行中" // 审核状态 if splan.Reviewed[stage.BillId] == 1 { stageStatus = "已审核" if splan.IsSend[stage.BillId] { stageStatus = "已发送" if splan.IsAck[stage.BillId] { stageStatus = "已接单" } else { stageStatus = "未接单" } } else { stageStatus = "未发送" } } else { stageStatus = "未审核" } } else if splan.State[stage.BillId] == "complete" { stageStatus = "已完成" statusMark = "【v】" } else { stageStatus = "未生成订单" } } fmt.Println(stageStatus) deliveryTime := stage.DeliveryTime.Local().Format("2006-01-02") stageNmae := fmt.Sprintf("%s %s", statusMark, stage.Name) createTime := "-" if len(stage.BillId) == 24 { createTime = splan.CreateTimes[stage.BillId].Local().Format("2006-01-02") cates[fmt.Sprintf("%s,%s,%s", createTime, deliveryTime, stageNmae)] = append(cates[fmt.Sprintf("%s,%s,%s", createTime, deliveryTime, stageNmae)], b.Row) } b.drawRow(b.Row, "", "", "", createTime, deliveryTime, stageNmae, "") b.Row++ } for stageInfo, cate := range cates { sf := strings.Split(stageInfo, ",") if len(sf) == 3 { fmt.Println(sf) mergeStartRow := cate[0] mergeEndRow := cate[len(cate)-1] b.Excel.MergeCell(b.SheetName, fmt.Sprintf("D%d", mergeStartRow), fmt.Sprintf("D%d", mergeEndRow)) b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("D%d", mergeEndRow), sf[0]) b.Excel.MergeCell(b.SheetName, fmt.Sprintf("E%d", mergeStartRow), fmt.Sprintf("E%d", mergeEndRow)) b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("E%d", mergeEndRow), sf[1]) b.Excel.MergeCell(b.SheetName, fmt.Sprintf("F%d", mergeStartRow), fmt.Sprintf("F%d", mergeEndRow)) b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("F%d", mergeEndRow), sf[2]) } } endRow := b.Row - 1 // 组件名字 startACell := fmt.Sprintf("%s%d", "C", startRow) endACell := fmt.Sprintf("%s%d", "C", endRow) b.Excel.MergeCell(b.SheetName, startACell, endACell) err := b.Excel.SetCellStyle(b.SheetName, startACell, endACell, b.AlignCenterStyle) if err != nil { return err } b.Excel.SetCellValue(b.SheetName, startACell, comp.Name) } } } // 序号 planEndRow := b.Row planStartACell := fmt.Sprintf("%s%d", "A", planStartRow) planEndACell := fmt.Sprintf("%s%d", "A", planEndRow-1) b.Excel.MergeCell(b.SheetName, planStartACell, planEndACell) err := b.Excel.SetCellStyle(b.SheetName, planStartACell, planEndACell, b.AlignCenterStyle) if err != nil { return err } b.Excel.SetCellValue(b.SheetName, planStartACell, index) // 产品名 planStartBCell := fmt.Sprintf("%s%d", "B", planStartRow) planEndBCell := fmt.Sprintf("%s%d", "B", planEndRow-1) b.Excel.MergeCell(b.SheetName, planStartBCell, planEndBCell) err = b.Excel.SetCellStyle(b.SheetName, planStartBCell, planEndBCell, b.AlignCenterStyle) if err != nil { return err } b.Excel.SetCellValue(b.SheetName, planStartBCell, fmt.Sprintf("%s(%d)", plan.Name, plan.Total)) // 备注 planStartFCell := fmt.Sprintf("%s%d", "G", planStartRow) planEndFCell := fmt.Sprintf("%s%d", "G", planEndRow-1) b.Excel.MergeCell(b.SheetName, planStartFCell, planEndFCell) err = b.Excel.SetCellStyle(b.SheetName, planStartFCell, planEndFCell, b.AlignCenterStyle) if err != nil { return err } b.Excel.SetCellValue(b.SheetName, planStartFCell, "") } return nil } func (b *SummarySampleExcel) drawSupplierContent() error { b.Row += 2 index := 0 supplier := "" // summaryPlans for _, splan := range b.Content.Plans { planStartRow := b.Row isContainSupplier := false plan := splan.Plan comps := plan.Pack.Components if len(comps) > 0 { for _, comp := range comps { if len(comp.Stages) > 0 { startRow := 0 cates := map[string][]int{} for _, stage := range comp.Stages { if stage.SupplierInfo != nil { if b.Content.SupplierId == stage.SupplierInfo.Id { supplier = stage.SupplierInfo.Name isContainSupplier = true // 材料 if startRow == 0 { startRow = b.Row } // 状态 stageStatus := "" statusMark := "【×】" if len(stage.BillId) < 1 { stageStatus = "未生成订单" } else { if splan.State[stage.BillId] == "created" { stageStatus = "进行中" // 审核状态 if splan.Reviewed[stage.BillId] == 1 { stageStatus = "已审核" if splan.IsSend[stage.BillId] { stageStatus = "已发送" if splan.IsAck[stage.BillId] { stageStatus = "已接单" } else { stageStatus = "未接单" } } else { stageStatus = "未发送" } } else { stageStatus = "未审核" } } else if splan.State[stage.BillId] == "complete" { stageStatus = "已完成" statusMark = "【√】" } else { stageStatus = "未生成订单" } } fmt.Println(stageStatus) deliveryTime := stage.DeliveryTime.Local().Format("2006-01-02") stageNmae := fmt.Sprintf("%s %s", statusMark, stage.Name) createTime := "-" if len(stage.BillId) == 24 { createTime = splan.CreateTimes[stage.BillId].Local().Format("2006-01-02") cates[fmt.Sprintf("%s,%s,%s", createTime, deliveryTime, stageNmae)] = append(cates[fmt.Sprintf("%s,%s,%s", createTime, deliveryTime, stageNmae)], b.Row) } b.drawRow(b.Row, "", "", "", createTime, deliveryTime, stageNmae, "") b.Row++ } } } for stageInfo, cate := range cates { sf := strings.Split(stageInfo, ",") if len(sf) == 3 { fmt.Println(sf) mergeStartRow := cate[0] mergeEndRow := cate[len(cate)-1] b.Excel.MergeCell(b.SheetName, fmt.Sprintf("D%d", mergeStartRow), fmt.Sprintf("D%d", mergeEndRow)) b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("D%d", mergeEndRow), sf[0]) b.Excel.MergeCell(b.SheetName, fmt.Sprintf("E%d", mergeStartRow), fmt.Sprintf("E%d", mergeEndRow)) b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("E%d", mergeEndRow), sf[1]) b.Excel.MergeCell(b.SheetName, fmt.Sprintf("F%d", mergeStartRow), fmt.Sprintf("F%d", mergeEndRow)) b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("F%d", mergeEndRow), sf[2]) } } if startRow != 0 { endRow := b.Row - 1 // 组件名字 startACell := fmt.Sprintf("%s%d", "C", startRow) endACell := fmt.Sprintf("%s%d", "C", endRow) b.Excel.MergeCell(b.SheetName, startACell, endACell) err := b.Excel.SetCellStyle(b.SheetName, startACell, endACell, b.AlignCenterStyle) if err != nil { return err } b.Excel.SetCellValue(b.SheetName, startACell, comp.Name) } } } } if !isContainSupplier { continue } index++ // 序号 planEndRow := b.Row planStartACell := fmt.Sprintf("%s%d", "A", planStartRow) planEndACell := fmt.Sprintf("%s%d", "A", planEndRow-1) b.Excel.MergeCell(b.SheetName, planStartACell, planEndACell) err := b.Excel.SetCellStyle(b.SheetName, planStartACell, planEndACell, b.AlignCenterStyle) if err != nil { return err } b.Excel.SetCellValue(b.SheetName, planStartACell, index) // 产品名 planStartBCell := fmt.Sprintf("%s%d", "B", planStartRow) planEndBCell := fmt.Sprintf("%s%d", "B", planEndRow-1) b.Excel.MergeCell(b.SheetName, planStartBCell, planEndBCell) err = b.Excel.SetCellStyle(b.SheetName, planStartBCell, planEndBCell, b.AlignCenterStyle) if err != nil { return err } b.Excel.SetCellValue(b.SheetName, planStartBCell, fmt.Sprintf("%s(%d)", plan.Name, plan.Total)) // 备注 planStartFCell := fmt.Sprintf("%s%d", "G", planStartRow) planEndFCell := fmt.Sprintf("%s%d", "G", planEndRow-1) b.Excel.MergeCell(b.SheetName, planStartFCell, planEndFCell) err = b.Excel.SetCellStyle(b.SheetName, planStartFCell, planEndFCell, b.AlignCenterStyle) if err != nil { return err } b.Excel.SetCellValue(b.SheetName, planStartFCell, "") } // 供应商名字标题 style, err := b.Excel.NewStyle(&excelize.Style{ Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true}, }) if err != nil { return err } err = b.Excel.SetCellStyle(b.SheetName, "A1", "G1", style) if err != nil { return err } b.Excel.SetRowHeight(b.SheetName, 1, 32) b.Excel.SetCellValue(b.SheetName, "A1", fmt.Sprintf("【%s】-追踪表", supplier)) return nil } func (b *SummarySampleExcel) Draws() { b.drawTitle() b.drawTableTitle() if !b.Content.SupplierId.IsZero() { b.drawSupplierContent() } else { b.drawAllContent() } } func NewSummarySampleExcel(f *excelize.File) *SummarySampleExcel { border := []excelize.Border{ {Type: "top", Style: 1, Color: "000000"}, {Type: "left", Style: 1, Color: "000000"}, {Type: "right", Style: 1, Color: "000000"}, {Type: "bottom", Style: 1, Color: "000000"}, } styleLeft, _ := f.NewStyle(&excelize.Style{ Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true}, Border: border, Font: &excelize.Font{Size: 10}, }) b := &SummarySampleExcel{ Title: "生产成本表", SheetName: "Sheet1", Excel: f, AlignCenterStyle: styleLeft, } f.SetColWidth(b.SheetName, "A", "A", 6) f.SetColWidth(b.SheetName, "B", "E", 16) f.SetColWidth(b.SheetName, "F", "F", 20) f.SetColWidth(b.SheetName, "G", "G", 16) f.SetPageMargins(b.SheetName, excelize.PageMarginTop(0), excelize.PageMarginLeft(0), excelize.PageMarginRight(0)) return b } func (b *SummarySampleExcel) FormatToEmpty(str *string) { if *str == "0" || *str == "0.000" { *str = "" } }