resource.go 48 KB

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