resource.go 32 KB

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