excel_op.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. package excel
  2. import (
  3. "errors"
  4. "eta/eta_api/models/data_manage"
  5. excelModel "eta/eta_api/models/data_manage/excel"
  6. "eta/eta_api/models/system"
  7. "eta/eta_api/services"
  8. "eta/eta_api/services/alarm_msg"
  9. "eta/eta_api/services/data/data_manage_permission"
  10. excel "eta/eta_api/services/excel"
  11. "eta/eta_api/utils"
  12. "fmt"
  13. "os"
  14. "strconv"
  15. "time"
  16. )
  17. // Delete excel删除
  18. func Delete(excelInfo *excelModel.ExcelInfo, sysUser *system.Admin) (err error, errMsg string, isSendEmail bool) {
  19. isSendEmail = true
  20. // 操作权限校验
  21. {
  22. // 数据权限
  23. haveOperaAuth, tmpErr := data_manage_permission.CheckExcelPermissionByExcelInfoId(excelInfo.ExcelInfoId, excelInfo.ExcelClassifyId, excelInfo.IsJoinPermission, sysUser.AdminId)
  24. if err != nil {
  25. errMsg = "获取ETA表格权限失败"
  26. err = errors.New("获取ETA表格权限失败,Err:" + tmpErr.Error())
  27. return
  28. }
  29. button := GetExcelInfoOpButton(sysUser, excelInfo.SysUserId, excelInfo.Source, haveOperaAuth)
  30. if !button.DeleteButton {
  31. errMsg = "无操作权限"
  32. err = errors.New(errMsg)
  33. isSendEmail = false
  34. return
  35. }
  36. }
  37. // 自定义分析,需要做这个指标关联的校验
  38. if excelInfo.Source == utils.CUSTOM_ANALYSIS_TABLE {
  39. list, tmpErr := excelModel.GetExcelEdbMappingByExcelInfoId(excelInfo.ExcelInfoId)
  40. if tmpErr != nil {
  41. errMsg = `获取关联的指标信息失败`
  42. err = tmpErr
  43. return
  44. }
  45. if len(list) > 0 {
  46. errMsg = "已关联指标,不可删除!"
  47. err = errors.New(errMsg)
  48. isSendEmail = false
  49. return
  50. }
  51. } else if excelInfo.Source == utils.BALANCE_TABLE {
  52. // 如果父级删除是否删除子表和静态表,同时删除图表和图表数据
  53. excelIds := make([]int, 0)
  54. //查询动态表所有的子表,并复制为静态表
  55. condition := " AND source=?"
  56. var pars []interface{}
  57. pars = append(pars, utils.BALANCE_TABLE)
  58. excelIds = append(excelIds, excelInfo.ExcelInfoId)
  59. if excelInfo.ParentId == 0 {
  60. if excelInfo.BalanceType == 0 {
  61. condition += " AND (parent_id = ? or rel_excel_info_id=?)"
  62. pars = append(pars, excelInfo.ExcelInfoId, excelInfo.ExcelInfoId)
  63. } else if excelInfo.BalanceType == 1 {
  64. condition += " AND (parent_id = ?)"
  65. pars = append(pars, excelInfo.ExcelInfoId)
  66. }
  67. excelList, e := excelModel.GetNoContentExcelInfoListByConditionNoPage(condition, pars)
  68. if e != nil {
  69. err = fmt.Errorf("获取子表失败 %s", err.Error())
  70. errMsg = "获取子表失败"
  71. return
  72. }
  73. for _, v := range excelList {
  74. excelIds = append(excelIds, v.ExcelInfoId)
  75. }
  76. }
  77. // 相关联指标
  78. mappingListTmp, e := excelModel.GetExcelChartEdbMappingByExcelInfoIds(excelIds)
  79. if e != nil {
  80. errMsg = "获取失败"
  81. err = fmt.Errorf(" 获取图表,指标信息失败 Err:%s", e.Error())
  82. return
  83. }
  84. charInfoIds := make([]int, 0)
  85. for _, v := range mappingListTmp {
  86. charInfoIds = append(charInfoIds, v.ChartInfoId)
  87. }
  88. err = excelModel.DeleteExcelChartEdbAndData(excelIds, charInfoIds)
  89. if err != nil {
  90. errMsg = "删除表格失败"
  91. err = fmt.Errorf("删除图表,指标信息失败 Err:%s", err.Error())
  92. return
  93. }
  94. _ = data_manage.DeleteEdbRelationByObjectIds(excelIds, utils.EDB_RELATION_TABLE)
  95. return
  96. }
  97. // 标记删除
  98. excelInfo.IsDelete = 1
  99. excelInfo.ModifyTime = time.Now()
  100. err = excelInfo.Update([]string{"IsDelete", "ModifyTime"})
  101. // 删除指标引用
  102. _ = data_manage.DeleteEdbRelationByObjectIds([]int{excelInfo.ExcelInfoId}, utils.EDB_RELATION_TABLE)
  103. return
  104. }
  105. // Copy 复制excel
  106. func Copy(oldExcelInfo *excelModel.ExcelInfo, excelClassifyId int, excelName string, sysUser *system.Admin) (excelInfo *excelModel.ExcelInfo, err error, errMsg string, isSendEmail bool) {
  107. isSendEmail = true
  108. excelName = utils.TrimLRStr(excelName)
  109. excelClassify, err := excelModel.GetExcelClassifyById(excelClassifyId)
  110. if err != nil {
  111. if err.Error() == utils.ErrNoRow() {
  112. errMsg = "分类不存在"
  113. err = errors.New(errMsg)
  114. isSendEmail = false
  115. return
  116. }
  117. errMsg = "获取分类信息失败"
  118. return
  119. }
  120. if excelClassify == nil {
  121. errMsg = "分类不存在"
  122. err = errors.New(errMsg)
  123. isSendEmail = false
  124. return
  125. }
  126. // 操作权限校验
  127. {
  128. // 数据权限
  129. haveOperaAuth, tmpErr := data_manage_permission.CheckExcelPermissionByExcelInfoId(oldExcelInfo.ExcelInfoId, oldExcelInfo.ExcelClassifyId, oldExcelInfo.IsJoinPermission, sysUser.AdminId)
  130. if err != nil {
  131. errMsg = "获取ETA表格权限失败"
  132. err = errors.New("获取ETA表格权限失败,Err:" + tmpErr.Error())
  133. return
  134. }
  135. button := GetExcelInfoOpButton(sysUser, oldExcelInfo.SysUserId, oldExcelInfo.Source, haveOperaAuth)
  136. if !button.CopyButton {
  137. errMsg = "无操作权限"
  138. err = errors.New(errMsg)
  139. isSendEmail = false
  140. return
  141. }
  142. }
  143. // 检验分类下是否存在该表格名称
  144. {
  145. var condition string
  146. var pars []interface{}
  147. condition += " AND excel_classify_id=? "
  148. pars = append(pars, excelClassifyId)
  149. condition += " AND excel_name=? "
  150. pars = append(pars, excelName)
  151. count, tmpErr := excelModel.GetExcelInfoCountByCondition(condition, pars)
  152. if tmpErr != nil {
  153. errMsg = "判断表格名称是否存在失败"
  154. err = tmpErr
  155. return
  156. }
  157. if count > 0 {
  158. errMsg = "表格名称已存在,请重新填写表格名称"
  159. err = errors.New(errMsg)
  160. isSendEmail = false
  161. return
  162. }
  163. }
  164. // 表格信息
  165. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  166. excelInfo = &excelModel.ExcelInfo{
  167. //ExcelInfoId: 0,
  168. ExcelName: excelName,
  169. Source: oldExcelInfo.Source,
  170. ExcelType: oldExcelInfo.ExcelType,
  171. UniqueCode: utils.MD5(utils.EXCEL_DATA_PREFIX + "_" + timestamp),
  172. ExcelClassifyId: excelClassifyId,
  173. SysUserId: sysUser.AdminId,
  174. SysUserRealName: sysUser.RealName,
  175. Content: oldExcelInfo.Content,
  176. ExcelImage: oldExcelInfo.ExcelImage,
  177. FileUrl: oldExcelInfo.FileUrl,
  178. Sort: 0,
  179. IsDelete: 0,
  180. ModifyTime: time.Now(),
  181. CreateTime: time.Now(),
  182. ExtraConfig: oldExcelInfo.ExtraConfig,
  183. }
  184. // 如果不是自定义分析,那么直接加主表就好了
  185. if excelInfo.Source != utils.CUSTOM_ANALYSIS_TABLE {
  186. // 获取excel与指标的关系表
  187. list, tmpErr := excelModel.GetAllExcelEdbMappingByExcelInfoId(excelInfo.ExcelInfoId)
  188. if tmpErr != nil {
  189. errMsg = "获取失败"
  190. err = tmpErr
  191. return
  192. }
  193. for k, v := range list {
  194. v.ExcelEdbMappingId = 0
  195. v.ExcelInfoId = 0
  196. list[k] = v
  197. }
  198. var childExcel *excelModel.ExcelInfo
  199. err = excelModel.AddExcelInfo(excelInfo, list, childExcel)
  200. if err != nil {
  201. errMsg = "保存失败"
  202. }
  203. return
  204. }
  205. // 自定义分析,需要有额外信息
  206. addSheetList := make([]excelModel.AddExcelSheetParams, 0)
  207. // 获取所有的sheet页
  208. oldSheetItemList, err := excelModel.GetAllSheetList(oldExcelInfo.ExcelInfoId)
  209. if err != nil {
  210. errMsg = `获取sheet页失败`
  211. return
  212. }
  213. // 获取所有的sheet页的sheet数据
  214. sheetCellDataMapList := make(map[int][]*excelModel.ExcelSheetData)
  215. {
  216. dataList, tmpErr := excelModel.GetAllSheetDataListByExcelInfoId(oldExcelInfo.ExcelInfoId)
  217. if tmpErr != nil {
  218. errMsg = `获取sheet页的单元格数据失败`
  219. err = tmpErr
  220. return
  221. }
  222. for _, cellData := range dataList {
  223. sheetDataList, ok := sheetCellDataMapList[cellData.ExcelSheetId]
  224. if !ok {
  225. sheetDataList = make([]*excelModel.ExcelSheetData, 0)
  226. }
  227. sheetCellDataMapList[cellData.ExcelSheetId] = append(sheetDataList, cellData)
  228. }
  229. }
  230. // sheet处理
  231. for _, sheetInfo := range oldSheetItemList {
  232. addSheetItem := excelModel.AddExcelSheetParams{
  233. ExcelSheetId: 0,
  234. ExcelInfoId: 0,
  235. SheetName: sheetInfo.SheetName,
  236. Sort: sheetInfo.Sort,
  237. Config: sheetInfo.Config,
  238. CalcChain: sheetInfo.CalcChain,
  239. }
  240. sheetDataList, ok := sheetCellDataMapList[sheetInfo.ExcelSheetId]
  241. if ok {
  242. for i, sheetData := range sheetDataList {
  243. sheetData.ExcelDataId = 0
  244. sheetData.ExcelSheetId = 0
  245. sheetData.ExcelInfoId = 0
  246. sheetDataList[i] = sheetData
  247. }
  248. }
  249. addSheetItem.DataList = sheetDataList
  250. addSheetList = append(addSheetList, addSheetItem)
  251. }
  252. // 添加表格
  253. err = excelModel.AddExcelInfoAndSheet(excelInfo, addSheetList)
  254. return
  255. }
  256. // UpdateExcelInfoFileUrl 更新excel表格的下载地址
  257. func UpdateExcelInfoFileUrl(excelInfo *excelModel.ExcelInfo) {
  258. var err error
  259. defer func() {
  260. if err != nil {
  261. go alarm_msg.SendAlarmMsg(fmt.Sprintf("更新excel表格的下载地址失败,表格id:%d;表格名称:%s; ERR:%s", excelInfo.ExcelInfoId, excelInfo.ExcelName, err), 3)
  262. utils.FileLog.Info(fmt.Sprintf("更新excel表格的下载地址失败,表格id:%d;表格名称:%s; ERR:%s", excelInfo.ExcelInfoId, excelInfo.ExcelName, err), 3)
  263. }
  264. }()
  265. fileName := excelInfo.ExcelName + "_" + excelInfo.UniqueCode + ".xlsx"
  266. var downloadFilePath string // excel文件下载地址
  267. switch excelInfo.Source {
  268. case utils.EXCEL_DEFAULT: // 自定义表格
  269. luckySheetData, tmpErr := excel.GetLuckySheetData(excelInfo.Content)
  270. if tmpErr != nil {
  271. err = tmpErr
  272. fmt.Println("err:", err)
  273. return
  274. }
  275. //_, err = luckySheetData.GetTableDataByLuckySheetDataStr()
  276. downloadFilePath, err = luckySheetData.ToExcel()
  277. case utils.CUSTOM_ANALYSIS_TABLE: // 自定义分析表格
  278. downloadFilePath, err, _ = GenerateExcelCustomAnalysisExcel(excelInfo)
  279. }
  280. if err != nil {
  281. fmt.Println("err:", err)
  282. return
  283. }
  284. defer func() {
  285. _ = os.Remove(downloadFilePath)
  286. }()
  287. var resourceUrl string
  288. //上传到阿里云
  289. //if utils.ObjectStorageClient == "minio" {
  290. // resourceUrl, err = services.UploadImgToMinIo(fileName, downloadFilePath)
  291. //} else {
  292. // resourceUrl, err = services.UploadAliyunV2(fileName, downloadFilePath)
  293. //}
  294. //if err != nil {
  295. // return
  296. //}
  297. ossClient := services.NewOssClient()
  298. if ossClient == nil {
  299. err = fmt.Errorf("初始化OSS服务失败")
  300. return
  301. }
  302. resourceUrl, err = ossClient.UploadFile(fileName, downloadFilePath, "")
  303. if err != nil {
  304. err = fmt.Errorf("文件上传失败, Err: %s", err.Error())
  305. return
  306. }
  307. excelInfo.FileUrl = resourceUrl
  308. err = excelInfo.Update([]string{"FileUrl"})
  309. }