excel_op.go 8.2 KB

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