resource.go 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255
  1. package knowledge
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta_gn/eta_api/models/knowledge"
  6. "eta_gn/eta_api/models/knowledge_approve"
  7. "eta_gn/eta_api/models/system"
  8. "eta_gn/eta_api/services"
  9. "eta_gn/eta_api/services/alarm_msg"
  10. "eta_gn/eta_api/utils"
  11. "fmt"
  12. "html"
  13. "strconv"
  14. "strings"
  15. "time"
  16. "github.com/go-redis/redis/v8"
  17. "github.com/rdlucklib/rdluck_tools/paging"
  18. "github.com/tealeg/xlsx"
  19. )
  20. func AddResource(req *knowledge.AddReq, sysUser *system.Admin) (item *knowledge.KnowledgeResource, err error, errMsg string) {
  21. var startTime time.Time
  22. var endTime time.Time
  23. if req.StartTime != "" {
  24. if req.ResourceType == knowledge.KnowledgeResourceTypeEvent || req.ResourceType == knowledge.KnowledgeResourceTypePolicy {
  25. startTime, err = time.ParseInLocation(utils.FormatDateTime, req.StartTime, time.Local)
  26. if err != nil { // 如果时间格式不正确,则返回错误信息
  27. errMsg = "开始时间格式不正确"
  28. err = fmt.Errorf("开始时间格式不正确,Err:" + err.Error())
  29. return
  30. }
  31. } else {
  32. startTime, err = time.ParseInLocation(utils.FormatDate, req.StartTime, time.Local)
  33. if err != nil { // 如果时间格式不正确,则返回错误信息
  34. errMsg = "开始时间格式不正确"
  35. err = fmt.Errorf("开始时间格式不正确,Err:" + err.Error())
  36. return
  37. }
  38. }
  39. }
  40. if req.EndTime != "" {
  41. if req.ResourceType == knowledge.KnowledgeResourceTypeEvent || req.ResourceType == knowledge.KnowledgeResourceTypePolicy {
  42. endTime, err = time.ParseInLocation(utils.FormatDateTime, req.EndTime, time.Local)
  43. if err != nil { // 如果时间格式不正确,则返回错误信息
  44. errMsg = "结束时间格式不正确"
  45. err = fmt.Errorf("结束时间格式不正确,Err:" + err.Error())
  46. return
  47. }
  48. }
  49. }
  50. isFile := 0
  51. if req.Content != "" {
  52. e := utils.ContentXssCheck(req.Content)
  53. if e != nil {
  54. errMsg = "存在非法标签"
  55. err = fmt.Errorf("存在非法标签, Err: " + e.Error())
  56. return
  57. }
  58. content, e := services.FilterReportContentBr(req.Content)
  59. if e != nil {
  60. errMsg = "内容去除前后空格失败"
  61. err = fmt.Errorf("内容去除前后空格失败, Err: " + e.Error())
  62. return
  63. }
  64. req.Content = content
  65. /*contentSub, err = services.GetReportContentSub(req.Content)
  66. if err != nil {
  67. go alarm_msg.SendAlarmMsg("ContentSub 失败,Err:"+err.Error(), 3)
  68. //utils.SendEmail(utils.APPNAME+"失败提醒", "解析 ContentSub 失败,Err:"+err.Error(), utils.EmailSendToUsers)
  69. return
  70. }*/
  71. } else {
  72. if len(req.FileUrl) == 0 {
  73. errMsg = "内容或文件不能为空"
  74. err = fmt.Errorf("内容或文件不能为空")
  75. return
  76. }
  77. isFile = 1
  78. }
  79. classifyObj := new(knowledge.KnowledgeClassify)
  80. classifyItem, err := classifyObj.GetClassifyById(req.ClassifyId)
  81. if err != nil {
  82. if utils.IsErrNoRow(err) {
  83. err = fmt.Errorf("分类不存在,Err:" + err.Error())
  84. errMsg = "分类不存在"
  85. return
  86. }
  87. errMsg = "获取分类失败"
  88. err = fmt.Errorf("获取分类失败,Err:" + err.Error())
  89. return
  90. }
  91. if req.ClassifyId != classifyItem.ClassifyId { // 如果传进来的分类id和数据库中查到的分类id不一致,说明传进来的分类id是错误的
  92. errMsg = "错误分类"
  93. err = fmt.Errorf("错误分类")
  94. return
  95. }
  96. // 校验标签
  97. if req.TagId > 0 {
  98. tagObj := new(knowledge.KnowledgeTag)
  99. _, e := tagObj.GetTagById(req.TagId)
  100. if e != nil {
  101. if utils.IsErrNoRow(e) {
  102. err = fmt.Errorf("标签不存在,Err:" + e.Error())
  103. errMsg = "标签不存在"
  104. return
  105. }
  106. err = fmt.Errorf("获取标签失败,Err:" + e.Error())
  107. return
  108. }
  109. }
  110. item = new(knowledge.KnowledgeResource)
  111. item.ClassifyId = req.ClassifyId
  112. item.TagId = req.TagId
  113. item.SourceFrom = req.SourceFrom
  114. item.ResourceType = req.ResourceType
  115. item.Title = req.Title
  116. item.State = knowledge.KnowledgeResourceStateUnpublished
  117. item.Content = html.EscapeString(req.Content)
  118. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  119. item.ResourceCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
  120. item.IsFile = isFile
  121. //todo 内容前5行
  122. //item.ContentSub = html.EscapeString(contentSub)
  123. if !startTime.IsZero() {
  124. item.StartTime = &startTime
  125. } else {
  126. item.StartTime = nil
  127. }
  128. if !endTime.IsZero() {
  129. item.EndTime = &endTime
  130. } else {
  131. item.EndTime = nil
  132. }
  133. item.ModifyTime = time.Now()
  134. item.AdminId = sysUser.AdminId
  135. item.AdminRealName = sysUser.RealName
  136. //item.LastModifyAdminId = sysUser.AdminId
  137. //item.LastModifyAdminName = sysUser.RealName
  138. item.ModifyTime = time.Now()
  139. item.CreateTime = time.Now()
  140. err = item.AddWithFile(req.FileUrl)
  141. if err != nil {
  142. errMsg = "保存失败"
  143. err = fmt.Errorf("保存失败,Err:" + err.Error())
  144. return
  145. }
  146. // 保存到es中
  147. go EsAddOrEditKnowledgeResource(item)
  148. return
  149. }
  150. func EditResource(resourceInfo *knowledge.KnowledgeResource, req knowledge.EditReq, sysUser *system.Admin) (err error, errMsg string) {
  151. errMsg = `保存失败`
  152. var startTime time.Time
  153. var endTime time.Time
  154. if req.StartTime != "" {
  155. if req.ResourceType == knowledge.KnowledgeResourceTypeEvent || req.ResourceType == knowledge.KnowledgeResourceTypePolicy {
  156. startTime, err = time.ParseInLocation(utils.FormatDateTime, req.StartTime, time.Local)
  157. if err != nil { // 如果时间格式不正确,则返回错误信息
  158. errMsg = "开始时间格式不正确"
  159. err = fmt.Errorf("开始时间格式不正确,Err:" + err.Error())
  160. return
  161. }
  162. } else {
  163. startTime, err = time.ParseInLocation(utils.FormatDate, req.StartTime, time.Local)
  164. if err != nil { // 如果时间格式不正确,则返回错误信息
  165. errMsg = "开始时间格式不正确"
  166. err = fmt.Errorf("开始时间格式不正确,Err:" + err.Error())
  167. return
  168. }
  169. }
  170. }
  171. if req.EndTime != "" {
  172. if req.ResourceType == knowledge.KnowledgeResourceTypeEvent || req.ResourceType == knowledge.KnowledgeResourceTypePolicy {
  173. endTime, err = time.ParseInLocation(utils.FormatDateTime, req.EndTime, time.Local)
  174. if err != nil { // 如果时间格式不正确,则返回错误信息
  175. errMsg = "结束时间格式不正确"
  176. err = fmt.Errorf("结束时间格式不正确,Err:" + err.Error())
  177. return
  178. }
  179. }
  180. }
  181. if resourceInfo.IsFile == 0 {
  182. if req.Content == "" {
  183. errMsg = "内容不能为空"
  184. err = fmt.Errorf("内容不能为空")
  185. return
  186. }
  187. } else if resourceInfo.IsFile == 1 {
  188. if len(req.FileUrl) == 0 {
  189. errMsg = "文件地址不能为空"
  190. err = fmt.Errorf("文件地址不能为空")
  191. return
  192. }
  193. }
  194. if req.Content != "" {
  195. e := utils.ContentXssCheck(req.Content)
  196. if e != nil {
  197. errMsg = "存在非法标签"
  198. err = fmt.Errorf("存在非法标签, Err: " + e.Error())
  199. return
  200. }
  201. content, e := services.FilterReportContentBr(req.Content)
  202. if e != nil {
  203. errMsg = "内容去除前后空格失败"
  204. err = fmt.Errorf("内容去除前后空格失败, Err: " + e.Error())
  205. return
  206. }
  207. req.Content = content
  208. /*contentSub, err = services.GetReportContentSub(req.Content)
  209. if err != nil {
  210. go alarm_msg.SendAlarmMsg("ContentSub 失败,Err:"+err.Error(), 3)
  211. //utils.SendEmail(utils.APPNAME+"失败提醒", "解析 ContentSub 失败,Err:"+err.Error(), utils.EmailSendToUsers)
  212. return
  213. }*/
  214. }
  215. classifyObj := new(knowledge.KnowledgeClassify)
  216. classifyItem, err := classifyObj.GetClassifyById(req.ClassifyId)
  217. if err != nil {
  218. if utils.IsErrNoRow(err) {
  219. err = fmt.Errorf("分类不存在,Err:" + err.Error())
  220. errMsg = "分类不存在"
  221. return
  222. }
  223. errMsg = "获取分类失败"
  224. err = fmt.Errorf("获取分类失败,Err:" + err.Error())
  225. return
  226. }
  227. if req.ClassifyId != classifyItem.ClassifyId { // 如果传进来的分类id和数据库中查到的分类id不一致,说明传进来的分类id是错误的
  228. errMsg = "错误分类"
  229. err = fmt.Errorf("错误分类")
  230. return
  231. }
  232. if req.TagId > 0 {
  233. tagObj := new(knowledge.KnowledgeTag)
  234. _, e := tagObj.GetTagById(req.TagId)
  235. if e != nil {
  236. if utils.IsErrNoRow(e) {
  237. err = fmt.Errorf("标签不存在,Err:" + e.Error())
  238. errMsg = "标签不存在"
  239. return
  240. }
  241. err = fmt.Errorf("获取标签失败,Err:" + e.Error())
  242. return
  243. }
  244. }
  245. resourceInfo.ClassifyId = req.ClassifyId
  246. resourceInfo.SourceFrom = req.SourceFrom
  247. resourceInfo.TagId = req.TagId
  248. resourceInfo.Title = req.Title
  249. if req.Content != "" {
  250. resourceInfo.Content = html.EscapeString(req.Content)
  251. }
  252. if !startTime.IsZero() {
  253. resourceInfo.StartTime = &startTime
  254. }
  255. if !endTime.IsZero() {
  256. resourceInfo.EndTime = &endTime
  257. }
  258. //resourceInfo.LastModifyAdminId = sysUser.AdminId
  259. //resourceInfo.LastModifyAdminName = sysUser.RealName
  260. resourceInfo.ModifyTime = time.Now()
  261. updateCols := []string{"ClassifyId", "SourceFrom", "TagId", "Title", "Content", "StartTime", "EndTime", "ModifyTime"}
  262. // 修改报告的基本信息,以及报告的授权用户
  263. err = resourceInfo.UpdateWithFile(updateCols, req.FileUrl)
  264. if err != nil {
  265. return
  266. }
  267. go EsAddOrEditKnowledgeResource(resourceInfo)
  268. return
  269. }
  270. // ImportResourceData
  271. // @Description: 数据导入
  272. // @author: Roc
  273. // @datetime 2024-08-01 11:27:21
  274. // @param path string
  275. // @param sysUser *system.Admin
  276. // @return successCount int
  277. // @return failCount int
  278. // @return err error
  279. // @return errMsg string
  280. func ImportResourceData(path string, resourceType int, sysUser *system.Admin) (successCount, failCount int, err error, errMsg string) {
  281. // 错误信息
  282. errMsgList := make([]string, 0)
  283. defer func() {
  284. // 错误信息记录
  285. if len(errMsgList) > 0 {
  286. utils.FileLog.Info("导入失败, errMsgList: %v", strings.Join(errMsgList, "\n"))
  287. }
  288. }()
  289. errMsg = `导入失败`
  290. xlFile, err := xlsx.OpenFile(path)
  291. if err != nil {
  292. fmt.Println(err.Error())
  293. return
  294. }
  295. if len(xlFile.Sheets) <= 0 {
  296. errMsg = "导入模板异常"
  297. err = errors.New(errMsg)
  298. return
  299. }
  300. //导入失败数据
  301. failDataList := make([]*knowledge.KnowledgeImportFail, 0)
  302. var indexDataList []knowledge.ResourceImportData
  303. tagObj := new(knowledge.KnowledgeTag)
  304. // 模板校验,然后处理成标准化格式
  305. for _, sheet := range xlFile.Sheets {
  306. var tmpFailDataList []*knowledge.KnowledgeImportFail
  307. var tmpIndexDataList []knowledge.ResourceImportData
  308. rowList := sheet.Rows
  309. if len(rowList) <= 0 {
  310. errMsg = sheet.Name + "页异常"
  311. err = errors.New(errMsg)
  312. return
  313. }
  314. if resourceType == knowledge.KnowledgeResourceTypeEvent || resourceType == knowledge.KnowledgeResourceTypePolicy {
  315. tmpIndexDataList, tmpFailDataList, err, errMsg = getDataByTemplateEvent(sheet, sysUser.AdminId, resourceType)
  316. } else {
  317. tmpIndexDataList, tmpFailDataList, err, errMsg = getDataByTemplatePoint(sheet, sysUser.AdminId, resourceType)
  318. }
  319. if err != nil {
  320. return
  321. }
  322. fmt.Println("tmpIndexDataList: ", tmpIndexDataList)
  323. for _, v := range tmpFailDataList {
  324. fmt.Println("tmpFailDataList: ", v)
  325. }
  326. indexDataList = append(indexDataList, tmpIndexDataList...)
  327. failDataList = append(failDataList, tmpFailDataList...)
  328. }
  329. if len(indexDataList) <= 0 {
  330. if len(failDataList) > 0 {
  331. errMsg = failDataList[0].Remark
  332. err = errors.New(errMsg)
  333. }
  334. return
  335. }
  336. // 遍历获取所有分类
  337. classifyNames := make([]string, 0)
  338. for _, v := range indexDataList {
  339. if v.ClassifyFirstName != "" {
  340. classifyNames = append(classifyNames, v.ClassifyFirstName)
  341. }
  342. if v.ClassifySecondName != "" {
  343. classifyNames = append(classifyNames, v.ClassifySecondName)
  344. }
  345. if v.ClassifyThirdName != "" {
  346. classifyNames = append(classifyNames, v.ClassifyThirdName)
  347. }
  348. }
  349. fmt.Println(classifyNames)
  350. classifyList, err := knowledge.GetFullClassifyListByName(classifyNames, resourceType)
  351. if err != nil {
  352. err = fmt.Errorf("获取分类数据失败 Err: %s", err.Error())
  353. errMsg = "获取分类数据失败"
  354. return
  355. }
  356. classifyObj := new(knowledge.KnowledgeClassify)
  357. classifyAllList, err := classifyObj.GetAllClassify(resourceType)
  358. if err != nil {
  359. err = fmt.Errorf("获取分类数据失败 Err: %s", err.Error())
  360. errMsg = "获取分类数据失败"
  361. return
  362. }
  363. classifyNameMap := make(map[string]int)
  364. classifyHasChildMap := make(map[int]bool)
  365. for _, v := range classifyAllList {
  366. if v.ParentId > 0 {
  367. classifyHasChildMap[v.ParentId] = true
  368. }
  369. }
  370. for _, v := range classifyList {
  371. name := fmt.Sprintf("%s/%s/%s", v.RootName, v.ParentName, v.ClassifyName)
  372. fmt.Println("name", name)
  373. if v.RootName == "" && v.ParentName == "" { //一级分类
  374. name = fmt.Sprintf("%s/%s/%s", v.ClassifyName, "", "")
  375. if _, ok := classifyHasChildMap[v.ClassifyId]; ok {
  376. continue
  377. }
  378. } else if v.RootName == "" { //二级分类
  379. name = fmt.Sprintf("%s/%s/%s", v.ParentName, v.ClassifyName, "")
  380. if _, ok := classifyHasChildMap[v.ClassifyId]; ok {
  381. continue
  382. }
  383. }
  384. classifyNameMap[name] = v.ClassifyId
  385. }
  386. tagNameMap := make(map[string]int)
  387. tagList, err := tagObj.GetAllTag(resourceType)
  388. for _, v := range tagList {
  389. tagNameMap[v.TagName] = v.TagId
  390. }
  391. var addlist []*knowledge.KnowledgeResource
  392. for _, v := range indexDataList {
  393. errTip := ""
  394. classifyId := 0
  395. if successCount > 100 {
  396. errTip = "导入数量超过100条"
  397. } else if v.Title == "" {
  398. errTip = "标题未填"
  399. } else if len(v.Title) > 600 {
  400. errTip = "标题过长"
  401. } else if v.Content == "" {
  402. errTip = "正文未填"
  403. } else if v.SourceFrom == "" {
  404. errTip = "来源未填"
  405. } else if v.ClassifyFirstName == "" {
  406. errTip = "分类未填"
  407. } else if v.StartDate == "" || v.StartTime == "" {
  408. if resourceType == knowledge.KnowledgeResourceTypeEvent || resourceType == knowledge.KnowledgeResourceTypePolicy {
  409. errTip = "开始时间未填"
  410. } else if v.StartDate == "" {
  411. errTip = "时间未填"
  412. }
  413. }
  414. if errTip == "" {
  415. // 判断分类是否存在
  416. classifyName := fmt.Sprintf("%s/%s/%s", v.ClassifyFirstName, v.ClassifySecondName, v.ClassifyThirdName)
  417. fmt.Println("classifyName: ", classifyName)
  418. classifyIdTmp, ok := classifyNameMap[classifyName]
  419. if !ok {
  420. errTip = "分类不存在"
  421. }
  422. classifyId = classifyIdTmp
  423. }
  424. if errTip != "" {
  425. failItem := &knowledge.KnowledgeImportFail{
  426. Title: v.Title,
  427. Content: v.Content,
  428. SourceFrom: v.SourceFrom,
  429. ClassifyFirst: v.ClassifyFirstName,
  430. ClassifySecond: v.ClassifySecondName,
  431. ClassifyThird: v.ClassifyThirdName,
  432. ResourceType: resourceType,
  433. StartDate: v.StartDate,
  434. EndDate: v.EndDate,
  435. StartTime: v.StartTime,
  436. EndTime: v.EndTime,
  437. Remark: errTip,
  438. SysUserId: fmt.Sprint(sysUser.AdminId),
  439. CreateTime: time.Now(),
  440. }
  441. failDataList = append(failDataList, failItem)
  442. continue
  443. }
  444. tagId, _ := tagNameMap[v.Tag]
  445. req := new(knowledge.AddReq)
  446. req.Title = v.Title
  447. req.Content = v.Content
  448. req.SourceFrom = v.SourceFrom
  449. req.ClassifyId = classifyId
  450. req.TagId = tagId
  451. req.ResourceType = resourceType
  452. if v.StartDate != "" {
  453. if resourceType == knowledge.KnowledgeResourceTypeEvent || resourceType == knowledge.KnowledgeResourceTypePolicy {
  454. req.StartTime = fmt.Sprintf("%s %s", v.StartDate, v.StartTime)
  455. } else {
  456. req.StartTime = v.StartDate
  457. }
  458. }
  459. if v.EndDate != "" {
  460. if resourceType == knowledge.KnowledgeResourceTypeEvent || resourceType == knowledge.KnowledgeResourceTypePolicy {
  461. req.EndTime = fmt.Sprintf("%s %s", v.EndDate, v.EndTime)
  462. }
  463. }
  464. item, tmpErr, msg := addExcelResource(req, sysUser)
  465. if tmpErr != nil {
  466. failItem := &knowledge.KnowledgeImportFail{
  467. Title: v.Title,
  468. Content: v.Content,
  469. SourceFrom: v.SourceFrom,
  470. ClassifyFirst: v.ClassifyFirstName,
  471. ClassifySecond: v.ClassifySecondName,
  472. ClassifyThird: v.ClassifyThirdName,
  473. ResourceType: resourceType,
  474. StartDate: v.StartDate,
  475. EndDate: v.EndDate,
  476. StartTime: v.StartTime,
  477. EndTime: v.EndTime,
  478. Remark: "新增事件失败:" + msg,
  479. SysUserId: fmt.Sprint(sysUser.AdminId),
  480. CreateTime: time.Now(),
  481. }
  482. failDataList = append(failDataList, failItem)
  483. continue
  484. } else {
  485. successCount++
  486. addlist = append(addlist, item)
  487. }
  488. }
  489. go func() {
  490. err = EsBatchAddOrEditKnowledgeResource(addlist)
  491. if err != nil {
  492. utils.FileLog.Info("知识资源EsBatchAddOrEditKnowledgeResource失败,err:%s, err", err.Error())
  493. }
  494. }()
  495. // 失败数量
  496. failCount = len(failDataList)
  497. //fmt.Println("failDataList:", len(failDataList))
  498. if failCount > 0 {
  499. failObj := new(knowledge.KnowledgeImportFail)
  500. //先删除导入失败记录
  501. _ = failObj.Delete(sysUser.AdminId, resourceType)
  502. // 批量添加导入失败记录
  503. err = failObj.MultiAdd(failDataList)
  504. if err != nil {
  505. go alarm_msg.SendAlarmMsg("导入数据 新增失败记录失败,Err:"+err.Error(), 3)
  506. }
  507. {
  508. //错误信息字符串切片,最后作为发送邮件通知使用
  509. failContents := make([]string, 0)
  510. for _, v := range failDataList {
  511. failContents = append(failContents, fmt.Sprint(v.Title, "导入失败:", v.Remark))
  512. }
  513. utils.FileLog.Info("导入数据 存在部分数据导入失败:" + strings.Join(failContents, ";"))
  514. //导入失败的话,最后统一邮件提醒就好啦,不需要每次都去提醒
  515. go alarm_msg.SendAlarmMsg("导入数据 存在部分数据导入失败:"+strings.Join(failContents, ";"), 3)
  516. }
  517. }
  518. return
  519. }
  520. // getDataByTemplateEvent
  521. func getDataByTemplateEvent(sheet *xlsx.Sheet, sysUserId, resourceType int) (indexDataList []knowledge.ResourceImportData, failDataList []*knowledge.KnowledgeImportFail, err error, errMsg string) {
  522. fmt.Println("sheet name: ", sheet.Name)
  523. indexDataList = make([]knowledge.ResourceImportData, 0)
  524. failDataList = make([]*knowledge.KnowledgeImportFail, 0)
  525. //遍历行读取
  526. maxRow := sheet.MaxRow
  527. fmt.Println("maxRow:", maxRow)
  528. // 表头信息
  529. if maxRow <= 3 {
  530. errMsg = "模板异常1"
  531. err = errors.New(errMsg)
  532. return
  533. }
  534. // 表头校验
  535. {
  536. headerRow := sheet.Row(1)
  537. cells := headerRow.Cells
  538. fmt.Println("cells:", len(cells))
  539. if len(cells) < 11 {
  540. errMsg = "导入文件异常,请下载最新导入模板文件"
  541. err = errors.New(errMsg)
  542. return
  543. }
  544. // 循环打印每个单元格
  545. for k, v := range cells {
  546. fmt.Println("第", k, "个单元格", v.Value)
  547. }
  548. templateFail := false
  549. if cells[0].Value != "开始时间" {
  550. templateFail = true
  551. }
  552. if cells[2].Value != "结束时间(非必填)" {
  553. templateFail = true
  554. }
  555. if cells[4].Value != "标题" {
  556. templateFail = true
  557. }
  558. if cells[5].Value != "正文" {
  559. templateFail = true
  560. }
  561. if cells[6].Value != "来源" {
  562. templateFail = true
  563. }
  564. if cells[7].Value != "一级分类" {
  565. templateFail = true
  566. }
  567. if cells[8].Value != "二级分类" {
  568. templateFail = true
  569. }
  570. if cells[9].Value != "三级分类" {
  571. templateFail = true
  572. }
  573. if cells[10].Value != "标签" {
  574. templateFail = true
  575. }
  576. headerRow2 := sheet.Row(2)
  577. cells2 := headerRow2.Cells
  578. for k, v := range cells2 {
  579. fmt.Println("2第", k, "个单元格", v.Value)
  580. }
  581. if len(cells2) < 11 {
  582. templateFail = true
  583. } else {
  584. if cells2[0].Value != "日期" {
  585. templateFail = true
  586. }
  587. if cells2[1].Value != "时间" {
  588. templateFail = true
  589. }
  590. if cells2[2].Value != "日期" {
  591. templateFail = true
  592. }
  593. if cells2[3].Value != "时间" {
  594. templateFail = true
  595. }
  596. }
  597. if templateFail {
  598. fmt.Println("头部信息校验失败")
  599. errMsg = "导入文件异常,请下载最新导入模板文件"
  600. err = errors.New(errMsg)
  601. return
  602. }
  603. }
  604. for i := 3; i < maxRow; i++ {
  605. row := sheet.Row(i)
  606. cells := row.Cells
  607. lenCell := len(cells)
  608. // 过滤空白行
  609. if lenCell <= 0 {
  610. continue
  611. }
  612. var startDateO, endDateO, startTimeO, endTimeO string
  613. var startDate, startTime, endDate, endTime string
  614. var title, content, sourceFrom, classifyName1, classifyName2, classifyName3, tag string
  615. for k, v := range cells {
  616. fmt.Println("数据第行", i, "第", k, "个单元格", v.Value)
  617. if k == 0 {
  618. startDate = strings.TrimSpace(cells[0].Value) //开始时间
  619. startDateO = startDate
  620. } else if k == 1 {
  621. startTime = strings.TrimSpace(cells[1].Value) //开始时间
  622. startTimeO = startTime
  623. } else if k == 2 {
  624. endDate = strings.TrimSpace(cells[2].Value) //结束时间
  625. endDateO = endDate
  626. } else if k == 3 {
  627. endTime = strings.TrimSpace(cells[3].Value) //结束时间
  628. endTimeO = endTime
  629. } else if k == 4 {
  630. title = strings.TrimSpace(cells[4].Value) //标题
  631. } else if k == 5 {
  632. content = strings.TrimSpace(cells[5].Value) //正文
  633. } else if k == 6 {
  634. sourceFrom = strings.TrimSpace(cells[6].Value) //来源
  635. } else if k == 7 {
  636. classifyName1 = strings.TrimSpace(cells[7].Value) //一级分类
  637. } else if k == 8 {
  638. classifyName2 = strings.TrimSpace(cells[8].Value) //二级分类
  639. } else if k == 9 {
  640. classifyName3 = strings.TrimSpace(cells[9].Value) //三级分类
  641. } else if k == 10 {
  642. tag = strings.TrimSpace(cells[10].Value) //标签
  643. }
  644. }
  645. if title == "" && startDate == "" && startTime == "" && endTime == "" && endDate == "" && content == "" && tag == "" && sourceFrom == "" && classifyName1 == "" && classifyName2 == "" && classifyName3 == "" { //过滤空白行
  646. continue
  647. }
  648. fmt.Println("lenCell:", lenCell)
  649. // 校验日期格式
  650. var startDateVal, endDateVal, startTimeVal float64
  651. var tmpErr error
  652. if startDate != "" {
  653. // 判断9:09:14日期格式是否正确
  654. // 判断是否是数字
  655. startDateVal, tmpErr = strconv.ParseFloat(startDate, 64)
  656. if tmpErr == nil {
  657. startDate = xlsx.TimeFromExcelTime(startDateVal, false).Format(utils.FormatDate)
  658. fmt.Println(startDate)
  659. } else {
  660. startDate, tmpErr = getExcelDate(startDate)
  661. if tmpErr != nil {
  662. failDataList = append(failDataList, &knowledge.KnowledgeImportFail{
  663. //Id: 0,
  664. Title: title,
  665. Content: content,
  666. SourceFrom: sourceFrom,
  667. ClassifyFirst: classifyName1,
  668. ClassifySecond: classifyName2,
  669. ClassifyThird: classifyName3,
  670. ResourceType: resourceType,
  671. StartDate: startDateO,
  672. EndDate: endDateO,
  673. StartTime: startTimeO,
  674. EndTime: endTimeO,
  675. Remark: "日期格式异常",
  676. SysUserId: strconv.Itoa(sysUserId),
  677. CreateTime: time.Now(),
  678. })
  679. continue
  680. }
  681. }
  682. if startTime != "" {
  683. // 判断9:09:14日期格式是否正确
  684. startTimeVal, tmpErr = strconv.ParseFloat(startTime, 64)
  685. if tmpErr == nil {
  686. startTimeVal = startDateVal + startTimeVal
  687. startTime = xlsx.TimeFromExcelTime(startTimeVal, false).Format(utils.FormatTime)
  688. fmt.Println(startTime)
  689. } else {
  690. _, tmpErr = time.Parse(utils.FormatDateTime, startDate+" "+startTime)
  691. if tmpErr != nil {
  692. failDataList = append(failDataList, &knowledge.KnowledgeImportFail{
  693. //Id: 0,
  694. Title: title,
  695. Content: content,
  696. SourceFrom: sourceFrom,
  697. ClassifyFirst: classifyName1,
  698. ClassifySecond: classifyName2,
  699. ClassifyThird: classifyName3,
  700. ResourceType: resourceType,
  701. StartDate: startDateO,
  702. EndDate: endDateO,
  703. StartTime: startTimeO,
  704. EndTime: endTimeO,
  705. Remark: "日期格式异常",
  706. SysUserId: strconv.Itoa(sysUserId),
  707. CreateTime: time.Now(),
  708. })
  709. continue
  710. }
  711. }
  712. }
  713. }
  714. if endDate != "" {
  715. endDateVal, tmpErr = strconv.ParseFloat(endDate, 64)
  716. if tmpErr == nil {
  717. endDate = xlsx.TimeFromExcelTime(endDateVal, false).Format(utils.FormatDate)
  718. fmt.Println(endDate)
  719. } else {
  720. endDate, tmpErr = getExcelDate(endDate)
  721. if tmpErr != nil {
  722. failDataList = append(failDataList, &knowledge.KnowledgeImportFail{
  723. //Id: 0,
  724. Title: title,
  725. Content: content,
  726. SourceFrom: sourceFrom,
  727. ClassifyFirst: classifyName1,
  728. ClassifySecond: classifyName2,
  729. ClassifyThird: classifyName3,
  730. ResourceType: resourceType,
  731. StartDate: startDateO,
  732. EndDate: endDateO,
  733. StartTime: startTimeO,
  734. EndTime: endTimeO,
  735. Remark: "日期格式异常",
  736. SysUserId: strconv.Itoa(sysUserId),
  737. CreateTime: time.Now(),
  738. })
  739. continue
  740. }
  741. }
  742. if endTime != "" {
  743. // 判断9:09:14日期格式是否正确
  744. endTimeVal, e := strconv.ParseFloat(endTime, 64)
  745. if e == nil {
  746. endTimeVal = endDateVal + endTimeVal
  747. endTime = xlsx.TimeFromExcelTime(endTimeVal, false).Format(utils.FormatTime)
  748. fmt.Println(endTime)
  749. } else {
  750. _, e = time.Parse(utils.FormatDateTime, endDate+" "+endTime)
  751. if e != nil {
  752. failDataList = append(failDataList, &knowledge.KnowledgeImportFail{
  753. //Id: 0,
  754. Title: title,
  755. Content: content,
  756. SourceFrom: sourceFrom,
  757. ClassifyFirst: classifyName1,
  758. ClassifySecond: classifyName2,
  759. ClassifyThird: classifyName3,
  760. ResourceType: resourceType,
  761. StartDate: startDate,
  762. EndDate: endDate,
  763. StartTime: startTimeO,
  764. EndTime: endTimeO,
  765. Remark: "日期格式异常",
  766. SysUserId: strconv.Itoa(sysUserId),
  767. CreateTime: time.Now(),
  768. })
  769. continue
  770. }
  771. }
  772. }
  773. }
  774. resourceItem := knowledge.ResourceImportData{
  775. Title: title,
  776. StartDate: startDate,
  777. EndDate: endDate,
  778. StartTime: startTime,
  779. EndTime: endTime,
  780. Content: content,
  781. Tag: tag,
  782. SourceFrom: sourceFrom,
  783. ClassifyFirstName: classifyName1,
  784. ClassifySecondName: classifyName2,
  785. ClassifyThirdName: classifyName3,
  786. }
  787. indexDataList = append(indexDataList, resourceItem)
  788. }
  789. return
  790. }
  791. // getDataByTemplatePoint
  792. func getDataByTemplatePoint(sheet *xlsx.Sheet, sysUserId, resourceType int) (indexDataList []knowledge.ResourceImportData, failDataList []*knowledge.KnowledgeImportFail, err error, errMsg string) {
  793. fmt.Println("sheet name: ", sheet.Name)
  794. indexDataList = make([]knowledge.ResourceImportData, 0)
  795. failDataList = make([]*knowledge.KnowledgeImportFail, 0)
  796. //遍历行读取
  797. maxRow := sheet.MaxRow
  798. fmt.Println("maxRow:", maxRow)
  799. // 表头信息
  800. if maxRow <= 3 {
  801. errMsg = "模板异常1"
  802. err = errors.New(errMsg)
  803. return
  804. }
  805. // 表头校验
  806. {
  807. headerRow := sheet.Row(1)
  808. cells := headerRow.Cells
  809. fmt.Println("cells:", len(cells))
  810. if len(cells) < 5 {
  811. errMsg = "导入文件异常,请下载最新导入模板文件"
  812. err = errors.New(errMsg)
  813. return
  814. }
  815. // 循环打印每个单元格
  816. for k, v := range cells {
  817. fmt.Println("第", k, "个单元格", v.Value)
  818. }
  819. templateFail := false
  820. if cells[0].Value != "时间" {
  821. templateFail = true
  822. }
  823. if cells[1].Value != "标题" {
  824. templateFail = true
  825. }
  826. if cells[2].Value != "正文" {
  827. templateFail = true
  828. }
  829. if cells[3].Value != "来源" {
  830. templateFail = true
  831. }
  832. if cells[4].Value != "一级分类" {
  833. templateFail = true
  834. }
  835. if cells[5].Value != "二级分类" {
  836. templateFail = true
  837. }
  838. if cells[6].Value != "三级分类" {
  839. templateFail = true
  840. }
  841. if cells[7].Value != "标签" {
  842. templateFail = true
  843. }
  844. if templateFail {
  845. fmt.Println("头部信息校验失败")
  846. errMsg = "导入文件异常,请下载最新导入模板文件"
  847. err = errors.New(errMsg)
  848. return
  849. }
  850. }
  851. for i := 2; i < maxRow; i++ {
  852. row := sheet.Row(i)
  853. cells := row.Cells
  854. lenCell := len(cells)
  855. // 过滤空白行
  856. if lenCell <= 0 {
  857. continue
  858. }
  859. var startDateO string
  860. var startDate string
  861. var title, content, sourceFrom, classifyName1, classifyName2, classifyName3, tag string
  862. for k, v := range cells {
  863. fmt.Println("数据第行", i, "第", k, "个单元格", v.Value)
  864. if k == 0 {
  865. startDate = strings.TrimSpace(cells[0].Value) //开始时间
  866. startDateO = startDate
  867. } else if k == 1 {
  868. title = strings.TrimSpace(cells[1].Value) //标题
  869. } else if k == 2 {
  870. content = strings.TrimSpace(cells[2].Value) //正文
  871. } else if k == 3 {
  872. sourceFrom = strings.TrimSpace(cells[3].Value) //来源
  873. } else if k == 4 {
  874. classifyName1 = strings.TrimSpace(cells[4].Value) //一级分类
  875. } else if k == 5 {
  876. classifyName2 = strings.TrimSpace(cells[5].Value) //二级分类
  877. } else if k == 6 {
  878. classifyName3 = strings.TrimSpace(cells[6].Value) //三级分类
  879. } else if k == 7 {
  880. tag = strings.TrimSpace(cells[7].Value) //标签
  881. }
  882. }
  883. if title == "" && startDate == "" && content == "" && tag == "" && sourceFrom == "" && classifyName1 == "" && classifyName2 == "" && classifyName3 == "" { //过滤空白行
  884. continue
  885. }
  886. // 校验日期格式
  887. if startDate != "" {
  888. // 判断9:09:14日期格式是否正确
  889. // 判断是否是数字
  890. startDateVal, tmpErr := strconv.ParseFloat(startDate, 64)
  891. if tmpErr == nil {
  892. startDate = xlsx.TimeFromExcelTime(startDateVal, false).Format(utils.FormatDate)
  893. fmt.Println(startDate)
  894. } else {
  895. startDate, tmpErr = getExcelDate(startDate)
  896. if tmpErr != nil {
  897. failDataList = append(failDataList, &knowledge.KnowledgeImportFail{
  898. //Id: 0,
  899. Title: title,
  900. Content: content,
  901. SourceFrom: sourceFrom,
  902. ClassifyFirst: classifyName1,
  903. ClassifySecond: classifyName2,
  904. ClassifyThird: classifyName3,
  905. ResourceType: resourceType,
  906. StartDate: startDateO,
  907. Remark: "日期格式异常",
  908. SysUserId: strconv.Itoa(sysUserId),
  909. CreateTime: time.Now(),
  910. })
  911. continue
  912. }
  913. }
  914. }
  915. resourceItem := knowledge.ResourceImportData{
  916. Title: title,
  917. StartDate: startDate,
  918. Content: content,
  919. Tag: tag,
  920. SourceFrom: sourceFrom,
  921. ClassifyFirstName: classifyName1,
  922. ClassifySecondName: classifyName2,
  923. ClassifyThirdName: classifyName3,
  924. }
  925. indexDataList = append(indexDataList, resourceItem)
  926. }
  927. return
  928. }
  929. func getExcelDate(createDate string) (newCreateDate string, err error) {
  930. if strings.Contains(createDate, "-") {
  931. //如果是带有 - 的普通日期格式文本
  932. _, err = time.Parse("2006-1-2", createDate)
  933. if err == nil {
  934. newCreateDate = createDate
  935. }
  936. } else if strings.Contains(createDate, "/") {
  937. //如果是带有 / 的普通日期格式文本
  938. createDateTime, timeErr := time.Parse("2006/1/2", createDate)
  939. if timeErr != nil {
  940. err = timeErr
  941. } else {
  942. newCreateDate = createDateTime.Format("2006-01-02")
  943. }
  944. } else {
  945. //可能是excel的日期格式
  946. _, tmpErr := strconv.Atoi(createDate)
  947. if tmpErr != nil {
  948. err = tmpErr
  949. } else {
  950. newCreateDate = utils.ConvertToFormatDay(createDate) //录入日期
  951. }
  952. }
  953. return
  954. }
  955. func addExcelResource(req *knowledge.AddReq, sysUser *system.Admin) (item *knowledge.KnowledgeResource, err error, errMsg string) {
  956. var startTime time.Time
  957. var endTime time.Time
  958. if req.StartTime != "" {
  959. if req.ResourceType == knowledge.KnowledgeResourceTypeEvent || req.ResourceType == knowledge.KnowledgeResourceTypePolicy {
  960. startTime, err = time.ParseInLocation(utils.FormatDateTime, req.StartTime, time.Local)
  961. if err != nil { // 如果时间格式不正确,则返回错误信息
  962. errMsg = "开始时间格式不正确"
  963. err = fmt.Errorf("开始时间格式不正确,Err:" + err.Error())
  964. return
  965. }
  966. } else {
  967. startTime, err = time.ParseInLocation(utils.FormatDate, req.StartTime, time.Local)
  968. if err != nil { // 如果时间格式不正确,则返回错误信息
  969. errMsg = "开始时间格式不正确"
  970. err = fmt.Errorf("开始时间格式不正确,Err:" + err.Error())
  971. return
  972. }
  973. }
  974. }
  975. if req.EndTime != "" {
  976. if req.ResourceType == knowledge.KnowledgeResourceTypeEvent || req.ResourceType == knowledge.KnowledgeResourceTypePolicy {
  977. endTime, err = time.ParseInLocation(utils.FormatDateTime, req.EndTime, time.Local)
  978. if err != nil { // 如果时间格式不正确,则返回错误信息
  979. errMsg = "结束时间格式不正确"
  980. err = fmt.Errorf("结束时间格式不正确,Err:" + err.Error())
  981. return
  982. }
  983. }
  984. }
  985. item = new(knowledge.KnowledgeResource)
  986. item.ClassifyId = req.ClassifyId
  987. item.TagId = req.TagId
  988. item.SourceFrom = req.SourceFrom
  989. item.ResourceType = req.ResourceType
  990. item.Title = req.Title
  991. item.State = knowledge.KnowledgeResourceStateUnpublished
  992. item.Content = html.EscapeString(req.Content)
  993. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  994. item.ResourceCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
  995. //todo 内容前5行
  996. //item.ContentSub = html.EscapeString(contentSub)
  997. if !startTime.IsZero() {
  998. item.StartTime = &startTime
  999. }
  1000. if !endTime.IsZero() {
  1001. item.EndTime = &endTime
  1002. }
  1003. item.ModifyTime = time.Now()
  1004. item.AdminId = sysUser.AdminId
  1005. item.AdminRealName = sysUser.RealName
  1006. //item.LastModifyAdminId = sysUser.AdminId
  1007. //item.LastModifyAdminName = sysUser.RealName
  1008. item.ModifyTime = time.Now()
  1009. item.CreateTime = time.Now()
  1010. err = item.Add(item)
  1011. if err != nil {
  1012. errMsg = "保存失败"
  1013. err = fmt.Errorf("保存失败,Err:" + err.Error())
  1014. return
  1015. }
  1016. //todo 是否需要保存到es中
  1017. return
  1018. }
  1019. func SetKnowledgeResourcePublic(knowledgeId int) (errMsg string, err error) {
  1020. obj := new(knowledge.KnowledgeResource)
  1021. obj, err = obj.GetById(knowledgeId)
  1022. if err != nil {
  1023. if utils.IsErrNoRow(err) {
  1024. errMsg = "资源不存在"
  1025. return
  1026. }
  1027. errMsg = "获取资源失败"
  1028. err = fmt.Errorf("获取资源失败,Err:%v", err.Error())
  1029. return
  1030. }
  1031. if !(obj.State == knowledge.KnowledgeResourceStateUnpublished || obj.State == knowledge.KnowledgeResourceStateRejected) {
  1032. errMsg = "不能重复设置公共"
  1033. err = fmt.Errorf("不能重复设置公共")
  1034. return
  1035. }
  1036. if obj.ResourceType != knowledge.KnowledgeResourceTypeOpinion {
  1037. errMsg = "非法操作"
  1038. err = fmt.Errorf("非法操作")
  1039. return
  1040. }
  1041. flowOb := new(knowledge_approve.KnowledgeResourceApproveFlow)
  1042. flowCond := fmt.Sprintf(` AND %s = ?`, knowledge_approve.KnowledgeResourceApproveFlowCols.ClassifyId)
  1043. flowPars := make([]interface{}, 0)
  1044. flowPars = append(flowPars, obj.ClassifyId)
  1045. flowItem, e := flowOb.GetItemByCondition(flowCond, flowPars, "")
  1046. var opening bool
  1047. if e != nil {
  1048. if utils.IsErrNoRow(e) {
  1049. opening = false
  1050. errMsg = "分类未开启审批流"
  1051. err = fmt.Errorf("分类未开启审批流")
  1052. return
  1053. }
  1054. err = fmt.Errorf("ApproveFlow GetItemByCondition err: %s", e.Error())
  1055. return
  1056. }
  1057. if flowItem != nil && flowItem.KnowledgeResourceApproveFlowId > 0 {
  1058. opening = true
  1059. }
  1060. if !opening {
  1061. errMsg = "分类未开启审批流"
  1062. err = fmt.Errorf("分类未开启审批流")
  1063. return
  1064. }
  1065. obj.State = knowledge.KnowledgeResourceStatePending
  1066. obj.ModifyTime = time.Now()
  1067. err = obj.Update([]string{"state", "modify_time"})
  1068. if err != nil {
  1069. errMsg = "设置失败"
  1070. err = fmt.Errorf("设置失败,Err:%s", err.Error())
  1071. return
  1072. }
  1073. return
  1074. }
  1075. func GetKnowledgeResourceTmpList(dashboardDetailId, adminId int) (resp *knowledge.BiDashboardResourceAddResp, msg string, err error) {
  1076. if dashboardDetailId <= 0 {
  1077. data, er := utils.Rc.RedisBytes(utils.CACHE_KEY_BI_DASHBOARD_PREPARE_RESOURCE + strconv.Itoa(adminId))
  1078. req := new(knowledge.BiDashboardResourceAddReq)
  1079. if er != nil {
  1080. if er == redis.Nil {
  1081. msg = "暂无知识库"
  1082. err = nil
  1083. return
  1084. }
  1085. msg = "获取知识库模板列表失败"
  1086. return
  1087. }
  1088. if err = json.Unmarshal(data, &req); err != nil {
  1089. msg = "获取知识库模板列表失败"
  1090. }
  1091. selectedIds := make([]int, 0)
  1092. for _, item := range req.KnowledgeResourceList {
  1093. selectedIds = append(selectedIds, item.KnowledgeResourceId)
  1094. }
  1095. knowledgeList, er := knowledge.GetKnowledgeResourceByIds(selectedIds)
  1096. if er != nil {
  1097. msg = "获取知识库模板列表失败"
  1098. return
  1099. }
  1100. resp = new(knowledge.BiDashboardResourceAddResp)
  1101. resp.KnowledgeResourceList = toKnowledgeResourceList(knowledgeList)
  1102. } else {
  1103. condition := ` AND bi_dashboard_detail_id = ? `
  1104. pars := []interface{}{dashboardDetailId}
  1105. resourceIds, er := knowledge.GetKnowledgeResourceIdsByCondition(condition, pars)
  1106. if er != nil {
  1107. msg = "获取知识库模板列表失败"
  1108. return
  1109. }
  1110. if len(resourceIds) == 0 {
  1111. msg = "暂无知识库"
  1112. return
  1113. }
  1114. resp = new(knowledge.BiDashboardResourceAddResp)
  1115. knowledgeList, er := knowledge.GetKnowledgeResourceByIds(resourceIds)
  1116. if er != nil {
  1117. msg = "获取知识库模板列表失败"
  1118. return
  1119. }
  1120. resp.BiDashboardDetailId = dashboardDetailId
  1121. resp.KnowledgeResourceList = toKnowledgeResourceList(knowledgeList)
  1122. }
  1123. return
  1124. }
  1125. func GetKnowledgeResourceList(dashboardDetailId int) (resp *knowledge.KnowledgeResourceListResp, msg string, err error) {
  1126. // var startSize int
  1127. // if pageSize <= 0 {
  1128. // pageSize = utils.PageSize20
  1129. // }
  1130. // if currentIndex <= 0 {
  1131. // currentIndex = 1
  1132. // }
  1133. // startSize = utils.StartIndex(currentIndex, pageSize)
  1134. condition := ` AND bi_dashboard_detail_id = ? `
  1135. var pars = []interface{}{dashboardDetailId}
  1136. resourceIds, err := knowledge.GetKnowledgeResourceIdsByCondition(condition, pars)
  1137. if err != nil {
  1138. msg = "获取知识库列表失败"
  1139. return
  1140. }
  1141. if len(resourceIds) == 0 {
  1142. resp = &knowledge.KnowledgeResourceListResp{
  1143. List: make([]*knowledge.KnowledgeResourceList, 0),
  1144. Paging: &paging.PagingItem{},
  1145. }
  1146. return
  1147. }
  1148. knowledgeList, err := knowledge.GetKnowledgeResourceByIds(resourceIds)
  1149. if err != nil {
  1150. msg = "获取知识库列表失败"
  1151. return
  1152. }
  1153. resp = new(knowledge.KnowledgeResourceListResp)
  1154. resp.List = toKnowledgeResourceList(knowledgeList)
  1155. return
  1156. }
  1157. func toKnowledgeResourceList(list []*knowledge.KnowledgeResource) (res []*knowledge.KnowledgeResourceList) {
  1158. for _, item := range list {
  1159. resItem := &knowledge.KnowledgeResourceList{
  1160. KnowledgeResourceId: item.KnowledgeResourceId,
  1161. ResourceType: item.ResourceType,
  1162. ClassifyId: item.ClassifyId,
  1163. Title: item.Title,
  1164. State: item.State,
  1165. ResourceCode: item.ResourceCode,
  1166. AdminId: item.AdminId,
  1167. AdminRealName: item.AdminRealName,
  1168. SourceFrom: item.SourceFrom,
  1169. TagId: item.TagId,
  1170. CreateTime: utils.TimeTransferString(utils.FormatDateTime, item.CreateTime),
  1171. ModifyTime: utils.TimeTransferString(utils.FormatDateTime, item.ModifyTime),
  1172. }
  1173. if item.StartTime != nil {
  1174. if item.ResourceType == knowledge.KnowledgeResourceTypeEvent || item.ResourceType == knowledge.KnowledgeResourceTypePolicy {
  1175. resItem.StartTime = utils.TimeTransferString(utils.FormatDateTime, *item.StartTime)
  1176. } else {
  1177. resItem.StartTime = utils.TimeTransferString(utils.FormatDate, *item.StartTime)
  1178. }
  1179. }
  1180. if item.EndTime != nil {
  1181. if item.ResourceType == knowledge.KnowledgeResourceTypeEvent || item.ResourceType == knowledge.KnowledgeResourceTypePolicy {
  1182. resItem.EndTime = utils.TimeTransferString(utils.FormatDateTime, *item.EndTime)
  1183. } else {
  1184. resItem.EndTime = utils.TimeTransferString(utils.FormatDate, *item.EndTime)
  1185. }
  1186. }
  1187. res = append(res, resItem)
  1188. }
  1189. return
  1190. }