report-produce-excel.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "fmt"
  5. "github.com/xuri/excelize/v2"
  6. )
  7. type ReportProduceExcel struct {
  8. Offset int
  9. Row int
  10. Title string //标题
  11. Excel *excelize.File
  12. SheetName string
  13. AlignCenterStyle int
  14. Content *model.ProduceBill
  15. BudgetCount float64
  16. RealCount float64
  17. }
  18. func (b *ReportProduceExcel) drawTitle() error {
  19. // 设置外边距
  20. marginLeft := excelize.PageMarginLeft(0.15)
  21. b.Excel.SetPageMargins(b.SheetName, marginLeft)
  22. tileIndex := b.Offset + 1
  23. startCell := fmt.Sprintf("A%d", tileIndex)
  24. err := b.Excel.MergeCell(b.SheetName, startCell, fmt.Sprintf("L%d", tileIndex))
  25. if err != nil {
  26. return err
  27. }
  28. style, err := b.Excel.NewStyle(&excelize.Style{
  29. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  30. Font: &excelize.Font{Bold: true, Size: 18}})
  31. if err != nil {
  32. return err
  33. }
  34. err = b.Excel.SetCellStyle(b.SheetName, startCell, startCell, style)
  35. if err != nil {
  36. return err
  37. }
  38. b.Excel.SetRowHeight(b.SheetName, tileIndex, 23)
  39. b.Excel.SetCellValue(b.SheetName, startCell, b.Title)
  40. return nil
  41. }
  42. func (b *ReportProduceExcel) drawSubTitles() error {
  43. row := b.Offset + 2
  44. styleLeft, err := b.Excel.NewStyle(&excelize.Style{
  45. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  46. Font: &excelize.Font{Size: 11}})
  47. if err != nil {
  48. return err
  49. }
  50. styleRight, err := b.Excel.NewStyle(&excelize.Style{
  51. Alignment: &excelize.Alignment{Horizontal: "right", Vertical: "center"},
  52. Font: &excelize.Font{Size: 11}})
  53. if err != nil {
  54. return err
  55. }
  56. var drawLeft = func(rowIndex int, value string) error {
  57. //左边1
  58. left1Cell := fmt.Sprintf("A%d", rowIndex)
  59. err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("E%d", rowIndex))
  60. if err != nil {
  61. return err
  62. }
  63. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  64. if err != nil {
  65. return err
  66. }
  67. b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  68. return nil
  69. }
  70. var drawRight = func(rowIndex int, value string) error {
  71. right1Cell := fmt.Sprintf("F%d", rowIndex)
  72. err = b.Excel.MergeCell(b.SheetName, right1Cell, fmt.Sprintf("L%d", rowIndex))
  73. if err != nil {
  74. return err
  75. }
  76. err = b.Excel.SetCellStyle(b.SheetName, right1Cell, right1Cell, styleRight)
  77. if err != nil {
  78. return err
  79. }
  80. b.Excel.SetCellValue(b.SheetName, right1Cell, value)
  81. return nil
  82. }
  83. //第一行
  84. drawLeft(row, "类别:"+b.Content.Type)
  85. drawRight(row, "单号:"+b.Content.SerialNumber)
  86. b.Excel.SetRowHeight(b.SheetName, row, 21)
  87. //第二行
  88. drawLeft(row+1, "供应商名称:"+b.Content.Supplier)
  89. timeformat := b.Content.CreateTime.Local().Format("2006年01月02号 15:04:05")
  90. drawRight(row+1, "下单时间:"+timeformat)
  91. b.Excel.SetRowHeight(b.SheetName, row+1, 21)
  92. //第三行
  93. drawLeft(row+2, "产品名称:"+b.Content.ProductName)
  94. status := ""
  95. if b.Content.Status == "complete" {
  96. status = fmt.Sprintf("已完成(%d)", b.Content.ConfirmCount)
  97. }
  98. if b.Content.Status == "created" {
  99. status = "进行中"
  100. }
  101. drawRight(row+2, "状态:"+status)
  102. b.Excel.SetRowHeight(b.SheetName, row+2, 21)
  103. return nil
  104. }
  105. func (b *ReportProduceExcel) drawTableTitle() error {
  106. row := b.Offset + 5
  107. //A加工项目 B规格(克) 尺寸C-D 数量E 单价F-G 交货时间H 备注I
  108. var drawCol = func(prefix string, value string) error {
  109. left1Cell := fmt.Sprintf("%s%d", prefix, row)
  110. left2Cell := fmt.Sprintf("%s%d", prefix, row+1)
  111. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  112. if err != nil {
  113. return err
  114. }
  115. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  116. if err != nil {
  117. return err
  118. }
  119. return b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  120. }
  121. var drawCol2 = func(prefix string, value1 string, value2 string) error {
  122. left1Cell := fmt.Sprintf("%s%d", prefix, row)
  123. left2Cell := fmt.Sprintf("%s%d", prefix, row+1)
  124. err := b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  125. if err != nil {
  126. return err
  127. }
  128. b.Excel.SetCellValue(b.SheetName, left1Cell, value1)
  129. b.Excel.SetCellValue(b.SheetName, left2Cell, value2)
  130. return nil
  131. }
  132. drawCol("A", "加工项目")
  133. drawCol("B", "规格")
  134. drawCol("C", "纸张")
  135. drawCol("D", "来纸尺寸")
  136. drawCol("E", "印刷尺寸")
  137. drawCol("F", "下单数量")
  138. drawCol("G", "完成数量")
  139. drawCol2("H", "单价", "元/张")
  140. drawCol("I", "预算金额")
  141. drawCol("J", "实际金额")
  142. drawCol("K", "交货时间")
  143. drawCol("L", "备注")
  144. return nil
  145. }
  146. func (b *ReportProduceExcel) drawTableContent() error {
  147. row := b.Offset + 7
  148. b.Row = row
  149. var DrawRow = func(rowIndex int, values ...string) {
  150. charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"}
  151. for i, c := range charas {
  152. v := ""
  153. if i < len(values) {
  154. v = values[i]
  155. }
  156. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v)
  157. val2Cel := fmt.Sprintf("%s%d", c, rowIndex)
  158. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  159. b.Excel.SetRowHeight(b.SheetName, rowIndex, 21)
  160. }
  161. }
  162. produces := b.Content.Produces
  163. if len(produces) > 0 {
  164. startRow := b.Row
  165. endRow := b.Row
  166. for _, produce := range produces {
  167. confirmCount := "-"
  168. realAmount := "-"
  169. price := produce.Price
  170. priceStr := fmt.Sprintf("%.3f", price)
  171. b.FormatToEmpty(&priceStr)
  172. // 预算金额
  173. budgetAmount := ""
  174. // 实际完成数
  175. confirmCount = fmt.Sprintf("%d", produce.ConfirmCount)
  176. b.FormatToEmpty(&confirmCount)
  177. // 实际金额
  178. realAmount = ""
  179. deliveryTime := produce.DeliveryTime.Local().Format("2006-01-02")
  180. DrawRow(row, produce.Name, produce.Norm, produce.Paper, produce.PaperSize, produce.PrintSize, fmt.Sprintf("%d", produce.OrderCount), confirmCount, priceStr, budgetAmount, realAmount, deliveryTime, produce.Remark)
  181. row++
  182. b.Row++
  183. }
  184. // 多个工序 合并预算价格、实际价格
  185. b.Excel.MergeCell(b.SheetName, fmt.Sprintf("I%d", startRow), fmt.Sprintf("I%d", endRow))
  186. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("I%d", startRow), b.Content.BudgetAmount)
  187. b.Excel.MergeCell(b.SheetName, fmt.Sprintf("J%d", startRow), fmt.Sprintf("J%d", endRow))
  188. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("J%d", startRow), b.Content.RealAmount)
  189. b.BudgetCount += b.Content.BudgetAmount
  190. b.RealCount += b.Content.RealAmount
  191. }
  192. return nil
  193. }
  194. func (b *ReportProduceExcel) Draws() {
  195. b.drawTitle()
  196. b.drawSubTitles()
  197. b.drawTableTitle()
  198. b.drawTableContent()
  199. }
  200. func (b *ReportProduceExcel) Count() {
  201. row := b.Row + 1
  202. startCell := fmt.Sprintf("%s%d", "A", row)
  203. endCell := fmt.Sprintf("%s%d", "H", row)
  204. b.Excel.MergeCell(b.SheetName, startCell, endCell)
  205. b.Excel.SetCellStyle(b.SheetName, startCell, endCell, b.AlignCenterStyle)
  206. b.Excel.SetCellValue(b.SheetName, startCell, "汇总金额")
  207. // 预算金额汇总
  208. budgetCountCell := fmt.Sprintf("%s%d", "I", row)
  209. b.Excel.SetCellValue(b.SheetName, budgetCountCell, b.BudgetCount)
  210. b.Excel.SetCellStyle(b.SheetName, budgetCountCell, budgetCountCell, b.AlignCenterStyle)
  211. // 实际金额汇总
  212. RealCountCell := fmt.Sprintf("%s%d", "J", row)
  213. b.Excel.SetCellValue(b.SheetName, RealCountCell, b.RealCount)
  214. b.Excel.SetCellStyle(b.SheetName, RealCountCell, RealCountCell, b.AlignCenterStyle)
  215. b.Excel.SetRowHeight(b.SheetName, row, 21)
  216. }
  217. func NewReportProduceExcel(f *excelize.File) *ReportProduceExcel {
  218. border := []excelize.Border{
  219. {Type: "top", Style: 1, Color: "000000"},
  220. {Type: "left", Style: 1, Color: "000000"},
  221. {Type: "right", Style: 1, Color: "000000"},
  222. {Type: "bottom", Style: 1, Color: "000000"},
  223. }
  224. styleLeft, _ := f.NewStyle(&excelize.Style{
  225. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  226. Border: border,
  227. })
  228. b := &ReportProduceExcel{
  229. Title: "中鱼互动加工单",
  230. SheetName: "Sheet1",
  231. Excel: f,
  232. Offset: 0,
  233. AlignCenterStyle: styleLeft,
  234. }
  235. f.SetColWidth(b.SheetName, "A", "A", 12)
  236. f.SetColWidth(b.SheetName, "B", "K", 9.5)
  237. f.SetColWidth(b.SheetName, "L", "L", 10)
  238. f.SetPageMargins(b.SheetName, excelize.PageMarginTop(0), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
  239. return b
  240. }
  241. func (b *ReportProduceExcel) FormatToEmpty(str *string) {
  242. if *str == "0" || *str == "0.000" {
  243. *str = ""
  244. }
  245. }