resource.go 46 KB

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