report_v2.go 14 KB

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