document_manage_service.go 11 KB

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