1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732 |
- package excel
- import (
- "archive/zip"
- "encoding/json"
- "errors"
- "eta/eta_api/models"
- "eta/eta_api/models/data_manage"
- "eta/eta_api/models/data_manage/excel"
- "eta/eta_api/models/data_manage/excel/request"
- "eta/eta_api/models/data_manage/excel/response"
- "eta/eta_api/models/system"
- "eta/eta_api/services/data"
- "eta/eta_api/services/data/data_manage_permission"
- excelService "eta/eta_api/services/data/excel"
- etaTrialService "eta/eta_api/services/eta_trial"
- excel2 "eta/eta_api/services/excel"
- "eta/eta_api/utils"
- "fmt"
- "github.com/tealeg/xlsx"
- "io/ioutil"
- "os"
- "sort"
- "strconv"
- "strings"
- "time"
- )
- // GetChildTable
- // @Title 获取子表
- // @Description 获取子表
- // @Param request body request.MixedTableCellDataReq true "type json string"
- // @router /excel_info/child_table [get]
- func (c *ExcelInfoController) GetChildTable() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- parentId, _ := c.GetInt("ParentId")
- if parentId <= 0 {
- br.Msg = "请选择父表"
- return
- }
- list := make([]*excel.ExcelInfo, 0)
- // 查询所有子表
- childList, err := excel.GetChildExcelInfoByParentId(parentId)
- if err != nil {
- br.Msg = "查询子表失败"
- return
- }
- if len(childList) > 0 {
- list = childList
- }
- resp := &response.BalanceChildTableResp{List: list}
- br.Ret = 200
- br.Success = true
- br.Msg = "查询成功"
- br.Data = resp
- }
- // Rename
- // @Title 表格重命名接口
- // @Description 表格重命名接口
- // @Param request body request.EditExcelInfoReq true "type json string"
- // @Success 200 {object} response.AddExcelInfoResp
- // @router /excel_info/rename [post]
- func (c *ExcelInfoController) Rename() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req request.RenameExcelInfoReq
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.ExcelInfoId <= 0 {
- br.Msg = "请传入ETA表格标识!"
- return
- }
- req.ExcelName = strings.Trim(req.ExcelName, " ")
- if req.ExcelName == "" {
- br.Msg = "请填写表格名称!"
- br.IsSendEmail = false
- return
- }
- excelInfo, err := excel.GetExcelInfoById(req.ExcelInfoId)
- if err != nil {
- br.Msg = "获取ETA表格失败"
- br.ErrMsg = "获取ETA表格失败,Err:" + err.Error()
- return
- }
- //判断表格是否存在
- var condition string
- var pars []interface{}
- condition += " AND excel_info_id != ? AND excel_classify_id=? AND excel_name=? AND parent_id=?"
- pars = append(pars, req.ExcelInfoId, excelInfo.ExcelClassifyId, req.ExcelName, excelInfo.ParentId)
- // 获取分类下是否存在该表格名称
- count, err := excel.GetExcelInfoCountByCondition(condition, pars)
- if err != nil {
- br.Msg = "ETA判断表格名称是否存在失败"
- br.ErrMsg = "判断ETA表格名称是否存在失败,Err:" + err.Error()
- return
- }
- if count > 0 {
- br.Msg = "ETA表格名称已存在,请重新填写"
- br.IsSendEmail = false
- return
- }
- // 操作权限校验, 增加协作人判断
- {
- checkExcelInfo := excelInfo
- if excelInfo.Source == utils.BALANCE_TABLE {
- checkExcelInfoId := excelInfo.ExcelInfoId
- if excelInfo.BalanceType == 1 {
- checkExcelInfoId = excelInfo.RelExcelInfoId
- } else {
- if excelInfo.ParentId > 0 {
- checkExcelInfoId = excelInfo.ParentId
- }
- }
- if checkExcelInfoId != excelInfo.ExcelInfoId {
- checkExcelInfo, err = excel.GetExcelInfoById(checkExcelInfoId)
- if err != nil {
- br.Msg = "获取平衡表格信息失败"
- br.ErrMsg = "获取平衡表格信息失败,Err:" + err.Error()
- return
- }
- }
- }
- // 数据权限
- haveOperaAuth, e := data_manage_permission.CheckExcelPermissionByExcelInfoId(checkExcelInfo.ExcelInfoId, checkExcelInfo.ExcelClassifyId, checkExcelInfo.IsJoinPermission, c.SysUser.AdminId)
- if e != nil {
- br.Msg = "获取ETA表格权限失败"
- br.ErrMsg = "获取表格权限信息失败,Err" + e.Error()
- return
- }
- button := excelService.GetBalanceExcelInfoOpButton(sysUser.AdminId, checkExcelInfo.SysUserId, haveOperaAuth, checkExcelInfo.ExcelInfoId)
- if !button.OpButton {
- br.Msg = "无操作权限"
- br.IsSendEmail = false
- return
- }
- }
- excelInfo.ModifyTime = time.Now()
- excelInfo.ExcelName = req.ExcelName
- excelInfo.UpdateUserId = sysUser.AdminId
- excelInfo.UpdateUserRealName = sysUser.RealName
- // 自动保存时不会传缩略图,也就不更新这个字段
- updateExcelInfoParams := []string{"ModifyTime", "ExcelName", "UpdateUserId", "UpdateUserRealName"}
- err = excelInfo.Update(updateExcelInfoParams)
- if err != nil {
- br.Msg = "保存失败"
- br.ErrMsg = "保存失败,Err:" + err.Error()
- return
- }
- resp := response.AddExcelInfoResp{
- ExcelInfoId: excelInfo.ExcelInfoId,
- UniqueCode: excelInfo.UniqueCode,
- }
- //删除公共图库那边的缓存
- _ = utils.Rc.Delete(utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL + ":" + excelInfo.UniqueCode)
- br.Ret = 200
- br.Success = true
- br.Msg = "修改成功"
- br.Data = resp
- br.IsAddLog = true
- }
- // SaveExcelWorker
- // @Title 新增编辑表格协作人接口
- // @Description 新增编辑表格协作人接口
- // @Param request body request.EditExcelInfoReq true "type json string"
- // @Success 200 {object} response.AddExcelInfoResp
- // @router /excel_info/worker/save [post]
- func (c *ExcelInfoController) SaveExcelWorker() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req request.SaveExcelInfoWorkerReq
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.ExcelInfoId <= 0 {
- br.Msg = "请传入ETA表格标识!"
- return
- }
- excelInfo, err := excel.GetExcelInfoById(req.ExcelInfoId)
- if err != nil {
- br.Msg = "获取ETA表格失败"
- br.ErrMsg = "获取ETA表格失败,Err:" + err.Error()
- return
- }
- if excelInfo.SysUserId != sysUser.AdminId {
- br.Msg = "您没有权限操作!"
- br.ErrMsg = "您没有权限操作!"
- return
- }
- // 操作权限校验, 增加协作人判断
- {
- // 数据权限
- haveOperaAuth, err := data_manage_permission.CheckExcelPermissionByExcelInfoId(excelInfo.ExcelInfoId, excelInfo.ExcelClassifyId, excelInfo.IsJoinPermission, c.SysUser.AdminId)
- if err != nil {
- br.Msg = "获取ETA表格失败"
- br.ErrMsg = "获取ETA表格权限失败,Err:" + err.Error()
- return
- }
- button := excelService.GetExcelInfoOpButton(sysUser, excelInfo.SysUserId, excelInfo.Source, haveOperaAuth)
- if !button.OpButton {
- br.Msg = "无操作权限"
- br.IsSendEmail = false
- return
- }
- }
- obj := new(excel.ExcelWorker)
- notDeleteWorkerIds := make([]string, 0)
- workerMap := make(map[int]struct{})
- if req.SysUserIds != "" {
- notDeleteWorkerIds = strings.Split(req.SysUserIds, ",")
- }
- existList, err := obj.GetByExcelInfoId(req.ExcelInfoId)
- if err != nil {
- br.Msg = "获取表格协作人失败!"
- br.ErrMsg = "获取表格协作人失败,Err:" + err.Error()
- return
- }
- for _, v := range existList {
- workerMap[v.SysUserId] = struct{}{}
- }
- addIds := make([]int, 0)
- for _, v := range notDeleteWorkerIds {
- id, _ := strconv.Atoi(v)
- if _, ok := workerMap[id]; !ok {
- addIds = append(addIds, id)
- }
- }
- // 查询协作人姓名
- adminList, err := system.GetAdminListByIdListWithoutEnable(addIds)
- addList := make([]*excel.ExcelWorker, 0)
- for _, v := range adminList {
- addList = append(addList, &excel.ExcelWorker{
- ExcelInfoId: req.ExcelInfoId,
- SysUserId: v.AdminId,
- SysUserRealName: v.RealName,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- })
- }
- // 保存协作人
- err = obj.AddWorker(req.ExcelInfoId, addList, notDeleteWorkerIds)
- if err != nil {
- br.Msg = "保存失败"
- br.ErrMsg = "保存失败,Err:" + err.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // GetWorker
- // @Title 查询协作人接口
- // @Description 新增编辑表格协作人接口
- // @Param request body request.EditExcelInfoReq true "type json string"
- // @Success 200 {object} response.AddExcelInfoResp
- // @router /excel_info/worker [get]
- func (c *ExcelInfoController) GetWorker() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- excelInfoId, _ := c.GetInt("ExcelInfoId")
- if excelInfoId <= 0 {
- br.Msg = "请选择表格"
- br.ErrMsg = "ExcelInfoId未传"
- br.IsSendEmail = false
- return
- }
- obj := new(excel.ExcelWorker)
- list, err := obj.GetByExcelInfoId(excelInfoId)
- if err != nil {
- br.Msg = "获取表格协作人失败!"
- br.ErrMsg = "获取表格协作人失败,Err:" + err.Error()
- return
- }
- ret := &response.BalanceTableWorkerResp{List: list}
- br.Data = ret
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // BalanceChartInfoAdd
- // @Title 新增图表接口
- // @Description 新增图表接口
- // @Param request body data_manage.AddChartInfoReq true "type json string"
- // @Success 200 {object} data_manage.AddChartInfoResp
- // @router /excel_info/balance/chart_add [post]
- func (c *ExcelInfoController) BalanceChartInfoAdd() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- deleteCache := true
- cacheKey := "CACHE_EXCEL_CHART_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
- defer func() {
- if deleteCache {
- utils.Rc.Delete(cacheKey)
- }
- }()
- if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
- deleteCache = false
- br.Msg = "系统处理中,请稍后重试!"
- br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(c.Ctx.Input.RequestBody)
- return
- }
- var req request.AddBalanceTableChartReq
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- req.ChartName = strings.Trim(req.ChartName, " ")
- if req.ChartName == "" {
- br.Msg = "请填写图表名称!"
- return
- }
- // 获取表格信息
- excelInfo, err := excel.GetExcelInfoById(req.ExcelInfoId)
- if err != nil {
- br.Msg = "获取ETA表格失败"
- return
- }
- if excelInfo.Source != utils.BALANCE_TABLE {
- br.Msg = "EXCEL类型错误!"
- return
- }
- // 操作权限校验, 增加协作人判断
- {
- checkExcelInfo := excelInfo
- if excelInfo.Source == utils.BALANCE_TABLE {
- checkExcelInfoId := excelInfo.ExcelInfoId
- if excelInfo.BalanceType == 1 {
- checkExcelInfoId = excelInfo.RelExcelInfoId
- } else {
- if excelInfo.ParentId > 0 {
- checkExcelInfoId = excelInfo.ParentId
- }
- }
- if checkExcelInfoId != excelInfo.ExcelInfoId {
- checkExcelInfo, err = excel.GetExcelInfoById(checkExcelInfoId)
- if err != nil {
- br.Msg = "获取平衡表格信息失败"
- br.ErrMsg = "获取平衡表格信息失败,Err:" + err.Error()
- return
- }
- }
- }
- // 数据权限
- haveOperaAuth, e := data_manage_permission.CheckExcelPermissionByExcelInfoId(checkExcelInfo.ExcelInfoId, checkExcelInfo.ExcelClassifyId, checkExcelInfo.IsJoinPermission, c.SysUser.AdminId)
- if e != nil {
- br.Msg = "获取ETA表格权限失败"
- br.ErrMsg = "获取表格权限信息失败,Err" + e.Error()
- return
- }
- button := excelService.GetBalanceExcelInfoOpButton(sysUser.AdminId, checkExcelInfo.SysUserId, haveOperaAuth, checkExcelInfo.ExcelInfoId)
- if !button.OpButton {
- br.Msg = "无操作权限"
- br.IsSendEmail = false
- return
- }
- }
- // 判断如果是静态表,则查询表数据,落库
- dataListMap := make(map[int][]*data_manage.EdbDataList)
- if excelInfo.BalanceType == 1 {
- newExcelDataMap, excelAllRows, excelAllCols, e, errMsg := excelService.GetBalanceExcelData(excelInfo, c.Lang)
- if e != nil {
- br.Msg = "获取表格数据失败"
- if errMsg != "" {
- br.Msg = errMsg
- }
- br.ErrMsg = "获取表格数据失败,Err:" + e.Error()
- return
- }
- for k, mapping := range req.ChartEdbInfoList {
- tmpMapping := &excel.ExcelChartEdb{
- ExcelChartEdbId: k,
- DateSequence: mapping.DateSequenceStr,
- DataSequence: mapping.DataSequenceStr,
- }
- er, msg := excelService.GetBalanceExcelEdbData(tmpMapping, newExcelDataMap, dataListMap, excelAllRows, excelAllCols)
- if er != nil {
- utils.FileLog.Info(fmt.Sprintf(" 获取图表,指标信息失败 Err:%s, %s", msg, er.Error()))
- continue
- }
- }
- }
- chartInfo, err, errMsg, isSendEmail := data.AddBalanceExcelChart(excelInfo, req, sysUser, dataListMap)
- if err != nil {
- br.Msg = "保存失败"
- if errMsg != `` {
- br.Msg = errMsg
- }
- br.ErrMsg = err.Error()
- br.IsSendEmail = isSendEmail
- return
- }
- resp := new(data_manage.AddChartInfoResp)
- resp.ChartInfoId = chartInfo.ChartInfoId
- resp.UniqueCode = chartInfo.UniqueCode
- resp.ChartType = req.ChartType
- //新增操作日志
- {
- chartLog := new(data_manage.ChartInfoLog)
- chartLog.ChartInfoId = chartInfo.ChartInfoId
- chartLog.ChartName = req.ChartName
- chartLog.ChartClassifyId = 0
- chartLog.SysUserId = sysUser.AdminId
- chartLog.SysUserRealName = sysUser.RealName
- chartLog.UniqueCode = chartInfo.UniqueCode
- chartLog.CreateTime = time.Now()
- chartLog.Content = string(c.Ctx.Input.RequestBody)
- chartLog.Status = "新增图表"
- chartLog.Method = c.Ctx.Input.URI()
- go data_manage.AddChartInfoLog(chartLog)
- }
- // 试用平台更新用户累计新增图表数
- adminItem, e := system.GetSysAdminById(sysUser.AdminId)
- if e != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "获取系统用户数据失败,Err:" + err.Error()
- return
- }
- if utils.BusinessCode == utils.BusinessCodeSandbox && adminItem.DepartmentName == "ETA试用客户" {
- go func() {
- var r etaTrialService.EtaTrialUserReq
- r.Mobile = adminItem.Mobile
- _, _ = etaTrialService.UpdateUserChartNum(r)
- }()
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "保存成功"
- br.Data = resp
- br.IsAddLog = true
- }
- // BalanceChartInfoEdit
- // @Title 编辑图表接口
- // @Description 新增图表接口
- // @Param request body data_manage.AddChartInfoReq true "type json string"
- // @Success 200 {object} data_manage.AddChartInfoResp
- // @router /excel_info/balance/chart_edit [post]
- func (c *ExcelInfoController) BalanceChartInfoEdit() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- deleteCache := true
- cacheKey := "CACHE_EXCEL_CHART_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
- defer func() {
- if deleteCache {
- utils.Rc.Delete(cacheKey)
- }
- }()
- if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
- deleteCache = false
- br.Msg = "系统处理中,请稍后重试!"
- br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(c.Ctx.Input.RequestBody)
- return
- }
- var req request.AddBalanceTableChartReq
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- req.ChartName = strings.Trim(req.ChartName, " ")
- if req.ChartName == "" {
- br.Msg = "请填写图表名称!"
- return
- }
- if req.ChartInfoId <= 0 {
- br.Msg = "请选择图表"
- return
- }
- // 获取表格信息
- excelInfo, err := excel.GetExcelInfoById(req.ExcelInfoId)
- if err != nil {
- br.Msg = "获取ETA表格失败"
- return
- }
- if excelInfo.Source != utils.BALANCE_TABLE {
- br.Msg = "EXCEL类型错误!"
- return
- }
- // 操作权限校验, 增加协作人判断
- {
- checkExcelInfo := excelInfo
- if excelInfo.Source == utils.BALANCE_TABLE {
- checkExcelInfoId := excelInfo.ExcelInfoId
- if excelInfo.BalanceType == 1 {
- checkExcelInfoId = excelInfo.RelExcelInfoId
- } else {
- if excelInfo.ParentId > 0 {
- checkExcelInfoId = excelInfo.ParentId
- }
- }
- if checkExcelInfoId != excelInfo.ExcelInfoId {
- checkExcelInfo, err = excel.GetExcelInfoById(checkExcelInfoId)
- if err != nil {
- br.Msg = "获取平衡表格信息失败"
- br.ErrMsg = "获取平衡表格信息失败,Err:" + err.Error()
- return
- }
- }
- }
- // 数据权限
- haveOperaAuth, e := data_manage_permission.CheckExcelPermissionByExcelInfoId(checkExcelInfo.ExcelInfoId, checkExcelInfo.ExcelClassifyId, checkExcelInfo.IsJoinPermission, c.SysUser.AdminId)
- if e != nil {
- br.Msg = "获取ETA表格权限失败"
- br.ErrMsg = "获取表格权限信息失败,Err" + e.Error()
- return
- }
- button := excelService.GetBalanceExcelInfoOpButton(sysUser.AdminId, checkExcelInfo.SysUserId, haveOperaAuth, checkExcelInfo.ExcelInfoId)
- if !button.OpButton {
- br.Msg = "无操作权限"
- br.IsSendEmail = false
- return
- }
- }
- // 判断如果是静态表,则查询表数据,落库
- dataListMap := make(map[int][]*data_manage.EdbDataList)
- if excelInfo.BalanceType == 1 {
- newExcelDataMap, excelAllRows, excelAllCols, e, errMsg := excelService.GetBalanceExcelData(excelInfo, c.Lang)
- if e != nil {
- br.Msg = "获取表格数据失败"
- if errMsg != "" {
- br.Msg = errMsg
- }
- br.ErrMsg = "获取表格数据失败,Err:" + e.Error()
- return
- }
- for k, mapping := range req.ChartEdbInfoList {
- tmpMapping := &excel.ExcelChartEdb{
- ExcelChartEdbId: k,
- DateSequence: mapping.DateSequenceStr,
- DataSequence: mapping.DataSequenceStr,
- }
- er, msg := excelService.GetBalanceExcelEdbData(tmpMapping, newExcelDataMap, dataListMap, excelAllRows, excelAllCols)
- if er != nil {
- utils.FileLog.Info(fmt.Sprintf(" 获取图表,指标信息失败 Err:%s, %s", msg, er.Error()))
- continue
- }
- }
- }
- chartItem, err, errMsg, isSendEmail := data.EditBalanceExcelChart(excelInfo, req, sysUser, dataListMap)
- if err != nil {
- br.Msg = "保存失败"
- if errMsg != `` {
- br.Msg = errMsg
- }
- br.ErrMsg = err.Error()
- br.IsSendEmail = isSendEmail
- return
- }
- resp := new(data_manage.AddChartInfoResp)
- resp.ChartInfoId = chartItem.ChartInfoId
- resp.UniqueCode = chartItem.UniqueCode
- resp.ChartType = req.ChartType
- //新增操作日志
- {
- chartLog := new(data_manage.ChartInfoLog)
- chartLog.ChartName = chartItem.ChartName
- chartLog.ChartInfoId = req.ChartInfoId
- chartLog.ChartClassifyId = chartItem.ChartClassifyId
- chartLog.SysUserId = sysUser.AdminId
- chartLog.SysUserRealName = sysUser.RealName
- chartLog.UniqueCode = chartItem.UniqueCode
- chartLog.CreateTime = time.Now()
- chartLog.Content = string(c.Ctx.Input.RequestBody)
- chartLog.Status = "编辑图表"
- chartLog.Method = c.Ctx.Input.URL()
- go data_manage.AddChartInfoLog(chartLog)
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "保存成功"
- br.Data = resp
- br.IsAddLog = true
- }
- // GetBalanceChartList
- // @Title 获取平衡表表关联的图表
- // @Description 获取平衡表表关联的图表
- // @Param request body request.MixedTableCellDataReq true "type json string"
- // @router /excel_info/balance/chart_list [get]
- func (c *ExcelInfoController) GetBalanceChartList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- excelInfoId, _ := c.GetInt("ExcelInfoId")
- if excelInfoId <= 0 {
- br.Msg = "请选择平衡表"
- return
- }
- // 查询所有子表
- excelInfo, err := excel.GetExcelInfoById(excelInfoId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "表格不存在"
- return
- }
- br.Msg = "查询子表失败"
- br.ErrMsg = "查询子表失败,Err:" + err.Error()
- return
- }
- list := make([]*data_manage.BalanceChartInfoDetailResp, 0)
- chartInfoList, mappingListMap, dataListMap, err, errMsg := excelService.GetBalanceExcelChartList(excelInfo, "")
- if err != nil {
- if errMsg != "" {
- br.Msg = errMsg
- br.ErrMsg = err.Error()
- return
- } else {
- br.Msg = "查询图表失败"
- br.ErrMsg = "查询图表失败,Err:" + err.Error()
- }
- return
- }
- for _, chartInfo := range chartInfoList {
- mappingList, ok := mappingListMap[chartInfo.ChartInfoId]
- if !ok {
- br.Msg = "未找到图表关联的指标信息"
- return
- }
- var chartInfoResp *data_manage.ChartInfoDetailResp
- chartInfoResp, err, errMsg = data.GetBalanceExcelChartDetail(chartInfo, mappingList, sysUser, dataListMap)
- if err != nil {
- br.Msg = "查询图表详情失败"
- br.ErrMsg = "查询图表详情失败,Err:" + err.Error()
- return
- }
- chartEdbList := make([]*data_manage.ExcelChartEdbView, 0)
- for _, v := range mappingList {
- tmp := &data_manage.ExcelChartEdbView{
- ExcelChartEdbId: v.ExcelChartEdbId,
- DateSequenceStr: v.DateSequence,
- DataSequenceStr: v.DataSequence,
- FromTag: v.FromTag,
- }
- chartEdbList = append(chartEdbList, tmp)
- }
- balanceChartInfoResp := &data_manage.BalanceChartInfoDetailResp{
- ChartInfoDetailResp: chartInfoResp,
- ExcelEdbList: chartEdbList,
- }
- list = append(list, balanceChartInfoResp)
- }
- ret := &data_manage.BalanceTableChartListResp{List: list}
- br.Ret = 200
- br.Success = true
- br.Msg = "查询成功"
- br.Data = ret
- }
- // DeleteBalanceChart
- // @Title 删除平衡表图表
- // @Description 删除平衡表图表
- // @Param request body data_manage.DeleteChartClassifyReq true "type json string"
- // @Success 200 Ret=200 删除成功
- // @router /excel_info/balance/chart_del [post]
- func (c *ExcelInfoController) DeleteBalanceChart() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req data_manage.DeleteChartClassifyReq
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.ChartInfoId <= 0 {
- br.Msg = "参数错误"
- br.IsSendEmail = false
- return
- }
- //删除图表
- if req.ChartInfoId > 0 {
- chartInfo, err := data_manage.GetChartInfoById(req.ChartInfoId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "图表已删除,请刷新页面"
- br.ErrMsg = "指标不存在,Err:" + err.Error()
- return
- } else {
- br.Msg = "删除失败"
- br.ErrMsg = "删除失败,获取指标信息失败,Err:" + err.Error()
- return
- }
- }
- if chartInfo == nil {
- br.Msg = "图表已删除,请刷新页面"
- return
- }
- // 操作权限校验, 增加协作人判断
- {
- // 获取表格信息
- excelInfo, e := excel.GetExcelInfoByChartInfoId(req.ChartInfoId)
- if e != nil {
- br.Msg = "获取ETA表格失败"
- return
- }
- checkExcelInfo := excelInfo
- if excelInfo.Source == utils.BALANCE_TABLE {
- checkExcelInfoId := excelInfo.ExcelInfoId
- if excelInfo.BalanceType == 1 {
- checkExcelInfoId = excelInfo.RelExcelInfoId
- } else {
- if excelInfo.ParentId > 0 {
- checkExcelInfoId = excelInfo.ParentId
- }
- }
- if checkExcelInfoId != excelInfo.ExcelInfoId {
- checkExcelInfo, e = excel.GetExcelInfoById(checkExcelInfoId)
- if e != nil {
- br.Msg = "获取平衡表格信息失败"
- br.ErrMsg = "获取平衡表格信息失败,Err:" + e.Error()
- return
- }
- }
- }
- // 数据权限
- haveOperaAuth, e := data_manage_permission.CheckExcelPermissionByExcelInfoId(checkExcelInfo.ExcelInfoId, checkExcelInfo.ExcelClassifyId, checkExcelInfo.IsJoinPermission, c.SysUser.AdminId)
- if e != nil {
- br.Msg = "获取ETA表格权限失败"
- br.ErrMsg = "获取表格权限信息失败,Err" + e.Error()
- return
- }
- button := excelService.GetBalanceExcelInfoOpButton(sysUser.AdminId, checkExcelInfo.SysUserId, haveOperaAuth, checkExcelInfo.ExcelInfoId)
- if !button.OpButton {
- br.Msg = "无操作权限"
- br.IsSendEmail = false
- return
- }
- }
- // 获取引用该图表的MyCharts, 用于ES删除
- var myCond string
- var myPars []interface{}
- myCond += ` AND a.chart_info_id = ? `
- myPars = append(myPars, req.ChartInfoId)
- myCharts, e := data_manage.GetMyChartListGroupByCharyInfoIdAndAdminIdByCondition(myCond, myPars)
- if e != nil {
- br.Msg = "删除失败"
- br.ErrMsg = "获取引用图表的MyChats失败, Err: " + e.Error()
- return
- }
- myIds := make([]int, 0)
- for _, m := range myCharts {
- myIds = append(myIds, m.MyChartId)
- }
- //删除图表及关联指标
- e = excel.DeleteBalanceExcelChartInfoAndData(req.ChartInfoId)
- if e != nil {
- br.Msg = "删除失败"
- br.ErrMsg = "删除失败,Err:" + e.Error()
- return
- }
- //删除ES
- {
- go data.EsDeleteChartInfo(req.ChartInfoId)
- // 删除MY ETA 图表 es数据
- //go data.EsDeleteMyChartInfoByChartInfoId(req.ChartInfoId)
- go data.EsDeleteMyChartInfoByMyChartIds(myIds)
- }
- //新增操作日志
- {
- chartLog := new(data_manage.ChartInfoLog)
- chartLog.ChartName = chartInfo.ChartName
- chartLog.ChartInfoId = req.ChartInfoId
- chartLog.ChartClassifyId = chartInfo.ChartClassifyId
- chartLog.SysUserId = sysUser.AdminId
- chartLog.SysUserRealName = sysUser.RealName
- chartLog.UniqueCode = chartInfo.UniqueCode
- chartLog.CreateTime = time.Now()
- chartLog.Content = string(c.Ctx.Input.RequestBody)
- chartLog.Status = "删除图表"
- chartLog.Method = c.Ctx.Input.URI()
- go data_manage.AddChartInfoLog(chartLog)
- }
- }
- br.Ret = 200
- br.Msg = "删除成功"
- br.Success = true
- br.IsAddLog = true
- }
- // BalanceSeasonChartLegendPreview
- // @Title 季节性图例预览接口
- // @Description 季节性图例预览接口
- // @Param request body request.BalanceSeasonChartLegendPreviewReq true "type json string"
- // @Success 200 {object} data_manage.BalanceSeasonChartLegendPreviewResp
- // @router /excel_info/balance/chartLegend/preview [post]
- func (c *ExcelInfoController) BalanceSeasonChartLegendPreview() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req request.BalanceSeasonChartLegendPreviewReq
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if len(req.DateArr) == 0 || len(req.DateArr) == 0 || len(req.DateArr) != len(req.DataArr) {
- br.Msg = "请输入正确的日期和数据"
- return
- }
- newDataMap := make(map[int]float64)
- for i, v := range req.DataArr {
- fv, e := strconv.ParseFloat(v, 64)
- if e != nil {
- br.Msg = "数据格式错误"
- return
- }
- newDataMap[i] = fv
- }
- //组装成excelEdbData
- dataList := make([]*data_manage.EdbDataList, 0)
- for i, v := range req.DateArr {
- dataTime, e := time.ParseInLocation(utils.FormatDate, v, time.Local)
- if e != nil {
- br.Msg = "日期格式错误"
- br.ErrMsg = v + "日期格式错误,Err:" + e.Error()
- return
- }
- timestamp := dataTime.UnixNano() / 1e6
- tmp := &data_manage.EdbDataList{
- EdbDataId: i,
- DataTime: v,
- DataTimestamp: timestamp,
- Value: newDataMap[i],
- }
- dataList = append(dataList, tmp)
- }
- // 对dataList 根据dataTimestamp 进行排序
- sort.Slice(dataList, func(i, j int) bool {
- return dataList[i].DataTimestamp < dataList[j].DataTimestamp
- })
- list, err, errMsg := data.GetBalanceExcelSeasonChartLegendPreview(dataList, req.Calendar, req.SeasonExtraConfig)
- if err != nil {
- if errMsg != "" {
- br.Msg = errMsg
- } else {
- br.Msg = "预览失败"
- }
- br.ErrMsg = err.Error()
- return
- }
- ret := &data_manage.BalanceSeasonChartLegendPreviewResp{List: list}
- br.Data = ret
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // AddStaticExcel
- // @Title 新增静态表
- // @Description 新增静态表
- // @Param request body request.EditExcelInfoReq true "type json string"
- // @Success 200 {object} response.AddExcelInfoResp
- // @router /excel_info/balance/static/add [post]
- func (c *ExcelInfoController) AddStaticExcel() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req request.AddBalanceStaticExcelInfoReq
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.ExcelInfoId <= 0 {
- br.Msg = "请传入ETA表格标识!"
- return
- }
- cacheKey := "CACHE_TABLE_INFO_BALANCE_STATIC_ADD_" + strconv.Itoa(req.ExcelInfoId)
- if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
- br.Msg = "系统处理中,请稍后重试!"
- br.ErrMsg = "系统处理中,请稍后重试!"
- return
- }
- defer func() {
- _ = utils.Rc.Delete(cacheKey)
- }()
- req.VersionName = strings.Trim(req.VersionName, " ")
- if req.VersionName == "" {
- br.Msg = "请填写版本名称!"
- br.IsSendEmail = false
- return
- }
- // 获取原ETA表格信息
- oldExcelInfo, err := excel.GetExcelInfoById(req.ExcelInfoId)
- if err != nil {
- br.Msg = "获取ETA表格失败"
- return
- }
- // 操作权限校验, 增加协作人判断
- {
- // 数据权限
- haveOperaAuth, err := data_manage_permission.CheckExcelPermissionByExcelInfoId(oldExcelInfo.ExcelInfoId, oldExcelInfo.ExcelClassifyId, oldExcelInfo.IsJoinPermission, c.SysUser.AdminId)
- if err != nil {
- br.Msg = "获取ETA表格失败"
- br.ErrMsg = "获取ETA表格权限失败,Err:" + err.Error()
- return
- }
- button := excelService.GetExcelInfoOpButton(sysUser, oldExcelInfo.SysUserId, oldExcelInfo.Source, haveOperaAuth)
- if !button.OpButton {
- br.Msg = "无操作权限"
- br.IsSendEmail = false
- return
- }
- }
- // 查询出每个子表的内容,并将内容转为静态版本
- excelDataMap := make(map[int]map[int][]*data_manage.EdbDataList)
- //查询动态表所有的子表,并复制为静态表
- condition := " AND parent_id = ? AND balance_type = 0 "
- var pars []interface{}
- pars = append(pars, req.ExcelInfoId)
- childExcelList, err := excel.GetExcelInfoListByCondition(condition, pars)
- if err != nil {
- br.Msg = "获取子表失败"
- br.ErrMsg = fmt.Sprintf("获取子表失败 %s", err.Error())
- return
- }
- for k, childExcelInfo := range childExcelList {
- //得到表格数据并落库
- newExcelDataMap, excelAllRows, excelAllCols, e, errMsg := excelService.GetBalanceExcelData(childExcelInfo, c.Lang)
- if e != nil {
- br.Msg = "获取表格数据失败"
- if errMsg != "" {
- br.Msg = errMsg
- }
- br.ErrMsg = "获取表格数据失败,Err:" + e.Error()
- return
- }
- tmpMappingList, e := excel.GetExcelChartEdbMappingByExcelInfoId(childExcelInfo.ExcelInfoId)
- if e != nil {
- br.Msg = "获取图表,指标信息失败"
- br.ErrMsg = fmt.Sprintf(" 获取图表,指标信息失败 Err:%s", err.Error())
- return
- }
- if len(tmpMappingList) > 0 {
- for _, mapping := range tmpMappingList {
- child, ok := excelDataMap[mapping.ChartInfoId]
- if !ok {
- child = make(map[int][]*data_manage.EdbDataList)
- }
- er, msg := excelService.GetBalanceExcelEdbData(mapping, newExcelDataMap, child, excelAllRows, excelAllCols)
- if er != nil {
- utils.FileLog.Info(fmt.Sprintf(" 获取图表,指标信息失败 Err:%s, %s", msg, er.Error()))
- continue
- }
- excelDataMap[mapping.ChartInfoId] = child
- }
- }
- content, e := excelService.TransferBalanceExcelContentToStatic(childExcelInfo, c.Lang)
- if e != nil {
- br.Msg = "动态内容转成静态失败"
- br.ErrMsg = fmt.Sprintf("动态内容转成静态失败 %s", e.Error())
- return
- }
- childExcelList[k].Content = content
- }
- excelInfo, err, errMsg, isSendEmail := data.AddBalanceStaticExcel(oldExcelInfo, oldExcelInfo.ExcelClassifyId, req.VersionName, sysUser, 0, req.ExcelInfoId, 1, childExcelList, true, excelDataMap)
- if err != nil {
- br.Msg = "复制失败"
- if errMsg != `` {
- br.Msg = errMsg
- }
- br.ErrMsg = "复制失败,Err:" + err.Error()
- br.IsSendEmail = isSendEmail
- return
- }
- resp := new(response.AddExcelInfoResp)
- resp.ExcelInfoId = excelInfo.ExcelInfoId
- resp.UniqueCode = excelInfo.UniqueCode
- br.Ret = 200
- br.Success = true
- br.Msg = "保存成功"
- br.Data = resp
- br.IsAddLog = true
- }
- // BalanceVersionList
- // @Title 查询平衡表版本号列表
- // @Description 查询平衡表版本号列表
- // @Param request body request.EditExcelInfoReq true "type json string"
- // @Success 200 {object} response.AddExcelInfoResp
- // @router /excel_info/balance/version [get]
- func (c *ExcelInfoController) BalanceVersionList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- excelInfoId, _ := c.GetInt("ExcelInfoId")
- if excelInfoId <= 0 {
- br.Msg = "请选择表格"
- br.ErrMsg = "ExcelInfoId未传"
- br.IsSendEmail = false
- return
- }
- excelInfo, err := excel.GetExcelInfoById(excelInfoId)
- if err != nil {
- br.Msg = "获取ETA表格失败"
- br.ErrMsg = "获取ETA表格失败,Err:" + err.Error()
- return
- }
- if excelInfo.Source != utils.BALANCE_TABLE {
- br.Msg = "请选择平衡表"
- return
- }
- if excelInfo.BalanceType != 0 && excelInfo.ParentId != 0 {
- br.Msg = "请选择动态平衡表"
- return
- }
- list := make([]*response.BalanceTableVersionListItem, 0)
- list = append(list, &response.BalanceTableVersionListItem{
- ExcelInfoId: excelInfo.ExcelInfoId,
- UniqueCode: excelInfo.UniqueCode,
- BalanceType: excelInfo.BalanceType,
- RelExcelInfoId: excelInfo.RelExcelInfoId,
- VersionName: "动态表", //todo 有个默认的版本名称
- })
- //查询动态表所有的子表,并复制为静态表
- condition := " AND rel_excel_info_id=? AND parent_id = 0 AND balance_type = 1 "
- var pars []interface{}
- pars = append(pars, excelInfoId)
- staticList, err := excel.GetNoContentExcelInfoListByConditionNoPage(condition, pars)
- if err != nil {
- br.Msg = "获取子表失败"
- br.ErrMsg = fmt.Sprintf("获取子表失败 %s", err.Error())
- return
- }
- for _, staticInfo := range staticList {
- tmp := &response.BalanceTableVersionListItem{
- ExcelInfoId: staticInfo.ExcelInfoId,
- UniqueCode: staticInfo.UniqueCode,
- BalanceType: staticInfo.BalanceType,
- RelExcelInfoId: staticInfo.RelExcelInfoId,
- VersionName: staticInfo.VersionName,
- }
- list = append(list, tmp)
- }
- ret := &response.BalanceTableVersionListResp{List: list}
- br.Data = ret
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- // ModifyBalanceExcelVersion
- // @Title 版本号重命名
- // @Description 版本号重命名
- // @Param request body request.AddBalanceStaticExcelInfoReq true "type json string"
- // @Success 200 {object} response.AddExcelInfoResp
- // @router /excel_info/balance/version/modify [post]
- func (c *ExcelInfoController) ModifyBalanceExcelVersion() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req request.AddBalanceStaticExcelInfoReq
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.ExcelInfoId <= 0 {
- br.Msg = "请传入ETA表格标识!"
- return
- }
- req.VersionName = strings.Trim(req.VersionName, " ")
- if req.VersionName == "" {
- br.Msg = "请填写版本名称!"
- br.IsSendEmail = false
- return
- }
- excelInfo, err := excel.GetExcelInfoById(req.ExcelInfoId)
- if err != nil {
- br.Msg = "获取ETA表格失败"
- br.ErrMsg = "获取ETA表格失败,Err:" + err.Error()
- return
- }
- if excelInfo.Source != utils.BALANCE_TABLE {
- br.Msg = "请选择平衡表"
- return
- }
- if excelInfo.BalanceType != 1 && excelInfo.ParentId != 0 {
- br.Msg = "请选择静态表"
- return
- }
- // 操作权限校验, 增加协作人判断
- {
- checkExcelInfo := excelInfo
- if excelInfo.Source == utils.BALANCE_TABLE {
- checkExcelInfoId := excelInfo.ExcelInfoId
- if excelInfo.BalanceType == 1 {
- checkExcelInfoId = excelInfo.RelExcelInfoId
- } else {
- if excelInfo.ParentId > 0 {
- checkExcelInfoId = excelInfo.ParentId
- }
- }
- if checkExcelInfoId != excelInfo.ExcelInfoId {
- checkExcelInfo, err = excel.GetExcelInfoById(checkExcelInfoId)
- if err != nil {
- br.Msg = "获取平衡表格信息失败"
- br.ErrMsg = "获取平衡表格信息失败,Err:" + err.Error()
- return
- }
- }
- }
- // 数据权限
- haveOperaAuth, e := data_manage_permission.CheckExcelPermissionByExcelInfoId(checkExcelInfo.ExcelInfoId, checkExcelInfo.ExcelClassifyId, checkExcelInfo.IsJoinPermission, c.SysUser.AdminId)
- if e != nil {
- br.Msg = "获取ETA表格权限失败"
- br.ErrMsg = "获取表格权限信息失败,Err" + e.Error()
- return
- }
- button := excelService.GetBalanceExcelInfoOpButton(sysUser.AdminId, checkExcelInfo.SysUserId, haveOperaAuth, checkExcelInfo.ExcelInfoId)
- if !button.OpButton {
- br.Msg = "无操作权限"
- br.IsSendEmail = false
- return
- }
- }
- // 检验分类下是否存在该版本号
- {
- var condition string
- var pars []interface{}
- condition += " AND rel_excel_info_id=? AND parent_id=0"
- pars = append(pars, req.ExcelInfoId)
- condition += " AND version_name=? "
- pars = append(pars, req.VersionName)
- count, tmpErr := excel.GetExcelInfoCountByCondition(condition, pars)
- if tmpErr != nil {
- br.Msg = "查询版本名称失败"
- br.ErrMsg = tmpErr.Error()
- return
- }
- if count > 0 {
- br.Msg = "表格版本名称已存在,请重新填写版本名称"
- br.IsSendEmail = false
- return
- }
- }
- excelInfo.ModifyTime = time.Now()
- excelInfo.VersionName = req.VersionName
- updateExcelInfoParams := []string{"ModifyTime", "VersionName"}
- // todo 同步修改静态表中的图表和指标名称
- // ETA表格信息变更
- err = excelInfo.Update(updateExcelInfoParams)
- if err != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "操作失败,Err:" + err.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- br.IsAddLog = true
- }
- func downloadBalanceTable(excelInfo *excel.ExcelInfo, lang string) (savePath, zipName string, uploadDir string, err error, errMsg string) {
- dateDir := time.Now().Format("20060102")
- randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
- uploadDir = "static/xls/" + dateDir + "/" + randStr
- err = os.MkdirAll(uploadDir, utils.DIR_MOD)
- if err != nil {
- return
- }
- fileList := make([]string, 0)
- if excelInfo.ParentId != 0 && excelInfo.BalanceType != 0 {
- errMsg = "平衡表类型错误"
- err = fmt.Errorf("平衡表类型错误 ")
- return
- }
- //复制静态表
- staticCondition := " AND parent_id = 0 AND balance_type = 1 AND rel_excel_info_id=? "
- var staticPars []interface{}
- staticPars = append(staticPars, excelInfo.ExcelInfoId)
- excelList, err := excel.GetExcelInfoListByCondition(staticCondition, staticPars)
- if err != nil {
- errMsg = "获取表格失败"
- err = fmt.Errorf("获取表格失败 %s", err.Error())
- return
- }
- excelList = append(excelList, excelInfo)
- for _, staticExcelInfo := range excelList {
- cCondition := " AND parent_id = ?"
- var cPars []interface{}
- cPars = append(cPars, staticExcelInfo.ExcelInfoId)
- childList, e := excel.GetExcelInfoListByCondition(cCondition, cPars)
- if e != nil {
- errMsg = "获取子表失败"
- err = fmt.Errorf("获取子表失败 %s", err.Error())
- return
- }
- xlsxFile := xlsx.NewFile()
- fileName := staticExcelInfo.ExcelName + ".xlsx"
- fpath := uploadDir + "/" + fileName
- for _, childExcelInfo := range childList {
- var result request.MixedTableReq
- err = json.Unmarshal([]byte(childExcelInfo.Content), &result)
- if err != nil {
- errMsg = "获取失败"
- err = fmt.Errorf("表格json转结构体失败,Err:" + err.Error())
- return
- }
- newResult, er, msg := excelService.GetMixedTableCellData(result, lang)
- if er != nil {
- err = er
- errMsg = msg
- return
- }
- tableData, er := excel2.GetTableDataByMixedTableData(newResult, false)
- if er != nil {
- errMsg = "获取失败"
- err = fmt.Errorf("转换成table失败,Err:" + err.Error())
- return
- }
- // 将单个sheet的数据写入到excel
- err = tableData.WriteExcelSheetData(xlsxFile, childExcelInfo.ExcelName)
- if err != nil {
- return
- }
- }
- //处理excel文件
- //return
- err = xlsxFile.Save(fpath)
- if err != nil {
- return
- }
- fileList = append(fileList, fileName)
- }
- // 创建zip
- zipName = excelInfo.ExcelName + ".zip"
- savePath = uploadDir + "/" + zipName
- fmt.Println(savePath)
- zipFile, err := os.Create(savePath)
- if err != nil {
- return
- }
- zipWriter := zip.NewWriter(zipFile)
- // 生成zip过程中报错关闭
- defer func() {
- if err != nil {
- zipWriter.Close()
- zipFile.Close()
- }
- //os.Remove(savePath)
- }()
- //写入zip
- for i := 0; i < len(fileList); i++ {
- ioWriter, e := zipWriter.Create(fileList[i])
- if e != nil {
- err = fmt.Errorf("创建zip失败,Err:" + e.Error())
- if os.IsPermission(e) {
- fmt.Println("权限不足: ", e)
- return
- }
- return
- }
- fullPath := uploadDir + "/" + fileList[i]
- content, e := ioutil.ReadFile(fullPath)
- if e != nil {
- err = fmt.Errorf("读取文件失败,Err:" + e.Error())
- return
- }
- ioWriter.Write(content)
- }
- // 生成zip后关闭,否则下载文件会损坏
- zipWriter.Close()
- zipFile.Close()
- //this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
- return
- }
- func refreshBalanceTable(excelDetail response.ExcelInfoDetail, lang string) (err error) {
- edbInfoIds := make([]int, 0)
- edbInfoIdExist := make(map[int]bool)
- if excelDetail.ParentId > 0 {
- newResult := excelDetail.TableData.(request.MixedTableReq)
- newData := newResult.Data
- if len(newData) > 0 {
- for _, t := range newData {
- for _, v := range t {
- if v.EdbInfoId > 0 && !edbInfoIdExist[v.EdbInfoId] {
- edbInfoIdExist[v.EdbInfoId] = true
- edbInfoIds = append(edbInfoIds, v.EdbInfoId)
- }
- }
- }
- }
- // 清除缓存
- key := utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL + ":" + excelDetail.UniqueCode
- if utils.Re == nil {
- _ = utils.Rc.Delete(key)
- }
- } else {
- //查询父表和子表
- cCondition := " AND parent_id = ? AND balance_type=0"
- var cPars []interface{}
- cPars = append(cPars, excelDetail.ExcelInfoId)
- childList, e := excel.GetExcelInfoListByCondition(cCondition, cPars)
- if e != nil {
- err = fmt.Errorf("获取子表失败 %s", err.Error())
- return
- }
- // 遍历
- for _, child := range childList {
- var result request.MixedTableReq
- err = json.Unmarshal([]byte(child.Content), &result)
- if err != nil {
- err = errors.New("表格json转结构体失败,Err:" + err.Error())
- return
- }
- var newData [][]request.MixedTableCellDataReq
- newData, err, _ = excelService.GetMixedTableCellData(result, lang)
- if err != nil {
- return
- }
- if len(newData) > 0 {
- for _, t := range newData {
- for _, v := range t {
- if v.EdbInfoId > 0 && !edbInfoIdExist[v.EdbInfoId] {
- edbInfoIdExist[v.EdbInfoId] = true
- edbInfoIds = append(edbInfoIds, v.EdbInfoId)
- }
- }
- }
- }
- // 清除缓存
- key := utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL + ":" + child.UniqueCode
- if utils.Re == nil {
- _ = utils.Rc.Delete(key)
- }
- }
- }
- if len(edbInfoIds) > 0 {
- err, _ = data.EdbInfoRefreshAllFromBaseV3(edbInfoIds, false, true, false)
- if err != nil {
- err = fmt.Errorf("刷新混合表格数据失败, Err: " + err.Error())
- return
- }
- }
- return
- }
- // BalanceChartInfoBaseEdit
- // @Title 编辑图表基础信息接口
- // @Description 编辑图表基础信息接口
- // @Param request body data_manage.EditChartEnInfoBaseReq true "type json string"
- // @Success Ret=200 编辑成功
- // @router /excel_info/balance/chart_base_edit [post]
- func (this *ExcelInfoController) BalanceChartInfoBaseEdit() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req data_manage.EditChartInfoBaseReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- req.ChartName = strings.Trim(req.ChartName, " ")
- if req.ChartInfoId <= 0 {
- br.Msg = "请选择图表"
- return
- }
- //判断指标名称是否存在
- chartItem, err := data_manage.GetChartInfoById(req.ChartInfoId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "图表已被删除,请刷新页面"
- br.ErrMsg = "图表已被删除,请刷新页面"
- return
- }
- br.Msg = "获取图表信息失败"
- br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
- return
- }
- // 判断是否有传入对应的指标配置
- noEdbInfoType := []int{10}
- if len(req.ChartEdbInfoList) <= 0 && !utils.InArrayByInt(noEdbInfoType, chartItem.ChartType) {
- br.Msg = "请选择指标!"
- return
- }
- var edbCondition string
- var edbPars []interface{}
- for _, v := range req.ChartEdbInfoList {
- edbInfoId := v.EdbInfoId
- edbInfo, err := excel.GetExcelChartEdbById(edbInfoId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "图表不存在!"
- br.ErrMsg = "图表指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId)
- return
- } else {
- br.Msg = "获取图表信息失败!"
- br.ErrMsg = "获取图表的指标信息失败,Err:" + err.Error()
- return
- }
- }
- if edbInfo == nil {
- br.Msg = "指标不存在!"
- br.ErrMsg = "指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId)
- return
- }
- //判断指标名称是否重复
- if v.EdbName != "" {
- edbCondition = ""
- edbPars = make([]interface{}, 0)
- edbCondition += " AND excel_chart_edb_id<>? AND chart_info_id=?"
- edbPars = append(edbPars, edbInfo.ExcelChartEdbId, chartItem.ChartInfoId)
- switch this.Lang {
- case utils.EnLangVersion:
- edbCondition += " AND edb_name_en =? "
- default:
- edbCondition += " AND edb_name =? "
- }
- edbPars = append(edbPars, v.EdbName)
- edbExist, err := excel.GetBalanceChartEdbByCondition(edbCondition, edbPars)
- if err != nil {
- if err.Error() != utils.ErrNoRow() {
- br.Msg = "判断英文指标名称是否存在失败"
- br.ErrMsg = "判断英文指标名称是否存在失败,Err:" + err.Error()
- return
- }
- }
- if err == nil && edbExist.ExcelChartEdbId > 0 {
- br.Msg = edbExist.EdbName + ":" + v.EdbName + "指标名称已存在"
- br.ErrMsg = "指标名称已存在,请重新填写"
- br.IsSendEmail = false
- return
- }
- }
- }
- if req.ChartName != "" || req.ExtraConfig != `` {
- var condition string
- var pars []interface{}
- condition += " AND chart_info_id<>? "
- pars = append(pars, req.ChartInfoId)
- switch this.Lang {
- case utils.EnLangVersion:
- condition += " AND chart_name_en = ? "
- default:
- condition += " AND chart_name = ? "
- }
- pars = append(pars, req.ChartName)
- existItem, err := data_manage.GetChartInfoByCondition(condition, pars)
- if err != nil {
- if err.Error() != utils.ErrNoRow() {
- br.Msg = "判断英文图表名称是否存在失败"
- br.ErrMsg = "判断英文图表名称是否存在失败,Err:" + err.Error()
- return
- }
- }
- if err == nil && existItem.ChartInfoId > 0 {
- br.Msg = existItem.ChartName + ":" + req.ChartName + "图表名称已存在"
- return
- }
- }
- err = excel.EditBalanceChartBaseInfoAndEdbEnInfo(&req, chartItem, this.Lang)
- if err != nil {
- br.Msg = "保存失败"
- br.ErrMsg = "保存失败,Err:" + err.Error()
- return
- }
- //添加es数据
- go data.EsAddOrEditChartInfo(chartItem.ChartInfoId)
- //修改my eta es数据
- go data.EsAddOrEditMyChartInfoByChartInfoId(chartItem.ChartInfoId)
- //新增操作日志
- {
- chartLog := new(data_manage.ChartInfoLog)
- chartLog.ChartName = chartItem.ChartName
- chartLog.ChartInfoId = req.ChartInfoId
- chartLog.ChartClassifyId = chartItem.ChartClassifyId
- chartLog.SysUserId = sysUser.AdminId
- chartLog.SysUserRealName = sysUser.RealName
- chartLog.UniqueCode = chartItem.UniqueCode
- chartLog.CreateTime = time.Now()
- chartLog.Content = string(this.Ctx.Input.RequestBody)
- chartLog.Status = "编辑图表英文信息"
- chartLog.Method = this.Ctx.Input.URL()
- go data_manage.AddChartInfoLog(chartLog)
- }
- //清除缓存
- if utils.Re == nil && utils.Rc != nil {
- utils.Rc.Delete(utils.HZ_CHART_LIB_DETAIL + chartItem.UniqueCode) //图表分享链接缓存
- utils.Rc.Delete(data.GetChartInfoDataKey(req.ChartInfoId))
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "编辑成功"
- br.IsAddLog = true
- }
|