resource.go 43 KB

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