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 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. for _, v := range indexDataList {
  392. errTip := ""
  393. classifyId := 0
  394. if successCount > 100 {
  395. errTip = "导入数量超过100条"
  396. } else if v.Title == "" {
  397. errTip = "标题未填"
  398. } else if len(v.Title) > 600 {
  399. errTip = "标题过长"
  400. } else if v.Content == "" {
  401. errTip = "正文未填"
  402. } else if v.SourceFrom == "" {
  403. errTip = "来源未填"
  404. } else if v.ClassifyFirstName == "" {
  405. errTip = "分类未填"
  406. } else if v.StartDate == "" || v.StartTime == "" {
  407. if resourceType == knowledge.KnowledgeResourceTypeEvent || resourceType == knowledge.KnowledgeResourceTypePolicy {
  408. errTip = "开始时间未填"
  409. } else if v.StartDate == "" {
  410. errTip = "时间未填"
  411. }
  412. }
  413. if errTip == "" {
  414. // 判断分类是否存在
  415. classifyName := fmt.Sprintf("%s/%s/%s", v.ClassifyFirstName, v.ClassifySecondName, v.ClassifyThirdName)
  416. fmt.Println("classifyName: ", classifyName)
  417. classifyIdTmp, ok := classifyNameMap[classifyName]
  418. if !ok {
  419. errTip = "分类不存在"
  420. }
  421. classifyId = classifyIdTmp
  422. }
  423. if errTip != "" {
  424. failItem := &knowledge.KnowledgeImportFail{
  425. Title: v.Title,
  426. Content: v.Content,
  427. SourceFrom: v.SourceFrom,
  428. ClassifyFirst: v.ClassifyFirstName,
  429. ClassifySecond: v.ClassifySecondName,
  430. ClassifyThird: v.ClassifyThirdName,
  431. ResourceType: resourceType,
  432. StartDate: v.StartDate,
  433. EndDate: v.EndDate,
  434. StartTime: v.StartTime,
  435. EndTime: v.EndTime,
  436. Remark: errTip,
  437. SysUserId: fmt.Sprint(sysUser.AdminId),
  438. CreateTime: time.Now(),
  439. }
  440. failDataList = append(failDataList, failItem)
  441. continue
  442. }
  443. tagId, _ := tagNameMap[v.Tag]
  444. req := new(knowledge.AddReq)
  445. req.Title = v.Title
  446. req.Content = v.Content
  447. req.SourceFrom = v.SourceFrom
  448. req.ClassifyId = classifyId
  449. req.TagId = tagId
  450. req.ResourceType = resourceType
  451. if v.StartDate != "" {
  452. if resourceType == knowledge.KnowledgeResourceTypeEvent || resourceType == knowledge.KnowledgeResourceTypePolicy {
  453. req.StartTime = fmt.Sprintf("%s %s", v.StartDate, v.StartTime)
  454. } else {
  455. req.StartTime = v.StartDate
  456. }
  457. }
  458. if v.EndDate != "" {
  459. if resourceType == knowledge.KnowledgeResourceTypeEvent || resourceType == knowledge.KnowledgeResourceTypePolicy {
  460. req.EndTime = fmt.Sprintf("%s %s", v.EndDate, v.EndTime)
  461. }
  462. }
  463. tmpErr, msg := addExcelResource(req, sysUser)
  464. if tmpErr != nil {
  465. failItem := &knowledge.KnowledgeImportFail{
  466. Title: v.Title,
  467. Content: v.Content,
  468. SourceFrom: v.SourceFrom,
  469. ClassifyFirst: v.ClassifyFirstName,
  470. ClassifySecond: v.ClassifySecondName,
  471. ClassifyThird: v.ClassifyThirdName,
  472. ResourceType: resourceType,
  473. StartDate: v.StartDate,
  474. EndDate: v.EndDate,
  475. StartTime: v.StartTime,
  476. EndTime: v.EndTime,
  477. Remark: "新增事件失败:" + msg,
  478. SysUserId: fmt.Sprint(sysUser.AdminId),
  479. CreateTime: time.Now(),
  480. }
  481. failDataList = append(failDataList, failItem)
  482. continue
  483. } else {
  484. successCount++
  485. }
  486. }
  487. // 失败数量
  488. failCount = len(failDataList)
  489. //fmt.Println("failDataList:", len(failDataList))
  490. if failCount > 0 {
  491. failObj := new(knowledge.KnowledgeImportFail)
  492. //先删除导入失败记录
  493. _ = failObj.Delete(sysUser.AdminId, resourceType)
  494. // 批量添加导入失败记录
  495. err = failObj.MultiAdd(failDataList)
  496. if err != nil {
  497. go alarm_msg.SendAlarmMsg("导入数据 新增失败记录失败,Err:"+err.Error(), 3)
  498. }
  499. {
  500. //错误信息字符串切片,最后作为发送邮件通知使用
  501. failContents := make([]string, 0)
  502. for _, v := range failDataList {
  503. failContents = append(failContents, fmt.Sprint(v.Title, "导入失败:", v.Remark))
  504. }
  505. utils.FileLog.Info("导入数据 存在部分数据导入失败:" + strings.Join(failContents, ";"))
  506. //导入失败的话,最后统一邮件提醒就好啦,不需要每次都去提醒
  507. go alarm_msg.SendAlarmMsg("导入数据 存在部分数据导入失败:"+strings.Join(failContents, ";"), 3)
  508. }
  509. }
  510. return
  511. }
  512. // getDataByTemplateEvent
  513. func getDataByTemplateEvent(sheet *xlsx.Sheet, sysUserId, resourceType int) (indexDataList []knowledge.ResourceImportData, failDataList []*knowledge.KnowledgeImportFail, err error, errMsg string) {
  514. fmt.Println("sheet name: ", sheet.Name)
  515. indexDataList = make([]knowledge.ResourceImportData, 0)
  516. failDataList = make([]*knowledge.KnowledgeImportFail, 0)
  517. //遍历行读取
  518. maxRow := sheet.MaxRow
  519. fmt.Println("maxRow:", maxRow)
  520. // 表头信息
  521. if maxRow <= 3 {
  522. errMsg = "模板异常1"
  523. err = errors.New(errMsg)
  524. return
  525. }
  526. // 表头校验
  527. {
  528. headerRow := sheet.Row(1)
  529. cells := headerRow.Cells
  530. fmt.Println("cells:", len(cells))
  531. if len(cells) < 11 {
  532. errMsg = "导入文件异常,请下载最新导入模板文件"
  533. err = errors.New(errMsg)
  534. return
  535. }
  536. // 循环打印每个单元格
  537. for k, v := range cells {
  538. fmt.Println("第", k, "个单元格", v.Value)
  539. }
  540. templateFail := false
  541. if cells[0].Value != "开始时间" {
  542. templateFail = true
  543. }
  544. if cells[2].Value != "结束时间(非必填)" {
  545. templateFail = true
  546. }
  547. if cells[4].Value != "标题" {
  548. templateFail = true
  549. }
  550. if cells[5].Value != "正文" {
  551. templateFail = true
  552. }
  553. if cells[6].Value != "来源" {
  554. templateFail = true
  555. }
  556. if cells[7].Value != "一级分类" {
  557. templateFail = true
  558. }
  559. if cells[8].Value != "二级分类" {
  560. templateFail = true
  561. }
  562. if cells[9].Value != "三级分类" {
  563. templateFail = true
  564. }
  565. if cells[10].Value != "标签" {
  566. templateFail = true
  567. }
  568. headerRow2 := sheet.Row(2)
  569. cells2 := headerRow2.Cells
  570. for k, v := range cells2 {
  571. fmt.Println("2第", k, "个单元格", v.Value)
  572. }
  573. if len(cells2) < 11 {
  574. templateFail = true
  575. } else {
  576. if cells2[0].Value != "日期" {
  577. templateFail = true
  578. }
  579. if cells2[1].Value != "时间" {
  580. templateFail = true
  581. }
  582. if cells2[2].Value != "日期" {
  583. templateFail = true
  584. }
  585. if cells2[3].Value != "时间" {
  586. templateFail = true
  587. }
  588. }
  589. if templateFail {
  590. fmt.Println("头部信息校验失败")
  591. errMsg = "导入文件异常,请下载最新导入模板文件"
  592. err = errors.New(errMsg)
  593. return
  594. }
  595. }
  596. for i := 3; i < maxRow; i++ {
  597. row := sheet.Row(i)
  598. cells := row.Cells
  599. lenCell := len(cells)
  600. // 过滤空白行
  601. if lenCell <= 0 {
  602. continue
  603. }
  604. var startDateO, endDateO, startTimeO, endTimeO string
  605. var startDate, startTime, endDate, endTime string
  606. var title, content, sourceFrom, classifyName1, classifyName2, classifyName3, tag string
  607. for k, v := range cells {
  608. fmt.Println("数据第行", i, "第", k, "个单元格", v.Value)
  609. if k == 0 {
  610. startDate = strings.TrimSpace(cells[0].Value) //开始时间
  611. startDateO = startDate
  612. } else if k == 1 {
  613. startTime = strings.TrimSpace(cells[1].Value) //开始时间
  614. startTimeO = startTime
  615. } else if k == 2 {
  616. endDate = strings.TrimSpace(cells[2].Value) //结束时间
  617. endDateO = endDate
  618. } else if k == 3 {
  619. endTime = strings.TrimSpace(cells[3].Value) //结束时间
  620. endTimeO = endTime
  621. } else if k == 4 {
  622. title = strings.TrimSpace(cells[4].Value) //标题
  623. } else if k == 5 {
  624. content = strings.TrimSpace(cells[5].Value) //正文
  625. } else if k == 6 {
  626. sourceFrom = strings.TrimSpace(cells[6].Value) //来源
  627. } else if k == 7 {
  628. classifyName1 = strings.TrimSpace(cells[7].Value) //一级分类
  629. } else if k == 8 {
  630. classifyName2 = strings.TrimSpace(cells[8].Value) //二级分类
  631. } else if k == 9 {
  632. classifyName3 = strings.TrimSpace(cells[9].Value) //三级分类
  633. } else if k == 10 {
  634. tag = strings.TrimSpace(cells[10].Value) //标签
  635. }
  636. }
  637. if title == "" && startDate == "" && startTime == "" && endTime == "" && endDate == "" && content == "" && tag == "" && sourceFrom == "" && classifyName1 == "" && classifyName2 == "" && classifyName3 == "" { //过滤空白行
  638. continue
  639. }
  640. fmt.Println("lenCell:", lenCell)
  641. // 校验日期格式
  642. var startDateVal, endDateVal, startTimeVal float64
  643. var tmpErr error
  644. if startDate != "" {
  645. // 判断9:09:14日期格式是否正确
  646. // 判断是否是数字
  647. startDateVal, tmpErr = strconv.ParseFloat(startDate, 64)
  648. if tmpErr == nil {
  649. startDate = xlsx.TimeFromExcelTime(startDateVal, false).Format(utils.FormatDate)
  650. fmt.Println(startDate)
  651. } else {
  652. startDate, tmpErr = getExcelDate(startDate)
  653. if tmpErr != nil {
  654. failDataList = append(failDataList, &knowledge.KnowledgeImportFail{
  655. //Id: 0,
  656. Title: title,
  657. Content: content,
  658. SourceFrom: sourceFrom,
  659. ClassifyFirst: classifyName1,
  660. ClassifySecond: classifyName2,
  661. ClassifyThird: classifyName3,
  662. ResourceType: resourceType,
  663. StartDate: startDateO,
  664. EndDate: endDateO,
  665. StartTime: startTimeO,
  666. EndTime: endTimeO,
  667. Remark: "日期格式异常",
  668. SysUserId: strconv.Itoa(sysUserId),
  669. CreateTime: time.Now(),
  670. })
  671. continue
  672. }
  673. }
  674. if startTime != "" {
  675. // 判断9:09:14日期格式是否正确
  676. startTimeVal, tmpErr = strconv.ParseFloat(startTime, 64)
  677. if tmpErr == nil {
  678. startTimeVal = startDateVal + startTimeVal
  679. startTime = xlsx.TimeFromExcelTime(startTimeVal, false).Format(utils.FormatTime)
  680. fmt.Println(startTime)
  681. } else {
  682. _, tmpErr = time.Parse(utils.FormatDateTime, startDate+" "+startTime)
  683. if tmpErr != nil {
  684. failDataList = append(failDataList, &knowledge.KnowledgeImportFail{
  685. //Id: 0,
  686. Title: title,
  687. Content: content,
  688. SourceFrom: sourceFrom,
  689. ClassifyFirst: classifyName1,
  690. ClassifySecond: classifyName2,
  691. ClassifyThird: classifyName3,
  692. ResourceType: resourceType,
  693. StartDate: startDateO,
  694. EndDate: endDateO,
  695. StartTime: startTimeO,
  696. EndTime: endTimeO,
  697. Remark: "日期格式异常",
  698. SysUserId: strconv.Itoa(sysUserId),
  699. CreateTime: time.Now(),
  700. })
  701. continue
  702. }
  703. }
  704. }
  705. }
  706. if endDate != "" {
  707. endDateVal, tmpErr = strconv.ParseFloat(endDate, 64)
  708. if tmpErr == nil {
  709. endDate = xlsx.TimeFromExcelTime(endDateVal, false).Format(utils.FormatDate)
  710. fmt.Println(endDate)
  711. } else {
  712. endDate, tmpErr = getExcelDate(endDate)
  713. if tmpErr != nil {
  714. failDataList = append(failDataList, &knowledge.KnowledgeImportFail{
  715. //Id: 0,
  716. Title: title,
  717. Content: content,
  718. SourceFrom: sourceFrom,
  719. ClassifyFirst: classifyName1,
  720. ClassifySecond: classifyName2,
  721. ClassifyThird: classifyName3,
  722. ResourceType: resourceType,
  723. StartDate: startDateO,
  724. EndDate: endDateO,
  725. StartTime: startTimeO,
  726. EndTime: endTimeO,
  727. Remark: "日期格式异常",
  728. SysUserId: strconv.Itoa(sysUserId),
  729. CreateTime: time.Now(),
  730. })
  731. continue
  732. }
  733. }
  734. if endTime != "" {
  735. // 判断9:09:14日期格式是否正确
  736. endTimeVal, e := strconv.ParseFloat(endTime, 64)
  737. if e == nil {
  738. endTimeVal = endDateVal + endTimeVal
  739. endTime = xlsx.TimeFromExcelTime(endTimeVal, false).Format(utils.FormatTime)
  740. fmt.Println(endTime)
  741. } else {
  742. _, e = time.Parse(utils.FormatDateTime, endDate+" "+endTime)
  743. if e != nil {
  744. failDataList = append(failDataList, &knowledge.KnowledgeImportFail{
  745. //Id: 0,
  746. Title: title,
  747. Content: content,
  748. SourceFrom: sourceFrom,
  749. ClassifyFirst: classifyName1,
  750. ClassifySecond: classifyName2,
  751. ClassifyThird: classifyName3,
  752. ResourceType: resourceType,
  753. StartDate: startDate,
  754. EndDate: endDate,
  755. StartTime: startTimeO,
  756. EndTime: endTimeO,
  757. Remark: "日期格式异常",
  758. SysUserId: strconv.Itoa(sysUserId),
  759. CreateTime: time.Now(),
  760. })
  761. continue
  762. }
  763. }
  764. }
  765. }
  766. resourceItem := knowledge.ResourceImportData{
  767. Title: title,
  768. StartDate: startDate,
  769. EndDate: endDate,
  770. StartTime: startTime,
  771. EndTime: endTime,
  772. Content: content,
  773. Tag: tag,
  774. SourceFrom: sourceFrom,
  775. ClassifyFirstName: classifyName1,
  776. ClassifySecondName: classifyName2,
  777. ClassifyThirdName: classifyName3,
  778. }
  779. indexDataList = append(indexDataList, resourceItem)
  780. }
  781. return
  782. }
  783. // getDataByTemplatePoint
  784. func getDataByTemplatePoint(sheet *xlsx.Sheet, sysUserId, resourceType int) (indexDataList []knowledge.ResourceImportData, failDataList []*knowledge.KnowledgeImportFail, err error, errMsg string) {
  785. fmt.Println("sheet name: ", sheet.Name)
  786. indexDataList = make([]knowledge.ResourceImportData, 0)
  787. failDataList = make([]*knowledge.KnowledgeImportFail, 0)
  788. //遍历行读取
  789. maxRow := sheet.MaxRow
  790. fmt.Println("maxRow:", maxRow)
  791. // 表头信息
  792. if maxRow <= 3 {
  793. errMsg = "模板异常1"
  794. err = errors.New(errMsg)
  795. return
  796. }
  797. // 表头校验
  798. {
  799. headerRow := sheet.Row(1)
  800. cells := headerRow.Cells
  801. fmt.Println("cells:", len(cells))
  802. if len(cells) < 5 {
  803. errMsg = "导入文件异常,请下载最新导入模板文件"
  804. err = errors.New(errMsg)
  805. return
  806. }
  807. // 循环打印每个单元格
  808. for k, v := range cells {
  809. fmt.Println("第", k, "个单元格", v.Value)
  810. }
  811. templateFail := false
  812. if cells[0].Value != "时间" {
  813. templateFail = true
  814. }
  815. if cells[1].Value != "标题" {
  816. templateFail = true
  817. }
  818. if cells[2].Value != "正文" {
  819. templateFail = true
  820. }
  821. if cells[3].Value != "来源" {
  822. templateFail = true
  823. }
  824. if cells[4].Value != "一级分类" {
  825. templateFail = true
  826. }
  827. if cells[5].Value != "二级分类" {
  828. templateFail = true
  829. }
  830. if cells[6].Value != "三级分类" {
  831. templateFail = true
  832. }
  833. if cells[7].Value != "标签" {
  834. templateFail = true
  835. }
  836. if templateFail {
  837. fmt.Println("头部信息校验失败")
  838. errMsg = "导入文件异常,请下载最新导入模板文件"
  839. err = errors.New(errMsg)
  840. return
  841. }
  842. }
  843. for i := 2; i < maxRow; i++ {
  844. row := sheet.Row(i)
  845. cells := row.Cells
  846. lenCell := len(cells)
  847. // 过滤空白行
  848. if lenCell <= 0 {
  849. continue
  850. }
  851. var startDateO string
  852. var startDate string
  853. var title, content, sourceFrom, classifyName1, classifyName2, classifyName3, tag string
  854. for k, v := range cells {
  855. fmt.Println("数据第行", i, "第", k, "个单元格", v.Value)
  856. if k == 0 {
  857. startDate = strings.TrimSpace(cells[0].Value) //开始时间
  858. startDateO = startDate
  859. } else if k == 1 {
  860. title = strings.TrimSpace(cells[1].Value) //标题
  861. } else if k == 2 {
  862. content = strings.TrimSpace(cells[2].Value) //正文
  863. } else if k == 3 {
  864. sourceFrom = strings.TrimSpace(cells[3].Value) //来源
  865. } else if k == 4 {
  866. classifyName1 = strings.TrimSpace(cells[4].Value) //一级分类
  867. } else if k == 5 {
  868. classifyName2 = strings.TrimSpace(cells[5].Value) //二级分类
  869. } else if k == 6 {
  870. classifyName3 = strings.TrimSpace(cells[6].Value) //三级分类
  871. } else if k == 7 {
  872. tag = strings.TrimSpace(cells[7].Value) //标签
  873. }
  874. }
  875. if title == "" && startDate == "" && content == "" && tag == "" && sourceFrom == "" && classifyName1 == "" && classifyName2 == "" && classifyName3 == "" { //过滤空白行
  876. continue
  877. }
  878. // 校验日期格式
  879. if startDate != "" {
  880. // 判断9:09:14日期格式是否正确
  881. // 判断是否是数字
  882. startDateVal, tmpErr := strconv.ParseFloat(startDate, 64)
  883. if tmpErr == nil {
  884. startDate = xlsx.TimeFromExcelTime(startDateVal, false).Format(utils.FormatDate)
  885. fmt.Println(startDate)
  886. } else {
  887. startDate, tmpErr = getExcelDate(startDate)
  888. if tmpErr != nil {
  889. failDataList = append(failDataList, &knowledge.KnowledgeImportFail{
  890. //Id: 0,
  891. Title: title,
  892. Content: content,
  893. SourceFrom: sourceFrom,
  894. ClassifyFirst: classifyName1,
  895. ClassifySecond: classifyName2,
  896. ClassifyThird: classifyName3,
  897. ResourceType: resourceType,
  898. StartDate: startDateO,
  899. Remark: "日期格式异常",
  900. SysUserId: strconv.Itoa(sysUserId),
  901. CreateTime: time.Now(),
  902. })
  903. continue
  904. }
  905. }
  906. }
  907. resourceItem := knowledge.ResourceImportData{
  908. Title: title,
  909. StartDate: startDate,
  910. Content: content,
  911. Tag: tag,
  912. SourceFrom: sourceFrom,
  913. ClassifyFirstName: classifyName1,
  914. ClassifySecondName: classifyName2,
  915. ClassifyThirdName: classifyName3,
  916. }
  917. indexDataList = append(indexDataList, resourceItem)
  918. }
  919. return
  920. }
  921. func getExcelDate(createDate string) (newCreateDate string, err error) {
  922. if strings.Contains(createDate, "-") {
  923. //如果是带有 - 的普通日期格式文本
  924. _, err = time.Parse("2006-1-2", createDate)
  925. if err == nil {
  926. newCreateDate = createDate
  927. }
  928. } else if strings.Contains(createDate, "/") {
  929. //如果是带有 / 的普通日期格式文本
  930. createDateTime, timeErr := time.Parse("2006/1/2", createDate)
  931. if timeErr != nil {
  932. err = timeErr
  933. } else {
  934. newCreateDate = createDateTime.Format("2006-01-02")
  935. }
  936. } else {
  937. //可能是excel的日期格式
  938. _, tmpErr := strconv.Atoi(createDate)
  939. if tmpErr != nil {
  940. err = tmpErr
  941. } else {
  942. newCreateDate = utils.ConvertToFormatDay(createDate) //录入日期
  943. }
  944. }
  945. return
  946. }
  947. func addExcelResource(req *knowledge.AddReq, sysUser *system.Admin) (err error, errMsg string) {
  948. var startTime time.Time
  949. var endTime time.Time
  950. if req.StartTime != "" {
  951. if req.ResourceType == knowledge.KnowledgeResourceTypeEvent || req.ResourceType == knowledge.KnowledgeResourceTypePolicy {
  952. startTime, err = time.ParseInLocation(utils.FormatDateTime, req.StartTime, time.Local)
  953. if err != nil { // 如果时间格式不正确,则返回错误信息
  954. errMsg = "开始时间格式不正确"
  955. err = fmt.Errorf("开始时间格式不正确,Err:" + err.Error())
  956. return
  957. }
  958. } else {
  959. startTime, err = time.ParseInLocation(utils.FormatDate, req.StartTime, time.Local)
  960. if err != nil { // 如果时间格式不正确,则返回错误信息
  961. errMsg = "开始时间格式不正确"
  962. err = fmt.Errorf("开始时间格式不正确,Err:" + err.Error())
  963. return
  964. }
  965. }
  966. }
  967. if req.EndTime != "" {
  968. if req.ResourceType == knowledge.KnowledgeResourceTypeEvent || req.ResourceType == knowledge.KnowledgeResourceTypePolicy {
  969. endTime, err = time.ParseInLocation(utils.FormatDateTime, req.EndTime, time.Local)
  970. if err != nil { // 如果时间格式不正确,则返回错误信息
  971. errMsg = "结束时间格式不正确"
  972. err = fmt.Errorf("结束时间格式不正确,Err:" + err.Error())
  973. return
  974. }
  975. }
  976. }
  977. item := new(knowledge.KnowledgeResource)
  978. item.ClassifyId = req.ClassifyId
  979. item.TagId = req.TagId
  980. item.SourceFrom = req.SourceFrom
  981. item.ResourceType = req.ResourceType
  982. item.Title = req.Title
  983. item.State = knowledge.KnowledgeResourceStateUnpublished
  984. item.Content = html.EscapeString(req.Content)
  985. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  986. item.ResourceCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
  987. //todo 内容前5行
  988. //item.ContentSub = html.EscapeString(contentSub)
  989. if !startTime.IsZero() {
  990. item.StartTime = &startTime
  991. }
  992. if !endTime.IsZero() {
  993. item.EndTime = &endTime
  994. }
  995. item.ModifyTime = time.Now()
  996. item.AdminId = sysUser.AdminId
  997. item.AdminRealName = sysUser.RealName
  998. //item.LastModifyAdminId = sysUser.AdminId
  999. //item.LastModifyAdminName = sysUser.RealName
  1000. item.ModifyTime = time.Now()
  1001. item.CreateTime = time.Now()
  1002. err = item.Add(item)
  1003. if err != nil {
  1004. errMsg = "保存失败"
  1005. err = fmt.Errorf("保存失败,Err:" + err.Error())
  1006. return
  1007. }
  1008. //todo 是否需要保存到es中
  1009. return
  1010. }
  1011. func SetKnowledgeResourcePublic(knowledgeId int) (errMsg string, err error) {
  1012. obj := new(knowledge.KnowledgeResource)
  1013. obj, err = obj.GetById(knowledgeId)
  1014. if err != nil {
  1015. if utils.IsErrNoRow(err) {
  1016. errMsg = "资源不存在"
  1017. return
  1018. }
  1019. errMsg = "获取资源失败"
  1020. err = fmt.Errorf("获取资源失败,Err:%v", err.Error())
  1021. return
  1022. }
  1023. if !(obj.State == knowledge.KnowledgeResourceStateUnpublished || obj.State == knowledge.KnowledgeResourceStateRejected) {
  1024. errMsg = "不能重复设置公共"
  1025. err = fmt.Errorf("不能重复设置公共")
  1026. return
  1027. }
  1028. if obj.ResourceType != knowledge.KnowledgeResourceTypeOpinion {
  1029. errMsg = "非法操作"
  1030. err = fmt.Errorf("非法操作")
  1031. return
  1032. }
  1033. flowOb := new(knowledge_approve.KnowledgeResourceApproveFlow)
  1034. flowCond := fmt.Sprintf(` AND %s = ?`, knowledge_approve.KnowledgeResourceApproveFlowCols.ClassifyId)
  1035. flowPars := make([]interface{}, 0)
  1036. flowPars = append(flowPars, obj.ClassifyId)
  1037. flowItem, e := flowOb.GetItemByCondition(flowCond, flowPars, "")
  1038. var opening bool
  1039. if e != nil {
  1040. if utils.IsErrNoRow(e) {
  1041. opening = false
  1042. errMsg = "分类未开启审批流"
  1043. err = fmt.Errorf("分类未开启审批流")
  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. }