free_viewer.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package services
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/tealeg/xlsx"
  6. "hongze/hongze_task/models"
  7. "hongze/hongze_task/utils"
  8. "os"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. //潜在客户回访记录
  14. func FreeViewerDetail(cont context.Context) (err error) {
  15. defer func() {
  16. if err != nil {
  17. fmt.Println("crete FreeViewerDetail err:", err.Error())
  18. utils.FileLog.Info("crete FreeViewerDetail err: %s", err.Error())
  19. }
  20. }()
  21. //endTime := time.Now().Format(utils.FormatDateTime)
  22. startTime := time.Now().AddDate(0, 0, -7).Format(utils.FormatDateTime)
  23. endTime := time.Now().Format(utils.FormatDateTime)
  24. userIdsStr := ""
  25. applyRecordIdsStr := ""
  26. items, err := models.GetFreeViewerDetails(startTime, endTime)
  27. if err != nil {
  28. return
  29. }
  30. title := "潜在客户回访记录"
  31. //创建excel
  32. file := xlsx.NewFile()
  33. sheet, err := file.AddSheet(title)
  34. if err != nil {
  35. return err
  36. }
  37. //标头
  38. rowTitle := sheet.AddRow()
  39. cellA := rowTitle.AddCell()
  40. cellA.Value = "姓名"
  41. cellB := rowTitle.AddCell()
  42. cellB.Value = "手机号"
  43. cellC := rowTitle.AddCell()
  44. cellC.Value = "客户公司"
  45. cellD := rowTitle.AddCell()
  46. cellD.Value = "注册时间"
  47. cellE := rowTitle.AddCell()
  48. cellE.Value = "邮箱"
  49. for _, item := range items {
  50. row := sheet.AddRow()
  51. cellA := row.AddCell()
  52. cellA.Value = item.RealName
  53. cellB := row.AddCell()
  54. cellB.Value = item.Mobile
  55. cellC := row.AddCell()
  56. cellC.Value = item.CompanyName
  57. cellD := row.AddCell()
  58. cellD.Value = item.LastTime
  59. cellE := row.AddCell()
  60. cellE.Value = item.Email
  61. userIdsStr += ","+strconv.Itoa(item.UserId)
  62. applyRecordIdsStr += ","+strconv.Itoa(item.ApplyRecordId)
  63. }
  64. userIdsStr = strings.Trim(userIdsStr, ",")
  65. applyRecordIdsStr = strings.Trim(applyRecordIdsStr, ",")
  66. _ = dealUser(userIdsStr, applyRecordIdsStr)
  67. savePath := "free_viewer_details" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  68. err = file.Save("./" + savePath)
  69. if err != nil {
  70. return
  71. }
  72. //发送邮件
  73. fmt.Println("start send email")
  74. sendResult := utils.SendEmailByHongze(title, "潜在客户回访记录',\"你好,上周潜在客户回访记录见附件。", utils.EmailSendToHzUsers, savePath, title+".xlsx")
  75. //sendResult:=utils.SendEmailByHongze(title,"潜在客户回访记录',\"你好,上周潜在客户回访记录见附件。",utils.EmailSendToMe,savePath, title+".xlsx")
  76. if sendResult {
  77. _ = os.Remove(savePath)
  78. }
  79. //fmt.Println("send result:", sendResult)
  80. fmt.Println("end send email")
  81. return nil
  82. }
  83. func dealUser(userIds, applyRecordIds string) (err error) {
  84. if userIds !="" {
  85. _ = models.DealWxUsers(userIds)
  86. }
  87. if applyRecordIds != "" {
  88. nowTime := time.Now()
  89. _ = models.DealFiccApply(applyRecordIds, nowTime)
  90. }
  91. return
  92. }