document_manage_service.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. "github.com/beego/beego/v2/core/logs"
  9. "github.com/google/uuid"
  10. "github.com/rdlucklib/rdluck_tools/paging"
  11. "html"
  12. "strconv"
  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. classifyIdList = append(classifyIdList, classifyId)
  122. } else {
  123. childrenClassifyIdList, err := GetAllClassifyIdsByParentId(id)
  124. if err != nil {
  125. return nil, err
  126. }
  127. classifyIdList = append(classifyIdList, childrenClassifyIdList...)
  128. }
  129. }
  130. condition += ` and t1.classify_id in (` + utils.GetOrmInReplace(len(classifyIdList)) + `)`
  131. for _, classifyId := range classifyIdList {
  132. pars = append(pars, classifyId)
  133. }
  134. }
  135. if len(chartPermissionIdList) > 0 {
  136. condition += ` and t2.chart_permission_id in (` + utils.GetOrmInReplace(len(chartPermissionIdList)) + `)`
  137. for _, chartPermissionId := range chartPermissionIdList {
  138. pars = append(pars, chartPermissionId)
  139. }
  140. }
  141. if keyword != "" {
  142. condition += ` and (t1.title like ? or t1.sys_user_name like ?) `
  143. pars = append(pars, "%"+keyword+"%", "%"+keyword+"%")
  144. }
  145. count, err := document_manage_model.GetOutsideReportListByConditionCount(condition, pars)
  146. if err != nil {
  147. return nil, err
  148. }
  149. reportPage := document_manage_model.OutsideReportPage{}
  150. page := paging.GetPaging(startSize, pageSize, count)
  151. if count <= 0 {
  152. reportPage.Paging = page
  153. return &reportPage, nil
  154. }
  155. if orderField != "" && orderType != "" {
  156. condition += ` group by t1.outside_report_id order by t1.` + orderField + ` ` + orderType
  157. } else {
  158. condition += ` group by t1.outside_report_id order by t1.report_update_time desc`
  159. }
  160. outsideReportList, err := document_manage_model.GetOutsideReportListByCondition(condition, pars, startSize, pageSize)
  161. if err != nil {
  162. return nil, err
  163. }
  164. reportPage.Paging = page
  165. reportPage.List = outsideReportList
  166. return &reportPage, nil
  167. }
  168. // GetAllClassifyIdsByParentId 递归查询子分类
  169. func GetAllClassifyIdsByParentId(parentId int) ([]string, error) {
  170. var classifyIdList []string
  171. // 获取子分类
  172. classifyList, err := models.GetClassifyListByParentId(parentId)
  173. if err != nil {
  174. return nil, err
  175. }
  176. // 遍历子分类
  177. for _, classify := range classifyList {
  178. classifyIdList = append(classifyIdList, strconv.Itoa(classify.Id))
  179. // 递归调用
  180. subClassifyIds, err := GetAllClassifyIdsByParentId(classify.Id)
  181. if err != nil {
  182. return nil, err
  183. }
  184. classifyIdList = append(classifyIdList, subClassifyIds...)
  185. }
  186. return classifyIdList, nil
  187. }
  188. func RuiSiReportList(classifyIdFirst, classifyIdSecond, classifyIdThird int, chartPermissionIdList []string, keyword string, orderField, orderType string, startSize, pageSize int) (*models.ReportListResp, error) {
  189. logs.Info("RuiSiReportList")
  190. var condition string
  191. pars := make([]interface{}, 0)
  192. if classifyIdFirst > 0 {
  193. condition += ` AND a.classify_id_first = ? `
  194. pars = append(pars, classifyIdFirst)
  195. }
  196. if classifyIdSecond > 0 {
  197. condition += ` AND a.classify_id_second = ? `
  198. pars = append(pars, classifyIdSecond)
  199. }
  200. if classifyIdThird > 0 {
  201. condition += ` AND a.classify_id_third = ? `
  202. pars = append(pars, classifyIdThird)
  203. }
  204. if keyword != "" {
  205. condition += ` and ( a.title like ? or a.admin_real_name like ? ) `
  206. pars = append(pars, "%"+keyword+"%", "%"+keyword+"%")
  207. }
  208. // 作者为 全球市场战略研究中心 PCI Research
  209. condition += ` and a.author = '全球市场战略研究中心 PCI Research'`
  210. // 已发布的报告
  211. condition += ` and a.state = 2`
  212. if len(chartPermissionIdList) > 0 {
  213. classifyIds, err := models.GetClassifyIdsByPermissionId(chartPermissionIdList)
  214. if err != nil {
  215. return nil, err
  216. }
  217. if len(classifyIds) > 0 {
  218. condition += ` AND ( (a.classify_id_first IN (` + utils.GetOrmInReplace(len(classifyIds)) + `) AND a.classify_id_second = 0)
  219. OR (a.classify_id_second IN (` + utils.GetOrmInReplace(len(classifyIds)) + `) AND a.classify_id_third = 0)
  220. OR a.classify_id_third IN (` + utils.GetOrmInReplace(len(classifyIds)) + `) )`
  221. for _, classifyId := range classifyIds {
  222. pars = append(pars, classifyId)
  223. }
  224. }
  225. }
  226. count, err := models.GetReportListCountV1(condition, pars)
  227. if err != nil {
  228. return nil, err
  229. }
  230. reportPage := models.ReportListResp{}
  231. page := paging.GetPaging(startSize, pageSize, count)
  232. if count <= 0 {
  233. reportPage.Paging = page
  234. return &reportPage, nil
  235. }
  236. if orderField != "" && orderType != "" {
  237. condition += ` group by a.id order by a.` + orderField + ` ` + orderType
  238. } else {
  239. condition += ` group by a.id order by a.publish_time desc`
  240. }
  241. reportList, err := models.GetReportListByCondition(condition, pars, startSize, pageSize)
  242. if err != nil {
  243. return nil, err
  244. }
  245. reportPage.Paging = page
  246. reportPage.List = reportList
  247. return &reportPage, nil
  248. }
  249. func DocumentRuiSiDetail(reportId int) (*models.ReportDetail, error) {
  250. logs.Info("DocumentRuiSiDetail")
  251. reportDetail, err := models.GetReportById(reportId)
  252. if err != nil {
  253. if err.Error() == utils.ErrNoRow() {
  254. return nil, fmt.Errorf("报告已被删除")
  255. }
  256. return nil, err
  257. }
  258. reportDetail.Content = html.UnescapeString(reportDetail.Content)
  259. return reportDetail, nil
  260. }
  261. func DocumentSave(outsideReport *document_manage_model.OutsideReportBO) error {
  262. logs.Info("DocumentSave")
  263. // 保存报告
  264. report := document_manage_model.OutsideReport{
  265. Source: outsideReport.Source,
  266. Title: outsideReport.Title,
  267. Abstract: outsideReport.Abstract,
  268. ClassifyId: outsideReport.ClassifyId,
  269. ClassifyName: outsideReport.ClassifyName,
  270. Content: outsideReport.Content,
  271. SysUserId: outsideReport.SysUserId,
  272. SysUserName: outsideReport.SysUserName,
  273. ReportUpdateTime: utils.GetCurrentTime(),
  274. ModifyTime: utils.GetCurrentTime(),
  275. CreateTime: utils.GetCurrentTime(),
  276. ReportCode: uuid.New().String(),
  277. }
  278. id, err := document_manage_model.SaveOutsideReport(report)
  279. if err != nil {
  280. return err
  281. }
  282. // 保存附件
  283. attachmentList := outsideReport.AttachmentList
  284. if len(attachmentList) > 0 {
  285. for _, attachment := range attachmentList {
  286. if attachment.Title == "" || attachment.Url == "" {
  287. continue
  288. }
  289. attachment.OutsideReportId = int(id)
  290. attachment.CreateTime = utils.GetCurrentTime()
  291. _, err := document_manage_model.SaveOutsideReportAttachment(attachment)
  292. if err != nil {
  293. return err
  294. }
  295. }
  296. }
  297. return err
  298. }
  299. func DocumentReportDetail(outsideReportId int) (*document_manage_model.OutsideReportBO, error) {
  300. logs.Info("DocumentReportDetail")
  301. outsideReport, err := document_manage_model.GetOutsideReportById(outsideReportId)
  302. if err != nil {
  303. return nil, err
  304. }
  305. attachmentList, err := document_manage_model.GetOutsideReportAttachmentListByReportId(outsideReportId)
  306. if err != nil {
  307. return nil, err
  308. }
  309. outsideReportBO := document_manage_model.OutsideReportBO{
  310. OutsideReportId: outsideReportId,
  311. Source: outsideReport.Source,
  312. Title: outsideReport.Title,
  313. Abstract: outsideReport.Abstract,
  314. ClassifyId: outsideReport.ClassifyId,
  315. ClassifyName: outsideReport.ClassifyName,
  316. Content: html.UnescapeString(outsideReport.Content),
  317. SysUserId: outsideReport.SysUserId,
  318. SysUserName: outsideReport.SysUserName,
  319. CreateTime: outsideReport.CreateTime,
  320. ModifyTime: outsideReport.ModifyTime,
  321. ReportCode: outsideReport.ReportCode,
  322. AttachmentList: attachmentList,
  323. }
  324. return &outsideReportBO, nil
  325. }
  326. func DocumentUpdate(outsideReport *document_manage_model.OutsideReportBO) error {
  327. logs.Info("DocumentUpdate")
  328. report, err := document_manage_model.GetOutsideReportById(outsideReport.OutsideReportId)
  329. if err != nil {
  330. return err
  331. }
  332. if report == nil {
  333. return fmt.Errorf("报告不存在")
  334. }
  335. // 更新报告
  336. if outsideReport.Title != "" {
  337. report.Title = outsideReport.Title
  338. }
  339. report.Abstract = outsideReport.Abstract
  340. if outsideReport.ClassifyId > 0 {
  341. report.ClassifyId = outsideReport.ClassifyId
  342. }
  343. if outsideReport.ClassifyName != "" {
  344. report.ClassifyName = outsideReport.ClassifyName
  345. }
  346. if outsideReport.Content != "" {
  347. report.Content = outsideReport.Content
  348. }
  349. report.ModifyTime = utils.GetCurrentTime()
  350. //report.ReportUpdateTime = utils.GetCurrentTime()
  351. err = document_manage_model.UpdateOutsideReport(report)
  352. if err != nil {
  353. return fmt.Errorf("更新报告失败, Err: %s", err.Error())
  354. }
  355. // 更新报告附件
  356. attachmentList := outsideReport.AttachmentList
  357. if len(attachmentList) > 0 {
  358. err = document_manage_model.DeleteReportAttachmentByReportId(outsideReport.OutsideReportId)
  359. if err != nil {
  360. return fmt.Errorf("删除报告附件失败, Err: %s", err.Error())
  361. }
  362. for _, attachment := range attachmentList {
  363. if attachment.Title == "" || attachment.Url == "" {
  364. continue
  365. }
  366. attachment.OutsideReportId = outsideReport.OutsideReportId
  367. attachment.CreateTime = utils.GetCurrentTime()
  368. _, err := document_manage_model.SaveOutsideReportAttachment(attachment)
  369. if err != nil {
  370. return err
  371. }
  372. }
  373. }
  374. return nil
  375. }
  376. func DocumentDelete(outsideReportId int) error {
  377. logs.Info("DocumentDelete")
  378. report, err := document_manage_model.GetOutsideReportById(outsideReportId)
  379. if err != nil {
  380. return err
  381. }
  382. if report == nil {
  383. return fmt.Errorf("报告不存在")
  384. }
  385. err = document_manage_model.DeleteOutsideReport(outsideReportId)
  386. if err != nil {
  387. return err
  388. }
  389. err = document_manage_model.DeleteReportAttachmentByReportId(outsideReportId)
  390. if err != nil {
  391. return err
  392. }
  393. return nil
  394. }