123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372 |
- package excel
- import (
- "archive/zip"
- "encoding/json"
- "eta/eta_api/controllers"
- "eta/eta_api/models"
- "eta/eta_api/models/data_manage"
- excel3 "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/services"
- "eta/eta_api/services/data"
- "eta/eta_api/services/data/data_manage_permission"
- excel2 "eta/eta_api/services/data/excel"
- "eta/eta_api/services/excel"
- "eta/eta_api/utils"
- "fmt"
- "io"
- "os"
- "path/filepath"
- "strconv"
- "strings"
- "time"
- "github.com/rdlucklib/rdluck_tools/paging"
- "github.com/shopspring/decimal"
- "github.com/yidane/formula"
- )
- // ExcelInfoController ETA表格管理
- type ExcelInfoController struct {
- controllers.BaseAuthController
- }
- // Add
- // @Title 新增表格接口
- // @Description 新增表格接口
- // @Param request body request.AddExcelInfoReq true "type json string"
- // @Success 200 {object} response.AddExcelInfoResp
- // @router /excel_info/add [post]
- func (c *ExcelInfoController) Add() {
- 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_TABLE_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 = "系统处理中,请稍后重试!"
- return
- }
- var req request.AddExcelInfoReq
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- req.ExcelName = strings.Trim(req.ExcelName, " ")
- if req.ExcelName == "" {
- br.Msg = "请填写表格名称!"
- br.IsSendEmail = false
- return
- }
- if req.ExcelClassifyId <= 0 && req.ParentId == 0 {
- br.Msg = "分类参数错误!"
- br.IsSendEmail = false
- return
- }
- if req.ExcelClassifyId > 0 {
- excelClassify, e := excel3.GetExcelClassifyById(req.ExcelClassifyId)
- if e != nil {
- if e.Error() == utils.ErrNoRow() {
- br.Msg = "分类不存在"
- br.ErrMsg = "分类不存在"
- br.IsSendEmail = false
- return
- }
- br.Msg = "获取分类信息失败"
- br.ErrMsg = "获取分类信息失败,Err:" + e.Error()
- return
- }
- if excelClassify == nil {
- br.Msg = "分类不存在"
- br.ErrMsg = "分类不存在"
- br.IsSendEmail = false
- return
- }
- }
- if req.ParentId > 0 {
- parentExcelInfo, e := excel3.GetExcelInfoById(req.ParentId)
- if e != nil {
- if e.Error() == utils.ErrNoRow() {
- br.Msg = "父级ETA表格被删除,请刷新页面"
- return
- }
- br.Msg = "获取父级ETA表格信息失败"
- br.ErrMsg = "获取父级ETA表格信息失败,Err:" + e.Error()
- return
- }
- req.ExcelClassifyId = parentExcelInfo.ExcelClassifyId
- }
- var condition string
- var pars []interface{}
- condition += " AND excel_classify_id=? AND parent_id=?"
- pars = append(pars, req.ExcelClassifyId, req.ParentId)
- condition += " AND excel_name=? "
- pars = append(pars, req.ExcelName)
- // 自定义分析私有化了
- if req.Source == utils.CUSTOM_ANALYSIS_TABLE {
- condition += ` AND sys_user_id = ? `
- pars = append(pars, sysUser.AdminId)
- }
- // 获取分类下是否存在该表格名称
- count, err := excel3.GetExcelInfoCountByCondition(condition, pars)
- if err != nil {
- br.Msg = "判断表格名称是否存在失败"
- br.ErrMsg = "判断表格名称是否存在失败,Err:" + err.Error()
- return
- }
- if count > 0 {
- br.Msg = "表格名称已存在,请重新填写表格名称"
- br.IsSendEmail = false
- return
- }
- // 默认的excel类型是1
- if req.Source == 0 {
- req.Source = 1
- }
- content := req.Content
- // 引用的指标id
- edbInfoIdList := make([]int, 0)
- // 自定义表格
- if req.Source == utils.TIME_TABLE {
- jsonStrByte, err := json.Marshal(req.TableData)
- if err != nil {
- br.Msg = "时间序列表格数据获取失败"
- br.ErrMsg = "时间序列表格数据获取失败,Err:" + err.Error()
- return
- }
- var tableData request.TableDataReq
- err = json.Unmarshal(jsonStrByte, &tableData)
- if err != nil {
- br.Msg = "时间序列表格数据获取失败"
- br.ErrMsg = "时间序列表格数据获取失败,Err:" + err.Error()
- return
- }
- tableDataConfig, err := excel2.GetTableDataConfig(tableData)
- if err != nil {
- br.Msg = "时间序列表格数据获取失败"
- br.ErrMsg = "时间序列表格数据获取失败,Err:" + err.Error()
- return
- }
- edbInfoIdList = tableDataConfig.EdbInfoIdList
- tableDataConfig.DecimalConfig = tableData.DecimalConfig
- contentByte, err := json.Marshal(tableDataConfig)
- if err != nil {
- br.Msg = "时间序列表格数据获取失败"
- br.ErrMsg = "时间序列表格数据获取失败,Err:" + err.Error()
- return
- }
- content = string(contentByte)
- }
- // 混合表格
- if req.Source == 3 || req.Source == 5 {
- contentByte, err := json.Marshal(req.TableData)
- if err != nil {
- br.Msg = "自定义表格数据获取失败"
- br.ErrMsg = "自定义表格数据获取后,转json失败,Err:" + err.Error()
- return
- }
- content = string(contentByte)
- var result request.MixedTableReq
- err = json.Unmarshal(contentByte, &result)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "表格json转结构体失败,Err:" + err.Error()
- return
- }
- newResult, err, errMsg := excel2.GetMixedTableCellData(result, c.Lang)
- if err != nil {
- br.Msg = "获取失败"
- if errMsg != `` {
- br.Msg = errMsg
- }
- br.ErrMsg = "获取最新的数据失败,Err:" + err.Error()
- return
- }
- edbInfoIdMap := make(map[int]int)
- for _, tmpV := range newResult {
- for _, v := range tmpV {
- if v.EdbInfoId > 0 {
- if _, ok := edbInfoIdMap[v.EdbInfoId]; !ok {
- edbInfoIdMap[v.EdbInfoId] = v.EdbInfoId
- edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
- }
- }
- }
- }
- }
- //获取该层级下最大的排序数
- maxSort, err := excel2.GetExcelClassifyMaxSort(req.ExcelClassifyId, req.Source)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "查询排序信息失败,Err:" + err.Error()
- return
- }
- timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
- excelInfo := &excel3.ExcelInfo{
- //ExcelInfoId: 0,
- ExcelName: req.ExcelName,
- Source: req.Source,
- ExcelType: req.ExcelType,
- UniqueCode: utils.MD5(utils.EXCEL_DATA_PREFIX + "_" + timestamp),
- ExcelClassifyId: req.ExcelClassifyId,
- SysUserId: sysUser.AdminId,
- SysUserRealName: sysUser.RealName,
- Content: content,
- ExcelImage: req.ExcelImage,
- Sort: maxSort + 1,
- IsDelete: 0,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- ParentId: req.ParentId,
- UpdateUserId: sysUser.AdminId,
- UpdateUserRealName: sysUser.RealName,
- SourcesFrom: req.SourcesFrom,
- }
- excelEdbMappingList := make([]*excel3.ExcelEdbMapping, 0)
- if len(edbInfoIdList) > 0 {
- for _, edbInfoId := range edbInfoIdList {
- excelEdbMappingList = append(excelEdbMappingList, &excel3.ExcelEdbMapping{
- //ExcelEdbMappingId: 0,
- //ExcelInfoId: 0,
- Source: excelInfo.Source,
- EdbInfoId: edbInfoId,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- })
- }
- }
- var childExcel *excel3.ExcelInfo
- if excelInfo.Source == utils.BALANCE_TABLE && req.ParentId == 0 && excelInfo.BalanceType == 0 { //首次创建平衡表时需要添加一个默认的子表
- timestamp = strconv.FormatInt(time.Now().UnixNano(), 10) + "_" + utils.GetRandString(10)
- childExcel = &excel3.ExcelInfo{
- //ExcelInfoId: 0,
- ExcelName: "平衡表",
- Source: excelInfo.Source,
- ExcelType: excelInfo.ExcelType,
- UniqueCode: utils.MD5(utils.EXCEL_DATA_PREFIX + "_" + timestamp),
- ExcelClassifyId: req.ExcelClassifyId,
- SysUserId: sysUser.AdminId,
- SysUserRealName: sysUser.RealName,
- Content: excelInfo.Content,
- //ExcelImage: req.ExcelImage,
- Sort: excelInfo.Sort + 1,
- IsDelete: 0,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- //ParentId: req.ParentId,
- UpdateUserId: sysUser.AdminId,
- UpdateUserRealName: sysUser.RealName,
- }
- }
- err = excel3.AddExcelInfo(excelInfo, excelEdbMappingList, childExcel)
- if err != nil {
- br.Msg = "保存失败"
- br.ErrMsg = "保存失败,Err:" + err.Error()
- return
- }
- // 更新excel下载地址(默认的EXCEL需要更新,自定义表格不需要更新)
- if req.Source == 1 {
- go excel2.UpdateExcelInfoFileUrl(excelInfo)
- }
- _, err = services.UpdateExcelEditMark(excelInfo.ExcelInfoId, sysUser.AdminId, 1, sysUser.RealName)
- if err != nil {
- br.Msg = "查询标记状态失败"
- br.ErrMsg = "查询标记状态失败,Err:" + err.Error()
- return
- }
- resp := new(response.AddExcelInfoResp)
- resp.ExcelInfoId = excelInfo.ExcelInfoId
- resp.UniqueCode = excelInfo.UniqueCode
- //新增指标引用记录
- _ = data.SaveExcelEdbInfoRelation(excelInfo.ExcelInfoId, excelInfo.Source, false)
- //新增操作日志
- //{
- // excelLog := &data_manage.ExcelInfoLog{
- // //ExcelInfoLogId: 0,
- // ExcelInfoId: excelInfo.ExcelInfoId,
- // ExcelName: req.ExcelName,
- // ExcelClassifyId: req.ExcelClassifyId,
- // SysUserId: sysUser.AdminId,
- // SysUserRealName: sysUser.RealName,
- // UniqueCode: excelInfo.UniqueCode,
- // CreateTime: time.Now(),
- // Content: string(c.Ctx.Input.RequestBody),
- // Status: "新增表格",
- // Method: c.Ctx.Input.URI(),
- // }
- // go data_manage.AddExcelInfoLog(excelLog)
- //}
- br.Ret = 200
- br.Success = true
- br.Msg = "保存成功"
- br.Data = resp
- br.IsAddLog = true
- }
- // List
- // @Title ETA表格列表接口
- // @Description ETA表格列表接口
- // @Param PageSize query int true "每页数据条数"
- // @Param CurrentIndex query int true "当前页页码,从1开始"
- // @Param ExcelClassifyId query int true "分类id"
- // @Param Keyword query string true "搜索关键词"
- // @Param AdminId query int false "创建人id"
- // @Param Source query int true "格来源,1:excel插件的表格,2:自定义表格,3:混合表格,默认:1"
- // @Param IsShowMe query bool true "是否只看我的,true、false"
- // @Success 200 {object} response.ExcelListResp
- // @router /excel_info/list [get]
- func (c *ExcelInfoController) List() {
- 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
- }
- excelClassifyId, _ := c.GetInt("ExcelClassifyId")
- pageSize, _ := c.GetInt("PageSize")
- currentIndex, _ := c.GetInt("CurrentIndex")
- keyword := c.GetString("KeyWord")
- adminId, _ := c.GetInt("AdminId")
- source, _ := c.GetInt("Source")
- if keyword == `` { // 兼容前端
- keyword = c.GetString("Keyword")
- }
- var total int
- page := paging.GetPaging(currentIndex, pageSize, total)
- var startSize int
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize = paging.StartIndex(currentIndex, pageSize)
- var condition string
- var pars []interface{}
- // 如果没有传的话,那就是代表前三种表格
- if source <= 0 {
- condition += " AND source in (?,?,?) "
- pars = append(pars, utils.EXCEL_DEFAULT, utils.TIME_TABLE, utils.MIXED_TABLE)
- } else {
- condition += " AND source = ? "
- pars = append(pars, source)
- if source == utils.BALANCE_TABLE { //平衡表的列表只显示动态表的一级表(不显示子表和静态表)
- condition += " AND parent_id = 0 AND balance_type=0 "
- }
- }
- // 筛选分类
- if excelClassifyId > 0 {
- _, err := excel3.GetExcelClassifyById(excelClassifyId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取表格信息失败"
- br.ErrMsg = "获取信息失败,GetExcelClassify,Err:" + err.Error()
- return
- }
- childClassify, e, _ := excel2.GetChildClassifyByClassifyId(excelClassifyId, source)
- if e != nil && e.Error() != utils.ErrNoRow() {
- br.Msg = "获取失败"
- br.ErrMsg = "获取分类信息失败, GetEdbClassify,Err:" + e.Error()
- return
- }
- if len(childClassify) == 0 {
- condition += " AND excel_classify_id = ? "
- pars = append(pars, excelClassifyId)
- } else {
- classifyIds := []int{excelClassifyId}
- for _, v := range childClassify {
- classifyIds = append(classifyIds, v.ExcelClassifyId)
- }
- condition += fmt.Sprintf(` AND excel_classify_id IN (%s) `, utils.GetOrmInReplace(len(classifyIds)))
- pars = append(pars, classifyIds)
- }
- }
- var keyWordArr []string
- if keyword != "" {
- newKeyWord := strings.Split(keyword, " ")
- keywordStr := strings.Replace(keyword, " ", "", -1)
- condition += " AND ( "
- condition += ` excel_name LIKE '%` + keywordStr + `%' OR`
- keyWordArr = append(keyWordArr, newKeyWord...)
- if len(keyWordArr) > 0 {
- for _, v := range keyWordArr {
- if v != "" {
- condition += ` excel_name LIKE '%` + v + `%' OR`
- }
- }
- }
- condition = strings.TrimRight(condition, "OR")
- condition += " ) "
- }
- if adminId > 0 {
- condition += " AND sys_user_id = ? "
- pars = append(pars, adminId)
- }
- //只看我的
- isShowMe, _ := c.GetBool("IsShowMe")
- // 获取所有有权限的指标和分类
- permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserExcelAndClassifyPermissionList(c.SysUser.AdminId, 0, 0)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
- return
- }
- hasCheck := make(map[int]bool)
- if isShowMe {
- if source == utils.BALANCE_TABLE { //平衡表的,显示同时需要显示协作人相关的图表
- //找到当前协作人相关的表格ID
- obj := new(excel3.ExcelWorker)
- existList, err := obj.GetBySysUserId(sysUser.AdminId)
- if err != nil {
- br.Msg = "获取表格协作人失败!"
- br.ErrMsg = "获取表格协作人失败,Err:" + err.Error()
- return
- }
- var excelIds []int
- newCondition := condition
- newPars := pars
- if len(existList) > 0 {
- for _, v := range existList {
- excelIds = append(excelIds, v.ExcelInfoId)
- }
- newCondition += fmt.Sprintf(` AND ( excel_info_id IN (%s) or sys_user_id = ?)`, utils.GetOrmInReplace(len(excelIds)))
- newPars = append(newPars, excelIds, sysUser.AdminId)
- } else {
- newCondition += ` AND sys_user_id = ? `
- newPars = append(newPars, sysUser.AdminId)
- }
- //获取表格信息
- tmpList, e := excel3.GetNoContentExcelListByConditionNoPage(newCondition, newPars)
- if e != nil && e.Error() != utils.ErrNoRow() {
- br.Success = true
- br.Msg = "获取表格信息失败"
- br.ErrMsg = "获取表格信息失败,Err:" + e.Error()
- return
- }
- classifyIdListTmp := make([]int, 0)
- for _, v := range tmpList {
- classifyIdListTmp = append(classifyIdListTmp, v.ExcelClassifyId)
- }
- classifyMap := make(map[int]*excel3.ExcelClassify)
- // 分类信息
- if len(classifyIdListTmp) > 0 {
- classifyListTmp, e := excel3.GetClassifyByIdList(classifyIdListTmp)
- if e != nil {
- br.Msg = "获取表格分类信息失败"
- br.ErrMsg = "获取表格分类列表数据失败,Err:" + e.Error()
- return
- }
- for _, v := range classifyListTmp {
- classifyMap[v.ExcelClassifyId] = v
- }
- }
- excelIds = make([]int, 0)
- for _, v := range tmpList {
- // 数据权限
- if classifyInfo, ok := classifyMap[v.ExcelClassifyId]; ok {
- v.HaveOperaAuth = data_manage_permission.CheckExcelPermissionByPermissionIdList(v.IsJoinPermission, classifyInfo.IsJoinPermission, v.ExcelInfoId, v.ExcelClassifyId, permissionEdbIdList, permissionClassifyIdList)
- if v.HaveOperaAuth {
- excelIds = append(excelIds, v.ExcelInfoId)
- }
- hasCheck[v.ExcelInfoId] = v.HaveOperaAuth
- }
- }
- if len(excelIds) > 0 {
- condition += fmt.Sprintf(` AND excel_info_id IN (%s)`, utils.GetOrmInReplace(len(excelIds)))
- pars = append(pars, excelIds)
- } else {
- list := make([]*excel3.MyExcelInfoList, 0)
- resp := response.ExcelListResp{
- Paging: page,
- List: list,
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- return
- }
- } else {
- condition += " AND sys_user_id = ? "
- pars = append(pars, sysUser.AdminId)
- }
- }
- //获取表格信息
- list, err := excel3.GetNoContentExcelListByCondition(condition, pars, startSize, pageSize)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Success = true
- br.Msg = "获取表格信息失败"
- br.ErrMsg = "获取表格信息失败,Err:" + err.Error()
- return
- }
- lenList := len(list)
- if list == nil || lenList <= 0 || (err != nil && err.Error() == utils.ErrNoRow()) {
- list = make([]*excel3.MyExcelInfoList, 0)
- }
- if lenList > 0 {
- classifyIdList := make([]int, 0)
- for _, v := range list {
- classifyIdList = append(classifyIdList, v.ExcelClassifyId)
- }
- classifyMap := make(map[int]*excel3.ExcelClassify)
- // 分类信息
- {
- classifyList, err := excel3.GetClassifyByIdList(classifyIdList)
- if err != nil {
- br.Msg = "获取表格列表信息失败"
- br.ErrMsg = "获取表格分类列表数据失败,Err:" + err.Error()
- return
- }
- for _, v := range classifyList {
- classifyMap[v.ExcelClassifyId] = v
- }
- }
- for k, v := range list {
- // 数据权限
- if authCheck, ok1 := hasCheck[v.ExcelInfoId]; ok1 {
- v.HaveOperaAuth = authCheck
- } else {
- if classifyInfo, ok := classifyMap[v.ExcelClassifyId]; ok {
- v.HaveOperaAuth = data_manage_permission.CheckExcelPermissionByPermissionIdList(v.IsJoinPermission, classifyInfo.IsJoinPermission, v.ExcelInfoId, v.ExcelClassifyId, permissionEdbIdList, permissionClassifyIdList)
- }
- }
- if v.Source == utils.BALANCE_TABLE {
- //处理按钮权限和编辑状态
- markStatus, err := services.UpdateExcelEditMark(v.ExcelInfoId, sysUser.AdminId, 2, sysUser.RealName)
- if err != nil {
- br.Msg = "查询标记状态失败"
- br.ErrMsg = "查询标记状态失败,Err:" + err.Error()
- return
- }
- if markStatus.Status == 0 {
- list[k].CanEdit = true
- } else {
- list[k].Editor = markStatus.Editor
- }
- // excel表格按钮权限
- list[k].Button = excel2.GetBalanceExcelInfoOpButton(sysUser.AdminId, v.SysUserId, v.HaveOperaAuth, v.ExcelInfoId)
- }
- }
- }
- // 总数据量
- dataCount, err := excel3.GetExcelListCountByCondition(condition, pars)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取表格列表信息失败"
- br.ErrMsg = "获取表格列表数据总数失败,Err:" + err.Error()
- return
- }
- page = paging.GetPaging(currentIndex, pageSize, dataCount)
- resp := response.ExcelListResp{
- Paging: page,
- List: list,
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // Detail
- // @Title 获取表格详情
- // @Description 获取表格详情接口
- // @Param ExcelInfoId query int true "表格id"
- // @Success 200 {object} data_manage.ExcelInfo
- // @router /excel_info/detail [get]
- func (c *ExcelInfoController) Detail() {
- 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
- }
- // 获取数据详情
- excelDetail, errMsg, err := excel2.GetExcelDetailInfoByExcelInfoId(excelInfoId, c.SysUser.AdminId, c.Lang)
- if err != nil {
- br.Msg = errMsg
- br.ErrMsg = err.Error()
- return
- }
- checkExcelInfoId := excelInfoId
- if excelDetail.Source == utils.BALANCE_TABLE {
- if excelDetail.BalanceType == 1 { // 平衡表静态表编辑状态以动态表的编辑状态为准
- checkExcelInfoId = excelDetail.RelExcelInfoId
- } else {
- if excelDetail.ParentId > 0 { // 子表编辑状态以父表的编辑状态为准
- checkExcelInfoId = excelDetail.ParentId
- }
- }
- }
- // 编辑状态
- markStatus, err := services.UpdateExcelEditMark(checkExcelInfoId, sysUser.AdminId, 2, sysUser.RealName)
- if err != nil {
- br.Msg = "查询标记状态失败"
- br.ErrMsg = "查询标记状态失败,Err:" + err.Error()
- return
- }
- if markStatus.Status == 0 {
- excelDetail.CanEdit = true
- } else {
- excelDetail.Editor = markStatus.Editor
- }
- // 图表的指标来源
- if excelDetail.Source == utils.TIME_TABLE {
- jsonStrByte, err := json.Marshal(excelDetail.TableData)
- if err != nil {
- br.Msg = "时间序列表格数据获取失败"
- br.ErrMsg = "时间序列表格数据获取失败,Err:" + err.Error()
- return
- }
- var tableData request.TableDataReq
- err = json.Unmarshal(jsonStrByte, &tableData)
- if err != nil {
- br.Msg = "时间序列表格数据获取失败"
- br.ErrMsg = "时间序列表格数据获取失败,Err:" + err.Error()
- return
- }
- sourceNameList, sourceNameEnList, err := excel2.GetEdbSourceByEdbInfoIdList(tableData.EdbInfoIdList)
- if err != nil {
- br.Msg = "时间序列表格数据获取失败"
- br.ErrMsg = "时间序列表格数据获取失败,Err:" + err.Error()
- return
- }
- excelDetail.ExcelSource = strings.Join(sourceNameList, ",")
- excelDetail.ExcelSourceEn = strings.Join(sourceNameEnList, ",")
- }
- // 数据刷新-混合表格
- if excelDetail.Source == utils.MIXED_TABLE {
- // todo 刷新动态表的所有子表中关联的指标数据
- jsonByte, e := json.Marshal(excelDetail.TableData)
- if e != nil {
- br.Msg = "刷新失败"
- br.ErrMsg = "JSON格式化混合表格数据失败, Err: " + e.Error()
- return
- }
- var tableData request.MixedTableReq
- if e = json.Unmarshal(jsonByte, &tableData); e != nil {
- br.Msg = "刷新失败"
- br.ErrMsg = "解析混合表格数据失败, Err: " + e.Error()
- return
- }
- edbInfoIds := make([]int, 0)
- edbInfoIdExist := make(map[int]bool)
- if len(tableData.Data) > 0 {
- for _, t := range tableData.Data {
- for _, v := range t {
- if v.EdbInfoId > 0 && !edbInfoIdExist[v.EdbInfoId] {
- edbInfoIdExist[v.EdbInfoId] = true
- edbInfoIds = append(edbInfoIds, v.EdbInfoId)
- }
- }
- }
- }
- sourceNameList, sourceNameEnList, err := excel2.GetEdbSourceByEdbInfoIdList(edbInfoIds)
- if err != nil {
- br.Msg = "自定义表格数据获取失败"
- br.ErrMsg = "自定义表格数据获取失败,Err:" + err.Error()
- return
- }
- excelDetail.ExcelSource = strings.Join(sourceNameList, ",")
- excelDetail.ExcelSourceEn = strings.Join(sourceNameEnList, ",")
- }
- // excel表格按钮权限
- if excelDetail.Source != utils.BALANCE_TABLE {
- excelDetail.Button = excel2.GetExcelInfoOpButton(sysUser, excelDetail.SysUserId, excelDetail.Source, excelDetail.HaveOperaAuth)
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = excelDetail
- }
- // Edit
- // @Title 编辑表格接口
- // @Description 新增表格接口
- // @Param request body request.EditExcelInfoReq true "type json string"
- // @Success 200 {object} response.AddExcelInfoResp
- // @router /excel_info/edit [post]
- func (c *ExcelInfoController) Edit() {
- 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
- var req request.EditExcelInfoReq
- 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_EDIT_" + strconv.Itoa(req.ExcelInfoId)
- defer func() {
- if deleteCache {
- _ = utils.Rc.Delete(cacheKey)
- }
- }()
- if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
- deleteCache = false
- br.Msg = "系统处理中,请稍后重试!"
- br.ErrMsg = "系统处理中,请稍后重试!"
- return
- }
- req.ExcelName = strings.Trim(req.ExcelName, " ")
- if req.ExcelName == "" {
- br.Msg = "请填写表格名称!"
- br.IsSendEmail = false
- return
- }
- excelInfo, err := excel3.GetExcelInfoById(req.ExcelInfoId)
- if err != nil {
- br.Msg = "获取ETA表格失败"
- br.ErrMsg = "获取ETA表格失败,Err:" + err.Error()
- return
- }
- if req.ExcelClassifyId <= 0 && excelInfo.ParentId == 0 {
- br.Msg = "分类参数错误!"
- br.IsSendEmail = false
- return
- }
- checkExcelInfoId := excelInfo.ExcelInfoId
- 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 = excel3.GetExcelInfoById(checkExcelInfoId)
- if err != nil {
- br.Msg = "获取平衡表格信息失败"
- br.ErrMsg = "获取平衡表格信息失败,Err:" + err.Error()
- return
- }
- }
- }
- // 标记编辑状态
- markRet, err := services.UpdateExcelEditMark(checkExcelInfoId, sysUser.AdminId, 1, sysUser.RealName)
- if err != nil {
- br.Msg = "查询标记状态失败"
- br.ErrMsg = "查询标记状态失败,Err:" + err.Error()
- return
- }
- if markRet.Status == 1 {
- br.Ret = 200
- br.Success = true
- br.Data = markRet
- return
- }
- if req.ExcelClassifyId > 0 {
- excelClassify, e := excel3.GetExcelClassifyById(req.ExcelClassifyId)
- if e != nil {
- if e.Error() == utils.ErrNoRow() {
- br.Msg = "分类不存在"
- br.ErrMsg = "分类不存在"
- br.IsSendEmail = false
- return
- }
- br.Msg = "获取分类信息失败"
- br.ErrMsg = "获取分类信息失败,Err:" + e.Error()
- return
- }
- if excelClassify == nil {
- br.Msg = "分类不存在"
- br.ErrMsg = "分类不存在"
- br.IsSendEmail = false
- return
- }
- }
- if excelInfo.ParentId > 0 {
- parentExcelInfo, e := excel3.GetExcelInfoById(excelInfo.ParentId)
- if e != nil {
- if e.Error() == utils.ErrNoRow() {
- br.Msg = "父级ETA表格被删除,请刷新页面"
- return
- }
- br.Msg = "获取父级ETA表格信息失败"
- br.ErrMsg = "获取父级ETA表格信息失败,Err:" + e.Error()
- return
- }
- req.ExcelClassifyId = parentExcelInfo.ExcelClassifyId
- }
- //判断表格是否存在
- var condition string
- var pars []interface{}
- condition += " AND excel_info_id != ? "
- pars = append(pars, req.ExcelInfoId)
- condition += " AND excel_classify_id=? AND parent_id=?"
- pars = append(pars, req.ExcelClassifyId, excelInfo.ParentId)
- condition += " AND excel_name=? "
- pars = append(pars, req.ExcelName)
- // 获取分类下是否存在该表格名称
- count, err := excel3.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
- }
- // 操作权限校验
- {
- // 数据权限
- haveOperaAuth, err := data_manage_permission.CheckExcelPermissionByExcelInfoId(checkExcelInfo.ExcelInfoId, checkExcelInfo.ExcelClassifyId, checkExcelInfo.IsJoinPermission, c.SysUser.AdminId)
- if err != nil {
- br.Msg = "获取ETA表格失败"
- br.ErrMsg = "获取ETA表格权限失败,Err:" + err.Error()
- return
- }
- var button excel3.ExcelInfoDetailButton
- if checkExcelInfo.Source == utils.BALANCE_TABLE {
- button = excel2.GetBalanceExcelInfoOpButton(sysUser.AdminId, checkExcelInfo.SysUserId, haveOperaAuth, checkExcelInfo.ExcelInfoId)
- } else {
- button = excel2.GetExcelInfoOpButton(sysUser, excelInfo.SysUserId, excelInfo.Source, haveOperaAuth)
- }
- if !button.OpButton {
- br.Msg = "无操作权限"
- br.IsSendEmail = false
- return
- }
- }
- // 引用的指标id
- edbInfoIdList := make([]int, 0)
- content := req.Content
- contentFlag := false
- if req.Content != excelInfo.Content {
- contentFlag = true
- }
- balanceTableData := make([][]request.MixedTableCellDataReq, 0)
- switch excelInfo.Source {
- case utils.TIME_TABLE: // 自定义表格
- jsonStrByte, err := json.Marshal(req.TableData)
- if err != nil {
- br.Msg = "时间序列表格数据获取失败"
- br.ErrMsg = "时间序列表格数据获取失败,Err:" + err.Error()
- return
- }
- var tableData request.TableDataReq
- err = json.Unmarshal(jsonStrByte, &tableData)
- if err != nil {
- br.Msg = "时间序列表格数据获取失败"
- br.ErrMsg = "时间序列表格数据获取失败,Err:" + err.Error()
- return
- }
- edbInfoIdList = tableData.EdbInfoIdList
- tableDataConfig, err := excel2.GetTableDataConfig(tableData)
- if err != nil {
- br.Msg = "时间序列表格数据获取失败"
- br.ErrMsg = "时间序列表格数据获取失败,Err:" + err.Error()
- return
- }
- tableDataConfig.DecimalConfig = tableData.DecimalConfig
- contentByte, err := json.Marshal(tableDataConfig)
- if err != nil {
- br.Msg = "时间序列表格数据获取失败"
- br.ErrMsg = "时间序列表格数据获取失败,Err:" + err.Error()
- return
- }
- content = string(contentByte)
- case utils.MIXED_TABLE, utils.BALANCE_TABLE: // 混合表格, 平衡表
- contentByte, err := json.Marshal(req.TableData)
- if err != nil {
- br.Msg = "混合表格数据获取失败"
- br.ErrMsg = "混合表格数据获取后,转json失败,Err:" + err.Error()
- return
- }
- content = string(contentByte)
- var result request.MixedTableReq
- err = json.Unmarshal(contentByte, &result)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "表格json转结构体失败,Err:" + err.Error()
- return
- }
- newResult, err, errMsg := excel2.GetMixedTableCellData(result, c.Lang)
- if err != nil {
- br.Msg = "获取失败"
- if errMsg != `` {
- br.Msg = errMsg
- }
- br.ErrMsg = "获取最新的数据失败,Err:" + err.Error()
- return
- }
- balanceTableData = newResult
- edbInfoIdMap := make(map[int]int)
- for _, tmpV := range newResult {
- for _, v := range tmpV {
- if v.EdbInfoId > 0 {
- if _, ok := edbInfoIdMap[v.EdbInfoId]; !ok {
- edbInfoIdMap[v.EdbInfoId] = v.EdbInfoId
- edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
- }
- }
- }
- }
- }
- excelInfo.ModifyTime = time.Now()
- excelInfo.ExcelName = req.ExcelName
- excelInfo.ExcelType = req.ExcelType
- excelInfo.ExcelClassifyId = req.ExcelClassifyId
- excelInfo.ExcelImage = req.ExcelImage
- excelInfo.UpdateUserId = sysUser.AdminId
- excelInfo.UpdateUserRealName = sysUser.RealName
- excelInfo.Content = content
- excelInfo.SourcesFrom = req.SourcesFrom
- // 自动保存时不会传缩略图,也就不更新这个字段
- var updateExcelInfoParams []string
- if req.ExcelImage != "" {
- updateExcelInfoParams = []string{"ModifyTime", "ExcelName", "ExcelType", "ExcelClassifyId", "ExcelImage", "Content", "SourcesFrom"}
- } else {
- updateExcelInfoParams = []string{"ModifyTime", "ExcelName", "ExcelType", "ExcelClassifyId", "Content", "SourcesFrom"}
- }
- excelEdbMappingList := make([]*excel3.ExcelEdbMapping, 0)
- if len(edbInfoIdList) > 0 {
- for _, edbInfoId := range edbInfoIdList {
- excelEdbMappingList = append(excelEdbMappingList, &excel3.ExcelEdbMapping{
- //ExcelEdbMappingId: 0,
- ExcelInfoId: excelInfo.ExcelInfoId,
- Source: excelInfo.Source,
- EdbInfoId: edbInfoId,
- CreateTime: time.Now(),
- ModifyTime: time.Now(),
- })
- }
- }
- err = excel3.EditExcelInfo(excelInfo, updateExcelInfoParams, excelEdbMappingList)
- if err != nil {
- br.Msg = "保存失败"
- br.ErrMsg = "保存失败,Err:" + err.Error()
- return
- }
- // 更新excel下载地址(默认的EXCEL需要更新,自定义表格不需要更新)
- if excelInfo.Source == 1 {
- go excel2.UpdateExcelInfoFileUrl(excelInfo)
- }
- // 更新平衡表图表指标
- if excelInfo.Source == utils.BALANCE_TABLE && excelInfo.BalanceType == 1 && contentFlag == true { //静态表更新表数据
- err = excel2.SyncBalanceEdbData(excelInfo, balanceTableData)
- if err != nil {
- br.Msg = "操作失败"
- br.ErrMsg = err.Error()
- return
- }
- }
- // 加入草稿
- {
- excelDraftInfo := &excel3.ExcelDraft{
- //ExcelDraftId: 0,
- ExcelId: req.ExcelInfoId,
- Name: req.ExcelName,
- Content: content,
- OpUserId: sysUser.AdminId,
- OpUserName: sysUser.RealName,
- CreateTime: time.Now(),
- }
- err = excel3.AddExcelDraft(excelDraftInfo)
- }
- resp := response.AddExcelInfoResp{
- ExcelInfoId: excelInfo.ExcelInfoId,
- UniqueCode: excelInfo.UniqueCode,
- }
- //新增指标引用记录
- _ = data.SaveExcelEdbInfoRelation(excelInfo.ExcelInfoId, excelInfo.Source, false)
- //删除公共图库那边的缓存
- _ = 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
- }
- // Move
- // @Title 移动表格接口
- // @Description 移动表格接口
- // @Param request body request.MoveExcelInfoReq true "type json string"
- // @Success Ret=200 移动成功
- // @router /excel_info/move [post]
- func (c *ExcelInfoController) Move() {
- 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.MoveExcelInfoReq
- 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 = "参数错误"
- br.ErrMsg = "表格id小于等于0"
- br.IsSendEmail = false
- return
- }
- if req.ExcelClassifyId <= 0 {
- br.Msg = "请选择分类"
- br.IsSendEmail = false
- return
- }
- //判断分类是否存在
- _, err = excel3.GetExcelClassifyById(req.ExcelClassifyId)
- if err != nil {
- br.Msg = "移动失败"
- br.ErrMsg = "获取分类信息失败" + err.Error()
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "分类已被删除,不可移动,请刷新页面"
- br.ErrMsg = "分类已被删除,不可移动,请刷新页面"
- br.IsSendEmail = false
- }
- return
- }
- // 获取表格信息
- excelInfo, err := excel3.GetExcelInfoById(req.ExcelInfoId)
- if err != nil {
- br.Msg = "移动失败"
- br.ErrMsg = "获取表格信息失败,Err:" + err.Error()
- return
- }
- //如果改变了分类,那么移动该表格数据
- if excelInfo.ExcelClassifyId != req.ExcelClassifyId {
- //查询需要修改的分类下是否存在同一个表格名称
- tmpExcelInfo, tmpErr := excel3.GetExcelInfoByClassifyIdAndName(req.ExcelClassifyId, excelInfo.ExcelName)
- if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
- br.Msg = "移动失败"
- br.ErrMsg = "移动失败,Err:" + tmpErr.Error()
- return
- }
- if tmpExcelInfo != nil {
- br.Msg = "移动失败,同一个分类下表格名称不允许重复"
- br.ErrMsg = "移动失败,同一个分类下表格名称不允许重复"
- br.IsSendEmail = false
- return
- }
- // 变更表格的所属分类
- excelInfo.ExcelClassifyId = req.ExcelClassifyId
- err = excelInfo.Update([]string{"ExcelClassifyId"})
- if err != nil {
- br.Msg = "移动失败"
- br.ErrMsg = "移动失败,Err:" + err.Error()
- return
- }
- }
- //移动排序
- updateCol := make([]string, 0)
- //如果有传入 上一个兄弟节点分类id
- if req.PrevExcelInfoId > 0 {
- prevExcelInfo, err := excel3.GetExcelInfoById(req.PrevExcelInfoId)
- if err != nil {
- br.Msg = "移动失败"
- br.ErrMsg = "获取上一个兄弟节点分类信息失败,Err:" + err.Error()
- return
- }
- //如果是移动在两个兄弟节点之间
- if req.NextExcelInfoId > 0 {
- //下一个兄弟节点
- nextExcelInfo, err := excel3.GetExcelInfoById(req.NextExcelInfoId)
- if err != nil {
- br.Msg = "移动失败"
- br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error()
- return
- }
- //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
- if prevExcelInfo.Sort == nextExcelInfo.Sort || prevExcelInfo.Sort == excelInfo.Sort {
- //变更兄弟节点的排序
- updateSortStr := `sort + 2`
- _ = excel3.UpdateExcelInfoSortByClassifyId(prevExcelInfo.ExcelClassifyId, prevExcelInfo.Sort, prevExcelInfo.ExcelInfoId, updateSortStr)
- } else {
- //如果下一个兄弟的排序权重正好是上个兄弟节点下一层,那么需要再加一层了
- if nextExcelInfo.Sort-prevExcelInfo.Sort == 1 {
- //变更兄弟节点的排序
- updateSortStr := `sort + 1`
- _ = excel3.UpdateExcelInfoSortByClassifyId(prevExcelInfo.ExcelClassifyId, prevExcelInfo.Sort, prevExcelInfo.ExcelInfoId, updateSortStr, source)
- }
- }
- }
- // 变更表格在当前分类下的排序
- excelInfo.Sort = prevExcelInfo.Sort + 1
- excelInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- } else {
- firstClassify, err := excel3.GetFirstExcelInfoByClassifyId(req.ExcelClassifyId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "移动失败"
- br.ErrMsg = "获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + err.Error()
- return
- }
- //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
- if firstClassify != nil && firstClassify.Sort == 0 {
- updateSortStr := ` sort + 1 `
- _ = excel3.UpdateExcelInfoSortByClassifyId(firstClassify.ExcelClassifyId, 0, firstClassify.ExcelInfoId+1, updateSortStr)
- }
- // 变更表格在当前分类下的排序
- excelInfo.Sort = 0 //那就是排在第一位
- excelInfo.ModifyTime = time.Now()
- updateCol = append(updateCol, "Sort", "ModifyTime")
- }
- //更新
- if len(updateCol) > 0 {
- err = excelInfo.Update(updateCol)
- if err != nil {
- br.Msg = "移动失败"
- br.ErrMsg = "修改失败,Err:" + err.Error()
- return
- }
- }
- if err != nil {
- br.Msg = "移动失败"
- br.ErrMsg = "修改失败,Err:" + err.Error()
- return
- }
- //新增操作日志
- //{
- // ExcelLog := new(data_manage.ExcelInfoLog)
- // ExcelLog.ExcelName = ExcelInfo.ExcelName
- // ExcelLog.ExcelInfoId = req.ExcelInfoId
- // ExcelLog.ExcelClassifyId = ExcelInfo.ExcelClassifyId
- // ExcelLog.SysUserId = sysUser.AdminId
- // ExcelLog.SysUserRealName = sysUser.RealName
- // ExcelLog.UniqueCode = ExcelInfo.UniqueCode
- // ExcelLog.CreateTime = time.Now()
- // ExcelLog.Content = string(c.Ctx.Input.RequestBody)
- // ExcelLog.Status = "移动表格"
- // ExcelLog.Method = c.Ctx.Input.URL()
- // go data_manage.AddExcelInfoLog(ExcelLog)
- //}
- */
- br.Ret = 200
- br.Success = true
- br.IsAddLog = true
- br.Msg = "移动成功"
- }
- // Delete
- // @Title 删除表格接口
- // @Description 删除表格接口
- // @Param request body request.DeleteExcelInfoReq true "type json string"
- // @Success Ret=200 删除成功
- // @router /excel_info/delete [post]
- func (c *ExcelInfoController) Delete() {
- br := new(models.BaseResponse).Init()
- defer func() {
- c.Data["json"] = br
- c.ServeJSON()
- }()
- resp := response.AddExcelInfoResp{}
- sysUser := c.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req request.DeleteExcelInfoReq
- 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 = "参数错误"
- br.ErrMsg = "表格id小于等于0"
- br.IsSendEmail = false
- return
- }
- // 获取表格信息
- excelInfo, err := excel3.GetExcelInfoById(req.ExcelInfoId)
- if err != nil {
- br.Msg = "删除失败"
- br.ErrMsg = "获取表格信息失败,Err:" + err.Error()
- return
- }
- // 删除excel
- err, errMsg, isSendEmail := excel2.Delete(excelInfo, sysUser)
- if err != nil {
- br.Msg = "删除失败"
- if errMsg != `` {
- br.Msg = errMsg
- }
- br.ErrMsg = "删除失败,Err:" + err.Error()
- br.IsSendEmail = isSendEmail
- return
- }
- // 返回下一个表格的信息
- {
- var nextItem *excel3.ExcelInfo
- var condition string
- var pars []interface{}
- condition += " AND excel_classify_id=? AND parent_id=0"
- pars = append(pars, excelInfo.ExcelClassifyId)
- condition += " AND sort>=? "
- pars = append(pars, excelInfo.Sort)
- nextItem, err = excel3.GetNextExcelInfoByCondition(condition, pars)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "删除失败"
- br.ErrMsg = "获取下一级表格信息失败,Err:" + err.Error()
- return
- }
- // 如果没找到,那么查找下一个分类的第一个表格
- if nextItem == nil {
- currClassifyInfo, err := excel3.GetExcelClassifyById(excelInfo.ExcelClassifyId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "删除失败"
- br.ErrMsg = "获取当前表格分类信息失败,Err:" + err.Error()
- return
- }
- //condition += " AND level=3 "
- //pars = append(pars, chartInfo.ExcelClassifyId)
- //var condition string
- //var pars []interface{}
- //condition += " AND sort>=? ORDER BY excel_classify_id ASC LIMIT 1 "
- //pars = append(pars, currClassifyInfo.Sort)
- //
- //classifyItem, err := data_manage.GetNextExcelClassifyByCondition(condition, pars)
- //if err != nil && err.Error() != utils.ErrNoRow() {
- // br.Msg = "删除失败"
- // br.ErrMsg = "获取下一级表格分类信息失败,Err:" + err.Error()
- // return
- //}
- //if classifyItem != nil {
- // nextItem, err = data_manage.GetNextExcelInfo(excelInfo.ExcelClassifyId,)
- // if err != nil && err.Error() != utils.ErrNoRow() {
- // br.Msg = "删除失败"
- // br.ErrMsg = "获取下一级表格信息失败,Err:" + err.Error()
- // return
- // }
- //}
- nextItem, err = excel3.GetNextExcelInfo(excelInfo.ExcelClassifyId, currClassifyInfo.Sort, currClassifyInfo.Source)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "删除失败"
- br.ErrMsg = "获取下一级表格信息失败,Err:" + err.Error()
- return
- }
- }
- // 如果找到下一个表格了,那么就返回
- if nextItem != nil {
- resp = response.AddExcelInfoResp{
- ExcelInfoId: nextItem.ExcelInfoId,
- UniqueCode: nextItem.UniqueCode,
- }
- }
- }
- //删除公共图库那边的缓存
- _ = utils.Rc.Delete(utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL)
- br.Ret = 200
- br.Success = true
- br.IsAddLog = true
- br.Data = resp
- br.Msg = "删除成功"
- }
- // AddDraft
- // @Title 新增表格草稿接口
- // @Description 新增表格草稿接口
- // @Param request body request.AddExcelDraftReq true "type json string"
- // @Success Ret=200 添加成功
- // @router /excel_info/draft/add [post]
- func (c *ExcelInfoController) AddDraft() {
- 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.AddExcelDraftReq
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- req.ExcelName = strings.Trim(req.ExcelName, " ")
- if req.ExcelName == "" {
- br.Msg = "请填写表格名称!"
- br.IsSendEmail = false
- return
- }
- if req.ExcelClassifyId <= 0 {
- br.Msg = "分类参数错误!"
- br.IsSendEmail = false
- return
- }
- excelClassify, err := excel3.GetExcelClassifyById(req.ExcelClassifyId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "分类不存在"
- br.ErrMsg = "分类不存在"
- br.IsSendEmail = false
- return
- }
- br.Msg = "获取分类信息失败"
- br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
- return
- }
- if excelClassify == nil {
- br.Msg = "分类不存在"
- br.ErrMsg = "分类不存在"
- br.IsSendEmail = false
- return
- }
- var condition string
- var pars []interface{}
- condition += " AND excel_classify_id=? "
- pars = append(pars, req.ExcelClassifyId)
- condition += " AND excel_name=? "
- pars = append(pars, req.ExcelName)
- // 获取分类下是否存在该表格名称
- _, err = excel3.GetExcelInfoById(req.ExcelInfoId)
- if err != nil {
- br.Msg = "判断表格名称是否存在失败"
- br.ErrMsg = "判断表格名称是否存在失败,Err:" + err.Error()
- return
- }
- excelDraftInfo := &excel3.ExcelDraft{
- //ExcelDraftId: 0,
- ExcelId: req.ExcelInfoId,
- Name: req.ExcelName,
- Content: req.Content,
- OpUserId: sysUser.AdminId,
- OpUserName: sysUser.RealName,
- CreateTime: time.Now(),
- }
- err = excel3.AddExcelDraft(excelDraftInfo)
- if err != nil {
- br.Msg = "保存失败"
- br.ErrMsg = "保存失败,Err:" + err.Error()
- return
- }
- _, err = services.UpdateExcelEditMark(req.ExcelInfoId, sysUser.AdminId, 1, sysUser.RealName)
- if err != nil {
- br.Msg = "查询标记状态失败"
- br.ErrMsg = "查询标记状态失败,Err:" + err.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "添加成功"
- }
- // GetExcelTableData
- // @Title 获取excel表格的table数据
- // @Description 获取excel表格的table数据接口
- // @Param UniqueCode query string true "表格code"
- // @Param FromScene query int true "场景来源,1:智能研报,2:研报列表;3:英文研报;4:中文PPT;5:英文PPT"
- // @Success 200 {object} response.ExcelTableDetailResp
- // @router /excel_info/table_data [get]
- func (c *ExcelInfoController) GetExcelTableData() {
- 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
- }
- uniqueCode := c.GetString("UniqueCode")
- fromScene, _ := c.GetInt("FromScene", 0)
- var err error
- if uniqueCode == `` {
- br.Msg = "请选择表格"
- br.ErrMsg = "UniqueCode未传"
- br.IsSendEmail = false
- return
- }
- //获取eta表格信息
- excelInfo, err := excel3.GetExcelInfoByUniqueCode(uniqueCode)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取ETA表格信息失败,Err:" + err.Error()
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "ETA表格被删除,请刷新页面"
- br.ErrMsg = "ETA表格被删除,请刷新页面,Err:" + err.Error()
- br.IsSendEmail = false
- }
- return
- }
- var tableData excel.TableData
- var excelSource, excelSourceEn string
- switch excelInfo.Source {
- case utils.EXCEL_DEFAULT:
- luckySheetData, err := excel.GetLuckySheetData(excelInfo.Content)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取excel数据失败,Err:" + err.Error()
- return
- }
- tableData, err = luckySheetData.GetTableDataByLuckySheetDataStr(true)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "转换成table失败,Err:" + err.Error()
- return
- }
- case utils.TIME_TABLE:
- var tableDataConfig excel2.TableDataConfig
- err = json.Unmarshal([]byte(excelInfo.Content), &tableDataConfig)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "表格json转结构体失败,Err:" + err.Error()
- return
- }
- result, err := excel2.GetDataByTableDataConfig(tableDataConfig)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取最新的表格数据失败,Err:" + err.Error()
- return
- }
- tableData, err = excel.GetTableDataByCustomData(excelInfo.ExcelType, result, c.Lang)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "转换成table失败,Err:" + err.Error()
- return
- }
- sourceNameList, sourceNameEnList, err := excel2.GetEdbSourceByEdbInfoIdList(result.EdbInfoIdList)
- if err != nil {
- br.Msg = "自定义表格数据获取失败"
- br.ErrMsg = "自定义表格数据获取失败,Err:" + err.Error()
- return
- }
- excelSource = strings.Join(sourceNameList, ",")
- excelSourceEn = strings.Join(sourceNameEnList, ",")
- case utils.MIXED_TABLE:
- var result request.MixedTableReq
- err = json.Unmarshal([]byte(excelInfo.Content), &result)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "表格json转结构体失败,Err:" + err.Error()
- return
- }
- newResult, err, errMsg := excel2.GetMixedTableCellData(result, c.Lang)
- if err != nil {
- br.Msg = "获取失败"
- if errMsg != `` {
- br.Msg = errMsg
- }
- br.ErrMsg = "获取最新的数据失败,Err:" + err.Error()
- return
- }
- tableData, err = excel.GetTableDataByMixedTableData(newResult, true, excelInfo.ExcelInfoId)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "转换成table失败,Err:" + err.Error()
- return
- }
- edbInfoIds := make([]int, 0)
- edbInfoIdExist := make(map[int]bool)
- if len(newResult) > 0 {
- for _, t := range newResult {
- for _, v := range t {
- if v.EdbInfoId > 0 && !edbInfoIdExist[v.EdbInfoId] {
- edbInfoIdExist[v.EdbInfoId] = true
- edbInfoIds = append(edbInfoIds, v.EdbInfoId)
- }
- }
- }
- }
- sourceNameList, sourceNameEnList, err := excel2.GetEdbSourceByEdbInfoIdList(edbInfoIds)
- if err != nil {
- br.Msg = "自定义表格数据获取失败"
- br.ErrMsg = "自定义表格数据获取失败,Err:" + err.Error()
- return
- }
- excelSource = strings.Join(sourceNameList, ",")
- excelSourceEn = strings.Join(sourceNameEnList, ",")
- }
- tableData = excel.HandleTableCell(tableData)
- // tableData, err = excel.HandleRuleToTableCell(excelInfo.ExcelInfoId, tableData)
- if err != nil {
- utils.FileLog.Info("表格管理规则处理失败,HandleRuleToTableCell err:", err.Error())
- }
- config := response.ExcelTableDetailConfigResp{
- FontSize: 9,
- }
- // 获取配置的字体大小
- confName := models.FromSceneMap[fromScene]
- if confName != `` {
- busConf, err := models.GetBusinessConfByKey(confName)
- if err == nil {
- sizeDeci, err := decimal.NewFromString(busConf.ConfVal)
- if err == nil {
- config.FontSize = int(sizeDeci.IntPart())
- }
- }
- }
- resp := response.ExcelTableDetailResp{
- UniqueCode: excelInfo.UniqueCode,
- ExcelImage: excelInfo.ExcelImage,
- ExcelName: excelInfo.ExcelName,
- TableInfo: tableData,
- Config: config,
- SourcesFrom: excelInfo.SourcesFrom,
- ExcelSource: excelSource,
- ExcelSourceEn: excelSourceEn,
- ExcelInfoId: excelInfo.ExcelInfoId,
- Source: excelInfo.Source,
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- //func init() {
- //
- // excelInfo, err := data_manage.GetExcelInfoById(52)
- // if err != nil {
- // fmt.Println("err:", err)
- // return
- // }
- //
- // luckySheetData, err := excel.GetLuckySheetData(excelInfo.Content)
- // if err != nil {
- // fmt.Println("err:", err)
- // return
- // }
- // //_, err = luckySheetData.GetTableDataByLuckySheetDataStr()
- // downloadFilePath, err := luckySheetData.ToExcel()
- // if err != nil {
- // fmt.Println("err:", err)
- // return
- // }
- // fmt.Println(downloadFilePath)
- //
- // //fmt.Println(excel.GetColumnName(988, 283))
- //}
- // Calculate
- // @Title 公式计算接口
- // @Description 公式计算接口
- // @Param request body request.CalculateReq true "type json string"
- // @router /excel_info/table/calculate [post]
- func (c *ExcelInfoController) Calculate() {
- 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.CalculateReq
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.CalculateFormula == "" {
- br.Msg = "请填写公式"
- return
- }
- calculateFormula := req.CalculateFormula
- calculateFormula = strings.TrimPrefix(calculateFormula, "=")
- calculateFormula = strings.Replace(calculateFormula, "(", "(", -1)
- calculateFormula = strings.Replace(calculateFormula, ")", ")", -1)
- calculateFormula = strings.Replace(calculateFormula, ",", ",", -1)
- calculateFormula = strings.Replace(calculateFormula, "。", ".", -1)
- calculateFormula = strings.Replace(calculateFormula, "%", "*0.01", -1)
- formulaFormStr := utils.ReplaceFormula(req.TagMap, calculateFormula)
- //计算公式异常,那么就移除该指标
- if formulaFormStr == `` {
- //removeDateList = append(removeDateList, sk)
- //fmt.Println("异常了")
- br.Msg = "公式异常"
- br.ErrMsg = "公式异常"
- return
- }
- expression := formula.NewExpression(formulaFormStr)
- calResult, err := expression.Evaluate()
- if err != nil {
- br.Msg = "计算失败"
- br.ErrMsg = "计算失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr
- // 分母为0的报错
- if strings.Contains(err.Error(), "divide by zero") {
- br.Msg = "分母不能为0"
- br.ErrMsg = "分母不能为空,计算公式:" + formulaFormStr
- br.IsSendEmail = false
- }
- return
- }
- // 如果计算结果是NAN,那么就提示报错
- if calResult.IsNan() {
- br.Msg = "计算失败"
- br.ErrMsg = "计算失败:计算结果是:NAN;formulaStr:" + formulaFormStr
- return
- }
- // 计算结果格式化
- calFloat, e := calResult.Float64()
- if e != nil {
- br.Msg = "计算失败"
- br.ErrMsg = "计算失败, Err: " + e.Error()
- return
- }
- calFloat, _ = decimal.NewFromFloat(calFloat).Round(4).Float64()
- calVal := utils.FormatMixTableDataShowValue(calFloat)
- //calVal := calResult.String()
- //if err != nil {
- // br.Msg = "计算失败"
- // br.ErrMsg = "计算失败,结果转 string 失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr
- // return
- //}
- br.Ret = 200
- br.Success = true
- br.Msg = "计算成功"
- br.Data = calVal
- }
- // GetFirstEdbData
- // @Title 获取未来日期数据接口
- // @Description 获取未来日期数据接口
- // @Param EdbInfoId query int true "指标id"
- // @Param Num query int true "默认期数"
- // @Param SortType query string true "如何排序,是正序还是倒序,枚举值:`asc 正序`,`desc 倒序`,不传默认倒序"
- // @router /excel_info/table/first_edb_data_list [get]
- func (c *ExcelInfoController) GetFirstEdbData() {
- 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
- }
- edbInfoId, _ := c.GetInt("EdbInfoId")
- num, _ := c.GetInt("Num")
- sortType := c.GetString("SortType")
- if edbInfoId <= 0 {
- br.Msg = "请选择指标"
- br.IsSendEmail = false
- return
- }
- if num <= 0 {
- br.Msg = "期数必须大于0"
- br.IsSendEmail = false
- return
- }
- edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = fmt.Sprint("获取指标信息失败,Err:", err.Error())
- return
- }
- dataList, err := excel2.GetFirstEdbDataList(edbInfo, num, []string{}, -1)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = fmt.Sprint("获取失败,Err:", err.Error())
- return
- }
- // 默认是倒序返回的数据,如果要正序的话,那么翻转下就好了
- if sortType == "asc" {
- for i, j := 0, len(dataList)-1; i < j; i, j = i+1, j-1 {
- dataList[i], dataList[j] = dataList[j], dataList[i]
- }
- }
- sourceNameList, sourceNameEnList, err := excel2.GetEdbSourceByEdbInfoIdList([]int{edbInfoId})
- if err != nil {
- br.Msg = "自定义表格数据获取失败"
- br.ErrMsg = "自定义表格数据获取失败,Err:" + err.Error()
- return
- }
- excelSource := strings.Join(sourceNameList, ",")
- excelSourceEn := strings.Join(sourceNameEnList, ",")
- if len(dataList) == 0 {
- br.Msg = "指标数据异常,请检查"
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = response.TableDataItem{
- EdbInfoId: edbInfoId,
- Data: dataList,
- Decimal: -1,
- ExcelSource: excelSource,
- ExcelSourceEn: excelSourceEn,
- }
- }
- // GetOtherEdbData
- // @Title 获取指定日期数据接口
- // @Description 获取未来日期数据接口
- // @Param request body request.GetOtherEdbDateDataReq true "type json string"
- // @router /excel_info/table/other_edb_data_list [post]
- func (c *ExcelInfoController) GetOtherEdbData() {
- 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.GetOtherEdbDateDataReq
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.EdbInfoId <= 0 {
- br.Msg = "请选择指标"
- br.IsSendEmail = false
- return
- }
- if len(req.DateList) <= 0 {
- br.Msg = "请选择日期"
- br.IsSendEmail = false
- return
- }
- edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = fmt.Sprint("获取指标信息失败,Err:", err.Error())
- return
- }
- dataList, err := excel2.GetOtherEdbDataListFollowDate(edbInfo, req.DateList, req.DateDecimal)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = fmt.Sprint("获取失败,Err:", err.Error())
- return
- }
- // 默认是倒序返回的数据,如果要正序的话,那么翻转下就好了
- if req.SortType == "asc" {
- for i, j := 0, len(dataList)-1; i < j; i, j = i+1, j-1 {
- dataList[i], dataList[j] = dataList[j], dataList[i]
- }
- }
- sourceNameList, sourceNameEnList, err := excel2.GetEdbSourceByEdbInfoIdList([]int{req.EdbInfoId})
- if err != nil {
- br.Msg = "自定义表格数据获取失败"
- br.ErrMsg = "自定义表格数据获取失败,Err:" + err.Error()
- return
- }
- excelSource := strings.Join(sourceNameList, ",")
- excelSourceEn := strings.Join(sourceNameEnList, ",")
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = response.TableDataItem{
- EdbInfoId: req.EdbInfoId,
- Data: dataList,
- Decimal: -1,
- ExcelSource: excelSource,
- ExcelSourceEn: excelSourceEn,
- }
- }
- // GetBatchEdbData
- // @Title 批量获取指定日期数据接口
- // @Description 批量获取未来日期数据接口
- // @Param request body request.GetBatchEdbDateDataReq true "type json string"
- // @router /excel_info/table/batch_edb_data_list [post]
- func (c *ExcelInfoController) GetBatchEdbData() {
- 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.GetBatchEdbDateDataReq
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if len(req.EdbInfoId) == 0 {
- br.Msg = "请选择指标"
- br.IsSendEmail = false
- return
- }
- tableList := make([]*response.TableDataItem, 0)
- var firstDataList []string
- for i, v := range req.EdbInfoId {
- if req.Num <= 0 {
- br.Msg = "期数必须大于0"
- br.IsSendEmail = false
- return
- }
- edbInfo, err := data_manage.GetEdbInfoById(v)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = fmt.Sprint("获取指标信息失败,Err:", err.Error())
- return
- }
- if len(req.DateList) == 0 {
- req.Num = 12
- }
- if len(firstDataList) == 0 {
- firstDataList = req.DateList
- }
- if len(firstDataList) != 0 && len(firstDataList) != req.Num {
- req.Num = len(firstDataList)
- }
- if i == 0 {
- dataList, err := excel2.GetFirstEdbDataList(edbInfo, req.Num, firstDataList, req.Decimal[i])
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = fmt.Sprint("获取失败,Err:", err.Error())
- return
- }
- // 默认是倒序返回的数据,如果要正序的话,那么翻转下就好了
- if req.SortType == "asc" {
- for i, j := 0, len(dataList)-1; i < j; i, j = i+1, j-1 {
- dataList[i], dataList[j] = dataList[j], dataList[i]
- }
- }
- dataList = excel2.PadFirstEdbDataList(dataList, req.DateList, req.SortType, req.Decimal[i])
- sourceNameList, sourceNameEnList, err := excel2.GetEdbSourceByEdbInfoIdList([]int{v})
- if err != nil {
- br.Msg = "时间序列表格数据获取失败"
- br.ErrMsg = "时间序列表格数据获取失败,Err:" + err.Error()
- return
- }
- excelSource := strings.Join(sourceNameList, ",")
- excelSourceEn := strings.Join(sourceNameEnList, ",")
- if len(dataList) == 0 {
- br.Msg = "指标数据异常,请检查"
- return
- }
- tableList = append(tableList, &response.TableDataItem{
- EdbInfoId: v,
- Data: dataList,
- Decimal: req.Decimal[i],
- ExcelSource: excelSource,
- ExcelSourceEn: excelSourceEn,
- })
- if len(firstDataList) == 0 {
- tmpDataList := make([]string, 0)
- for _, v := range dataList {
- tmpDataList = append(tmpDataList, v.DataTime)
- }
- firstDataList = tmpDataList
- }
- } else {
- dataList, err := excel2.GetOtherEdbDataList(edbInfo, firstDataList, req.Decimal[i])
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = fmt.Sprint("获取失败,Err:", err.Error())
- return
- }
- // 默认是倒序返回的数据,如果要正序的话,那么翻转下就好了
- if req.SortType == "asc" {
- for i, j := 0, len(dataList)-1; i < j; i, j = i+1, j-1 {
- dataList[i], dataList[j] = dataList[j], dataList[i]
- }
- }
- sourceNameList, sourceNameEnList, err := excel2.GetEdbSourceByEdbInfoIdList([]int{v})
- if err != nil {
- br.Msg = "自定义表格数据获取失败"
- br.ErrMsg = "自定义表格数据获取失败,Err:" + err.Error()
- return
- }
- excelSource := strings.Join(sourceNameList, ",")
- excelSourceEn := strings.Join(sourceNameEnList, ",")
- tableList = append(tableList, &response.TableDataItem{
- EdbInfoId: v,
- Decimal: req.Decimal[i],
- Data: dataList,
- ExcelSource: excelSource,
- ExcelSourceEn: excelSourceEn,
- })
- }
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = tableList
- }
- // GetFutureDateData
- // @Title 获取未来日期数据接口
- // @Description 获取未来日期数据接口
- // @Param request body request.GetFutureDateDataReq true "type json string"
- // @router /excel_info/table/future_date_list [post]
- func (c *ExcelInfoController) GetFutureDateData() {
- 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.GetFutureDateDataReq
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.StartDate == `` {
- br.Msg = "开始日期未传"
- br.IsSendEmail = false
- return
- }
- startDate, err := time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local)
- if err != nil {
- br.Msg = "开始日期异常"
- br.ErrMsg = fmt.Sprint("开始日期异常,startDate:", req.StartDate, ";err:", err.Error())
- return
- }
- if len(req.EdbInfoIdList) <= 0 {
- br.Msg = "请选择指标"
- br.IsSendEmail = false
- return
- }
- baseEdbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoIdList[0])
- if err != nil {
- br.Msg = "查找指标失败"
- br.Msg = "查找指标失败,err:" + err.Error()
- return
- }
- // 第一个指标的数据列表
- var firstEdbDataList []*data_manage.EdbDataList
- {
- switch baseEdbInfo.EdbInfoType {
- case 0:
- firstEdbDataList, err = data_manage.GetEdbDataList(baseEdbInfo.Source, baseEdbInfo.SubSource, baseEdbInfo.EdbInfoId, req.StartDate, ``)
- case 1:
- _, firstEdbDataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(baseEdbInfo.EdbInfoId, req.StartDate, ``, false)
- default:
- br.Msg = "指标类型异常"
- br.ErrMsg = fmt.Sprint("获取失败,指标类型异常", baseEdbInfo.EdbInfoType)
- br.IsSendEmail = false
- return
- }
- }
- dateList := make([]time.Time, 0)
- if req.DateType == 1 {
- num := req.Num
- i := 0 // 数据中有的日期数
- for _, v := range firstEdbDataList {
- tmpDate, err := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
- if err != nil {
- br.Msg = "结束日期异常"
- br.ErrMsg = fmt.Sprint("结束日期异常,startDate:", req.EndDate, ";err:", err.Error())
- return
- }
- if tmpDate.After(startDate) {
- dateList = append(dateList, tmpDate)
- startDate = tmpDate
- i++
- }
- if i >= num {
- break
- }
- }
- num = num - i
- if num > 0 {
- tmpDateList := utils.GetPredictEdbDayListByNum(startDate, num, baseEdbInfo.Frequency)
- dateList = append(dateList, tmpDateList...)
- }
- } else {
- if req.EndDate == `` {
- br.Msg = "结束日期未传"
- br.IsSendEmail = false
- return
- }
- endDate, err := time.ParseInLocation(utils.FormatDate, req.EndDate, time.Local)
- if err != nil {
- br.Msg = "结束日期异常"
- br.ErrMsg = fmt.Sprint("结束日期异常,startDate:", req.EndDate, ";err:", err.Error())
- return
- }
- for _, v := range firstEdbDataList {
- tmpDate, err := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
- if err != nil {
- br.Msg = "结束日期异常"
- br.ErrMsg = fmt.Sprint("结束日期异常,startDate:", req.EndDate, ";err:", err.Error())
- return
- }
- if tmpDate.Equal(endDate) || (tmpDate.Before(endDate) && tmpDate.After(startDate)) {
- dateList = append(dateList, tmpDate)
- startDate = tmpDate
- }
- }
- if startDate.Before(endDate) {
- tmpDateList := utils.GetPredictEdbDayListByEndDate(startDate, endDate, baseEdbInfo.Frequency)
- dateList = append(dateList, tmpDateList...)
- }
- }
- // 获取所有的指标信息
- edbInfoMap := make(map[int]*data_manage.EdbInfo)
- {
- edbInfoList, err := data_manage.GetEdbInfoByIdList(req.EdbInfoIdList)
- if err != nil {
- br.Msg = "获取指标信息失败"
- br.ErrMsg = "获取指标信息失败,err:" + err.Error()
- return
- }
- for _, v := range edbInfoList {
- edbInfoMap[v.EdbInfoId] = v
- }
- }
- result := make([]response.TableDataItem, 0)
- dateStrList := make([]string, 0)
- lenDate := len(dateList)
- if lenDate > 0 {
- for i := lenDate - 1; i >= 0; i-- {
- dateStrList = append(dateStrList, dateList[i].Format(utils.FormatDate))
- }
- for j, v := range req.EdbInfoIdList {
- tmpEdbInfo, ok := edbInfoMap[v]
- if !ok {
- br.Msg = "获取指标信息失败"
- br.ErrMsg = "获取指标信息失败,err:" + err.Error()
- return
- }
- dataList, err := excel2.GetOtherEdbDataList(tmpEdbInfo, dateStrList, req.Decimal[j])
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = fmt.Sprint("获取失败,Err:", err.Error())
- return
- }
- result = append(result, response.TableDataItem{
- EdbInfoId: v,
- Decimal: req.Decimal[j],
- Data: dataList,
- })
- }
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = result
- }
- // GetHistoryDateData
- // @Title 获取历史日期数据接口
- // @Description 获取历史日期数据接口
- // @Param request body request.GetFutureDateDataReq true "type json string"
- // @router /excel_info/table/history_date_list [post]
- func (c *ExcelInfoController) GetHistoryDateData() {
- 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.GetHistoryDateDataReq
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.EndDate == `` {
- br.Msg = "结束日期未传"
- br.IsSendEmail = false
- return
- }
- if len(req.EdbInfoIdList) <= 0 {
- br.Msg = "请选择指标"
- br.IsSendEmail = false
- return
- }
- // 获取所有的指标信息
- edbInfoMap := make(map[int]*data_manage.EdbInfo)
- {
- edbInfoList, err := data_manage.GetEdbInfoByIdList(req.EdbInfoIdList)
- if err != nil {
- br.Msg = "获取指标信息失败"
- br.ErrMsg = "获取指标信息失败,err:" + err.Error()
- return
- }
- for _, v := range edbInfoList {
- edbInfoMap[v.EdbInfoId] = v
- }
- }
- result := make([]response.TableDataItem, 0)
- dateStrList := make([]string, 0)
- // 获取A列指标的历史数据
- {
- tmpEdbInfo, ok := edbInfoMap[req.EdbInfoIdList[0]]
- if !ok {
- br.Msg = "获取指标信息失败"
- br.ErrMsg = "获取指标信息失败,err:" + err.Error()
- return
- }
- firstDataList, err := excel2.GetFirstHistoryEdbDataList(tmpEdbInfo, req.Num, req.EndDate, req.Decimal[0])
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = fmt.Sprint("获取失败,Err:", err.Error())
- return
- }
- result = append(result, response.TableDataItem{
- EdbInfoId: req.EdbInfoIdList[0],
- Decimal: req.Decimal[0],
- Data: firstDataList,
- })
- for _, v := range firstDataList {
- dateStrList = append(dateStrList, v.DataTime)
- }
- }
- if len(dateStrList) > 0 {
- for k, v := range req.EdbInfoIdList {
- if k == 0 {
- continue
- }
- tmpEdbInfo, ok := edbInfoMap[v]
- if !ok {
- br.Msg = "获取指标信息失败"
- br.ErrMsg = "获取指标信息失败,err:" + err.Error()
- return
- }
- dataList, err := excel2.GetOtherEdbDataList(tmpEdbInfo, dateStrList, req.Decimal[k])
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = fmt.Sprint("获取失败,Err:", err.Error())
- return
- }
- result = append(result, response.TableDataItem{
- EdbInfoId: v,
- Decimal: req.Decimal[k],
- Data: dataList,
- })
- }
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = result
- }
- // Refresh
- // @Title 刷新
- // @Description 刷新接口
- // @Param ExcelInfoId query int true "表格id"
- // @router /excel_info/table/refresh [get]
- func (c *ExcelInfoController) Refresh() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- 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")
- chartInfoId, _ := c.GetInt("ChartInfoId")
- if excelInfoId <= 0 && chartInfoId <= 0 {
- br.Msg = "请选择表格"
- br.ErrMsg = "ExcelInfoId未传"
- br.IsSendEmail = false
- return
- }
- // todo 如果请求入参是chart_info_id,则需要获取excel_info_id
- if chartInfoId > 0 && excelInfoId == 0 {
- excelInfo, err := excel3.GetExcelInfoByChartInfoId(chartInfoId)
- if err != nil {
- br.Msg = "请选择表格"
- br.ErrMsg = "未找到对应的表格"
- br.IsSendEmail = false
- return
- }
- excelInfoId = excelInfo.ExcelInfoId
- }
- // 获取数据详情
- excelDetail, errMsg, err := excel2.GetExcelDetailInfoByExcelInfoId(excelInfoId, c.SysUser.AdminId, c.Lang)
- if err != nil {
- br.Msg = errMsg
- br.ErrMsg = err.Error()
- return
- }
- // 操作权限校验
- {
- button := excelDetail.Button
- if excelDetail.Source != utils.BALANCE_TABLE {
- button = excel2.GetExcelInfoOpButton(sysUser, excelDetail.SysUserId, excelDetail.Source, excelDetail.HaveOperaAuth)
- }
- if !button.RefreshButton {
- br.Msg = "无操作权限"
- br.IsSendEmail = false
- return
- }
- }
- // 数据刷新(只有自定义表格有刷新)
- if excelDetail.Source == utils.TIME_TABLE {
- jsonStrByte, err := json.Marshal(excelDetail.TableData)
- if err != nil {
- br.Msg = "自定义表格数据获取失败"
- br.ErrMsg = "自定义表格数据获取失败,转json失败,Err:" + err.Error()
- return
- }
- var tableData request.TableDataReq
- err = json.Unmarshal(jsonStrByte, &tableData)
- if err != nil {
- br.Msg = "自定义表格数据获取失败"
- br.ErrMsg = "自定义表格数据获取失败,json转结构体失败,Err:" + err.Error()
- return
- }
- if len(tableData.EdbInfoIdList) > 0 {
- err, _ = data.EdbInfoRefreshAllFromBaseV3(tableData.EdbInfoIdList, false, true, false)
- if err != nil {
- br.Msg = "刷新失败"
- br.ErrMsg = "刷新失败,Err:" + err.Error()
- return
- }
- }
- }
- // 数据刷新-混合表格
- if excelDetail.Source == utils.MIXED_TABLE {
- // todo 刷新动态表的所有子表中关联的指标数据
- jsonByte, e := json.Marshal(excelDetail.TableData)
- if e != nil {
- br.Msg = "刷新失败"
- br.ErrMsg = "JSON格式化混合表格数据失败, Err: " + e.Error()
- return
- }
- var tableData request.MixedTableReq
- if e = json.Unmarshal(jsonByte, &tableData); e != nil {
- br.Msg = "刷新失败"
- br.ErrMsg = "解析混合表格数据失败, Err: " + e.Error()
- return
- }
- edbInfoIds := make([]int, 0)
- edbInfoIdExist := make(map[int]bool)
- if len(tableData.Data) > 0 {
- for _, t := range tableData.Data {
- for _, v := range t {
- if v.EdbInfoId > 0 && !edbInfoIdExist[v.EdbInfoId] {
- edbInfoIdExist[v.EdbInfoId] = true
- edbInfoIds = append(edbInfoIds, v.EdbInfoId)
- }
- }
- }
- }
- if len(edbInfoIds) > 0 {
- e, _ = data.EdbInfoRefreshAllFromBaseV3(edbInfoIds, false, true, false)
- if e != nil {
- br.Msg = "刷新失败"
- br.ErrMsg = "刷新混合表格数据失败, Err: " + err.Error()
- return
- }
- }
- }
- if excelDetail.Source == utils.BALANCE_TABLE {
- err = refreshBalanceTable(excelDetail, c.Lang)
- if err != nil {
- br.Msg = "刷新失败"
- br.ErrMsg = "刷新失败,Err:" + err.Error()
- return
- }
- }
- // 清除缓存
- key := utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL + ":" + excelDetail.UniqueCode
- if utils.Re == nil {
- _ = utils.Rc.Delete(key)
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "刷新成功"
- }
- // Download
- // @Title 获取表格详情
- // @Description 获取表格详情接口
- // @Param UniqueCode query string true "表格code"
- // @Param ExcelInfoId query int true "表格id"
- // @Success 200 {object} data_manage.ExcelInfo
- // @router /excel_info/table/download [get]
- func (c *ExcelInfoController) Download() {
- 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")
- uniqueCode := c.GetString("UniqueCode")
- if excelInfoId <= 0 && uniqueCode == `` {
- br.Msg = "请选择表格"
- br.ErrMsg = "ExcelInfoId和UniqueCode都未传"
- br.IsSendEmail = false
- return
- }
- var excelInfo *excel3.ExcelInfo
- var err error
- //获取eta表格信息
- if excelInfoId > 0 {
- excelInfo, err = excel3.GetExcelInfoById(excelInfoId)
- } else {
- excelInfo, err = excel3.GetExcelInfoByUniqueCode(uniqueCode)
- }
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取ETA表格信息失败,Err:" + err.Error()
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "ETA表格被删除,请刷新页面"
- br.ErrMsg = "ETA表格被删除,请刷新页面,Err:" + err.Error()
- br.IsSendEmail = false
- }
- return
- }
- if excelInfo.Source == 1 {
- br.Msg = "不是自定义表格"
- br.IsSendEmail = false
- return
- }
- var tableData excel.TableData
- switch excelInfo.Source {
- case utils.EXCEL_DEFAULT:
- br.Msg = "表格类型异常"
- case utils.TIME_TABLE: // 自定义表格
- var tableDataConfig excel2.TableDataConfig
- err = json.Unmarshal([]byte(excelInfo.Content), &tableDataConfig)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "表格json转结构体失败,Err:" + err.Error()
- return
- }
- result, err := excel2.GetDataByTableDataConfig(tableDataConfig)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取最新的表格数据失败,Err:" + err.Error()
- return
- }
- tableData, err = excel.GetTableDataByCustomData(excelInfo.ExcelType, result, c.Lang)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "转换成table失败,Err:" + err.Error()
- return
- }
- case utils.MIXED_TABLE: // 混合表格
- var result request.MixedTableReq
- err = json.Unmarshal([]byte(excelInfo.Content), &result)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "表格json转结构体失败,Err:" + err.Error()
- return
- }
- newResult, err, errMsg := excel2.GetMixedTableCellData(result, c.Lang)
- if err != nil {
- br.Msg = "获取失败"
- if errMsg != `` {
- br.Msg = errMsg
- }
- br.ErrMsg = "获取最新的数据失败,Err:" + err.Error()
- return
- }
- tableData, err = excel.GetTableDataByMixedTableData(newResult, false, excelInfo.ExcelInfoId)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "转换成table失败,Err:" + err.Error()
- return
- }
- case utils.BALANCE_TABLE: // 混合表格
- savePath, fileName, uploadDir, err, errMsg := downloadBalanceTable(excelInfo, c.Lang)
- if err != nil {
- br.Msg = "下载失败"
- if errMsg != `` {
- br.Msg = errMsg
- }
- br.ErrMsg = "获取最新的数据失败,Err:" + err.Error()
- return
- }
- defer func() {
- _ = os.RemoveAll(uploadDir)
- }()
- // todo 删除文件
- c.Ctx.Output.Download(savePath, fileName)
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = excelInfo
- return
- }
- downloadFilePath, err := tableData.ToExcel()
- if err != nil {
- fmt.Println("err:", err)
- return
- }
- defer func() {
- _ = os.Remove(downloadFilePath)
- }()
- downloadFileName := excelInfo.ExcelName + ".xlsx"
- c.Ctx.Output.Download(downloadFilePath, downloadFileName)
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = excelInfo
- }
- // Copy
- // @Title 复制表格
- // @Description 复制表格接口
- // @Param request body request.EditExcelInfoReq true "type json string"
- // @Success 200 {object} response.AddExcelInfoResp
- // @router /excel_info/copy [post]
- func (c *ExcelInfoController) Copy() {
- 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.CopyExcelInfoReq
- 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_EDIT_" + 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.ExcelName = strings.Trim(req.ExcelName, " ")
- if req.ExcelName == "" {
- br.Msg = "请填写表格名称!"
- br.IsSendEmail = false
- return
- }
- if req.ExcelClassifyId <= 0 {
- br.Msg = "分类参数错误!"
- br.IsSendEmail = false
- return
- }
- // 获取原ETA表格信息
- oldExcelInfo, err := excel3.GetExcelInfoById(req.ExcelInfoId)
- if err != nil {
- br.Msg = "获取ETA表格失败"
- return
- }
- var (
- excelInfo *excel3.ExcelInfo
- errMsg string
- isSendEmail bool
- )
- if oldExcelInfo.Source == utils.BALANCE_TABLE {
- excelInfo, err, errMsg, isSendEmail = data.CopyBalanceExcel(oldExcelInfo, req.ExcelClassifyId, req.ExcelName, sysUser)
- } else {
- excelInfo, err, errMsg, isSendEmail = excel2.Copy(oldExcelInfo, req.ExcelClassifyId, req.ExcelName, sysUser)
- }
- if err != nil {
- br.Msg = "复制失败"
- if errMsg != `` {
- br.Msg = errMsg
- }
- br.ErrMsg = "复制失败,Err:" + err.Error()
- br.IsSendEmail = isSendEmail
- return
- }
- //新增指标引用记录
- _ = data.SaveExcelEdbInfoRelation(excelInfo.ExcelInfoId, excelInfo.Source, true)
- 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
- }
- //func init() {
- // //list, err := data.GetFirstEdbDataList(102739, 12)
- // list, err := data.GetFirstEdbDataList(101323, 12)
- // if err != nil {
- // fmt.Println(err)
- // return
- // }
- //
- // fmt.Println("START")
- // dateList := make([]string, 0)
- // for _, v := range list {
- // fmt.Println(v)
- // dateList = append(dateList, v.DataTime)
- // }
- //
- // fmt.Println("End")
- //
- // {
- // list, err := data.GetOtherEdbDataList(102227, dateList)
- // if err != nil {
- // fmt.Println(err)
- // return
- // }
- //
- // for _, v := range list {
- // fmt.Println(v)
- // }
- // }
- //}
- //func init() {
- // str := `{"EdbInfoIdList":[101323,102227],"Sort":1,"Data":[{"EdbInfoId":101323,"Tag":"A","Data":[{"DataType":4,"DataTime":"2023-06-10","DataTimeType":4,"ShowValue":"318.2726","Value":"=A2-B2","RelationEdbInfoList":[{"Tag":"A","Row":"2"},{"Tag":"B","Row":"2"}]},{"DataType":3,"DataTime":"2023-06-09","DataTimeType":2,"ShowValue":"15","Value":"15","RelationEdbInfoList":[]},{"DataType":1,"DataTime":"2023-06-07","DataTimeType":1,"ShowValue":"16.43","Value":"16.43","RelationEdbInfoList":[]},{"DataType":1,"DataTime":"2023-06-05","DataTimeType":1,"ShowValue":"16.85","Value":"16.85","RelationEdbInfoList":[]},{"DataType":1,"DataTime":"2023-06-02","DataTimeType":1,"ShowValue":"15.88","Value":"15.88","RelationEdbInfoList":[]}]},{"EdbInfoId":102227,"Tag":"B","Data":[{"DataType":4,"DataTime":"2023-06-10","DataTimeType":1,"ShowValue":"318.2726","Value":"=A2+B2","RelationEdbInfoList":[{"Tag":"A","Row":"2"},{"Tag":"B","Row":"2"}]},{"DataType":1,"DataTime":"2023-06-09","DataTimeType":1,"ShowValue":"318.2726","Value":"318.2726","RelationEdbInfoList":[]},{"DataType":2,"DataTime":"2023-06-07","DataTimeType":1,"ShowValue":"100.43","Value":"100.43","RelationEdbInfoList":[]},{"DataType":2,"DataTime":"2023-06-05","DataTimeType":1,"ShowValue":"102.85","Value":"102.85","RelationEdbInfoList":[]},{"DataType":1,"DataTime":"2023-06-02","DataTimeType":1,"ShowValue":"311.5201","Value":"311.5201","RelationEdbInfoList":[]}]}],"TextRowData":[[{"DataType":3,"DataTime":"","ShowValue":"总计","Value":"总计"},{"DataType":4,"DataTime":"","ShowValue":"0","Value":"=A3+B3","RelationEdbInfoList":[{"Tag":"A","Row":"3"},{"Tag":"B","Row":"3"}]},{"DataType":3,"DataTime":"","ShowValue":"","Value":"","RelationEdbInfoList":[]}],[{"DataType":3,"DataTime":"","ShowValue":"垃圾汇总","Value":"垃圾汇总"},{"DataType":4,"DataTime":"","ShowValue":"0","Value":"=A4+B4","RelationEdbInfoList":[{"Tag":"A","Row":"4"},{"Tag":"B","Row":"4"}]},{"DataType":3,"DataTime":"","ShowValue":"这里是文本文案","Value":"这里是文本文案","RelationEdbInfoList":[]}]]}`
- // str = `{"EdbInfoIdList":[102942,102938,102937],"Sort":1,"Data":[],"Num":12,"RemoveDate":["2023-06-05","2023-06-08"],"ManualDate":null,"TableEdbInfoList":[{"EdbInfoId":102942,"Tag":"A","EdbName":"","EdbAliasName":"","Frequency":"","Unit":""},{"EdbInfoId":102938,"Tag":"B","EdbName":"","EdbAliasName":"","Frequency":"","Unit":""},{"EdbInfoId":102937,"Tag":"C","EdbName":"","EdbAliasName":"","Frequency":"","Unit":""}],"TextRowData":[[{"DataType":3,"DataTime":"","DataTimeType":0,"ShowValue":"dwd","Value":"dwd","RelationEdbInfoList":[]},{"DataType":3,"DataTime":"","DataTimeType":0,"ShowValue":"4454","Value":"4454","RelationEdbInfoList":[]},{"DataType":3,"DataTime":"","DataTimeType":0,"ShowValue":"454","Value":"454","RelationEdbInfoList":[]},{"DataType":3,"DataTime":"","DataTimeType":0,"ShowValue":"51","Value":"51","RelationEdbInfoList":[]}],[{"DataType":4,"DataTime":"","DataTimeType":0,"ShowValue":"a1b1","Value":"=a1+b1","RelationEdbInfoList":[{"Tag":"A","Row":"1"},{"Tag":"B","Row":"1"}]},{"DataType":4,"DataTime":"","DataTimeType":0,"ShowValue":"2247.1444","Value":"=a2+b2","RelationEdbInfoList":[{"Tag":"A","Row":"2"},{"Tag":"B","Row":"2"}]},{"DataType":3,"DataTime":"","DataTimeType":0,"ShowValue":"","Value":"","RelationEdbInfoList":[]},{"DataType":3,"DataTime":"","DataTimeType":0,"ShowValue":"","Value":"","RelationEdbInfoList":[]}]]}`
- //
- // //var req request.TableDataReq
- // //err := json.Unmarshal([]byte(str), &req)
- // //if err != nil {
- // // fmt.Println(err)
- // // return
- // //}
- // //tableDataConfig, err := data.GetTableDataConfig(req)
- // //if err != nil {
- // // fmt.Println(err)
- // // return
- // //}
- // var tableDataConfig data.TableDataConfig
- // err := json.Unmarshal([]byte(str), &tableDataConfig)
- // if err != nil {
- // fmt.Println(err)
- // return
- // }
- //
- // fmt.Println(tableDataConfig)
- //
- // result, err := data.GetDataByTableDataConfig(tableDataConfig)
- // if err != nil {
- // fmt.Println(err)
- // return
- // }
- // fmt.Println(result)
- // //byteStr, _ := json.Marshal(result)
- // //utils.FileLog.Info(string(byteStr))
- //}
- //func init() {
- // startNow := time.Now()
- // fmt.Println(startNow.Format(utils.FormatDateTime))
- // startTime := startNow.Unix()
- // //filePath := `static/tmpFile/test/化工23.05.24.xlsx`
- // filePath := `static/tmpFile/1. TAROUND_202308010201-500.xlsx`
- // //filePath := `static/tmpFile/化工23.05.24.xml`
- // //filePath := `static/tmpFile/化工23.05.24/xl/workbook.xml`
- // //filePath := `static/tmpFile/1. TAROUND_202308010201.xlsx`
- //
- // //filePath := `static/tmpFile/1. TAROUND_202308010201-2.xlsx`
- //
- // //filePath := `static/tmpFile/5. Outage_Values_Daily.xlsx`
- // //filePath := `static/tmpFile/1. TAROUND_202308010201 - 副本.xlsx`
- // excel.ConvToLuckySheet(filePath)
- // cz := time.Now().Unix() - startTime
- // fmt.Printf("处理完需要的时间:%d秒;约耗时:%d分%d秒", cz, cz/60, cz%60)
- // fmt.Println()
- //
- // //sheetName := `日度`
- // //sheetName := `月度`
- // //fmt.Println(excel.GetDefaultSheetIndex(filePath, sheetName))
- //
- // //decompressZip(filePath)
- //}
- // src: 需要解压的zip文件
- func decompressZip(src string) error {
- archive, err := zip.OpenReader(src)
- if err != nil {
- return err
- }
- dir := filepath.Dir(src) + "/zip"
- defer archive.Close()
- for _, f := range archive.File {
- filePath := filepath.Join(dir, f.Name)
- if f.FileInfo().IsDir() {
- os.MkdirAll(filePath, os.ModePerm)
- continue
- }
- if err := os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil {
- return fmt.Errorf("failed to make directory (%v)", err)
- }
- dstFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
- if err != nil {
- return fmt.Errorf("failed to create file (%v)", err)
- }
- fileInArchive, err := f.Open()
- if err != nil {
- return fmt.Errorf("failed to open file in zip (%v)", err)
- }
- if _, err := io.Copy(dstFile, fileInArchive); err != nil {
- return fmt.Errorf("failed to copy file in zip (%v)", err)
- }
- dstFile.Close()
- fileInArchive.Close()
- }
- return nil
- }
- // MarkEditStatus
- // @Title 标记沙盘编辑状态
- // @Description 标记沙盘编辑状态接口
- // @Param request body request.MarkEditSandbox true "type json string"
- // @Success 200 标记成功 ;202 标记成功
- // @router /excel_info/mark [post]
- func (c *ExcelInfoController) MarkEditStatus() {
- 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.MarkEditExcel
- 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 = "缺少表格编号"
- return
- }
- if req.Status <= 0 {
- br.Msg = "标记状态异常"
- return
- }
- status := 1
- if req.Status == 2 {
- status = 0
- }
- //更新标记key
- data, err := services.UpdateExcelEditMark(req.ExcelInfoId, sysUser.AdminId, status, sysUser.RealName)
- if err != nil {
- br.Msg = err.Error()
- return
- }
- msg := "标记成功"
- br.Ret = 200
- br.Success = true
- br.Msg = msg
- br.Data = data
- }
- // BatchRefresh
- // @Title 批量刷新表格接口
- // @Description 批量刷新图表接口
- // @Param request body excel3.BatchRefreshExcelReq true "type json string"
- // @Success Ret=200 刷新成功
- // @router /excel_info/table/batch_refresh [post]
- func (c *ExcelInfoController) BatchRefresh() {
- br := new(models.BaseResponse).Init()
- defer func() {
- if br.ErrMsg == "" {
- br.IsSendEmail = false
- }
- 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 excel3.BatchRefreshExcelReq
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if len(req.ExcelCodes) == 0 {
- br.Ret = 200
- br.Success = true
- br.Msg = "刷新成功"
- return
- }
- if req.Source == "" {
- br.Msg = "刷新来源有误"
- return
- }
- if req.PrimaryId <= 0 {
- br.Msg = "刷新对象有误"
- return
- }
- // 获取表格关联的指标IDs
- edbIds, e := excel2.GetEdbIdsFromExcelCodes(req.ExcelCodes, sysUser.AdminId, c.Lang)
- if e != nil {
- br.Msg = "刷新失败"
- br.ErrMsg = "获取表格关联的指标IDs失败, Err: " + e.Error()
- return
- }
- redisKey := data.GetBatchChartRefreshKey(req.Source, req.PrimaryId, req.SubId)
- refreshKeys := make([]string, 0)
- for _, v := range req.ExcelCodes {
- refreshKeys = append(refreshKeys, fmt.Sprint(utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL, v))
- }
- // 刷新相关指标
- syncing, e := data.BatchRefreshEdbByEdbIds(edbIds, redisKey, refreshKeys)
- if e != nil {
- br.Msg = "刷新失败"
- br.ErrMsg = "刷新表格关联指标信息失败,Err:" + err.Error()
- return
- }
- br.Msg = "刷新成功"
- if syncing {
- br.Msg = "表格关联指标较多,请10分钟后刷新页面查看最新数据"
- }
- br.Ret = 200
- br.Success = true
- }
- // GetBatchChartRefreshResult
- // @Title 获取批量刷新表格结果
- // @Description 获取批量刷新表格结果
- // @Param request body excel3.BatchRefreshExcelReq true "type json string"
- // @Success Ret=200 刷新成功
- // @router /excel_info/table/batch_refresh/result [post]
- func (c *ExcelInfoController) GetBatchChartRefreshResult() {
- 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 excel3.BatchRefreshExcelReq
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.Source == "" {
- br.Msg = "刷新来源有误"
- return
- }
- if req.PrimaryId <= 0 {
- br.Msg = "刷新对象有误"
- return
- }
- // 校验缓存是否存在, 存在说明还在刷新中
- result := true
- redisKey := excel2.GetExcelEdbBatchRefreshKey(req.Source, req.PrimaryId, req.SubId)
- if redisKey != `` {
- // 如果找到了key,那么就是还在更新中
- ok := utils.Rc.IsExist(redisKey)
- if ok {
- result = false
- }
- }
- resp := struct {
- RefreshResult bool `description:"刷新结果"`
- }{
- RefreshResult: result,
- }
- br.Data = resp
- br.Ret = 200
- br.Success = true
- }
- // GetBatchChartRefreshResult
- // @Title 获取批量刷新表格结果
- // @Description 获取批量刷新表格结果
- // @Param EdbInfoId query int true "edb id"
- // @Success Ret=200 刷新成功
- // @router /excel_info/get_edb_source [get]
- func (c *ExcelInfoController) GetEdbSource() {
- 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
- }
- edbInfoId, _ := c.GetInt("EdbInfoId")
- if edbInfoId <= 0 {
- br.Msg = "请选择指标"
- br.ErrMsg = "请选择指标"
- br.IsSendEmail = false
- return
- }
- sourceNameList, sourceNameEnList, err := excel2.GetEdbSourceByEdbInfoIdList([]int{edbInfoId})
- if err != nil {
- br.Msg = "自定义表格数据获取失败"
- br.ErrMsg = "自定义表格数据获取失败,Err:" + err.Error()
- return
- }
- excelSource := strings.Join(sourceNameList, ",")
- excelSourceEn := strings.Join(sourceNameEnList, ",")
- var resp struct {
- ExcelSource string `description:"表格来源"`
- ExcelSourceEn string `description:"表格来源(英文)"`
- }
- resp.ExcelSource = excelSource
- resp.ExcelSourceEn = excelSourceEn
- br.Data = resp
- br.Ret = 200
- br.Success = true
- }
- // ExcelRule
- // @Title 添加表格规则
- // @Description 添加表格规则
- // @Param request body excel3.BatchRefreshExcelReq true "type json string"
- // @Success Ret=200 刷新成功
- // @router /excel_info/rule/add [post]
- func (c *ExcelInfoController) AddExcelRule() {
- 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.ExcelRuleMappingReq
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.LeftValue == "" {
- br.Msg = "条件值不能为空"
- return
- }
- if req.Scope == "" {
- br.Msg = "应用选区不能为空"
- return
- }
- if req.FontColor == "" && req.BackgroundColor == "" {
- br.Msg = "字体颜色和背景颜色不能同时为空"
- return
- }
- if req.RuleType == 3 && req.RightValue == "" {
- br.Msg = "条件值不能为空"
- return
- }
- err = excel2.AddExcelRule(req, c.Lang)
- if err != nil {
- br.Msg = "规则添加失败"
- br.ErrMsg = "规则添加失败,Err:" + err.Error()
- return
- }
- br.Msg = "添加成功"
- br.Ret = 200
- br.Success = true
- }
- // EditExcelRule
- // @Title 编辑表格规则
- // @Description 编辑表格规则
- // @Param request body request.ExcelRuleMappingReq true "type json string"
- // @Success Ret=200 刷新成功
- // @router /excel_info/rule/edit [post]
- func (c *ExcelInfoController) EditExcelRule() {
- 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.ExcelRuleMappingReq
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.LeftValue == "" {
- br.Msg = "条件值不能为空"
- return
- }
- if req.Scope == "" {
- br.Msg = "应用选区不能为空"
- return
- }
- req.BackgroundColor = strings.TrimSpace(req.BackgroundColor)
- req.FontColor = strings.TrimSpace(req.FontColor)
- if req.FontColor == "" && req.BackgroundColor == "" {
- br.Msg = "字体颜色或背景颜色不能同时为空"
- return
- }
- if req.RuleType == 3 && req.RightValue == "" {
- br.Msg = "条件值不能为空"
- return
- }
- if req.ExcelRuleMappingId <= 0 {
- br.Msg = "非法的管理规则ID"
- return
- }
- err = excel2.ModifyExcelRule(req, c.Lang)
- if err != nil {
- br.Msg = "规则编辑失败"
- br.ErrMsg = "规则编辑失败,Err:" + err.Error()
- return
- }
- br.Msg = "编辑成功"
- br.Ret = 200
- br.Success = true
- }
- // DeleteExcelRule
- // @Title 删除表格规则
- // @Description 删除表格规则
- // @Param request body request.ExcelRuleMappingReq true "type json string"
- // @Success Ret=200 刷新成功
- // @router /excel_info/rule/delete [post]
- func (c *ExcelInfoController) DeleteExcelRule() {
- 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.DeleteExcelRuleMappingReq
- err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.ExcelRuleMappingId <= 0 {
- br.Msg = "非法的管理规则ID"
- return
- }
- err = excel3.DeleteExcelRuleMappingById(req.ExcelRuleMappingId)
- if err != nil {
- br.Msg = "规则删除失败"
- br.ErrMsg = "规则删除失败,Err:" + err.Error()
- return
- }
- br.Msg = "删除成功"
- br.Ret = 200
- br.Success = true
- }
- // GetExcelRuleList
- // @Title 表格规则列表
- // @Description 表格规则列表
- // @Param ExcelInfoId query int true "id"
- // @Success Ret=200 获取成功
- // @router /excel_info/rule/list [get]
- func (c *ExcelInfoController) GetExcelRuleList() {
- 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
- }
- items, err := excel2.GetExcelRuleList(ExcelInfoId)
- if err != nil {
- br.Msg = "管理规则编辑失败"
- br.ErrMsg = "管理规则添加失败,Err:" + err.Error()
- return
- }
- if items.List == nil {
- items.List = []*excel3.ExcelInfoRuleMappingView{}
- }
- br.Data = items
- br.Msg = "获取成功"
- br.Ret = 200
- br.Success = true
- }
- // GetExcelRuleDetail
- // @Title 表格规则详情
- // @Description 表格规则详情
- // @Param ExcelInfoRuleMappingId query int true "id"
- // @Success Ret=200 获取成功
- // @router /excel_info/rule/detail [get]
- func (c *ExcelInfoController) GetExcelRuleDetail() {
- 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
- }
- excelInfoRuleMappingId, _ := c.GetInt("ExcelInfoRuleMappingId")
- items, err := excel2.GetExcelRuleDetail(excelInfoRuleMappingId)
- if err != nil {
- br.Msg = "管理规则编辑失败"
- br.ErrMsg = "管理规则添加失败,Err:" + err.Error()
- return
- }
- br.Data = items
- br.Msg = "获取成功"
- br.Ret = 200
- br.Success = true
- }
|