package knowledge import ( "eta_gn/eta_report/models" "eta_gn/eta_report/services" "eta_gn/eta_report/utils" "fmt" "html" "io/ioutil" "mime/multipart" "os" "path" "strconv" "strings" "time" ) func ReportKnowledgeResourceAdd(title string, fileUrl []string, adminId int, adminRealName, classifyPath, sourceFrom string, pushTime time.Time) (msg string, err error) { classifyObj := new(models.KnowledgeClassify) var childClassifyId int if classifyPath != "" { classifyPathArr := strings.Split(classifyPath, "/") var classifyFirst, classifySecond, classifyThird string switch len(classifyPathArr) { case 1: classifyFirst = classifyPathArr[0] case 2: classifyFirst = classifyPathArr[0] classifySecond = classifyPathArr[1] case 3: classifyFirst = classifyPathArr[0] classifySecond = classifyPathArr[1] classifyThird = classifyPathArr[2] } childClassifyId, err = classifyObj.GetChildClassifyIdByNamePath(models.KnowledgeResourceTypeReport, classifyFirst, classifySecond, classifyThird) if err != nil { if utils.IsErrNoRow(err) { msg = "分类不存在,请检查知识资源-报告库中是否存在-" + classifyPath return } msg = "报告库添加失败" return } } else { msg = "分类不能为空" return } if childClassifyId == 0 { msg = "分类不存在,请检查知识资源-报告库中是否存在-" + classifyPath return } obj := new(models.KnowledgeResource) obj.ResourceType = models.KnowledgeResourceTypeReport obj.ClassifyId = childClassifyId obj.Title = title obj.CreateTime = time.Now() obj.ModifyTime = time.Now() obj.State = models.KnowledgeResourceStatePublished timestamp := strconv.FormatInt(time.Now().UnixNano(), 10) obj.ResourceCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp) obj.AdminId = adminId obj.AdminRealName = adminRealName obj.SourceFrom = sourceFrom obj.StartTime = &pushTime // 此处传入的url会在入库时存入knowledge_resource_file表中 if len(fileUrl) != 0 { obj.IsFile = 1 } err = obj.Create(fileUrl) if err != nil { return } return } func ReportKnowledgeResourceClassifyCheckAndSave(reportClassifyId int) (classifyId int, err error) { classifyObj := new(models.Classify) classifyObj, err = classifyObj.GetItemById(reportClassifyId) if err != nil { if !utils.IsErrNoRow(err) { return } } levelPath := classifyObj.LevelPath classifyIdStrs := strings.Split(levelPath, ",") classifyIds := make([]int, 0, len(classifyIdStrs)) for _, classifyIdStr := range classifyIdStrs { classifyId, _ := strconv.Atoi(classifyIdStr) classifyIds = append(classifyIds, classifyId) } classifyList, err := classifyObj.GetItemsByIds(classifyIds) if err != nil { return } knowledgeClassifyObj := new(models.KnowledgeClassify) var parentId int for i, classify := range classifyList { knowledgeClassifyObj, err = knowledgeClassifyObj.GetClassifyByNameTypeAndParentId(classify.ClassifyName, models.KnowledgeResourceTypeOpinion, parentId) if err != nil { if !utils.IsErrNoRow(err) { return } for j := i; j < len(classifyList); j++ { tmp := &models.KnowledgeClassify{ ClassifyName: classifyList[j].ClassifyName, Sort: 1, ParentId: parentId, CreateTime: time.Now(), ModifyTime: time.Now(), Enabled: 1, Level: classifyList[j].Level, ResourceType: models.KnowledgeResourceTypeOpinion, } err = tmp.Create() if err != nil { return } parentId = tmp.ClassifyId } classifyId = parentId return } parentId = knowledgeClassifyObj.ClassifyId } classifyId = parentId return } // ViewPointSave 知识资源观点库保存 func ViewPointSave(outId, adminId int, adminRealName, title, content, sourceFrom, classifyName string, classifyId, outSource int, startTime *time.Time) (msg string, err error) { if classifyId <= 0 { tmpClassifyId, er := ViewPointKnowledgeResourceClassifyCheckAndSave(classifyName, models.KnowledgeResourceTypeOpinion) if er != nil { msg = "观点保存失败" err = er return } classifyId = tmpClassifyId } obj := new(models.KnowledgeResource) knowledgeResource, err := obj.GetKnowledgeResourceByOutId(outId) esObj := new(models.KnowledgeResource) if err != nil && !utils.IsErrNoRow(err) { msg = "观点保存失败" return } if outId <= 0 || utils.IsErrNoRow(err) { // 创建新的知识资源库 err = nil obj.ResourceType = models.KnowledgeResourceTypeOpinion obj.ClassifyId = classifyId obj.Title = title obj.CreateTime = time.Now() obj.ModifyTime = time.Now() obj.State = models.KnowledgeResourceStateApproved obj.OutSource = outSource obj.Content = html.EscapeString(content) timestamp := strconv.FormatInt(time.Now().UnixNano(), 10) obj.ResourceCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp) obj.AdminId = adminId if adminRealName == "" || obj.AdminId == 0 { obj.AdminRealName = "无" } else { obj.AdminRealName = adminRealName } obj.SourceFrom = sourceFrom obj.StartTime = startTime obj.IsFile = 0 err = obj.Create([]string{}) if err != nil { msg = "观点保存失败" return } esObj = obj } else { // 更新知识资源库的状态 var updateCols []string if knowledgeResource.Title != title { knowledgeResource.Title = title updateCols = append(updateCols, "title") } if knowledgeResource.Content != content { knowledgeResource.Content = content updateCols = append(updateCols, "content") } if !knowledgeResource.StartTime.Equal(*startTime) { knowledgeResource.StartTime = startTime updateCols = append(updateCols, "start_time") } if knowledgeResource.SourceFrom != sourceFrom { knowledgeResource.SourceFrom = sourceFrom updateCols = append(updateCols, "source_from") } if len(updateCols) > 0 { knowledgeResource.ModifyTime = time.Now() updateCols = append(updateCols, "modify_time") err = knowledgeResource.Update(updateCols) if err != nil { msg = "观点保存失败" return } } esObj = knowledgeResource } go func() { er := EsAddOrEditKnowledgeResource(esObj) if er != nil { utils.FileLog.Info("es ViewPointSave error:%v", er) } }() return } func ViewPointKnowledgeResourceClassifyCheckAndSave(classifyName string, resourceType int) (classifyId int, err error) { obj := new(models.KnowledgeClassify) obj, err = obj.GetClassifyByNameTypeAndParentId(classifyName, resourceType, 0) if err != nil { if !utils.IsErrNoRow(err) { return } // 创建新的分类 err = nil obj.ClassifyName = classifyName obj.ResourceType = resourceType obj.CreateTime = time.Now() obj.ModifyTime = time.Now() obj.Enabled = 1 obj.Level = 1 obj.Sort = 1 err = obj.Create() if err != nil { return } classifyId = obj.ClassifyId } else { classifyId = obj.ClassifyId } return } func UploadFormFile(multipartFile map[string][]*multipart.FileHeader) (fileUrl []string, msg string, err error) { client := services.NewOssClient() // 定义允许的文件格式 allowedExtensions := map[string]bool{ ".pdf": true, ".doc": true, ".docx": true, ".ppt": true, ".pptx": true, } for _, file := range multipartFile { for _, f := range file { if f.Filename == "" { continue } ext := path.Ext(f.Filename) if !allowedExtensions[ext] { msg = "文件格式不正确" err = fmt.Errorf("文件格式不正确") return } saveName := fmt.Sprint(utils.MD5(f.Filename), time.Now().Format(utils.FormatDateTimeUnSpace), time.Now().Nanosecond(), ext) dataDir := time.Now().Format(utils.FormatDate) filePath := utils.StaticDir + "files/" + dataDir ok, _ := utils.PathExists(filePath) if !ok { err = os.MkdirAll(filePath, utils.DIR_MOD) if err != nil { msg = "目录创建失败" return } } fileContent, er := f.Open() if er != nil { msg = "文件打开失败" err = er return } fileContentBytes, er := ioutil.ReadAll(fileContent) if er != nil { msg = "文件读取失败" err = er return } filePath = filePath + "/" + saveName // 保存到本地 er = utils.SaveToFile(fileContentBytes, filePath) if er != nil { msg = "文件保存失败" err = er return } _, er = os.Stat(filePath) if er != nil { msg = "文件信息获取失败" err = er return } randStr := utils.GetRandStringNoSpecialChar(28) fileName := randStr + ext savePath := fmt.Sprintf("%s%s%s", utils.UploadFileDir, time.Now().Format("200601/20060102/"), fileName) resourceUrl, er := client.UploadFile(fileName, filePath, savePath) if er != nil { msg = "文件上传失败" err = er return } fileUrl = append(fileUrl, resourceUrl) } } return }