resource.go 49 KB

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