bill-produce-excel.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. package api
  2. import (
  3. "box-cost/db/model"
  4. "fmt"
  5. "strings"
  6. "github.com/xuri/excelize/v2"
  7. )
  8. type ProduceBillExcel struct {
  9. Offset int
  10. Row int
  11. Title string //标题
  12. Excel *excelize.File
  13. SheetName string
  14. AlignCenterStyle int
  15. Content *model.ProduceBill
  16. Signatures []*model.Signature
  17. IsPdf string
  18. RowMap map[string]int
  19. RowWidthArray []float64
  20. RowsHeightArray []map[int]float64
  21. }
  22. // 批量设置行高
  23. func (b *ProduceBillExcel) setRowsHeight() {
  24. for _, rowHeight := range b.RowsHeightArray {
  25. for row, height := range rowHeight {
  26. b.Excel.SetRowHeight(b.SheetName, row, height)
  27. }
  28. }
  29. }
  30. // 获取范围内单元格的宽度 A:F
  31. func (b *ProduceBillExcel) getRangeWidth(r string) float64 {
  32. rg := strings.Split(r, ":")
  33. if len(rg) == 1 {
  34. start := b.RowMap[rg[0]]
  35. return b.RowWidthArray[start]
  36. } else if len(rg) == 2 {
  37. start := b.RowMap[rg[0]]
  38. end := b.RowMap[rg[1]]
  39. rowr := b.RowWidthArray[start : end+1]
  40. width := 0.0
  41. for _, v := range rowr {
  42. width += v
  43. }
  44. return width
  45. }
  46. return 0.0
  47. }
  48. func (b *ProduceBillExcel) drawTitle() error {
  49. b.Row++
  50. //是打印
  51. startCell := fmt.Sprintf("A%d", b.Row)
  52. marginLeft := excelize.PageMarginLeft(0.1)
  53. endCell := fmt.Sprintf("L%d", b.Row)
  54. if b.Content.IsPrint {
  55. b.RowMap = map[string]int{"A": 0, "B": 1, "C": 2, "D": 3, "E": 4, "F": 5, "G": 6, "H": 7, "I": 8, "J": 9, "K": 10, "L": 11}
  56. b.RowWidthArray = []float64{11, 11, 11, 10, 7, 10, 7, 6.5, 9, 9, 12, 14}
  57. b.Excel.SetColWidth(b.SheetName, "A", "C", 11)
  58. b.Excel.SetColWidth(b.SheetName, "D", "D", 10)
  59. b.Excel.SetColWidth(b.SheetName, "E", "E", 7)
  60. b.Excel.SetColWidth(b.SheetName, "F", "F", 10)
  61. b.Excel.SetColWidth(b.SheetName, "G", "G", 7)
  62. b.Excel.SetColWidth(b.SheetName, "H", "H", 6.5)
  63. b.Excel.SetColWidth(b.SheetName, "I", "J", 9)
  64. b.Excel.SetColWidth(b.SheetName, "K", "K", 12)
  65. b.Excel.SetColWidth(b.SheetName, "L", "L", 14)
  66. } else {
  67. // 不是打印
  68. endCell = fmt.Sprintf("H%d", b.Row)
  69. marginLeft = excelize.PageMarginLeft(0.6)
  70. b.Excel.SetColWidth(b.SheetName, "A", "B", 17)
  71. b.Excel.SetColWidth(b.SheetName, "C", "C", 12)
  72. b.Excel.SetColWidth(b.SheetName, "D", "D", 6)
  73. b.Excel.SetColWidth(b.SheetName, "E", "G", 12)
  74. b.Excel.SetColWidth(b.SheetName, "H", "H", 20)
  75. // 是覆膜
  76. if b.Content.IsLam {
  77. b.RowMap = map[string]int{"A": 0, "B": 1, "C": 2, "D": 3, "E": 4, "F": 5, "G": 6, "H": 7, "I": 8}
  78. b.RowWidthArray = []float64{14, 14, 14, 10, 11, 11, 12, 12, 16}
  79. endCell = fmt.Sprintf("I%d", b.Row)
  80. marginLeft = excelize.PageMarginLeft(0.2)
  81. b.Excel.SetColWidth(b.SheetName, "A", "C", 14)
  82. // 覆膜规格
  83. b.Excel.SetColWidth(b.SheetName, "D", "D", 10)
  84. b.Excel.SetColWidth(b.SheetName, "E", "F", 11)
  85. b.Excel.SetColWidth(b.SheetName, "G", "H", 12)
  86. b.Excel.SetColWidth(b.SheetName, "I", "I", 16)
  87. }
  88. }
  89. b.Excel.SetPageMargins(b.SheetName, marginLeft)
  90. err := b.Excel.MergeCell(b.SheetName, startCell, endCell)
  91. if err != nil {
  92. return err
  93. }
  94. style, err := b.Excel.NewStyle(&excelize.Style{
  95. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  96. Font: &excelize.Font{Bold: true, Size: 18}})
  97. if err != nil {
  98. return err
  99. }
  100. err = b.Excel.SetCellStyle(b.SheetName, startCell, startCell, style)
  101. if err != nil {
  102. return err
  103. }
  104. b.Excel.SetRowHeight(b.SheetName, b.Row, 23)
  105. b.Excel.SetCellValue(b.SheetName, startCell, b.Title)
  106. return nil
  107. }
  108. func (b *ProduceBillExcel) drawSubTitles() error {
  109. b.Row++
  110. styleLeft, err := b.Excel.NewStyle(&excelize.Style{
  111. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  112. Font: &excelize.Font{Size: 11}})
  113. if err != nil {
  114. return err
  115. }
  116. styleRight, err := b.Excel.NewStyle(&excelize.Style{
  117. Alignment: &excelize.Alignment{Horizontal: "right", Vertical: "center"},
  118. Font: &excelize.Font{Size: 11}})
  119. if err != nil {
  120. return err
  121. }
  122. drawLeft := func(rowIndex int, value string) error {
  123. //左边1
  124. left1Cell := fmt.Sprintf("A%d", rowIndex)
  125. err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("I%d", rowIndex))
  126. if err != nil {
  127. return err
  128. }
  129. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  130. if err != nil {
  131. return err
  132. }
  133. b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  134. // 设置行高
  135. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{rowIndex: getRowHeight(value, b.getRangeWidth("A:I"), 18)})
  136. return nil
  137. }
  138. drawRight := func(rowIndex int, value string) error {
  139. right1Cell := fmt.Sprintf("J%d", rowIndex)
  140. err = b.Excel.MergeCell(b.SheetName, right1Cell, fmt.Sprintf("L%d", rowIndex))
  141. if err != nil {
  142. return err
  143. }
  144. err = b.Excel.SetCellStyle(b.SheetName, right1Cell, right1Cell, styleRight)
  145. if err != nil {
  146. return err
  147. }
  148. b.Excel.SetCellValue(b.SheetName, right1Cell, value)
  149. return nil
  150. }
  151. drawLall := func(rowIndex int, value string) error {
  152. //左边1
  153. left1Cell := fmt.Sprintf("A%d", rowIndex)
  154. err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("L%d", rowIndex))
  155. if err != nil {
  156. return err
  157. }
  158. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  159. if err != nil {
  160. return err
  161. }
  162. b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  163. // 设置行高
  164. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{rowIndex: getRowHeight(value, b.getRangeWidth("A:L"), 18)})
  165. return nil
  166. }
  167. if !b.Content.IsPrint {
  168. drawLeft = func(rowIndex int, value string) error {
  169. //左边1
  170. left1Cell := fmt.Sprintf("A%d", rowIndex)
  171. err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("F%d", rowIndex))
  172. if err != nil {
  173. return err
  174. }
  175. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  176. if err != nil {
  177. return err
  178. }
  179. b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  180. // 设置行高
  181. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{rowIndex: getRowHeight(value, b.getRangeWidth("A:F"), 18)})
  182. return nil
  183. }
  184. drawRight = func(rowIndex int, value string) error {
  185. right1Cell := fmt.Sprintf("G%d", rowIndex)
  186. err = b.Excel.MergeCell(b.SheetName, right1Cell, fmt.Sprintf("H%d", rowIndex))
  187. if err != nil {
  188. return err
  189. }
  190. err = b.Excel.SetCellStyle(b.SheetName, right1Cell, right1Cell, styleRight)
  191. if err != nil {
  192. return err
  193. }
  194. b.Excel.SetCellValue(b.SheetName, right1Cell, value)
  195. return nil
  196. }
  197. drawLall = func(rowIndex int, value string) error {
  198. //左边1
  199. left1Cell := fmt.Sprintf("A%d", rowIndex)
  200. err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("H%d", rowIndex))
  201. if err != nil {
  202. return err
  203. }
  204. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  205. if err != nil {
  206. return err
  207. }
  208. b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  209. // 设置行高
  210. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{rowIndex: getRowHeight(value, b.getRangeWidth("A:H"), 18)})
  211. return nil
  212. }
  213. if b.Content.IsLam {
  214. drawLeft = func(rowIndex int, value string) error {
  215. //左边1
  216. left1Cell := fmt.Sprintf("A%d", rowIndex)
  217. err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("F%d", rowIndex))
  218. if err != nil {
  219. return err
  220. }
  221. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  222. if err != nil {
  223. return err
  224. }
  225. b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  226. // 设置行高
  227. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{rowIndex: getRowHeight(value, b.getRangeWidth("A:F"), 18)})
  228. return nil
  229. }
  230. drawRight = func(rowIndex int, value string) error {
  231. right1Cell := fmt.Sprintf("G%d", rowIndex)
  232. err = b.Excel.MergeCell(b.SheetName, right1Cell, fmt.Sprintf("I%d", rowIndex))
  233. if err != nil {
  234. return err
  235. }
  236. err = b.Excel.SetCellStyle(b.SheetName, right1Cell, right1Cell, styleRight)
  237. if err != nil {
  238. return err
  239. }
  240. b.Excel.SetCellValue(b.SheetName, right1Cell, value)
  241. return nil
  242. }
  243. drawLall = func(rowIndex int, value string) error {
  244. //左边1
  245. left1Cell := fmt.Sprintf("A%d", rowIndex)
  246. err = b.Excel.MergeCell(b.SheetName, left1Cell, fmt.Sprintf("I%d", rowIndex))
  247. if err != nil {
  248. return err
  249. }
  250. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left1Cell, styleLeft)
  251. if err != nil {
  252. return err
  253. }
  254. b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  255. // 设置行高
  256. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{rowIndex: getRowHeight(value, b.getRangeWidth("A:I"), 18)})
  257. return nil
  258. }
  259. }
  260. }
  261. //第一行
  262. drawLeft(b.Row, "类别:"+b.Content.Type)
  263. drawRight(b.Row, "单号:"+b.Content.SerialNumber)
  264. //第二行
  265. drawLeft(b.Row+1, "供应商名称:"+b.Content.Supplier)
  266. timeformat := b.Content.CreateTime.Local().Format("2006年01月02号 15:04:05")
  267. drawRight(b.Row+1, "下单时间:"+timeformat)
  268. //第三行
  269. drawLeft(b.Row+2, "产品名称:"+b.Content.ProductName)
  270. status := ""
  271. if b.Content.Status == "complete" {
  272. status = "已完成"
  273. }
  274. if b.Content.Status == "created" {
  275. status = "进行中"
  276. }
  277. drawRight(b.Row+2, "状态:"+status)
  278. // 第四行
  279. drawLall(b.Row+3, "包含工序:"+b.Content.CompProduceName)
  280. return nil
  281. }
  282. func (b *ProduceBillExcel) drawTableTitle() error {
  283. b.Row += 4
  284. var drawCol = func(prefix string, value string) error {
  285. left1Cell := fmt.Sprintf("%s%d", prefix, b.Row)
  286. left2Cell := fmt.Sprintf("%s%d", prefix, b.Row+1)
  287. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  288. if err != nil {
  289. return err
  290. }
  291. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  292. if err != nil {
  293. return err
  294. }
  295. return b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  296. }
  297. var drawCol3 = func(prefix1 string, prefix2 string, value1 string, value2 string, value3 string) error {
  298. left1Cell := fmt.Sprintf("%s%d", prefix1, b.Row)
  299. left2Cell := fmt.Sprintf("%s%d", prefix2, b.Row)
  300. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  301. if err != nil {
  302. return err
  303. }
  304. if err != nil {
  305. fmt.Println(err)
  306. return err
  307. }
  308. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  309. if err != nil {
  310. return err
  311. }
  312. b.Excel.SetCellValue(b.SheetName, left1Cell, value1)
  313. val2Cel := fmt.Sprintf("%s%d", prefix1, b.Row+1)
  314. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  315. b.Excel.SetCellValue(b.SheetName, val2Cel, value2)
  316. val3Cel := fmt.Sprintf("%s%d", prefix2, b.Row+1)
  317. b.Excel.SetCellStyle(b.SheetName, val3Cel, val3Cel, b.AlignCenterStyle)
  318. b.Excel.SetCellValue(b.SheetName, val3Cel, value3)
  319. return nil
  320. }
  321. unit := b.Content.Produces[0].Unit
  322. if b.Content.IsPrint {
  323. drawCol("A", "加工项目")
  324. drawCol("B", "规格")
  325. drawCol("C", "纸张")
  326. drawCol("D", "来纸尺寸")
  327. drawCol("E", "来纸数量")
  328. drawCol("F", "印刷尺寸")
  329. drawCol("G", "下单数量")
  330. drawCol("H", "单位")
  331. drawCol("I", "单价")
  332. drawCol("J", "预算金额")
  333. drawCol("K", "交货时间")
  334. drawCol("L", "备注")
  335. } else {
  336. if b.Content.IsLam {
  337. drawCol("A", "加工项目")
  338. drawCol("B", "规格")
  339. drawCol("C", "覆膜尺寸")
  340. drawCol("D", "下单数量")
  341. unit2 := b.Content.Produces[0].Unit2
  342. drawCol3("E", "F", "单价", unit, unit2)
  343. drawCol("G", "预算金额")
  344. drawCol("H", "交货时间")
  345. drawCol("I", "备注")
  346. } else {
  347. drawCol("A", "加工项目")
  348. drawCol("B", "规格")
  349. drawCol("C", "下单数量")
  350. drawCol("D", "单位")
  351. drawCol("E", "单价")
  352. drawCol("F", "预算金额")
  353. drawCol("G", "交货时间")
  354. drawCol("H", "备注")
  355. }
  356. }
  357. return nil
  358. }
  359. func (b *ProduceBillExcel) drawTableContent() error {
  360. b.Row += 2
  361. var DrawRow = func(rowIndex int, values ...string) float64 {
  362. charas := []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"}
  363. if !b.Content.IsPrint {
  364. charas = []string{"A", "B", "C", "D", "E", "F", "G", "H"}
  365. if b.Content.IsLam {
  366. charas = []string{"A", "B", "C", "D", "E", "F", "G", "H", "I"}
  367. }
  368. }
  369. // 获取改行最大行高
  370. max := getRowHeight(values[0], b.getRangeWidth(charas[0]))
  371. for i, c := range charas {
  372. v := ""
  373. if i < len(values) {
  374. v = values[i]
  375. }
  376. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v)
  377. val2Cel := fmt.Sprintf("%s%d", c, rowIndex)
  378. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  379. if getRowHeight(v, b.getRangeWidth(c)) > max {
  380. max = getRowHeight(v, b.getRangeWidth(c))
  381. }
  382. }
  383. return max
  384. }
  385. produces := b.Content.Produces
  386. if len(produces) > 0 {
  387. for _, produce := range produces {
  388. realCount := ""
  389. price := produce.OrderPrice
  390. priceStr := fmt.Sprintf("%.3f", price)
  391. b.FormatToEmpty(&priceStr)
  392. // 预算金额
  393. budgetAmount := fmt.Sprintf("%.3f", produce.OrderPrice*float64(produce.OrderCount))
  394. b.FormatToEmpty(&budgetAmount)
  395. // 实际金额
  396. realPrice := ""
  397. // 实际完成数
  398. realCount = fmt.Sprintf("%d", produce.ConfirmCount)
  399. b.FormatToEmpty(&realCount)
  400. realPrice = fmt.Sprintf("%.3f", produce.OrderPrice*float64(produce.ConfirmCount))
  401. b.FormatToEmpty(&realPrice)
  402. deliveryTime := produce.DeliveryTime.Local().Format("2006-01-02")
  403. paperCount := fmt.Sprintf("%d", produce.PaperCount)
  404. b.FormatToEmpty(&paperCount)
  405. rowMaxHeight := 0.0
  406. if b.Content.IsPrint {
  407. rowMaxHeight = DrawRow(b.Row, produce.Name, produce.Norm, produce.Paper, produce.PaperSize, paperCount, produce.PrintSize, fmt.Sprintf("%d", produce.OrderCount), produce.Unit, priceStr, budgetAmount, deliveryTime, produce.Remark)
  408. } else {
  409. // 覆膜
  410. if b.Content.IsLam {
  411. rowMaxHeight = DrawRow(b.Row, produce.Name, produce.Norm, produce.PrintSize, fmt.Sprintf("%d", produce.OrderCount), priceStr, fmt.Sprintf("%.3f", produce.Price2), budgetAmount, deliveryTime, produce.Remark)
  412. } else {
  413. rowMaxHeight = DrawRow(b.Row, produce.Name, produce.Norm, fmt.Sprintf("%d", produce.OrderCount), produce.Unit, priceStr, budgetAmount, deliveryTime, produce.Remark)
  414. }
  415. }
  416. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row: rowMaxHeight})
  417. b.Row++
  418. }
  419. }
  420. return nil
  421. }
  422. func (b *ProduceBillExcel) drawTableFooter() error {
  423. border := []excelize.Border{
  424. {Type: "top", Style: 1, Color: "000000"},
  425. {Type: "left", Style: 1, Color: "000000"},
  426. {Type: "right", Style: 1, Color: "000000"},
  427. {Type: "bottom", Style: 1, Color: "000000"},
  428. }
  429. styleLeft, _ := b.Excel.NewStyle(&excelize.Style{
  430. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  431. Border: border,
  432. })
  433. sendToStartCell := fmt.Sprintf("A%d", b.Row)
  434. sendToEndCell := fmt.Sprintf("I%d", b.Row)
  435. supplierStartCell := fmt.Sprintf("J%d", b.Row)
  436. supplierEndCell := fmt.Sprintf("L%d", b.Row)
  437. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row: getRowHeight("送货地址:"+b.Content.SendTo, b.getRangeWidth("A:I"))})
  438. if !b.Content.IsPrint {
  439. sendToEndCell = fmt.Sprintf("E%d", b.Row)
  440. supplierStartCell = fmt.Sprintf("F%d", b.Row)
  441. supplierEndCell = fmt.Sprintf("H%d", b.Row)
  442. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row: getRowHeight("送货地址:"+b.Content.SendTo, b.getRangeWidth("A:E"))})
  443. if b.Content.IsLam {
  444. sendToEndCell = fmt.Sprintf("F%d", b.Row)
  445. supplierStartCell = fmt.Sprintf("G%d", b.Row)
  446. supplierEndCell = fmt.Sprintf("I%d", b.Row)
  447. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row: getRowHeight("送货地址:"+b.Content.SendTo, b.getRangeWidth("A:F"))})
  448. }
  449. }
  450. b.Excel.MergeCell(b.SheetName, sendToStartCell, sendToEndCell)
  451. b.Excel.SetCellStyle(b.SheetName, sendToStartCell, sendToEndCell, styleLeft)
  452. b.Excel.SetCellValue(b.SheetName, sendToStartCell, "送货地址:"+b.Content.SendTo)
  453. b.Excel.MergeCell(b.SheetName, supplierStartCell, supplierEndCell)
  454. b.Excel.SetCellStyle(b.SheetName, supplierStartCell, supplierEndCell, styleLeft)
  455. b.Excel.SetCellValue(b.SheetName, supplierStartCell, "供应商签字:")
  456. return nil
  457. }
  458. func (b *ProduceBillExcel) drawSupplierRemark() error {
  459. b.Row++
  460. border := []excelize.Border{
  461. {Type: "top", Style: 1, Color: "000000"},
  462. {Type: "left", Style: 1, Color: "000000"},
  463. {Type: "right", Style: 1, Color: "000000"},
  464. {Type: "bottom", Style: 1, Color: "000000"},
  465. }
  466. styleLeft, _ := b.Excel.NewStyle(&excelize.Style{
  467. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  468. Border: border,
  469. })
  470. sendToStartCell := fmt.Sprintf("A%d", b.Row)
  471. sendToEndCell := fmt.Sprintf("L%d", b.Row)
  472. // 设置行高
  473. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row: getRowHeight("供应商备注:"+b.Content.SupplierRemark, b.getRangeWidth("A:L"), 18)})
  474. if !b.Content.IsPrint {
  475. sendToEndCell = fmt.Sprintf("H%d", b.Row)
  476. // 设置行高
  477. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row: getRowHeight("供应商备注:"+b.Content.SupplierRemark, b.getRangeWidth("A:H"), 18)})
  478. if b.Content.IsLam {
  479. sendToEndCell = fmt.Sprintf("I%d", b.Row)
  480. // 设置行高
  481. b.RowsHeightArray = append(b.RowsHeightArray, map[int]float64{b.Row: getRowHeight("供应商备注:"+b.Content.SupplierRemark, b.getRangeWidth("A:I"), 18)})
  482. }
  483. }
  484. b.Excel.MergeCell(b.SheetName, sendToStartCell, sendToEndCell)
  485. b.Excel.SetCellStyle(b.SheetName, sendToStartCell, sendToEndCell, styleLeft)
  486. b.Excel.SetCellValue(b.SheetName, sendToStartCell, "供应商备注:"+b.Content.SupplierRemark)
  487. return nil
  488. }
  489. func (b *ProduceBillExcel) drawTableSignature() error {
  490. b.Row += 2
  491. style1, _ := b.Excel.NewStyle(&excelize.Style{
  492. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
  493. // Border: border,
  494. })
  495. // 制单人
  496. billUserCellS := fmt.Sprintf("A%d", b.Row+1)
  497. billUserCellE := fmt.Sprintf("B%d", b.Row+1)
  498. b.Excel.MergeCell(b.SheetName, billUserCellS, billUserCellE)
  499. b.Excel.SetCellValue(b.SheetName, billUserCellS, "制单人:"+b.Content.UserName)
  500. fontNum := "G"
  501. fontENum := "H"
  502. image1s := "I"
  503. image2s := "K"
  504. image1e := "J"
  505. image2e := "L"
  506. if !b.Content.IsPrint {
  507. fontNum = "E"
  508. fontENum = "E"
  509. image1s = "F"
  510. image2s = "H"
  511. image1e = "G"
  512. image2e = "H"
  513. if b.Content.IsLam {
  514. fontNum = "E"
  515. fontENum = "E"
  516. image1s = "F"
  517. image2s = "H"
  518. image1e = "G"
  519. image2e = "I"
  520. }
  521. }
  522. fontCell := fmt.Sprintf("%s%d", fontNum, b.Row)
  523. fontEndCell := fmt.Sprintf("%s%d", fontENum, b.Row+2)
  524. imageCell1 := fmt.Sprintf("%s%d", image1s, b.Row)
  525. imageEndCell1 := fmt.Sprintf("%s%d", image1e, b.Row+2)
  526. imageCell2 := fmt.Sprintf("%s%d", image2s, b.Row)
  527. imageEndCell2 := fmt.Sprintf("%s%d", image2e, b.Row+2)
  528. b.Excel.MergeCell(b.SheetName, fontCell, fontEndCell)
  529. b.Excel.SetCellStyle(b.SheetName, fontCell, fontCell, style1)
  530. b.Excel.SetCellValue(b.SheetName, fontCell, "审核签字:")
  531. // 签字图片1 I10-J12
  532. b.Excel.MergeCell(b.SheetName, imageCell1, imageEndCell1)
  533. b.Excel.SetCellStyle(b.SheetName, imageCell1, imageCell1, style1)
  534. b.Excel.SetCellValue(b.SheetName, imageCell1, "")
  535. // 签字图片2 K10-L12
  536. b.Excel.MergeCell(b.SheetName, imageCell2, imageEndCell2)
  537. b.Excel.SetCellStyle(b.SheetName, imageCell2, imageCell1, style1)
  538. b.Excel.SetCellValue(b.SheetName, imageCell2, "")
  539. // 状态为已审核时,签字
  540. if b.Content.Reviewed == 1 {
  541. imageCells := []string{imageCell1, imageCell2}
  542. if len(b.Signatures) > 0 {
  543. for k, sign := range b.Signatures {
  544. b.Excel.AddPicture(b.SheetName, imageCells[k], sign.Path, sign.Format)
  545. }
  546. }
  547. }
  548. b.Excel.SetRowHeight(b.SheetName, b.Row, 21)
  549. return nil
  550. }
  551. func (b *ProduceBillExcel) Draws() {
  552. b.drawTitle()
  553. b.drawSubTitles()
  554. b.drawTableTitle()
  555. b.drawTableContent()
  556. b.drawTableFooter()
  557. if len(b.Content.SupplierRemark) > 1 {
  558. b.drawSupplierRemark()
  559. }
  560. b.drawTableSignature()
  561. // 设置行高
  562. b.setRowsHeight()
  563. }
  564. func NewProduceBill(f *excelize.File) *ProduceBillExcel {
  565. border := []excelize.Border{
  566. {Type: "top", Style: 1, Color: "000000"},
  567. {Type: "left", Style: 1, Color: "000000"},
  568. {Type: "right", Style: 1, Color: "000000"},
  569. {Type: "bottom", Style: 1, Color: "000000"},
  570. }
  571. styleLeft, _ := f.NewStyle(&excelize.Style{
  572. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  573. Border: border,
  574. })
  575. b := &ProduceBillExcel{
  576. Title: "中鱼互动加工单",
  577. SheetName: "Sheet1",
  578. Excel: f,
  579. Offset: 0,
  580. AlignCenterStyle: styleLeft,
  581. Signatures: make([]*model.Signature, 0),
  582. RowMap: map[string]int{"A": 0, "B": 1, "C": 2, "D": 3, "E": 4, "F": 5, "G": 6, "H": 7},
  583. RowWidthArray: []float64{17, 17, 12, 6, 12, 12, 12, 20},
  584. RowsHeightArray: make([]map[int]float64, 0),
  585. }
  586. f.SetPageMargins(b.SheetName, excelize.PageMarginTop(1), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
  587. return b
  588. }
  589. func (b *ProduceBillExcel) FormatToEmpty(str *string) {
  590. if *str == "0" || *str == "0.000" {
  591. *str = "-"
  592. }
  593. }
  594. func (b *ProduceBillExcel) SetContent(content interface{}) {
  595. b.Content = content.(*model.ProduceBill)
  596. }
  597. func (b *ProduceBillExcel) SetTitle(title string) {
  598. b.Title = title
  599. }
  600. func (b *ProduceBillExcel) SetSheetName(name string) {
  601. b.SheetName = name
  602. }
  603. func (b *ProduceBillExcel) SetRow(row int) {
  604. b.Row = row
  605. }
  606. func (b *ProduceBillExcel) SetIsPdf(isPdf string) {
  607. b.IsPdf = isPdf
  608. }
  609. func (b *ProduceBillExcel) GetRow() int {
  610. return b.Row
  611. }
  612. func (b *ProduceBillExcel) SetSignatures(sign interface{}) {
  613. b.Signatures = sign.([]*model.Signature)
  614. }