summary-sample-excel.go 13 KB

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