report-produce-excel.go 8.8 KB

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