report_v2.go 12 KB

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