resource.go 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685
  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. knowledgeSrv "eta_gn/eta_api/services/knowledge"
  9. "eta_gn/eta_api/services/knowledge_approve"
  10. "eta_gn/eta_api/utils"
  11. "fmt"
  12. "html"
  13. "io/ioutil"
  14. "os"
  15. "path"
  16. "strconv"
  17. "strings"
  18. "time"
  19. "github.com/h2non/filetype"
  20. "github.com/rdlucklib/rdluck_tools/paging"
  21. "github.com/tealeg/xlsx"
  22. )
  23. // 分类
  24. type ResourceController struct {
  25. controllers.BaseAuthController
  26. }
  27. // ListReport
  28. // @Title 获取事件列表接口
  29. // @Description 获取事件列表
  30. // @Param PageSize query int true "每页数据条数"
  31. // @Param CurrentIndex query int true "当前页页码,从1开始"
  32. // @Param TimeType query string true "筛选的时间类别:publish_time(发布时间),modify_time(更新时间);approve_time(审批时间)"
  33. // @Param StartDate query string true "开始时间"
  34. // @Param EndDate query string true "结束时间"
  35. // @Param Frequency query string true "频度"
  36. // @Param ClassifyIds query int true "分类id, 多选, 英文逗号分隔"
  37. // @Param State query string true "状态, 多选, 英文逗号分隔"
  38. // @Param KeyWord query string true "搜索关键词"
  39. // @Param PublishSort query string true "desc:降序,asc 升序(预留)"
  40. // @Param FilterReportType query string true "筛选事件类型,1:公共研报,2:共享研报,3:我的研报"
  41. // @Param IncludeFile query string true "是否包含文件, no:不包含"
  42. // @Param SourceFrom query string true "来源, 用英文逗号分隔"
  43. // @Param IsShowMe query string true "只看我, false:全部, true:只看我"
  44. // @Param IsShowPublic query string true "只看我, false:全部, true:只看发布"
  45. // @Success 200 {object} knowledge.KnowledgeResourceListResp
  46. // @router /resource/list [get]
  47. func (this *ResourceController) List() {
  48. br := new(models.BaseResponse).Init()
  49. defer func() {
  50. this.Data["json"] = br
  51. this.ServeJSON()
  52. }()
  53. pageSize, _ := this.GetInt("PageSize")
  54. currentIndex, _ := this.GetInt("CurrentIndex")
  55. classifyIds := this.GetString("ClassifyIds")
  56. state := this.GetString("State")
  57. // 添加人
  58. addUserIds, _ := this.GetInt("SysUserIds")
  59. // 标签
  60. tagIds := this.GetString("TagIds")
  61. keyWord := this.GetString("Keyword")
  62. resourceType, _ := this.GetInt("ResourceType")
  63. sourceFrom := this.GetString("SourceFrom")
  64. includeFile := this.GetString("IncludeFile")
  65. IsShowMe, _ := this.GetBool("IsShowMe")
  66. isShowPublic, _ := this.GetBool("IsShowPublic")
  67. var startSize int
  68. if pageSize <= 0 {
  69. pageSize = utils.PageSize20
  70. }
  71. if currentIndex <= 0 {
  72. currentIndex = 1
  73. }
  74. startSize = utils.StartIndex(currentIndex, pageSize)
  75. var condition string
  76. var pars []interface{}
  77. condition += ` AND resource_type = ? `
  78. pars = append(pars, resourceType)
  79. if includeFile == "no" {
  80. condition += ` AND is_file = ? `
  81. pars = append(pars, 0)
  82. }
  83. if IsShowMe {
  84. condition += ` AND admin_id = ? `
  85. pars = append(pars, this.SysUser.AdminId)
  86. }
  87. if state != "" {
  88. stateArr := strings.Split(state, ",")
  89. condition += ` AND state in (?) `
  90. pars = append(pars, stateArr)
  91. }
  92. if isShowPublic {
  93. condition += ` AND state = ? `
  94. pars = append(pars, knowledge.KnowledgeResourceStatePassed)
  95. }
  96. if keyWord != "" {
  97. //按照空格划分为关键词数组
  98. keyWordArr := strings.Split(keyWord, " ")
  99. lengthKey := len(keyWordArr)
  100. if lengthKey == 1 {
  101. condition += ` AND (title LIKE ?) `
  102. pars = utils.GetLikeKeywordPars(pars, keyWord, 1)
  103. } else if lengthKey > 1 {
  104. for k, v := range keyWordArr {
  105. if k == 0 {
  106. condition += ` AND (title LIKE ? `
  107. pars = utils.GetLikeKeywordPars(pars, v, 1)
  108. } else if k == lengthKey-1 {
  109. condition += ` OR title LIKE ? ) `
  110. pars = utils.GetLikeKeywordPars(pars, v, 1)
  111. } else {
  112. condition += ` OR title LIKE ? `
  113. pars = utils.GetLikeKeywordPars(pars, v, 1)
  114. }
  115. }
  116. }
  117. }
  118. if sourceFrom != "" {
  119. sourceArr := strings.Split(sourceFrom, ",")
  120. condition += ` AND source_from in (?) `
  121. pars = append(pars, sourceArr)
  122. }
  123. if classifyIds != "" {
  124. //转成数组,并把类型转成int
  125. classifyIdsArr := strings.Split(classifyIds, ",")
  126. var classifyIdsInt []int
  127. for _, v := range classifyIdsArr {
  128. classifyId, _ := strconv.Atoi(v)
  129. classifyIdsInt = append(classifyIdsInt, classifyId)
  130. }
  131. condition += ` AND classify_id in (?) `
  132. pars = append(pars, classifyIdsInt)
  133. }
  134. if addUserIds > 0 {
  135. // 转成数组,并把类型转成int
  136. addUserIdsArr := strings.Split(strconv.Itoa(addUserIds), ",")
  137. var addUserIdsInt []int
  138. for _, v := range addUserIdsArr {
  139. addUserId, _ := strconv.Atoi(v)
  140. addUserIdsInt = append(addUserIdsInt, addUserId)
  141. }
  142. condition += ` AND admin_id in (?) `
  143. pars = append(pars, addUserIdsInt)
  144. }
  145. if tagIds != "" {
  146. //转成数组,并把类型转成int
  147. tagIdsArr := strings.Split(tagIds, ",")
  148. var tagIdsInt []int
  149. for _, v := range tagIdsArr {
  150. tagId, _ := strconv.Atoi(v)
  151. tagIdsInt = append(tagIdsInt, tagId)
  152. }
  153. condition += ` AND tag_id in (?) `
  154. pars = append(pars, tagIdsInt)
  155. }
  156. var err error
  157. var total int
  158. var list []*knowledge.KnowledgeResourceList
  159. // 共享事件需要连表查询,所以需要单独写
  160. total, err = knowledge.GetKnowledgeResourceListCount(condition, pars)
  161. if err != nil {
  162. br.Msg = "获取失败"
  163. br.ErrMsg = "获取失败,Err:" + err.Error()
  164. return
  165. }
  166. listTmp, err := knowledge.GetKnowledgeResourcePageList(condition, pars, startSize, pageSize)
  167. if err != nil {
  168. br.Msg = "获取失败"
  169. br.ErrMsg = "获取失败,Err:" + err.Error()
  170. return
  171. }
  172. // 整理分类ID
  173. classifyFullNameMap := make(map[int]string)
  174. classifyIdsArr := make([]int, 0)
  175. for _, v := range listTmp {
  176. classifyIdsArr = append(classifyIdsArr, v.ClassifyId)
  177. }
  178. // 获取分类列表
  179. classifyList, err := knowledge.GetFullClassifyListByIdList(classifyIdsArr, resourceType)
  180. if err != nil {
  181. br.Msg = "获取失败"
  182. br.ErrMsg = "获取失败,Err:" + err.Error()
  183. return
  184. }
  185. for _, v := range classifyList {
  186. name := v.ClassifyName
  187. if v.ParentName != "" {
  188. name = v.ParentName + "/" + name
  189. }
  190. if v.RootName != "" {
  191. name = v.RootName + "/" + name
  192. }
  193. classifyFullNameMap[v.ClassifyId] = name
  194. }
  195. // 整理标签ID
  196. tagNameMap := make(map[int]string)
  197. tagIdsArr := make([]int, 0)
  198. for _, v := range listTmp {
  199. tagIdsArr = append(tagIdsArr, v.TagId)
  200. }
  201. // 获取标签列表
  202. tagObj := new(knowledge.KnowledgeTag)
  203. tagList, err := tagObj.GetListByIdList(tagIdsArr, resourceType)
  204. if err != nil {
  205. br.Msg = "获取失败"
  206. br.ErrMsg = "获取失败,Err:" + err.Error()
  207. }
  208. for _, v := range tagList {
  209. tagNameMap[v.TagId] = v.TagName
  210. }
  211. for _, v := range listTmp {
  212. var startTime, endTime string
  213. if v.StartTime != nil && !v.StartTime.IsZero() {
  214. startTime = v.StartTime.In(time.Local).Format(utils.FormatDateTime)
  215. if resourceType == knowledge.KnowledgeResourceTypeReport || resourceType == knowledge.KnowledgeResourceTypeKnow {
  216. startTime = v.StartTime.In(time.Local).Format(utils.FormatDate)
  217. }
  218. }
  219. if v.EndTime != nil && !v.EndTime.IsZero() {
  220. endTime = v.EndTime.In(time.Local).Format(utils.FormatDateTime)
  221. if resourceType == knowledge.KnowledgeResourceTypeReport || resourceType == knowledge.KnowledgeResourceTypeKnow {
  222. endTime = v.EndTime.In(time.Local).Format(utils.FormatDate)
  223. }
  224. }
  225. modifyTime := v.ModifyTime.In(time.Local).Format(utils.FormatDateTime)
  226. createTime := v.CreateTime.In(time.Local).Format(utils.FormatDateTime)
  227. tmp := &knowledge.KnowledgeResourceList{
  228. KnowledgeResourceId: v.KnowledgeResourceId,
  229. ResourceType: v.ResourceType,
  230. ClassifyId: v.ClassifyId,
  231. Title: v.Title,
  232. CreateTime: createTime,
  233. ModifyTime: modifyTime,
  234. State: v.State,
  235. ResourceCode: v.ResourceCode,
  236. AdminId: v.AdminId,
  237. AdminRealName: v.AdminRealName,
  238. SourceFrom: v.SourceFrom,
  239. TagId: v.TagId,
  240. StartTime: startTime,
  241. EndTime: endTime,
  242. OutSource: v.OutSource,
  243. }
  244. // todo 编辑状态
  245. /*markStatus, err := services.UpdateReportEditMark(item.Id, 0, this.SysUser.AdminId, 2, this.SysUser.RealName, this.Lang)
  246. if err != nil {
  247. br.Msg = "查询标记状态失败"
  248. br.ErrMsg = "查询标记状态失败,Err:" + err.Error()
  249. return
  250. }
  251. if markStatus.Status == 0 {
  252. item.CanEdit = true
  253. } else {
  254. item.Editor = markStatus.Editor
  255. }*/
  256. classifyName, ok := classifyFullNameMap[tmp.ClassifyId]
  257. if ok {
  258. tmp.ClassifyFullName = classifyName
  259. }
  260. tagName, ok := tagNameMap[v.TagId]
  261. if ok {
  262. tmp.TagName = tagName
  263. }
  264. list = append(list, tmp)
  265. }
  266. page := paging.GetPaging(currentIndex, pageSize, total)
  267. resp := new(knowledge.KnowledgeResourceListResp)
  268. resp.Paging = page
  269. resp.List = list
  270. br.Ret = 200
  271. br.Success = true
  272. br.Msg = "获取成功"
  273. br.Data = resp
  274. }
  275. // Search
  276. // @Title 获取事件列表接口
  277. // @Description 获取事件列表
  278. // @Param PageSize query int true "每页数据条数"
  279. // @Param CurrentIndex query int true "当前页页码,从1开始"
  280. // @Param TimeType query string true "筛选的时间类别:publish_time(发布时间),modify_time(更新时间);approve_time(审批时间)"
  281. // @Param StartDate query string true "开始时间"
  282. // @Param EndDate query string true "结束时间"
  283. // @Param Frequency query string true "频度"
  284. // @Param ClassifyIds query int true "分类id, 多选, 英文逗号分隔"
  285. // @Param SysUserIds query int true "用户id, 多选, 英文逗号分隔"
  286. // @Param State query int true "状态"
  287. // @Param KeyWord query string true "搜索关键词"
  288. // @Param PublishSort query string true "desc:降序,asc 升序(预留)"
  289. // @Param FilterReportType query string true "筛选事件类型,1:公共研报,2:共享研报,3:我的研报"
  290. // @Param IncludeFile query string true "是否包含文件, no:不包含"
  291. // @Param SourceFrom query string true "来源, 用英文逗号分隔"
  292. // @Param IsShowMe query string true "只看我, false:全部, true:只看我"
  293. // @Success 200 {object} knowledge.KnowledgeResourceListResp
  294. // @router /resource/search_by_es [get]
  295. func (this *ResourceController) Search() {
  296. br := new(models.BaseResponse).Init()
  297. defer func() {
  298. this.Data["json"] = br
  299. this.ServeJSON()
  300. }()
  301. pageSize, _ := this.GetInt("PageSize")
  302. currentIndex, _ := this.GetInt("CurrentIndex")
  303. classifyIds := this.GetString("ClassifyIds")
  304. // 添加人
  305. addUserIds := this.GetString("SysUserIds")
  306. // 标签
  307. tagIds := this.GetString("TagIds")
  308. keyWord := this.GetString("Keyword")
  309. resourceType, _ := this.GetInt("ResourceType")
  310. sourceFrom := this.GetString("SourceFrom")
  311. includeFile := this.GetString("IncludeFile")
  312. IsShowMe, _ := this.GetBool("IsShowMe")
  313. var startSize int
  314. if pageSize <= 0 {
  315. pageSize = utils.PageSize20
  316. }
  317. if currentIndex <= 0 {
  318. currentIndex = 1
  319. }
  320. startSize = utils.StartIndex(currentIndex, pageSize)
  321. isIncludeFile := true
  322. if includeFile == "no" {
  323. isIncludeFile = false
  324. }
  325. var searchUserIds []int
  326. if addUserIds != "" {
  327. // 转成数组,并把类型转成int
  328. addUserIdsArr := strings.Split(addUserIds, ",")
  329. for _, v := range addUserIdsArr {
  330. addUserId, _ := strconv.Atoi(v)
  331. searchUserIds = append(searchUserIds, addUserId)
  332. }
  333. }
  334. var myId int
  335. if IsShowMe {
  336. myId = this.SysUser.AdminId
  337. }
  338. var sourceFromArr []string
  339. if sourceFrom != "" {
  340. sourceFromArr = strings.Split(sourceFrom, ",")
  341. }
  342. var classifyIdsInt []int
  343. if classifyIds != "" {
  344. //转成数组,并把类型转成int
  345. classifyIdsArr := strings.Split(classifyIds, ",")
  346. for _, v := range classifyIdsArr {
  347. classifyId, _ := strconv.Atoi(v)
  348. classifyIdsInt = append(classifyIdsInt, classifyId)
  349. }
  350. }
  351. var tagIdsInt []int
  352. if tagIds != "" {
  353. //转成数组,并把类型转成int
  354. tagIdsArr := strings.Split(tagIds, ",")
  355. for _, v := range tagIdsArr {
  356. tagId, _ := strconv.Atoi(v)
  357. tagIdsInt = append(tagIdsInt, tagId)
  358. }
  359. }
  360. listTmp, total, err := knowledgeSrv.SearchKnowledgeResourceByEs(
  361. resourceType,
  362. keyWord,
  363. searchUserIds,
  364. myId,
  365. classifyIdsInt,
  366. sourceFromArr,
  367. tagIdsInt,
  368. isIncludeFile,
  369. startSize,
  370. pageSize,
  371. )
  372. if err != nil {
  373. br.Msg = "获取失败"
  374. br.ErrMsg = "获取失败,Err:" + err.Error()
  375. return
  376. }
  377. var list []*knowledge.KnowledgeResourceList
  378. // 整理分类ID
  379. classifyFullNameMap := make(map[int]string)
  380. classifyIdsArr := make([]int, 0)
  381. for _, v := range listTmp {
  382. classifyIdsArr = append(classifyIdsArr, v.ClassifyId)
  383. }
  384. // 获取分类列表
  385. classifyList, err := knowledge.GetFullClassifyListByIdList(classifyIdsArr, resourceType)
  386. if err != nil {
  387. br.Msg = "获取失败"
  388. br.ErrMsg = "获取失败,Err:" + err.Error()
  389. return
  390. }
  391. for _, v := range classifyList {
  392. name := v.ClassifyName
  393. if v.ParentName != "" {
  394. name = v.ParentName + "/" + name
  395. }
  396. if v.RootName != "" {
  397. name = v.RootName + "/" + name
  398. }
  399. classifyFullNameMap[v.ClassifyId] = name
  400. }
  401. // 整理标签ID
  402. tagNameMap := make(map[int]string)
  403. tagIdsArr := make([]int, 0)
  404. for _, v := range listTmp {
  405. tagIdsArr = append(tagIdsArr, v.TagId)
  406. }
  407. // 获取标签列表
  408. tagObj := new(knowledge.KnowledgeTag)
  409. tagList, err := tagObj.GetListByIdList(tagIdsArr, resourceType)
  410. if err != nil {
  411. br.Msg = "获取失败"
  412. br.ErrMsg = "获取失败,Err:" + err.Error()
  413. }
  414. for _, v := range tagList {
  415. tagNameMap[v.TagId] = v.TagName
  416. }
  417. for _, v := range listTmp {
  418. var startTime, endTime string
  419. if v.StartTime != nil && !v.StartTime.IsZero() {
  420. startTime = v.StartTime.In(time.Local).Format(utils.FormatDateTime)
  421. if resourceType == knowledge.KnowledgeResourceTypeOpinion || resourceType == knowledge.KnowledgeResourceTypeKnow {
  422. startTime = v.StartTime.In(time.Local).Format(utils.FormatDate)
  423. }
  424. }
  425. if v.EndTime != nil && !v.EndTime.IsZero() {
  426. endTime = v.EndTime.In(time.Local).Format(utils.FormatDateTime)
  427. if resourceType == knowledge.KnowledgeResourceTypeOpinion || resourceType == knowledge.KnowledgeResourceTypeKnow {
  428. endTime = v.EndTime.In(time.Local).Format(utils.FormatDate)
  429. }
  430. }
  431. modifyTime := v.ModifyTime.In(time.Local).Format(utils.FormatDateTime)
  432. createTime := v.CreateTime.In(time.Local).Format(utils.FormatDateTime)
  433. tmp := &knowledge.KnowledgeResourceList{
  434. KnowledgeResourceId: v.KnowledgeResourceId,
  435. ResourceType: v.ResourceType,
  436. ClassifyId: v.ClassifyId,
  437. Title: v.Title,
  438. Content: v.Content,
  439. CreateTime: createTime,
  440. ModifyTime: modifyTime,
  441. State: v.State,
  442. ResourceCode: v.ResourceCode,
  443. AdminId: v.AdminId,
  444. AdminRealName: v.AdminRealName,
  445. SourceFrom: v.SourceFrom,
  446. TagId: v.TagId,
  447. StartTime: startTime,
  448. EndTime: endTime,
  449. }
  450. classifyName, ok := classifyFullNameMap[tmp.ClassifyId]
  451. if ok {
  452. tmp.ClassifyFullName = classifyName
  453. }
  454. tagName, ok := tagNameMap[v.TagId]
  455. if ok {
  456. tmp.TagName = tagName
  457. }
  458. list = append(list, tmp)
  459. }
  460. // list := make([]*knowledge.KnowledgeResourceList, 0, len(knowledges))
  461. // for _, v := range knowledges {
  462. // item := new(knowledge.KnowledgeResourceList)
  463. // item.KnowledgeResourceId = v.KnowledgeResourceId
  464. // item.ResourceType = v.ResourceType
  465. // item.ClassifyId = v.ClassifyId
  466. // item.Title = v.Title
  467. // item.CreateTime = v.CreateTime.Format(utils.FormatDateTime)
  468. // item.ModifyTime = v.ModifyTime.Format(utils.FormatDateTime)
  469. // item.State = v.State
  470. // item.Content = v.Content
  471. // item.ResourceCode = v.ResourceCode
  472. // item.AdminId = v.AdminId
  473. // item.AdminRealName = v.AdminRealName
  474. // item.SourceFrom = v.SourceFrom
  475. // item.TagId = v.TagId
  476. // if v.StartTime != nil {
  477. // item.StartTime = v.StartTime.Format(utils.FormatDateTime)
  478. // }
  479. // if v.EndTime != nil {
  480. // item.EndTime = v.EndTime.Format(utils.FormatDateTime)
  481. // }
  482. // list = append(list, item)
  483. // }
  484. page := paging.GetPaging(currentIndex, pageSize, int(total))
  485. resp := new(knowledge.KnowledgeResourceListResp)
  486. resp.Paging = page
  487. resp.List = list
  488. br.Ret = 200
  489. br.Success = true
  490. br.Msg = "获取成功"
  491. br.Data = resp
  492. }
  493. // Add
  494. // @Title 新增事件接口
  495. // @Description 新增事件(区分事件类型)
  496. // @Param request body knowledge.AddReq true "type json string"
  497. // @Success 200 {object} knowledge.AddResp
  498. // @router /resource/add [post]
  499. func (this *ResourceController) Add() {
  500. br := new(models.BaseResponse).Init()
  501. defer func() {
  502. this.Data["json"] = br
  503. this.ServeJSON()
  504. }()
  505. sysUser := this.SysUser
  506. if sysUser == nil {
  507. br.Msg = "请登录"
  508. br.ErrMsg = "请登录,SysUser Is Empty"
  509. br.Ret = 408
  510. return
  511. }
  512. var req knowledge.AddReq
  513. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  514. if err != nil {
  515. br.Msg = "参数解析异常!"
  516. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  517. return
  518. }
  519. if req.Title == `` {
  520. br.Msg = "标题不能为空"
  521. br.ErrMsg = "标题不能为空"
  522. br.IsSendEmail = false
  523. return
  524. }
  525. // 判断标题长度,如果超过600个字符则报错
  526. //fmt.Println(len(req.Title))
  527. if len(req.Title) > 600 {
  528. br.Msg = "标题过长"
  529. return
  530. }
  531. if req.ClassifyId <= 0 {
  532. br.Msg = "分类必填"
  533. br.ErrMsg = "分类必填"
  534. br.IsSendEmail = false
  535. return
  536. }
  537. if req.ResourceType == knowledge.KnowledgeResourceTypeEvent || req.ResourceType == knowledge.KnowledgeResourceTypePolicy || req.ResourceType == knowledge.KnowledgeResourceTypeOpinion {
  538. if req.StartTime == "" {
  539. br.Msg = "开始时间必填"
  540. br.ErrMsg = "开始时间必填"
  541. return
  542. }
  543. }
  544. item, err, errMsg := knowledgeSrv.AddResource(&req, sysUser)
  545. if err != nil {
  546. br.Msg = "保存失败"
  547. if errMsg != "" {
  548. br.Msg = errMsg
  549. }
  550. br.ErrMsg = "保存失败,Err:" + err.Error()
  551. return
  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 := knowledgeSrv.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 := knowledgeSrv.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 := knowledgeSrv.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 3:
  1381. downUrl = "./static/template/知识库上传模板.xlsx"
  1382. fileName = "知识库上传模板.xlsx"
  1383. case 4:
  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. // cancel
  1403. // @Title 设置知识库事件公开接口
  1404. // @Description 设置知识库事件公开接口
  1405. // @Param request body knowledge.AddReq true "type json string"
  1406. // @Success 200 {object} knowledge.AddResp
  1407. // @router /resource/cancel [post]
  1408. func (this *ResourceController) Cancel() {
  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. approveId, err := knowledge_approve.GetKnowledgeApproveIdByKnowledgeResourceId(req.KnowledgeResourceId)
  1426. if err != nil {
  1427. if utils.IsErrNoRow(err) {
  1428. br.Msg = "该知识库事件未发起审批"
  1429. return
  1430. }
  1431. br.Msg = "获取审批记录失败"
  1432. br.ErrMsg = "获取审批记录失败,Err:" + err.Error()
  1433. return
  1434. }
  1435. msg, err := knowledge_approve.KnowledgeResourceApproveCancel(approveId, sysUser.AdminId, sysUser.RealName)
  1436. if err != nil {
  1437. if msg == "" {
  1438. msg = "设置失败"
  1439. }
  1440. br.Msg = msg
  1441. br.ErrMsg = "设置失败,Err:" + err.Error()
  1442. return
  1443. }
  1444. br.Msg = "设置成功"
  1445. br.Ret = 200
  1446. br.Success = true
  1447. }
  1448. // Public
  1449. // @Title 设置知识库事件公开接口
  1450. // @Description 设置知识库事件公开接口
  1451. // @Param request body knowledge.AddReq true "type json string"
  1452. // @Success 200 {object} knowledge.AddResp
  1453. // @router /resource/public [post]
  1454. func (this *ResourceController) Public() {
  1455. br := new(models.BaseResponse).Init()
  1456. defer func() {
  1457. this.Data["json"] = br
  1458. this.ServeJSON()
  1459. }()
  1460. sysUser := this.SysUser
  1461. if sysUser == nil {
  1462. br.Msg = "请重新登录"
  1463. return
  1464. }
  1465. var req knowledge.PublicReq
  1466. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  1467. br.Msg = "参数解析失败"
  1468. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  1469. return
  1470. }
  1471. obj := new(knowledge.KnowledgeResource)
  1472. obj, err := obj.GetById(req.KnowledgeResourceId)
  1473. if err != nil {
  1474. if utils.IsErrNoRow(err) {
  1475. br.Msg = "该知识库事件不存在"
  1476. return
  1477. }
  1478. br.Msg = "获取知识库事件失败"
  1479. br.ErrMsg = "获取知识库事件失败,Err:" + err.Error()
  1480. return
  1481. }
  1482. _, err = knowledge_approve.SubmitKnowledgeResourceApprove(obj.KnowledgeResourceId, obj.Title, obj.ClassifyId, sysUser.AdminId, sysUser.RealName)
  1483. if err != nil {
  1484. br.Msg = "设置公共失败"
  1485. br.ErrMsg = "设置公共失败,Err:" + err.Error()
  1486. return
  1487. }
  1488. msg, err := knowledgeSrv.SetKnowledgeResourcePublic(req.KnowledgeResourceId)
  1489. if err != nil {
  1490. if msg == "" {
  1491. msg = "设置公共失败"
  1492. }
  1493. br.Msg = msg
  1494. br.ErrMsg = "设置失败,Err:" + err.Error()
  1495. return
  1496. }
  1497. br.Msg = "设置成功"
  1498. br.Ret = 200
  1499. br.Success = true
  1500. }
  1501. // BiDashboardTmpResourceList
  1502. // @Title 获取bi看报中的知识资源列表
  1503. // @Description 获取bi看报中的知识资源列表
  1504. // @Success 200 {object} models.EdbdataClassifyResp
  1505. // @Param biDashboardDetailId query int true "看板详情ID"
  1506. // @router /resource/bi_dashboard/list [get]
  1507. func (this *ResourceController) BiDashboardCacheResourceList() {
  1508. br := new(models.BaseResponse).Init()
  1509. defer func() {
  1510. this.Data["json"] = br
  1511. this.ServeJSON()
  1512. }()
  1513. biDashboardDetailId, _ := this.GetInt("BiDashboardDetailId")
  1514. resp, msg, err := knowledgeSrv.GetKnowledgeResourceTmpList(biDashboardDetailId, this.SysUser.AdminId)
  1515. if err != nil {
  1516. if msg == "" {
  1517. msg = "获取数据失败"
  1518. }
  1519. br.Msg = msg
  1520. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  1521. return
  1522. }
  1523. br.Ret = 200
  1524. br.Success = true
  1525. br.Msg = "获取成功"
  1526. br.Data = resp
  1527. }
  1528. // BiDashboardResourceSave
  1529. // @Title 将知识资源库添加到bi看板
  1530. // @Description 将知识资源库添加到bi看板
  1531. // @Param request body knowledge.DeleteReq true "type json string"
  1532. // @Success 200 {object} models.EdbdataClassifyResp
  1533. // @router /resource/bi_dashboard/save [post]
  1534. func (this *ResourceController) BiDashboardResourceSave() {
  1535. br := new(models.BaseResponse).Init()
  1536. defer func() {
  1537. this.Data["json"] = br
  1538. this.ServeJSON()
  1539. }()
  1540. var req knowledge.BiDashboardResourceAddReq
  1541. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  1542. br.Msg = "参数解析失败"
  1543. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  1544. return
  1545. }
  1546. if len(req.KnowledgeResourceList) == 0 {
  1547. // 则删除缓存中的数据
  1548. err := utils.Rc.Delete(utils.CACHE_KEY_BI_DASHBOARD_PREPARE_RESOURCE + strconv.Itoa(this.SysUser.AdminId))
  1549. if err != nil {
  1550. br.Msg = "添加失败"
  1551. br.ErrMsg = "删除缓存失败,Err:" + err.Error()
  1552. return
  1553. }
  1554. br.Msg = "添加成功"
  1555. br.Ret = 200
  1556. br.Success = true
  1557. return
  1558. }
  1559. cacheData, err := json.Marshal(req)
  1560. if err != nil {
  1561. br.Msg = "添加失败"
  1562. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1563. return
  1564. }
  1565. err = utils.Rc.Put(utils.CACHE_KEY_BI_DASHBOARD_PREPARE_RESOURCE+strconv.Itoa(this.SysUser.AdminId), cacheData, time.Hour*24)
  1566. if err != nil {
  1567. br.Msg = "添加失败"
  1568. br.ErrMsg = "添加失败,Err:" + err.Error()
  1569. return
  1570. }
  1571. br.Msg = "添加成功"
  1572. br.Ret = 200
  1573. br.Success = true
  1574. }