report_v2.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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(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. if len(updateCols) > 0 {
  205. _, err = to.Update(reportChapterInfo, updateCols...)
  206. if err != nil {
  207. return
  208. }
  209. }
  210. // 新增报告章节授权用户
  211. if len(addReportChapterGrantList) > 0 {
  212. _, err = to.InsertMulti(500, addReportChapterGrantList)
  213. if err != nil {
  214. return
  215. }
  216. }
  217. // 删除报告章节授权用户
  218. delNum := len(delReportChapterGrantIdList)
  219. if delNum > 0 {
  220. sql := `DELETE FROM report_chapter_grant WHERE grant_id IN (` + utils.GetOrmInReplace(delNum) + `)`
  221. _, err = to.Raw(sql, delReportChapterGrantIdList).Exec()
  222. if err != nil {
  223. return
  224. }
  225. }
  226. // 新增报告章节的品种配置
  227. if len(addChapterPermissionMap) > 0 {
  228. _, err = to.InsertMulti(500, addChapterPermissionMap)
  229. if err != nil {
  230. return
  231. }
  232. }
  233. // 删除报告章节的品种配置
  234. delNum = len(delChapterPermissionMappingIdList)
  235. if delNum > 0 {
  236. sql := `DELETE FROM report_chapter_permission_mapping WHERE report_chapter_permission_mapping_id IN (` + utils.GetOrmInReplace(delNum) + `)`
  237. _, err = to.Raw(sql, delChapterPermissionMappingIdList).Exec()
  238. if err != nil {
  239. return
  240. }
  241. }
  242. return
  243. }
  244. // DelChapterAndPermission
  245. // @Description: 删除报告章节、授权用户权限、品种权限
  246. // @author: Roc
  247. // @datetime 2024-06-06 17:25:47
  248. // @param reportInfo *Report
  249. // @param updateReportCols []string
  250. // @param reportChapterInfo *ReportChapter
  251. // @return err error
  252. func DelChapterAndPermission(reportInfo *Report, updateReportCols []string, reportChapterInfo *ReportChapter) (err error) {
  253. o := orm.NewOrmUsingDB("rddp")
  254. to, err := o.Begin()
  255. if err != nil {
  256. return
  257. }
  258. defer func() {
  259. if err != nil {
  260. _ = to.Rollback()
  261. } else {
  262. _ = to.Commit()
  263. }
  264. }()
  265. // 变更报告信息
  266. if len(updateReportCols) > 0 {
  267. _, err = to.Update(reportInfo, updateReportCols...)
  268. if err != nil {
  269. return
  270. }
  271. }
  272. // 删除报告对应章节
  273. {
  274. _, err = to.Delete(reportChapterInfo)
  275. if err != nil {
  276. return
  277. }
  278. }
  279. // 删除报告章节的授权用户权限
  280. {
  281. sql := `DELETE FROM report_chapter_grant WHERE report_chapter_id = ? `
  282. _, err = to.Raw(sql, reportChapterInfo.ReportChapterId).Exec()
  283. if err != nil {
  284. return
  285. }
  286. }
  287. // 删除报告章节的品种配置
  288. {
  289. sql := `DELETE FROM report_chapter_permission_mapping WHERE report_chapter_id = ? `
  290. _, err = to.Raw(sql, reportChapterInfo.ReportChapterId).Exec()
  291. if err != nil {
  292. return
  293. }
  294. }
  295. return
  296. }
  297. // GetReportListCountByAuthorized
  298. // @Description: 获取有权限的报告列表的报告数量
  299. // @author: Roc
  300. // @datetime 2024-05-30 15:14:01
  301. // @param condition string
  302. // @param pars []interface{}
  303. // @return count int
  304. // @return err error
  305. func GetReportListCountByAuthorized(condition string, pars []interface{}) (count int, err error) {
  306. o := orm.NewOrmUsingDB("rddp")
  307. sql := `SELECT COUNT(1) AS count FROM report as a
  308. WHERE 1=1 `
  309. if condition != "" {
  310. sql += condition
  311. }
  312. err = o.Raw(sql, pars).QueryRow(&count)
  313. return
  314. }
  315. // GetReportListByAuthorized
  316. // @Description: 获取有权限的报告列表的数据
  317. // @author: Roc
  318. // @datetime 2024-05-30 15:15:07
  319. // @param condition string
  320. // @param pars []interface{}
  321. // @param startSize int
  322. // @param pageSize int
  323. // @return items []*ReportList
  324. // @return err error
  325. func GetReportListByAuthorized(condition string, pars []interface{}, startSize, pageSize int) (items []*ReportList, err error) {
  326. o := orm.NewOrmUsingDB("rddp")
  327. 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 FROM report as a WHERE 1=1 `
  328. if condition != "" {
  329. sql += condition
  330. }
  331. // 排序:1:未发布;2:已发布;3-待提交;4-待审批;5-已驳回;6-已通过
  332. sql += ` GROUP BY a.id ORDER BY FIELD(state,3,1,4,5,6,2), modify_time DESC LIMIT ?,?`
  333. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  334. return
  335. }
  336. // ModifyReportClassifyAndReportChapterTypeByCondition
  337. // @Description:
  338. // @author: Roc
  339. // @datetime 2024-06-17 16:12:44
  340. // @param condition string
  341. // @param pars []interface{}
  342. // @param updateStr string
  343. // @param chapterTypeIdMap map[int]int 当前的章节类型ID ---> 继承的章节类型ID
  344. // @param oldClassifyId int
  345. // @param currClassifyId int
  346. // @param currClassifyName string
  347. // @return err error
  348. func ModifyReportClassifyAndReportChapterTypeByCondition(condition string, pars []interface{}, updateStr string, chapterTypeIdMap map[int]int, oldClassifyId, currClassifyId int, currClassifyName string) (err error) {
  349. o := orm.NewOrmUsingDB("rddp")
  350. to, err := o.Begin()
  351. if err != nil {
  352. return
  353. }
  354. defer func() {
  355. if err != nil {
  356. _ = to.Rollback()
  357. } else {
  358. _ = to.Commit()
  359. }
  360. }()
  361. if condition == `` {
  362. err = errors.New("condition不能为空")
  363. return
  364. }
  365. // 修改报告的所属分类
  366. sql := `UPDATE report as a SET ` + updateStr + ` WHERE 1=1 `
  367. if condition != "" {
  368. sql += condition
  369. }
  370. _, err = to.Raw(sql, pars).Exec()
  371. if err != nil {
  372. return
  373. }
  374. // 修改历史报告中的章节分类归属
  375. sql = `UPDATE report_chapter set classify_id_first=?,classify_name_first=? where classify_id_first = ?`
  376. _, err = to.Raw(sql, currClassifyId, currClassifyName, oldClassifyId).Exec()
  377. if err != nil {
  378. return
  379. }
  380. for currTypeId, oldTypeId := range chapterTypeIdMap {
  381. // 没有章节类型的不处理
  382. if oldTypeId == 0 {
  383. continue
  384. }
  385. tmpSql := `UPDATE report_chapter set type_id=? where type_id = ?`
  386. _, err = to.Raw(tmpSql, currTypeId, oldTypeId).Exec()
  387. if err != nil {
  388. return
  389. }
  390. }
  391. return
  392. }
  393. // EditLayoutImgReq
  394. // @Description: 版图设置请求
  395. type EditLayoutImgReq struct {
  396. ReportId int64 `description:"报告id"`
  397. HeadImg string `description:"报告头图地址"`
  398. EndImg string `description:"报告尾图地址"`
  399. CanvasColor string `description:"画布颜色"`
  400. HeadResourceId int `description:"版头资源ID"`
  401. EndResourceId int `description:"版尾资源ID"`
  402. }