exa_excel_parse.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. package example
  2. import (
  3. "bytes"
  4. "errors"
  5. "fmt"
  6. "github.com/flipped-aurora/gin-vue-admin/server/global"
  7. "github.com/flipped-aurora/gin-vue-admin/server/model/autocode"
  8. "github.com/flipped-aurora/gin-vue-admin/server/model/system"
  9. "github.com/xuri/excelize/v2"
  10. "strconv"
  11. "strings"
  12. )
  13. type ExcelService struct {
  14. }
  15. func (exa *ExcelService) ParseInfoList2Excel(infoList []system.SysBaseMenu, filePath string) error {
  16. excel := excelize.NewFile()
  17. excel.SetSheetRow("Sheet1", "A1", &[]string{"ID", "路由Name", "路由Path", "是否隐藏", "父节点", "排序", "文件名称"})
  18. for i, menu := range infoList {
  19. axis := fmt.Sprintf("A%d", i+2)
  20. excel.SetSheetRow("Sheet1", axis, &[]interface{}{
  21. menu.ID,
  22. menu.Name,
  23. menu.Path,
  24. menu.Hidden,
  25. menu.ParentId,
  26. menu.Sort,
  27. menu.Component,
  28. })
  29. }
  30. err := excel.SaveAs(filePath)
  31. return err
  32. }
  33. func (exa *ExcelService) ProblemInfoList2Excel(infoList []autocode.ProblemInfo, filePath string, siteType system.SysDictionary, list []autocode.ProblemType) error {
  34. excel := excelize.NewFile()
  35. excel.SetSheetRow("Sheet1", "A1", &[]string{"ID", "问题发布时间", "问题处理时间", "问题视频地址", "问题图片", "站点名称", "站点位置", "问题", "站点类型", "一类单位", "评分", "责任部门", "已查阅次数", "处理状态", "处理图片", "处理内容", "处理人"})
  36. //站点类型
  37. siteMap := make(map[int]string)
  38. for _, siteT := range siteType.SysDictionaryDetails {
  39. siteMap[siteT.Value] = siteT.Label
  40. }
  41. //问题类型
  42. siteProMap := make(map[int]map[int]string)
  43. siteId := list[0].SiteType
  44. proMap := make(map[int]string)
  45. for _, pro := range list {
  46. if *siteId != *pro.SiteType {
  47. siteProMap[*siteId] = proMap
  48. siteId = pro.SiteType
  49. proMap = make(map[int]string)
  50. }
  51. proMap[int(pro.ID)] = pro.Problem
  52. }
  53. for i, problemInfo := range infoList {
  54. axis := fmt.Sprintf("A%d", i+2)
  55. var matterStr bytes.Buffer
  56. var status string
  57. if problemInfo.Status != "Untreated" {
  58. status = "未处理"
  59. } else {
  60. status = "已处理"
  61. }
  62. if problemInfo.Matter != "" {
  63. for index, id := range strings.Split(problemInfo.Matter, "|") {
  64. id, _ := strconv.Atoi(id)
  65. matterStr.WriteString(strconv.Itoa(index+1) + "." + siteProMap[*problemInfo.SiteType][id] + " \n")
  66. }
  67. } else {
  68. matterStr.WriteString(problemInfo.Remark)
  69. }
  70. excel.SetSheetRow("Sheet1", axis, &[]interface{}{
  71. problemInfo.ID,
  72. problemInfo.CreatedAt,
  73. problemInfo.UpdatedAt,
  74. problemInfo.Video,
  75. problemInfo.Imgs,
  76. problemInfo.SiteName,
  77. "https://apis.map.qq.com/uri/v1/marker?marker=coord:" + problemInfo.Position + ";title:" + problemInfo.SiteName + ";addr:" + siteMap[*problemInfo.SiteType],
  78. matterStr.String(),
  79. siteMap[*problemInfo.SiteType],
  80. problemInfo.UnitName,
  81. float64(*problemInfo.Integral) / 10,
  82. problemInfo.Department,
  83. *problemInfo.Count,
  84. status,
  85. problemInfo.HandImgs,
  86. problemInfo.HandText,
  87. &problemInfo.Handler,
  88. })
  89. }
  90. err := excel.SaveAs(filePath)
  91. return err
  92. }
  93. func (exa *ExcelService) ParseExcel2InfoList() ([]system.SysBaseMenu, error) {
  94. skipHeader := true
  95. fixedHeader := []string{"ID", "路由Name", "路由Path", "是否隐藏", "父节点", "排序", "文件名称"}
  96. file, err := excelize.OpenFile(global.GVA_CONFIG.Excel.Dir + "ExcelImport.xlsx")
  97. if err != nil {
  98. return nil, err
  99. }
  100. menus := make([]system.SysBaseMenu, 0)
  101. rows, err := file.Rows("Sheet1")
  102. if err != nil {
  103. return nil, err
  104. }
  105. for rows.Next() {
  106. row, err := rows.Columns()
  107. if err != nil {
  108. return nil, err
  109. }
  110. if skipHeader {
  111. if exa.compareStrSlice(row, fixedHeader) {
  112. skipHeader = false
  113. continue
  114. } else {
  115. return nil, errors.New("Excel格式错误")
  116. }
  117. }
  118. if len(row) != len(fixedHeader) {
  119. continue
  120. }
  121. id, _ := strconv.Atoi(row[0])
  122. hidden, _ := strconv.ParseBool(row[3])
  123. sort, _ := strconv.Atoi(row[5])
  124. menu := system.SysBaseMenu{
  125. GVA_MODEL: global.GVA_MODEL{
  126. ID: uint(id),
  127. },
  128. Name: row[1],
  129. Path: row[2],
  130. Hidden: hidden,
  131. ParentId: row[4],
  132. Sort: sort,
  133. Component: row[6],
  134. }
  135. menus = append(menus, menu)
  136. }
  137. return menus, nil
  138. }
  139. func (exa *ExcelService) compareStrSlice(a, b []string) bool {
  140. if len(a) != len(b) {
  141. return false
  142. }
  143. if (b == nil) != (a == nil) {
  144. return false
  145. }
  146. for key, value := range a {
  147. if value != b[key] {
  148. return false
  149. }
  150. }
  151. return true
  152. }