document_manage_service.go 12 KB

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