report_v2.go 12 KB

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