resource.go 32 KB

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