document_manage_service.go 12 KB

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