report-excel.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. drawCol("J", "对账状态")
  101. // b.Excel.SetRowHeight(b.SheetName, b.Row, 35)
  102. return nil
  103. }
  104. func (b *ReportExcel) drawTableContent() error {
  105. b.Row += 2
  106. var DrawRow = func(rowIndex int, values ...string) {
  107. charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"}
  108. for i, c := range charas {
  109. v := ""
  110. if i < len(values) {
  111. v = values[i]
  112. }
  113. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v)
  114. val2Cel := fmt.Sprintf("%s%d", c, rowIndex)
  115. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  116. b.Excel.SetRowHeight(b.SheetName, rowIndex, 46)
  117. }
  118. }
  119. lists := b.Content
  120. if len(lists) > 0 {
  121. index := 0
  122. var totalPrice float64 = 0.00
  123. var totalCount int = 0
  124. for _, list := range lists {
  125. data, err := json.Marshal(list)
  126. if err != nil {
  127. log.Error(err)
  128. continue
  129. }
  130. if list["billType"] == "purchase" {
  131. purchase := model.PurchaseBill{}
  132. err = json.Unmarshal(data, &purchase)
  133. if err != nil {
  134. log.Error(err)
  135. panic(err)
  136. }
  137. record := "否"
  138. if *purchase.IsRecord {
  139. record = "是"
  140. }
  141. for _, paper := range purchase.Paper {
  142. index++
  143. completeTime := purchase.CompleteTime.Local().Format("2006-01-02")
  144. // 实际完成数
  145. realCount := fmt.Sprintf("%d", paper.ConfirmCount)
  146. b.FormatToEmpty(&realCount)
  147. // 实际金额
  148. realPrice := fmt.Sprintf("%.3f", paper.OrderPrice*float64(paper.ConfirmCount))
  149. b.FormatToEmpty(&realPrice)
  150. 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, record)
  151. totalPrice += paper.OrderPrice * float64(paper.ConfirmCount)
  152. totalCount += paper.ConfirmCount
  153. b.Row++
  154. }
  155. }
  156. if list["billType"] == "produce" {
  157. produce := model.ProduceBill{}
  158. err = json.Unmarshal(data, &produce)
  159. if err != nil {
  160. log.Error(err)
  161. panic(err)
  162. }
  163. record := "否"
  164. if *produce.IsRecord {
  165. record = "是"
  166. }
  167. for _, pproduce := range produce.Produces {
  168. index++
  169. completeTime := produce.CompleteTime.Local().Format("2006-01-02")
  170. // 实际完成数
  171. realCount := fmt.Sprintf("%d", pproduce.ConfirmCount)
  172. b.FormatToEmpty(&realCount)
  173. // 实际金额
  174. realPrice := fmt.Sprintf("%.3f", pproduce.OrderPrice*float64(pproduce.ConfirmCount))
  175. b.FormatToEmpty(&realPrice)
  176. 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, record)
  177. totalPrice += pproduce.OrderPrice * float64(pproduce.ConfirmCount)
  178. totalCount += pproduce.ConfirmCount
  179. b.Row++
  180. }
  181. }
  182. if list["billType"] == "product" {
  183. product := model.ProductBill{}
  184. err = json.Unmarshal(data, &product)
  185. if err != nil {
  186. log.Error(err)
  187. panic(err)
  188. }
  189. record := "否"
  190. if *product.IsRecord {
  191. record = "是"
  192. }
  193. for _, pproduct := range product.Products {
  194. index++
  195. completeTime := product.CompleteTime.Local().Format("2006-01-02")
  196. // 实际完成数
  197. realCount := fmt.Sprintf("%d", pproduct.ConfirmCount)
  198. b.FormatToEmpty(&realCount)
  199. // 实际金额
  200. realPrice := fmt.Sprintf("%.3f", pproduct.OrderPrice*float64(pproduct.ConfirmCount))
  201. b.FormatToEmpty(&realPrice)
  202. 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, record)
  203. totalPrice += pproduct.OrderPrice * float64(pproduct.ConfirmCount)
  204. totalCount += pproduct.ConfirmCount
  205. b.Row++
  206. }
  207. }
  208. }
  209. DrawRow(b.Row, "", "", "", "", "", "", "", "", "")
  210. startCell := fmt.Sprintf("%s%d", "A", b.Row)
  211. endCell := fmt.Sprintf("%s%d", "B", b.Row)
  212. FCell := fmt.Sprintf("%s%d", "F", b.Row)
  213. HCell := fmt.Sprintf("%s%d", "H", b.Row)
  214. b.Excel.MergeCell(b.SheetName, startCell, endCell)
  215. b.Excel.SetCellValue(b.SheetName, startCell, "合计")
  216. // 数量汇总
  217. b.Excel.SetCellValue(b.SheetName, FCell, totalCount)
  218. // 金额汇总
  219. b.Excel.SetCellValue(b.SheetName, HCell, fmt.Sprintf("%.3f", totalPrice))
  220. }
  221. return nil
  222. }
  223. func (b *ReportExcel) Draws() {
  224. b.drawTitle()
  225. b.drawSubTitles()
  226. b.drawTableTitle()
  227. b.drawTableContent()
  228. }
  229. func NewReportExcel(f *excelize.File) *ReportExcel {
  230. border := []excelize.Border{
  231. {Type: "top", Style: 1, Color: "000000"},
  232. {Type: "left", Style: 1, Color: "000000"},
  233. {Type: "right", Style: 1, Color: "000000"},
  234. {Type: "bottom", Style: 1, Color: "000000"},
  235. }
  236. styleLeft, _ := f.NewStyle(&excelize.Style{
  237. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  238. Border: border,
  239. })
  240. b := &ReportExcel{
  241. Title: "供应商统计报表",
  242. SupplierName: "",
  243. SheetName: "Sheet1",
  244. Excel: f,
  245. AlignCenterStyle: styleLeft,
  246. }
  247. f.SetPageMargins(b.SheetName, excelize.PageMarginTop(0), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
  248. return b
  249. }
  250. func (b *ReportExcel) FormatToEmpty(str *string) {
  251. if *str == "0" || *str == "0.000" {
  252. *str = ""
  253. }
  254. }