report_v2.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. // EditChapterBaseInfoAndPermission
  120. // @Description: 修改报告章节的基础信息、授权用户权限、品种权限
  121. // @author: Roc
  122. // @datetime 2024-06-05 11:45:04
  123. // @param reportChapterInfo *ReportChapter
  124. // @param updateCols []string
  125. // @param addReportChapterGrantList []report.ReportChapterGrant
  126. // @param addChapterPermissionMap []*report.ReportChapterPermissionMapping
  127. // @param delReportChapterGrantIdList []int
  128. // @param delChapterPermissionMappingIdList []int
  129. // @return err error
  130. func EditChapterBaseInfoAndPermission(reportChapterInfo *ReportChapter, updateCols []string, addReportChapterGrantList []*report.ReportChapterGrant, addChapterPermissionMap []*report.ReportChapterPermissionMapping, delReportChapterGrantIdList, delChapterPermissionMappingIdList []int) (err error) {
  131. o := orm.NewOrmUsingDB("rddp")
  132. to, err := o.Begin()
  133. if err != nil {
  134. return
  135. }
  136. defer func() {
  137. if err != nil {
  138. _ = to.Rollback()
  139. } else {
  140. _ = to.Commit()
  141. }
  142. }()
  143. // 变更报告章节信息
  144. if len(updateCols) > 0 {
  145. _, err = to.Update(reportChapterInfo, updateCols...)
  146. if err != nil {
  147. return
  148. }
  149. }
  150. // 新增报告章节授权用户
  151. if len(addReportChapterGrantList) > 0 {
  152. _, err = to.InsertMulti(500, addReportChapterGrantList)
  153. if err != nil {
  154. return
  155. }
  156. }
  157. // 删除报告章节授权用户
  158. delNum := len(delReportChapterGrantIdList)
  159. if delNum > 0 {
  160. sql := `DELETE FROM report_chapter_grant WHERE grant_id IN (` + utils.GetOrmInReplace(delNum) + `)`
  161. _, err = to.Raw(sql, delReportChapterGrantIdList).Exec()
  162. if err != nil {
  163. return
  164. }
  165. }
  166. // 新增报告章节的品种配置
  167. if len(addChapterPermissionMap) > 0 {
  168. _, err = to.InsertMulti(500, addChapterPermissionMap)
  169. if err != nil {
  170. return
  171. }
  172. }
  173. // 删除报告章节的品种配置
  174. delNum = len(delChapterPermissionMappingIdList)
  175. if delNum > 0 {
  176. sql := `DELETE FROM report_chapter_permission_mapping WHERE report_chapter_permission_mapping_id IN (` + utils.GetOrmInReplace(delNum) + `)`
  177. _, err = to.Raw(sql, delChapterPermissionMappingIdList).Exec()
  178. if err != nil {
  179. return
  180. }
  181. }
  182. return
  183. }
  184. // DelChapterAndPermission
  185. // @Description: 删除报告章节、授权用户权限、品种权限
  186. // @author: Roc
  187. // @datetime 2024-06-06 17:25:47
  188. // @param reportInfo *Report
  189. // @param updateReportCols []string
  190. // @param reportChapterInfo *ReportChapter
  191. // @return err error
  192. func DelChapterAndPermission(reportInfo *Report, updateReportCols []string, reportChapterInfo *ReportChapter) (err error) {
  193. o := orm.NewOrmUsingDB("rddp")
  194. to, err := o.Begin()
  195. if err != nil {
  196. return
  197. }
  198. defer func() {
  199. if err != nil {
  200. _ = to.Rollback()
  201. } else {
  202. _ = to.Commit()
  203. }
  204. }()
  205. // 变更报告信息
  206. if len(updateReportCols) > 0 {
  207. _, err = to.Update(reportInfo, updateReportCols...)
  208. if err != nil {
  209. return
  210. }
  211. }
  212. // 删除报告对应章节
  213. {
  214. _, err = to.Delete(reportChapterInfo)
  215. if err != nil {
  216. return
  217. }
  218. }
  219. // 删除报告章节的授权用户权限
  220. {
  221. sql := `DELETE FROM report_chapter_grant WHERE report_chapter_id = ? `
  222. _, err = to.Raw(sql, reportChapterInfo.ReportChapterId).Exec()
  223. if err != nil {
  224. return
  225. }
  226. }
  227. // 删除报告章节的品种配置
  228. {
  229. sql := `DELETE FROM report_chapter_permission_mapping WHERE report_chapter_id = ? `
  230. _, err = to.Raw(sql, reportChapterInfo.ReportChapterId).Exec()
  231. if err != nil {
  232. return
  233. }
  234. }
  235. return
  236. }
  237. // GetReportListCountByAuthorized
  238. // @Description: 获取有权限的报告列表的报告数量
  239. // @author: Roc
  240. // @datetime 2024-05-30 15:14:01
  241. // @param condition string
  242. // @param pars []interface{}
  243. // @return count int
  244. // @return err error
  245. func GetReportListCountByAuthorized(condition string, pars []interface{}) (count int, err error) {
  246. o := orm.NewOrmUsingDB("rddp")
  247. sql := `SELECT COUNT(1) AS count FROM report as a
  248. WHERE 1=1 `
  249. if condition != "" {
  250. sql += condition
  251. }
  252. err = o.Raw(sql, pars).QueryRow(&count)
  253. return
  254. }
  255. // GetReportListByAuthorized
  256. // @Description: 获取有权限的报告列表的数据
  257. // @author: Roc
  258. // @datetime 2024-05-30 15:15:07
  259. // @param condition string
  260. // @param pars []interface{}
  261. // @param startSize int
  262. // @param pageSize int
  263. // @return items []*ReportList
  264. // @return err error
  265. func GetReportListByAuthorized(condition string, pars []interface{}, startSize, pageSize int) (items []*ReportList, err error) {
  266. o := orm.NewOrmUsingDB("rddp")
  267. 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 FROM report as a WHERE 1=1 `
  268. if condition != "" {
  269. sql += condition
  270. }
  271. // 排序:1:未发布;2:已发布;3-待提交;4-待审批;5-已驳回;6-已通过
  272. sql += ` GROUP BY a.id ORDER BY FIELD(state,3,1,4,5,6,2), modify_time DESC LIMIT ?,?`
  273. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  274. return
  275. }