report_v2.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. package models
  2. import (
  3. "eta/eta_api/models/report"
  4. "eta/eta_api/utils"
  5. "github.com/beego/beego/v2/client/orm"
  6. )
  7. // AddReportAndChapter
  8. // @Description: 新增报告及章节
  9. // @author: Roc
  10. // @datetime 2024-06-06 17:08:34
  11. // @param reportItem *Report
  12. // @param allGrantUserList []*report.ReportGrant
  13. // @param addReportChapterList []AddReportChapter
  14. // @return reportId int64
  15. // @return err error
  16. func AddReportAndChapter(reportItem *Report, allGrantUserList []*report.ReportGrant, addReportChapterList []AddReportChapter) (reportId int64, err error) {
  17. o := orm.NewOrmUsingDB("rddp")
  18. to, err := o.Begin()
  19. if err != nil {
  20. return
  21. }
  22. defer func() {
  23. if err != nil {
  24. _ = to.Rollback()
  25. } else {
  26. _ = to.Commit()
  27. }
  28. }()
  29. // 新增报告
  30. reportId, err = to.Insert(reportItem)
  31. if err != nil {
  32. return
  33. }
  34. reportItem.Id = int(reportId)
  35. // 新增报告授权
  36. if len(allGrantUserList) > 0 {
  37. for _, v := range allGrantUserList {
  38. v.ReportId = reportItem.Id
  39. }
  40. _, err = to.InsertMulti(500, allGrantUserList)
  41. if err != nil {
  42. return
  43. }
  44. }
  45. // 新增报告章节
  46. if len(addReportChapterList) > 0 {
  47. for _, addReportChapter := range addReportChapterList {
  48. // 新增章节
  49. chapterItem := addReportChapter.ReportChapter
  50. chapterItem.ReportId = int(reportId)
  51. cpId, tmpErr := to.Insert(chapterItem)
  52. if tmpErr != nil {
  53. err = tmpErr
  54. return
  55. }
  56. chapterItem.ReportChapterId = int(cpId)
  57. // 新增章节授权
  58. if len(addReportChapter.GrantList) > 0 {
  59. grantList := addReportChapter.GrantList
  60. for _, v := range grantList {
  61. v.ReportChapterId = chapterItem.ReportChapterId
  62. }
  63. _, err = to.InsertMulti(500, grantList)
  64. if err != nil {
  65. return
  66. }
  67. }
  68. }
  69. }
  70. return
  71. }
  72. // EditReportAndPermission
  73. // @Description: 修改报告的基础信息、授权用户权限
  74. // @author: Roc
  75. // @datetime 2024-06-06 17:11:12
  76. // @param reportInfo *Report
  77. // @param updateCols []string
  78. // @param addReportGrantList []*report.ReportGrant
  79. // @param delReportGrantIdList []int
  80. // @return err error
  81. func EditReportAndPermission(reportInfo *Report, updateCols []string, addReportGrantList []*report.ReportGrant, delReportGrantIdList []int) (err error) {
  82. o := orm.NewOrmUsingDB("rddp")
  83. to, err := o.Begin()
  84. if err != nil {
  85. return
  86. }
  87. defer func() {
  88. if err != nil {
  89. _ = to.Rollback()
  90. } else {
  91. _ = to.Commit()
  92. }
  93. }()
  94. // 变更报告章节信息
  95. if len(updateCols) > 0 {
  96. _, err = to.Update(reportInfo, updateCols...)
  97. if err != nil {
  98. return
  99. }
  100. }
  101. // 新增报告授权用户
  102. if len(addReportGrantList) > 0 {
  103. _, err = to.InsertMulti(500, addReportGrantList)
  104. if err != nil {
  105. return
  106. }
  107. }
  108. // 删除报告授权用户
  109. delNum := len(delReportGrantIdList)
  110. if delNum > 0 {
  111. sql := `DELETE FROM report_grant WHERE grant_id IN (` + utils.GetOrmInReplace(delNum) + `)`
  112. _, err = to.Raw(sql, delReportGrantIdList).Exec()
  113. if err != nil {
  114. return
  115. }
  116. }
  117. return
  118. }
  119. // AddChapterBaseInfoAndPermission
  120. // @Description: 新增报告章节的基础信息、授权用户权限
  121. // @author: Roc
  122. // @datetime 2024-06-11 15:33:50
  123. // @param reportChapterInfo *ReportChapter
  124. // @param addReportChapterGrantList []*report.ReportChapterGrant
  125. // @param addChapterPermissionMap []*report.ReportChapterPermissionMapping
  126. // @return err error
  127. func AddChapterBaseInfoAndPermission(reportChapterInfo *ReportChapter, addReportChapterGrantList []*report.ReportChapterGrant, addChapterPermissionMap []*report.ReportChapterPermissionMapping) (err error) {
  128. o := orm.NewOrmUsingDB("rddp")
  129. to, err := o.Begin()
  130. if err != nil {
  131. return
  132. }
  133. defer func() {
  134. if err != nil {
  135. _ = to.Rollback()
  136. } else {
  137. _ = to.Commit()
  138. }
  139. }()
  140. lastId, err := to.Insert(reportChapterInfo)
  141. if err != nil {
  142. return
  143. }
  144. reportChapterInfo.ReportChapterId = int(lastId)
  145. // 新增报告章节授权用户
  146. if len(addReportChapterGrantList) > 0 {
  147. for k, _ := range addReportChapterGrantList {
  148. addReportChapterGrantList[k].ReportChapterId = reportChapterInfo.ReportChapterId
  149. }
  150. _, err = to.InsertMulti(500, addReportChapterGrantList)
  151. if err != nil {
  152. return
  153. }
  154. }
  155. // 新增报告章节的品种配置
  156. if len(addChapterPermissionMap) > 0 {
  157. for k, _ := range addChapterPermissionMap {
  158. addChapterPermissionMap[k].ReportChapterId = reportChapterInfo.ReportChapterId
  159. }
  160. _, err = to.InsertMulti(500, addChapterPermissionMap)
  161. if err != nil {
  162. return
  163. }
  164. }
  165. return
  166. }
  167. // EditChapterBaseInfoAndPermission
  168. // @Description: 修改报告章节的基础信息、授权用户权限、品种权限
  169. // @author: Roc
  170. // @datetime 2024-06-05 11:45:04
  171. // @param reportChapterInfo *ReportChapter
  172. // @param updateCols []string
  173. // @param addReportChapterGrantList []report.ReportChapterGrant
  174. // @param addChapterPermissionMap []*report.ReportChapterPermissionMapping
  175. // @param delReportChapterGrantIdList []int
  176. // @param delChapterPermissionMappingIdList []int
  177. // @return err error
  178. func EditChapterBaseInfoAndPermission(reportChapterInfo *ReportChapter, updateCols []string, addReportChapterGrantList []*report.ReportChapterGrant, addChapterPermissionMap []*report.ReportChapterPermissionMapping, delReportChapterGrantIdList, delChapterPermissionMappingIdList []int) (err error) {
  179. o := orm.NewOrmUsingDB("rddp")
  180. to, err := o.Begin()
  181. if err != nil {
  182. return
  183. }
  184. defer func() {
  185. if err != nil {
  186. _ = to.Rollback()
  187. } else {
  188. _ = to.Commit()
  189. }
  190. }()
  191. // 变更报告章节信息
  192. if len(updateCols) > 0 {
  193. _, err = to.Update(reportChapterInfo, updateCols...)
  194. if err != nil {
  195. return
  196. }
  197. }
  198. // 新增报告章节授权用户
  199. if len(addReportChapterGrantList) > 0 {
  200. _, err = to.InsertMulti(500, addReportChapterGrantList)
  201. if err != nil {
  202. return
  203. }
  204. }
  205. // 删除报告章节授权用户
  206. delNum := len(delReportChapterGrantIdList)
  207. if delNum > 0 {
  208. sql := `DELETE FROM report_chapter_grant WHERE grant_id IN (` + utils.GetOrmInReplace(delNum) + `)`
  209. _, err = to.Raw(sql, delReportChapterGrantIdList).Exec()
  210. if err != nil {
  211. return
  212. }
  213. }
  214. // 新增报告章节的品种配置
  215. if len(addChapterPermissionMap) > 0 {
  216. _, err = to.InsertMulti(500, addChapterPermissionMap)
  217. if err != nil {
  218. return
  219. }
  220. }
  221. // 删除报告章节的品种配置
  222. delNum = len(delChapterPermissionMappingIdList)
  223. if delNum > 0 {
  224. sql := `DELETE FROM report_chapter_permission_mapping WHERE report_chapter_permission_mapping_id IN (` + utils.GetOrmInReplace(delNum) + `)`
  225. _, err = to.Raw(sql, delChapterPermissionMappingIdList).Exec()
  226. if err != nil {
  227. return
  228. }
  229. }
  230. return
  231. }
  232. // DelChapterAndPermission
  233. // @Description: 删除报告章节、授权用户权限、品种权限
  234. // @author: Roc
  235. // @datetime 2024-06-06 17:25:47
  236. // @param reportInfo *Report
  237. // @param updateReportCols []string
  238. // @param reportChapterInfo *ReportChapter
  239. // @return err error
  240. func DelChapterAndPermission(reportInfo *Report, updateReportCols []string, reportChapterInfo *ReportChapter) (err error) {
  241. o := orm.NewOrmUsingDB("rddp")
  242. to, err := o.Begin()
  243. if err != nil {
  244. return
  245. }
  246. defer func() {
  247. if err != nil {
  248. _ = to.Rollback()
  249. } else {
  250. _ = to.Commit()
  251. }
  252. }()
  253. // 变更报告信息
  254. if len(updateReportCols) > 0 {
  255. _, err = to.Update(reportInfo, updateReportCols...)
  256. if err != nil {
  257. return
  258. }
  259. }
  260. // 删除报告对应章节
  261. {
  262. _, err = to.Delete(reportChapterInfo)
  263. if err != nil {
  264. return
  265. }
  266. }
  267. // 删除报告章节的授权用户权限
  268. {
  269. sql := `DELETE FROM report_chapter_grant WHERE report_chapter_id = ? `
  270. _, err = to.Raw(sql, reportChapterInfo.ReportChapterId).Exec()
  271. if err != nil {
  272. return
  273. }
  274. }
  275. // 删除报告章节的品种配置
  276. {
  277. sql := `DELETE FROM report_chapter_permission_mapping WHERE report_chapter_id = ? `
  278. _, err = to.Raw(sql, reportChapterInfo.ReportChapterId).Exec()
  279. if err != nil {
  280. return
  281. }
  282. }
  283. return
  284. }
  285. // GetReportListCountByAuthorized
  286. // @Description: 获取有权限的报告列表的报告数量
  287. // @author: Roc
  288. // @datetime 2024-05-30 15:14:01
  289. // @param condition string
  290. // @param pars []interface{}
  291. // @return count int
  292. // @return err error
  293. func GetReportListCountByAuthorized(condition string, pars []interface{}) (count int, err error) {
  294. o := orm.NewOrmUsingDB("rddp")
  295. sql := `SELECT COUNT(1) AS count FROM report as a
  296. WHERE 1=1 `
  297. if condition != "" {
  298. sql += condition
  299. }
  300. err = o.Raw(sql, pars).QueryRow(&count)
  301. return
  302. }
  303. // GetReportListByAuthorized
  304. // @Description: 获取有权限的报告列表的数据
  305. // @author: Roc
  306. // @datetime 2024-05-30 15:15:07
  307. // @param condition string
  308. // @param pars []interface{}
  309. // @param startSize int
  310. // @param pageSize int
  311. // @return items []*ReportList
  312. // @return err error
  313. func GetReportListByAuthorized(condition string, pars []interface{}, startSize, pageSize int) (items []*ReportList, err error) {
  314. o := orm.NewOrmUsingDB("rddp")
  315. 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 `
  316. if condition != "" {
  317. sql += condition
  318. }
  319. // 排序:1:未发布;2:已发布;3-待提交;4-待审批;5-已驳回;6-已通过
  320. sql += ` GROUP BY a.id ORDER BY FIELD(state,3,1,4,5,6,2), modify_time DESC LIMIT ?,?`
  321. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  322. return
  323. }