document_manage_service.go 15 KB

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