resource.go 38 KB

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