document_manage_service.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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(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 += ` order by t1.` + orderField + ` ` + orderType
  183. } else {
  184. condition += ` order by t1.modify_time desc`
  185. }
  186. outsideReportList, err := document_manage_model.GetOutsideReportListByCondition(condition, pars, startSize, pageSize)
  187. if err != nil {
  188. return nil, err
  189. }
  190. reportPage.Paging = page
  191. reportPage.List = outsideReportList
  192. return &reportPage, nil
  193. }
  194. func RuiSiReportList(classifyIdFirst, classifyIdSecond, classifyIdThird []string, keyword, orderField, orderType string, startSize, pageSize int) (*models.ReportListResp, error) {
  195. logs.Info("RuiSiReportList")
  196. // 作者为 全球市场战略研究中心 PCI Research
  197. author := "全球市场战略研究中心 PCI Research"
  198. // 已发布的报告
  199. state := 2
  200. count, err := models.GetReportListByCollectCount(classifyIdFirst, classifyIdSecond, classifyIdThird, keyword, author, state)
  201. if err != nil {
  202. return nil, err
  203. }
  204. reportPage := models.ReportListResp{}
  205. page := paging.GetPaging(startSize, pageSize, count)
  206. if count <= 0 {
  207. reportPage.Paging = page
  208. return &reportPage, nil
  209. }
  210. reportList, err := models.GetReportListByCollectList(classifyIdFirst, classifyIdSecond, classifyIdThird, keyword, orderField, orderType, startSize, pageSize, author, state)
  211. if err != nil {
  212. return nil, err
  213. }
  214. for _, report := range reportList {
  215. report.ModifyTime = report.ModifyTime.UTC()
  216. }
  217. reportPage.Paging = page
  218. reportPage.List = reportList
  219. return &reportPage, nil
  220. }
  221. func DocumentRuiSiDetail(reportId int) (*models.ReportDetail, error) {
  222. logs.Info("DocumentRuiSiDetail")
  223. reportDetail, err := models.GetReportById(reportId)
  224. if err != nil {
  225. return nil, err
  226. }
  227. return reportDetail, nil
  228. }
  229. func DocumentSave(outsideReport *document_manage_model.OutsideReportBO) error {
  230. logs.Info("DocumentSave")
  231. // 保存报告
  232. report := document_manage_model.OutsideReport{
  233. Source: outsideReport.Source,
  234. Title: outsideReport.Title,
  235. Abstract: outsideReport.Abstract,
  236. ClassifyId: outsideReport.ClassifyId,
  237. ClassifyName: outsideReport.ClassifyName,
  238. Content: outsideReport.Content,
  239. SysUserId: outsideReport.SysUserId,
  240. SysUserName: outsideReport.SysUserName,
  241. ReportUpdateTime: utils.GetCurrentTime(),
  242. ModifyTime: utils.GetCurrentTime(),
  243. CreateTime: utils.GetCurrentTime(),
  244. }
  245. id, err := document_manage_model.SaveOutsideReport(report)
  246. if err != nil {
  247. return err
  248. }
  249. // 保存附件
  250. attachmentList := outsideReport.AttachmentList
  251. if len(attachmentList) > 0 {
  252. for _, attachment := range attachmentList {
  253. if attachment.Title == "" || attachment.Url == "" {
  254. continue
  255. }
  256. attachment.OutsideReportId = int(id)
  257. attachment.CreateTime = utils.GetCurrentTime()
  258. _, err := document_manage_model.SaveOutsideReportAttachment(attachment)
  259. if err != nil {
  260. return err
  261. }
  262. }
  263. }
  264. return err
  265. }
  266. func DocumentReportDetail(outsideReportId int) (*document_manage_model.OutsideReportBO, error) {
  267. logs.Info("DocumentReportDetail")
  268. outsideReport, err := document_manage_model.GetOutsideReportById(outsideReportId)
  269. if err != nil {
  270. return nil, err
  271. }
  272. attachmentList, err := document_manage_model.GetOutsideReportAttachmentListByReportId(outsideReportId)
  273. if err != nil {
  274. return nil, err
  275. }
  276. outsideReportBO := document_manage_model.OutsideReportBO{
  277. OutsideReportId: outsideReportId,
  278. Source: outsideReport.Source,
  279. Title: outsideReport.Title,
  280. Abstract: outsideReport.Abstract,
  281. ClassifyId: outsideReport.ClassifyId,
  282. ClassifyName: outsideReport.ClassifyName,
  283. Content: html.UnescapeString(outsideReport.Content),
  284. SysUserId: outsideReport.SysUserId,
  285. SysUserName: outsideReport.SysUserName,
  286. CreateTime: outsideReport.CreateTime,
  287. ModifyTime: outsideReport.ModifyTime,
  288. ReportCode: outsideReport.ReportCode,
  289. AttachmentList: attachmentList,
  290. }
  291. return &outsideReportBO, nil
  292. }
  293. func DocumentUpdate(outsideReport *document_manage_model.OutsideReportBO) error {
  294. logs.Info("DocumentUpdate")
  295. report, err := document_manage_model.GetOutsideReportById(outsideReport.OutsideReportId)
  296. if err != nil {
  297. return err
  298. }
  299. if report == nil {
  300. return fmt.Errorf("报告不存在")
  301. }
  302. // 更新报告
  303. if outsideReport.Title != "" {
  304. report.Title = outsideReport.Title
  305. }
  306. if outsideReport.Abstract != "" {
  307. report.Abstract = outsideReport.Abstract
  308. }
  309. if outsideReport.ClassifyId > 0 {
  310. report.ClassifyId = outsideReport.ClassifyId
  311. }
  312. if outsideReport.ClassifyName != "" {
  313. report.ClassifyName = outsideReport.ClassifyName
  314. }
  315. if outsideReport.Content != "" {
  316. report.Content = outsideReport.Content
  317. }
  318. report.ModifyTime = utils.GetCurrentTime()
  319. report.ReportUpdateTime = utils.GetCurrentTime()
  320. err = document_manage_model.UpdateOutsideReport(report)
  321. if err != nil {
  322. return fmt.Errorf("更新报告失败, Err: %s", err.Error())
  323. }
  324. // 更新报告附件
  325. attachmentList := outsideReport.AttachmentList
  326. if len(attachmentList) > 0 {
  327. err = document_manage_model.DeleteReportAttachmentByReportId(outsideReport.OutsideReportId)
  328. if err != nil {
  329. return fmt.Errorf("删除报告附件失败, Err: %s", err.Error())
  330. }
  331. for _, attachment := range attachmentList {
  332. if attachment.Title == "" || attachment.Url == "" {
  333. continue
  334. }
  335. attachment.OutsideReportId = outsideReport.OutsideReportId
  336. attachment.CreateTime = utils.GetCurrentTime()
  337. _, err := document_manage_model.SaveOutsideReportAttachment(attachment)
  338. if err != nil {
  339. return err
  340. }
  341. }
  342. }
  343. return nil
  344. }
  345. func DocumentDelete(outsideReportId int) error {
  346. logs.Info("DocumentDelete")
  347. report, err := document_manage_model.GetOutsideReportById(outsideReportId)
  348. if err != nil {
  349. return err
  350. }
  351. if report == nil {
  352. return fmt.Errorf("报告不存在")
  353. }
  354. err = document_manage_model.DeleteOutsideReport(outsideReportId)
  355. if err != nil {
  356. return err
  357. }
  358. err = document_manage_model.DeleteReportAttachmentByReportId(outsideReportId)
  359. if err != nil {
  360. return err
  361. }
  362. return nil
  363. }
  364. func RuiSiReportListV2(classifyIdList, chartPermissionIdList []string, keyword, orderField, orderType string, startSize, pageSize int) (*models.ReportListResp, error) {
  365. logs.Info("RuiSiReportList")
  366. allClassifyList, err := models.GetClassifyListByIdStrList(classifyIdList)
  367. if err != nil {
  368. return nil, err
  369. }
  370. classifyIdFirst := make([]string, 0)
  371. classifyIdSecond := make([]string, 0)
  372. classifyIdThird := make([]string, 0)
  373. for _, v := range allClassifyList {
  374. switch v.Level {
  375. case 1:
  376. classifyIdFirst = append(classifyIdFirst, strconv.Itoa(v.Id))
  377. case 2:
  378. classifyIdSecond = append(classifyIdSecond, strconv.Itoa(v.Id))
  379. case 3:
  380. classifyIdThird = append(classifyIdThird, strconv.Itoa(v.Id))
  381. }
  382. }
  383. // 作者为 全球市场战略研究中心 PCI Research
  384. author := "全球市场战略研究中心 PCI Research"
  385. // 已发布的报告
  386. state := 2
  387. count, err := models.GetReportListByCollectCountV2(classifyIdFirst, classifyIdSecond, classifyIdThird, chartPermissionIdList, keyword, author, state)
  388. if err != nil {
  389. return nil, err
  390. }
  391. reportPage := models.ReportListResp{}
  392. page := paging.GetPaging(startSize, pageSize, count)
  393. if count <= 0 {
  394. reportPage.Paging = page
  395. return &reportPage, nil
  396. }
  397. reportList, err := models.GetReportListByCollectListV2(classifyIdFirst, classifyIdSecond, classifyIdThird, chartPermissionIdList, keyword, orderField, orderType, startSize, pageSize, author, state)
  398. if err != nil {
  399. return nil, err
  400. }
  401. for _, report := range reportList {
  402. report.ModifyTime = report.ModifyTime.UTC()
  403. }
  404. reportPage.Paging = page
  405. reportPage.List = reportList
  406. return &reportPage, nil
  407. }