document_manage_service.go 12 KB

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