report_v2.go 13 KB

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