document_manage_service.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. "strconv"
  12. )
  13. func DocumentClassifyList() ([]models.ClassifyVO, error) {
  14. logs.Info("DocumentClassifyList")
  15. //获取所有已启用分类
  16. classifyList, err := models.GetClassifyListByKeyword(``, utils.IS_ENABLE)
  17. if err != nil {
  18. return nil, err
  19. }
  20. // 递归处理分类 拿到父级分类和对应的子级分类
  21. classifyVOList := make([]models.ClassifyVO, 0)
  22. for _, classify := range classifyList {
  23. if classify.ParentId == 0 {
  24. classifyVO := models.ClassifyVO{
  25. Id: classify.Id,
  26. ClassifyName: classify.ClassifyName,
  27. ParentId: classify.ParentId,
  28. Sort: classify.Sort,
  29. Enabled: classify.Enabled,
  30. Level: classify.Level,
  31. }
  32. classifyVO.Children = getChildClassify(classifyList, classify.Id)
  33. classifyVOList = append(classifyVOList, classifyVO)
  34. }
  35. }
  36. return classifyVOList, nil
  37. }
  38. func getChildClassify(classifyList []*models.ClassifyList, classifyId int) *[]models.ClassifyVO {
  39. childList := make([]models.ClassifyVO, 0)
  40. for _, classify := range classifyList {
  41. if classify.ParentId == classifyId {
  42. classifyVO := models.ClassifyVO{
  43. Id: classify.Id,
  44. ClassifyName: classify.ClassifyName,
  45. ParentId: classify.ParentId,
  46. Sort: classify.Sort,
  47. Enabled: classify.Enabled,
  48. }
  49. classifyVO.Children = getChildClassify(classifyList, classify.Id)
  50. childList = append(childList, classifyVO)
  51. }
  52. }
  53. return &childList
  54. }
  55. func DocumentVarietyList() ([]models.ChartPermissionVO, error) {
  56. logs.Info("DocumentVarietyList")
  57. cp := new(models.ChartPermission)
  58. var condition string
  59. pars := make([]interface{}, 0)
  60. condition = ` and enabled=?`
  61. pars = append(pars, 1)
  62. chartPermissionList, err := cp.GetItemsByCondition(condition, pars)
  63. if err != nil {
  64. return nil, err
  65. }
  66. // 递归处理品种 拿到父级品种和对应的子级品种
  67. permissionVOList := make([]models.ChartPermissionVO, 0)
  68. for _, chartPermission := range chartPermissionList {
  69. if chartPermission.ParentId == 0 {
  70. permissionVO := models.ChartPermissionVO{
  71. ChartPermissionId: chartPermission.ChartPermissionId,
  72. ChartPermissionName: chartPermission.ChartPermissionName,
  73. ParentId: chartPermission.ParentId,
  74. Enabled: chartPermission.Enabled,
  75. Sort: chartPermission.Sort,
  76. CreatedTime: chartPermission.CreatedTime,
  77. }
  78. permissionVO.Children = getChildVariety(chartPermissionList, permissionVO.ChartPermissionId)
  79. permissionVOList = append(permissionVOList, permissionVO)
  80. }
  81. }
  82. return permissionVOList, nil
  83. }
  84. func getChildVariety(permissionList []*models.ChartPermission, permissionId int) []*models.ChartPermissionVO {
  85. childList := make([]*models.ChartPermissionVO, 0)
  86. for _, permission := range permissionList {
  87. if permission.ParentId == permissionId {
  88. permissionVO := models.ChartPermissionVO{
  89. ChartPermissionId: permission.ChartPermissionId,
  90. ChartPermissionName: permission.ChartPermissionName,
  91. ParentId: permission.ParentId,
  92. Enabled: permission.Enabled,
  93. Sort: permission.Sort,
  94. CreatedTime: permission.CreatedTime,
  95. }
  96. permissionVO.Children = getChildVariety(permissionList, permissionVO.ChartPermissionId)
  97. childList = append(childList, &permissionVO)
  98. }
  99. }
  100. return childList
  101. }
  102. func DocumentReportList(documentType int, chartPermissionIdList []string, classifyIdList []string, keyword string, orderField, orderType string, startSize, pageSize int) (*document_manage_model.OutsideReportPage, error) {
  103. logs.Info("DocumentVarietyList")
  104. var condition string
  105. pars := make([]interface{}, 0)
  106. if documentType == 1 {
  107. condition = ` and t1.source!=3`
  108. }
  109. if len(classifyIdList) > 0 {
  110. // 递归查询子分类
  111. for _, classifyId := range classifyIdList {
  112. id, err := strconv.Atoi(classifyId)
  113. if err != nil {
  114. return nil, err
  115. }
  116. childenClassifyIdList, err := GetAllClassifyIdsByParentId(id)
  117. if err != nil {
  118. return nil, err
  119. }
  120. classifyIdList = append(classifyIdList, childenClassifyIdList...)
  121. }
  122. condition += ` and t1.classify_id in (` + utils.GetOrmInReplace(len(classifyIdList)) + `)`
  123. for _, classifyId := range classifyIdList {
  124. pars = append(pars, classifyId)
  125. }
  126. }
  127. if len(chartPermissionIdList) > 0 {
  128. condition += ` and t2.chart_permission_id in (` + utils.GetOrmInReplace(len(chartPermissionIdList)) + `)`
  129. for _, chartPermissionId := range chartPermissionIdList {
  130. pars = append(pars, chartPermissionId)
  131. }
  132. }
  133. if keyword != "" {
  134. condition += ` and t1.title like ? or t1.sys_user_name like ?`
  135. pars = append(pars, "%"+keyword+"%", "%"+keyword+"%")
  136. }
  137. count, err := document_manage_model.GetOutsideReportListByConditionCount(condition, pars)
  138. if err != nil {
  139. return nil, err
  140. }
  141. reportPage := document_manage_model.OutsideReportPage{}
  142. page := paging.GetPaging(startSize, pageSize, count)
  143. if count <= 0 {
  144. reportPage.Paging = page
  145. return &reportPage, nil
  146. }
  147. if orderField != "" && orderType != "" {
  148. condition += ` order by t1.` + orderField + ` ` + orderType
  149. } else {
  150. condition += ` order by t1.modify_time desc`
  151. }
  152. outsideReportList, err := document_manage_model.GetOutsideReportListByCondition(condition, pars, startSize, pageSize)
  153. if err != nil {
  154. return nil, err
  155. }
  156. reportPage.Paging = page
  157. reportPage.List = outsideReportList
  158. return &reportPage, nil
  159. }
  160. // GetAllClassifyIdsByParentId 递归查询子分类
  161. func GetAllClassifyIdsByParentId(parentId int) ([]string, error) {
  162. var classifyIdList []string
  163. // 获取子分类
  164. classifyList, err := models.GetClassifyListByParentId(parentId)
  165. if err != nil {
  166. return nil, err
  167. }
  168. // 遍历子分类
  169. for _, classify := range classifyList {
  170. classifyIdList = append(classifyIdList, strconv.Itoa(classify.Id))
  171. // 递归调用
  172. subClassifyIds, err := GetAllClassifyIdsByParentId(classify.Id)
  173. if err != nil {
  174. return nil, err
  175. }
  176. classifyIdList = append(classifyIdList, subClassifyIds...)
  177. }
  178. return classifyIdList, nil
  179. }
  180. func RuiSiReportList(classifyIdFirst, classifyIdSecond, classifyIdThird int, keyword string, orderField, orderType string, startSize, pageSize int) (*models.ReportListResp, error) {
  181. logs.Info("RuiSiReportList")
  182. var condition string
  183. pars := make([]interface{}, 0)
  184. if classifyIdFirst > 0 {
  185. condition += ` AND a.classify_id_first = ? `
  186. pars = append(pars, classifyIdFirst)
  187. }
  188. if classifyIdSecond > 0 {
  189. condition += ` AND a.classify_id_second = ? `
  190. pars = append(pars, classifyIdSecond)
  191. }
  192. if classifyIdThird > 0 {
  193. condition += ` AND a.classify_id_third = ? `
  194. pars = append(pars, classifyIdThird)
  195. }
  196. if keyword != "" {
  197. condition += ` and a.title like ? or a.admin_real_name like ?`
  198. pars = append(pars, "%"+keyword+"%", "%"+keyword+"%")
  199. }
  200. // 作者为 全球市场战略研究中心 PCI Research
  201. condition += ` and a.author = '全球市场战略研究中心 PCI Research'`
  202. // 已发布的报告
  203. condition += ` and a.state = 2`
  204. count, err := models.GetReportListCountV1(condition, pars)
  205. if err != nil {
  206. return nil, err
  207. }
  208. reportPage := models.ReportListResp{}
  209. page := paging.GetPaging(startSize, pageSize, count)
  210. if count <= 0 {
  211. reportPage.Paging = page
  212. return &reportPage, nil
  213. }
  214. if orderField != "" && orderType != "" {
  215. condition += ` order by a.` + orderField + ` ` + orderType
  216. } else {
  217. condition += ` order by a.publish_time desc`
  218. }
  219. reportList, err := models.GetReportListByCondition(condition, pars, startSize, pageSize)
  220. if err != nil {
  221. return nil, err
  222. }
  223. reportPage.Paging = page
  224. reportPage.List = reportList
  225. return &reportPage, nil
  226. }
  227. func DocumentRuiSiDetail(reportId int) (*models.ReportDetail, error) {
  228. logs.Info("DocumentRuiSiDetail")
  229. reportDetail, err := models.GetReportById(reportId)
  230. if err != nil {
  231. if err.Error() == utils.ErrNoRow() {
  232. return nil, fmt.Errorf("报告已被删除")
  233. }
  234. return nil, err
  235. }
  236. return reportDetail, nil
  237. }
  238. func DocumentSave(outsideReport *document_manage_model.OutsideReportBO) error {
  239. logs.Info("DocumentSave")
  240. // 保存报告
  241. report := document_manage_model.OutsideReport{
  242. Source: outsideReport.Source,
  243. Title: outsideReport.Title,
  244. Abstract: outsideReport.Abstract,
  245. ClassifyId: outsideReport.ClassifyId,
  246. ClassifyName: outsideReport.ClassifyName,
  247. Content: outsideReport.Content,
  248. SysUserId: outsideReport.SysUserId,
  249. SysUserName: outsideReport.SysUserName,
  250. ReportUpdateTime: utils.GetCurrentTime(),
  251. ModifyTime: utils.GetCurrentTime(),
  252. CreateTime: utils.GetCurrentTime(),
  253. ReportCode: uuid.New().String(),
  254. }
  255. id, err := document_manage_model.SaveOutsideReport(report)
  256. if err != nil {
  257. return err
  258. }
  259. // 保存附件
  260. attachmentList := outsideReport.AttachmentList
  261. if len(attachmentList) > 0 {
  262. for _, attachment := range attachmentList {
  263. if attachment.Title == "" || attachment.Url == "" {
  264. continue
  265. }
  266. attachment.OutsideReportId = int(id)
  267. attachment.CreateTime = utils.GetCurrentTime()
  268. _, err := document_manage_model.SaveOutsideReportAttachment(attachment)
  269. if err != nil {
  270. return err
  271. }
  272. }
  273. }
  274. return err
  275. }
  276. func DocumentReportDetail(outsideReportId int) (*document_manage_model.OutsideReportBO, error) {
  277. logs.Info("DocumentReportDetail")
  278. outsideReport, err := document_manage_model.GetOutsideReportById(outsideReportId)
  279. if err != nil {
  280. return nil, err
  281. }
  282. attachmentList, err := document_manage_model.GetOutsideReportAttachmentListByReportId(outsideReportId)
  283. if err != nil {
  284. return nil, err
  285. }
  286. outsideReportBO := document_manage_model.OutsideReportBO{
  287. OutsideReportId: outsideReportId,
  288. Source: outsideReport.Source,
  289. Title: outsideReport.Title,
  290. Abstract: outsideReport.Abstract,
  291. ClassifyId: outsideReport.ClassifyId,
  292. ClassifyName: outsideReport.ClassifyName,
  293. Content: outsideReport.Content,
  294. SysUserId: outsideReport.SysUserId,
  295. SysUserName: outsideReport.SysUserName,
  296. AttachmentList: attachmentList,
  297. }
  298. return &outsideReportBO, nil
  299. }
  300. func DocumentUpdate(outsideReport *document_manage_model.OutsideReportBO) error {
  301. logs.Info("DocumentUpdate")
  302. report, err := document_manage_model.GetOutsideReportById(outsideReport.OutsideReportId)
  303. if err != nil {
  304. return err
  305. }
  306. if report == nil {
  307. return fmt.Errorf("报告不存在")
  308. }
  309. // 更新报告
  310. if outsideReport.Title != "" {
  311. report.Title = outsideReport.Title
  312. }
  313. if outsideReport.Abstract != "" {
  314. report.Abstract = outsideReport.Abstract
  315. }
  316. if outsideReport.ClassifyId > 0 {
  317. report.ClassifyId = outsideReport.ClassifyId
  318. }
  319. if outsideReport.ClassifyName != "" {
  320. report.ClassifyName = outsideReport.ClassifyName
  321. }
  322. if outsideReport.Content != "" {
  323. report.Content = outsideReport.Content
  324. }
  325. report.ModifyTime = utils.GetCurrentTime()
  326. report.ReportUpdateTime = utils.GetCurrentTime()
  327. err = document_manage_model.UpdateOutsideReport(report)
  328. if err != nil {
  329. return fmt.Errorf("更新报告失败, Err: %s", err.Error())
  330. }
  331. // 更新报告附件
  332. attachmentList := outsideReport.AttachmentList
  333. if len(attachmentList) > 0 {
  334. err = document_manage_model.DeleteReportAttachmentByReportId(outsideReport.OutsideReportId)
  335. if err != nil {
  336. return fmt.Errorf("删除报告附件失败, Err: %s", err.Error())
  337. }
  338. for _, attachment := range attachmentList {
  339. if attachment.Title == "" || attachment.Url == "" {
  340. continue
  341. }
  342. attachment.OutsideReportId = outsideReport.OutsideReportId
  343. attachment.CreateTime = utils.GetCurrentTime()
  344. _, err := document_manage_model.SaveOutsideReportAttachment(attachment)
  345. if err != nil {
  346. return err
  347. }
  348. }
  349. }
  350. return nil
  351. }
  352. func DocumentDelete(outsideReportId int) error {
  353. logs.Info("DocumentDelete")
  354. report, err := document_manage_model.GetOutsideReportById(outsideReportId)
  355. if err != nil {
  356. return err
  357. }
  358. if report == nil {
  359. return fmt.Errorf("报告不存在")
  360. }
  361. err = document_manage_model.DeleteOutsideReport(outsideReportId)
  362. if err != nil {
  363. return err
  364. }
  365. err = document_manage_model.DeleteReportAttachmentByReportId(outsideReportId)
  366. if err != nil {
  367. return err
  368. }
  369. return nil
  370. }