document_manage_service.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. // @Author gmy 2024/9/19 16:45:00
  2. package document_manage_service
  3. import (
  4. "eta/eta_mobile/models"
  5. "eta/eta_mobile/models/document_manage_model"
  6. "eta/eta_mobile/utils"
  7. "fmt"
  8. "github.com/beego/beego/v2/core/logs"
  9. "github.com/rdlucklib/rdluck_tools/paging"
  10. "html"
  11. "strconv"
  12. )
  13. func DocumentClassifyList(userId int) ([]models.ClassifyVO, error) {
  14. logs.Info("DocumentClassifyList")
  15. //获取所有已启用分类
  16. var condition string
  17. pars := make([]interface{}, 0)
  18. condition = ` and enabled=?`
  19. pars = append(pars, 1)
  20. classifyList, err := models.GetClassifyListByCondition(condition, pars)
  21. if err != nil {
  22. return nil, err
  23. }
  24. // 查询用户已收藏的分类
  25. collectClassifyList, err := models.GetUserCollectClassifyList(userId, 0)
  26. if err != nil {
  27. return nil, err
  28. }
  29. // 转换成map
  30. collectClassifyMap := make(map[int]bool)
  31. for _, collectClassify := range collectClassifyList {
  32. collectClassifyMap[collectClassify.ClassifyId] = true
  33. }
  34. // 递归处理分类 拿到父级分类和对应的子级分类
  35. classifyVOList := make([]models.ClassifyVO, 0)
  36. for _, classify := range classifyList {
  37. if classify.ParentId == 0 {
  38. classifyVO := models.ClassifyVO{
  39. Id: classify.Id,
  40. ClassifyName: classify.ClassifyName,
  41. ParentId: classify.ParentId,
  42. Sort: classify.Sort,
  43. Enabled: classify.Enabled,
  44. Level: classify.Level,
  45. }
  46. if _, ok := collectClassifyMap[classify.Id]; ok {
  47. classifyVO.IsCollect = 1
  48. }
  49. classifyVO.Children = getChildClassify(classifyList, classify.Id, collectClassifyMap)
  50. classifyVOList = append(classifyVOList, classifyVO)
  51. }
  52. }
  53. return classifyVOList, nil
  54. }
  55. func getChildClassify(classifyList []models.Classify, classifyId int, collectClassifyMap map[int]bool) *[]models.ClassifyVO {
  56. childList := make([]models.ClassifyVO, 0)
  57. for _, classify := range classifyList {
  58. if classify.ParentId == classifyId {
  59. classifyVO := models.ClassifyVO{
  60. Id: classify.Id,
  61. ClassifyName: classify.ClassifyName,
  62. ParentId: classify.ParentId,
  63. Sort: classify.Sort,
  64. Enabled: classify.Enabled,
  65. }
  66. if _, ok := collectClassifyMap[classify.Id]; ok {
  67. classifyVO.IsCollect = 1
  68. }
  69. classifyVO.Children = getChildClassify(classifyList, classify.Id, collectClassifyMap)
  70. childList = append(childList, classifyVO)
  71. }
  72. }
  73. return &childList
  74. }
  75. func DocumentCollectClassify(classifyId, userId int) error {
  76. logs.Info("DocumentCollectClassify")
  77. // 查询用户是否已收藏该分类
  78. collectClassifyList, err := models.GetUserCollectClassifyList(userId, classifyId)
  79. if err != nil {
  80. return err
  81. }
  82. if len(collectClassifyList) > 0 {
  83. err := models.DeleteUserCollectClassify(userId, classifyId)
  84. if err != nil {
  85. return err
  86. }
  87. } else {
  88. err = models.InsertUserCollectClassify(models.UserCollectClassify{
  89. ClassifyId: classifyId,
  90. SysUserId: userId,
  91. CreateTime: utils.GetCurrentTime(),
  92. })
  93. if err != nil {
  94. return err
  95. }
  96. }
  97. return nil
  98. }
  99. func DocumentVarietyList() ([]models.ChartPermissionVO, error) {
  100. logs.Info("DocumentVarietyList")
  101. cp := new(models.ChartPermission)
  102. var condition string
  103. pars := make([]interface{}, 0)
  104. condition = ` and enabled=?`
  105. pars = append(pars, 1)
  106. chartPermissionList, err := cp.GetItemsByCondition(condition, pars)
  107. if err != nil {
  108. return nil, err
  109. }
  110. // 递归处理品种 拿到父级品种和对应的子级品种
  111. permissionVOList := make([]models.ChartPermissionVO, 0)
  112. for _, chartPermission := range chartPermissionList {
  113. if chartPermission.ParentId == 0 {
  114. permissionVO := models.ChartPermissionVO{
  115. ChartPermissionId: chartPermission.ChartPermissionId,
  116. ChartPermissionName: chartPermission.ChartPermissionName,
  117. PermissionName: chartPermission.PermissionName,
  118. ParentId: chartPermission.ParentId,
  119. Enabled: chartPermission.Enabled,
  120. Sort: chartPermission.Sort,
  121. CreatedTime: chartPermission.CreatedTime,
  122. }
  123. permissionVO.Children = getChildVariety(chartPermissionList, permissionVO.ChartPermissionId)
  124. permissionVOList = append(permissionVOList, permissionVO)
  125. }
  126. }
  127. return permissionVOList, nil
  128. }
  129. func getChildVariety(permissionList []*models.ChartPermission, permissionId int) []*models.ChartPermissionVO {
  130. childList := make([]*models.ChartPermissionVO, 0)
  131. for _, permission := range permissionList {
  132. if permission.ParentId == permissionId {
  133. permissionVO := models.ChartPermissionVO{
  134. ChartPermissionId: permission.ChartPermissionId,
  135. ChartPermissionName: permission.ChartPermissionName,
  136. PermissionName: permission.PermissionName,
  137. ParentId: permission.ParentId,
  138. Enabled: permission.Enabled,
  139. Sort: permission.Sort,
  140. CreatedTime: permission.CreatedTime,
  141. }
  142. permissionVO.Children = getChildVariety(permissionList, permissionVO.ChartPermissionId)
  143. childList = append(childList, &permissionVO)
  144. }
  145. }
  146. return childList
  147. }
  148. func DocumentReportList(userId, documentType int, chartPermissionIdList []string, classifyIdList []string, keyword string, orderField, orderType string, startSize, pageSize int) (*document_manage_model.OutsideReportPage, error) {
  149. logs.Info("DocumentVarietyList")
  150. var condition string
  151. pars := make([]interface{}, 0)
  152. if documentType == 1 {
  153. condition = ` and t1.source!=3`
  154. }
  155. if len(classifyIdList) > 0 {
  156. // 递归查询子分类
  157. for _, classifyId := range classifyIdList {
  158. id, err := strconv.Atoi(classifyId)
  159. if err != nil {
  160. return nil, err
  161. }
  162. if id == 0 {
  163. classifyIdList = append(classifyIdList, classifyId)
  164. } else {
  165. childrenClassifyIdList, err := GetAllClassifyIdsByParentId(id)
  166. if err != nil {
  167. return nil, err
  168. }
  169. classifyIdList = append(classifyIdList, childrenClassifyIdList...)
  170. }
  171. }
  172. condition += ` and t1.classify_id in (` + utils.GetOrmInReplace(len(classifyIdList)) + `)`
  173. for _, classifyId := range classifyIdList {
  174. pars = append(pars, classifyId)
  175. }
  176. }
  177. if len(chartPermissionIdList) > 0 {
  178. condition += ` and t2.chart_permission_id in (` + utils.GetOrmInReplace(len(chartPermissionIdList)) + `)`
  179. for _, chartPermissionId := range chartPermissionIdList {
  180. pars = append(pars, chartPermissionId)
  181. }
  182. }
  183. if keyword != "" {
  184. condition += ` and (t1.title like ? or t1.sys_user_name like ?) `
  185. pars = append(pars, "%"+keyword+"%", "%"+keyword+"%")
  186. }
  187. if len(chartPermissionIdList) > 0 {
  188. condition += ` GROUP BY t1.outside_report_id HAVING COUNT(DISTINCT t2.chart_permission_id) = ` + strconv.Itoa(len(chartPermissionIdList))
  189. }
  190. count, err := document_manage_model.GetOutsideReportListByConditionCount(condition, pars, chartPermissionIdList)
  191. if err != nil {
  192. return nil, err
  193. }
  194. reportPage := document_manage_model.OutsideReportPage{}
  195. page := paging.GetPaging(startSize, pageSize, count)
  196. if count <= 0 {
  197. reportPage.Paging = page
  198. return &reportPage, nil
  199. }
  200. if orderField != "" && orderType != "" {
  201. condition += ` order by t1.` + orderField + ` ` + orderType
  202. } else {
  203. condition += ` order by t1.report_update_time desc`
  204. }
  205. outsideReportList, err := document_manage_model.GetOutsideReportListByCondition(condition, pars, startSize, pageSize)
  206. if err != nil {
  207. return nil, err
  208. }
  209. // 查询用户已收藏的分类
  210. collectClassifyList, err := models.GetUserCollectClassifyList(userId, 0)
  211. if err != nil {
  212. return nil, err
  213. }
  214. // 转换成map
  215. collectClassifyMap := make(map[int]bool)
  216. for _, collectClassify := range collectClassifyList {
  217. collectClassifyMap[collectClassify.ClassifyId] = true
  218. }
  219. for i, _ := range outsideReportList {
  220. if _, ok := collectClassifyMap[outsideReportList[i].ClassifyId]; ok {
  221. outsideReportList[i].IsCollect = 1
  222. }
  223. }
  224. reportPage.Paging = page
  225. reportPage.List = outsideReportList
  226. return &reportPage, nil
  227. }
  228. // GetAllClassifyIdsByParentId 递归查询子分类
  229. func GetAllClassifyIdsByParentId(parentId int) ([]string, error) {
  230. var classifyIdList []string
  231. // 获取子分类
  232. classifyList, err := models.GetClassifyListByParentId(parentId)
  233. if err != nil {
  234. return nil, err
  235. }
  236. // 遍历子分类
  237. for _, classify := range classifyList {
  238. classifyIdList = append(classifyIdList, strconv.Itoa(classify.Id))
  239. // 递归调用
  240. subClassifyIds, err := GetAllClassifyIdsByParentId(classify.Id)
  241. if err != nil {
  242. return nil, err
  243. }
  244. classifyIdList = append(classifyIdList, subClassifyIds...)
  245. }
  246. return classifyIdList, nil
  247. }
  248. func RuiSiReportList(classifyIdFirst, classifyIdSecond, classifyIdThird []string, keyword, orderField, orderType string, startSize, pageSize int) (*models.ReportListResp, error) {
  249. logs.Info("RuiSiReportList")
  250. // 作者为 全球市场战略研究中心 PCI Research
  251. author := "全球市场战略研究中心 PCI Research"
  252. // 已发布的报告
  253. state := 2
  254. count, err := models.GetReportListByCollectCount(classifyIdFirst, classifyIdSecond, classifyIdThird, keyword, author, state)
  255. if err != nil {
  256. return nil, err
  257. }
  258. reportPage := models.ReportListResp{}
  259. page := paging.GetPaging(startSize, pageSize, count)
  260. if count <= 0 {
  261. reportPage.Paging = page
  262. return &reportPage, nil
  263. }
  264. reportList, err := models.GetReportListByCollectList(classifyIdFirst, classifyIdSecond, classifyIdThird, keyword, orderField, orderType, startSize, pageSize, author, state)
  265. if err != nil {
  266. return nil, err
  267. }
  268. for _, report := range reportList {
  269. report.ModifyTime = report.ModifyTime.UTC()
  270. }
  271. reportPage.Paging = page
  272. reportPage.List = reportList
  273. return &reportPage, nil
  274. }
  275. func DocumentRuiSiDetail(reportId int) (*models.ReportDetail, error) {
  276. logs.Info("DocumentRuiSiDetail")
  277. reportDetail, err := models.GetReportById(reportId)
  278. if err != nil {
  279. return nil, err
  280. }
  281. return reportDetail, nil
  282. }
  283. func DocumentSave(outsideReport *document_manage_model.OutsideReportBO) error {
  284. logs.Info("DocumentSave")
  285. // 保存报告
  286. report := document_manage_model.OutsideReport{
  287. Source: outsideReport.Source,
  288. Title: outsideReport.Title,
  289. Abstract: outsideReport.Abstract,
  290. ClassifyId: outsideReport.ClassifyId,
  291. ClassifyName: outsideReport.ClassifyName,
  292. Content: outsideReport.Content,
  293. SysUserId: outsideReport.SysUserId,
  294. SysUserName: outsideReport.SysUserName,
  295. ReportUpdateTime: utils.GetCurrentTime(),
  296. ModifyTime: utils.GetCurrentTime(),
  297. CreateTime: utils.GetCurrentTime(),
  298. }
  299. id, err := document_manage_model.SaveOutsideReport(report)
  300. if err != nil {
  301. return err
  302. }
  303. // 保存附件
  304. attachmentList := outsideReport.AttachmentList
  305. if len(attachmentList) > 0 {
  306. for _, attachment := range attachmentList {
  307. if attachment.Title == "" || attachment.Url == "" {
  308. continue
  309. }
  310. attachment.OutsideReportId = int(id)
  311. attachment.CreateTime = utils.GetCurrentTime()
  312. _, err := document_manage_model.SaveOutsideReportAttachment(attachment)
  313. if err != nil {
  314. return err
  315. }
  316. }
  317. }
  318. return err
  319. }
  320. func DocumentReportDetail(outsideReportId int) (*document_manage_model.OutsideReportBO, error) {
  321. logs.Info("DocumentReportDetail")
  322. outsideReport, err := document_manage_model.GetOutsideReportById(outsideReportId)
  323. if err != nil {
  324. return nil, err
  325. }
  326. attachmentList, err := document_manage_model.GetOutsideReportAttachmentListByReportId(outsideReportId)
  327. if err != nil {
  328. return nil, err
  329. }
  330. outsideReportBO := document_manage_model.OutsideReportBO{
  331. OutsideReportId: outsideReportId,
  332. Source: outsideReport.Source,
  333. Title: outsideReport.Title,
  334. Abstract: outsideReport.Abstract,
  335. ClassifyId: outsideReport.ClassifyId,
  336. ClassifyName: outsideReport.ClassifyName,
  337. Content: html.UnescapeString(outsideReport.Content),
  338. SysUserId: outsideReport.SysUserId,
  339. SysUserName: outsideReport.SysUserName,
  340. CreateTime: outsideReport.CreateTime,
  341. ModifyTime: outsideReport.ModifyTime,
  342. ReportCode: outsideReport.ReportCode,
  343. AttachmentList: attachmentList,
  344. }
  345. return &outsideReportBO, nil
  346. }
  347. func DocumentUpdate(outsideReport *document_manage_model.OutsideReportBO) error {
  348. logs.Info("DocumentUpdate")
  349. report, err := document_manage_model.GetOutsideReportById(outsideReport.OutsideReportId)
  350. if err != nil {
  351. return err
  352. }
  353. if report == nil {
  354. return fmt.Errorf("报告不存在")
  355. }
  356. // 更新报告
  357. if outsideReport.Title != "" {
  358. report.Title = outsideReport.Title
  359. }
  360. if outsideReport.Abstract != "" {
  361. report.Abstract = outsideReport.Abstract
  362. }
  363. if outsideReport.ClassifyId > 0 {
  364. report.ClassifyId = outsideReport.ClassifyId
  365. }
  366. if outsideReport.ClassifyName != "" {
  367. report.ClassifyName = outsideReport.ClassifyName
  368. }
  369. if outsideReport.Content != "" {
  370. report.Content = outsideReport.Content
  371. }
  372. report.ModifyTime = utils.GetCurrentTime()
  373. report.ReportUpdateTime = utils.GetCurrentTime()
  374. err = document_manage_model.UpdateOutsideReport(report)
  375. if err != nil {
  376. return fmt.Errorf("更新报告失败, Err: %s", err.Error())
  377. }
  378. // 更新报告附件
  379. attachmentList := outsideReport.AttachmentList
  380. if len(attachmentList) > 0 {
  381. err = document_manage_model.DeleteReportAttachmentByReportId(outsideReport.OutsideReportId)
  382. if err != nil {
  383. return fmt.Errorf("删除报告附件失败, Err: %s", err.Error())
  384. }
  385. for _, attachment := range attachmentList {
  386. if attachment.Title == "" || attachment.Url == "" {
  387. continue
  388. }
  389. attachment.OutsideReportId = outsideReport.OutsideReportId
  390. attachment.CreateTime = utils.GetCurrentTime()
  391. _, err := document_manage_model.SaveOutsideReportAttachment(attachment)
  392. if err != nil {
  393. return err
  394. }
  395. }
  396. }
  397. return nil
  398. }
  399. func DocumentDelete(outsideReportId int) error {
  400. logs.Info("DocumentDelete")
  401. report, err := document_manage_model.GetOutsideReportById(outsideReportId)
  402. if err != nil {
  403. return err
  404. }
  405. if report == nil {
  406. return fmt.Errorf("报告不存在")
  407. }
  408. err = document_manage_model.DeleteOutsideReport(outsideReportId)
  409. if err != nil {
  410. return err
  411. }
  412. err = document_manage_model.DeleteReportAttachmentByReportId(outsideReportId)
  413. if err != nil {
  414. return err
  415. }
  416. return nil
  417. }
  418. func RuiSiReportListV2(classifyIdList, chartPermissionIdList []string, keyword, orderField, orderType string, userId, startSize, pageSize int) (*models.ReportListResp, error) {
  419. logs.Info("RuiSiReportList")
  420. allClassifyList, err := models.GetClassifyListByIdStrList(classifyIdList)
  421. if err != nil {
  422. return nil, err
  423. }
  424. classifyIdFirst := make([]string, 0)
  425. classifyIdSecond := make([]string, 0)
  426. classifyIdThird := make([]string, 0)
  427. for _, v := range allClassifyList {
  428. switch v.Level {
  429. case 1:
  430. classifyIdFirst = append(classifyIdFirst, strconv.Itoa(v.Id))
  431. case 2:
  432. classifyIdSecond = append(classifyIdSecond, strconv.Itoa(v.Id))
  433. case 3:
  434. classifyIdThird = append(classifyIdThird, strconv.Itoa(v.Id))
  435. }
  436. }
  437. // 作者为 全球市场战略研究中心 PCI Research
  438. author := "战研中心 PCIR"
  439. // 已发布的报告
  440. state := 2
  441. count, err := models.GetReportListByCollectCountV2(classifyIdFirst, classifyIdSecond, classifyIdThird, chartPermissionIdList, keyword, author, state)
  442. if err != nil {
  443. return nil, err
  444. }
  445. reportPage := models.ReportListResp{}
  446. page := paging.GetPaging(startSize, pageSize, count)
  447. if count <= 0 {
  448. reportPage.Paging = page
  449. return &reportPage, nil
  450. }
  451. reportList, err := models.GetReportListByCollectListV2(classifyIdFirst, classifyIdSecond, classifyIdThird, chartPermissionIdList, keyword, orderField, orderType, startSize, pageSize, author, state)
  452. if err != nil {
  453. return nil, err
  454. }
  455. // 查询用户已收藏的分类
  456. collectClassifyList, err := models.GetUserCollectClassifyList(userId, 0)
  457. if err != nil {
  458. return nil, err
  459. }
  460. // 转换成map
  461. collectClassifyMap := make(map[int]bool)
  462. for _, collectClassify := range collectClassifyList {
  463. collectClassifyMap[collectClassify.ClassifyId] = true
  464. }
  465. for i, report := range reportList {
  466. report.ModifyTime = report.ModifyTime.UTC()
  467. if _, ok := collectClassifyMap[reportList[i].ClassifyIdFirst]; ok {
  468. reportList[i].IsCollect = 1
  469. }
  470. if _, ok := collectClassifyMap[reportList[i].ClassifyIdSecond]; ok {
  471. reportList[i].IsCollect = 1
  472. }
  473. if _, ok := collectClassifyMap[reportList[i].ClassifyIdThird]; ok {
  474. reportList[i].IsCollect = 1
  475. }
  476. }
  477. reportPage.Paging = page
  478. reportPage.List = reportList
  479. return &reportPage, nil
  480. }