package knowledge import ( "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" "github.com/tealeg/xlsx" "html" "strconv" "strings" "time" ) 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 } //todo 是否需要保存到es中 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 } 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 }