aiweek-reconstruction/udesk/reply/method.go

177 lines
4.7 KiB
Go
Raw Normal View History

2024-05-18 01:46:35 +08:00
package reply
import (
2024-05-20 01:00:09 +08:00
"aiweek/option"
2024-05-18 01:46:35 +08:00
"aiweek/tools"
"aiweek/udesk/auth"
"aiweek/udesk/filter"
"fmt"
"github.com/PuerkitoBio/goquery"
"github.com/tidwall/gjson"
"github.com/xuri/excelize/v2"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
2024-05-20 01:00:09 +08:00
"sync"
"time"
2024-05-18 01:46:35 +08:00
)
//设置真正的id-content键值对。
2024-05-20 01:00:09 +08:00
func (e *Excel) SetReplyContent() *Excel {
2024-05-18 01:46:35 +08:00
uDeskId := filter.GetWeeklyTicket()
contentMap := make(map[string][]string)
2024-05-20 01:00:09 +08:00
var wg sync.WaitGroup
2024-05-18 01:46:35 +08:00
//计数器
2024-05-20 01:00:09 +08:00
j := 0
2024-05-18 01:46:35 +08:00
for _, v := range uDeskId {
2024-05-20 01:00:09 +08:00
j++
wg.Add(1)
log.Printf("goroutie%v:开始执行\n", j)
time.Sleep(150 * time.Millisecond)
go func(v string, j int) {
defer wg.Done()
idUrl := fmt.Sprintf("https://servicecenter-alauda.udesk.cn/open_api_v1/tickets/%v/replies?", v)
repliesUrl := auth.Geturlstring(idUrl)
resp, err := http.Get(repliesUrl)
2024-05-18 01:46:35 +08:00
if err != nil {
2024-05-20 01:00:09 +08:00
// 处理错误
log.Printf("获取工单过滤器下的回复信息失败,发送请求过程,错误是:%v\n", err)
2024-05-18 01:46:35 +08:00
return
}
2024-05-20 01:00:09 +08:00
body, err := io.ReadAll(resp.Body)
if err != nil {
// 处理错误
log.Printf("获取工单过滤器下的回复信息失败,读取返回体过程,错误是:%v\n", err)
return
}
defer resp.Body.Close()
jsonData := string(body)
//*查找回复内容和配图
repliesContent := gjson.Get(jsonData, "replies").Array()
var s = make([]string, 0)
//排除关于客户的回复
for _, v := range repliesContent {
b := v.String()
replyType := gjson.Get(b, "author.user_type").String()
if replyType == "agent" {
finalContent := gjson.Get(b, "content").String()
doc, _ := goquery.NewDocumentFromReader(strings.NewReader(finalContent))
doc.Find("p,img").Each(func(i int, selection *goquery.Selection) {
imgSrc, exists := selection.Attr("src")
if exists {
s = append(s, imgSrc)
s = append(s, "\n")
}
s = append(s, selection.Text())
//排除掉宏的内容
macros := excludeMacros()
s = tools.ExcludeSlice(macros, s)
s = tools.RemoveEmptyStrings(s)
2024-05-18 01:46:35 +08:00
s = append(s, "\n")
2024-05-20 01:00:09 +08:00
})
}
2024-05-18 01:46:35 +08:00
}
2024-05-20 01:00:09 +08:00
repliesContentSlice := tools.ReverseSlice(s)
repliesContentSlice = tools.RemoveNewlineElements(repliesContentSlice)
2024-06-01 23:48:00 +08:00
//跳过没有回复内容的工单。
if len(repliesContentSlice) == 0 {
return
}
2024-05-20 01:00:09 +08:00
repliesContentSlice = tools.AddNewlineToEachElement(repliesContentSlice)
2024-05-18 01:46:35 +08:00
2024-05-20 01:00:09 +08:00
//工单id转换为cloudid
2024-06-01 23:48:00 +08:00
L1:
cloudId, httpCode := filter.Id2CloudId(v)
if httpCode != http.StatusOK {
time.Sleep(time.Second * 10)
goto L1
2024-05-26 09:15:34 +08:00
2024-06-01 23:48:00 +08:00
} else {
if cloudId == "" {
contentMap[v] = repliesContentSlice
log.Printf("goroutine%v:回复内容处理完成工单id(非cloudid是%v\n)", j, v)
log.Printf("goroutine%v:UdeskId为%v,内容为:%v\n", j, v, contentMap[v])
} else {
contentMap[cloudId] = repliesContentSlice
log.Printf("goroutine%v:回复内容处理完成工单id(cloudid是%v\n)", j, cloudId)
log.Printf("goroutine%v:cloudId为%v,内容为:%v\n", j, cloudId, contentMap[cloudId])
}
2024-05-20 01:00:09 +08:00
}
log.Printf("goroutine%v结束\n", j)
}(v, j)
2024-05-18 01:46:35 +08:00
}
2024-05-20 01:00:09 +08:00
wg.Wait()
e.Reply.replyContent = contentMap
return e
2024-05-18 01:46:35 +08:00
}
2024-05-20 01:00:09 +08:00
func (e *Excel) CreateNewExcel() { // 示例的 map
2024-05-18 01:46:35 +08:00
// 创建一个新的 Excel 文件
f := excelize.NewFile()
// 设置工作表的名称
index, err := f.NewSheet("Sheet1")
if err != nil {
2024-05-19 00:27:10 +08:00
log.Println("设置工作表名称失败,错误是:", err)
2024-05-18 01:46:35 +08:00
return
}
// 在第一行,第一列写入标题
2024-06-01 23:48:00 +08:00
err = f.SetCellValue("Sheet1", "A1", "工单id或UDeskid")
2024-05-19 00:27:10 +08:00
if err != nil {
log.Println("写入A1标题失败错误是", err)
return
}
err = f.SetCellValue("Sheet1", "B1", "工单回复")
if err != nil {
log.Println("写入B1标题失败错误是", err)
return
}
2024-05-18 01:46:35 +08:00
//遍历 map将键和值写入 Excel 表格
row := 2
2024-05-20 01:00:09 +08:00
for key, value := range e.Reply.replyContent {
2024-05-18 01:46:35 +08:00
log.Println(row, key, value)
2024-05-19 00:27:10 +08:00
err := f.SetCellValue("Sheet1", "A"+strconv.Itoa(row), key)
if err != nil {
log.Println("插入A行数据到excel实例中失败错误是", err)
return
}
err = f.SetCellValue("Sheet1", "B"+strconv.Itoa(row), value)
if err != nil {
log.Println("插入B行数据到excel实例中失败错误是", err)
return
}
2024-05-18 01:46:35 +08:00
row++
}
// 设置工作表为默认激活状态
f.SetActiveSheet(index)
// 保存 Excel 文件
2024-06-01 23:48:00 +08:00
if err := f.SaveAs(e.ExcelName); err != nil {
2024-05-18 01:46:35 +08:00
log.Fatal(err)
}
wd, _ := os.Getwd()
2024-06-01 23:48:00 +08:00
relPath := filepath.Join(wd, e.ExcelName)
2024-05-20 01:00:09 +08:00
absPath, err := filepath.Abs(relPath)
if err != nil {
log.Printf("获取excel路径失败错误是:%v", err)
}
2024-05-18 01:46:35 +08:00
e.ExcelPath = absPath
2024-05-20 01:00:09 +08:00
address := option.FILESERVER_ADDRESS + e.ExcelName
2024-05-18 01:46:35 +08:00
e.ExcelAddress = address
2024-05-20 01:00:09 +08:00
2024-05-18 01:46:35 +08:00
}