document_manage_service.go 12 KB

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