exprot.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package service
  2. import (
  3. "app.yhyue.com/moapp/jybase/mail"
  4. "archive/zip"
  5. . "bp.jydev.jianyu360.cn/BaseService/biService/entity"
  6. "fmt"
  7. "github.com/gogf/gf/v2/util/gconv"
  8. "github.com/xuri/excelize/v2"
  9. "io"
  10. "log"
  11. "os"
  12. "path/filepath"
  13. "strings"
  14. "time"
  15. )
  16. type ExportReq struct {
  17. Mail string
  18. Mapping []string
  19. PositionId int64
  20. }
  21. func (a *ExportReq) AllProjectExport() {
  22. tableColumn := []string{}
  23. exportKey := []interface{}{}
  24. for _, value := range a.Mapping {
  25. valueArr := strings.Split(value, ":")
  26. tableColumn = append(tableColumn, valueArr[0])
  27. exportKey = append(exportKey, valueArr[1])
  28. }
  29. timeStr := time.Now().Format("20060102150405")
  30. pathArr := export("customer_data_ttzl_project", tableColumn, exportKey, timeStr)
  31. if len(pathArr) > 0 {
  32. pathstr := compressFiles(pathArr, timeStr)
  33. state := sendMail(a.Mail, pathstr)
  34. BiService.Insert("export_record", map[string]interface{}{
  35. "positionId": a.PositionId,
  36. "type": "project",
  37. "excelPath": strings.Join(pathArr, ","),
  38. "zipPath": pathstr,
  39. "sendState": state,
  40. "createTime": time.Now().Format(time.DateTime),
  41. })
  42. }
  43. }
  44. func (a *ExportReq) AllInfoExport() {
  45. tableColumn := []string{}
  46. exportKey := []interface{}{}
  47. for _, value := range a.Mapping {
  48. valueArr := strings.Split(value, ":")
  49. tableColumn = append(tableColumn, valueArr[0])
  50. exportKey = append(exportKey, valueArr[1])
  51. }
  52. timeStr := time.Now().Format("20060102150405")
  53. pathArr := export("customer_data_ttzl", tableColumn, exportKey, timeStr)
  54. if len(pathArr) > 0 {
  55. pathstr := compressFiles(pathArr, timeStr)
  56. state := sendMail(a.Mail, pathstr)
  57. BiService.Insert("export_record", map[string]interface{}{
  58. "positionId": a.PositionId,
  59. "type": "project",
  60. "excelPath": strings.Join(pathArr, ","),
  61. "zipPath": pathstr,
  62. "sendState": state,
  63. "createTime": time.Now().Format(time.DateTime),
  64. })
  65. }
  66. }
  67. func export(table string, tableColumn []string, key []interface{}, timeStr string) []string {
  68. pathArr := []string{}
  69. sqlStr := fmt.Sprintf("SELECT %s FROM %s", strings.Join(tableColumn, ","), table)
  70. fileCounter := 1
  71. writer, file := InirWrite(key)
  72. a := 0
  73. path := ""
  74. BiService.SelectByBath(1, func(l *[]map[string]interface{}) bool {
  75. if a%2000 == 0 && a > 0 {
  76. a = 0
  77. //入excel处理
  78. writer, file, path = Warehousing(writer, file, fileCounter, key, timeStr)
  79. fileCounter++
  80. pathArr = append(pathArr, path)
  81. }
  82. dataHandle(writer, l, a, tableColumn)
  83. a++
  84. return true
  85. }, sqlStr)
  86. if a > 0 && a != 2000 {
  87. writer, file, path = Warehousing(writer, file, fileCounter, key, timeStr)
  88. pathArr = append(pathArr, path)
  89. }
  90. return pathArr
  91. }
  92. func dataHandle(writer *excelize.StreamWriter, l *[]map[string]interface{}, a int, tableColumn []string) {
  93. for _, value := range *l {
  94. valueArr := []interface{}{}
  95. for _, column := range tableColumn {
  96. valueArr = append(valueArr, gconv.String(value[column]))
  97. }
  98. cell, _ := excelize.CoordinatesToCellName(1, a+1)
  99. writer.SetRow(cell, valueArr)
  100. }
  101. }
  102. func Warehousing(writer *excelize.StreamWriter, file *excelize.File, fileCounter int, key []interface{}, timeStr string) (*excelize.StreamWriter, *excelize.File, string) {
  103. log.Println(fmt.Sprintf("开始第%d数据", fileCounter))
  104. writer.Flush()
  105. folderPath := fmt.Sprintf("static/excel/%s", timeStr)
  106. _, err := os.Stat(folderPath)
  107. if os.IsNotExist(err) {
  108. //创建文件夹
  109. err := os.MkdirAll(folderPath, os.ModePerm)
  110. if err != nil {
  111. log.Println("创建文件夹失败:", err)
  112. }
  113. }
  114. fileName := fmt.Sprintf("%s/output%d.xlsx", folderPath, fileCounter)
  115. err = file.SaveAs(fileName)
  116. if err != nil {
  117. log.Println(err)
  118. }
  119. writer, file = InirWrite(key)
  120. return writer, file, fileName
  121. }
  122. func InirWrite(key []interface{}) (*excelize.StreamWriter, *excelize.File) {
  123. //文件初始化
  124. file := excelize.NewFile()
  125. //设置表名
  126. file.SetSheetName("Sheet1", "表1")
  127. //创建流式写入
  128. writer, _ := file.NewStreamWriter("表1")
  129. // 定义每个 Excel 文件的数据行数限制
  130. writer.SetRow("A1", key)
  131. return writer, file
  132. }
  133. func sendMail(target_mail string, path string) bool {
  134. mailStr := "<html lang=\"en\">\n<head>\n <meta charset=\"UTF-8\">\n <title>铁塔全量标讯数据导出</title>\n</head>\n<body>\n<div>\n <p>铁塔全量标讯数据已导出成功,请查收附件,导出时间:%s,请点击链接下载数据:<a href=\"%s\">%s</a></p>\n </div>\n</body>"
  135. html := fmt.Sprintf(mailStr, time.Now().Format(time.DateTime), path, path)
  136. fool := false
  137. for k, v := range GmailAuth {
  138. fool := mail.GSendMail("剑鱼标讯", target_mail, "", "", "铁塔全量标讯数据导出", html, "", "", v)
  139. if fool {
  140. log.Println(target_mail, fmt.Sprintf("使用%s发送邮件成功", v.User))
  141. break
  142. }
  143. if k < len(GmailAuth)-1 {
  144. log.Println(target_mail, fmt.Sprintf("使用%s发送邮件失败!3s后使用其他邮箱尝试", v.User))
  145. } else {
  146. log.Println(target_mail, fmt.Sprintf("使用%s发送邮件失败!", v.User))
  147. }
  148. time.Sleep(time.Second * 3)
  149. }
  150. return fool
  151. }
  152. // 文件压缩
  153. func compressFiles(filePattern []string, timeStr string) string {
  154. folderPath := fmt.Sprintf("static/zip/%s", timeStr)
  155. _, err := os.Stat(folderPath)
  156. if os.IsNotExist(err) {
  157. //创建文件夹
  158. err := os.MkdirAll(folderPath, os.ModePerm)
  159. if err != nil {
  160. log.Println("创建文件夹失败:", err)
  161. }
  162. }
  163. zipFilename := fmt.Sprintf("%s/output%s.zip", folderPath, timeStr)
  164. // 创建 ZIP 文件
  165. zipFile, err := os.Create(zipFilename)
  166. if err != nil {
  167. log.Fatal(err)
  168. }
  169. defer zipFile.Close()
  170. // 创建 ZIP Writer
  171. zipWriter := zip.NewWriter(zipFile)
  172. defer zipWriter.Close()
  173. // 遍历文件列表
  174. for _, file := range filePattern {
  175. // 打开文件
  176. f, err := os.Open(file)
  177. if err != nil {
  178. log.Fatal(err)
  179. }
  180. defer f.Close()
  181. // 获取文件信息
  182. _, err = f.Stat()
  183. if err != nil {
  184. log.Fatal(err)
  185. }
  186. // 创建 ZIP 内部文件
  187. zipFile, err := zipWriter.Create(file)
  188. if err != nil {
  189. log.Fatal(err)
  190. }
  191. // 将文件内容复制到 ZIP 文件中
  192. _, err = io.Copy(zipFile, f)
  193. if err != nil {
  194. log.Fatal(err)
  195. }
  196. }
  197. // 打印压缩文件路径
  198. absPath, err := filepath.Abs(zipFilename)
  199. if err != nil {
  200. log.Fatal(err)
  201. }
  202. log.Printf("Files compressed to: %s", absPath)
  203. return zipFilename
  204. }