excel_op.go 7.4 KB

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