summary-sample-excel.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. package api
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/xuri/excelize/v2"
  6. )
  7. // 生产成本表
  8. type SummarySampleExcel struct {
  9. Row int
  10. Title string
  11. Excel *excelize.File
  12. SheetName string
  13. AlignCenterStyle int
  14. Content *SupplierPlanSummary
  15. }
  16. func (b *SummarySampleExcel) drawTitle() error {
  17. b.Row++
  18. startCell := fmt.Sprintf("A%d", b.Row)
  19. err := b.Excel.MergeCell(b.SheetName, startCell, fmt.Sprintf("G%d", b.Row))
  20. if err != nil {
  21. return err
  22. }
  23. style, err := b.Excel.NewStyle(&excelize.Style{
  24. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  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, b.Row, 23)
  34. b.Excel.SetCellValue(b.SheetName, startCell, "追踪表")
  35. return nil
  36. }
  37. func (b *SummarySampleExcel) drawTableTitle() error {
  38. b.Row++
  39. var drawCol = func(prefix string, value string) error {
  40. left1Cell := fmt.Sprintf("%s%d", prefix, b.Row)
  41. left2Cell := fmt.Sprintf("%s%d", prefix, b.Row+1)
  42. err := b.Excel.MergeCell(b.SheetName, left1Cell, left2Cell)
  43. if err != nil {
  44. return err
  45. }
  46. err = b.Excel.SetCellStyle(b.SheetName, left1Cell, left2Cell, b.AlignCenterStyle)
  47. if err != nil {
  48. return err
  49. }
  50. return b.Excel.SetCellValue(b.SheetName, left1Cell, value)
  51. }
  52. drawCol("A", "序号")
  53. drawCol("B", "产品名称")
  54. drawCol("C", "产品部件名称")
  55. drawCol("D", "下单日期")
  56. drawCol("E", "交货日期")
  57. drawCol("F", "工序")
  58. drawCol("G", "备注")
  59. return nil
  60. }
  61. func (b *SummarySampleExcel) drawRow(rowIndex int, values ...string) {
  62. charas := []string{"A", "B", "C", "D", "E", "F", "G"}
  63. for i, c := range charas {
  64. v := ""
  65. if i < len(values) {
  66. v = values[i]
  67. }
  68. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("%s%d", c, rowIndex), v)
  69. val2Cel := fmt.Sprintf("%s%d", c, rowIndex)
  70. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, b.AlignCenterStyle)
  71. if c == "F" {
  72. border := []excelize.Border{
  73. {Type: "top", Style: 1, Color: "000000"},
  74. {Type: "left", Style: 1, Color: "000000"},
  75. {Type: "right", Style: 1, Color: "000000"},
  76. {Type: "bottom", Style: 1, Color: "000000"},
  77. }
  78. styleLeft, _ := b.Excel.NewStyle(&excelize.Style{
  79. Alignment: &excelize.Alignment{Horizontal: "left", Vertical: "center", WrapText: true},
  80. Font: &excelize.Font{Size: 11},
  81. Border: border})
  82. b.Excel.SetCellStyle(b.SheetName, val2Cel, val2Cel, styleLeft)
  83. }
  84. b.Excel.SetRowHeight(b.SheetName, rowIndex, 32)
  85. }
  86. }
  87. func (b *SummarySampleExcel) drawAllContent() error {
  88. b.Row += 2
  89. // summaryPlans
  90. index := 0
  91. for _, splan := range b.Content.Plans {
  92. index++
  93. planStartRow := b.Row
  94. plan := splan.Plan
  95. comps := plan.Pack.Components
  96. if len(comps) > 0 {
  97. for _, comp := range comps {
  98. if len(comp.Stages) > 0 {
  99. startRow := b.Row
  100. cates := map[string][]int{}
  101. for _, stage := range comp.Stages {
  102. // 状态
  103. stageStatus := ""
  104. statusMark := "【x】"
  105. // if len(stage.BillId) < 1 {
  106. // stageStatus = "未生成订单"
  107. // }
  108. // if splan.State[stage.BillId] == "created" {
  109. // stageStatus = "进行中"
  110. // }
  111. // // 审核状态
  112. // if splan.Reviewed[stage.BillId] == 1 {
  113. // stageStatus = "已审核"
  114. // } else {
  115. // stageStatus = "未审核"
  116. // }
  117. // // 接单状态
  118. // if splan.IsAck[stage.BillId] {
  119. // stageStatus = "已接单"
  120. // } else {
  121. // stageStatus = "未接单"
  122. // }
  123. // // 完成状态
  124. // if splan.State[stage.BillId] == "complete" {
  125. // stageStatus = "已完成"
  126. // statusMark = "【v】"
  127. // }
  128. if len(stage.BillId) < 1 {
  129. stageStatus = "未生成订单"
  130. } else {
  131. if splan.State[stage.BillId] == "created" {
  132. stageStatus = "进行中"
  133. // 审核状态
  134. if splan.Reviewed[stage.BillId] == 1 {
  135. stageStatus = "已审核"
  136. if splan.IsAck[stage.BillId] {
  137. stageStatus = "已接单"
  138. } else {
  139. stageStatus = "未接单"
  140. }
  141. } else {
  142. stageStatus = "未审核"
  143. }
  144. } else {
  145. stageStatus = "已完成"
  146. statusMark = "【v】"
  147. }
  148. }
  149. fmt.Println(stageStatus)
  150. createTime := splan.CreateTimes[stage.BillId].Local().Format("2006-01-02")
  151. deliveryTime := stage.DeliveryTime.Local().Format("2006-01-02")
  152. stageNmae := fmt.Sprintf("%s %s", statusMark, stage.Name)
  153. b.drawRow(b.Row, "", "", "", createTime, deliveryTime, stageNmae, "")
  154. cates[fmt.Sprintf("%s,%s,%s", createTime, deliveryTime, stageNmae)] = append(cates[fmt.Sprintf("%s,%s,%s", createTime, deliveryTime, stageNmae)], b.Row)
  155. b.Row++
  156. }
  157. for stageInfo, cate := range cates {
  158. sf := strings.Split(stageInfo, ",")
  159. fmt.Println(sf)
  160. mergeStartRow := cate[0]
  161. mergeEndRow := cate[len(cate)-1]
  162. b.Excel.MergeCell(b.SheetName, fmt.Sprintf("D%d", mergeStartRow), fmt.Sprintf("D%d", mergeEndRow))
  163. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("D%d", mergeEndRow), sf[0])
  164. b.Excel.MergeCell(b.SheetName, fmt.Sprintf("E%d", mergeStartRow), fmt.Sprintf("E%d", mergeEndRow))
  165. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("E%d", mergeEndRow), sf[1])
  166. b.Excel.MergeCell(b.SheetName, fmt.Sprintf("F%d", mergeStartRow), fmt.Sprintf("F%d", mergeEndRow))
  167. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("F%d", mergeEndRow), sf[2])
  168. }
  169. endRow := b.Row - 1
  170. // 组件名字
  171. startACell := fmt.Sprintf("%s%d", "C", startRow)
  172. endACell := fmt.Sprintf("%s%d", "C", endRow)
  173. b.Excel.MergeCell(b.SheetName, startACell, endACell)
  174. err := b.Excel.SetCellStyle(b.SheetName, startACell, endACell, b.AlignCenterStyle)
  175. if err != nil {
  176. return err
  177. }
  178. b.Excel.SetCellValue(b.SheetName, startACell, comp.Name)
  179. }
  180. }
  181. }
  182. // 序号
  183. planEndRow := b.Row
  184. planStartACell := fmt.Sprintf("%s%d", "A", planStartRow)
  185. planEndACell := fmt.Sprintf("%s%d", "A", planEndRow-1)
  186. b.Excel.MergeCell(b.SheetName, planStartACell, planEndACell)
  187. err := b.Excel.SetCellStyle(b.SheetName, planStartACell, planEndACell, b.AlignCenterStyle)
  188. if err != nil {
  189. return err
  190. }
  191. b.Excel.SetCellValue(b.SheetName, planStartACell, index)
  192. // 产品名
  193. planStartBCell := fmt.Sprintf("%s%d", "B", planStartRow)
  194. planEndBCell := fmt.Sprintf("%s%d", "B", planEndRow-1)
  195. b.Excel.MergeCell(b.SheetName, planStartBCell, planEndBCell)
  196. err = b.Excel.SetCellStyle(b.SheetName, planStartBCell, planEndBCell, b.AlignCenterStyle)
  197. if err != nil {
  198. return err
  199. }
  200. b.Excel.SetCellValue(b.SheetName, planStartBCell, plan.Name)
  201. // 备注
  202. planStartFCell := fmt.Sprintf("%s%d", "G", planStartRow)
  203. planEndFCell := fmt.Sprintf("%s%d", "G", planEndRow-1)
  204. b.Excel.MergeCell(b.SheetName, planStartFCell, planEndFCell)
  205. err = b.Excel.SetCellStyle(b.SheetName, planStartFCell, planEndFCell, b.AlignCenterStyle)
  206. if err != nil {
  207. return err
  208. }
  209. b.Excel.SetCellValue(b.SheetName, planStartFCell, "")
  210. }
  211. return nil
  212. }
  213. func (b *SummarySampleExcel) drawSupplierContent() error {
  214. b.Row += 2
  215. index := 0
  216. supplier := ""
  217. // summaryPlans
  218. for _, splan := range b.Content.Plans {
  219. index++
  220. planStartRow := b.Row
  221. plan := splan.Plan
  222. comps := plan.Pack.Components
  223. if len(comps) > 0 {
  224. for _, comp := range comps {
  225. if len(comp.Stages) > 0 {
  226. startRow := 0
  227. cates := map[string][]int{}
  228. for _, stage := range comp.Stages {
  229. if stage.SupplierInfo != nil {
  230. if b.Content.SupplierId == stage.SupplierInfo.Id {
  231. supplier = stage.SupplierInfo.Name
  232. // 材料
  233. if startRow == 0 {
  234. startRow = b.Row
  235. }
  236. // 状态
  237. stageStatus := ""
  238. statusMark := "【x】"
  239. // if len(stage.BillId) < 1 {
  240. // stageStatus = "未生成订单"
  241. // }
  242. // if splan.State[stage.BillId] == "created" {
  243. // stageStatus = "进行中"
  244. // }
  245. // // 审核状态
  246. // if splan.Reviewed[stage.BillId] == 1 {
  247. // stageStatus = "已审核"
  248. // } else {
  249. // stageStatus = "未审核"
  250. // }
  251. // // 接单状态
  252. // if splan.IsAck[stage.BillId] {
  253. // stageStatus = "已接单"
  254. // } else {
  255. // stageStatus = "未接单"
  256. // }
  257. // // 完成状态
  258. // if splan.State[stage.BillId] == "complete" {
  259. // stageStatus = "已完成"
  260. // statusMark = "【v】"
  261. // }
  262. if len(stage.BillId) < 1 {
  263. stageStatus = "未生成订单"
  264. } else {
  265. if splan.State[stage.BillId] == "created" {
  266. stageStatus = "进行中"
  267. // 审核状态
  268. if splan.Reviewed[stage.BillId] == 1 {
  269. stageStatus = "已审核"
  270. if splan.IsAck[stage.BillId] {
  271. stageStatus = "已接单"
  272. } else {
  273. stageStatus = "未接单"
  274. }
  275. } else {
  276. stageStatus = "未审核"
  277. }
  278. } else {
  279. stageStatus = "已完成"
  280. statusMark = "【v】"
  281. }
  282. }
  283. fmt.Println(stageStatus)
  284. createTime := splan.CreateTimes[stage.BillId].Local().Format("2006-01-02")
  285. deliveryTime := stage.DeliveryTime.Local().Format("2006-01-02")
  286. stageNmae := fmt.Sprintf("%s %s", statusMark, stage.Name)
  287. b.drawRow(b.Row, "", "", "", createTime, deliveryTime, stageNmae, "")
  288. cates[fmt.Sprintf("%s,%s,%s", createTime, deliveryTime, stageNmae)] = append(cates[fmt.Sprintf("%s,%s,%s", createTime, deliveryTime, stageNmae)], b.Row)
  289. b.Row++
  290. }
  291. }
  292. }
  293. for stageInfo, cate := range cates {
  294. sf := strings.Split(stageInfo, ",")
  295. fmt.Println(sf)
  296. mergeStartRow := cate[0]
  297. mergeEndRow := cate[len(cate)-1]
  298. b.Excel.MergeCell(b.SheetName, fmt.Sprintf("D%d", mergeStartRow), fmt.Sprintf("D%d", mergeEndRow))
  299. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("D%d", mergeEndRow), sf[0])
  300. b.Excel.MergeCell(b.SheetName, fmt.Sprintf("E%d", mergeStartRow), fmt.Sprintf("E%d", mergeEndRow))
  301. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("E%d", mergeEndRow), sf[1])
  302. b.Excel.MergeCell(b.SheetName, fmt.Sprintf("F%d", mergeStartRow), fmt.Sprintf("F%d", mergeEndRow))
  303. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("F%d", mergeEndRow), sf[2])
  304. }
  305. if startRow != 0 {
  306. endRow := b.Row - 1
  307. // 组件名字
  308. startACell := fmt.Sprintf("%s%d", "C", startRow)
  309. endACell := fmt.Sprintf("%s%d", "C", endRow)
  310. b.Excel.MergeCell(b.SheetName, startACell, endACell)
  311. err := b.Excel.SetCellStyle(b.SheetName, startACell, endACell, b.AlignCenterStyle)
  312. if err != nil {
  313. return err
  314. }
  315. b.Excel.SetCellValue(b.SheetName, startACell, comp.Name)
  316. }
  317. }
  318. }
  319. }
  320. // 序号
  321. planEndRow := b.Row
  322. planStartACell := fmt.Sprintf("%s%d", "A", planStartRow)
  323. planEndACell := fmt.Sprintf("%s%d", "A", planEndRow-1)
  324. b.Excel.MergeCell(b.SheetName, planStartACell, planEndACell)
  325. err := b.Excel.SetCellStyle(b.SheetName, planStartACell, planEndACell, b.AlignCenterStyle)
  326. if err != nil {
  327. return err
  328. }
  329. b.Excel.SetCellValue(b.SheetName, planStartACell, index)
  330. // 产品名
  331. planStartBCell := fmt.Sprintf("%s%d", "B", planStartRow)
  332. planEndBCell := fmt.Sprintf("%s%d", "B", planEndRow-1)
  333. b.Excel.MergeCell(b.SheetName, planStartBCell, planEndBCell)
  334. err = b.Excel.SetCellStyle(b.SheetName, planStartBCell, planEndBCell, b.AlignCenterStyle)
  335. if err != nil {
  336. return err
  337. }
  338. b.Excel.SetCellValue(b.SheetName, planStartBCell, plan.Name)
  339. // 备注
  340. planStartFCell := fmt.Sprintf("%s%d", "G", planStartRow)
  341. planEndFCell := fmt.Sprintf("%s%d", "G", planEndRow-1)
  342. b.Excel.MergeCell(b.SheetName, planStartFCell, planEndFCell)
  343. err = b.Excel.SetCellStyle(b.SheetName, planStartFCell, planEndFCell, b.AlignCenterStyle)
  344. if err != nil {
  345. return err
  346. }
  347. b.Excel.SetCellValue(b.SheetName, planStartFCell, "")
  348. }
  349. // 供应商名字标题
  350. style, err := b.Excel.NewStyle(&excelize.Style{
  351. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  352. })
  353. if err != nil {
  354. return err
  355. }
  356. err = b.Excel.SetCellStyle(b.SheetName, "A1", "G1", style)
  357. if err != nil {
  358. return err
  359. }
  360. b.Excel.SetRowHeight(b.SheetName, 1, 32)
  361. b.Excel.SetCellValue(b.SheetName, "A1", fmt.Sprintf("【%s】-追踪表", supplier))
  362. return nil
  363. }
  364. func (b *SummarySampleExcel) Draws() {
  365. b.drawTitle()
  366. b.drawTableTitle()
  367. if !b.Content.SupplierId.IsZero() {
  368. b.drawSupplierContent()
  369. } else {
  370. b.drawAllContent()
  371. }
  372. }
  373. func NewSummarySampleExcel(f *excelize.File) *SummarySampleExcel {
  374. border := []excelize.Border{
  375. {Type: "top", Style: 1, Color: "000000"},
  376. {Type: "left", Style: 1, Color: "000000"},
  377. {Type: "right", Style: 1, Color: "000000"},
  378. {Type: "bottom", Style: 1, Color: "000000"},
  379. }
  380. styleLeft, _ := f.NewStyle(&excelize.Style{
  381. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  382. Border: border,
  383. Font: &excelize.Font{Size: 10},
  384. })
  385. b := &SummarySampleExcel{
  386. Title: "生产成本表",
  387. SheetName: "Sheet1",
  388. Excel: f,
  389. AlignCenterStyle: styleLeft,
  390. }
  391. f.SetColWidth(b.SheetName, "A", "A", 6)
  392. f.SetColWidth(b.SheetName, "B", "E", 16)
  393. f.SetColWidth(b.SheetName, "F", "F", 20)
  394. f.SetColWidth(b.SheetName, "G", "G", 16)
  395. f.SetPageMargins(b.SheetName, excelize.PageMarginTop(0), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
  396. return b
  397. }
  398. func (b *SummarySampleExcel) FormatToEmpty(str *string) {
  399. if *str == "0" || *str == "0.000" {
  400. *str = ""
  401. }
  402. }