document_manage_service.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. condition += ` and t1.classify_id in (` + utils.GetOrmInReplace(len(classifyIdList)) + `)`
  157. for _, classifyId := range classifyIdList {
  158. pars = append(pars, classifyId)
  159. }
  160. }
  161. if len(chartPermissionIdList) > 0 {
  162. condition += ` and t2.chart_permission_id in (` + utils.GetOrmInReplace(len(chartPermissionIdList)) + `)`
  163. for _, chartPermissionId := range chartPermissionIdList {
  164. pars = append(pars, chartPermissionId)
  165. }
  166. }
  167. if keyword != "" {
  168. condition += ` and (t1.title like ? or t1.sys_user_name like ? )`
  169. pars = append(pars, "%"+keyword+"%", "%"+keyword+"%")
  170. }
  171. count, err := document_manage_model.GetOutsideReportListByConditionCount(condition, pars)
  172. if err != nil {
  173. return nil, err
  174. }
  175. reportPage := document_manage_model.OutsideReportPage{}
  176. page := paging.GetPaging(startSize, pageSize, count)
  177. if count <= 0 {
  178. reportPage.Paging = page
  179. return &reportPage, nil
  180. }
  181. if orderField != "" && orderType != "" {
  182. condition += ` group by t1.outside_report_id order by t1.` + orderField + ` ` + orderType
  183. } else {
  184. condition += ` group by t1.outside_report_id order by t1.report_update_time desc`
  185. }
  186. outsideReportList, err := document_manage_model.GetOutsideReportListByCondition(condition, pars, startSize, pageSize)
  187. if err != nil {
  188. return nil, err
  189. }
  190. // 查询用户已收藏的分类
  191. collectClassifyList, err := models.GetUserCollectClassifyList(userId, 0)
  192. if err != nil {
  193. return nil, err
  194. }
  195. // 转换成map
  196. collectClassifyMap := make(map[int]bool)
  197. for _, collectClassify := range collectClassifyList {
  198. collectClassifyMap[collectClassify.ClassifyId] = true
  199. }
  200. for i, _ := range outsideReportList {
  201. if _, ok := collectClassifyMap[outsideReportList[i].ClassifyId]; ok {
  202. outsideReportList[i].IsCollect = 1
  203. }
  204. }
  205. reportPage.Paging = page
  206. reportPage.List = outsideReportList
  207. return &reportPage, nil
  208. }
  209. func RuiSiReportList(classifyIdFirst, classifyIdSecond, classifyIdThird []string, keyword, orderField, orderType string, startSize, pageSize int) (*models.ReportListResp, error) {
  210. logs.Info("RuiSiReportList")
  211. // 作者为 全球市场战略研究中心 PCI Research
  212. author := "全球市场战略研究中心 PCI Research"
  213. // 已发布的报告
  214. state := 2
  215. count, err := models.GetReportListByCollectCount(classifyIdFirst, classifyIdSecond, classifyIdThird, keyword, author, state)
  216. if err != nil {
  217. return nil, err
  218. }
  219. reportPage := models.ReportListResp{}
  220. page := paging.GetPaging(startSize, pageSize, count)
  221. if count <= 0 {
  222. reportPage.Paging = page
  223. return &reportPage, nil
  224. }
  225. reportList, err := models.GetReportListByCollectList(classifyIdFirst, classifyIdSecond, classifyIdThird, keyword, orderField, orderType, startSize, pageSize, author, state)
  226. if err != nil {
  227. return nil, err
  228. }
  229. for _, report := range reportList {
  230. report.ModifyTime = report.ModifyTime.UTC()
  231. }
  232. reportPage.Paging = page
  233. reportPage.List = reportList
  234. return &reportPage, nil
  235. }
  236. func DocumentRuiSiDetail(reportId int) (*models.ReportDetail, error) {
  237. logs.Info("DocumentRuiSiDetail")
  238. reportDetail, err := models.GetReportById(reportId)
  239. if err != nil {
  240. return nil, err
  241. }
  242. return reportDetail, nil
  243. }
  244. func DocumentSave(outsideReport *document_manage_model.OutsideReportBO) error {
  245. logs.Info("DocumentSave")
  246. // 保存报告
  247. report := document_manage_model.OutsideReport{
  248. Source: outsideReport.Source,
  249. Title: outsideReport.Title,
  250. Abstract: outsideReport.Abstract,
  251. ClassifyId: outsideReport.ClassifyId,
  252. ClassifyName: outsideReport.ClassifyName,
  253. Content: outsideReport.Content,
  254. SysUserId: outsideReport.SysUserId,
  255. SysUserName: outsideReport.SysUserName,
  256. ReportUpdateTime: utils.GetCurrentTime(),
  257. ModifyTime: utils.GetCurrentTime(),
  258. CreateTime: utils.GetCurrentTime(),
  259. }
  260. id, err := document_manage_model.SaveOutsideReport(report)
  261. if err != nil {
  262. return err
  263. }
  264. // 保存附件
  265. attachmentList := outsideReport.AttachmentList
  266. if len(attachmentList) > 0 {
  267. for _, attachment := range attachmentList {
  268. if attachment.Title == "" || attachment.Url == "" {
  269. continue
  270. }
  271. attachment.OutsideReportId = int(id)
  272. attachment.CreateTime = utils.GetCurrentTime()
  273. _, err := document_manage_model.SaveOutsideReportAttachment(attachment)
  274. if err != nil {
  275. return err
  276. }
  277. }
  278. }
  279. return err
  280. }
  281. func DocumentReportDetail(outsideReportId int) (*document_manage_model.OutsideReportBO, error) {
  282. logs.Info("DocumentReportDetail")
  283. outsideReport, err := document_manage_model.GetOutsideReportById(outsideReportId)
  284. if err != nil {
  285. return nil, err
  286. }
  287. attachmentList, err := document_manage_model.GetOutsideReportAttachmentListByReportId(outsideReportId)
  288. if err != nil {
  289. return nil, err
  290. }
  291. outsideReportBO := document_manage_model.OutsideReportBO{
  292. OutsideReportId: outsideReportId,
  293. Source: outsideReport.Source,
  294. Title: outsideReport.Title,
  295. Abstract: outsideReport.Abstract,
  296. ClassifyId: outsideReport.ClassifyId,
  297. ClassifyName: outsideReport.ClassifyName,
  298. Content: html.UnescapeString(outsideReport.Content),
  299. SysUserId: outsideReport.SysUserId,
  300. SysUserName: outsideReport.SysUserName,
  301. CreateTime: outsideReport.CreateTime,
  302. ModifyTime: outsideReport.ModifyTime,
  303. ReportCode: outsideReport.ReportCode,
  304. AttachmentList: attachmentList,
  305. }
  306. return &outsideReportBO, nil
  307. }
  308. func DocumentUpdate(outsideReport *document_manage_model.OutsideReportBO) error {
  309. logs.Info("DocumentUpdate")
  310. report, err := document_manage_model.GetOutsideReportById(outsideReport.OutsideReportId)
  311. if err != nil {
  312. return err
  313. }
  314. if report == nil {
  315. return fmt.Errorf("报告不存在")
  316. }
  317. // 更新报告
  318. if outsideReport.Title != "" {
  319. report.Title = outsideReport.Title
  320. }
  321. if outsideReport.Abstract != "" {
  322. report.Abstract = outsideReport.Abstract
  323. }
  324. if outsideReport.ClassifyId > 0 {
  325. report.ClassifyId = outsideReport.ClassifyId
  326. }
  327. if outsideReport.ClassifyName != "" {
  328. report.ClassifyName = outsideReport.ClassifyName
  329. }
  330. if outsideReport.Content != "" {
  331. report.Content = outsideReport.Content
  332. }
  333. report.ModifyTime = utils.GetCurrentTime()
  334. report.ReportUpdateTime = utils.GetCurrentTime()
  335. err = document_manage_model.UpdateOutsideReport(report)
  336. if err != nil {
  337. return fmt.Errorf("更新报告失败, Err: %s", err.Error())
  338. }
  339. // 更新报告附件
  340. attachmentList := outsideReport.AttachmentList
  341. if len(attachmentList) > 0 {
  342. err = document_manage_model.DeleteReportAttachmentByReportId(outsideReport.OutsideReportId)
  343. if err != nil {
  344. return fmt.Errorf("删除报告附件失败, Err: %s", err.Error())
  345. }
  346. for _, attachment := range attachmentList {
  347. if attachment.Title == "" || attachment.Url == "" {
  348. continue
  349. }
  350. attachment.OutsideReportId = outsideReport.OutsideReportId
  351. attachment.CreateTime = utils.GetCurrentTime()
  352. _, err := document_manage_model.SaveOutsideReportAttachment(attachment)
  353. if err != nil {
  354. return err
  355. }
  356. }
  357. }
  358. return nil
  359. }
  360. func DocumentDelete(outsideReportId int) error {
  361. logs.Info("DocumentDelete")
  362. report, err := document_manage_model.GetOutsideReportById(outsideReportId)
  363. if err != nil {
  364. return err
  365. }
  366. if report == nil {
  367. return fmt.Errorf("报告不存在")
  368. }
  369. err = document_manage_model.DeleteOutsideReport(outsideReportId)
  370. if err != nil {
  371. return err
  372. }
  373. err = document_manage_model.DeleteReportAttachmentByReportId(outsideReportId)
  374. if err != nil {
  375. return err
  376. }
  377. return nil
  378. }
  379. func RuiSiReportListV2(classifyIdList, chartPermissionIdList []string, keyword, orderField, orderType string, userId, startSize, pageSize int) (*models.ReportListResp, error) {
  380. logs.Info("RuiSiReportList")
  381. allClassifyList, err := models.GetClassifyListByIdStrList(classifyIdList)
  382. if err != nil {
  383. return nil, err
  384. }
  385. classifyIdFirst := make([]string, 0)
  386. classifyIdSecond := make([]string, 0)
  387. classifyIdThird := make([]string, 0)
  388. for _, v := range allClassifyList {
  389. switch v.Level {
  390. case 1:
  391. classifyIdFirst = append(classifyIdFirst, strconv.Itoa(v.Id))
  392. case 2:
  393. classifyIdSecond = append(classifyIdSecond, strconv.Itoa(v.Id))
  394. case 3:
  395. classifyIdThird = append(classifyIdThird, strconv.Itoa(v.Id))
  396. }
  397. }
  398. // 作者为 全球市场战略研究中心 PCI Research
  399. author := "全球市场战略研究中心 PCI Research"
  400. // 已发布的报告
  401. state := 2
  402. count, err := models.GetReportListByCollectCountV2(classifyIdFirst, classifyIdSecond, classifyIdThird, chartPermissionIdList, keyword, author, state)
  403. if err != nil {
  404. return nil, err
  405. }
  406. reportPage := models.ReportListResp{}
  407. page := paging.GetPaging(startSize, pageSize, count)
  408. if count <= 0 {
  409. reportPage.Paging = page
  410. return &reportPage, nil
  411. }
  412. reportList, err := models.GetReportListByCollectListV2(classifyIdFirst, classifyIdSecond, classifyIdThird, chartPermissionIdList, keyword, orderField, orderType, startSize, pageSize, author, state)
  413. if err != nil {
  414. return nil, err
  415. }
  416. // 查询用户已收藏的分类
  417. collectClassifyList, err := models.GetUserCollectClassifyList(userId, 0)
  418. if err != nil {
  419. return nil, err
  420. }
  421. // 转换成map
  422. collectClassifyMap := make(map[int]bool)
  423. for _, collectClassify := range collectClassifyList {
  424. collectClassifyMap[collectClassify.ClassifyId] = true
  425. }
  426. for i, report := range reportList {
  427. report.ModifyTime = report.ModifyTime.UTC()
  428. if _, ok := collectClassifyMap[reportList[i].ClassifyIdFirst]; ok {
  429. reportList[i].IsCollect = 1
  430. }
  431. if _, ok := collectClassifyMap[reportList[i].ClassifyIdSecond]; ok {
  432. reportList[i].IsCollect = 1
  433. }
  434. if _, ok := collectClassifyMap[reportList[i].ClassifyIdThird]; ok {
  435. reportList[i].IsCollect = 1
  436. }
  437. }
  438. reportPage.Paging = page
  439. reportPage.List = reportList
  440. return &reportPage, nil
  441. }