report-excel.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "box-cost/log"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/xuri/excelize/v2"
  8. )
  9. type ReportExcel struct {
  10. Row int
  11. Title string //标题
  12. SupplierName string //供应商名
  13. TimeRange []string
  14. Excel *excelize.File
  15. SheetName string
  16. AlignCenterStyle int
  17. Content []map[string]interface{}
  18. }
  19. func (b *ReportExcel) drawTitle() error {
  20. b.Row++
  21. // 设置外边距
  22. startCell := fmt.Sprintf("A%d", b.Row)
  23. endCell := fmt.Sprintf("I%d", b.Row)
  24. b.Excel.SetColWidth(b.SheetName, "A", "A", 10)
  25. b.Excel.SetColWidth(b.SheetName, "B", "B", 16)
  26. b.Excel.SetColWidth(b.SheetName, "C", "D", 20)
  27. b.Excel.SetColWidth(b.SheetName, "E", "H", 18)
  28. b.Excel.SetColWidth(b.SheetName, "I", "I", 35)
  29. err := b.Excel.MergeCell(b.SheetName, startCell, endCell)
  30. if err != nil {
  31. return err
  32. }
  33. style, err := b.Excel.NewStyle(&excelize.Style{
  34. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  35. Font: &excelize.Font{Bold: true, Size: 18}})
  36. if err != nil {
  37. return err
  38. }
  39. err = b.Excel.SetCellStyle(b.SheetName, startCell, startCell, style)
  40. if err != nil {
  41. return err
  42. }
  43. b.Excel.SetRowHeight(b.SheetName, b.Row, 35)
  44. b.Excel.SetCellValue(b.SheetName, startCell, b.Title)
  45. return nil
  46. }
  47. func (b *ReportExcel) drawSubTitles() error {
  48. b.Row++
  49. styleLeft, err := b.Excel.NewStyle(&excelize.Style{
  50. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center"},
  51. Font: &excelize.Font{Size: 11}})
  52. if err != nil {
  53. return err
  54. }
  55. var drawLeft = func(rowIndex int, value string) error {
  56. left1Cell := fmt.Sprintf("A%d", rowIndex)
  57. err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("I%d", rowIndex))
  58. if err != nil {
  59. return err
  60. }
  61. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  62. if err != nil {
  63. return err
  64. }
  65. b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  66. return nil
  67. }
  68. timeString := ""
  69. if len(b.TimeRange) == 2 {
  70. timeString = fmt.Sprintf(" ( %s~%s ) ", b.TimeRange[0], b.TimeRange[1])
  71. }
  72. drawLeft(b.Row, "单位名称:"+b.SupplierName+timeString)
  73. b.Excel.SetRowHeight(b.SheetName, b.Row, 35)
  74. return nil
  75. }
  76. func (b *ReportExcel) drawTableTitle() error {
  77. b.Row++
  78. var drawCol = func(prefix string, value string) error {
  79. left1Cell := fmt.Sprintf("%s%d", prefix, b.Row)
  80. left2Cell := fmt.Sprintf("%s%d", prefix, b.Row+1)
  81. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  82. if err != nil {
  83. return err
  84. }
  85. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  86. if err != nil {
  87. return err
  88. }
  89. return b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  90. }
  91. drawCol("A", "序号")
  92. drawCol("B", "日期")
  93. drawCol("C", "采购或加工项目")
  94. drawCol("D", "规格")
  95. drawCol("E", "尺寸")
  96. drawCol("F", "数量")
  97. drawCol("G", "单价")
  98. drawCol("H", "金额")
  99. drawCol("I", "产品名称")
  100. // b.Excel.SetRowHeight(b.SheetName, b.Row, 35)
  101. return nil
  102. }
  103. func (b *ReportExcel) drawTableContent() error {
  104. b.Row += 2
  105. var DrawRow = func(rowIndex int, values ...string) {
  106. charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I"}
  107. for i, c := range charas {
  108. v := ""
  109. if i < len(values) {
  110. v = values[i]
  111. }
  112. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v)
  113. val2Cel := fmt.Sprintf("%s%d", c, rowIndex)
  114. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  115. b.Excel.SetRowHeight(b.SheetName, rowIndex, 46)
  116. }
  117. }
  118. lists := b.Content
  119. if len(lists) > 0 {
  120. index := 0
  121. var totalPrice float64 = 0.00
  122. var totalCount int = 0
  123. for _, list := range lists {
  124. data, err := json.Marshal(list)
  125. if err != nil {
  126. log.Error(err)
  127. continue
  128. }
  129. if list["billType"] == "purchase" {
  130. purchase := model.PurchaseBill{}
  131. err = json.Unmarshal(data, &purchase)
  132. if err != nil {
  133. log.Error(err)
  134. continue
  135. }
  136. for _, paper := range purchase.Paper {
  137. index++
  138. completeTime := purchase.CompleteTime.Local().Format("2006-01-02")
  139. // 实际完成数
  140. realCount := fmt.Sprintf("%d", paper.ConfirmCount)
  141. b.FormatToEmpty(&realCount)
  142. // 实际金额
  143. realPrice := fmt.Sprintf("%.3f", paper.OrderPrice*float64(paper.ConfirmCount))
  144. b.FormatToEmpty(&realPrice)
  145. DrawRow(b.Row, fmt.Sprintf("%d", index), completeTime, paper.Name, paper.Norm, fmt.Sprintf("%s*%s", paper.Height, paper.Width), fmt.Sprintf("%d", paper.ConfirmCount), fmt.Sprintf("%.3f", paper.OrderPrice), realPrice, purchase.ProductName)
  146. totalPrice += paper.OrderPrice * float64(paper.ConfirmCount)
  147. totalCount += paper.ConfirmCount
  148. b.Row++
  149. }
  150. }
  151. if list["billType"] == "produce" {
  152. produce := model.ProduceBill{}
  153. err = json.Unmarshal(data, &produce)
  154. if err != nil {
  155. log.Error(err)
  156. continue
  157. }
  158. for _, pproduce := range produce.Produces {
  159. index++
  160. completeTime := produce.CompleteTime.Local().Format("2006-01-02")
  161. // 实际完成数
  162. realCount := fmt.Sprintf("%d", pproduce.ConfirmCount)
  163. b.FormatToEmpty(&realCount)
  164. // 实际金额
  165. realPrice := fmt.Sprintf("%.3f", pproduce.OrderPrice*float64(pproduce.ConfirmCount))
  166. b.FormatToEmpty(&realPrice)
  167. DrawRow(b.Row, fmt.Sprintf("%d", index), completeTime, pproduce.Name, pproduce.Norm, pproduce.PrintSize, fmt.Sprintf("%d", pproduce.ConfirmCount), fmt.Sprintf("%.3f", pproduce.OrderPrice), realPrice, produce.ProductName)
  168. totalPrice += pproduce.OrderPrice * float64(pproduce.ConfirmCount)
  169. totalCount += pproduce.ConfirmCount
  170. b.Row++
  171. }
  172. }
  173. if list["billType"] == "product" {
  174. product := model.ProductBill{}
  175. err = json.Unmarshal(data, &product)
  176. if err != nil {
  177. log.Error(err)
  178. continue
  179. }
  180. for _, pproduct := range product.Products {
  181. index++
  182. completeTime := product.CompleteTime.Local().Format("2006-01-02")
  183. // 实际完成数
  184. realCount := fmt.Sprintf("%d", pproduct.ConfirmCount)
  185. b.FormatToEmpty(&realCount)
  186. // 实际金额
  187. realPrice := fmt.Sprintf("%.3f", pproduct.OrderPrice*float64(pproduct.ConfirmCount))
  188. b.FormatToEmpty(&realPrice)
  189. DrawRow(b.Row, fmt.Sprintf("%d", index), completeTime, pproduct.Name, pproduct.Norm, "-", fmt.Sprintf("%d", pproduct.ConfirmCount), fmt.Sprintf("%.3f", pproduct.OrderPrice), realPrice, product.ProductName)
  190. totalPrice += pproduct.OrderPrice * float64(pproduct.ConfirmCount)
  191. totalCount += pproduct.ConfirmCount
  192. b.Row++
  193. }
  194. }
  195. }
  196. DrawRow(b.Row, "", "", "", "", "", "", "", "", "")
  197. startCell := fmt.Sprintf("%s%d", "A", b.Row)
  198. endCell := fmt.Sprintf("%s%d", "B", b.Row)
  199. FCell := fmt.Sprintf("%s%d", "F", b.Row)
  200. HCell := fmt.Sprintf("%s%d", "H", b.Row)
  201. b.Excel.MergeCell(b.SheetName, startCell, endCell)
  202. b.Excel.SetCellValue(b.SheetName, startCell, "合计")
  203. // 数量汇总
  204. b.Excel.SetCellValue(b.SheetName, FCell, totalCount)
  205. // 金额汇总
  206. b.Excel.SetCellValue(b.SheetName, HCell, fmt.Sprintf("%.3f", totalPrice))
  207. }
  208. return nil
  209. }
  210. func (b *ReportExcel) Draws() {
  211. b.drawTitle()
  212. b.drawSubTitles()
  213. b.drawTableTitle()
  214. b.drawTableContent()
  215. }
  216. func NewReportExcel(f *excelize.File) *ReportExcel {
  217. border := []excelize.Border{
  218. {Type: "top", Style: 1, Color: "000000"},
  219. {Type: "left", Style: 1, Color: "000000"},
  220. {Type: "right", Style: 1, Color: "000000"},
  221. {Type: "bottom", Style: 1, Color: "000000"},
  222. }
  223. styleLeft, _ := f.NewStyle(&excelize.Style{
  224. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  225. Border: border,
  226. })
  227. b := &ReportExcel{
  228. Title: "供应商统计报表",
  229. SupplierName: "",
  230. SheetName: "Sheet1",
  231. Excel: f,
  232. AlignCenterStyle: styleLeft,
  233. }
  234. f.SetPageMargins(b.SheetName, excelize.PageMarginTop(0), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
  235. return b
  236. }
  237. func (b *ReportExcel) FormatToEmpty(str *string) {
  238. if *str == "0" || *str == "0.000" {
  239. *str = ""
  240. }
  241. }