resource.go 49 KB

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