knowledge_resource.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. package knowledge
  2. import (
  3. "eta_gn/eta_report/models"
  4. "eta_gn/eta_report/services"
  5. "eta_gn/eta_report/utils"
  6. "fmt"
  7. "html"
  8. "io/ioutil"
  9. "mime/multipart"
  10. "os"
  11. "path"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. func ReportKnowledgeResourceAdd(title string, fileUrl []string, adminId int, adminRealName, classifyPath, sourceFrom string, pushTime time.Time) (msg string, err error) {
  17. classifyObj := new(models.KnowledgeClassify)
  18. var childClassifyId int
  19. if classifyPath != "" {
  20. classifyPathArr := strings.Split(classifyPath, "/")
  21. var classifyFirst, classifySecond, classifyThird string
  22. switch len(classifyPathArr) {
  23. case 1:
  24. classifyFirst = classifyPathArr[0]
  25. case 2:
  26. classifyFirst = classifyPathArr[0]
  27. classifySecond = classifyPathArr[1]
  28. case 3:
  29. classifyFirst = classifyPathArr[0]
  30. classifySecond = classifyPathArr[1]
  31. classifyThird = classifyPathArr[2]
  32. }
  33. childClassifyId, err = classifyObj.GetChildClassifyIdByNamePath(models.KnowledgeResourceTypeReport, classifyFirst, classifySecond, classifyThird)
  34. if err != nil {
  35. if utils.IsErrNoRow(err) {
  36. msg = "分类不存在,请检查知识资源-报告库中是否存在-" + classifyPath
  37. return
  38. }
  39. msg = "报告库添加失败"
  40. return
  41. }
  42. } else {
  43. msg = "分类不能为空"
  44. return
  45. }
  46. if childClassifyId == 0 {
  47. msg = "分类不存在,请检查知识资源-报告库中是否存在-" + classifyPath
  48. return
  49. }
  50. obj := new(models.KnowledgeResource)
  51. obj.ResourceType = models.KnowledgeResourceTypeReport
  52. obj.ClassifyId = childClassifyId
  53. obj.Title = title
  54. obj.CreateTime = time.Now()
  55. obj.ModifyTime = time.Now()
  56. obj.State = models.KnowledgeResourceStatePublished
  57. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  58. obj.ResourceCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
  59. obj.AdminId = adminId
  60. obj.AdminRealName = adminRealName
  61. obj.SourceFrom = sourceFrom
  62. obj.StartTime = &pushTime
  63. // 此处传入的url会在入库时存入knowledge_resource_file表中
  64. if len(fileUrl) != 0 {
  65. obj.IsFile = 1
  66. }
  67. err = obj.Create(fileUrl)
  68. if err != nil {
  69. return
  70. }
  71. return
  72. }
  73. func ReportKnowledgeResourceClassifyCheckAndSave(reportClassifyId int) (classifyId int, err error) {
  74. classifyObj := new(models.Classify)
  75. classifyObj, err = classifyObj.GetItemById(reportClassifyId)
  76. if err != nil {
  77. if !utils.IsErrNoRow(err) {
  78. return
  79. }
  80. }
  81. levelPath := classifyObj.LevelPath
  82. classifyIdStrs := strings.Split(levelPath, ",")
  83. classifyIds := make([]int, 0, len(classifyIdStrs))
  84. for _, classifyIdStr := range classifyIdStrs {
  85. classifyId, _ := strconv.Atoi(classifyIdStr)
  86. classifyIds = append(classifyIds, classifyId)
  87. }
  88. classifyList, err := classifyObj.GetItemsByIds(classifyIds)
  89. if err != nil {
  90. return
  91. }
  92. knowledgeClassifyObj := new(models.KnowledgeClassify)
  93. var parentId int
  94. for i, classify := range classifyList {
  95. knowledgeClassifyObj, err = knowledgeClassifyObj.GetClassifyByNameTypeAndParentId(classify.ClassifyName, models.KnowledgeResourceTypeOpinion, parentId)
  96. if err != nil {
  97. if !utils.IsErrNoRow(err) {
  98. return
  99. }
  100. for j := i; j < len(classifyList); j++ {
  101. tmp := &models.KnowledgeClassify{
  102. ClassifyName: classifyList[j].ClassifyName,
  103. Sort: 1,
  104. ParentId: parentId,
  105. CreateTime: time.Now(),
  106. ModifyTime: time.Now(),
  107. Enabled: 1,
  108. Level: classifyList[j].Level,
  109. ResourceType: models.KnowledgeResourceTypeOpinion,
  110. }
  111. err = tmp.Create()
  112. if err != nil {
  113. return
  114. }
  115. parentId = tmp.ClassifyId
  116. }
  117. classifyId = parentId
  118. return
  119. }
  120. parentId = knowledgeClassifyObj.ClassifyId
  121. }
  122. classifyId = parentId
  123. return
  124. }
  125. // ViewPointSave 知识资源观点库保存
  126. func ViewPointSave(outId, adminId int, adminRealName, title, content, sourceFrom, classifyName string, classifyId, outSource int, startTime *time.Time) (msg string, err error) {
  127. if classifyId <= 0 {
  128. tmpClassifyId, er := ViewPointKnowledgeResourceClassifyCheckAndSave(classifyName, models.KnowledgeResourceTypeOpinion)
  129. if er != nil {
  130. msg = "观点保存失败"
  131. err = er
  132. return
  133. }
  134. classifyId = tmpClassifyId
  135. }
  136. obj := new(models.KnowledgeResource)
  137. knowledgeResource, err := obj.GetKnowledgeResourceByOutId(outId)
  138. esObj := new(models.KnowledgeResource)
  139. if err != nil && !utils.IsErrNoRow(err) {
  140. msg = "观点保存失败"
  141. return
  142. }
  143. if outId <= 0 || utils.IsErrNoRow(err) {
  144. // 创建新的知识资源库
  145. err = nil
  146. obj.ResourceType = models.KnowledgeResourceTypeOpinion
  147. obj.ClassifyId = classifyId
  148. obj.Title = title
  149. obj.CreateTime = time.Now()
  150. obj.ModifyTime = time.Now()
  151. obj.State = models.KnowledgeResourceStateApproved
  152. obj.OutSource = outSource
  153. obj.Content = html.EscapeString(content)
  154. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  155. obj.ResourceCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
  156. obj.AdminId = adminId
  157. if adminRealName == "" || obj.AdminId == 0 {
  158. obj.AdminRealName = "无"
  159. } else {
  160. obj.AdminRealName = adminRealName
  161. }
  162. obj.SourceFrom = sourceFrom
  163. obj.StartTime = startTime
  164. obj.IsFile = 0
  165. err = obj.Create([]string{})
  166. if err != nil {
  167. msg = "观点保存失败"
  168. return
  169. }
  170. esObj = obj
  171. } else {
  172. // 更新知识资源库的状态
  173. var updateCols []string
  174. if knowledgeResource.Title != title {
  175. knowledgeResource.Title = title
  176. updateCols = append(updateCols, "title")
  177. }
  178. if knowledgeResource.Content != content {
  179. knowledgeResource.Content = content
  180. updateCols = append(updateCols, "content")
  181. }
  182. if !knowledgeResource.StartTime.Equal(*startTime) {
  183. knowledgeResource.StartTime = startTime
  184. updateCols = append(updateCols, "start_time")
  185. }
  186. if knowledgeResource.SourceFrom != sourceFrom {
  187. knowledgeResource.SourceFrom = sourceFrom
  188. updateCols = append(updateCols, "source_from")
  189. }
  190. if len(updateCols) > 0 {
  191. knowledgeResource.ModifyTime = time.Now()
  192. updateCols = append(updateCols, "modify_time")
  193. err = knowledgeResource.Update(updateCols)
  194. if err != nil {
  195. msg = "观点保存失败"
  196. return
  197. }
  198. }
  199. esObj = knowledgeResource
  200. }
  201. go func() {
  202. er := EsAddOrEditKnowledgeResource(esObj)
  203. if er != nil {
  204. utils.FileLog.Info("es ViewPointSave error:%v", er)
  205. }
  206. }()
  207. return
  208. }
  209. func ViewPointKnowledgeResourceClassifyCheckAndSave(classifyName string, resourceType int) (classifyId int, err error) {
  210. obj := new(models.KnowledgeClassify)
  211. obj, err = obj.GetClassifyByNameTypeAndParentId(classifyName, resourceType, 0)
  212. if err != nil {
  213. if !utils.IsErrNoRow(err) {
  214. return
  215. }
  216. // 创建新的分类
  217. err = nil
  218. obj.ClassifyName = classifyName
  219. obj.ResourceType = resourceType
  220. obj.CreateTime = time.Now()
  221. obj.ModifyTime = time.Now()
  222. obj.Enabled = 1
  223. obj.Level = 1
  224. obj.Sort = 1
  225. err = obj.Create()
  226. if err != nil {
  227. return
  228. }
  229. classifyId = obj.ClassifyId
  230. } else {
  231. classifyId = obj.ClassifyId
  232. }
  233. return
  234. }
  235. func UploadFormFile(multipartFile map[string][]*multipart.FileHeader) (fileUrl []string, msg string, err error) {
  236. client := services.NewOssClient()
  237. // 定义允许的文件格式
  238. allowedExtensions := map[string]bool{
  239. ".pdf": true,
  240. ".doc": true,
  241. ".docx": true,
  242. ".ppt": true,
  243. ".pptx": true,
  244. }
  245. for _, file := range multipartFile {
  246. for _, f := range file {
  247. if f.Filename == "" {
  248. continue
  249. }
  250. ext := path.Ext(f.Filename)
  251. if !allowedExtensions[ext] {
  252. msg = "文件格式不正确"
  253. err = fmt.Errorf("文件格式不正确")
  254. return
  255. }
  256. saveName := fmt.Sprint(utils.MD5(f.Filename), time.Now().Format(utils.FormatDateTimeUnSpace), time.Now().Nanosecond(), ext)
  257. dataDir := time.Now().Format(utils.FormatDate)
  258. filePath := utils.StaticDir + "files/" + dataDir
  259. ok, _ := utils.PathExists(filePath)
  260. if !ok {
  261. err = os.MkdirAll(filePath, utils.DIR_MOD)
  262. if err != nil {
  263. msg = "目录创建失败"
  264. return
  265. }
  266. }
  267. fileContent, er := f.Open()
  268. if er != nil {
  269. msg = "文件打开失败"
  270. err = er
  271. return
  272. }
  273. fileContentBytes, er := ioutil.ReadAll(fileContent)
  274. if er != nil {
  275. msg = "文件读取失败"
  276. err = er
  277. return
  278. }
  279. filePath = filePath + "/" + saveName
  280. // 保存到本地
  281. er = utils.SaveToFile(fileContentBytes, filePath)
  282. if er != nil {
  283. msg = "文件保存失败"
  284. err = er
  285. return
  286. }
  287. _, er = os.Stat(filePath)
  288. if er != nil {
  289. msg = "文件信息获取失败"
  290. err = er
  291. return
  292. }
  293. randStr := utils.GetRandStringNoSpecialChar(28)
  294. fileName := randStr + ext
  295. savePath := fmt.Sprintf("%s%s%s", utils.UploadFileDir, time.Now().Format("200601/20060102/"), fileName)
  296. resourceUrl, er := client.UploadFile(fileName, filePath, savePath)
  297. if er != nil {
  298. msg = "文件上传失败"
  299. err = er
  300. return
  301. }
  302. fileUrl = append(fileUrl, resourceUrl)
  303. }
  304. }
  305. return
  306. }