excel_op.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. }
  183. // 如果不是自定义分析,那么直接加主表就好了
  184. if excelInfo.Source != utils.CUSTOM_ANALYSIS_TABLE {
  185. // 获取excel与指标的关系表
  186. list, tmpErr := excelModel.GetAllExcelEdbMappingByExcelInfoId(excelInfo.ExcelInfoId)
  187. if tmpErr != nil {
  188. errMsg = "获取失败"
  189. err = tmpErr
  190. return
  191. }
  192. for k, v := range list {
  193. v.ExcelEdbMappingId = 0
  194. v.ExcelInfoId = 0
  195. list[k] = v
  196. }
  197. var childExcel *excelModel.ExcelInfo
  198. err = excelModel.AddExcelInfo(excelInfo, list, childExcel)
  199. if err != nil {
  200. errMsg = "保存失败"
  201. }
  202. return
  203. }
  204. // 自定义分析,需要有额外信息
  205. addSheetList := make([]excelModel.AddExcelSheetParams, 0)
  206. // 获取所有的sheet页
  207. oldSheetItemList, err := excelModel.GetAllSheetList(oldExcelInfo.ExcelInfoId)
  208. if err != nil {
  209. errMsg = `获取sheet页失败`
  210. return
  211. }
  212. // 获取所有的sheet页的sheet数据
  213. sheetCellDataMapList := make(map[int][]*excelModel.ExcelSheetData)
  214. {
  215. dataList, tmpErr := excelModel.GetAllSheetDataListByExcelInfoId(oldExcelInfo.ExcelInfoId)
  216. if tmpErr != nil {
  217. errMsg = `获取sheet页的单元格数据失败`
  218. err = tmpErr
  219. return
  220. }
  221. for _, cellData := range dataList {
  222. sheetDataList, ok := sheetCellDataMapList[cellData.ExcelSheetId]
  223. if !ok {
  224. sheetDataList = make([]*excelModel.ExcelSheetData, 0)
  225. }
  226. sheetCellDataMapList[cellData.ExcelSheetId] = append(sheetDataList, cellData)
  227. }
  228. }
  229. // sheet处理
  230. for _, sheetInfo := range oldSheetItemList {
  231. addSheetItem := excelModel.AddExcelSheetParams{
  232. ExcelSheetId: 0,
  233. ExcelInfoId: 0,
  234. SheetName: sheetInfo.SheetName,
  235. Sort: sheetInfo.Sort,
  236. Config: sheetInfo.Config,
  237. CalcChain: sheetInfo.CalcChain,
  238. }
  239. sheetDataList, ok := sheetCellDataMapList[sheetInfo.ExcelSheetId]
  240. if ok {
  241. for i, sheetData := range sheetDataList {
  242. sheetData.ExcelDataId = 0
  243. sheetData.ExcelSheetId = 0
  244. sheetData.ExcelInfoId = 0
  245. sheetDataList[i] = sheetData
  246. }
  247. }
  248. addSheetItem.DataList = sheetDataList
  249. addSheetList = append(addSheetList, addSheetItem)
  250. }
  251. // 添加表格
  252. err = excelModel.AddExcelInfoAndSheet(excelInfo, addSheetList)
  253. return
  254. }
  255. // UpdateExcelInfoFileUrl 更新excel表格的下载地址
  256. func UpdateExcelInfoFileUrl(excelInfo *excelModel.ExcelInfo) {
  257. var err error
  258. defer func() {
  259. if err != nil {
  260. go alarm_msg.SendAlarmMsg(fmt.Sprintf("更新excel表格的下载地址失败,表格id:%d;表格名称:%s; ERR:%s", excelInfo.ExcelInfoId, excelInfo.ExcelName, err), 3)
  261. utils.FileLog.Info(fmt.Sprintf("更新excel表格的下载地址失败,表格id:%d;表格名称:%s; ERR:%s", excelInfo.ExcelInfoId, excelInfo.ExcelName, err), 3)
  262. }
  263. }()
  264. fileName := excelInfo.ExcelName + "_" + excelInfo.UniqueCode + ".xlsx"
  265. var downloadFilePath string // excel文件下载地址
  266. switch excelInfo.Source {
  267. case utils.EXCEL_DEFAULT: // 自定义表格
  268. luckySheetData, tmpErr := excel.GetLuckySheetData(excelInfo.Content)
  269. if tmpErr != nil {
  270. err = tmpErr
  271. fmt.Println("err:", err)
  272. return
  273. }
  274. //_, err = luckySheetData.GetTableDataByLuckySheetDataStr()
  275. downloadFilePath, err = luckySheetData.ToExcel()
  276. case utils.CUSTOM_ANALYSIS_TABLE: // 自定义分析表格
  277. downloadFilePath, err, _ = GenerateExcelCustomAnalysisExcel(excelInfo)
  278. }
  279. if err != nil {
  280. fmt.Println("err:", err)
  281. return
  282. }
  283. defer func() {
  284. _ = os.Remove(downloadFilePath)
  285. }()
  286. var resourceUrl string
  287. //上传到阿里云
  288. //if utils.ObjectStorageClient == "minio" {
  289. // resourceUrl, err = services.UploadImgToMinIo(fileName, downloadFilePath)
  290. //} else {
  291. // resourceUrl, err = services.UploadAliyunV2(fileName, downloadFilePath)
  292. //}
  293. //if err != nil {
  294. // return
  295. //}
  296. ossClient := services.NewOssClient()
  297. if ossClient == nil {
  298. err = fmt.Errorf("初始化OSS服务失败")
  299. return
  300. }
  301. resourceUrl, err = ossClient.UploadFile(fileName, downloadFilePath, "")
  302. if err != nil {
  303. err = fmt.Errorf("文件上传失败, Err: %s", err.Error())
  304. return
  305. }
  306. excelInfo.FileUrl = resourceUrl
  307. err = excelInfo.Update([]string{"FileUrl"})
  308. }