123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716 |
- package excel
- import (
- "archive/zip"
- "encoding/json"
- "errors"
- "eta_gn/eta_api/models"
- "eta_gn/eta_api/models/data_manage"
- "eta_gn/eta_api/models/data_manage/excel"
- "eta_gn/eta_api/models/data_manage/excel/request"
- "eta_gn/eta_api/models/data_manage/excel/response"
- "eta_gn/eta_api/models/system"
- "eta_gn/eta_api/services/data"
- "eta_gn/eta_api/services/data/data_manage_permission"
- excelService "eta_gn/eta_api/services/data/excel"
- excel2 "eta_gn/eta_api/services/excel"
- "eta_gn/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)
- }
- 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 utils.IsErrNoRow(err) {
- 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 utils.IsErrNoRow(err) {
- 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 utils.IsErrNoRow(err) {
- 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 utils.IsErrNoRow(err) {
- 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 !utils.IsErrNoRow(err) {
- 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 !utils.IsErrNoRow(err) {
- 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
- }
|