123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- package excel
- import (
- "errors"
- excelModel "eta/eta_api/models/data_manage/excel"
- "eta/eta_api/models/system"
- "eta/eta_api/services"
- "eta/eta_api/services/alarm_msg"
- excel "eta/eta_api/services/excel"
- "eta/eta_api/utils"
- "fmt"
- "os"
- "strconv"
- "time"
- )
- // Delete excel删除
- func Delete(excelInfo *excelModel.ExcelInfo, sysUser *system.Admin) (err error, errMsg string, isSendEmail bool) {
- isSendEmail = true
- // 操作权限校验
- {
- button := GetExcelInfoOpButton(sysUser, excelInfo.SysUserId, excelInfo.Source)
- if !button.DeleteButton {
- errMsg = "无操作权限"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- }
- // 自定义分析,需要做这个指标关联的校验
- if excelInfo.Source == utils.CUSTOM_ANALYSIS_TABLE {
- list, tmpErr := excelModel.GetExcelEdbMappingByExcelInfoId(excelInfo.ExcelInfoId)
- if tmpErr != nil {
- errMsg = `获取关联的指标信息失败`
- err = tmpErr
- return
- }
- if len(list) > 0 {
- errMsg = "已关联指标,不可删除!"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- }
- // 标记删除
- excelInfo.IsDelete = 1
- excelInfo.ModifyTime = time.Now()
- err = excelInfo.Update([]string{"IsDelete", "ModifyTime"})
- return
- }
- // Copy 复制excel
- func Copy(oldExcelInfoId, excelClassifyId int, excelName string, sysUser *system.Admin) (excelInfo *excelModel.ExcelInfo, err error, errMsg string, isSendEmail bool) {
- isSendEmail = true
- excelName = utils.TrimLRStr(excelName)
- excelClassify, err := excelModel.GetExcelClassifyById(excelClassifyId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "分类不存在"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- errMsg = "获取分类信息失败"
- return
- }
- if excelClassify == nil {
- errMsg = "分类不存在"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- // 获取原ETA表格信息
- oldExcelInfo, err := excelModel.GetExcelInfoById(oldExcelInfoId)
- if err != nil {
- errMsg = "获取ETA表格失败"
- return
- }
- // 操作权限校验
- {
- button := GetExcelInfoOpButton(sysUser, oldExcelInfo.SysUserId, oldExcelInfo.Source)
- if !button.CopyButton {
- errMsg = "无操作权限"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- }
- // 检验分类下是否存在该表格名称
- {
- var condition string
- var pars []interface{}
- condition += " AND excel_classify_id=? "
- pars = append(pars, excelClassifyId)
- condition += " AND excel_name=? "
- pars = append(pars, excelName)
- count, tmpErr := excelModel.GetExcelInfoCountByCondition(condition, pars)
- if tmpErr != nil {
- errMsg = "判断表格名称是否存在失败"
- err = tmpErr
- return
- }
- if count > 0 {
- errMsg = "表格名称已存在,请重新填写表格名称"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- }
- // 表格信息
- timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
- excelInfo = &excelModel.ExcelInfo{
- //ExcelInfoId: 0,
- ExcelName: excelName,
- Source: oldExcelInfo.Source,
- ExcelType: oldExcelInfo.ExcelType,
- UniqueCode: utils.MD5(utils.EXCEL_DATA_PREFIX + "_" + timestamp),
- ExcelClassifyId: excelClassifyId,
- SysUserId: sysUser.AdminId,
- SysUserRealName: sysUser.RealName,
- Content: oldExcelInfo.Content,
- ExcelImage: oldExcelInfo.ExcelImage,
- FileUrl: oldExcelInfo.FileUrl,
- Sort: 0,
- IsDelete: 0,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- }
- // 如果不是自定义分析,那么直接加主表就好了
- if excelInfo.Source != utils.CUSTOM_ANALYSIS_TABLE {
- // 获取excel与指标的关系表
- list, tmpErr := excelModel.GetAllExcelEdbMappingByExcelInfoId(excelInfo.ExcelInfoId)
- if tmpErr != nil {
- errMsg = "获取失败"
- err = tmpErr
- return
- }
- for k, v := range list {
- v.ExcelEdbMappingId = 0
- v.ExcelInfoId = 0
- list[k] = v
- }
- err = excelModel.AddExcelInfo(excelInfo, list)
- if err != nil {
- errMsg = "保存失败"
- }
- return
- }
- // 自定义分析,需要有额外信息
- addSheetList := make([]excelModel.AddExcelSheetParams, 0)
- // 获取所有的sheet页
- oldSheetItemList, err := excelModel.GetAllSheetList(oldExcelInfo.ExcelInfoId)
- if err != nil {
- errMsg = `获取sheet页失败`
- return
- }
- // 获取所有的sheet页的sheet数据
- sheetCellDataMapList := make(map[int][]*excelModel.ExcelSheetData)
- {
- dataList, tmpErr := excelModel.GetAllSheetDataListByExcelInfoId(oldExcelInfo.ExcelInfoId)
- if tmpErr != nil {
- errMsg = `获取sheet页的单元格数据失败`
- err = tmpErr
- return
- }
- for _, cellData := range dataList {
- sheetDataList, ok := sheetCellDataMapList[cellData.ExcelSheetId]
- if !ok {
- sheetDataList = make([]*excelModel.ExcelSheetData, 0)
- }
- sheetCellDataMapList[cellData.ExcelSheetId] = append(sheetDataList, cellData)
- }
- }
- // sheet处理
- for _, sheetInfo := range oldSheetItemList {
- addSheetItem := excelModel.AddExcelSheetParams{
- ExcelSheetId: 0,
- ExcelInfoId: 0,
- SheetName: sheetInfo.SheetName,
- Sort: sheetInfo.Sort,
- Config: sheetInfo.Config,
- CalcChain: sheetInfo.CalcChain,
- }
- sheetDataList, ok := sheetCellDataMapList[sheetInfo.ExcelSheetId]
- if ok {
- for i, sheetData := range sheetDataList {
- sheetData.ExcelDataId = 0
- sheetData.ExcelSheetId = 0
- sheetData.ExcelInfoId = 0
- sheetDataList[i] = sheetData
- }
- }
- addSheetItem.DataList = sheetDataList
- addSheetList = append(addSheetList, addSheetItem)
- }
- // 添加表格
- err = excelModel.AddExcelInfoAndSheet(excelInfo, addSheetList)
- return
- }
- // UpdateExcelInfoFileUrl 更新excel表格的下载地址
- func UpdateExcelInfoFileUrl(excelInfo *excelModel.ExcelInfo) {
- var err error
- defer func() {
- if err != nil {
- go alarm_msg.SendAlarmMsg(fmt.Sprintf("更新excel表格的下载地址失败,表格id:%d;表格名称:%s; ERR:%s", excelInfo.ExcelInfoId, excelInfo.ExcelName, err), 3)
- utils.FileLog.Info(fmt.Sprintf("更新excel表格的下载地址失败,表格id:%d;表格名称:%s; ERR:%s", excelInfo.ExcelInfoId, excelInfo.ExcelName, err), 3)
- }
- }()
- fileName := excelInfo.ExcelName + "_" + excelInfo.UniqueCode + ".xlsx"
- var downloadFilePath string // excel文件下载地址
- switch excelInfo.Source {
- case utils.EXCEL_DEFAULT: // 自定义表格
- luckySheetData, tmpErr := excel.GetLuckySheetData(excelInfo.Content)
- if tmpErr != nil {
- err = tmpErr
- fmt.Println("err:", err)
- return
- }
- //_, err = luckySheetData.GetTableDataByLuckySheetDataStr()
- downloadFilePath, err = luckySheetData.ToExcel()
- case utils.CUSTOM_ANALYSIS_TABLE: // 自定义分析表格
- downloadFilePath, err, _ = GenerateExcelCustomAnalysisExcel(excelInfo)
- }
- if err != nil {
- fmt.Println("err:", err)
- return
- }
- defer func() {
- _ = os.Remove(downloadFilePath)
- }()
- var resourceUrl string
- //上传到阿里云
- //if utils.ObjectStorageClient == "minio" {
- // resourceUrl, err = services.UploadImgToMinIo(fileName, downloadFilePath)
- //} else {
- // resourceUrl, err = services.UploadAliyunV2(fileName, downloadFilePath)
- //}
- //if err != nil {
- // return
- //}
- ossClient := services.NewOssClient()
- if ossClient == nil {
- err = fmt.Errorf("初始化OSS服务失败")
- return
- }
- resourceUrl, err = ossClient.UploadFile(fileName, downloadFilePath, "")
- if err != nil {
- err = fmt.Errorf("文件上传失败, Err: %s", err.Error())
- return
- }
- excelInfo.FileUrl = resourceUrl
- err = excelInfo.Update([]string{"FileUrl"})
- }
|