123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262 |
- package api
- import (
- "box-cost/db/model"
- "fmt"
- "github.com/xuri/excelize/v2"
- )
- type ReportProduceExcel struct {
- Offset int
- Row int
- Title string //标题
- Excel *excelize.File
- SheetName string
- AlignCenterStyle int
- Content *model.ProduceBill
- BudgetAmount float64
- RealAmount float64
- }
- func (b *ReportProduceExcel) drawTitle() error {
- // 设置外边距
- marginLeft := excelize.PageMarginLeft(0.15)
- b.Excel.SetPageMargins(b.SheetName, marginLeft)
- b.Row++
- startCell := fmt.Sprintf("A%d", b.Row)
- err := b.Excel.MergeCell(b.SheetName, startCell, fmt.Sprintf("L%d", b.Row))
- if err != nil {
- return err
- }
- style, err := b.Excel.NewStyle(&excelize.Style{
- Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
- 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, b.Title)
- return nil
- }
- func (b *ReportProduceExcel) drawSubTitles() error {
- b.Row++
- styleLeft, err := b.Excel.NewStyle(&excelize.Style{
- Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
- Font: &excelize.Font{Size: 11}})
- if err != nil {
- return err
- }
- styleRight, err := b.Excel.NewStyle(&excelize.Style{
- Alignment: &excelize.Alignment{Horizontal: "right", Vertical: "center"},
- Font: &excelize.Font{Size: 11}})
- if err != nil {
- return err
- }
- var drawLeft = func(rowIndex int, value string) error {
- //左边1
- left1Cell := fmt.Sprintf("A%d", rowIndex)
- err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("E%d", rowIndex))
- if err != nil {
- return err
- }
- err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
- if err != nil {
- return err
- }
- b.Excel.SetCellValue(b.SheetName, left1Cell, value)
- return nil
- }
- var drawRight = func(rowIndex int, value string) error {
- right1Cell := fmt.Sprintf("F%d", rowIndex)
- err = b.Excel.MergeCell(b.SheetName, right1Cell, fmt.Sprintf("L%d", rowIndex))
- if err != nil {
- return err
- }
- err = b.Excel.SetCellStyle(b.SheetName, right1Cell, right1Cell, styleRight)
- if err != nil {
- return err
- }
- b.Excel.SetCellValue(b.SheetName, right1Cell, value)
- return nil
- }
- //第一行
- drawLeft(b.Row, "类别:"+b.Content.Type)
- drawRight(b.Row, "单号:"+b.Content.SerialNumber)
- b.Excel.SetRowHeight(b.SheetName, b.Row, 21)
- //第二行
- drawLeft(b.Row+1, "供应商名称:"+b.Content.Supplier)
- timeformat := b.Content.CreateTime.Local().Format("2006年01月02号 15:04:05")
- drawRight(b.Row+1, "下单时间:"+timeformat)
- b.Excel.SetRowHeight(b.SheetName, b.Row+1, 21)
- //第三行
- drawLeft(b.Row+2, "产品名称:"+b.Content.ProductName)
- status := ""
- if b.Content.Status == "complete" {
- status = fmt.Sprintf("已完成(%d)", b.Content.ConfirmCount)
- }
- if b.Content.Status == "created" {
- status = "进行中"
- }
- drawRight(b.Row+2, "状态:"+status)
- b.Excel.SetRowHeight(b.SheetName, b.Row+2, 21)
- return nil
- }
- func (b *ReportProduceExcel) drawTableTitle() error {
- b.Row += 3
- //A加工项目 B规格(克) 尺寸C-D 数量E 单价F-G 交货时间H 备注I
- 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)
- }
- var drawCol2 = func(prefix string, value1 string, value2 string) error {
- left1Cell := fmt.Sprintf("%s%d", prefix, b.Row)
- left2Cell := fmt.Sprintf("%s%d", prefix, b.Row+1)
- err := b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
- if err != nil {
- return err
- }
- b.Excel.SetCellValue(b.SheetName, left1Cell, value1)
- b.Excel.SetCellValue(b.SheetName, left2Cell, value2)
- return nil
- }
- drawCol("A", "加工项目")
- drawCol("B", "规格")
- drawCol("C", "纸张")
- drawCol("D", "来纸尺寸")
- drawCol("E", "印刷尺寸")
- drawCol("F", "下单数量")
- drawCol("G", "完成数量")
- drawCol2("H", "单价", "元/张")
- drawCol("I", "预算金额")
- drawCol("J", "实际金额")
- drawCol("K", "交货时间")
- drawCol("L", "备注")
- return nil
- }
- func (b *ReportProduceExcel) drawTableContent() error {
- b.Row += 2
- var DrawRow = func(rowIndex int, values ...string) {
- charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"}
- 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)
- b.Excel.SetRowHeight(b.SheetName, rowIndex, 21)
- }
- }
- produces := b.Content.Produces
- if len(produces) > 0 {
- for _, produce := range produces {
- realCount := ""
- price := produce.Price
- priceStr := fmt.Sprintf("%.3f", price)
- b.FormatToEmpty(&priceStr)
- // 预算金额
- orderPrice := fmt.Sprintf("%.3f", produce.Price*float64(produce.OrderCount))
- b.FormatToEmpty(&orderPrice)
- // 实际金额
- realPrice := ""
- if b.Content.Status == "complete" {
- // 实际完成数
- realCount = fmt.Sprintf("%d", produce.ConfirmCount)
- b.FormatToEmpty(&realCount)
- realPrice = fmt.Sprintf("%.3f", produce.Price*float64(produce.ConfirmCount))
- }
- deliveryTime := produce.DeliveryTime.Local().Format("2006-01-02")
- DrawRow(b.Row, produce.Name, produce.Norm, produce.Paper, produce.PaperSize, produce.PrintSize, fmt.Sprintf("%d", produce.OrderCount), realCount, priceStr, orderPrice, realPrice, deliveryTime, produce.Remark)
- b.Row++
- b.BudgetAmount += produce.Price * float64(produce.OrderCount)
- b.RealAmount += produce.Price * float64(produce.ConfirmCount)
- }
- }
- return nil
- }
- func (b *ReportProduceExcel) Draws() {
- b.drawTitle()
- b.drawSubTitles()
- b.drawTableTitle()
- b.drawTableContent()
- }
- func NewReportProduceExcel(f *excelize.File) *ReportProduceExcel {
- 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"},
- Border: border,
- })
- b := &ReportProduceExcel{
- Title: "中鱼互动加工单",
- SheetName: "Sheet1",
- Excel: f,
- Offset: 0,
- AlignCenterStyle: styleLeft,
- }
- f.SetColWidth(b.SheetName, "A", "A", 12)
- f.SetColWidth(b.SheetName, "B", "K", 9.5)
- f.SetColWidth(b.SheetName, "L", "L", 10)
- f.SetPageMargins(b.SheetName, excelize.PageMarginTop(0), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
- return b
- }
- func (b *ReportProduceExcel) FormatToEmpty(str *string) {
- if *str == "0" || *str == "0.000" {
- *str = ""
- }
- }
|