123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440 |
- 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 = ""
- }
- }
|