resource.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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. "github.com/h2non/filetype"
  12. "github.com/rdlucklib/rdluck_tools/paging"
  13. "github.com/tealeg/xlsx"
  14. "html"
  15. "io/ioutil"
  16. "os"
  17. "path"
  18. "strconv"
  19. "strings"
  20. "time"
  21. )
  22. type ResourceController struct {
  23. controllers.BaseAuthController
  24. }
  25. // @router /resource/list [get]
  26. func (this *ResourceController) List() {
  27. br := new(models.BaseResponse).Init()
  28. defer func() {
  29. this.Data["json"] = br
  30. this.ServeJSON()
  31. }()
  32. pageSize, _ := this.GetInt("PageSize")
  33. currentIndex, _ := this.GetInt("CurrentIndex")
  34. classifyIds := this.GetString("ClassifyIds")
  35. addUserIds, _ := this.GetInt("SysUserIds")
  36. tagIds := this.GetString("TagIds")
  37. keyWord := this.GetString("Keyword")
  38. resourceType, _ := this.GetInt("ResourceType")
  39. sourceFrom := this.GetString("SourceFrom")
  40. var startSize int
  41. if pageSize <= 0 {
  42. pageSize = utils.PageSize20
  43. }
  44. if currentIndex <= 0 {
  45. currentIndex = 1
  46. }
  47. startSize = utils.StartIndex(currentIndex, pageSize)
  48. var condition string
  49. var pars []interface{}
  50. condition += ` AND resource_type = ? `
  51. pars = append(pars, resourceType)
  52. if keyWord != "" {
  53. keyWordArr := strings.Split(keyWord, " ")
  54. lengthKey := len(keyWordArr)
  55. if lengthKey == 1 {
  56. condition += ` AND (title LIKE ?) `
  57. pars = utils.GetLikeKeywordPars(pars, keyWord, 1)
  58. } else if lengthKey > 1 {
  59. for k, v := range keyWordArr {
  60. if k == 0 {
  61. condition += ` AND (title LIKE ? `
  62. pars = utils.GetLikeKeywordPars(pars, v, 1)
  63. } else if k == lengthKey-1 {
  64. condition += ` OR title LIKE ? ) `
  65. pars = utils.GetLikeKeywordPars(pars, v, 1)
  66. } else {
  67. condition += ` OR title LIKE ? `
  68. pars = utils.GetLikeKeywordPars(pars, v, 1)
  69. }
  70. }
  71. }
  72. }
  73. if sourceFrom != "" {
  74. sourceArr := strings.Split(sourceFrom, ",")
  75. condition += ` AND source_from in (?) `
  76. pars = append(pars, sourceArr)
  77. }
  78. if classifyIds != "" {
  79. classifyIdsArr := strings.Split(classifyIds, ",")
  80. var classifyIdsInt []int
  81. for _, v := range classifyIdsArr {
  82. classifyId, _ := strconv.Atoi(v)
  83. classifyIdsInt = append(classifyIdsInt, classifyId)
  84. }
  85. condition += ` AND classify_id in (?) `
  86. pars = append(pars, classifyIdsInt)
  87. }
  88. if addUserIds > 0 {
  89. addUserIdsArr := strings.Split(strconv.Itoa(addUserIds), ",")
  90. var addUserIdsInt []int
  91. for _, v := range addUserIdsArr {
  92. addUserId, _ := strconv.Atoi(v)
  93. addUserIdsInt = append(addUserIdsInt, addUserId)
  94. }
  95. condition += ` AND admin_id in (?) `
  96. pars = append(pars, addUserIdsInt)
  97. }
  98. if tagIds != "" {
  99. tagIdsArr := strings.Split(tagIds, ",")
  100. var tagIdsInt []int
  101. for _, v := range tagIdsArr {
  102. tagId, _ := strconv.Atoi(v)
  103. tagIdsInt = append(tagIdsInt, tagId)
  104. }
  105. condition += ` AND tag_id in (?) `
  106. pars = append(pars, tagIdsInt)
  107. }
  108. var err error
  109. var total int
  110. var list []*knowledge.KnowledgeResourceList
  111. total, err = knowledge.GetKnowledgeResourceListCount(condition, pars)
  112. if err != nil {
  113. br.Msg = "获取失败"
  114. br.ErrMsg = "获取失败,Err:" + err.Error()
  115. return
  116. }
  117. listTmp, err := knowledge.GetKnowledgeResourcePageList(condition, pars, startSize, pageSize)
  118. if err != nil {
  119. br.Msg = "获取失败"
  120. br.ErrMsg = "获取失败,Err:" + err.Error()
  121. return
  122. }
  123. classifyFullNameMap := make(map[int]string)
  124. classifyIdsArr := make([]int, 0)
  125. for _, v := range listTmp {
  126. classifyIdsArr = append(classifyIdsArr, v.ClassifyId)
  127. }
  128. classifyList, err := knowledge.GetFullClassifyListByIdList(classifyIdsArr, resourceType)
  129. if err != nil {
  130. br.Msg = "获取失败"
  131. br.ErrMsg = "获取失败,Err:" + err.Error()
  132. return
  133. }
  134. for _, v := range classifyList {
  135. name := v.ClassifyName
  136. if v.ParentName != "" {
  137. name = v.ParentName + "/" + name
  138. }
  139. if v.RootName != "" {
  140. name = v.RootName + "/" + name
  141. }
  142. classifyFullNameMap[v.ClassifyId] = name
  143. }
  144. tagNameMap := make(map[int]string)
  145. tagIdsArr := make([]int, 0)
  146. for _, v := range listTmp {
  147. tagIdsArr = append(tagIdsArr, v.TagId)
  148. }
  149. tagObj := new(knowledge.KnowledgeTag)
  150. tagList, err := tagObj.GetListByIdList(tagIdsArr, resourceType)
  151. if err != nil {
  152. br.Msg = "获取失败"
  153. br.ErrMsg = "获取失败,Err:" + err.Error()
  154. }
  155. for _, v := range tagList {
  156. tagNameMap[v.TagId] = v.TagName
  157. }
  158. for _, v := range listTmp {
  159. var startTime, endTime string
  160. if v.StartTime != nil && !v.StartTime.IsZero() {
  161. startTime = v.StartTime.In(time.Local).Format(utils.FormatDateTime)
  162. if resourceType == knowledge.KnowledgeResourceTypeOpinion || resourceType == knowledge.KnowledgeResourceTypeKnow {
  163. startTime = v.StartTime.In(time.Local).Format(utils.FormatDate)
  164. }
  165. }
  166. if v.EndTime != nil && !v.EndTime.IsZero() {
  167. endTime = v.EndTime.In(time.Local).Format(utils.FormatDateTime)
  168. if resourceType == knowledge.KnowledgeResourceTypeOpinion || resourceType == knowledge.KnowledgeResourceTypeKnow {
  169. endTime = v.EndTime.In(time.Local).Format(utils.FormatDate)
  170. }
  171. }
  172. modifyTime := v.ModifyTime.In(time.Local).Format(utils.FormatDateTime)
  173. createTime := v.CreateTime.In(time.Local).Format(utils.FormatDateTime)
  174. tmp := &knowledge.KnowledgeResourceList{
  175. KnowledgeResourceId: v.KnowledgeResourceId,
  176. ResourceType: v.ResourceType,
  177. ClassifyId: v.ClassifyId,
  178. Title: v.Title,
  179. CreateTime: createTime,
  180. ModifyTime: modifyTime,
  181. State: v.State,
  182. ResourceCode: v.ResourceCode,
  183. AdminId: v.AdminId,
  184. AdminRealName: v.AdminRealName,
  185. SourceFrom: v.SourceFrom,
  186. TagId: v.TagId,
  187. StartTime: startTime,
  188. EndTime: endTime,
  189. }
  190. classifyName, ok := classifyFullNameMap[tmp.ClassifyId]
  191. if ok {
  192. tmp.ClassifyFullName = classifyName
  193. }
  194. tagName, ok := tagNameMap[v.TagId]
  195. if ok {
  196. tmp.TagName = tagName
  197. }
  198. list = append(list, tmp)
  199. }
  200. page := paging.GetPaging(currentIndex, pageSize, total)
  201. resp := new(knowledge.KnowledgeResourceListResp)
  202. resp.Paging = page
  203. resp.List = list
  204. br.Ret = 200
  205. br.Success = true
  206. br.Msg = "获取成功"
  207. br.Data = resp
  208. }
  209. // @router /resource/add [post]
  210. func (this *ResourceController) Add() {
  211. br := new(models.BaseResponse).Init()
  212. defer func() {
  213. this.Data["json"] = br
  214. this.ServeJSON()
  215. }()
  216. sysUser := this.SysUser
  217. if sysUser == nil {
  218. br.Msg = "请登录"
  219. br.ErrMsg = "请登录,SysUser Is Empty"
  220. br.Ret = 408
  221. return
  222. }
  223. var req knowledge.AddReq
  224. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  225. if err != nil {
  226. br.Msg = "参数解析异常!"
  227. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  228. return
  229. }
  230. if req.Title == `` {
  231. br.Msg = "标题不能为空"
  232. br.ErrMsg = "标题不能为空"
  233. br.IsSendEmail = false
  234. return
  235. }
  236. if len(req.Title) > 600 {
  237. br.Msg = "标题过长"
  238. return
  239. }
  240. if req.ClassifyId <= 0 {
  241. br.Msg = "分类必填"
  242. br.ErrMsg = "分类必填"
  243. br.IsSendEmail = false
  244. return
  245. }
  246. if req.ResourceType == knowledge.KnowledgeResourceTypeEvent || req.ResourceType == knowledge.KnowledgeResourceTypePolicy {
  247. if req.StartTime == "" {
  248. br.Msg = "开始时间必填"
  249. br.ErrMsg = "开始时间必填"
  250. return
  251. }
  252. }
  253. item, err, errMsg := knowledgeServ.AddResource(&req, sysUser)
  254. if err != nil {
  255. br.Msg = "保存失败"
  256. if errMsg != "" {
  257. br.Msg = errMsg
  258. }
  259. br.ErrMsg = "保存失败,Err:" + err.Error()
  260. return
  261. }
  262. resp := new(knowledge.AddResp)
  263. resp.KnowledgeResourceId = item.KnowledgeResourceId
  264. resp.ResourceCode = item.ResourceCode
  265. br.Ret = 200
  266. br.Success = true
  267. br.Msg = "保存成功"
  268. br.Data = resp
  269. }
  270. // @router /resource/edit [post]
  271. func (this *ResourceController) Edit() {
  272. br := new(models.BaseResponse).Init()
  273. defer func() {
  274. this.Data["json"] = br
  275. this.ServeJSON()
  276. }()
  277. sysUser := this.SysUser
  278. if sysUser == nil {
  279. br.Msg = "请登录"
  280. br.ErrMsg = "请登录,SysUser Is Empty"
  281. br.Ret = 408
  282. return
  283. }
  284. var req knowledge.EditReq
  285. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  286. if err != nil {
  287. br.Msg = "参数解析异常!"
  288. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  289. return
  290. }
  291. if req.KnowledgeResourceId <= 0 {
  292. br.Msg = "请选择事件"
  293. br.ErrMsg = "请选择事件"
  294. return
  295. }
  296. if req.Title == `` {
  297. br.Msg = "标题不能为空"
  298. br.ErrMsg = "标题不能为空"
  299. br.IsSendEmail = false
  300. return
  301. }
  302. if len(req.Title) > 600 {
  303. br.Msg = "标题过长"
  304. return
  305. }
  306. if req.ClassifyId <= 0 {
  307. br.Msg = "分类必填"
  308. br.ErrMsg = "分类必填"
  309. br.IsSendEmail = false
  310. return
  311. }
  312. if req.ResourceType == knowledge.KnowledgeResourceTypeEvent || req.ResourceType == knowledge.KnowledgeResourceTypePolicy {
  313. if req.StartTime == "" {
  314. br.Msg = "开始时间必填"
  315. br.ErrMsg = "开始时间必填"
  316. return
  317. }
  318. }
  319. resourceObj := new(knowledge.KnowledgeResource)
  320. resourceInfo, e := resourceObj.GetById(int(req.KnowledgeResourceId))
  321. if e != nil {
  322. if utils.IsErrNoRow(e) {
  323. br.Msg = "事件已被删除, 请刷新页面"
  324. return
  325. }
  326. br.Msg = "操作失败"
  327. br.ErrMsg = "获取事件失败, Err: " + e.Error()
  328. return
  329. }
  330. if resourceInfo.ResourceType != req.ResourceType {
  331. br.Msg = "事件类型错误"
  332. return
  333. }
  334. err, errMsg := knowledgeServ.EditResource(resourceInfo, req, sysUser)
  335. if err != nil {
  336. br.Msg = "保存失败"
  337. if errMsg != "" {
  338. br.Msg = errMsg
  339. }
  340. br.ErrMsg = "保存失败,Err:" + err.Error()
  341. return
  342. }
  343. resp := new(knowledge.EditResp)
  344. resp.KnowledgeResourceId = req.KnowledgeResourceId
  345. resp.ResourceCode = resourceInfo.ResourceCode
  346. br.Ret = 200
  347. br.Success = true
  348. br.Msg = "保存成功"
  349. br.Data = resp
  350. }
  351. // @router /resource/detail [get]
  352. func (this *ResourceController) Detail() {
  353. br := new(models.BaseResponse).Init()
  354. defer func() {
  355. this.Data["json"] = br
  356. this.ServeJSON()
  357. }()
  358. knowledgeResourceId, err := this.GetInt("KnowledgeResourceId")
  359. if err != nil {
  360. br.Msg = "获取参数失败!"
  361. br.ErrMsg = "获取参数失败,Err:" + err.Error()
  362. return
  363. }
  364. if knowledgeResourceId <= 0 {
  365. br.Msg = "参数错误"
  366. return
  367. }
  368. resourceType, _ := this.GetInt("ResourceType")
  369. resourceObj := new(knowledge.KnowledgeResource)
  370. item, err := resourceObj.GetById(knowledgeResourceId)
  371. if err != nil {
  372. if utils.IsErrNoRow(err) {
  373. br.Msg = "事件已被删除"
  374. return
  375. }
  376. br.Msg = "获取失败"
  377. br.ErrMsg = "获取失败,Err:" + err.Error()
  378. return
  379. }
  380. if item.ResourceType != resourceType {
  381. br.Msg = "事件类型错误"
  382. return
  383. }
  384. item.Content = html.UnescapeString(item.Content)
  385. resp := &knowledge.KnowledgeResourceDetailView{
  386. KnowledgeResource: item,
  387. }
  388. br.Ret = 200
  389. br.Success = true
  390. br.Msg = "获取成功"
  391. br.Data = resp
  392. }
  393. // @router /resource/saveContent [post]
  394. func (this *ResourceController) SaveReportContent() {
  395. br := new(models.BaseResponse).Init()
  396. br.IsSendEmail = false
  397. defer func() {
  398. this.Data["json"] = br
  399. this.ServeJSON()
  400. }()
  401. sysUser := this.SysUser
  402. if sysUser == nil {
  403. br.Msg = "请登录"
  404. br.ErrMsg = "请登录,SysUser Is Empty"
  405. br.Ret = 408
  406. return
  407. }
  408. var req knowledge.SaveReportContent
  409. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  410. if err != nil {
  411. br.Msg = "参数解析异常!"
  412. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  413. return
  414. }
  415. knowledgeResourceId := req.KnowledgeResourceId
  416. if knowledgeResourceId <= 0 {
  417. resp := new(knowledge.SaveReportContentResp)
  418. resp.KnowledgeResourceId = knowledgeResourceId
  419. br.Ret = 200
  420. br.Success = true
  421. br.Msg = "保存成功"
  422. br.Data = resp
  423. return
  424. }
  425. resp := new(knowledge.SaveReportContentResp)
  426. resp.KnowledgeResourceId = knowledgeResourceId
  427. br.Ret = 200
  428. br.Success = true
  429. br.Msg = "保存成功"
  430. br.Data = resp
  431. }
  432. // @router /resource/detail/base [get]
  433. func (this *ResourceController) BaseDetail() {
  434. br := new(models.BaseResponse).Init()
  435. defer func() {
  436. this.Data["json"] = br
  437. this.ServeJSON()
  438. }()
  439. }
  440. // @router /resource/delete [post]
  441. func (this *ResourceController) Delete() {
  442. br := new(models.BaseResponse).Init()
  443. defer func() {
  444. this.Data["json"] = br
  445. this.ServeJSON()
  446. }()
  447. var req knowledge.DeleteReq
  448. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  449. if err != nil {
  450. br.Msg = "参数解析异常!"
  451. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  452. return
  453. }
  454. if req.KnowledgeResourceId <= 0 {
  455. br.Msg = "参数错误"
  456. br.ErrMsg = "参数错误,事件id不可为空"
  457. return
  458. }
  459. resourceObj := new(knowledge.KnowledgeResource)
  460. item, err := resourceObj.GetById(req.KnowledgeResourceId)
  461. if err != nil {
  462. if utils.IsErrNoRow(err) {
  463. br.Msg = "事件不存在"
  464. br.ErrMsg = "事件不存在"
  465. return
  466. }
  467. }
  468. if item.ResourceType != req.ResourceType {
  469. br.Msg = "事件类型不匹配"
  470. return
  471. }
  472. if err = item.Delete(req.KnowledgeResourceId); err != nil {
  473. br.Msg = "删除失败"
  474. br.ErrMsg = "删除事件失败, Err: " + err.Error()
  475. return
  476. }
  477. br.Ret = 200
  478. br.Success = true
  479. br.Msg = "删除成功"
  480. }
  481. // @router /resource/import_add [post]
  482. func (c *ResourceController) ImportData() {
  483. br := new(models.BaseResponse).Init()
  484. defer func() {
  485. c.Data["json"] = br
  486. c.ServeJSON()
  487. }()
  488. sysUser := c.SysUser
  489. if sysUser == nil {
  490. br.Msg = "请重新登录"
  491. return
  492. }
  493. resourceType, _ := c.GetInt("ResourceType")
  494. file, _, err := c.GetFile("EntryFile")
  495. if err != nil {
  496. br.Msg = "获取文件失败"
  497. br.ErrMsg = "获取文件失败,Err:" + err.Error()
  498. return
  499. }
  500. path := "./static/knowledge_import_" + strconv.Itoa(resourceType) + "_" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  501. defer file.Close()
  502. err = c.SaveToFile("EntryFile", path)
  503. if err != nil {
  504. br.Msg = "文件保存失败"
  505. br.ErrMsg = "文件保存失败,Err:" + err.Error()
  506. return
  507. }
  508. defer os.Remove(path)
  509. successCount, failCount, err, errMsg := knowledgeServ.ImportResourceData(path, resourceType, sysUser)
  510. if err != nil {
  511. br.Msg = errMsg
  512. br.ErrMsg = err.Error()
  513. return
  514. }
  515. resp := models.EdbdataImportResp{
  516. SuccessCount: successCount,
  517. FailCount: failCount,
  518. }
  519. if failCount > 0 {
  520. if successCount == 0 {
  521. resp.Status = -1
  522. resp.Msg = "导入失败"
  523. } else {
  524. resp.Status = 1
  525. resp.Msg = "存在部分导入失败"
  526. }
  527. } else {
  528. resp.Status = 0
  529. }
  530. br.Msg = "导入成功"
  531. br.Ret = 200
  532. br.Success = true
  533. br.Data = resp
  534. }
  535. // @router /resource/download_fail [get]
  536. func (this *ResourceController) ImportFailListDownload() {
  537. br := new(models.BaseResponse).Init()
  538. defer func() {
  539. this.Data["json"] = br
  540. this.ServeJSON()
  541. }()
  542. sysUser := this.SysUser
  543. if sysUser == nil {
  544. br.Msg = "请重新登录"
  545. return
  546. }
  547. resourceType, _ := this.GetInt("ResourceType")
  548. failObj := new(knowledge.KnowledgeImportFail)
  549. item, err := failObj.GetListBySysUserId(sysUser.AdminId, resourceType)
  550. if err != nil {
  551. br.Msg = "获取数据失败"
  552. br.Msg = "获取数据失败,Err:" + err.Error()
  553. return
  554. }
  555. xlsxFile := xlsx.NewFile()
  556. SheetName := "导入失败数据"
  557. if this.Lang == utils.EnLangVersion {
  558. SheetName = "Import Fail List"
  559. }
  560. sheet, err := xlsxFile.AddSheet(SheetName)
  561. if err != nil {
  562. br.Msg = "获取sheet失败"
  563. br.Msg = "获取sheet失败,Err:" + err.Error()
  564. return
  565. }
  566. if resourceType == knowledge.KnowledgeResourceTypeEvent || resourceType == knowledge.KnowledgeResourceTypePolicy {
  567. headRow := sheet.AddRow()
  568. headRow.AddCell().SetValue("开始日期")
  569. headRow.AddCell().SetValue("开始时间")
  570. headRow.AddCell().SetValue("结束日期")
  571. headRow.AddCell().SetValue("结束时间")
  572. headRow.AddCell().SetValue("标题")
  573. headRow.AddCell().SetValue("正文")
  574. headRow.AddCell().SetValue("来源")
  575. headRow.AddCell().SetValue("一级分类")
  576. headRow.AddCell().SetValue("二级分类")
  577. headRow.AddCell().SetValue("三级分类")
  578. headRow.AddCell().SetValue("标签")
  579. headRow.AddCell().SetValue("备注")
  580. for _, v := range item {
  581. row := sheet.AddRow()
  582. row.AddCell().SetValue(v.StartDate)
  583. row.AddCell().SetValue(v.StartTime)
  584. row.AddCell().SetValue(v.EndDate)
  585. row.AddCell().SetValue(v.EndTime)
  586. row.AddCell().SetValue(v.Title)
  587. row.AddCell().SetValue(v.Content)
  588. row.AddCell().SetValue(v.SourceFrom)
  589. row.AddCell().SetValue(v.ClassifyFirst)
  590. row.AddCell().SetValue(v.ClassifySecond)
  591. row.AddCell().SetValue(v.ClassifyThird)
  592. row.AddCell().SetValue(v.Tag)
  593. row.AddCell().SetValue(v.Remark)
  594. }
  595. } else {
  596. headRow := sheet.AddRow()
  597. headRow.AddCell().SetValue("时间")
  598. headRow.AddCell().SetValue("标题")
  599. headRow.AddCell().SetValue("正文")
  600. headRow.AddCell().SetValue("来源")
  601. headRow.AddCell().SetValue("一级分类")
  602. headRow.AddCell().SetValue("二级分类")
  603. headRow.AddCell().SetValue("三级分类")
  604. headRow.AddCell().SetValue("标签")
  605. headRow.AddCell().SetValue("备注")
  606. for _, v := range item {
  607. row := sheet.AddRow()
  608. row.AddCell().SetValue(v.StartDate)
  609. row.AddCell().SetValue(v.Title)
  610. row.AddCell().SetValue(v.Content)
  611. row.AddCell().SetValue(v.SourceFrom)
  612. row.AddCell().SetValue(v.ClassifyFirst)
  613. row.AddCell().SetValue(v.ClassifySecond)
  614. row.AddCell().SetValue(v.ClassifyThird)
  615. row.AddCell().SetValue(v.Tag)
  616. row.AddCell().SetValue(v.Remark)
  617. }
  618. }
  619. fileName := time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  620. savePath := "./static/" + fileName
  621. err = xlsxFile.Save(savePath)
  622. if err != nil {
  623. br.Msg = "文件保存失败"
  624. br.ErrMsg = "文件保存失败,Err:" + err.Error()
  625. return
  626. }
  627. defer func() {
  628. os.Remove(savePath)
  629. }()
  630. finalFileName := "失败列表.xlsx"
  631. if this.Lang == utils.EnLangVersion {
  632. finalFileName = "Failure List.xlsx"
  633. }
  634. this.Ctx.Output.Download(savePath, finalFileName)
  635. }
  636. // @router /resource/upload [post]
  637. func (this *ResourceController) ResourceUpload() {
  638. br := new(models.BaseResponse).Init()
  639. defer func() {
  640. if br.ErrMsg == "" {
  641. br.IsSendEmail = false
  642. }
  643. this.Data["json"] = br
  644. this.ServeJSON()
  645. }()
  646. sysUser := this.SysUser
  647. if sysUser == nil {
  648. br.Msg = "请登录"
  649. br.ErrMsg = "请登录,SysUser Is Empty"
  650. br.Ret = 408
  651. return
  652. }
  653. f, h, e := this.GetFile("File")
  654. if e != nil {
  655. br.Msg = "获取资源信息失败"
  656. br.ErrMsg = "获取资源信息失败, Err:" + e.Error()
  657. return
  658. }
  659. defer func() {
  660. _ = f.Close()
  661. }()
  662. fileData, e := ioutil.ReadAll(f)
  663. if e != nil {
  664. br.Msg = "上传失败"
  665. br.ErrMsg = "读取文件失败, Err: " + e.Error()
  666. return
  667. }
  668. ext := path.Ext(h.Filename)
  669. kind, _ := filetype.Match(fileData)
  670. 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" {
  671. br.Msg = "文件格式有误"
  672. return
  673. }
  674. uploadDir := utils.STATIC_DIR + "hongze/" + time.Now().Format("20060102")
  675. if e = os.MkdirAll(uploadDir, utils.DIR_MOD); e != nil {
  676. br.Msg = "存储目录创建失败"
  677. br.ErrMsg = "存储目录创建失败, Err:" + e.Error()
  678. return
  679. }
  680. ossFileName := utils.GetRandStringNoSpecialChar(28) + ext
  681. filePath := uploadDir + "/" + ossFileName
  682. if e = this.SaveToFile("File", filePath); e != nil {
  683. br.Msg = "文件保存失败"
  684. br.ErrMsg = "文件保存失败, Err:" + e.Error()
  685. return
  686. }
  687. defer func() {
  688. _ = os.Remove(filePath)
  689. }()
  690. ossDir := utils.RESOURCE_DIR + "knowledge_resource/"
  691. resourceUrl := ``
  692. savePath := ossDir + time.Now().Format("200601/20060102/") + ossFileName
  693. ossClient := services.NewOssClient()
  694. if ossClient == nil {
  695. br.Msg = "上传失败"
  696. br.ErrMsg = "初始化OSS服务失败"
  697. return
  698. }
  699. resourceUrl, e = ossClient.UploadFile(ossFileName, filePath, savePath)
  700. if e != nil {
  701. br.Msg = "文件上传失败"
  702. br.ErrMsg = "文件上传失败,Err:" + e.Error()
  703. return
  704. }
  705. resourceItem := &knowledge.UpdateResourceResp{
  706. FileUrl: resourceUrl,
  707. }
  708. br.Msg = "上传成功"
  709. br.Ret = 200
  710. br.Success = true
  711. br.Data = resourceItem
  712. }
  713. // @router /resource/source_from [get]
  714. func (this *ResourceController) GetSourceFrom() {
  715. br := new(models.BaseResponse).Init()
  716. defer func() {
  717. this.Data["json"] = br
  718. this.ServeJSON()
  719. }()
  720. sysUser := this.SysUser
  721. if sysUser == nil {
  722. br.Msg = "请重新登录"
  723. return
  724. }
  725. resourceType, _ := this.GetInt("ResourceType")
  726. keyword := this.GetString("Keyword")
  727. resourceObj := new(knowledge.KnowledgeResource)
  728. list, err := resourceObj.GetSourceFrom(keyword, resourceType)
  729. if err != nil {
  730. br.Msg = "获取数据失败"
  731. br.Msg = "获取数据失败,Err:" + err.Error()
  732. return
  733. }
  734. resp := new(knowledge.GetSourceFromResp)
  735. resp.List = list
  736. br.Ret = 200
  737. br.Success = true
  738. br.Msg = "获取成功"
  739. br.Data = resp
  740. }
  741. // @router /resource/admins [get]
  742. func (this *ResourceController) GetAdminList() {
  743. br := new(models.BaseResponse).Init()
  744. defer func() {
  745. this.Data["json"] = br
  746. this.ServeJSON()
  747. }()
  748. sysUser := this.SysUser
  749. if sysUser == nil {
  750. br.Msg = "请重新登录"
  751. return
  752. }
  753. resourceType, _ := this.GetInt("ResourceType")
  754. keyword := this.GetString("Keyword")
  755. resourceObj := new(knowledge.KnowledgeResource)
  756. list, err := resourceObj.GetAdmins(keyword, resourceType)
  757. if err != nil {
  758. br.Msg = "获取数据失败"
  759. br.Msg = "获取数据失败,Err:" + err.Error()
  760. return
  761. }
  762. resp := new(knowledge.GetAdminsResp)
  763. resp.List = list
  764. br.Ret = 200
  765. br.Success = true
  766. br.Msg = "获取成功"
  767. br.Data = resp
  768. }
  769. // @router /resource/template [get]
  770. func (this *ResourceController) TemplateDownload() {
  771. br := new(models.BaseResponse).Init()
  772. defer func() {
  773. this.Data["json"] = br
  774. this.ServeJSON()
  775. }()
  776. resourceType, _ := this.GetInt("ResourceType")
  777. downUrl := ""
  778. fileName := ""
  779. switch resourceType {
  780. case 1:
  781. downUrl = "./static/template/政策库上传模板.xlsx"
  782. fileName = "政策库上传模板.xlsx"
  783. case 2:
  784. downUrl = "./static/template/观点库上传模板.xlsx"
  785. fileName = "观点库上传模板.xlsx"
  786. case 3:
  787. downUrl = "./static/template/知识库上传模板.xlsx"
  788. fileName = "知识库上传模板.xlsx"
  789. default:
  790. downUrl = "./static/template/事件库上传模板.xlsx"
  791. fileName = "事件库上传模板.xlsx"
  792. }
  793. if _, err := os.Stat(downUrl); os.IsNotExist(err) {
  794. br.Msg = "文件不存在"
  795. return
  796. }
  797. fmt.Println(downUrl, fileName)
  798. this.Ctx.Output.Download(downUrl, fileName)
  799. br.Ret = 200
  800. br.Success = true
  801. br.Msg = "下载成功"
  802. }