document_manage_service.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. ParentId: permission.ParentId,
  135. Enabled: permission.Enabled,
  136. Sort: permission.Sort,
  137. CreatedTime: permission.CreatedTime,
  138. }
  139. permissionVO.Children = getChildVariety(permissionList, permissionVO.ChartPermissionId)
  140. childList = append(childList, &permissionVO)
  141. }
  142. }
  143. return childList
  144. }
  145. func DocumentReportList(documentType int, chartPermissionIdList []string, classifyIdList []string, keyword string, orderField, orderType string, startSize, pageSize int) (*document_manage_model.OutsideReportPage, error) {
  146. logs.Info("DocumentVarietyList")
  147. var condition string
  148. pars := make([]interface{}, 0)
  149. if documentType == 1 {
  150. condition = ` and t1.source!=3`
  151. }
  152. if len(classifyIdList) > 0 {
  153. condition += ` and t1.classify_id in (` + utils.GetOrmInReplace(len(classifyIdList)) + `)`
  154. for _, classifyId := range classifyIdList {
  155. pars = append(pars, classifyId)
  156. }
  157. }
  158. if len(chartPermissionIdList) > 0 {
  159. condition += ` and t2.chart_permission_id in (` + utils.GetOrmInReplace(len(chartPermissionIdList)) + `)`
  160. for _, chartPermissionId := range chartPermissionIdList {
  161. pars = append(pars, chartPermissionId)
  162. }
  163. }
  164. if keyword != "" {
  165. condition += ` and t1.title like ? or t1.sys_user_name like ?`
  166. pars = append(pars, "%"+keyword+"%", "%"+keyword+"%")
  167. }
  168. count, err := document_manage_model.GetOutsideReportListByConditionCount(condition, pars)
  169. if err != nil {
  170. return nil, err
  171. }
  172. reportPage := document_manage_model.OutsideReportPage{}
  173. page := paging.GetPaging(startSize, pageSize, count)
  174. if count <= 0 {
  175. reportPage.Paging = page
  176. return &reportPage, nil
  177. }
  178. if orderField != "" && orderType != "" {
  179. condition += ` order by t1.` + orderField + ` ` + orderType
  180. } else {
  181. condition += ` order by t1.modify_time desc`
  182. }
  183. outsideReportList, err := document_manage_model.GetOutsideReportListByCondition(condition, pars, startSize, pageSize)
  184. if err != nil {
  185. return nil, err
  186. }
  187. reportPage.Paging = page
  188. reportPage.List = outsideReportList
  189. return &reportPage, nil
  190. }
  191. func RuiSiReportList(classifyIdFirst, classifyIdSecond, classifyIdThird []string, keyword, orderField, orderType string, startSize, pageSize int) (*models.ReportListResp, error) {
  192. logs.Info("RuiSiReportList")
  193. // 作者为 全球市场战略研究中心 PCI Research
  194. author := "全球市场战略研究中心 PCI Research"
  195. // 已发布的报告
  196. state := 2
  197. count, err := models.GetReportListByCollectCount(classifyIdFirst, classifyIdSecond, classifyIdThird, keyword, author, state)
  198. if err != nil {
  199. return nil, err
  200. }
  201. reportPage := models.ReportListResp{}
  202. page := paging.GetPaging(startSize, pageSize, count)
  203. if count <= 0 {
  204. reportPage.Paging = page
  205. return &reportPage, nil
  206. }
  207. reportList, err := models.GetReportListByCollectList(classifyIdFirst, classifyIdSecond, classifyIdThird, keyword, orderField, orderType, startSize, pageSize, author, state)
  208. if err != nil {
  209. return nil, err
  210. }
  211. reportPage.Paging = page
  212. reportPage.List = reportList
  213. return &reportPage, nil
  214. }
  215. func DocumentRuiSiDetail(reportId int) (*models.ReportDetail, error) {
  216. logs.Info("DocumentRuiSiDetail")
  217. reportDetail, err := models.GetReportById(reportId)
  218. if err != nil {
  219. return nil, err
  220. }
  221. return reportDetail, nil
  222. }
  223. func DocumentSave(outsideReport *document_manage_model.OutsideReportBO) error {
  224. logs.Info("DocumentSave")
  225. // 保存报告
  226. report := document_manage_model.OutsideReport{
  227. Source: outsideReport.Source,
  228. Title: outsideReport.Title,
  229. Abstract: outsideReport.Abstract,
  230. ClassifyId: outsideReport.ClassifyId,
  231. ClassifyName: outsideReport.ClassifyName,
  232. Content: outsideReport.Content,
  233. SysUserId: outsideReport.SysUserId,
  234. SysUserName: outsideReport.SysUserName,
  235. ReportUpdateTime: utils.GetCurrentTime(),
  236. ModifyTime: utils.GetCurrentTime(),
  237. CreateTime: utils.GetCurrentTime(),
  238. }
  239. id, err := document_manage_model.SaveOutsideReport(report)
  240. if err != nil {
  241. return err
  242. }
  243. // 保存附件
  244. attachmentList := outsideReport.AttachmentList
  245. if len(attachmentList) > 0 {
  246. for _, attachment := range attachmentList {
  247. if attachment.Title == "" || attachment.Url == "" {
  248. continue
  249. }
  250. attachment.OutsideReportId = int(id)
  251. attachment.CreateTime = utils.GetCurrentTime()
  252. _, err := document_manage_model.SaveOutsideReportAttachment(attachment)
  253. if err != nil {
  254. return err
  255. }
  256. }
  257. }
  258. return err
  259. }
  260. func DocumentReportDetail(outsideReportId int) (*document_manage_model.OutsideReportBO, error) {
  261. logs.Info("DocumentReportDetail")
  262. outsideReport, err := document_manage_model.GetOutsideReportById(outsideReportId)
  263. if err != nil {
  264. return nil, err
  265. }
  266. attachmentList, err := document_manage_model.GetOutsideReportAttachmentListByReportId(outsideReportId)
  267. if err != nil {
  268. return nil, err
  269. }
  270. outsideReportBO := document_manage_model.OutsideReportBO{
  271. OutsideReportId: outsideReportId,
  272. Source: outsideReport.Source,
  273. Title: outsideReport.Title,
  274. Abstract: outsideReport.Abstract,
  275. ClassifyId: outsideReport.ClassifyId,
  276. ClassifyName: outsideReport.ClassifyName,
  277. Content: outsideReport.Content,
  278. SysUserId: outsideReport.SysUserId,
  279. SysUserName: outsideReport.SysUserName,
  280. CreateTime: outsideReport.CreateTime,
  281. ModifyTime: outsideReport.ModifyTime,
  282. ReportCode: outsideReport.ReportCode,
  283. AttachmentList: attachmentList,
  284. }
  285. return &outsideReportBO, nil
  286. }
  287. func DocumentUpdate(outsideReport *document_manage_model.OutsideReportBO) error {
  288. logs.Info("DocumentUpdate")
  289. report, err := document_manage_model.GetOutsideReportById(outsideReport.OutsideReportId)
  290. if err != nil {
  291. return err
  292. }
  293. if report == nil {
  294. return fmt.Errorf("报告不存在")
  295. }
  296. // 更新报告
  297. if outsideReport.Title != "" {
  298. report.Title = outsideReport.Title
  299. }
  300. if outsideReport.Abstract != "" {
  301. report.Abstract = outsideReport.Abstract
  302. }
  303. if outsideReport.ClassifyId > 0 {
  304. report.ClassifyId = outsideReport.ClassifyId
  305. }
  306. if outsideReport.ClassifyName != "" {
  307. report.ClassifyName = outsideReport.ClassifyName
  308. }
  309. if outsideReport.Content != "" {
  310. report.Content = outsideReport.Content
  311. }
  312. report.ModifyTime = utils.GetCurrentTime()
  313. report.ReportUpdateTime = utils.GetCurrentTime()
  314. err = document_manage_model.UpdateOutsideReport(report)
  315. if err != nil {
  316. return fmt.Errorf("更新报告失败, Err: %s", err.Error())
  317. }
  318. // 更新报告附件
  319. attachmentList := outsideReport.AttachmentList
  320. if len(attachmentList) > 0 {
  321. err = document_manage_model.DeleteReportAttachmentByReportId(outsideReport.OutsideReportId)
  322. if err != nil {
  323. return fmt.Errorf("删除报告附件失败, Err: %s", err.Error())
  324. }
  325. for _, attachment := range attachmentList {
  326. if attachment.Title == "" || attachment.Url == "" {
  327. continue
  328. }
  329. attachment.OutsideReportId = outsideReport.OutsideReportId
  330. attachment.CreateTime = utils.GetCurrentTime()
  331. _, err := document_manage_model.SaveOutsideReportAttachment(attachment)
  332. if err != nil {
  333. return err
  334. }
  335. }
  336. }
  337. return nil
  338. }
  339. func DocumentDelete(outsideReportId int) error {
  340. logs.Info("DocumentDelete")
  341. report, err := document_manage_model.GetOutsideReportById(outsideReportId)
  342. if err != nil {
  343. return err
  344. }
  345. if report == nil {
  346. return fmt.Errorf("报告不存在")
  347. }
  348. err = document_manage_model.DeleteOutsideReport(outsideReportId)
  349. if err != nil {
  350. return err
  351. }
  352. err = document_manage_model.DeleteReportAttachmentByReportId(outsideReportId)
  353. if err != nil {
  354. return err
  355. }
  356. return nil
  357. }