document_manage_service.go 12 KB

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