report_v2.go 14 KB

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