1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246 |
- package knowledge
- import (
- "encoding/json"
- "errors"
- "eta_gn/eta_api/models/knowledge"
- "eta_gn/eta_api/models/knowledge_approve"
- "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 = knowledge.KnowledgeResourceStateUnpublished
- 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 = knowledge.KnowledgeResourceStateUnpublished
- 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 SetKnowledgeResourcePublic(knowledgeId int) (errMsg string, err error) {
- obj := new(knowledge.KnowledgeResource)
- obj, err = obj.GetById(knowledgeId)
- if err != nil {
- if utils.IsErrNoRow(err) {
- errMsg = "资源不存在"
- return
- }
- errMsg = "获取资源失败"
- err = fmt.Errorf("获取资源失败,Err:%v", err.Error())
- return
- }
- if !(obj.State == knowledge.KnowledgeResourceStateUnpublished || obj.State == knowledge.KnowledgeResourceStateRejected) {
- errMsg = "不能重复设置公共"
- err = fmt.Errorf("不能重复设置公共")
- return
- }
- if obj.ResourceType != knowledge.KnowledgeResourceTypeOpinion {
- errMsg = "非法操作"
- err = fmt.Errorf("非法操作")
- return
- }
- flowOb := new(knowledge_approve.KnowledgeResourceApproveFlow)
- flowCond := fmt.Sprintf(` AND %s = ?`, knowledge_approve.KnowledgeResourceApproveFlowCols.ClassifyId)
- flowPars := make([]interface{}, 0)
- flowPars = append(flowPars, obj.ClassifyId)
- flowItem, e := flowOb.GetItemByCondition(flowCond, flowPars, "")
- var opening bool
- if e != nil {
- if utils.IsErrNoRow(e) {
- opening = false
- return
- }
- err = fmt.Errorf("ApproveFlow GetItemByCondition err: %s", e.Error())
- return
- }
- if flowItem != nil && flowItem.KnowledgeResourceApproveFlowId > 0 {
- opening = true
- }
- if !opening {
- errMsg = "分类未开启审批流"
- err = fmt.Errorf("分类未开启审批流")
- return
- }
- obj.State = knowledge.KnowledgeResourceStatePending
- obj.ModifyTime = time.Now()
- err = obj.Update([]string{"state", "modify_time"})
- if err != nil {
- errMsg = "设置失败"
- err = fmt.Errorf("设置失败,Err:" + err.Error())
- return
- }
- 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
- }
|