document_manage_service.go 12 KB

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