report-excel.go 8.3 KB

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