plan-cost-excel.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "fmt"
  5. "github.com/xuri/excelize/v2"
  6. )
  7. // 生产成本表
  8. type PlanCostExcel struct {
  9. Offset int
  10. Title string //标题
  11. Excel *excelize.File
  12. SheetName string
  13. AlignCenterStyle int
  14. Content *model.ProductPlan
  15. }
  16. func (b *PlanCostExcel) drawTitle() error {
  17. tileIndex := b.Offset + 1
  18. startCell := fmt.Sprintf("A%d", tileIndex)
  19. err := b.Excel.MergeCell(b.SheetName, startCell, fmt.Sprintf("O%d", tileIndex))
  20. if err != nil {
  21. return err
  22. }
  23. style, err := b.Excel.NewStyle(&excelize.Style{
  24. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  25. Font: &excelize.Font{Bold: true, Size: 18}})
  26. if err != nil {
  27. return err
  28. }
  29. err = b.Excel.SetCellStyle(b.SheetName, startCell, startCell, style)
  30. if err != nil {
  31. return err
  32. }
  33. b.Excel.SetRowHeight(b.SheetName, tileIndex, 23)
  34. b.Excel.SetCellValue(b.SheetName, startCell, b.Title)
  35. return nil
  36. }
  37. func (b *PlanCostExcel) drawTableTitle() error {
  38. row := b.Offset + 2
  39. //A采购项目 B规格(克) 尺寸C-D 数量E 单价F-G 交货时间H 备注I
  40. // A产品名称
  41. var drawCol = func(prefix string, value string) error {
  42. left1Cell := fmt.Sprintf("%s%d", prefix, row)
  43. left2Cell := fmt.Sprintf("%s%d", prefix, row+1)
  44. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  45. if err != nil {
  46. return err
  47. }
  48. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  49. if err != nil {
  50. return err
  51. }
  52. return b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  53. }
  54. // var drawCol1 = func(prefix string, value1 string, value2 string) error {
  55. // topCell := fmt.Sprintf("%s%d", prefix, row)
  56. // bottomCell := fmt.Sprintf("%s%d", prefix, row+1)
  57. // b.Excel.SetCellStyle(b.SheetName, topCell, topCell, b.AlignCenterStyle)
  58. // b.Excel.SetCellValue(b.SheetName, topCell, value1)
  59. // b.Excel.SetCellStyle(b.SheetName, bottomCell, bottomCell, b.AlignCenterStyle)
  60. // b.Excel.SetCellValue(b.SheetName, bottomCell, value2)
  61. // return nil
  62. // }
  63. var drawCol2 = func(prefix1 string, prefix2 string, value1 string, value2 string, value3 string) error {
  64. left1Cell := fmt.Sprintf("%s%d", prefix1, row)
  65. left2Cell := fmt.Sprintf("%s%d", prefix2, row)
  66. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  67. if err != nil {
  68. return err
  69. }
  70. if err != nil {
  71. fmt.Println(err)
  72. return err
  73. }
  74. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  75. if err != nil {
  76. return err
  77. }
  78. b.Excel.SetCellValue(b.SheetName, left1Cell, value1)
  79. val2Cel := fmt.Sprintf("%s%d", prefix1, row+1)
  80. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  81. b.Excel.SetCellValue(b.SheetName, val2Cel, value2)
  82. val3Cel := fmt.Sprintf("%s%d", prefix2, row+1)
  83. b.Excel.SetCellStyle(b.SheetName, val3Cel, val3Cel, b.AlignCenterStyle)
  84. b.Excel.SetCellValue(b.SheetName, val3Cel, value3)
  85. return nil
  86. }
  87. var drawCol3 = func(prefix1 string, prefix2 string, prefix3 string, value1 string, value2 string, value3 string, value4 string) error {
  88. left1Cell := fmt.Sprintf("%s%d", prefix1, row)
  89. // left2Cell := fmt.Sprintf("%s%d", prefix2, row)
  90. left3Cell := fmt.Sprintf("%s%d", prefix3, row)
  91. err := b.Excel.MergeCell(b.SheetName, left1Cell, left3Cell)
  92. if err != nil {
  93. return err
  94. }
  95. if err != nil {
  96. fmt.Println(err)
  97. return err
  98. }
  99. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left3Cell, b.AlignCenterStyle)
  100. if err != nil {
  101. return err
  102. }
  103. b.Excel.SetCellValue(b.SheetName, left1Cell, value1)
  104. val2Cel := fmt.Sprintf("%s%d", prefix1, row+1)
  105. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  106. b.Excel.SetCellValue(b.SheetName, val2Cel, value2)
  107. val3Cel := fmt.Sprintf("%s%d", prefix2, row+1)
  108. b.Excel.SetCellStyle(b.SheetName, val3Cel, val3Cel, b.AlignCenterStyle)
  109. b.Excel.SetCellValue(b.SheetName, val3Cel, value3)
  110. val4Cel := fmt.Sprintf("%s%d", prefix3, row+1)
  111. b.Excel.SetCellStyle(b.SheetName, val4Cel, val4Cel, b.AlignCenterStyle)
  112. b.Excel.SetCellValue(b.SheetName, val4Cel, value4)
  113. return nil
  114. }
  115. drawCol("A", "产品名称")
  116. drawCol("B", "产品配料名称")
  117. drawCol2("C", "D", "材料/工序", "材料", "工序")
  118. drawCol("E", "供应商名称")
  119. drawCol3("F", "G", "H", "规格", "厚度(纸克)", "长", "宽")
  120. drawCol("I", "单位")
  121. drawCol2("J", "K", "数量", "下单数量", "成品数量")
  122. drawCol2("L", "M", "单价(预算)", "单价1", "单价2")
  123. drawCol("N", "预算金额")
  124. drawCol("O", "汇总金额")
  125. return nil
  126. }
  127. func (b *PlanCostExcel) drawTableContent() error {
  128. row := b.Offset + 4
  129. var DrawRow = func(rowIndex int, values ...string) {
  130. charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "G", "K", "L", "M", "N", "O"}
  131. for i, c := range charas {
  132. v := ""
  133. if i < len(values) {
  134. v = values[i]
  135. }
  136. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v)
  137. val2Cel := fmt.Sprintf("%s%d", c, rowIndex)
  138. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  139. b.Excel.SetRowHeight(b.SheetName, rowIndex, 21)
  140. }
  141. }
  142. comps := b.Content.Pack.Components
  143. if len(comps) > 0 {
  144. row1 := row
  145. for _, comp := range comps {
  146. if len(comp.Mats) > 0 {
  147. startRow := row
  148. for _, mat := range comp.Mats {
  149. // 材料
  150. supplierName := ""
  151. if mat.Supplier.SupplierInfo != nil {
  152. supplierName = mat.Supplier.SupplierInfo.Name
  153. }
  154. heigth := fmt.Sprintf("%.2f", mat.MatInfo.Heigth)
  155. width := fmt.Sprintf("%.2f", mat.MatInfo.Width)
  156. orderCount := fmt.Sprintf("%.2f", mat.Supplier.OrderCount)
  157. price := fmt.Sprintf("%.2f", mat.MatInfo.Price)
  158. calcName := ""
  159. if mat.Supplier.Calc != nil {
  160. calcName = mat.Supplier.Calc.Name
  161. }
  162. orderPrice := fmt.Sprintf("%.2f", mat.Supplier.OrderPrice)
  163. orderRealPrice := fmt.Sprintf("%.2f", mat.Supplier.OrderRealPrice)
  164. DrawRow(row, b.Content.Name, "", mat.MatInfo.Name, "", supplierName, mat.MatInfo.Norm, heigth, width, mat.MatInfo.Unit, orderCount, "-", calcName, price, orderPrice, orderRealPrice)
  165. row++
  166. if len(mat.Crafts) > 0 {
  167. // 工序
  168. for _, craft := range mat.Crafts {
  169. craftSupplierName := ""
  170. if craft.Supplier.SupplierInfo != nil {
  171. craftSupplierName = craft.Supplier.SupplierInfo.Name
  172. }
  173. carftOrderCount := fmt.Sprintf("%.2f", craft.Supplier.OrderCount)
  174. carftCalcName := ""
  175. if craft.Supplier.Calc != nil {
  176. carftCalcName = craft.Supplier.Calc.Name
  177. }
  178. carftPrice := fmt.Sprintf("%.2f", craft.CraftInfo.Price)
  179. carftOrderPrice := fmt.Sprintf("%.2f", craft.Supplier.OrderPrice)
  180. carftOrderRealPrice := fmt.Sprintf("%.2f", craft.Supplier.OrderRealPrice)
  181. DrawRow(row, b.Content.Name, "", "", craft.CraftInfo.Name, craftSupplierName, craft.CraftInfo.Norm, "", "", craft.CraftInfo.Unit, carftOrderCount, "-", carftCalcName, carftPrice, carftOrderPrice, carftOrderRealPrice)
  182. row++
  183. }
  184. }
  185. }
  186. endRow := row - 1
  187. startCell := fmt.Sprintf("%s%d", "B", startRow)
  188. // left2Cell := fmt.Sprintf("%s%d", prefix2, row)
  189. endCell := fmt.Sprintf("%s%d", "B", endRow)
  190. b.Excel.MergeCell(b.SheetName, startCell, endCell)
  191. err := b.Excel.SetCellStyle(b.SheetName, startCell, endCell, b.AlignCenterStyle)
  192. if err != nil {
  193. return err
  194. }
  195. b.Excel.SetCellValue(b.SheetName, startCell, comp.Name)
  196. }
  197. }
  198. endRow := row - 1
  199. startCell := fmt.Sprintf("%s%d", "A", row1)
  200. endCell := fmt.Sprintf("%s%d", "A", endRow)
  201. b.Excel.MergeCell(b.SheetName, startCell, endCell)
  202. err := b.Excel.SetCellStyle(b.SheetName, startCell, endCell, b.AlignCenterStyle)
  203. if err != nil {
  204. return err
  205. }
  206. b.Excel.SetCellValue(b.SheetName, startCell, b.Content.Name)
  207. }
  208. return nil
  209. }
  210. func (b *PlanCostExcel) Draws() {
  211. b.drawTitle()
  212. b.drawTableTitle()
  213. b.drawTableContent()
  214. }
  215. func NewPlanCostExcel(f *excelize.File) *PlanCostExcel {
  216. border := []excelize.Border{
  217. {Type: "top", Style: 1, Color: "000000"},
  218. {Type: "left", Style: 1, Color: "000000"},
  219. {Type: "right", Style: 1, Color: "000000"},
  220. {Type: "bottom", Style: 1, Color: "000000"},
  221. }
  222. styleLeft, _ := f.NewStyle(&excelize.Style{
  223. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  224. Border: border,
  225. Font: &excelize.Font{Size: 10},
  226. })
  227. b := &PlanCostExcel{
  228. Title: "生产成本表",
  229. SheetName: "Sheet1",
  230. Excel: f,
  231. Offset: 0,
  232. AlignCenterStyle: styleLeft,
  233. }
  234. f.SetColWidth(b.SheetName, "A", "A", 17)
  235. f.SetColWidth(b.SheetName, "B", "B", 13)
  236. f.SetColWidth(b.SheetName, "C", "E", 12)
  237. f.SetColWidth(b.SheetName, "F", "O", 9.5)
  238. f.SetPageMargins(b.SheetName, excelize.PageMarginTop(0), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
  239. return b
  240. }