summary-sample-excel.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. } else {
  108. if splan.State[stage.BillId] == "created" {
  109. stageStatus = "进行中"
  110. // 审核状态
  111. if splan.Reviewed[stage.BillId] == 1 {
  112. stageStatus = "已审核"
  113. if splan.IsSend[stage.BillId] {
  114. stageStatus = "已发送"
  115. if splan.IsAck[stage.BillId] {
  116. stageStatus = "已接单"
  117. } else {
  118. stageStatus = "未接单"
  119. }
  120. } else {
  121. stageStatus = "未发送"
  122. }
  123. } else {
  124. stageStatus = "未审核"
  125. }
  126. } else if splan.State[stage.BillId] == "complete" {
  127. stageStatus = "已完成"
  128. statusMark = "【v】"
  129. } else {
  130. stageStatus = "订单已销毁"
  131. }
  132. }
  133. fmt.Println(stageStatus)
  134. createTime := splan.CreateTimes[stage.BillId].Local().Format("2006-01-02")
  135. deliveryTime := stage.DeliveryTime.Local().Format("2006-01-02")
  136. stageNmae := fmt.Sprintf("%s %s", statusMark, stage.Name)
  137. b.drawRow(b.Row, "", "", "", createTime, deliveryTime, stageNmae, "")
  138. cates[fmt.Sprintf("%s,%s,%s", createTime, deliveryTime, stageNmae)] = append(cates[fmt.Sprintf("%s,%s,%s", createTime, deliveryTime, stageNmae)], b.Row)
  139. b.Row++
  140. }
  141. for stageInfo, cate := range cates {
  142. sf := strings.Split(stageInfo, ",")
  143. fmt.Println(sf)
  144. mergeStartRow := cate[0]
  145. mergeEndRow := cate[len(cate)-1]
  146. b.Excel.MergeCell(b.SheetName, fmt.Sprintf("D%d", mergeStartRow), fmt.Sprintf("D%d", mergeEndRow))
  147. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("D%d", mergeEndRow), sf[0])
  148. b.Excel.MergeCell(b.SheetName, fmt.Sprintf("E%d", mergeStartRow), fmt.Sprintf("E%d", mergeEndRow))
  149. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("E%d", mergeEndRow), sf[1])
  150. b.Excel.MergeCell(b.SheetName, fmt.Sprintf("F%d", mergeStartRow), fmt.Sprintf("F%d", mergeEndRow))
  151. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("F%d", mergeEndRow), sf[2])
  152. }
  153. endRow := b.Row - 1
  154. // 组件名字
  155. startACell := fmt.Sprintf("%s%d", "C", startRow)
  156. endACell := fmt.Sprintf("%s%d", "C", endRow)
  157. b.Excel.MergeCell(b.SheetName, startACell, endACell)
  158. err := b.Excel.SetCellStyle(b.SheetName, startACell, endACell, b.AlignCenterStyle)
  159. if err != nil {
  160. return err
  161. }
  162. b.Excel.SetCellValue(b.SheetName, startACell, comp.Name)
  163. }
  164. }
  165. }
  166. // 序号
  167. planEndRow := b.Row
  168. planStartACell := fmt.Sprintf("%s%d", "A", planStartRow)
  169. planEndACell := fmt.Sprintf("%s%d", "A", planEndRow-1)
  170. b.Excel.MergeCell(b.SheetName, planStartACell, planEndACell)
  171. err := b.Excel.SetCellStyle(b.SheetName, planStartACell, planEndACell, b.AlignCenterStyle)
  172. if err != nil {
  173. return err
  174. }
  175. b.Excel.SetCellValue(b.SheetName, planStartACell, index)
  176. // 产品名
  177. planStartBCell := fmt.Sprintf("%s%d", "B", planStartRow)
  178. planEndBCell := fmt.Sprintf("%s%d", "B", planEndRow-1)
  179. b.Excel.MergeCell(b.SheetName, planStartBCell, planEndBCell)
  180. err = b.Excel.SetCellStyle(b.SheetName, planStartBCell, planEndBCell, b.AlignCenterStyle)
  181. if err != nil {
  182. return err
  183. }
  184. b.Excel.SetCellValue(b.SheetName, planStartBCell, plan.Name)
  185. // 备注
  186. planStartFCell := fmt.Sprintf("%s%d", "G", planStartRow)
  187. planEndFCell := fmt.Sprintf("%s%d", "G", planEndRow-1)
  188. b.Excel.MergeCell(b.SheetName, planStartFCell, planEndFCell)
  189. err = b.Excel.SetCellStyle(b.SheetName, planStartFCell, planEndFCell, b.AlignCenterStyle)
  190. if err != nil {
  191. return err
  192. }
  193. b.Excel.SetCellValue(b.SheetName, planStartFCell, "")
  194. }
  195. return nil
  196. }
  197. func (b *SummarySampleExcel) drawSupplierContent() error {
  198. b.Row += 2
  199. index := 0
  200. supplier := ""
  201. // summaryPlans
  202. for _, splan := range b.Content.Plans {
  203. index++
  204. planStartRow := b.Row
  205. plan := splan.Plan
  206. comps := plan.Pack.Components
  207. if len(comps) > 0 {
  208. for _, comp := range comps {
  209. if len(comp.Stages) > 0 {
  210. startRow := 0
  211. cates := map[string][]int{}
  212. for _, stage := range comp.Stages {
  213. if stage.SupplierInfo != nil {
  214. if b.Content.SupplierId == stage.SupplierInfo.Id {
  215. supplier = stage.SupplierInfo.Name
  216. // 材料
  217. if startRow == 0 {
  218. startRow = b.Row
  219. }
  220. // 状态
  221. stageStatus := ""
  222. statusMark := "【x】"
  223. // if len(stage.BillId) < 1 {
  224. // stageStatus = "未生成订单"
  225. // }
  226. // if splan.State[stage.BillId] == "created" {
  227. // stageStatus = "进行中"
  228. // }
  229. // // 审核状态
  230. // if splan.Reviewed[stage.BillId] == 1 {
  231. // stageStatus = "已审核"
  232. // } else {
  233. // stageStatus = "未审核"
  234. // }
  235. // // 接单状态
  236. // if splan.IsAck[stage.BillId] {
  237. // stageStatus = "已接单"
  238. // } else {
  239. // stageStatus = "未接单"
  240. // }
  241. // // 完成状态
  242. // if splan.State[stage.BillId] == "complete" {
  243. // stageStatus = "已完成"
  244. // statusMark = "【v】"
  245. // }
  246. if len(stage.BillId) < 1 {
  247. stageStatus = "未生成订单"
  248. } else {
  249. if splan.State[stage.BillId] == "created" {
  250. stageStatus = "进行中"
  251. // 审核状态
  252. if splan.Reviewed[stage.BillId] == 1 {
  253. stageStatus = "已审核"
  254. if splan.IsSend[stage.BillId] {
  255. stageStatus = "已发送"
  256. if splan.IsAck[stage.BillId] {
  257. stageStatus = "已接单"
  258. } else {
  259. stageStatus = "未接单"
  260. }
  261. } else {
  262. stageStatus = "未发送"
  263. }
  264. } else {
  265. stageStatus = "未审核"
  266. }
  267. } else if splan.State[stage.BillId] == "complete" {
  268. stageStatus = "已完成"
  269. statusMark = "【v】"
  270. } else {
  271. stageStatus = "订单已销毁"
  272. }
  273. }
  274. fmt.Println(stageStatus)
  275. createTime := splan.CreateTimes[stage.BillId].Local().Format("2006-01-02")
  276. deliveryTime := stage.DeliveryTime.Local().Format("2006-01-02")
  277. stageNmae := fmt.Sprintf("%s %s", statusMark, stage.Name)
  278. b.drawRow(b.Row, "", "", "", createTime, deliveryTime, stageNmae, "")
  279. cates[fmt.Sprintf("%s,%s,%s", createTime, deliveryTime, stageNmae)] = append(cates[fmt.Sprintf("%s,%s,%s", createTime, deliveryTime, stageNmae)], b.Row)
  280. b.Row++
  281. }
  282. }
  283. }
  284. for stageInfo, cate := range cates {
  285. sf := strings.Split(stageInfo, ",")
  286. fmt.Println(sf)
  287. mergeStartRow := cate[0]
  288. mergeEndRow := cate[len(cate)-1]
  289. b.Excel.MergeCell(b.SheetName, fmt.Sprintf("D%d", mergeStartRow), fmt.Sprintf("D%d", mergeEndRow))
  290. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("D%d", mergeEndRow), sf[0])
  291. b.Excel.MergeCell(b.SheetName, fmt.Sprintf("E%d", mergeStartRow), fmt.Sprintf("E%d", mergeEndRow))
  292. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("E%d", mergeEndRow), sf[1])
  293. b.Excel.MergeCell(b.SheetName, fmt.Sprintf("F%d", mergeStartRow), fmt.Sprintf("F%d", mergeEndRow))
  294. b.Excel.SetCellValue(b.SheetName, fmt.Sprintf("F%d", mergeEndRow), sf[2])
  295. }
  296. if startRow != 0 {
  297. endRow := b.Row - 1
  298. // 组件名字
  299. startACell := fmt.Sprintf("%s%d", "C", startRow)
  300. endACell := fmt.Sprintf("%s%d", "C", endRow)
  301. b.Excel.MergeCell(b.SheetName, startACell, endACell)
  302. err := b.Excel.SetCellStyle(b.SheetName, startACell, endACell, b.AlignCenterStyle)
  303. if err != nil {
  304. return err
  305. }
  306. b.Excel.SetCellValue(b.SheetName, startACell, comp.Name)
  307. }
  308. }
  309. }
  310. }
  311. // 序号
  312. planEndRow := b.Row
  313. planStartACell := fmt.Sprintf("%s%d", "A", planStartRow)
  314. planEndACell := fmt.Sprintf("%s%d", "A", planEndRow-1)
  315. b.Excel.MergeCell(b.SheetName, planStartACell, planEndACell)
  316. err := b.Excel.SetCellStyle(b.SheetName, planStartACell, planEndACell, b.AlignCenterStyle)
  317. if err != nil {
  318. return err
  319. }
  320. b.Excel.SetCellValue(b.SheetName, planStartACell, index)
  321. // 产品名
  322. planStartBCell := fmt.Sprintf("%s%d", "B", planStartRow)
  323. planEndBCell := fmt.Sprintf("%s%d", "B", planEndRow-1)
  324. b.Excel.MergeCell(b.SheetName, planStartBCell, planEndBCell)
  325. err = b.Excel.SetCellStyle(b.SheetName, planStartBCell, planEndBCell, b.AlignCenterStyle)
  326. if err != nil {
  327. return err
  328. }
  329. b.Excel.SetCellValue(b.SheetName, planStartBCell, plan.Name)
  330. // 备注
  331. planStartFCell := fmt.Sprintf("%s%d", "G", planStartRow)
  332. planEndFCell := fmt.Sprintf("%s%d", "G", planEndRow-1)
  333. b.Excel.MergeCell(b.SheetName, planStartFCell, planEndFCell)
  334. err = b.Excel.SetCellStyle(b.SheetName, planStartFCell, planEndFCell, b.AlignCenterStyle)
  335. if err != nil {
  336. return err
  337. }
  338. b.Excel.SetCellValue(b.SheetName, planStartFCell, "")
  339. }
  340. // 供应商名字标题
  341. style, err := b.Excel.NewStyle(&excelize.Style{
  342. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  343. })
  344. if err != nil {
  345. return err
  346. }
  347. err = b.Excel.SetCellStyle(b.SheetName, "A1", "G1", style)
  348. if err != nil {
  349. return err
  350. }
  351. b.Excel.SetRowHeight(b.SheetName, 1, 32)
  352. b.Excel.SetCellValue(b.SheetName, "A1", fmt.Sprintf("【%s】-追踪表", supplier))
  353. return nil
  354. }
  355. func (b *SummarySampleExcel) Draws() {
  356. b.drawTitle()
  357. b.drawTableTitle()
  358. if !b.Content.SupplierId.IsZero() {
  359. b.drawSupplierContent()
  360. } else {
  361. b.drawAllContent()
  362. }
  363. }
  364. func NewSummarySampleExcel(f *excelize.File) *SummarySampleExcel {
  365. border := []excelize.Border{
  366. {Type: "top", Style: 1, Color: "000000"},
  367. {Type: "left", Style: 1, Color: "000000"},
  368. {Type: "right", Style: 1, Color: "000000"},
  369. {Type: "bottom", Style: 1, Color: "000000"},
  370. }
  371. styleLeft, _ := f.NewStyle(&excelize.Style{
  372. Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center", WrapText: true},
  373. Border: border,
  374. Font: &excelize.Font{Size: 10},
  375. })
  376. b := &SummarySampleExcel{
  377. Title: "生产成本表",
  378. SheetName: "Sheet1",
  379. Excel: f,
  380. AlignCenterStyle: styleLeft,
  381. }
  382. f.SetColWidth(b.SheetName, "A", "A", 6)
  383. f.SetColWidth(b.SheetName, "B", "E", 16)
  384. f.SetColWidth(b.SheetName, "F", "F", 20)
  385. f.SetColWidth(b.SheetName, "G", "G", 16)
  386. f.SetPageMargins(b.SheetName, excelize.PageMarginTop(0), excelize.PageMarginLeft(0), excelize.PageMarginRight(0))
  387. return b
  388. }
  389. func (b *SummarySampleExcel) FormatToEmpty(str *string) {
  390. if *str == "0" || *str == "0.000" {
  391. *str = ""
  392. }
  393. }