12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187 |
- package knowledge
- import (
- "encoding/json"
- "errors"
- "eta_gn/eta_api/models/knowledge"
- "eta_gn/eta_api/models/system"
- "eta_gn/eta_api/services"
- "eta_gn/eta_api/services/alarm_msg"
- "eta_gn/eta_api/utils"
- "fmt"
- "html"
- "strconv"
- "strings"
- "time"
- "github.com/go-redis/redis/v8"
- "github.com/rdlucklib/rdluck_tools/paging"
- "github.com/tealeg/xlsx"
- )
- func AddResource(req *knowledge.AddReq, sysUser *system.Admin) (item *knowledge.KnowledgeResource, err error, errMsg string) {
- var startTime time.Time
- var endTime time.Time
- if req.StartTime != "" {
- if req.ResourceType == knowledge.KnowledgeResourceTypeEvent || req.ResourceType == knowledge.KnowledgeResourceTypePolicy {
- startTime, err = time.ParseInLocation(utils.FormatDateTime, req.StartTime, time.Local)
- if err != nil { // 如果时间格式不正确,则返回错误信息
- errMsg = "开始时间格式不正确"
- err = fmt.Errorf("开始时间格式不正确,Err:" + err.Error())
- return
- }
- } else {
- startTime, err = time.ParseInLocation(utils.FormatDate, req.StartTime, time.Local)
- if err != nil { // 如果时间格式不正确,则返回错误信息
- errMsg = "开始时间格式不正确"
- err = fmt.Errorf("开始时间格式不正确,Err:" + err.Error())
- return
- }
- }
- }
- if req.EndTime != "" {
- if req.ResourceType == knowledge.KnowledgeResourceTypeEvent || req.ResourceType == knowledge.KnowledgeResourceTypePolicy {
- endTime, err = time.ParseInLocation(utils.FormatDateTime, req.EndTime, time.Local)
- if err != nil { // 如果时间格式不正确,则返回错误信息
- errMsg = "结束时间格式不正确"
- err = fmt.Errorf("结束时间格式不正确,Err:" + err.Error())
- return
- }
- }
- }
- isFile := 0
- if req.Content != "" {
- e := utils.ContentXssCheck(req.Content)
- if e != nil {
- errMsg = "存在非法标签"
- err = fmt.Errorf("存在非法标签, Err: " + e.Error())
- return
- }
- content, e := services.FilterReportContentBr(req.Content)
- if e != nil {
- errMsg = "内容去除前后空格失败"
- err = fmt.Errorf("内容去除前后空格失败, Err: " + e.Error())
- return
- }
- req.Content = content
- /*contentSub, err = services.GetReportContentSub(req.Content)
- if err != nil {
- go alarm_msg.SendAlarmMsg("ContentSub 失败,Err:"+err.Error(), 3)
- //utils.SendEmail(utils.APPNAME+"失败提醒", "解析 ContentSub 失败,Err:"+err.Error(), utils.EmailSendToUsers)
- return
- }*/
- } else {
- if req.FileUrl == "" {
- errMsg = "内容或文件不能为空"
- err = fmt.Errorf("内容或文件不能为空")
- return
- }
- isFile = 1
- }
- classifyObj := new(knowledge.KnowledgeClassify)
- classifyItem, err := classifyObj.GetClassifyById(req.ClassifyId)
- if err != nil {
- if utils.IsErrNoRow(err) {
- err = fmt.Errorf("分类不存在,Err:" + err.Error())
- errMsg = "分类不存在"
- return
- }
- errMsg = "获取分类失败"
- err = fmt.Errorf("获取分类失败,Err:" + err.Error())
- return
- }
- if req.ClassifyId != classifyItem.ClassifyId { // 如果传进来的分类id和数据库中查到的分类id不一致,说明传进来的分类id是错误的
- errMsg = "错误分类"
- err = fmt.Errorf("错误分类")
- return
- }
- // 校验标签
- if req.TagId > 0 {
- tagObj := new(knowledge.KnowledgeTag)
- _, e := tagObj.GetTagById(req.TagId)
- if e != nil {
- if utils.IsErrNoRow(e) {
- err = fmt.Errorf("标签不存在,Err:" + e.Error())
- errMsg = "标签不存在"
- return
- }
- err = fmt.Errorf("获取标签失败,Err:" + e.Error())
- return
- }
- }
- item = new(knowledge.KnowledgeResource)
- item.ClassifyId = req.ClassifyId
- item.TagId = req.TagId
- item.SourceFrom = req.SourceFrom
- item.ResourceType = req.ResourceType
- item.Title = req.Title
- item.State = 1
- item.Content = html.EscapeString(req.Content)
- timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
- item.ResourceCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
- item.IsFile = isFile
- item.FileUrl = req.FileUrl
- //todo 内容前5行
- //item.ContentSub = html.EscapeString(contentSub)
- if !startTime.IsZero() {
- item.StartTime = &startTime
- } else {
- item.StartTime = nil
- }
- if !endTime.IsZero() {
- item.EndTime = &endTime
- } else {
- item.EndTime = nil
- }
- item.ModifyTime = time.Now()
- item.AdminId = sysUser.AdminId
- item.AdminRealName = sysUser.RealName
- //item.LastModifyAdminId = sysUser.AdminId
- //item.LastModifyAdminName = sysUser.RealName
- item.ModifyTime = time.Now()
- item.CreateTime = time.Now()
- err = item.Add(item)
- if err != nil {
- errMsg = "保存失败"
- err = fmt.Errorf("保存失败,Err:" + err.Error())
- return
- }
- // 保存到es中
- go EsAddOrEditKnowledgeResource(item)
- return
- }
- func EditResource(resourceInfo *knowledge.KnowledgeResource, req knowledge.EditReq, sysUser *system.Admin) (err error, errMsg string) {
- errMsg = `保存失败`
- var startTime time.Time
- var endTime time.Time
- if req.StartTime != "" {
- if req.ResourceType == knowledge.KnowledgeResourceTypeEvent || req.ResourceType == knowledge.KnowledgeResourceTypePolicy {
- startTime, err = time.ParseInLocation(utils.FormatDateTime, req.StartTime, time.Local)
- if err != nil { // 如果时间格式不正确,则返回错误信息
- errMsg = "开始时间格式不正确"
- err = fmt.Errorf("开始时间格式不正确,Err:" + err.Error())
- return
- }
- } else {
- startTime, err = time.ParseInLocation(utils.FormatDate, req.StartTime, time.Local)
- if err != nil { // 如果时间格式不正确,则返回错误信息
- errMsg = "开始时间格式不正确"
- err = fmt.Errorf("开始时间格式不正确,Err:" + err.Error())
- return
- }
- }
- }
- if req.EndTime != "" {
- if req.ResourceType == knowledge.KnowledgeResourceTypeEvent || req.ResourceType == knowledge.KnowledgeResourceTypePolicy {
- endTime, err = time.ParseInLocation(utils.FormatDateTime, req.EndTime, time.Local)
- if err != nil { // 如果时间格式不正确,则返回错误信息
- errMsg = "结束时间格式不正确"
- err = fmt.Errorf("结束时间格式不正确,Err:" + err.Error())
- return
- }
- }
- }
- if resourceInfo.IsFile == 0 {
- if req.Content == "" {
- errMsg = "内容不能为空"
- err = fmt.Errorf("内容不能为空")
- return
- }
- } else if resourceInfo.IsFile == 1 {
- if req.FileUrl == "" {
- errMsg = "文件地址不能为空"
- err = fmt.Errorf("文件地址不能为空")
- return
- }
- }
- if req.Content != "" {
- e := utils.ContentXssCheck(req.Content)
- if e != nil {
- errMsg = "存在非法标签"
- err = fmt.Errorf("存在非法标签, Err: " + e.Error())
- return
- }
- content, e := services.FilterReportContentBr(req.Content)
- if e != nil {
- errMsg = "内容去除前后空格失败"
- err = fmt.Errorf("内容去除前后空格失败, Err: " + e.Error())
- return
- }
- req.Content = content
- /*contentSub, err = services.GetReportContentSub(req.Content)
- if err != nil {
- go alarm_msg.SendAlarmMsg("ContentSub 失败,Err:"+err.Error(), 3)
- //utils.SendEmail(utils.APPNAME+"失败提醒", "解析 ContentSub 失败,Err:"+err.Error(), utils.EmailSendToUsers)
- return
- }*/
- }
- classifyObj := new(knowledge.KnowledgeClassify)
- classifyItem, err := classifyObj.GetClassifyById(req.ClassifyId)
- if err != nil {
- if utils.IsErrNoRow(err) {
- err = fmt.Errorf("分类不存在,Err:" + err.Error())
- errMsg = "分类不存在"
- return
- }
- errMsg = "获取分类失败"
- err = fmt.Errorf("获取分类失败,Err:" + err.Error())
- return
- }
- if req.ClassifyId != classifyItem.ClassifyId { // 如果传进来的分类id和数据库中查到的分类id不一致,说明传进来的分类id是错误的
- errMsg = "错误分类"
- err = fmt.Errorf("错误分类")
- return
- }
- if req.TagId > 0 {
- tagObj := new(knowledge.KnowledgeTag)
- _, e := tagObj.GetTagById(req.TagId)
- if e != nil {
- if utils.IsErrNoRow(e) {
- err = fmt.Errorf("标签不存在,Err:" + e.Error())
- errMsg = "标签不存在"
- return
- }
- err = fmt.Errorf("获取标签失败,Err:" + e.Error())
- return
- }
- }
- resourceInfo.ClassifyId = req.ClassifyId
- resourceInfo.SourceFrom = req.SourceFrom
- resourceInfo.TagId = req.TagId
- resourceInfo.Title = req.Title
- if req.Content != "" {
- resourceInfo.Content = html.EscapeString(req.Content)
- }
- if !startTime.IsZero() {
- resourceInfo.StartTime = &startTime
- }
- if !endTime.IsZero() {
- resourceInfo.EndTime = &endTime
- }
- resourceInfo.FileUrl = req.FileUrl
- //resourceInfo.LastModifyAdminId = sysUser.AdminId
- //resourceInfo.LastModifyAdminName = sysUser.RealName
- resourceInfo.ModifyTime = time.Now()
- updateCols := []string{"ClassifyId", "SourceFrom", "TagId", "Title", "Content", "StartTime", "EndTime", "ModifyTime", "FileUrl"}
- // 修改报告的基本信息,以及报告的授权用户
- err = resourceInfo.Update(updateCols)
- if err != nil {
- return
- }
- go EsAddOrEditKnowledgeResource(resourceInfo)
- return
- }
- // ImportResourceData
- // @Description: 数据导入
- // @author: Roc
- // @datetime 2024-08-01 11:27:21
- // @param path string
- // @param sysUser *system.Admin
- // @return successCount int
- // @return failCount int
- // @return err error
- // @return errMsg string
- func ImportResourceData(path string, resourceType int, sysUser *system.Admin) (successCount, failCount int, err error, errMsg string) {
- // 错误信息
- errMsgList := make([]string, 0)
- defer func() {
- // 错误信息记录
- if len(errMsgList) > 0 {
- utils.FileLog.Info("导入失败, errMsgList: %v", strings.Join(errMsgList, "\n"))
- }
- }()
- errMsg = `导入失败`
- xlFile, err := xlsx.OpenFile(path)
- if err != nil {
- fmt.Println(err.Error())
- return
- }
- if len(xlFile.Sheets) <= 0 {
- errMsg = "导入模板异常"
- err = errors.New(errMsg)
- return
- }
- //导入失败数据
- failDataList := make([]*knowledge.KnowledgeImportFail, 0)
- var indexDataList []knowledge.ResourceImportData
- tagObj := new(knowledge.KnowledgeTag)
- // 模板校验,然后处理成标准化格式
- for _, sheet := range xlFile.Sheets {
- var tmpFailDataList []*knowledge.KnowledgeImportFail
- var tmpIndexDataList []knowledge.ResourceImportData
- rowList := sheet.Rows
- if len(rowList) <= 0 {
- errMsg = sheet.Name + "页异常"
- err = errors.New(errMsg)
- return
- }
- if resourceType == knowledge.KnowledgeResourceTypeEvent || resourceType == knowledge.KnowledgeResourceTypePolicy {
- tmpIndexDataList, tmpFailDataList, err, errMsg = getDataByTemplateEvent(sheet, sysUser.AdminId, resourceType)
- } else {
- tmpIndexDataList, tmpFailDataList, err, errMsg = getDataByTemplatePoint(sheet, sysUser.AdminId, resourceType)
- }
- if err != nil {
- return
- }
- fmt.Println("tmpIndexDataList: ", tmpIndexDataList)
- for _, v := range tmpFailDataList {
- fmt.Println("tmpFailDataList: ", v)
- }
- indexDataList = append(indexDataList, tmpIndexDataList...)
- failDataList = append(failDataList, tmpFailDataList...)
- }
- if len(indexDataList) <= 0 {
- if len(failDataList) > 0 {
- errMsg = failDataList[0].Remark
- err = errors.New(errMsg)
- }
- return
- }
- // 遍历获取所有分类
- classifyNames := make([]string, 0)
- for _, v := range indexDataList {
- if v.ClassifyFirstName != "" {
- classifyNames = append(classifyNames, v.ClassifyFirstName)
- }
- if v.ClassifySecondName != "" {
- classifyNames = append(classifyNames, v.ClassifySecondName)
- }
- if v.ClassifyThirdName != "" {
- classifyNames = append(classifyNames, v.ClassifyThirdName)
- }
- }
- fmt.Println(classifyNames)
- classifyList, err := knowledge.GetFullClassifyListByName(classifyNames, resourceType)
- if err != nil {
- err = fmt.Errorf("获取分类数据失败 Err: %s", err.Error())
- errMsg = "获取分类数据失败"
- return
- }
- classifyObj := new(knowledge.KnowledgeClassify)
- classifyAllList, err := classifyObj.GetAllClassify(resourceType)
- if err != nil {
- err = fmt.Errorf("获取分类数据失败 Err: %s", err.Error())
- errMsg = "获取分类数据失败"
- return
- }
- classifyNameMap := make(map[string]int)
- classifyHasChildMap := make(map[int]bool)
- for _, v := range classifyAllList {
- if v.ParentId > 0 {
- classifyHasChildMap[v.ParentId] = true
- }
- }
- for _, v := range classifyList {
- name := fmt.Sprintf("%s/%s/%s", v.RootName, v.ParentName, v.ClassifyName)
- fmt.Println("name", name)
- if v.RootName == "" && v.ParentName == "" { //一级分类
- name = fmt.Sprintf("%s/%s/%s", v.ClassifyName, "", "")
- if _, ok := classifyHasChildMap[v.ClassifyId]; ok {
- continue
- }
- } else if v.RootName == "" { //二级分类
- name = fmt.Sprintf("%s/%s/%s", v.ParentName, v.ClassifyName, "")
- if _, ok := classifyHasChildMap[v.ClassifyId]; ok {
- continue
- }
- }
- classifyNameMap[name] = v.ClassifyId
- }
- tagNameMap := make(map[string]int)
- tagList, err := tagObj.GetAllTag(resourceType)
- for _, v := range tagList {
- tagNameMap[v.TagName] = v.TagId
- }
- for _, v := range indexDataList {
- errTip := ""
- classifyId := 0
- if successCount > 100 {
- errTip = "导入数量超过100条"
- } else if v.Title == "" {
- errTip = "标题未填"
- } else if len(v.Title) > 600 {
- errTip = "标题过长"
- } else if v.Content == "" {
- errTip = "正文未填"
- } else if v.SourceFrom == "" {
- errTip = "来源未填"
- } else if v.ClassifyFirstName == "" {
- errTip = "分类未填"
- } else if v.StartDate == "" || v.StartTime == "" {
- if resourceType == knowledge.KnowledgeResourceTypeEvent || resourceType == knowledge.KnowledgeResourceTypePolicy {
- errTip = "开始时间未填"
- } else if v.StartDate == "" {
- errTip = "时间未填"
- }
- }
- if errTip == "" {
- // 判断分类是否存在
- classifyName := fmt.Sprintf("%s/%s/%s", v.ClassifyFirstName, v.ClassifySecondName, v.ClassifyThirdName)
- fmt.Println("classifyName: ", classifyName)
- classifyIdTmp, ok := classifyNameMap[classifyName]
- if !ok {
- errTip = "分类不存在"
- }
- classifyId = classifyIdTmp
- }
- if errTip != "" {
- failItem := &knowledge.KnowledgeImportFail{
- Title: v.Title,
- Content: v.Content,
- SourceFrom: v.SourceFrom,
- ClassifyFirst: v.ClassifyFirstName,
- ClassifySecond: v.ClassifySecondName,
- ClassifyThird: v.ClassifyThirdName,
- ResourceType: resourceType,
- StartDate: v.StartDate,
- EndDate: v.EndDate,
- StartTime: v.StartTime,
- EndTime: v.EndTime,
- Remark: errTip,
- SysUserId: fmt.Sprint(sysUser.AdminId),
- CreateTime: time.Now(),
- }
- failDataList = append(failDataList, failItem)
- continue
- }
- tagId, _ := tagNameMap[v.Tag]
- req := new(knowledge.AddReq)
- req.Title = v.Title
- req.Content = v.Content
- req.SourceFrom = v.SourceFrom
- req.ClassifyId = classifyId
- req.TagId = tagId
- req.ResourceType = resourceType
- if v.StartDate != "" {
- if resourceType == knowledge.KnowledgeResourceTypeEvent || resourceType == knowledge.KnowledgeResourceTypePolicy {
- req.StartTime = fmt.Sprintf("%s %s", v.StartDate, v.StartTime)
- } else {
- req.StartTime = v.StartDate
- }
- }
- if v.EndDate != "" {
- if resourceType == knowledge.KnowledgeResourceTypeEvent || resourceType == knowledge.KnowledgeResourceTypePolicy {
- req.EndTime = fmt.Sprintf("%s %s", v.EndDate, v.EndTime)
- }
- }
- tmpErr, msg := addExcelResource(req, sysUser)
- if tmpErr != nil {
- failItem := &knowledge.KnowledgeImportFail{
- Title: v.Title,
- Content: v.Content,
- SourceFrom: v.SourceFrom,
- ClassifyFirst: v.ClassifyFirstName,
- ClassifySecond: v.ClassifySecondName,
- ClassifyThird: v.ClassifyThirdName,
- ResourceType: resourceType,
- StartDate: v.StartDate,
- EndDate: v.EndDate,
- StartTime: v.StartTime,
- EndTime: v.EndTime,
- Remark: "新增事件失败:" + msg,
- SysUserId: fmt.Sprint(sysUser.AdminId),
- CreateTime: time.Now(),
- }
- failDataList = append(failDataList, failItem)
- continue
- } else {
- successCount++
- }
- }
- // 失败数量
- failCount = len(failDataList)
- //fmt.Println("failDataList:", len(failDataList))
- if failCount > 0 {
- failObj := new(knowledge.KnowledgeImportFail)
- //先删除导入失败记录
- _ = failObj.Delete(sysUser.AdminId, resourceType)
- // 批量添加导入失败记录
- err = failObj.MultiAdd(failDataList)
- if err != nil {
- go alarm_msg.SendAlarmMsg("导入数据 新增失败记录失败,Err:"+err.Error(), 3)
- }
- {
- //错误信息字符串切片,最后作为发送邮件通知使用
- failContents := make([]string, 0)
- for _, v := range failDataList {
- failContents = append(failContents, fmt.Sprint(v.Title, "导入失败:", v.Remark))
- }
- utils.FileLog.Info("导入数据 存在部分数据导入失败:" + strings.Join(failContents, ";"))
- //导入失败的话,最后统一邮件提醒就好啦,不需要每次都去提醒
- go alarm_msg.SendAlarmMsg("导入数据 存在部分数据导入失败:"+strings.Join(failContents, ";"), 3)
- }
- }
- return
- }
- // getDataByTemplateEvent
- func getDataByTemplateEvent(sheet *xlsx.Sheet, sysUserId, resourceType int) (indexDataList []knowledge.ResourceImportData, failDataList []*knowledge.KnowledgeImportFail, err error, errMsg string) {
- fmt.Println("sheet name: ", sheet.Name)
- indexDataList = make([]knowledge.ResourceImportData, 0)
- failDataList = make([]*knowledge.KnowledgeImportFail, 0)
- //遍历行读取
- maxRow := sheet.MaxRow
- fmt.Println("maxRow:", maxRow)
- // 表头信息
- if maxRow <= 3 {
- errMsg = "模板异常1"
- err = errors.New(errMsg)
- return
- }
- // 表头校验
- {
- headerRow := sheet.Row(1)
- cells := headerRow.Cells
- fmt.Println("cells:", len(cells))
- if len(cells) < 11 {
- errMsg = "导入文件异常,请下载最新导入模板文件"
- err = errors.New(errMsg)
- return
- }
- // 循环打印每个单元格
- for k, v := range cells {
- fmt.Println("第", k, "个单元格", v.Value)
- }
- templateFail := false
- if cells[0].Value != "开始时间" {
- templateFail = true
- }
- if cells[2].Value != "结束时间(非必填)" {
- templateFail = true
- }
- if cells[4].Value != "标题" {
- templateFail = true
- }
- if cells[5].Value != "正文" {
- templateFail = true
- }
- if cells[6].Value != "来源" {
- templateFail = true
- }
- if cells[7].Value != "一级分类" {
- templateFail = true
- }
- if cells[8].Value != "二级分类" {
- templateFail = true
- }
- if cells[9].Value != "三级分类" {
- templateFail = true
- }
- if cells[10].Value != "标签" {
- templateFail = true
- }
- headerRow2 := sheet.Row(2)
- cells2 := headerRow2.Cells
- for k, v := range cells2 {
- fmt.Println("2第", k, "个单元格", v.Value)
- }
- if len(cells2) < 11 {
- templateFail = true
- } else {
- if cells2[0].Value != "日期" {
- templateFail = true
- }
- if cells2[1].Value != "时间" {
- templateFail = true
- }
- if cells2[2].Value != "日期" {
- templateFail = true
- }
- if cells2[3].Value != "时间" {
- templateFail = true
- }
- }
- if templateFail {
- fmt.Println("头部信息校验失败")
- errMsg = "导入文件异常,请下载最新导入模板文件"
- err = errors.New(errMsg)
- return
- }
- }
- for i := 3; i < maxRow; i++ {
- row := sheet.Row(i)
- cells := row.Cells
- lenCell := len(cells)
- // 过滤空白行
- if lenCell <= 0 {
- continue
- }
- var startDateO, endDateO, startTimeO, endTimeO string
- var startDate, startTime, endDate, endTime string
- var title, content, sourceFrom, classifyName1, classifyName2, classifyName3, tag string
- for k, v := range cells {
- fmt.Println("数据第行", i, "第", k, "个单元格", v.Value)
- if k == 0 {
- startDate = strings.TrimSpace(cells[0].Value) //开始时间
- startDateO = startDate
- } else if k == 1 {
- startTime = strings.TrimSpace(cells[1].Value) //开始时间
- startTimeO = startTime
- } else if k == 2 {
- endDate = strings.TrimSpace(cells[2].Value) //结束时间
- endDateO = endDate
- } else if k == 3 {
- endTime = strings.TrimSpace(cells[3].Value) //结束时间
- endTimeO = endTime
- } else if k == 4 {
- title = strings.TrimSpace(cells[4].Value) //标题
- } else if k == 5 {
- content = strings.TrimSpace(cells[5].Value) //正文
- } else if k == 6 {
- sourceFrom = strings.TrimSpace(cells[6].Value) //来源
- } else if k == 7 {
- classifyName1 = strings.TrimSpace(cells[7].Value) //一级分类
- } else if k == 8 {
- classifyName2 = strings.TrimSpace(cells[8].Value) //二级分类
- } else if k == 9 {
- classifyName3 = strings.TrimSpace(cells[9].Value) //三级分类
- } else if k == 10 {
- tag = strings.TrimSpace(cells[10].Value) //标签
- }
- }
- if title == "" && startDate == "" && startTime == "" && endTime == "" && endDate == "" && content == "" && tag == "" && sourceFrom == "" && classifyName1 == "" && classifyName2 == "" && classifyName3 == "" { //过滤空白行
- continue
- }
- fmt.Println("lenCell:", lenCell)
- // 校验日期格式
- var startDateVal, endDateVal, startTimeVal float64
- var tmpErr error
- if startDate != "" {
- // 判断9:09:14日期格式是否正确
- // 判断是否是数字
- startDateVal, tmpErr = strconv.ParseFloat(startDate, 64)
- if tmpErr == nil {
- startDate = xlsx.TimeFromExcelTime(startDateVal, false).Format(utils.FormatDate)
- fmt.Println(startDate)
- } else {
- startDate, tmpErr = getExcelDate(startDate)
- if tmpErr != nil {
- failDataList = append(failDataList, &knowledge.KnowledgeImportFail{
- //Id: 0,
- Title: title,
- Content: content,
- SourceFrom: sourceFrom,
- ClassifyFirst: classifyName1,
- ClassifySecond: classifyName2,
- ClassifyThird: classifyName3,
- ResourceType: resourceType,
- StartDate: startDateO,
- EndDate: endDateO,
- StartTime: startTimeO,
- EndTime: endTimeO,
- Remark: "日期格式异常",
- SysUserId: strconv.Itoa(sysUserId),
- CreateTime: time.Now(),
- })
- continue
- }
- }
- if startTime != "" {
- // 判断9:09:14日期格式是否正确
- startTimeVal, tmpErr = strconv.ParseFloat(startTime, 64)
- if tmpErr == nil {
- startTimeVal = startDateVal + startTimeVal
- startTime = xlsx.TimeFromExcelTime(startTimeVal, false).Format(utils.FormatTime)
- fmt.Println(startTime)
- } else {
- _, tmpErr = time.Parse(utils.FormatDateTime, startDate+" "+startTime)
- if tmpErr != nil {
- failDataList = append(failDataList, &knowledge.KnowledgeImportFail{
- //Id: 0,
- Title: title,
- Content: content,
- SourceFrom: sourceFrom,
- ClassifyFirst: classifyName1,
- ClassifySecond: classifyName2,
- ClassifyThird: classifyName3,
- ResourceType: resourceType,
- StartDate: startDateO,
- EndDate: endDateO,
- StartTime: startTimeO,
- EndTime: endTimeO,
- Remark: "日期格式异常",
- SysUserId: strconv.Itoa(sysUserId),
- CreateTime: time.Now(),
- })
- continue
- }
- }
- }
- }
- if endDate != "" {
- endDateVal, tmpErr = strconv.ParseFloat(endDate, 64)
- if tmpErr == nil {
- endDate = xlsx.TimeFromExcelTime(endDateVal, false).Format(utils.FormatDate)
- fmt.Println(endDate)
- } else {
- endDate, tmpErr = getExcelDate(endDate)
- if tmpErr != nil {
- failDataList = append(failDataList, &knowledge.KnowledgeImportFail{
- //Id: 0,
- Title: title,
- Content: content,
- SourceFrom: sourceFrom,
- ClassifyFirst: classifyName1,
- ClassifySecond: classifyName2,
- ClassifyThird: classifyName3,
- ResourceType: resourceType,
- StartDate: startDateO,
- EndDate: endDateO,
- StartTime: startTimeO,
- EndTime: endTimeO,
- Remark: "日期格式异常",
- SysUserId: strconv.Itoa(sysUserId),
- CreateTime: time.Now(),
- })
- continue
- }
- }
- if endTime != "" {
- // 判断9:09:14日期格式是否正确
- endTimeVal, e := strconv.ParseFloat(endTime, 64)
- if e == nil {
- endTimeVal = endDateVal + endTimeVal
- endTime = xlsx.TimeFromExcelTime(endTimeVal, false).Format(utils.FormatTime)
- fmt.Println(endTime)
- } else {
- _, e = time.Parse(utils.FormatDateTime, endDate+" "+endTime)
- if e != nil {
- failDataList = append(failDataList, &knowledge.KnowledgeImportFail{
- //Id: 0,
- Title: title,
- Content: content,
- SourceFrom: sourceFrom,
- ClassifyFirst: classifyName1,
- ClassifySecond: classifyName2,
- ClassifyThird: classifyName3,
- ResourceType: resourceType,
- StartDate: startDate,
- EndDate: endDate,
- StartTime: startTimeO,
- EndTime: endTimeO,
- Remark: "日期格式异常",
- SysUserId: strconv.Itoa(sysUserId),
- CreateTime: time.Now(),
- })
- continue
- }
- }
- }
- }
- resourceItem := knowledge.ResourceImportData{
- Title: title,
- StartDate: startDate,
- EndDate: endDate,
- StartTime: startTime,
- EndTime: endTime,
- Content: content,
- Tag: tag,
- SourceFrom: sourceFrom,
- ClassifyFirstName: classifyName1,
- ClassifySecondName: classifyName2,
- ClassifyThirdName: classifyName3,
- }
- indexDataList = append(indexDataList, resourceItem)
- }
- return
- }
- // getDataByTemplatePoint
- func getDataByTemplatePoint(sheet *xlsx.Sheet, sysUserId, resourceType int) (indexDataList []knowledge.ResourceImportData, failDataList []*knowledge.KnowledgeImportFail, err error, errMsg string) {
- fmt.Println("sheet name: ", sheet.Name)
- indexDataList = make([]knowledge.ResourceImportData, 0)
- failDataList = make([]*knowledge.KnowledgeImportFail, 0)
- //遍历行读取
- maxRow := sheet.MaxRow
- fmt.Println("maxRow:", maxRow)
- // 表头信息
- if maxRow <= 3 {
- errMsg = "模板异常1"
- err = errors.New(errMsg)
- return
- }
- // 表头校验
- {
- headerRow := sheet.Row(1)
- cells := headerRow.Cells
- fmt.Println("cells:", len(cells))
- if len(cells) < 5 {
- errMsg = "导入文件异常,请下载最新导入模板文件"
- err = errors.New(errMsg)
- return
- }
- // 循环打印每个单元格
- for k, v := range cells {
- fmt.Println("第", k, "个单元格", v.Value)
- }
- templateFail := false
- if cells[0].Value != "时间" {
- templateFail = true
- }
- if cells[1].Value != "标题" {
- templateFail = true
- }
- if cells[2].Value != "正文" {
- templateFail = true
- }
- if cells[3].Value != "来源" {
- templateFail = true
- }
- if cells[4].Value != "一级分类" {
- templateFail = true
- }
- if cells[5].Value != "二级分类" {
- templateFail = true
- }
- if cells[6].Value != "三级分类" {
- templateFail = true
- }
- if cells[7].Value != "标签" {
- templateFail = true
- }
- if templateFail {
- fmt.Println("头部信息校验失败")
- errMsg = "导入文件异常,请下载最新导入模板文件"
- err = errors.New(errMsg)
- return
- }
- }
- for i := 2; i < maxRow; i++ {
- row := sheet.Row(i)
- cells := row.Cells
- lenCell := len(cells)
- // 过滤空白行
- if lenCell <= 0 {
- continue
- }
- var startDateO string
- var startDate string
- var title, content, sourceFrom, classifyName1, classifyName2, classifyName3, tag string
- for k, v := range cells {
- fmt.Println("数据第行", i, "第", k, "个单元格", v.Value)
- if k == 0 {
- startDate = strings.TrimSpace(cells[0].Value) //开始时间
- startDateO = startDate
- } else if k == 1 {
- title = strings.TrimSpace(cells[1].Value) //标题
- } else if k == 2 {
- content = strings.TrimSpace(cells[2].Value) //正文
- } else if k == 3 {
- sourceFrom = strings.TrimSpace(cells[3].Value) //来源
- } else if k == 4 {
- classifyName1 = strings.TrimSpace(cells[4].Value) //一级分类
- } else if k == 5 {
- classifyName2 = strings.TrimSpace(cells[5].Value) //二级分类
- } else if k == 6 {
- classifyName3 = strings.TrimSpace(cells[6].Value) //三级分类
- } else if k == 7 {
- tag = strings.TrimSpace(cells[7].Value) //标签
- }
- }
- if title == "" && startDate == "" && content == "" && tag == "" && sourceFrom == "" && classifyName1 == "" && classifyName2 == "" && classifyName3 == "" { //过滤空白行
- continue
- }
- // 校验日期格式
- if startDate != "" {
- // 判断9:09:14日期格式是否正确
- // 判断是否是数字
- startDateVal, tmpErr := strconv.ParseFloat(startDate, 64)
- if tmpErr == nil {
- startDate = xlsx.TimeFromExcelTime(startDateVal, false).Format(utils.FormatDate)
- fmt.Println(startDate)
- } else {
- startDate, tmpErr = getExcelDate(startDate)
- if tmpErr != nil {
- failDataList = append(failDataList, &knowledge.KnowledgeImportFail{
- //Id: 0,
- Title: title,
- Content: content,
- SourceFrom: sourceFrom,
- ClassifyFirst: classifyName1,
- ClassifySecond: classifyName2,
- ClassifyThird: classifyName3,
- ResourceType: resourceType,
- StartDate: startDateO,
- Remark: "日期格式异常",
- SysUserId: strconv.Itoa(sysUserId),
- CreateTime: time.Now(),
- })
- continue
- }
- }
- }
- resourceItem := knowledge.ResourceImportData{
- Title: title,
- StartDate: startDate,
- Content: content,
- Tag: tag,
- SourceFrom: sourceFrom,
- ClassifyFirstName: classifyName1,
- ClassifySecondName: classifyName2,
- ClassifyThirdName: classifyName3,
- }
- indexDataList = append(indexDataList, resourceItem)
- }
- return
- }
- func getExcelDate(createDate string) (newCreateDate string, err error) {
- if strings.Contains(createDate, "-") {
- //如果是带有 - 的普通日期格式文本
- _, err = time.Parse("2006-1-2", createDate)
- if err == nil {
- newCreateDate = createDate
- }
- } else if strings.Contains(createDate, "/") {
- //如果是带有 / 的普通日期格式文本
- createDateTime, timeErr := time.Parse("2006/1/2", createDate)
- if timeErr != nil {
- err = timeErr
- } else {
- newCreateDate = createDateTime.Format("2006-01-02")
- }
- } else {
- //可能是excel的日期格式
- _, tmpErr := strconv.Atoi(createDate)
- if tmpErr != nil {
- err = tmpErr
- } else {
- newCreateDate = utils.ConvertToFormatDay(createDate) //录入日期
- }
- }
- return
- }
- func addExcelResource(req *knowledge.AddReq, sysUser *system.Admin) (err error, errMsg string) {
- var startTime time.Time
- var endTime time.Time
- if req.StartTime != "" {
- if req.ResourceType == knowledge.KnowledgeResourceTypeEvent || req.ResourceType == knowledge.KnowledgeResourceTypePolicy {
- startTime, err = time.ParseInLocation(utils.FormatDateTime, req.StartTime, time.Local)
- if err != nil { // 如果时间格式不正确,则返回错误信息
- errMsg = "开始时间格式不正确"
- err = fmt.Errorf("开始时间格式不正确,Err:" + err.Error())
- return
- }
- } else {
- startTime, err = time.ParseInLocation(utils.FormatDate, req.StartTime, time.Local)
- if err != nil { // 如果时间格式不正确,则返回错误信息
- errMsg = "开始时间格式不正确"
- err = fmt.Errorf("开始时间格式不正确,Err:" + err.Error())
- return
- }
- }
- }
- if req.EndTime != "" {
- if req.ResourceType == knowledge.KnowledgeResourceTypeEvent || req.ResourceType == knowledge.KnowledgeResourceTypePolicy {
- endTime, err = time.ParseInLocation(utils.FormatDateTime, req.EndTime, time.Local)
- if err != nil { // 如果时间格式不正确,则返回错误信息
- errMsg = "结束时间格式不正确"
- err = fmt.Errorf("结束时间格式不正确,Err:" + err.Error())
- return
- }
- }
- }
- item := new(knowledge.KnowledgeResource)
- item.ClassifyId = req.ClassifyId
- item.TagId = req.TagId
- item.SourceFrom = req.SourceFrom
- item.ResourceType = req.ResourceType
- item.Title = req.Title
- item.State = 1
- item.Content = html.EscapeString(req.Content)
- timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
- item.ResourceCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
- //todo 内容前5行
- //item.ContentSub = html.EscapeString(contentSub)
- if !startTime.IsZero() {
- item.StartTime = &startTime
- }
- if !endTime.IsZero() {
- item.EndTime = &endTime
- }
- item.ModifyTime = time.Now()
- item.AdminId = sysUser.AdminId
- item.AdminRealName = sysUser.RealName
- //item.LastModifyAdminId = sysUser.AdminId
- //item.LastModifyAdminName = sysUser.RealName
- item.ModifyTime = time.Now()
- item.CreateTime = time.Now()
- err = item.Add(item)
- if err != nil {
- errMsg = "保存失败"
- err = fmt.Errorf("保存失败,Err:" + err.Error())
- return
- }
- //todo 是否需要保存到es中
- return
- }
- func GetKnowledgeResourceTmpList(dashboardDetailId, adminId int) (resp *knowledge.BiDashboardResourceAddResp, msg string, err error) {
- if dashboardDetailId <= 0 {
- data, er := utils.Rc.RedisBytes(utils.CACHE_KEY_BI_DASHBOARD_PREPARE_RESOURCE + strconv.Itoa(adminId))
- req := new(knowledge.BiDashboardResourceAddReq)
- if er != nil {
- if er == redis.Nil {
- msg = "暂无知识库"
- err = nil
- return
- }
- msg = "获取知识库模板列表失败"
- return
- }
- if err = json.Unmarshal(data, &req); err != nil {
- msg = "获取知识库模板列表失败"
- }
- selectedIds := make([]int, 0)
- for _, item := range req.KnowledgeResourceList {
- selectedIds = append(selectedIds, item.KnowledgeResourceId)
- }
- knowledgeList, er := knowledge.GetKnowledgeResourceByIds(selectedIds)
- if er != nil {
- msg = "获取知识库模板列表失败"
- return
- }
- resp = new(knowledge.BiDashboardResourceAddResp)
- resp.KnowledgeResourceList = toKnowledgeResourceList(knowledgeList)
- } else {
- condition := ` AND bi_dashboard_detail_id = ? `
- pars := []interface{}{dashboardDetailId}
- resourceIds, er := knowledge.GetKnowledgeResourceIdsByCondition(condition, pars)
- if er != nil {
- msg = "获取知识库模板列表失败"
- return
- }
- if len(resourceIds) == 0 {
- msg = "暂无知识库"
- return
- }
- resp = new(knowledge.BiDashboardResourceAddResp)
- knowledgeList, er := knowledge.GetKnowledgeResourceByIds(resourceIds)
- if er != nil {
- msg = "获取知识库模板列表失败"
- return
- }
- resp.BiDashboardDetailId = dashboardDetailId
- resp.KnowledgeResourceList = toKnowledgeResourceList(knowledgeList)
- }
- return
- }
- func GetKnowledgeResourceList(dashboardDetailId int) (resp *knowledge.KnowledgeResourceListResp, msg string, err error) {
- // var startSize int
- // if pageSize <= 0 {
- // pageSize = utils.PageSize20
- // }
- // if currentIndex <= 0 {
- // currentIndex = 1
- // }
- // startSize = utils.StartIndex(currentIndex, pageSize)
- condition := ` AND bi_dashboard_detail_id = ? `
- var pars = []interface{}{dashboardDetailId}
- resourceIds, err := knowledge.GetKnowledgeResourceIdsByCondition(condition, pars)
- if err != nil {
- msg = "获取知识库列表失败"
- return
- }
- if len(resourceIds) == 0 {
- resp = &knowledge.KnowledgeResourceListResp{
- List: make([]*knowledge.KnowledgeResourceList, 0),
- Paging: &paging.PagingItem{},
- }
- return
- }
- knowledgeList, err := knowledge.GetKnowledgeResourceByIds(resourceIds)
- if err != nil {
- msg = "获取知识库列表失败"
- return
- }
- resp = new(knowledge.KnowledgeResourceListResp)
- resp.List = toKnowledgeResourceList(knowledgeList)
- return
- }
- func toKnowledgeResourceList(list []*knowledge.KnowledgeResource) (res []*knowledge.KnowledgeResourceList) {
- for _, item := range list {
- resItem := &knowledge.KnowledgeResourceList{
- KnowledgeResourceId: item.KnowledgeResourceId,
- ResourceType: item.ResourceType,
- ClassifyId: item.ClassifyId,
- Title: item.Title,
- State: item.State,
- ResourceCode: item.ResourceCode,
- AdminId: item.AdminId,
- AdminRealName: item.AdminRealName,
- SourceFrom: item.SourceFrom,
- TagId: item.TagId,
- CreateTime: utils.TimeTransferString(utils.FormatDateTime, item.CreateTime),
- ModifyTime: utils.TimeTransferString(utils.FormatDateTime, item.ModifyTime),
- }
- if item.StartTime != nil {
- if item.ResourceType == knowledge.KnowledgeResourceTypeEvent || item.ResourceType == knowledge.KnowledgeResourceTypePolicy {
- resItem.StartTime = utils.TimeTransferString(utils.FormatDateTime, *item.StartTime)
- } else {
- resItem.StartTime = utils.TimeTransferString(utils.FormatDate, *item.StartTime)
- }
- }
- if item.EndTime != nil {
- if item.ResourceType == knowledge.KnowledgeResourceTypeEvent || item.ResourceType == knowledge.KnowledgeResourceTypePolicy {
- resItem.EndTime = utils.TimeTransferString(utils.FormatDateTime, *item.EndTime)
- } else {
- resItem.EndTime = utils.TimeTransferString(utils.FormatDate, *item.EndTime)
- }
- }
- res = append(res, resItem)
- }
- return
- }
|