report_v2.go 12 KB

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