report_v2.go 11 KB

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