resource.go 48 KB

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