resource.go 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568
  1. package knowledge
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/controllers"
  5. "eta_gn/eta_api/models"
  6. "eta_gn/eta_api/models/knowledge"
  7. "eta_gn/eta_api/services"
  8. knowledgeServ "eta_gn/eta_api/services/knowledge"
  9. "eta_gn/eta_api/utils"
  10. "fmt"
  11. "html"
  12. "io/ioutil"
  13. "os"
  14. "path"
  15. "strconv"
  16. "strings"
  17. "time"
  18. "github.com/h2non/filetype"
  19. "github.com/rdlucklib/rdluck_tools/paging"
  20. "github.com/tealeg/xlsx"
  21. )
  22. // 分类
  23. type ResourceController struct {
  24. controllers.BaseAuthController
  25. }
  26. // ListReport
  27. // @Title 获取事件列表接口
  28. // @Description 获取事件列表
  29. // @Param PageSize query int true "每页数据条数"
  30. // @Param CurrentIndex query int true "当前页页码,从1开始"
  31. // @Param TimeType query string true "筛选的时间类别:publish_time(发布时间),modify_time(更新时间);approve_time(审批时间)"
  32. // @Param StartDate query string true "开始时间"
  33. // @Param EndDate query string true "结束时间"
  34. // @Param Frequency query string true "频度"
  35. // @Param ClassifyIds query int true "分类id, 多选, 英文逗号分隔"
  36. // @Param State query int true "状态"
  37. // @Param KeyWord query string true "搜索关键词"
  38. // @Param PublishSort query string true "desc:降序,asc 升序(预留)"
  39. // @Param FilterReportType query string true "筛选事件类型,1:公共研报,2:共享研报,3:我的研报"
  40. // @Param IncludeFile query string true "是否包含文件, no:不包含"
  41. // @Param SourceFrom query string true "来源, 用英文逗号分隔"
  42. // @Param IsShowMe query string true "只看我, false:全部, true:只看我"
  43. // @Success 200 {object} knowledge.KnowledgeResourceListResp
  44. // @router /resource/list [get]
  45. func (this *ResourceController) List() {
  46. br := new(models.BaseResponse).Init()
  47. defer func() {
  48. this.Data["json"] = br
  49. this.ServeJSON()
  50. }()
  51. pageSize, _ := this.GetInt("PageSize")
  52. currentIndex, _ := this.GetInt("CurrentIndex")
  53. classifyIds := this.GetString("ClassifyIds")
  54. // 添加人
  55. addUserIds, _ := this.GetInt("SysUserIds")
  56. // 标签
  57. tagIds := this.GetString("TagIds")
  58. keyWord := this.GetString("Keyword")
  59. resourceType, _ := this.GetInt("ResourceType")
  60. sourceFrom := this.GetString("SourceFrom")
  61. includeFile := this.GetString("IncludeFile")
  62. IsShowMe, _ := this.GetBool("IsShowMe")
  63. var startSize int
  64. if pageSize <= 0 {
  65. pageSize = utils.PageSize20
  66. }
  67. if currentIndex <= 0 {
  68. currentIndex = 1
  69. }
  70. startSize = utils.StartIndex(currentIndex, pageSize)
  71. var condition string
  72. var pars []interface{}
  73. condition += ` AND resource_type = ? `
  74. pars = append(pars, resourceType)
  75. if includeFile == "no" {
  76. condition += ` AND is_file = ? `
  77. pars = append(pars, 0)
  78. }
  79. if IsShowMe {
  80. condition += ` AND admin_id = ? `
  81. pars = append(pars, this.SysUser.AdminId)
  82. }
  83. if keyWord != "" {
  84. //按照空格划分为关键词数组
  85. keyWordArr := strings.Split(keyWord, " ")
  86. lengthKey := len(keyWordArr)
  87. if lengthKey == 1 {
  88. condition += ` AND (title LIKE ?) `
  89. pars = utils.GetLikeKeywordPars(pars, keyWord, 1)
  90. } else if lengthKey > 1 {
  91. for k, v := range keyWordArr {
  92. if k == 0 {
  93. condition += ` AND (title LIKE ? `
  94. pars = utils.GetLikeKeywordPars(pars, v, 1)
  95. } else if k == lengthKey-1 {
  96. condition += ` OR title LIKE ? ) `
  97. pars = utils.GetLikeKeywordPars(pars, v, 1)
  98. } else {
  99. condition += ` OR title LIKE ? `
  100. pars = utils.GetLikeKeywordPars(pars, v, 1)
  101. }
  102. }
  103. }
  104. }
  105. if sourceFrom != "" {
  106. sourceArr := strings.Split(sourceFrom, ",")
  107. condition += ` AND source_from in (?) `
  108. pars = append(pars, sourceArr)
  109. }
  110. if classifyIds != "" {
  111. //转成数组,并把类型转成int
  112. classifyIdsArr := strings.Split(classifyIds, ",")
  113. var classifyIdsInt []int
  114. for _, v := range classifyIdsArr {
  115. classifyId, _ := strconv.Atoi(v)
  116. classifyIdsInt = append(classifyIdsInt, classifyId)
  117. }
  118. condition += ` AND classify_id in (?) `
  119. pars = append(pars, classifyIdsInt)
  120. }
  121. if addUserIds > 0 {
  122. // 转成数组,并把类型转成int
  123. addUserIdsArr := strings.Split(strconv.Itoa(addUserIds), ",")
  124. var addUserIdsInt []int
  125. for _, v := range addUserIdsArr {
  126. addUserId, _ := strconv.Atoi(v)
  127. addUserIdsInt = append(addUserIdsInt, addUserId)
  128. }
  129. condition += ` AND admin_id in (?) `
  130. pars = append(pars, addUserIdsInt)
  131. }
  132. if tagIds != "" {
  133. //转成数组,并把类型转成int
  134. tagIdsArr := strings.Split(tagIds, ",")
  135. var tagIdsInt []int
  136. for _, v := range tagIdsArr {
  137. tagId, _ := strconv.Atoi(v)
  138. tagIdsInt = append(tagIdsInt, tagId)
  139. }
  140. condition += ` AND tag_id in (?) `
  141. pars = append(pars, tagIdsInt)
  142. }
  143. var err error
  144. var total int
  145. var list []*knowledge.KnowledgeResourceList
  146. // 共享事件需要连表查询,所以需要单独写
  147. total, err = knowledge.GetKnowledgeResourceListCount(condition, pars)
  148. if err != nil {
  149. br.Msg = "获取失败"
  150. br.ErrMsg = "获取失败,Err:" + err.Error()
  151. return
  152. }
  153. listTmp, err := knowledge.GetKnowledgeResourcePageList(condition, pars, startSize, pageSize)
  154. if err != nil {
  155. br.Msg = "获取失败"
  156. br.ErrMsg = "获取失败,Err:" + err.Error()
  157. return
  158. }
  159. // 整理分类ID
  160. classifyFullNameMap := make(map[int]string)
  161. classifyIdsArr := make([]int, 0)
  162. for _, v := range listTmp {
  163. classifyIdsArr = append(classifyIdsArr, v.ClassifyId)
  164. }
  165. // 获取分类列表
  166. classifyList, err := knowledge.GetFullClassifyListByIdList(classifyIdsArr, resourceType)
  167. if err != nil {
  168. br.Msg = "获取失败"
  169. br.ErrMsg = "获取失败,Err:" + err.Error()
  170. return
  171. }
  172. for _, v := range classifyList {
  173. name := v.ClassifyName
  174. if v.ParentName != "" {
  175. name = v.ParentName + "/" + name
  176. }
  177. if v.RootName != "" {
  178. name = v.RootName + "/" + name
  179. }
  180. classifyFullNameMap[v.ClassifyId] = name
  181. }
  182. // 整理标签ID
  183. tagNameMap := make(map[int]string)
  184. tagIdsArr := make([]int, 0)
  185. for _, v := range listTmp {
  186. tagIdsArr = append(tagIdsArr, v.TagId)
  187. }
  188. // 获取标签列表
  189. tagObj := new(knowledge.KnowledgeTag)
  190. tagList, err := tagObj.GetListByIdList(tagIdsArr, resourceType)
  191. if err != nil {
  192. br.Msg = "获取失败"
  193. br.ErrMsg = "获取失败,Err:" + err.Error()
  194. }
  195. for _, v := range tagList {
  196. tagNameMap[v.TagId] = v.TagName
  197. }
  198. for _, v := range listTmp {
  199. var startTime, endTime string
  200. if v.StartTime != nil && !v.StartTime.IsZero() {
  201. startTime = v.StartTime.In(time.Local).Format(utils.FormatDateTime)
  202. if resourceType == knowledge.KnowledgeResourceTypeReport || resourceType == knowledge.KnowledgeResourceTypeKnow {
  203. startTime = v.StartTime.In(time.Local).Format(utils.FormatDate)
  204. }
  205. }
  206. if v.EndTime != nil && !v.EndTime.IsZero() {
  207. endTime = v.EndTime.In(time.Local).Format(utils.FormatDateTime)
  208. if resourceType == knowledge.KnowledgeResourceTypeReport || resourceType == knowledge.KnowledgeResourceTypeKnow {
  209. endTime = v.EndTime.In(time.Local).Format(utils.FormatDate)
  210. }
  211. }
  212. modifyTime := v.ModifyTime.In(time.Local).Format(utils.FormatDateTime)
  213. createTime := v.CreateTime.In(time.Local).Format(utils.FormatDateTime)
  214. tmp := &knowledge.KnowledgeResourceList{
  215. KnowledgeResourceId: v.KnowledgeResourceId,
  216. ResourceType: v.ResourceType,
  217. ClassifyId: v.ClassifyId,
  218. Title: v.Title,
  219. CreateTime: createTime,
  220. ModifyTime: modifyTime,
  221. State: v.State,
  222. ResourceCode: v.ResourceCode,
  223. AdminId: v.AdminId,
  224. AdminRealName: v.AdminRealName,
  225. SourceFrom: v.SourceFrom,
  226. TagId: v.TagId,
  227. StartTime: startTime,
  228. EndTime: endTime,
  229. }
  230. // todo 编辑状态
  231. /*markStatus, err := services.UpdateReportEditMark(item.Id, 0, this.SysUser.AdminId, 2, this.SysUser.RealName, this.Lang)
  232. if err != nil {
  233. br.Msg = "查询标记状态失败"
  234. br.ErrMsg = "查询标记状态失败,Err:" + err.Error()
  235. return
  236. }
  237. if markStatus.Status == 0 {
  238. item.CanEdit = true
  239. } else {
  240. item.Editor = markStatus.Editor
  241. }*/
  242. classifyName, ok := classifyFullNameMap[tmp.ClassifyId]
  243. if ok {
  244. tmp.ClassifyFullName = classifyName
  245. }
  246. tagName, ok := tagNameMap[v.TagId]
  247. if ok {
  248. tmp.TagName = tagName
  249. }
  250. list = append(list, tmp)
  251. }
  252. page := paging.GetPaging(currentIndex, pageSize, total)
  253. resp := new(knowledge.KnowledgeResourceListResp)
  254. resp.Paging = page
  255. resp.List = list
  256. br.Ret = 200
  257. br.Success = true
  258. br.Msg = "获取成功"
  259. br.Data = resp
  260. }
  261. // Search
  262. // @Title 获取事件列表接口
  263. // @Description 获取事件列表
  264. // @Param PageSize query int true "每页数据条数"
  265. // @Param CurrentIndex query int true "当前页页码,从1开始"
  266. // @Param TimeType query string true "筛选的时间类别:publish_time(发布时间),modify_time(更新时间);approve_time(审批时间)"
  267. // @Param StartDate query string true "开始时间"
  268. // @Param EndDate query string true "结束时间"
  269. // @Param Frequency query string true "频度"
  270. // @Param ClassifyIds query int true "分类id, 多选, 英文逗号分隔"
  271. // @Param SysUserIds query int true "用户id, 多选, 英文逗号分隔"
  272. // @Param State query int true "状态"
  273. // @Param KeyWord query string true "搜索关键词"
  274. // @Param PublishSort query string true "desc:降序,asc 升序(预留)"
  275. // @Param FilterReportType query string true "筛选事件类型,1:公共研报,2:共享研报,3:我的研报"
  276. // @Param IncludeFile query string true "是否包含文件, no:不包含"
  277. // @Param SourceFrom query string true "来源, 用英文逗号分隔"
  278. // @Param IsShowMe query string true "只看我, false:全部, true:只看我"
  279. // @Success 200 {object} knowledge.KnowledgeResourceListResp
  280. // @router /resource/search_by_es [get]
  281. func (this *ResourceController) Search() {
  282. br := new(models.BaseResponse).Init()
  283. defer func() {
  284. this.Data["json"] = br
  285. this.ServeJSON()
  286. }()
  287. pageSize, _ := this.GetInt("PageSize")
  288. currentIndex, _ := this.GetInt("CurrentIndex")
  289. classifyIds := this.GetString("ClassifyIds")
  290. // 添加人
  291. addUserIds := this.GetString("SysUserIds")
  292. // 标签
  293. tagIds := this.GetString("TagIds")
  294. keyWord := this.GetString("Keyword")
  295. resourceType, _ := this.GetInt("ResourceType")
  296. sourceFrom := this.GetString("SourceFrom")
  297. includeFile := this.GetString("IncludeFile")
  298. IsShowMe, _ := this.GetBool("IsShowMe")
  299. var startSize int
  300. if pageSize <= 0 {
  301. pageSize = utils.PageSize20
  302. }
  303. if currentIndex <= 0 {
  304. currentIndex = 1
  305. }
  306. startSize = utils.StartIndex(currentIndex, pageSize)
  307. isIncludeFile := true
  308. if includeFile == "no" {
  309. isIncludeFile = false
  310. }
  311. var searchUserIds []int
  312. if addUserIds != "" {
  313. // 转成数组,并把类型转成int
  314. addUserIdsArr := strings.Split(addUserIds, ",")
  315. for _, v := range addUserIdsArr {
  316. addUserId, _ := strconv.Atoi(v)
  317. searchUserIds = append(searchUserIds, addUserId)
  318. }
  319. }
  320. var myId int
  321. if IsShowMe {
  322. myId = this.SysUser.AdminId
  323. }
  324. var sourceFromArr []string
  325. if sourceFrom != "" {
  326. sourceFromArr = strings.Split(sourceFrom, ",")
  327. }
  328. var classifyIdsInt []int
  329. if classifyIds != "" {
  330. //转成数组,并把类型转成int
  331. classifyIdsArr := strings.Split(classifyIds, ",")
  332. for _, v := range classifyIdsArr {
  333. classifyId, _ := strconv.Atoi(v)
  334. classifyIdsInt = append(classifyIdsInt, classifyId)
  335. }
  336. }
  337. var tagIdsInt []int
  338. if tagIds != "" {
  339. //转成数组,并把类型转成int
  340. tagIdsArr := strings.Split(tagIds, ",")
  341. for _, v := range tagIdsArr {
  342. tagId, _ := strconv.Atoi(v)
  343. tagIdsInt = append(tagIdsInt, tagId)
  344. }
  345. }
  346. listTmp, total, err := knowledgeServ.SearchKnowledgeResourceByEs(
  347. resourceType,
  348. keyWord,
  349. searchUserIds,
  350. myId,
  351. classifyIdsInt,
  352. sourceFromArr,
  353. tagIdsInt,
  354. isIncludeFile,
  355. startSize,
  356. pageSize,
  357. )
  358. if err != nil {
  359. br.Msg = "获取失败"
  360. br.ErrMsg = "获取失败,Err:" + err.Error()
  361. return
  362. }
  363. var list []*knowledge.KnowledgeResourceList
  364. // 整理分类ID
  365. classifyFullNameMap := make(map[int]string)
  366. classifyIdsArr := make([]int, 0)
  367. for _, v := range listTmp {
  368. classifyIdsArr = append(classifyIdsArr, v.ClassifyId)
  369. }
  370. // 获取分类列表
  371. classifyList, err := knowledge.GetFullClassifyListByIdList(classifyIdsArr, resourceType)
  372. if err != nil {
  373. br.Msg = "获取失败"
  374. br.ErrMsg = "获取失败,Err:" + err.Error()
  375. return
  376. }
  377. for _, v := range classifyList {
  378. name := v.ClassifyName
  379. if v.ParentName != "" {
  380. name = v.ParentName + "/" + name
  381. }
  382. if v.RootName != "" {
  383. name = v.RootName + "/" + name
  384. }
  385. classifyFullNameMap[v.ClassifyId] = name
  386. }
  387. // 整理标签ID
  388. tagNameMap := make(map[int]string)
  389. tagIdsArr := make([]int, 0)
  390. for _, v := range listTmp {
  391. tagIdsArr = append(tagIdsArr, v.TagId)
  392. }
  393. // 获取标签列表
  394. tagObj := new(knowledge.KnowledgeTag)
  395. tagList, err := tagObj.GetListByIdList(tagIdsArr, resourceType)
  396. if err != nil {
  397. br.Msg = "获取失败"
  398. br.ErrMsg = "获取失败,Err:" + err.Error()
  399. }
  400. for _, v := range tagList {
  401. tagNameMap[v.TagId] = v.TagName
  402. }
  403. for _, v := range listTmp {
  404. var startTime, endTime string
  405. if v.StartTime != nil && !v.StartTime.IsZero() {
  406. startTime = v.StartTime.In(time.Local).Format(utils.FormatDateTime)
  407. if resourceType == knowledge.KnowledgeResourceTypeOpinion || resourceType == knowledge.KnowledgeResourceTypeKnow {
  408. startTime = v.StartTime.In(time.Local).Format(utils.FormatDate)
  409. }
  410. }
  411. if v.EndTime != nil && !v.EndTime.IsZero() {
  412. endTime = v.EndTime.In(time.Local).Format(utils.FormatDateTime)
  413. if resourceType == knowledge.KnowledgeResourceTypeOpinion || resourceType == knowledge.KnowledgeResourceTypeKnow {
  414. endTime = v.EndTime.In(time.Local).Format(utils.FormatDate)
  415. }
  416. }
  417. modifyTime := v.ModifyTime.In(time.Local).Format(utils.FormatDateTime)
  418. createTime := v.CreateTime.In(time.Local).Format(utils.FormatDateTime)
  419. tmp := &knowledge.KnowledgeResourceList{
  420. KnowledgeResourceId: v.KnowledgeResourceId,
  421. ResourceType: v.ResourceType,
  422. ClassifyId: v.ClassifyId,
  423. Title: v.Title,
  424. Content: v.Content,
  425. CreateTime: createTime,
  426. ModifyTime: modifyTime,
  427. State: v.State,
  428. ResourceCode: v.ResourceCode,
  429. AdminId: v.AdminId,
  430. AdminRealName: v.AdminRealName,
  431. SourceFrom: v.SourceFrom,
  432. TagId: v.TagId,
  433. StartTime: startTime,
  434. EndTime: endTime,
  435. }
  436. classifyName, ok := classifyFullNameMap[tmp.ClassifyId]
  437. if ok {
  438. tmp.ClassifyFullName = classifyName
  439. }
  440. tagName, ok := tagNameMap[v.TagId]
  441. if ok {
  442. tmp.TagName = tagName
  443. }
  444. list = append(list, tmp)
  445. }
  446. // list := make([]*knowledge.KnowledgeResourceList, 0, len(knowledges))
  447. // for _, v := range knowledges {
  448. // item := new(knowledge.KnowledgeResourceList)
  449. // item.KnowledgeResourceId = v.KnowledgeResourceId
  450. // item.ResourceType = v.ResourceType
  451. // item.ClassifyId = v.ClassifyId
  452. // item.Title = v.Title
  453. // item.CreateTime = v.CreateTime.Format(utils.FormatDateTime)
  454. // item.ModifyTime = v.ModifyTime.Format(utils.FormatDateTime)
  455. // item.State = v.State
  456. // item.Content = v.Content
  457. // item.ResourceCode = v.ResourceCode
  458. // item.AdminId = v.AdminId
  459. // item.AdminRealName = v.AdminRealName
  460. // item.SourceFrom = v.SourceFrom
  461. // item.TagId = v.TagId
  462. // if v.StartTime != nil {
  463. // item.StartTime = v.StartTime.Format(utils.FormatDateTime)
  464. // }
  465. // if v.EndTime != nil {
  466. // item.EndTime = v.EndTime.Format(utils.FormatDateTime)
  467. // }
  468. // list = append(list, item)
  469. // }
  470. page := paging.GetPaging(currentIndex, pageSize, int(total))
  471. resp := new(knowledge.KnowledgeResourceListResp)
  472. resp.Paging = page
  473. resp.List = list
  474. br.Ret = 200
  475. br.Success = true
  476. br.Msg = "获取成功"
  477. br.Data = resp
  478. }
  479. // Add
  480. // @Title 新增事件接口
  481. // @Description 新增事件(区分事件类型)
  482. // @Param request body knowledge.AddReq true "type json string"
  483. // @Success 200 {object} knowledge.AddResp
  484. // @router /resource/add [post]
  485. func (this *ResourceController) Add() {
  486. br := new(models.BaseResponse).Init()
  487. defer func() {
  488. this.Data["json"] = br
  489. this.ServeJSON()
  490. }()
  491. sysUser := this.SysUser
  492. if sysUser == nil {
  493. br.Msg = "请登录"
  494. br.ErrMsg = "请登录,SysUser Is Empty"
  495. br.Ret = 408
  496. return
  497. }
  498. var reqList []knowledge.AddReq
  499. err := json.Unmarshal(this.Ctx.Input.RequestBody, &reqList)
  500. if err != nil {
  501. br.Msg = "参数解析异常!"
  502. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  503. return
  504. }
  505. for _, req := range reqList {
  506. if req.Title == `` {
  507. br.Msg = "标题不能为空"
  508. br.ErrMsg = "标题不能为空"
  509. br.IsSendEmail = false
  510. return
  511. }
  512. // 判断标题长度,如果超过600个字符则报错
  513. //fmt.Println(len(req.Title))
  514. if len(req.Title) > 600 {
  515. br.Msg = "标题过长"
  516. return
  517. }
  518. if req.ClassifyId <= 0 {
  519. br.Msg = "分类必填"
  520. br.ErrMsg = "分类必填"
  521. br.IsSendEmail = false
  522. return
  523. }
  524. if req.ResourceType == knowledge.KnowledgeResourceTypeEvent || req.ResourceType == knowledge.KnowledgeResourceTypePolicy || req.ResourceType == knowledge.KnowledgeResourceTypeOpinion {
  525. if req.StartTime == "" {
  526. br.Msg = "开始时间必填"
  527. br.ErrMsg = "开始时间必填"
  528. return
  529. }
  530. }
  531. _, err, errMsg := knowledgeServ.AddResource(&req, sysUser)
  532. if err != nil {
  533. br.Msg = "保存失败"
  534. if errMsg != "" {
  535. br.Msg = errMsg
  536. }
  537. br.ErrMsg = "保存失败,Err:" + err.Error()
  538. return
  539. }
  540. }
  541. /*resp := new(knowledge.AddResp)
  542. resp.KnowledgeResourceId = item.KnowledgeResourceId
  543. resp.ResourceCode = item.ResourceCode*/
  544. br.Ret = 200
  545. br.Success = true
  546. br.Msg = "保存成功"
  547. //br.Data = resp
  548. }
  549. // Edit
  550. // @Title 编辑事件基础信息接口
  551. // @Description 编辑事件基础信息(不区分事件类型)
  552. // @Param request body knowledge.EditReq true "type json string"
  553. // @Success 200 {object} knowledge.EditResp
  554. // @router /resource/edit [post]
  555. func (this *ResourceController) Edit() {
  556. br := new(models.BaseResponse).Init()
  557. defer func() {
  558. this.Data["json"] = br
  559. this.ServeJSON()
  560. }()
  561. sysUser := this.SysUser
  562. if sysUser == nil {
  563. br.Msg = "请登录"
  564. br.ErrMsg = "请登录,SysUser Is Empty"
  565. br.Ret = 408
  566. return
  567. }
  568. var req knowledge.EditReq
  569. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  570. if err != nil {
  571. br.Msg = "参数解析异常!"
  572. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  573. return
  574. }
  575. if req.KnowledgeResourceId <= 0 {
  576. br.Msg = "请选择事件"
  577. br.ErrMsg = "请选择事件"
  578. return
  579. }
  580. if req.Title == `` {
  581. br.Msg = "标题不能为空"
  582. br.ErrMsg = "标题不能为空"
  583. br.IsSendEmail = false
  584. return
  585. }
  586. if len(req.Title) > 600 {
  587. br.Msg = "标题过长"
  588. return
  589. }
  590. if req.ClassifyId <= 0 {
  591. br.Msg = "分类必填"
  592. br.ErrMsg = "分类必填"
  593. br.IsSendEmail = false
  594. return
  595. }
  596. if req.ResourceType == knowledge.KnowledgeResourceTypeEvent || req.ResourceType == knowledge.KnowledgeResourceTypePolicy || req.ResourceType == knowledge.KnowledgeResourceTypeOpinion {
  597. if req.StartTime == "" {
  598. br.Msg = "开始时间必填"
  599. br.ErrMsg = "开始时间必填"
  600. return
  601. }
  602. }
  603. // todo 更新标记key
  604. /*markStatus, err := services.UpdateReportEditMark(int(req.KnowledgeResourceId), 0, sysUser.AdminId, 1, sysUser.RealName, this.Lang)
  605. if err != nil {
  606. br.Msg = err.Error()
  607. return
  608. }
  609. if markStatus.Status == 1 {
  610. br.Msg = markStatus.Msg
  611. //br.Ret = 202 //202 服务器已接受请求,但尚未处理。
  612. return
  613. }*/
  614. resourceObj := new(knowledge.KnowledgeResource)
  615. resourceInfo, e := resourceObj.GetById(int(req.KnowledgeResourceId))
  616. if e != nil {
  617. if utils.IsErrNoRow(e) {
  618. br.Msg = "事件已被删除, 请刷新页面"
  619. return
  620. }
  621. br.Msg = "操作失败"
  622. br.ErrMsg = "获取事件失败, Err: " + e.Error()
  623. return
  624. }
  625. if resourceInfo.ResourceType != req.ResourceType {
  626. br.Msg = "事件类型错误"
  627. return
  628. }
  629. // 编辑事件信息
  630. err, errMsg := knowledgeServ.EditResource(resourceInfo, req, sysUser)
  631. if err != nil {
  632. br.Msg = "保存失败"
  633. if errMsg != "" {
  634. br.Msg = errMsg
  635. }
  636. br.ErrMsg = "保存失败,Err:" + err.Error()
  637. return
  638. }
  639. resp := new(knowledge.EditResp)
  640. resp.KnowledgeResourceId = req.KnowledgeResourceId
  641. resp.ResourceCode = resourceInfo.ResourceCode
  642. br.Ret = 200
  643. br.Success = true
  644. br.Msg = "保存成功"
  645. br.Data = resp
  646. }
  647. // Detail
  648. // @Title 获取事件详情接口
  649. // @Description 获取事件详情
  650. // @Param request body knowledge.KnowledgeResourceDetailReq true "type json string"
  651. // @Success 200 {object} knowledge.KnowledgeResource
  652. // @router /resource/detail [get]
  653. func (this *ResourceController) Detail() {
  654. br := new(models.BaseResponse).Init()
  655. defer func() {
  656. this.Data["json"] = br
  657. this.ServeJSON()
  658. }()
  659. /*var req knowledge.KnowledgeResourceDetailReq
  660. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  661. if err != nil {
  662. br.Msg = "参数解析异常!"
  663. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  664. return
  665. }
  666. if req.KnowledgeResourceId <= 0 {
  667. br.Msg = "参数错误"
  668. return
  669. }*/
  670. knowledgeResourceId, err := this.GetInt("KnowledgeResourceId")
  671. if err != nil {
  672. br.Msg = "获取参数失败!"
  673. br.ErrMsg = "获取参数失败,Err:" + err.Error()
  674. return
  675. }
  676. if knowledgeResourceId <= 0 {
  677. br.Msg = "参数错误"
  678. return
  679. }
  680. resourceType, _ := this.GetInt("ResourceType")
  681. resourceObj := new(knowledge.KnowledgeResource)
  682. item, err := resourceObj.GetById(knowledgeResourceId)
  683. if err != nil {
  684. if utils.IsErrNoRow(err) {
  685. br.Msg = "事件已被删除"
  686. return
  687. }
  688. br.Msg = "获取失败"
  689. br.ErrMsg = "获取失败,Err:" + err.Error()
  690. return
  691. }
  692. if item.ResourceType != resourceType {
  693. br.Msg = "事件类型错误"
  694. return
  695. }
  696. item.Content = html.UnescapeString(item.Content)
  697. resp := &knowledge.KnowledgeResourceDetailView{
  698. KnowledgeResource: item,
  699. }
  700. br.Ret = 200
  701. br.Success = true
  702. br.Msg = "获取成功"
  703. br.Data = resp
  704. }
  705. // SaveReportContent
  706. // @Title 保存草稿
  707. // @Description 保存草稿
  708. // @Param request body knowledge.SaveReportContent true "type json string"
  709. // @Success 200 {object} knowledge.KnowledgeResourceAuthorResp
  710. // @router /resource/saveContent [post]
  711. func (this *ResourceController) SaveReportContent() {
  712. br := new(models.BaseResponse).Init()
  713. br.IsSendEmail = false
  714. defer func() {
  715. this.Data["json"] = br
  716. this.ServeJSON()
  717. }()
  718. sysUser := this.SysUser
  719. if sysUser == nil {
  720. br.Msg = "请登录"
  721. br.ErrMsg = "请登录,SysUser Is Empty"
  722. br.Ret = 408
  723. return
  724. }
  725. var req knowledge.SaveReportContent
  726. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  727. if err != nil {
  728. br.Msg = "参数解析异常!"
  729. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  730. return
  731. }
  732. knowledgeResourceId := req.KnowledgeResourceId
  733. //noChangeFlag := req.NoChange
  734. if knowledgeResourceId <= 0 {
  735. resp := new(knowledge.SaveReportContentResp)
  736. resp.KnowledgeResourceId = knowledgeResourceId
  737. br.Ret = 200
  738. br.Success = true
  739. br.Msg = "保存成功"
  740. br.Data = resp
  741. return
  742. }
  743. // 获取事件详情
  744. /*resourceInfo, _ := knowledge.GetReportByknowledgeResourceId(req.KnowledgeResourceId)
  745. if resourceInfo != nil && resourceInfo.State == 2 {
  746. br.Msg = "该事件已发布,不允许编辑"
  747. br.ErrMsg = "该事件已发布,不允许编辑"
  748. br.IsSendEmail = false
  749. return
  750. }
  751. // 标记更新中
  752. {
  753. markStatus, err := services.UpdateReportEditMark(req.KnowledgeResourceId, 0, sysUser.AdminId, 1, sysUser.RealName, this.Lang)
  754. if err != nil {
  755. br.Msg = err.Error()
  756. return
  757. }
  758. if markStatus.Status == 1 {
  759. br.Msg = markStatus.Msg
  760. return
  761. }
  762. }
  763. // 内容有过修改的话,那么逻辑处理
  764. if noChangeFlag != 1 {
  765. content := req.Content
  766. if content == "" {
  767. content = this.GetString("Content")
  768. }
  769. if content != "" {
  770. e := utils.ContentXssCheck(req.Content)
  771. if e != nil {
  772. br.Msg = "存在非法标签"
  773. br.ErrMsg = "存在非法标签, Err: " + e.Error()
  774. return
  775. }
  776. contentClean, e := services.FilterReportContentBr(req.Content)
  777. if e != nil {
  778. br.Msg = "内容去除前后空格失败"
  779. br.ErrMsg = "内容去除前后空格失败, Err: " + e.Error()
  780. return
  781. }
  782. content = contentClean
  783. contentSub, err := services.GetReportContentSub(content)
  784. if err != nil {
  785. go alarm_msg.SendAlarmMsg("解析 ContentSub 失败,Err:"+err.Error(), 3)
  786. //utils.SendEmail(utils.APPNAME+"失败提醒", "解析 ContentSub 失败,Err:"+err.Error(), utils.EmailSendToUsers)
  787. }
  788. resourceInfo.Content = html.EscapeString(content)
  789. resourceInfo.ContentSub = html.EscapeString(contentSub)
  790. resourceInfo.ContentStruct = html.EscapeString(req.ContentStruct)
  791. resourceInfo.HeadImg = req.HeadImg
  792. resourceInfo.EndImg = req.EndImg
  793. resourceInfo.CanvasColor = req.CanvasColor
  794. resourceInfo.HeadResourceId = req.HeadResourceId
  795. resourceInfo.EndResourceId = req.EndResourceId
  796. resourceInfo.ModifyTime = time.Now()
  797. resourceInfo.ContentModifyTime = time.Now()
  798. updateCols := []string{"Content", "ContentSub", "ContentStruct", "HeadImg", "EndImg", "CanvasColor", "HeadResourceId", "EndResourceId", "ModifyTime", "ContentModifyTime"}
  799. err = resourceInfo.UpdateReport(updateCols)
  800. if err != nil {
  801. br.Msg = "保存失败"
  802. br.ErrMsg = "保存失败,Err:" + err.Error()
  803. return
  804. }
  805. go knowledge.AddReportSaveLog(knowledgeResourceId, this.SysUser.AdminId, resourceInfo.Content, resourceInfo.ContentSub, resourceInfo.ContentStruct, resourceInfo.CanvasColor, this.SysUser.AdminName, resourceInfo.HeadResourceId, resourceInfo.EndResourceId)
  806. }
  807. }
  808. */
  809. resp := new(knowledge.SaveReportContentResp)
  810. resp.KnowledgeResourceId = knowledgeResourceId
  811. br.Ret = 200
  812. br.Success = true
  813. br.Msg = "保存成功"
  814. br.Data = resp
  815. }
  816. // BaseDetail
  817. // @Title 获取事件基础信息详情接口
  818. // @Description 获取事件基础信息详情接口
  819. // @Param request body knowledge.KnowledgeResourceDetailReq true "type json string"
  820. // @Success 200 {object} knowledge.KnowledgeResource
  821. // @router /resource/detail/base [get]
  822. func (this *ResourceController) BaseDetail() {
  823. br := new(models.BaseResponse).Init()
  824. defer func() {
  825. this.Data["json"] = br
  826. this.ServeJSON()
  827. }()
  828. /*var req knowledge.KnowledgeResourceDetailReq
  829. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  830. if err != nil {
  831. br.Msg = "参数解析异常!"
  832. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  833. return
  834. }
  835. if req.KnowledgeResourceId <= 0 {
  836. br.Msg = "参数错误"
  837. return
  838. }*/
  839. /*knowledgeResourceId, err := this.GetInt("KnowledgeResourceId")
  840. if err != nil {
  841. br.Msg = "获取参数失败!"
  842. br.ErrMsg = "获取参数失败,Err:" + err.Error()
  843. return
  844. }
  845. if knowledgeResourceId <= 0 {
  846. br.Msg = "参数错误"
  847. return
  848. }
  849. resourceInfo, err := knowledge.GetReportById(knowledgeResourceId)
  850. if err != nil {
  851. if utils.IsErrNoRow(err) {
  852. br.Msg = "事件已被删除"
  853. return
  854. }
  855. br.Msg = "获取失败"
  856. br.ErrMsg = "获取失败,Err:" + err.Error()
  857. return
  858. }
  859. // 基础信息就不获取章节信息了
  860. chapterList := make([]*knowledge.KnowledgeResourceChapter, 0)
  861. resourceInfo.Content = html.UnescapeString(resourceInfo.Content)
  862. resourceInfo.ContentSub = html.UnescapeString(resourceInfo.ContentSub)
  863. grandAdminList := make([]knowledge.KnowledgeResourceDetailViewAdmin, 0)
  864. permissionList := make([]knowledge.KnowledgeResourceDetailViewPermission, 0)
  865. // 处理事件授权用户列表
  866. {
  867. obj := report.ReportGrant{}
  868. grantList, tmpErr := obj.GetGrantListById(knowledgeResourceId)
  869. if tmpErr != nil {
  870. br.Msg = "获取章节id授权用户列表失败"
  871. br.ErrMsg = "获取章节id授权用户列表失败, Err: " + tmpErr.Error()
  872. return
  873. }
  874. if len(grantList) > 0 {
  875. grandAdminIdList := make([]int, 0)
  876. for _, v := range grantList {
  877. grandAdminIdList = append(grandAdminIdList, v.AdminId)
  878. }
  879. adminList, tmpErr := system.GetAdminListByIdList(grandAdminIdList)
  880. if tmpErr != nil {
  881. br.Msg = "获取章节id授权用户列表失败"
  882. br.ErrMsg = "获取章节id授权用户列表失败, Err: " + tmpErr.Error()
  883. return
  884. }
  885. for _, v := range adminList {
  886. grandAdminList = append(grandAdminList, knowledge.KnowledgeResourceDetailViewAdmin{
  887. AdminId: v.AdminId,
  888. AdminName: v.RealName,
  889. })
  890. }
  891. }
  892. }
  893. // 处理章节id关联品种id列表
  894. {
  895. minClassifyId := resourceInfo.ClassifyIdThird
  896. if minClassifyId <= 0 {
  897. minClassifyId = resourceInfo.ClassifyIdSecond
  898. }
  899. if minClassifyId <= 0 {
  900. minClassifyId = resourceInfo.ClassifyIdFirst
  901. }
  902. if minClassifyId <= 0 {
  903. br.Msg = "分类异常"
  904. br.ErrMsg = "分类异常"
  905. return
  906. }
  907. // 获取分类关联的品种id
  908. classifyPermissionList, tmpErr := knowledge.GetPermission(minClassifyId)
  909. if tmpErr != nil {
  910. br.Msg = "获取分类信息失败"
  911. br.ErrMsg = "获取失败,Err:" + tmpErr.Error()
  912. return
  913. }
  914. if len(classifyPermissionList) > 0 {
  915. permissionIdList := make([]int, 0)
  916. for _, v := range classifyPermissionList {
  917. permissionIdList = append(permissionIdList, v.ChartPermissionId)
  918. }
  919. adminList, tmpErr := knowledge.GetChartPermissionByIdList(permissionIdList)
  920. if tmpErr != nil {
  921. br.Msg = "获取章节id授权用户列表失败"
  922. br.ErrMsg = "获取章节id授权用户列表失败, Err: " + tmpErr.Error()
  923. return
  924. }
  925. for _, v := range adminList {
  926. permissionList = append(permissionList, knowledge.KnowledgeResourceDetailViewPermission{
  927. PermissionId: v.ChartPermissionId,
  928. PermissionName: v.PermissionName,
  929. })
  930. }
  931. }
  932. }
  933. resp := &knowledge.KnowledgeResourceDetailView{
  934. ReportDetail: resourceInfo,
  935. ChapterList: chapterList,
  936. GrandAdminList: grandAdminList,
  937. PermissionList: permissionList,
  938. }
  939. br.Ret = 200
  940. br.Success = true
  941. br.Msg = "获取成功"
  942. br.Data = resp*/
  943. }
  944. // Delete
  945. // @Title 删除事件接口
  946. // @Description 删除事件
  947. // @Param request body knowledge.DeleteReq true "type json string"
  948. // @Success 200 Ret=200 删除成功
  949. // @router /resource/delete [post]
  950. func (this *ResourceController) Delete() {
  951. br := new(models.BaseResponse).Init()
  952. defer func() {
  953. this.Data["json"] = br
  954. this.ServeJSON()
  955. }()
  956. var req knowledge.DeleteReq
  957. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  958. if err != nil {
  959. br.Msg = "参数解析异常!"
  960. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  961. return
  962. }
  963. if req.KnowledgeResourceId <= 0 {
  964. br.Msg = "参数错误"
  965. br.ErrMsg = "参数错误,事件id不可为空"
  966. return
  967. }
  968. resourceObj := new(knowledge.KnowledgeResource)
  969. item, err := resourceObj.GetById(req.KnowledgeResourceId)
  970. if err != nil {
  971. if utils.IsErrNoRow(err) {
  972. br.Msg = "事件不存在"
  973. br.ErrMsg = "事件不存在"
  974. return
  975. }
  976. }
  977. if item.ResourceType != req.ResourceType {
  978. br.Msg = "事件类型不匹配"
  979. return
  980. }
  981. if err = item.Delete(req.KnowledgeResourceId); err != nil {
  982. br.Msg = "删除失败"
  983. br.ErrMsg = "删除事件失败, Err: " + err.Error()
  984. return
  985. }
  986. go func() {
  987. item.IsDelete = 1
  988. er := knowledgeServ.EsAddOrEditKnowledgeResource(item)
  989. if er != nil {
  990. utils.FileLog.Info("知识资源id:" + strconv.Itoa(req.KnowledgeResourceId) + ",删除事件同步es失败, Err: " + er.Error())
  991. }
  992. }()
  993. br.Ret = 200
  994. br.Success = true
  995. br.Msg = "删除成功"
  996. }
  997. // ImportData
  998. // @Title Excel导入事件
  999. // @Description Excel导入事件
  1000. // @Param EntryFile query file true "文件"
  1001. // @Success 200 Ret=200 录入成功
  1002. // @router /resource/import_add [post]
  1003. func (c *ResourceController) ImportData() {
  1004. br := new(models.BaseResponse).Init()
  1005. defer func() {
  1006. c.Data["json"] = br
  1007. c.ServeJSON()
  1008. }()
  1009. sysUser := c.SysUser
  1010. if sysUser == nil {
  1011. br.Msg = "请重新登录"
  1012. return
  1013. }
  1014. resourceType, _ := c.GetInt("ResourceType")
  1015. file, _, err := c.GetFile("EntryFile")
  1016. if err != nil {
  1017. br.Msg = "获取文件失败"
  1018. br.ErrMsg = "获取文件失败,Err:" + err.Error()
  1019. return
  1020. }
  1021. path := "./static/knowledge_import_" + strconv.Itoa(resourceType) + "_" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  1022. defer file.Close()
  1023. err = c.SaveToFile("EntryFile", path)
  1024. if err != nil {
  1025. br.Msg = "文件保存失败"
  1026. br.ErrMsg = "文件保存失败,Err:" + err.Error()
  1027. return
  1028. }
  1029. //if utils.RunMode == "debug" {
  1030. defer os.Remove(path)
  1031. //}
  1032. successCount, failCount, err, errMsg := knowledgeServ.ImportResourceData(path, resourceType, sysUser)
  1033. if err != nil {
  1034. br.Msg = errMsg
  1035. br.ErrMsg = err.Error()
  1036. return
  1037. }
  1038. resp := models.EdbdataImportResp{
  1039. SuccessCount: successCount,
  1040. FailCount: failCount,
  1041. }
  1042. if failCount > 0 {
  1043. if successCount == 0 {
  1044. resp.Status = -1
  1045. resp.Msg = "导入失败"
  1046. } else {
  1047. resp.Status = 1
  1048. resp.Msg = "存在部分导入失败"
  1049. }
  1050. } else {
  1051. resp.Status = 0
  1052. }
  1053. br.Msg = "导入成功"
  1054. br.Ret = 200
  1055. br.Success = true
  1056. br.Data = resp
  1057. }
  1058. // ImportFailListDownload
  1059. // @Title 下载失败列表
  1060. // @Description 下载失败列表
  1061. // @Success 200 {object} models.EdbdataClassifyResp
  1062. // @router /resource/download_fail [get]
  1063. func (this *ResourceController) ImportFailListDownload() {
  1064. br := new(models.BaseResponse).Init()
  1065. defer func() {
  1066. this.Data["json"] = br
  1067. this.ServeJSON()
  1068. }()
  1069. sysUser := this.SysUser
  1070. if sysUser == nil {
  1071. br.Msg = "请重新登录"
  1072. return
  1073. }
  1074. resourceType, _ := this.GetInt("ResourceType")
  1075. failObj := new(knowledge.KnowledgeImportFail)
  1076. item, err := failObj.GetListBySysUserId(sysUser.AdminId, resourceType)
  1077. if err != nil {
  1078. br.Msg = "获取数据失败"
  1079. br.Msg = "获取数据失败,Err:" + err.Error()
  1080. return
  1081. }
  1082. xlsxFile := xlsx.NewFile()
  1083. SheetName := "导入失败数据"
  1084. if this.Lang == utils.EnLangVersion {
  1085. SheetName = "Import Fail List"
  1086. }
  1087. sheet, err := xlsxFile.AddSheet(SheetName)
  1088. if err != nil {
  1089. br.Msg = "获取sheet失败"
  1090. br.Msg = "获取sheet失败,Err:" + err.Error()
  1091. return
  1092. }
  1093. if resourceType == knowledge.KnowledgeResourceTypeEvent || resourceType == knowledge.KnowledgeResourceTypePolicy {
  1094. headRow := sheet.AddRow()
  1095. headRow.AddCell().SetValue("开始日期")
  1096. headRow.AddCell().SetValue("开始时间")
  1097. headRow.AddCell().SetValue("结束日期")
  1098. headRow.AddCell().SetValue("结束时间")
  1099. headRow.AddCell().SetValue("标题")
  1100. headRow.AddCell().SetValue("正文")
  1101. headRow.AddCell().SetValue("来源")
  1102. headRow.AddCell().SetValue("一级分类")
  1103. headRow.AddCell().SetValue("二级分类")
  1104. headRow.AddCell().SetValue("三级分类")
  1105. headRow.AddCell().SetValue("标签")
  1106. headRow.AddCell().SetValue("备注")
  1107. for _, v := range item {
  1108. row := sheet.AddRow()
  1109. row.AddCell().SetValue(v.StartDate)
  1110. row.AddCell().SetValue(v.StartTime)
  1111. row.AddCell().SetValue(v.EndDate)
  1112. row.AddCell().SetValue(v.EndTime)
  1113. row.AddCell().SetValue(v.Title)
  1114. row.AddCell().SetValue(v.Content)
  1115. row.AddCell().SetValue(v.SourceFrom)
  1116. row.AddCell().SetValue(v.ClassifyFirst)
  1117. row.AddCell().SetValue(v.ClassifySecond)
  1118. row.AddCell().SetValue(v.ClassifyThird)
  1119. row.AddCell().SetValue(v.Tag)
  1120. row.AddCell().SetValue(v.Remark)
  1121. }
  1122. } else {
  1123. headRow := sheet.AddRow()
  1124. headRow.AddCell().SetValue("时间")
  1125. headRow.AddCell().SetValue("标题")
  1126. headRow.AddCell().SetValue("正文")
  1127. headRow.AddCell().SetValue("来源")
  1128. headRow.AddCell().SetValue("一级分类")
  1129. headRow.AddCell().SetValue("二级分类")
  1130. headRow.AddCell().SetValue("三级分类")
  1131. headRow.AddCell().SetValue("标签")
  1132. headRow.AddCell().SetValue("备注")
  1133. for _, v := range item {
  1134. row := sheet.AddRow()
  1135. row.AddCell().SetValue(v.StartDate)
  1136. row.AddCell().SetValue(v.Title)
  1137. row.AddCell().SetValue(v.Content)
  1138. row.AddCell().SetValue(v.SourceFrom)
  1139. row.AddCell().SetValue(v.ClassifyFirst)
  1140. row.AddCell().SetValue(v.ClassifySecond)
  1141. row.AddCell().SetValue(v.ClassifyThird)
  1142. row.AddCell().SetValue(v.Tag)
  1143. row.AddCell().SetValue(v.Remark)
  1144. }
  1145. }
  1146. fileName := time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  1147. savePath := "./static/" + fileName
  1148. err = xlsxFile.Save(savePath)
  1149. if err != nil {
  1150. br.Msg = "文件保存失败"
  1151. br.ErrMsg = "文件保存失败,Err:" + err.Error()
  1152. return
  1153. }
  1154. defer func() {
  1155. os.Remove(savePath)
  1156. }()
  1157. finalFileName := "失败列表.xlsx"
  1158. if this.Lang == utils.EnLangVersion {
  1159. finalFileName = "Failure List.xlsx"
  1160. }
  1161. this.Ctx.Output.Download(savePath, finalFileName)
  1162. }
  1163. // ResourceUpload 上传文件
  1164. // @Title 上传文件
  1165. // @Description 上传文件
  1166. // @Param MenuId query int true "目录ID"
  1167. // @Param File query file true "文件"
  1168. // @Success 200 Ret=200 操作成功
  1169. // @router /resource/upload [post]
  1170. func (this *ResourceController) ResourceUpload() {
  1171. br := new(models.BaseResponse).Init()
  1172. defer func() {
  1173. if br.ErrMsg == "" {
  1174. br.IsSendEmail = false
  1175. }
  1176. this.Data["json"] = br
  1177. this.ServeJSON()
  1178. }()
  1179. sysUser := this.SysUser
  1180. if sysUser == nil {
  1181. br.Msg = "请登录"
  1182. br.ErrMsg = "请登录,SysUser Is Empty"
  1183. br.Ret = 408
  1184. return
  1185. }
  1186. f, h, e := this.GetFile("File")
  1187. if e != nil {
  1188. br.Msg = "获取资源信息失败"
  1189. br.ErrMsg = "获取资源信息失败, Err:" + e.Error()
  1190. return
  1191. }
  1192. defer func() {
  1193. _ = f.Close()
  1194. }()
  1195. // 不依赖于文件扩展名检查文件格式
  1196. fileData, e := ioutil.ReadAll(f)
  1197. if e != nil {
  1198. br.Msg = "上传失败"
  1199. br.ErrMsg = "读取文件失败, Err: " + e.Error()
  1200. return
  1201. }
  1202. ext := path.Ext(h.Filename)
  1203. //fmt.Println("ext", ext)
  1204. kind, _ := filetype.Match(fileData)
  1205. //fmt.Println("kind.Extension", kind.Extension)
  1206. if ext != ".pdf" && ext != ".ppt" && ext != ".pptx" && ext != ".docx" && ext != ".doc" && kind.Extension != "pdf" && kind.Extension != "ppt" && kind.Extension != "pptx" && kind.Extension != "docx" && kind.Extension != "doc" {
  1207. br.Msg = "文件格式有误"
  1208. return
  1209. }
  1210. uploadDir := utils.STATIC_DIR + "hongze/" + time.Now().Format("20060102")
  1211. if e = os.MkdirAll(uploadDir, utils.DIR_MOD); e != nil {
  1212. br.Msg = "存储目录创建失败"
  1213. br.ErrMsg = "存储目录创建失败, Err:" + e.Error()
  1214. return
  1215. }
  1216. ossFileName := utils.GetRandStringNoSpecialChar(28) + ext
  1217. filePath := uploadDir + "/" + ossFileName
  1218. if e = this.SaveToFile("File", filePath); e != nil {
  1219. br.Msg = "文件保存失败"
  1220. br.ErrMsg = "文件保存失败, Err:" + e.Error()
  1221. return
  1222. }
  1223. defer func() {
  1224. _ = os.Remove(filePath)
  1225. }()
  1226. // 上传到阿里云
  1227. ossDir := utils.RESOURCE_DIR + "knowledge_resource/"
  1228. resourceUrl := ``
  1229. savePath := ossDir + time.Now().Format("200601/20060102/") + ossFileName
  1230. ossClient := services.NewOssClient()
  1231. if ossClient == nil {
  1232. br.Msg = "上传失败"
  1233. br.ErrMsg = "初始化OSS服务失败"
  1234. return
  1235. }
  1236. resourceUrl, e = ossClient.UploadFile(ossFileName, filePath, savePath)
  1237. if e != nil {
  1238. br.Msg = "文件上传失败"
  1239. br.ErrMsg = "文件上传失败,Err:" + e.Error()
  1240. return
  1241. }
  1242. resourceItem := &knowledge.UpdateResourceResp{
  1243. FileUrl: resourceUrl,
  1244. }
  1245. br.Msg = "上传成功"
  1246. br.Ret = 200
  1247. br.Success = true
  1248. br.Data = resourceItem
  1249. }
  1250. // GetSourceFrom
  1251. // @Title 获取所有来源
  1252. // @Description 获取所有来源
  1253. // @Success 200 {object} models.EdbdataClassifyResp
  1254. // @router /resource/source_from [get]
  1255. func (this *ResourceController) GetSourceFrom() {
  1256. br := new(models.BaseResponse).Init()
  1257. defer func() {
  1258. this.Data["json"] = br
  1259. this.ServeJSON()
  1260. }()
  1261. sysUser := this.SysUser
  1262. if sysUser == nil {
  1263. br.Msg = "请重新登录"
  1264. return
  1265. }
  1266. resourceType, _ := this.GetInt("ResourceType")
  1267. keyword := this.GetString("Keyword")
  1268. resourceObj := new(knowledge.KnowledgeResource)
  1269. list, err := resourceObj.GetSourceFrom(keyword, resourceType)
  1270. if err != nil {
  1271. br.Msg = "获取数据失败"
  1272. br.Msg = "获取数据失败,Err:" + err.Error()
  1273. return
  1274. }
  1275. resp := new(knowledge.GetSourceFromResp)
  1276. resp.List = list
  1277. br.Ret = 200
  1278. br.Success = true
  1279. br.Msg = "获取成功"
  1280. br.Data = resp
  1281. }
  1282. // GetAdminList
  1283. // @Title 获取所有创建人
  1284. // @Description 获取所有创建人
  1285. // @Success 200 {object} models.EdbdataClassifyResp
  1286. // @router /resource/admins [get]
  1287. func (this *ResourceController) GetAdminList() {
  1288. br := new(models.BaseResponse).Init()
  1289. defer func() {
  1290. this.Data["json"] = br
  1291. this.ServeJSON()
  1292. }()
  1293. sysUser := this.SysUser
  1294. if sysUser == nil {
  1295. br.Msg = "请重新登录"
  1296. return
  1297. }
  1298. resourceType, _ := this.GetInt("ResourceType")
  1299. keyword := this.GetString("Keyword")
  1300. resourceObj := new(knowledge.KnowledgeResource)
  1301. list, err := resourceObj.GetAdmins(keyword, resourceType)
  1302. if err != nil {
  1303. br.Msg = "获取数据失败"
  1304. br.Msg = "获取数据失败,Err:" + err.Error()
  1305. return
  1306. }
  1307. resp := new(knowledge.GetAdminsResp)
  1308. resp.List = list
  1309. br.Ret = 200
  1310. br.Success = true
  1311. br.Msg = "获取成功"
  1312. br.Data = resp
  1313. }
  1314. // TemplateDownload
  1315. // @Title 下载模板
  1316. // @Description 下载模板
  1317. // @Success 200 {object} models.EdbdataClassifyResp
  1318. // @Param ResourceType query int false "来源:1:模板1;2:模板2"
  1319. // @router /resource/template [get]
  1320. func (this *ResourceController) TemplateDownload() {
  1321. br := new(models.BaseResponse).Init()
  1322. defer func() {
  1323. this.Data["json"] = br
  1324. this.ServeJSON()
  1325. }()
  1326. resourceType, _ := this.GetInt("ResourceType")
  1327. downUrl := ""
  1328. fileName := ""
  1329. switch resourceType {
  1330. case 1:
  1331. downUrl = "./static/template/政策库上传模板.xlsx"
  1332. fileName = "政策库上传模板.xlsx"
  1333. case 2:
  1334. downUrl = "./static/template/观点库上传模板.xlsx"
  1335. fileName = "观点库上传模板.xlsx"
  1336. case 3:
  1337. downUrl = "./static/template/知识库上传模板.xlsx"
  1338. fileName = "知识库上传模板.xlsx"
  1339. default:
  1340. downUrl = "./static/template/事件库上传模板.xlsx"
  1341. fileName = "事件库上传模板.xlsx"
  1342. }
  1343. // 判断文件是否存在
  1344. if _, err := os.Stat(downUrl); os.IsNotExist(err) {
  1345. br.Msg = "文件不存在"
  1346. return
  1347. }
  1348. fmt.Println(downUrl, fileName)
  1349. this.Ctx.Output.Download(downUrl, fileName)
  1350. //this.Ctx.Output.Download("./static/数据导入模板.xlsx", "数据导入模板.xlsx")
  1351. br.Ret = 200
  1352. br.Success = true
  1353. br.Msg = "下载成功"
  1354. }
  1355. // Public
  1356. // @Title 设置知识库事件公开接口
  1357. // @Description 设置知识库事件公开接口
  1358. // @Param request body knowledge.AddReq true "type json string"
  1359. // @Success 200 {object} knowledge.AddResp
  1360. // @router /resource/public [post]
  1361. func (this *ResourceController) Public() {
  1362. br := new(models.BaseResponse).Init()
  1363. defer func() {
  1364. this.Data["json"] = br
  1365. this.ServeJSON()
  1366. }()
  1367. sysUser := this.SysUser
  1368. if sysUser == nil {
  1369. br.Msg = "请重新登录"
  1370. return
  1371. }
  1372. var req knowledge.PublicReq
  1373. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  1374. br.Msg = "参数解析失败"
  1375. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  1376. return
  1377. }
  1378. msg, err := knowledgeServ.SetKnowledgeResourcePublic(req.KnowledgeResourceId)
  1379. if err != nil {
  1380. if msg == "" {
  1381. msg = "设置失败"
  1382. }
  1383. br.Msg = msg
  1384. br.ErrMsg = "设置失败,Err:" + err.Error()
  1385. return
  1386. }
  1387. br.Msg = "设置成功"
  1388. br.Ret = 200
  1389. br.Success = true
  1390. }
  1391. // BiDashboardTmpResourceList
  1392. // @Title 获取bi看报中的知识资源列表
  1393. // @Description 获取bi看报中的知识资源列表
  1394. // @Success 200 {object} models.EdbdataClassifyResp
  1395. // @Param biDashboardDetailId query int true "看板详情ID"
  1396. // @router /resource/bi_dashboard/list [get]
  1397. func (this *ResourceController) BiDashboardCacheResourceList() {
  1398. br := new(models.BaseResponse).Init()
  1399. defer func() {
  1400. this.Data["json"] = br
  1401. this.ServeJSON()
  1402. }()
  1403. biDashboardDetailId, _ := this.GetInt("BiDashboardDetailId")
  1404. resp, msg, err := knowledgeServ.GetKnowledgeResourceTmpList(biDashboardDetailId, this.SysUser.AdminId)
  1405. if err != nil {
  1406. if msg == "" {
  1407. msg = "获取数据失败"
  1408. }
  1409. br.Msg = msg
  1410. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1411. return
  1412. }
  1413. br.Ret = 200
  1414. br.Success = true
  1415. br.Msg = "获取成功"
  1416. br.Data = resp
  1417. }
  1418. // BiDashboardResourceSave
  1419. // @Title 将知识资源库添加到bi看板
  1420. // @Description 将知识资源库添加到bi看板
  1421. // @Param request body knowledge.DeleteReq true "type json string"
  1422. // @Success 200 {object} models.EdbdataClassifyResp
  1423. // @router /resource/bi_dashboard/save [post]
  1424. func (this *ResourceController) BiDashboardResourceSave() {
  1425. br := new(models.BaseResponse).Init()
  1426. defer func() {
  1427. this.Data["json"] = br
  1428. this.ServeJSON()
  1429. }()
  1430. var req knowledge.BiDashboardResourceAddReq
  1431. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  1432. br.Msg = "参数解析失败"
  1433. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  1434. return
  1435. }
  1436. if len(req.KnowledgeResourceList) == 0 {
  1437. // 则删除缓存中的数据
  1438. err := utils.Rc.Delete(utils.CACHE_KEY_BI_DASHBOARD_PREPARE_RESOURCE + strconv.Itoa(this.SysUser.AdminId))
  1439. if err != nil {
  1440. br.Msg = "添加失败"
  1441. br.ErrMsg = "删除缓存失败,Err:" + err.Error()
  1442. return
  1443. }
  1444. br.Msg = "添加成功"
  1445. br.Ret = 200
  1446. br.Success = true
  1447. return
  1448. }
  1449. cacheData, err := json.Marshal(req)
  1450. if err != nil {
  1451. br.Msg = "添加失败"
  1452. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1453. return
  1454. }
  1455. err = utils.Rc.Put(utils.CACHE_KEY_BI_DASHBOARD_PREPARE_RESOURCE+strconv.Itoa(this.SysUser.AdminId), cacheData, time.Hour*24)
  1456. if err != nil {
  1457. br.Msg = "添加失败"
  1458. br.ErrMsg = "添加失败,Err:" + err.Error()
  1459. return
  1460. }
  1461. br.Msg = "添加成功"
  1462. br.Ret = 200
  1463. br.Success = true
  1464. }