report-produce-excel.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. BudgetAmount float64
  16. RealAmount float64
  17. }
  18. func (b *ReportProduceExcel) drawTitle() error {
  19. // 设置外边距
  20. marginLeft := excelize.PageMarginLeft(0.15)
  21. b.Excel.SetPageMargins(b.SheetName, marginLeft)
  22. b.Row++
  23. startCell := fmt.Sprintf("A%d", b.Row)
  24. err := b.Excel.MergeCell(b.SheetName, startCell, fmt.Sprintf("L%d", b.Row))
  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, b.Row, 23)
  39. b.Excel.SetCellValue(b.SheetName, startCell, b.Title)
  40. return nil
  41. }
  42. func (b *ReportProduceExcel) drawSubTitles() error {
  43. b.Row++
  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(b.Row, "类别:"+b.Content.Type)
  85. drawRight(b.Row, "单号:"+b.Content.SerialNumber)
  86. b.Excel.SetRowHeight(b.SheetName, b.Row, 21)
  87. //第二行
  88. drawLeft(b.Row+1, "供应商名称:"+b.Content.Supplier)
  89. timeformat := b.Content.CreateTime.Local().Format("2006年01月02号 15:04:05")
  90. drawRight(b.Row+1, "下单时间:"+timeformat)
  91. b.Excel.SetRowHeight(b.SheetName, b.Row+1, 21)
  92. //第三行
  93. drawLeft(b.Row+2, "产品名称:"+b.Content.ProductName)
  94. status := ""
  95. if b.Content.Status == "complete" {
  96. status = "已完成"
  97. }
  98. if b.Content.Status == "created" {
  99. status = "进行中"
  100. }
  101. drawRight(b.Row+2, "状态:"+status)
  102. b.Excel.SetRowHeight(b.SheetName, b.Row+2, 21)
  103. return nil
  104. }
  105. func (b *ReportProduceExcel) drawTableTitle() error {
  106. b.Row += 3
  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, b.Row)
  110. left2Cell := fmt.Sprintf("%s%d", prefix, b.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, b.Row)
  123. left2Cell := fmt.Sprintf("%s%d", prefix, b.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. b.Row += 2
  148. var DrawRow = func(rowIndex int, values ...string) {
  149. charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"}
  150. for i, c := range charas {
  151. v := ""
  152. if i < len(values) {
  153. v = values[i]
  154. }
  155. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v)
  156. val2Cel := fmt.Sprintf("%s%d", c, rowIndex)
  157. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  158. b.Excel.SetRowHeight(b.SheetName, rowIndex, 21)
  159. }
  160. }
  161. produces := b.Content.Produces
  162. if len(produces) > 0 {
  163. for _, produce := range produces {
  164. realCount := ""
  165. price := produce.OrderPrice
  166. priceStr := fmt.Sprintf("%.3f", price)
  167. b.FormatToEmpty(&priceStr)
  168. // 预算金额
  169. budgetAmount := fmt.Sprintf("%.3f", produce.OrderPrice*float64(produce.OrderCount))
  170. b.FormatToEmpty(&budgetAmount)
  171. // 实际金额
  172. realPrice := ""
  173. // 实际完成数
  174. realCount = fmt.Sprintf("%d", produce.ConfirmCount)
  175. b.FormatToEmpty(&realCount)
  176. realPrice = fmt.Sprintf("%.3f", produce.OrderPrice*float64(produce.ConfirmCount))
  177. b.FormatToEmpty(&realPrice)
  178. deliveryTime := produce.DeliveryTime.Local().Format("2006-01-02")
  179. DrawRow(b.Row, produce.Name, produce.Norm, produce.Paper, produce.PaperSize, produce.PrintSize, fmt.Sprintf("%d", produce.OrderCount), realCount, priceStr, budgetAmount, realPrice, deliveryTime, produce.Remark)
  180. b.Row++
  181. b.BudgetAmount += produce.OrderPrice * float64(produce.OrderCount)
  182. b.RealAmount += produce.OrderPrice * float64(produce.ConfirmCount)
  183. }
  184. }
  185. return nil
  186. }
  187. func (b *ReportProduceExcel) Draws() {
  188. b.drawTitle()
  189. b.drawSubTitles()
  190. b.drawTableTitle()
  191. b.drawTableContent()
  192. }
  193. func NewReportProduceExcel(f *excelize.File) *ReportProduceExcel {
  194. border := []excelize.Border{
  195. {Type: "top", Style: 1, Color: "000000"},
  196. {Type: "left", Style: 1, Color: "000000"},
  197. {Type: "right", Style: 1, Color: "000000"},
  198. {Type: "bottom", Style: 1, Color: "000000"},
  199. }
  200. styleLeft, _ := f.NewStyle(&excelize.Style{
  201. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  202. Border: border,
  203. })
  204. b := &ReportProduceExcel{
  205. Title: "中鱼互动加工单",
  206. SheetName: "Sheet1",
  207. Excel: f,
  208. Offset: 0,
  209. AlignCenterStyle: styleLeft,
  210. }
  211. f.SetColWidth(b.SheetName, "A", "A", 12)
  212. f.SetColWidth(b.SheetName, "B", "K", 9.5)
  213. f.SetColWidth(b.SheetName, "L", "L", 10)
  214. f.SetPageMargins(b.SheetName, excelize.PageMarginTop(0), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
  215. return b
  216. }
  217. func (b *ReportProduceExcel) FormatToEmpty(str *string) {
  218. if *str == "0" || *str == "0.000" {
  219. *str = ""
  220. }
  221. }