report_v2.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. func AddReportAndChapter(reportItem *Report, allGrantUserList []*report.ReportGrant, addReportChapterList []AddReportChapter) (reportId int64, err error) {
  9. to := global.DmSQL["rddp"].Begin()
  10. defer func() {
  11. if err != nil {
  12. _ = to.Rollback()
  13. } else {
  14. _ = to.Commit()
  15. }
  16. }()
  17. err = to.Create(reportItem).Error
  18. reportId = int64(reportItem.Id)
  19. if err != nil {
  20. return
  21. }
  22. reportItem.Id = int(reportId)
  23. if len(allGrantUserList) > 0 {
  24. for _, v := range allGrantUserList {
  25. v.ReportId = reportItem.Id
  26. }
  27. err = to.CreateInBatches(allGrantUserList, utils.MultiAddNum).Error
  28. if err != nil {
  29. return
  30. }
  31. }
  32. if len(addReportChapterList) > 0 {
  33. for _, addReportChapter := range addReportChapterList {
  34. chapterItem := addReportChapter.ReportChapter
  35. chapterItem.ReportId = int(reportId)
  36. tmpErr := to.Create(chapterItem).Error
  37. if tmpErr != nil {
  38. err = tmpErr
  39. return
  40. }
  41. cpId := chapterItem.ReportChapterId
  42. chapterItem.ReportChapterId = int(cpId)
  43. if len(addReportChapter.GrantList) > 0 {
  44. grantList := addReportChapter.GrantList
  45. for _, v := range grantList {
  46. v.ReportChapterId = chapterItem.ReportChapterId
  47. }
  48. err = to.CreateInBatches(grantList, utils.MultiAddNum).Error
  49. if err != nil {
  50. return
  51. }
  52. }
  53. if len(addReportChapter.GrantPermissionList) > 0 {
  54. permissionList := addReportChapter.GrantPermissionList
  55. for _, v := range permissionList {
  56. v.ReportChapterId = chapterItem.ReportChapterId
  57. }
  58. err = to.CreateInBatches(permissionList, utils.MultiAddNum).Error
  59. if err != nil {
  60. return
  61. }
  62. }
  63. }
  64. }
  65. return
  66. }
  67. func EditReportAndPermission(reportInfo *Report, updateCols []string, addReportGrantList []*report.ReportGrant, delReportGrantIdList []int) (err error) {
  68. to := global.DmSQL["rddp"].Begin()
  69. defer func() {
  70. if err != nil {
  71. _ = to.Rollback()
  72. } else {
  73. _ = to.Commit()
  74. }
  75. }()
  76. if len(updateCols) > 0 {
  77. err = to.Select(updateCols).Updates(reportInfo).Error
  78. if err != nil {
  79. return
  80. }
  81. }
  82. if len(addReportGrantList) > 0 {
  83. err = to.CreateInBatches(addReportGrantList, utils.MultiAddNum).Error
  84. if err != nil {
  85. return
  86. }
  87. }
  88. delNum := len(delReportGrantIdList)
  89. if delNum > 0 {
  90. sql := `DELETE FROM report_grant WHERE grant_id IN (` + utils.GetOrmInReplace(delNum) + `)`
  91. err = to.Exec(sql, delReportGrantIdList).Error
  92. if err != nil {
  93. return
  94. }
  95. }
  96. return
  97. }
  98. func AddChapterBaseInfoAndPermission(reportChapterInfo *ReportChapter, addReportChapterGrantList []*report.ReportChapterGrant, addChapterPermissionMap []*report.ReportChapterPermissionMapping) (err error) {
  99. to := global.DmSQL["rddp"].Begin()
  100. defer func() {
  101. if err != nil {
  102. _ = to.Rollback()
  103. } else {
  104. _ = to.Commit()
  105. }
  106. }()
  107. err = to.Create(reportChapterInfo).Error
  108. if err != nil {
  109. return
  110. }
  111. lastId := reportChapterInfo.ReportChapterId
  112. reportChapterInfo.ReportChapterId = int(lastId)
  113. if len(addReportChapterGrantList) > 0 {
  114. for k, _ := range addReportChapterGrantList {
  115. addReportChapterGrantList[k].ReportChapterId = reportChapterInfo.ReportChapterId
  116. }
  117. err = to.CreateInBatches(addReportChapterGrantList, utils.MultiAddNum).Error
  118. if err != nil {
  119. return
  120. }
  121. }
  122. if len(addChapterPermissionMap) > 0 {
  123. for k, _ := range addChapterPermissionMap {
  124. addChapterPermissionMap[k].ReportChapterId = reportChapterInfo.ReportChapterId
  125. }
  126. err = to.CreateInBatches(addChapterPermissionMap, utils.MultiAddNum).Error
  127. if err != nil {
  128. return
  129. }
  130. }
  131. return
  132. }
  133. func EditChapterBaseInfoAndPermission(reportInfo *Report, reportChapterInfo *ReportChapter, updateCols []string, addReportChapterGrantList []*report.ReportChapterGrant, addChapterPermissionMap []*report.ReportChapterPermissionMapping, delReportChapterGrantIdList, delChapterPermissionMappingIdList []int) (err error) {
  134. to := global.DmSQL["rddp"].Begin()
  135. defer func() {
  136. if err != nil {
  137. _ = to.Rollback()
  138. } else {
  139. _ = to.Commit()
  140. }
  141. }()
  142. {
  143. err = to.Select("LastModifyAdminId", "LastModifyAdminName", "ModifyTime").Updates(reportInfo).Error
  144. if err != nil {
  145. return
  146. }
  147. }
  148. if len(updateCols) > 0 {
  149. err = to.Select(updateCols).Updates(reportChapterInfo).Error
  150. if err != nil {
  151. return
  152. }
  153. }
  154. if len(addReportChapterGrantList) > 0 {
  155. err = to.CreateInBatches(addReportChapterGrantList, utils.MultiAddNum).Error
  156. if err != nil {
  157. return
  158. }
  159. }
  160. delNum := len(delReportChapterGrantIdList)
  161. if delNum > 0 {
  162. sql := `DELETE FROM report_chapter_grant WHERE grant_id IN (` + utils.GetOrmInReplace(delNum) + `)`
  163. err = to.Exec(sql, delReportChapterGrantIdList).Error
  164. if err != nil {
  165. return
  166. }
  167. }
  168. if len(addChapterPermissionMap) > 0 {
  169. err = to.CreateInBatches(addChapterPermissionMap, utils.MultiAddNum).Error
  170. if err != nil {
  171. return
  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.Exec(sql, delChapterPermissionMappingIdList).Error
  178. if err != nil {
  179. return
  180. }
  181. }
  182. return
  183. }
  184. func DelChapterAndPermission(reportInfo *Report, updateReportCols []string, reportChapterInfo *ReportChapter) (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. if len(updateReportCols) > 0 {
  194. err = to.Select(updateReportCols).Updates(reportInfo).Error
  195. if err != nil {
  196. return
  197. }
  198. }
  199. {
  200. err = to.Delete(reportChapterInfo).Error
  201. if err != nil {
  202. return
  203. }
  204. }
  205. {
  206. sql := `DELETE FROM report_chapter_grant WHERE report_chapter_id = ? `
  207. err = to.Exec(sql, reportChapterInfo.ReportChapterId).Error
  208. if err != nil {
  209. return
  210. }
  211. }
  212. {
  213. sql := `DELETE FROM report_chapter_permission_mapping WHERE report_chapter_id = ? `
  214. err = to.Exec(sql, reportChapterInfo.ReportChapterId).Error
  215. if err != nil {
  216. return
  217. }
  218. }
  219. return
  220. }
  221. func GetReportListCountByAuthorized(condition string, pars []interface{}) (count int, err error) {
  222. sql := `SELECT COUNT(1) AS count FROM report as a
  223. WHERE 1=1 `
  224. if condition != "" {
  225. sql += condition
  226. }
  227. err = global.DmSQL["rddp"].Raw(sql, pars...).Scan(&count).Error
  228. return
  229. }
  230. func GetReportListByAuthorized(condition string, pars []interface{}, startSize, pageSize int) (items []*ReportList, err error) {
  231. 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 `
  232. if condition != "" {
  233. sql += condition
  234. }
  235. 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
  236. ORDER BY report_create_time DESC LIMIT ?,?`
  237. pars = append(pars, startSize)
  238. pars = append(pars, pageSize)
  239. err = global.DmSQL["rddp"].Raw(sql, pars...).Find(&items).Error
  240. return
  241. }
  242. func ModifyReportClassifyAndReportChapterTypeByCondition(condition string, pars []interface{}, updateStr string, chapterTypeIdMap map[int]int, oldClassifyId, currClassifyId int, currClassifyName string) (err error) {
  243. to := global.DmSQL["rddp"].Begin()
  244. defer func() {
  245. if err != nil {
  246. _ = to.Rollback()
  247. } else {
  248. _ = to.Commit()
  249. }
  250. }()
  251. if condition == `` {
  252. err = errors.New("condition不能为空")
  253. return
  254. }
  255. sql := `UPDATE report as a SET ` + updateStr + ` WHERE 1=1 `
  256. if condition != "" {
  257. sql += condition
  258. }
  259. err = to.Exec(sql, pars...).Error
  260. if err != nil {
  261. return
  262. }
  263. sql = `UPDATE report_chapter set classify_id_first=?,classify_name_first=? where classify_id_first = ?`
  264. err = to.Exec(sql, currClassifyId, currClassifyName, oldClassifyId).Error
  265. if err != nil {
  266. return
  267. }
  268. for currTypeId, oldTypeId := range chapterTypeIdMap {
  269. if oldTypeId == 0 {
  270. continue
  271. }
  272. tmpSql := `UPDATE report_chapter set type_id=? where type_id = ?`
  273. err = to.Exec(tmpSql, currTypeId, oldTypeId).Error
  274. if err != nil {
  275. return
  276. }
  277. }
  278. return
  279. }
  280. type EditLayoutImgReq struct {
  281. ReportId int64 `description:"报告id"`
  282. HeadImg string `description:"报告头图地址"`
  283. EndImg string `description:"报告尾图地址"`
  284. CanvasColor string `description:"画布颜色"`
  285. HeadResourceId int `description:"版头资源ID"`
  286. EndResourceId int `description:"版尾资源ID"`
  287. }