report_v2.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. package models
  2. import (
  3. sql2 "database/sql"
  4. "errors"
  5. "eta/eta_api/global"
  6. "eta/eta_api/models/report"
  7. "eta/eta_api/utils"
  8. )
  9. // AddReportAndChapter
  10. // @Description: 新增报告及章节
  11. // @author: Roc
  12. // @datetime 2024-06-06 17:08:34
  13. // @param reportItem *Report
  14. // @param allGrantUserList []*report.ReportGrant
  15. // @param addReportChapterList []AddReportChapter
  16. // @return reportId int64
  17. // @return err error
  18. func AddReportAndChapter(reportItem *Report, reportFreeLayoutList []*report.ReportFreeLayout, allGrantUserList []*report.ReportGrant, addReportChapterList []AddReportChapter) (reportId int64, err error) {
  19. to := global.DbMap[utils.DbNameReport].Begin()
  20. defer func() {
  21. if err != nil {
  22. _ = to.Rollback()
  23. } else {
  24. _ = to.Commit()
  25. }
  26. }()
  27. // 新增报告
  28. err = to.Create(reportItem).Error
  29. if err != nil {
  30. return
  31. }
  32. reportId = int64(reportItem.Id)
  33. // 新增报告分页内容
  34. if len(reportFreeLayoutList) > 0 {
  35. for _, reportFreeLayout := range reportFreeLayoutList {
  36. reportFreeLayout.ReportId = int(reportId)
  37. }
  38. err = to.CreateInBatches(reportFreeLayoutList, utils.MultiAddNum).Error
  39. if err != nil {
  40. return
  41. }
  42. }
  43. // 新增报告授权
  44. if len(allGrantUserList) > 0 {
  45. for _, v := range allGrantUserList {
  46. v.ReportId = reportItem.Id
  47. }
  48. err = to.CreateInBatches(allGrantUserList, utils.MultiAddNum).Error
  49. if err != nil {
  50. return
  51. }
  52. }
  53. // 新增报告章节
  54. if len(addReportChapterList) > 0 {
  55. for _, addReportChapter := range addReportChapterList {
  56. // 新增章节
  57. chapterItem := addReportChapter.ReportChapter
  58. chapterItem.ReportId = int(reportId)
  59. tmpErr := to.Create(chapterItem).Error
  60. if tmpErr != nil {
  61. err = tmpErr
  62. return
  63. }
  64. // 新增章节授权
  65. if len(addReportChapter.GrantList) > 0 {
  66. grantList := addReportChapter.GrantList
  67. for _, v := range grantList {
  68. v.ReportChapterId = chapterItem.ReportChapterId
  69. }
  70. err = to.CreateInBatches(grantList, utils.MultiAddNum).Error
  71. if err != nil {
  72. return
  73. }
  74. }
  75. // 新增报告章节关联的品种
  76. if len(addReportChapter.GrantPermissionList) > 0 {
  77. permissionList := addReportChapter.GrantPermissionList
  78. for _, v := range permissionList {
  79. v.ReportChapterId = chapterItem.ReportChapterId
  80. }
  81. err = to.CreateInBatches(permissionList, utils.MultiAddNum).Error
  82. if err != nil {
  83. return
  84. }
  85. }
  86. // 新增报告章节分页内容
  87. if len(addReportChapter.ReportChapterFreeLayoutList) > 0 {
  88. reportChapterFreeLayoutList := addReportChapter.ReportChapterFreeLayoutList
  89. for _, reportChapterFreeLayout := range reportChapterFreeLayoutList {
  90. reportChapterFreeLayout.ReportId = int(reportId)
  91. reportChapterFreeLayout.ReportChapterId = chapterItem.ReportChapterId
  92. }
  93. err = to.CreateInBatches(reportChapterFreeLayoutList, utils.MultiAddNum).Error
  94. if err != nil {
  95. return
  96. }
  97. }
  98. }
  99. }
  100. return
  101. }
  102. // EditReportAndPermission
  103. // @Description: 修改报告的基础信息、授权用户权限
  104. // @author: Roc
  105. // @datetime 2024-06-06 17:11:12
  106. // @param reportInfo *Report
  107. // @param updateCols []string
  108. // @param addReportGrantList []*report.ReportGrant
  109. // @param delReportGrantIdList []int
  110. // @return err error
  111. func EditReportAndPermission(reportInfo *Report, updateCols []string, addReportGrantList []*report.ReportGrant, delReportGrantIdList []int) (err error) {
  112. to := global.DbMap[utils.DbNameReport].Begin()
  113. defer func() {
  114. if err != nil {
  115. _ = to.Rollback()
  116. } else {
  117. _ = to.Commit()
  118. }
  119. }()
  120. // 变更报告章节信息
  121. if len(updateCols) > 0 {
  122. err = to.Select(updateCols).Updates(reportInfo).Error
  123. if err != nil {
  124. return
  125. }
  126. }
  127. // 新增报告授权用户
  128. if len(addReportGrantList) > 0 {
  129. err = to.CreateInBatches(addReportGrantList, utils.MultiAddNum).Error
  130. if err != nil {
  131. return
  132. }
  133. }
  134. // 删除报告授权用户
  135. delNum := len(delReportGrantIdList)
  136. if delNum > 0 {
  137. sql := `DELETE FROM report_grant WHERE grant_id IN (` + utils.GetOrmInReplace(delNum) + `)`
  138. err = to.Exec(sql, delReportGrantIdList).Error
  139. if err != nil {
  140. return
  141. }
  142. }
  143. return
  144. }
  145. // AddChapterBaseInfoAndPermission
  146. // @Description: 新增报告章节的基础信息、授权用户权限
  147. // @author: Roc
  148. // @datetime 2024-06-11 15:33:50
  149. // @param reportChapterInfo *ReportChapter
  150. // @param addReportChapterGrantList []*report.ReportChapterGrant
  151. // @param addChapterPermissionMap []*report.ReportChapterPermissionMapping
  152. // @return err error
  153. func AddChapterBaseInfoAndPermission(reportChapterInfo *ReportChapter, addReportChapterGrantList []*report.ReportChapterGrant, addChapterPermissionMap []*report.ReportChapterPermissionMapping) (err error) {
  154. to := global.DbMap[utils.DbNameReport].Begin()
  155. defer func() {
  156. if err != nil {
  157. _ = to.Rollback()
  158. } else {
  159. _ = to.Commit()
  160. }
  161. }()
  162. err = to.Create(reportChapterInfo).Error
  163. if err != nil {
  164. return
  165. }
  166. // 新增报告章节授权用户
  167. if len(addReportChapterGrantList) > 0 {
  168. for k, _ := range addReportChapterGrantList {
  169. addReportChapterGrantList[k].ReportChapterId = reportChapterInfo.ReportChapterId
  170. }
  171. err = to.CreateInBatches(addReportChapterGrantList, utils.MultiAddNum).Error
  172. if err != nil {
  173. return
  174. }
  175. }
  176. // 新增报告章节关联的品种配置
  177. if len(addChapterPermissionMap) > 0 {
  178. for k, _ := range addChapterPermissionMap {
  179. addChapterPermissionMap[k].ReportChapterId = reportChapterInfo.ReportChapterId
  180. }
  181. err = to.CreateInBatches(addChapterPermissionMap, utils.MultiAddNum).Error
  182. if err != nil {
  183. return
  184. }
  185. }
  186. return
  187. }
  188. // EditChapterBaseInfoAndPermission
  189. // @Description: 修改报告章节的基础信息、授权用户权限、品种权限
  190. // @author: Roc
  191. // @datetime 2024-06-05 11:45:04
  192. // @param reportChapterInfo *ReportChapter
  193. // @param updateCols []string
  194. // @param addReportChapterGrantList []report.ReportChapterGrant
  195. // @param addChapterPermissionMap []*report.ReportChapterPermissionMapping
  196. // @param delReportChapterGrantIdList []int
  197. // @param delChapterPermissionMappingIdList []int
  198. // @return err error
  199. func EditChapterBaseInfoAndPermission(reportInfo *Report, reportChapterInfo *ReportChapter, updateCols []string, addReportChapterGrantList []*report.ReportChapterGrant, addChapterPermissionMap []*report.ReportChapterPermissionMapping, delReportChapterGrantIdList, delChapterPermissionMappingIdList []int) (err error) {
  200. to := global.DbMap[utils.DbNameReport].Begin()
  201. defer func() {
  202. if err != nil {
  203. _ = to.Rollback()
  204. } else {
  205. _ = to.Commit()
  206. }
  207. }()
  208. // 变更报告的最后编辑人信息
  209. {
  210. err = to.Select([]string{"LastModifyAdminId", "LastModifyAdminName", "ModifyTime"}).Updates(reportInfo).Error
  211. if err != nil {
  212. return
  213. }
  214. }
  215. // 变更报告章节信息
  216. if len(updateCols) > 0 {
  217. err = to.Select(updateCols).Updates(reportChapterInfo).Error
  218. if err != nil {
  219. return
  220. }
  221. }
  222. // 新增报告章节授权用户
  223. if len(addReportChapterGrantList) > 0 {
  224. err = to.CreateInBatches(addReportChapterGrantList, utils.MultiAddNum).Error
  225. if err != nil {
  226. return
  227. }
  228. }
  229. // 删除报告章节授权用户
  230. delNum := len(delReportChapterGrantIdList)
  231. if delNum > 0 {
  232. sql := `DELETE FROM report_chapter_grant WHERE grant_id IN (` + utils.GetOrmInReplace(delNum) + `)`
  233. err = to.Exec(sql, delReportChapterGrantIdList).Error
  234. if err != nil {
  235. return
  236. }
  237. }
  238. // 新增报告章节的品种配置
  239. if len(addChapterPermissionMap) > 0 {
  240. err = to.CreateInBatches(addChapterPermissionMap, utils.MultiAddNum).Error
  241. if err != nil {
  242. return
  243. }
  244. }
  245. // 删除报告章节的品种配置
  246. delNum = len(delChapterPermissionMappingIdList)
  247. if delNum > 0 {
  248. sql := `DELETE FROM report_chapter_permission_mapping WHERE report_chapter_permission_mapping_id IN (` + utils.GetOrmInReplace(delNum) + `)`
  249. err = to.Exec(sql, delChapterPermissionMappingIdList).Error
  250. if err != nil {
  251. return
  252. }
  253. }
  254. return
  255. }
  256. // DelChapterAndPermission
  257. // @Description: 删除报告章节、授权用户权限、品种权限
  258. // @author: Roc
  259. // @datetime 2024-06-06 17:25:47
  260. // @param reportInfo *Report
  261. // @param updateReportCols []string
  262. // @param reportChapterInfo *ReportChapter
  263. // @return err error
  264. func DelChapterAndPermission(reportInfo *Report, updateReportCols []string, reportChapterInfo *ReportChapter) (err error) {
  265. to := global.DbMap[utils.DbNameReport].Begin()
  266. defer func() {
  267. if err != nil {
  268. _ = to.Rollback()
  269. } else {
  270. _ = to.Commit()
  271. }
  272. }()
  273. // 变更报告信息
  274. if len(updateReportCols) > 0 {
  275. err = to.Select(updateReportCols).Updates(reportInfo).Error
  276. if err != nil {
  277. return
  278. }
  279. }
  280. // 删除报告对应章节
  281. {
  282. err = to.Delete(reportChapterInfo).Error
  283. if err != nil {
  284. return
  285. }
  286. }
  287. // 删除报告章节的授权用户权限
  288. {
  289. sql := `DELETE FROM report_chapter_grant WHERE report_chapter_id = ? `
  290. err = to.Exec(sql, reportChapterInfo.ReportChapterId).Error
  291. if err != nil {
  292. return
  293. }
  294. }
  295. // 删除报告章节的品种配置
  296. {
  297. sql := `DELETE FROM report_chapter_permission_mapping WHERE report_chapter_id = ? `
  298. err = to.Exec(sql, reportChapterInfo.ReportChapterId).Error
  299. if err != nil {
  300. return
  301. }
  302. }
  303. return
  304. }
  305. // GetReportListCountByAuthorized
  306. // @Description: 获取有权限的报告列表的报告数量
  307. // @author: Roc
  308. // @datetime 2024-05-30 15:14:01
  309. // @param condition string
  310. // @param pars []interface{}
  311. // @return count int
  312. // @return err error
  313. func GetReportListCountByAuthorized(condition string, pars []interface{}) (count int, err error) {
  314. o := global.DbMap[utils.DbNameReport]
  315. sql := `SELECT COUNT(1) AS count FROM report as a WHERE 1=1 `
  316. if condition != "" {
  317. sql += condition
  318. }
  319. var countNull sql2.NullInt64
  320. err = o.Raw(sql, pars...).Scan(&countNull).Error
  321. if err != nil {
  322. return
  323. }
  324. if countNull.Valid {
  325. count = int(countNull.Int64)
  326. }
  327. return
  328. }
  329. // GetReportListByAuthorized
  330. // @Description: 获取有权限的报告列表的数据
  331. // @author: Roc
  332. // @datetime 2024-05-30 15:15:07
  333. // @param condition string
  334. // @param pars []interface{}
  335. // @param startSize int
  336. // @param pageSize int
  337. // @return items []*ReportList
  338. // @return err error
  339. func GetReportListByAuthorized(condition string, pars []interface{}, startSize, pageSize int) (items []*ReportList, err error) {
  340. o := global.DbMap[utils.DbNameReport]
  341. sql := `SELECT id,classify_id_first,classify_name_first,classify_id_second,classify_name_second,classify_id_third,classify_name_third,title,stage,create_time,modify_time,content_modify_time,report_create_time,state,author,report_layout,collaborate_type,is_public_publish,abstract,has_chapter,publish_time FROM report as a WHERE 1=1 `
  342. if condition != "" {
  343. sql += condition
  344. }
  345. // 排序:1:未发布;2:已发布;3-待提交;4-待审批;5-已驳回;6-已通过
  346. sql += ` GROUP BY a.id ORDER BY report_create_time DESC LIMIT ?,?`
  347. pars = append(pars, startSize, pageSize)
  348. err = o.Raw(sql, pars...).Find(&items).Error
  349. return
  350. }
  351. // ModifyReportClassifyAndReportChapterTypeByCondition
  352. // @Description:
  353. // @author: Roc
  354. // @datetime 2024-06-17 16:12:44
  355. // @param condition string
  356. // @param pars []interface{}
  357. // @param updateStr string
  358. // @param chapterTypeIdMap map[int]int 当前的章节类型ID ---> 继承的章节类型ID
  359. // @param oldClassifyId int
  360. // @param currClassifyId int
  361. // @param currClassifyName string
  362. // @return err error
  363. func ModifyReportClassifyAndReportChapterTypeByCondition(condition string, pars []interface{}, updateStr string, chapterTypeIdMap map[int]int, oldClassifyId, currClassifyId int, currClassifyName string) (err error) {
  364. to := global.DbMap[utils.DbNameReport].Begin()
  365. defer func() {
  366. if err != nil {
  367. _ = to.Rollback()
  368. } else {
  369. _ = to.Commit()
  370. }
  371. }()
  372. if condition == `` {
  373. err = errors.New("condition不能为空")
  374. return
  375. }
  376. // 修改报告的所属分类
  377. sql := `UPDATE report as a SET ` + updateStr + ` WHERE 1=1 `
  378. if condition != "" {
  379. sql += condition
  380. }
  381. err = to.Exec(sql, pars...).Error
  382. if err != nil {
  383. return
  384. }
  385. // 修改历史报告中的章节分类归属
  386. sql = `UPDATE report_chapter set classify_id_first=?,classify_name_first=? where classify_id_first = ?`
  387. err = to.Exec(sql, currClassifyId, currClassifyName, oldClassifyId).Error
  388. if err != nil {
  389. return
  390. }
  391. for currTypeId, oldTypeId := range chapterTypeIdMap {
  392. // 没有章节类型的不处理
  393. if oldTypeId == 0 {
  394. continue
  395. }
  396. tmpSql := `UPDATE report_chapter set type_id=? where type_id = ?`
  397. err = to.Exec(tmpSql, currTypeId, oldTypeId).Error
  398. if err != nil {
  399. return
  400. }
  401. }
  402. return
  403. }
  404. // EditLayoutImgReq
  405. // @Description: 版图设置请求
  406. type EditLayoutImgReq struct {
  407. ReportId int64 `description:"报告id"`
  408. HeadImg string `description:"报告头图地址"`
  409. EndImg string `description:"报告尾图地址"`
  410. CanvasColor string `description:"画布颜色"`
  411. HeadResourceId int `description:"版头资源ID"`
  412. EndResourceId int `description:"版尾资源ID"`
  413. }