package models import ( "errors" "eta_gn/eta_api/global" "eta_gn/eta_api/models/report" "eta_gn/eta_api/utils" ) func AddReportAndChapter(reportItem *Report, allGrantUserList []*report.ReportGrant, addReportChapterList []AddReportChapter) (reportId int64, err error) { to := global.DmSQL["rddp"].Begin() defer func() { if err != nil { _ = to.Rollback() } else { _ = to.Commit() } }() err = to.Create(reportItem).Error reportId = int64(reportItem.Id) if err != nil { return } reportItem.Id = int(reportId) if len(allGrantUserList) > 0 { for _, v := range allGrantUserList { v.ReportId = reportItem.Id } err = to.CreateInBatches(allGrantUserList, utils.MultiAddNum).Error if err != nil { return } } if len(addReportChapterList) > 0 { for _, addReportChapter := range addReportChapterList { chapterItem := addReportChapter.ReportChapter chapterItem.ReportId = int(reportId) tmpErr := to.Create(chapterItem).Error if tmpErr != nil { err = tmpErr return } cpId := chapterItem.ReportChapterId chapterItem.ReportChapterId = int(cpId) if len(addReportChapter.GrantList) > 0 { grantList := addReportChapter.GrantList for _, v := range grantList { v.ReportChapterId = chapterItem.ReportChapterId } err = to.CreateInBatches(grantList, utils.MultiAddNum).Error if err != nil { return } } if len(addReportChapter.GrantPermissionList) > 0 { permissionList := addReportChapter.GrantPermissionList for _, v := range permissionList { v.ReportChapterId = chapterItem.ReportChapterId } err = to.CreateInBatches(permissionList, utils.MultiAddNum).Error if err != nil { return } } } } return } func EditReportAndPermission(reportInfo *Report, updateCols []string, addReportGrantList []*report.ReportGrant, delReportGrantIdList []int) (err error) { to := global.DmSQL["rddp"].Begin() defer func() { if err != nil { _ = to.Rollback() } else { _ = to.Commit() } }() if len(updateCols) > 0 { err = to.Select(updateCols).Updates(reportInfo).Error if err != nil { return } } if len(addReportGrantList) > 0 { err = to.CreateInBatches(addReportGrantList, utils.MultiAddNum).Error if err != nil { return } } delNum := len(delReportGrantIdList) if delNum > 0 { sql := `DELETE FROM report_grant WHERE grant_id IN (` + utils.GetOrmInReplace(delNum) + `)` err = to.Exec(sql, delReportGrantIdList).Error if err != nil { return } } return } func AddChapterBaseInfoAndPermission(reportChapterInfo *ReportChapter, addReportChapterGrantList []*report.ReportChapterGrant, addChapterPermissionMap []*report.ReportChapterPermissionMapping) (err error) { to := global.DmSQL["rddp"].Begin() defer func() { if err != nil { _ = to.Rollback() } else { _ = to.Commit() } }() err = to.Create(reportChapterInfo).Error if err != nil { return } lastId := reportChapterInfo.ReportChapterId reportChapterInfo.ReportChapterId = int(lastId) if len(addReportChapterGrantList) > 0 { for k, _ := range addReportChapterGrantList { addReportChapterGrantList[k].ReportChapterId = reportChapterInfo.ReportChapterId } err = to.CreateInBatches(addReportChapterGrantList, utils.MultiAddNum).Error if err != nil { return } } if len(addChapterPermissionMap) > 0 { for k, _ := range addChapterPermissionMap { addChapterPermissionMap[k].ReportChapterId = reportChapterInfo.ReportChapterId } err = to.CreateInBatches(addChapterPermissionMap, utils.MultiAddNum).Error if err != nil { return } } return } func EditChapterBaseInfoAndPermission(reportInfo *Report, reportChapterInfo *ReportChapter, updateCols []string, addReportChapterGrantList []*report.ReportChapterGrant, addChapterPermissionMap []*report.ReportChapterPermissionMapping, delReportChapterGrantIdList, delChapterPermissionMappingIdList []int) (err error) { to := global.DmSQL["rddp"].Begin() defer func() { if err != nil { _ = to.Rollback() } else { _ = to.Commit() } }() { err = to.Select("LastModifyAdminId", "LastModifyAdminName", "ModifyTime").Updates(reportInfo).Error if err != nil { return } } if len(updateCols) > 0 { err = to.Select(updateCols).Updates(reportChapterInfo).Error if err != nil { return } } if len(addReportChapterGrantList) > 0 { err = to.CreateInBatches(addReportChapterGrantList, utils.MultiAddNum).Error if err != nil { return } } delNum := len(delReportChapterGrantIdList) if delNum > 0 { sql := `DELETE FROM report_chapter_grant WHERE grant_id IN (` + utils.GetOrmInReplace(delNum) + `)` err = to.Exec(sql, delReportChapterGrantIdList).Error if err != nil { return } } if len(addChapterPermissionMap) > 0 { err = to.CreateInBatches(addChapterPermissionMap, utils.MultiAddNum).Error if err != nil { return } } delNum = len(delChapterPermissionMappingIdList) if delNum > 0 { sql := `DELETE FROM report_chapter_permission_mapping WHERE report_chapter_permission_mapping_id IN (` + utils.GetOrmInReplace(delNum) + `)` err = to.Exec(sql, delChapterPermissionMappingIdList).Error if err != nil { return } } return } func DelChapterAndPermission(reportInfo *Report, updateReportCols []string, reportChapterInfo *ReportChapter) (err error) { to := global.DmSQL["rddp"].Begin() defer func() { if err != nil { _ = to.Rollback() } else { _ = to.Commit() } }() if len(updateReportCols) > 0 { err = to.Select(updateReportCols).Updates(reportInfo).Error if err != nil { return } } { err = to.Delete(reportChapterInfo).Error if err != nil { return } } { sql := `DELETE FROM report_chapter_grant WHERE report_chapter_id = ? ` err = to.Exec(sql, reportChapterInfo.ReportChapterId).Error if err != nil { return } } { sql := `DELETE FROM report_chapter_permission_mapping WHERE report_chapter_id = ? ` err = to.Exec(sql, reportChapterInfo.ReportChapterId).Error if err != nil { return } } return } func GetReportListCountByAuthorized(condition string, pars []interface{}) (count int, err error) { sql := `SELECT COUNT(1) AS count FROM report as a WHERE 1=1 ` if condition != "" { sql += condition } err = global.DmSQL["rddp"].Raw(sql, pars...).Scan(&count).Error return } func GetReportListByAuthorized(condition string, pars []interface{}, startSize, pageSize int) (items []*ReportList, err error) { 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 ` if condition != "" { sql += condition } 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 ORDER BY report_create_time DESC LIMIT ?,?` pars = append(pars, startSize) pars = append(pars, pageSize) err = global.DmSQL["rddp"].Raw(sql, pars...).Find(&items).Error return } func ModifyReportClassifyAndReportChapterTypeByCondition(condition string, pars []interface{}, updateStr string, chapterTypeIdMap map[int]int, oldClassifyId, currClassifyId int, currClassifyName string) (err error) { to := global.DmSQL["rddp"].Begin() defer func() { if err != nil { _ = to.Rollback() } else { _ = to.Commit() } }() if condition == `` { err = errors.New("condition不能为空") return } sql := `UPDATE report as a SET ` + updateStr + ` WHERE 1=1 ` if condition != "" { sql += condition } err = to.Exec(sql, pars...).Error if err != nil { return } sql = `UPDATE report_chapter set classify_id_first=?,classify_name_first=? where classify_id_first = ?` err = to.Exec(sql, currClassifyId, currClassifyName, oldClassifyId).Error if err != nil { return } for currTypeId, oldTypeId := range chapterTypeIdMap { if oldTypeId == 0 { continue } tmpSql := `UPDATE report_chapter set type_id=? where type_id = ?` err = to.Exec(tmpSql, currTypeId, oldTypeId).Error if err != nil { return } } return } type EditLayoutImgReq struct { ReportId int64 `description:"报告id"` HeadImg string `description:"报告头图地址"` EndImg string `description:"报告尾图地址"` CanvasColor string `description:"画布颜色"` HeadResourceId int `description:"版头资源ID"` EndResourceId int `description:"版尾资源ID"` }