excel_info.go 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327
  1. package excel
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/controllers"
  5. "eta_gn/eta_api/models"
  6. "eta_gn/eta_api/models/data_manage"
  7. excel3 "eta_gn/eta_api/models/data_manage/excel"
  8. "eta_gn/eta_api/models/data_manage/excel/request"
  9. "eta_gn/eta_api/models/data_manage/excel/response"
  10. "eta_gn/eta_api/services"
  11. "eta_gn/eta_api/services/data"
  12. "eta_gn/eta_api/services/data/data_manage_permission"
  13. excel2 "eta_gn/eta_api/services/data/excel"
  14. "eta_gn/eta_api/services/excel"
  15. "eta_gn/eta_api/utils"
  16. "fmt"
  17. "os"
  18. "strconv"
  19. "strings"
  20. "time"
  21. "github.com/rdlucklib/rdluck_tools/paging"
  22. "github.com/shopspring/decimal"
  23. "github.com/yidane/formula"
  24. )
  25. // ExcelInfoController ETA表格管理
  26. type ExcelInfoController struct {
  27. controllers.BaseAuthController
  28. }
  29. // Add
  30. // @Title 新增表格接口
  31. // @Description 新增表格接口
  32. // @Param request body request.AddExcelInfoReq true "type json string"
  33. // @Success 200 {object} response.AddExcelInfoResp
  34. // @router /excel_info/add [post]
  35. func (c *ExcelInfoController) Add() {
  36. br := new(models.BaseResponse).Init()
  37. defer func() {
  38. c.Data["json"] = br
  39. c.ServeJSON()
  40. }()
  41. sysUser := c.SysUser
  42. if sysUser == nil {
  43. br.Msg = "请登录"
  44. br.ErrMsg = "请登录,SysUser Is Empty"
  45. br.Ret = 408
  46. return
  47. }
  48. deleteCache := true
  49. cacheKey := "CACHE_EXCEL_TABLE_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  50. defer func() {
  51. if deleteCache {
  52. _ = utils.Rc.Delete(cacheKey)
  53. }
  54. }()
  55. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  56. deleteCache = false
  57. br.Msg = "系统处理中,请稍后重试!"
  58. br.ErrMsg = "系统处理中,请稍后重试!"
  59. return
  60. }
  61. var req request.AddExcelInfoReq
  62. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  63. if err != nil {
  64. br.Msg = "参数解析异常!"
  65. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  66. return
  67. }
  68. req.ExcelName = strings.Trim(req.ExcelName, " ")
  69. if req.ExcelName == "" {
  70. br.Msg = "请填写表格名称!"
  71. br.IsSendEmail = false
  72. return
  73. }
  74. if req.ExcelClassifyId <= 0 && req.ParentId == 0 {
  75. br.Msg = "分类参数错误!"
  76. br.IsSendEmail = false
  77. return
  78. }
  79. if req.ExcelClassifyId > 0 {
  80. excelClassify, e := excel3.GetExcelClassifyById(req.ExcelClassifyId)
  81. if e != nil {
  82. if utils.IsErrNoRow(e) {
  83. br.Msg = "分类不存在"
  84. br.ErrMsg = "分类不存在"
  85. br.IsSendEmail = false
  86. return
  87. }
  88. br.Msg = "获取分类信息失败"
  89. br.ErrMsg = "获取分类信息失败,Err:" + e.Error()
  90. return
  91. }
  92. if excelClassify == nil {
  93. br.Msg = "分类不存在"
  94. br.ErrMsg = "分类不存在"
  95. br.IsSendEmail = false
  96. return
  97. }
  98. }
  99. // 来源字数校验
  100. type SourcesFrom struct {
  101. Text string `json:"text"`
  102. }
  103. if req.SourcesFrom != "" && len(req.SourcesFrom) > 0 {
  104. var sourcesFrom SourcesFrom
  105. err = json.Unmarshal([]byte(req.SourcesFrom), &sourcesFrom)
  106. if err != nil {
  107. return
  108. }
  109. if len(sourcesFrom.Text) > 50 {
  110. br.Msg = "字数已达上限!"
  111. br.ErrMsg = "来源文本字数已达上限,请修改!"
  112. return
  113. }
  114. }
  115. if req.ParentId > 0 {
  116. parentExcelInfo, e := excel3.GetExcelInfoById(req.ParentId)
  117. if e != nil {
  118. if utils.IsErrNoRow(e) {
  119. br.Msg = "父级ETA表格被删除,请刷新页面"
  120. return
  121. }
  122. br.Msg = "获取父级ETA表格信息失败"
  123. br.ErrMsg = "获取父级ETA表格信息失败,Err:" + e.Error()
  124. return
  125. }
  126. req.ExcelClassifyId = parentExcelInfo.ExcelClassifyId
  127. }
  128. var condition string
  129. var pars []interface{}
  130. condition += " AND excel_classify_id=? AND parent_id=?"
  131. pars = append(pars, req.ExcelClassifyId, req.ParentId)
  132. condition += " AND excel_name=? "
  133. pars = append(pars, req.ExcelName)
  134. // 自定义分析私有化了
  135. if req.Source == utils.CUSTOM_ANALYSIS_TABLE {
  136. condition += ` AND sys_user_id = ? `
  137. pars = append(pars, sysUser.AdminId)
  138. }
  139. // 获取分类下是否存在该表格名称
  140. count, err := excel3.GetExcelInfoCountByCondition(condition, pars)
  141. if err != nil {
  142. br.Msg = "判断表格名称是否存在失败"
  143. br.ErrMsg = "判断表格名称是否存在失败,Err:" + err.Error()
  144. return
  145. }
  146. if count > 0 {
  147. br.Msg = "表格名称已存在,请重新填写表格名称"
  148. br.IsSendEmail = false
  149. return
  150. }
  151. // 默认的excel类型是1
  152. if req.Source == 0 {
  153. req.Source = 1
  154. }
  155. content := req.Content
  156. // 引用的指标id
  157. edbInfoIdList := make([]int, 0)
  158. // 自定义表格
  159. if req.Source == utils.TIME_TABLE {
  160. jsonStrByte, err := json.Marshal(req.TableData)
  161. if err != nil {
  162. br.Msg = "时间序列表格数据获取失败"
  163. br.ErrMsg = "时间序列表格数据获取失败,Err:" + err.Error()
  164. return
  165. }
  166. var tableData request.TableDataReq
  167. err = json.Unmarshal(jsonStrByte, &tableData)
  168. if err != nil {
  169. br.Msg = "时间序列表格数据获取失败"
  170. br.ErrMsg = "时间序列表格数据获取失败,Err:" + err.Error()
  171. return
  172. }
  173. tableDataConfig, err := excel2.GetTableDataConfig(tableData)
  174. if err != nil {
  175. br.Msg = "时间序列表格数据获取失败"
  176. br.ErrMsg = "时间序列表格数据获取失败,Err:" + err.Error()
  177. return
  178. }
  179. edbInfoIdList = tableDataConfig.EdbInfoIdList
  180. tableDataConfig.DecimalConfig = tableData.DecimalConfig
  181. contentByte, err := json.Marshal(tableDataConfig)
  182. if err != nil {
  183. br.Msg = "时间序列表格数据获取失败"
  184. br.ErrMsg = "时间序列表格数据获取失败,Err:" + err.Error()
  185. return
  186. }
  187. content = string(contentByte)
  188. }
  189. // 混合表格
  190. if req.Source == 3 || req.Source == 5 {
  191. contentByte, err := json.Marshal(req.TableData)
  192. if err != nil {
  193. br.Msg = "自定义表格数据获取失败"
  194. br.ErrMsg = "自定义表格数据获取后,转json失败,Err:" + err.Error()
  195. return
  196. }
  197. content = string(contentByte)
  198. var result request.MixedTableReq
  199. err = json.Unmarshal(contentByte, &result)
  200. if err != nil {
  201. br.Msg = "获取失败"
  202. br.ErrMsg = "表格json转结构体失败,Err:" + err.Error()
  203. return
  204. }
  205. newResult, err, errMsg := excel2.GetMixedTableCellData(result, c.Lang)
  206. if err != nil {
  207. br.Msg = "获取失败"
  208. if errMsg != `` {
  209. br.Msg = errMsg
  210. }
  211. br.ErrMsg = "获取最新的数据失败,Err:" + err.Error()
  212. return
  213. }
  214. edbInfoIdMap := make(map[int]int)
  215. for _, tmpV := range newResult {
  216. for _, v := range tmpV {
  217. if v.EdbInfoId > 0 {
  218. if _, ok := edbInfoIdMap[v.EdbInfoId]; !ok {
  219. edbInfoIdMap[v.EdbInfoId] = v.EdbInfoId
  220. edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
  221. }
  222. }
  223. }
  224. }
  225. }
  226. //获取该层级下最大的排序数
  227. maxSort, err := excel2.GetExcelClassifyMaxSort(req.ExcelClassifyId, req.Source)
  228. if err != nil {
  229. br.Msg = "获取失败"
  230. br.ErrMsg = "查询排序信息失败,Err:" + err.Error()
  231. return
  232. }
  233. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  234. excelInfo := &excel3.ExcelInfo{
  235. //ExcelInfoId: 0,
  236. ExcelName: req.ExcelName,
  237. Source: req.Source,
  238. ExcelType: req.ExcelType,
  239. UniqueCode: utils.MD5(utils.EXCEL_DATA_PREFIX + "_" + timestamp),
  240. ExcelClassifyId: req.ExcelClassifyId,
  241. SysUserId: sysUser.AdminId,
  242. SysUserRealName: sysUser.RealName,
  243. Content: content,
  244. ExcelImage: req.ExcelImage,
  245. Sort: maxSort + 1,
  246. IsDelete: 0,
  247. ModifyTime: time.Now(),
  248. CreateTime: time.Now(),
  249. ParentId: req.ParentId,
  250. UpdateUserId: sysUser.AdminId,
  251. UpdateUserRealName: sysUser.RealName,
  252. SourcesFrom: req.SourcesFrom,
  253. }
  254. excelEdbMappingList := make([]*excel3.ExcelEdbMapping, 0)
  255. if len(edbInfoIdList) > 0 {
  256. for _, edbInfoId := range edbInfoIdList {
  257. excelEdbMappingList = append(excelEdbMappingList, &excel3.ExcelEdbMapping{
  258. //ExcelEdbMappingId: 0,
  259. //ExcelInfoId: 0,
  260. Source: excelInfo.Source,
  261. EdbInfoId: edbInfoId,
  262. CreateTime: time.Now(),
  263. ModifyTime: time.Now(),
  264. })
  265. }
  266. }
  267. var childExcel *excel3.ExcelInfo
  268. if excelInfo.Source == utils.BALANCE_TABLE && req.ParentId == 0 && excelInfo.BalanceType == 0 { //首次创建平衡表时需要添加一个默认的子表
  269. timestamp = strconv.FormatInt(time.Now().UnixNano(), 10) + "_" + utils.GetRandString(10)
  270. childExcel = &excel3.ExcelInfo{
  271. //ExcelInfoId: 0,
  272. ExcelName: "平衡表",
  273. Source: excelInfo.Source,
  274. ExcelType: excelInfo.ExcelType,
  275. UniqueCode: utils.MD5(utils.EXCEL_DATA_PREFIX + "_" + timestamp),
  276. ExcelClassifyId: req.ExcelClassifyId,
  277. SysUserId: sysUser.AdminId,
  278. SysUserRealName: sysUser.RealName,
  279. Content: excelInfo.Content,
  280. //ExcelImage: req.ExcelImage,
  281. Sort: excelInfo.Sort + 1,
  282. IsDelete: 0,
  283. ModifyTime: time.Now(),
  284. CreateTime: time.Now(),
  285. //ParentId: req.ParentId,
  286. UpdateUserId: sysUser.AdminId,
  287. UpdateUserRealName: sysUser.RealName,
  288. }
  289. }
  290. err = excel3.AddExcelInfo(excelInfo, excelEdbMappingList, childExcel)
  291. if err != nil {
  292. br.Msg = "保存失败"
  293. br.ErrMsg = "保存失败,Err:" + err.Error()
  294. return
  295. }
  296. // 更新excel下载地址(默认的EXCEL需要更新,自定义表格不需要更新)
  297. if req.Source == 1 {
  298. go excel2.UpdateExcelInfoFileUrl(excelInfo)
  299. }
  300. _, err = services.UpdateExcelEditMark(excelInfo.ExcelInfoId, sysUser.AdminId, 1, sysUser.RealName)
  301. if err != nil {
  302. br.Msg = "查询标记状态失败"
  303. br.ErrMsg = "查询标记状态失败,Err:" + err.Error()
  304. return
  305. }
  306. resp := new(response.AddExcelInfoResp)
  307. resp.ExcelInfoId = excelInfo.ExcelInfoId
  308. resp.UniqueCode = excelInfo.UniqueCode
  309. //新增指标引用记录
  310. _ = data.SaveExcelEdbInfoRelation(excelInfo.ExcelInfoId, excelInfo.Source, false)
  311. //新增操作日志
  312. //{
  313. // excelLog := &data_manage.ExcelInfoLog{
  314. // //ExcelInfoLogId: 0,
  315. // ExcelInfoId: excelInfo.ExcelInfoId,
  316. // ExcelName: req.ExcelName,
  317. // ExcelClassifyId: req.ExcelClassifyId,
  318. // SysUserId: sysUser.AdminId,
  319. // SysUserRealName: sysUser.RealName,
  320. // UniqueCode: excelInfo.UniqueCode,
  321. // CreateTime: time.Now(),
  322. // Content: string(c.Ctx.Input.RequestBody),
  323. // Status: "新增表格",
  324. // Method: c.Ctx.Input.URI(),
  325. // }
  326. // go data_manage.AddExcelInfoLog(excelLog)
  327. //}
  328. br.Ret = 200
  329. br.Success = true
  330. br.Msg = "保存成功"
  331. br.Data = resp
  332. br.IsAddLog = true
  333. }
  334. // List
  335. // @Title ETA表格列表接口
  336. // @Description ETA表格列表接口
  337. // @Param PageSize query int true "每页数据条数"
  338. // @Param CurrentIndex query int true "当前页页码,从1开始"
  339. // @Param ExcelClassifyId query int true "分类id"
  340. // @Param Keyword query string true "搜索关键词"
  341. // @Param AdminId query int false "创建人id"
  342. // @Param Source query int true "格来源,1:excel插件的表格,2:自定义表格,3:混合表格,默认:1"
  343. // @Param IsShowMe query bool true "是否只看我的,true、false"
  344. // @Success 200 {object} response.ExcelListResp
  345. // @router /excel_info/list [get]
  346. func (c *ExcelInfoController) List() {
  347. br := new(models.BaseResponse).Init()
  348. defer func() {
  349. c.Data["json"] = br
  350. c.ServeJSON()
  351. }()
  352. sysUser := c.SysUser
  353. if sysUser == nil {
  354. br.Msg = "请登录"
  355. br.ErrMsg = "请登录,SysUser Is Empty"
  356. br.Ret = 408
  357. return
  358. }
  359. excelClassifyId, _ := c.GetInt("ExcelClassifyId")
  360. pageSize, _ := c.GetInt("PageSize")
  361. currentIndex, _ := c.GetInt("CurrentIndex")
  362. keyword := c.GetString("Keyword")
  363. adminId, _ := c.GetInt("AdminId")
  364. source, _ := c.GetInt("Source")
  365. var total int
  366. page := paging.GetPaging(currentIndex, pageSize, total)
  367. var startSize int
  368. if pageSize <= 0 {
  369. pageSize = utils.PageSize20
  370. }
  371. if currentIndex <= 0 {
  372. currentIndex = 1
  373. }
  374. startSize = paging.StartIndex(currentIndex, pageSize)
  375. var condition string
  376. var pars []interface{}
  377. // 如果没有传的话,那就是代表前三种表格
  378. if source <= 0 {
  379. condition += " AND source in (?,?,?) "
  380. pars = append(pars, utils.EXCEL_DEFAULT, utils.TIME_TABLE, utils.MIXED_TABLE)
  381. } else {
  382. condition += " AND source = ? "
  383. pars = append(pars, source)
  384. if source == utils.BALANCE_TABLE { //平衡表的列表只显示动态表的一级表(不显示子表和静态表)
  385. condition += " AND parent_id = 0 AND balance_type=0 "
  386. }
  387. }
  388. // 筛选分类
  389. if excelClassifyId > 0 {
  390. _, err := excel3.GetExcelClassifyById(excelClassifyId)
  391. if err != nil && !utils.IsErrNoRow(err) {
  392. br.Msg = "获取表格信息失败"
  393. br.ErrMsg = "获取信息失败,GetExcelClassify,Err:" + err.Error()
  394. return
  395. }
  396. childClassify, e, _ := excel2.GetChildClassifyByClassifyId(excelClassifyId, source)
  397. if e != nil && !utils.IsErrNoRow(e) {
  398. br.Msg = "获取失败"
  399. br.ErrMsg = "获取分类信息失败, GetEdbClassify,Err:" + e.Error()
  400. return
  401. }
  402. if len(childClassify) == 0 {
  403. condition += " AND excel_classify_id = ? "
  404. pars = append(pars, excelClassifyId)
  405. } else {
  406. classifyIds := []int{excelClassifyId}
  407. for _, v := range childClassify {
  408. classifyIds = append(classifyIds, v.ExcelClassifyId)
  409. }
  410. condition += fmt.Sprintf(` AND excel_classify_id IN (%s) `, utils.GetOrmInReplace(len(classifyIds)))
  411. pars = append(pars, classifyIds)
  412. }
  413. }
  414. if keyword != "" {
  415. condition += ` AND ( excel_name LIKE ? )`
  416. pars = utils.GetLikeKeywordPars(pars, keyword, 1)
  417. }
  418. if adminId > 0 {
  419. condition += " AND sys_user_id = ? "
  420. pars = append(pars, adminId)
  421. }
  422. //只看我的
  423. isShowMe, _ := c.GetBool("IsShowMe")
  424. // 获取所有有权限的指标和分类
  425. permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserExcelAndClassifyPermissionList(c.SysUser.AdminId, 0, 0)
  426. if err != nil {
  427. br.Msg = "获取失败"
  428. br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  429. return
  430. }
  431. hasCheck := make(map[int]bool)
  432. if isShowMe {
  433. if source == utils.BALANCE_TABLE { //平衡表的,显示同时需要显示协作人相关的图表
  434. //找到当前协作人相关的表格ID
  435. obj := new(excel3.ExcelWorker)
  436. existList, err := obj.GetBySysUserId(sysUser.AdminId)
  437. if err != nil {
  438. br.Msg = "获取表格协作人失败!"
  439. br.ErrMsg = "获取表格协作人失败,Err:" + err.Error()
  440. return
  441. }
  442. var excelIds []int
  443. newCondition := condition
  444. newPars := pars
  445. if len(existList) > 0 {
  446. for _, v := range existList {
  447. excelIds = append(excelIds, v.ExcelInfoId)
  448. }
  449. newCondition += fmt.Sprintf(` AND ( excel_info_id IN (%s) or sys_user_id = ?)`, utils.GetOrmInReplace(len(excelIds)))
  450. newPars = append(newPars, excelIds, sysUser.AdminId)
  451. } else {
  452. newCondition += ` AND sys_user_id = ? `
  453. newPars = append(newPars, sysUser.AdminId)
  454. }
  455. //获取表格信息
  456. tmpList, e := excel3.GetNoContentExcelListByConditionNoPage(newCondition, newPars)
  457. if e != nil && !utils.IsErrNoRow(e) {
  458. br.Success = true
  459. br.Msg = "获取表格信息失败"
  460. br.ErrMsg = "获取表格信息失败,Err:" + e.Error()
  461. return
  462. }
  463. classifyIdListTmp := make([]int, 0)
  464. for _, v := range tmpList {
  465. classifyIdListTmp = append(classifyIdListTmp, v.ExcelClassifyId)
  466. }
  467. classifyMap := make(map[int]*excel3.ExcelClassify)
  468. // 分类信息
  469. if len(classifyIdListTmp) > 0 {
  470. classifyListTmp, e := excel3.GetClassifyByIdList(classifyIdListTmp)
  471. if e != nil {
  472. br.Msg = "获取表格分类信息失败"
  473. br.ErrMsg = "获取表格分类列表数据失败,Err:" + e.Error()
  474. return
  475. }
  476. for _, v := range classifyListTmp {
  477. classifyMap[v.ExcelClassifyId] = v
  478. }
  479. }
  480. excelIds = make([]int, 0)
  481. for _, v := range tmpList {
  482. // 数据权限
  483. if classifyInfo, ok := classifyMap[v.ExcelClassifyId]; ok {
  484. v.HaveOperaAuth = data_manage_permission.CheckExcelPermissionByPermissionIdList(v.IsJoinPermission, classifyInfo.IsJoinPermission, v.ExcelInfoId, v.ExcelClassifyId, permissionEdbIdList, permissionClassifyIdList)
  485. if v.HaveOperaAuth {
  486. excelIds = append(excelIds, v.ExcelInfoId)
  487. }
  488. hasCheck[v.ExcelInfoId] = v.HaveOperaAuth
  489. }
  490. }
  491. if len(excelIds) > 0 {
  492. condition += fmt.Sprintf(` AND excel_info_id IN (%s)`, utils.GetOrmInReplace(len(excelIds)))
  493. pars = append(pars, excelIds)
  494. } else {
  495. list := make([]*excel3.MyExcelInfoList, 0)
  496. resp := response.ExcelListResp{
  497. Paging: page,
  498. List: list,
  499. }
  500. br.Ret = 200
  501. br.Success = true
  502. br.Msg = "获取成功"
  503. br.Data = resp
  504. return
  505. }
  506. } else {
  507. condition += " AND sys_user_id = ? "
  508. pars = append(pars, sysUser.AdminId)
  509. }
  510. }
  511. //获取表格信息
  512. list, err := excel3.GetNoContentExcelListByCondition(condition, pars, startSize, pageSize)
  513. if err != nil && !utils.IsErrNoRow(err) {
  514. br.Success = true
  515. br.Msg = "获取表格信息失败"
  516. br.ErrMsg = "获取表格信息失败,Err:" + err.Error()
  517. return
  518. }
  519. lenList := len(list)
  520. if list == nil || lenList <= 0 || (err != nil && utils.IsErrNoRow(err)) {
  521. list = make([]*excel3.MyExcelInfoList, 0)
  522. }
  523. if lenList > 0 {
  524. classifyIdList := make([]int, 0)
  525. for _, v := range list {
  526. classifyIdList = append(classifyIdList, v.ExcelClassifyId)
  527. }
  528. classifyMap := make(map[int]*excel3.ExcelClassify)
  529. // 分类信息
  530. {
  531. classifyList, err := excel3.GetClassifyByIdList(classifyIdList)
  532. if err != nil {
  533. br.Msg = "获取表格列表信息失败"
  534. br.ErrMsg = "获取表格分类列表数据失败,Err:" + err.Error()
  535. return
  536. }
  537. for _, v := range classifyList {
  538. classifyMap[v.ExcelClassifyId] = v
  539. }
  540. }
  541. for k, v := range list {
  542. // 数据权限
  543. if authCheck, ok1 := hasCheck[v.ExcelInfoId]; ok1 {
  544. v.HaveOperaAuth = authCheck
  545. } else {
  546. if classifyInfo, ok := classifyMap[v.ExcelClassifyId]; ok {
  547. v.HaveOperaAuth = data_manage_permission.CheckExcelPermissionByPermissionIdList(v.IsJoinPermission, classifyInfo.IsJoinPermission, v.ExcelInfoId, v.ExcelClassifyId, permissionEdbIdList, permissionClassifyIdList)
  548. }
  549. }
  550. if v.Source == utils.BALANCE_TABLE {
  551. //处理按钮权限和编辑状态
  552. markStatus, err := services.UpdateExcelEditMark(v.ExcelInfoId, sysUser.AdminId, 2, sysUser.RealName)
  553. if err != nil {
  554. br.Msg = "查询标记状态失败"
  555. br.ErrMsg = "查询标记状态失败,Err:" + err.Error()
  556. return
  557. }
  558. if markStatus.Status == 0 {
  559. list[k].CanEdit = true
  560. } else {
  561. list[k].Editor = markStatus.Editor
  562. }
  563. // excel表格按钮权限
  564. list[k].Button = excel2.GetBalanceExcelInfoOpButton(sysUser.AdminId, v.SysUserId, v.HaveOperaAuth, v.ExcelInfoId)
  565. }
  566. }
  567. }
  568. // 总数据量
  569. dataCount, err := excel3.GetExcelListCountByCondition(condition, pars)
  570. if err != nil && !utils.IsErrNoRow(err) {
  571. br.Msg = "获取表格列表信息失败"
  572. br.ErrMsg = "获取表格列表数据总数失败,Err:" + err.Error()
  573. return
  574. }
  575. page = paging.GetPaging(currentIndex, pageSize, dataCount)
  576. resp := response.ExcelListResp{
  577. Paging: page,
  578. List: list,
  579. }
  580. br.Ret = 200
  581. br.Success = true
  582. br.Msg = "获取成功"
  583. br.Data = resp
  584. }
  585. // Detail
  586. // @Title 获取表格详情
  587. // @Description 获取表格详情接口
  588. // @Param ExcelInfoId query int true "表格id"
  589. // @Success 200 {object} data_manage.ExcelInfo
  590. // @router /excel_info/detail [get]
  591. func (c *ExcelInfoController) Detail() {
  592. br := new(models.BaseResponse).Init()
  593. defer func() {
  594. c.Data["json"] = br
  595. c.ServeJSON()
  596. }()
  597. sysUser := c.SysUser
  598. if sysUser == nil {
  599. br.Msg = "请登录"
  600. br.ErrMsg = "请登录,SysUser Is Empty"
  601. br.Ret = 408
  602. return
  603. }
  604. excelInfoId, _ := c.GetInt("ExcelInfoId")
  605. if excelInfoId <= 0 {
  606. br.Msg = "请选择表格"
  607. br.ErrMsg = "ExcelInfoId未传"
  608. br.IsSendEmail = false
  609. return
  610. }
  611. // 获取数据详情
  612. excelDetail, errMsg, err := excel2.GetExcelDetailInfoByExcelInfoId(excelInfoId, c.SysUser.AdminId, c.Lang)
  613. if err != nil {
  614. br.Msg = errMsg
  615. br.ErrMsg = err.Error()
  616. return
  617. }
  618. checkExcelInfoId := excelInfoId
  619. if excelDetail.Source == utils.BALANCE_TABLE {
  620. if excelDetail.BalanceType == 1 { // 平衡表静态表编辑状态以动态表的编辑状态为准
  621. checkExcelInfoId = excelDetail.RelExcelInfoId
  622. } else {
  623. if excelDetail.ParentId > 0 { // 子表编辑状态以父表的编辑状态为准
  624. checkExcelInfoId = excelDetail.ParentId
  625. }
  626. }
  627. }
  628. // 编辑状态
  629. markStatus, err := services.UpdateExcelEditMark(checkExcelInfoId, sysUser.AdminId, 2, sysUser.RealName)
  630. if err != nil {
  631. br.Msg = "查询标记状态失败"
  632. br.ErrMsg = "查询标记状态失败,Err:" + err.Error()
  633. return
  634. }
  635. if markStatus.Status == 0 {
  636. excelDetail.CanEdit = true
  637. } else {
  638. excelDetail.Editor = markStatus.Editor
  639. }
  640. // 图表的指标来源
  641. if excelDetail.Source == utils.TIME_TABLE {
  642. jsonStrByte, err := json.Marshal(excelDetail.TableData)
  643. if err != nil {
  644. br.Msg = "时间序列表格数据获取失败"
  645. br.ErrMsg = "时间序列表格数据获取失败,Err:" + err.Error()
  646. return
  647. }
  648. var tableData request.TableDataReq
  649. err = json.Unmarshal(jsonStrByte, &tableData)
  650. if err != nil {
  651. br.Msg = "时间序列表格数据获取失败"
  652. br.ErrMsg = "时间序列表格数据获取失败,Err:" + err.Error()
  653. return
  654. }
  655. sourceNameList, sourceNameEnList, err := excel2.GetEdbSourceByEdbInfoIdList(tableData.EdbInfoIdList)
  656. if err != nil {
  657. br.Msg = "时间序列表格数据获取失败"
  658. br.ErrMsg = "时间序列表格数据获取失败,Err:" + err.Error()
  659. return
  660. }
  661. excelDetail.ExcelSource = strings.Join(sourceNameList, ",")
  662. excelDetail.ExcelSourceEn = strings.Join(sourceNameEnList, ",")
  663. }
  664. // 数据刷新-混合表格
  665. if excelDetail.Source == utils.MIXED_TABLE {
  666. // todo 刷新动态表的所有子表中关联的指标数据
  667. jsonByte, e := json.Marshal(excelDetail.TableData)
  668. if e != nil {
  669. br.Msg = "刷新失败"
  670. br.ErrMsg = "JSON格式化混合表格数据失败, Err: " + e.Error()
  671. return
  672. }
  673. var tableData request.MixedTableReq
  674. if e = json.Unmarshal(jsonByte, &tableData); e != nil {
  675. br.Msg = "刷新失败"
  676. br.ErrMsg = "解析混合表格数据失败, Err: " + e.Error()
  677. return
  678. }
  679. edbInfoIds := make([]int, 0)
  680. edbInfoIdExist := make(map[int]bool)
  681. if len(tableData.Data) > 0 {
  682. for _, t := range tableData.Data {
  683. for _, v := range t {
  684. if v.EdbInfoId > 0 && !edbInfoIdExist[v.EdbInfoId] {
  685. edbInfoIdExist[v.EdbInfoId] = true
  686. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  687. }
  688. }
  689. }
  690. }
  691. sourceNameList, sourceNameEnList, err := excel2.GetEdbSourceByEdbInfoIdList(edbInfoIds)
  692. if err != nil {
  693. br.Msg = "自定义表格数据获取失败"
  694. br.ErrMsg = "自定义表格数据获取失败,Err:" + err.Error()
  695. return
  696. }
  697. excelDetail.ExcelSource = strings.Join(sourceNameList, ",")
  698. excelDetail.ExcelSourceEn = strings.Join(sourceNameEnList, ",")
  699. }
  700. // excel表格按钮权限
  701. if excelDetail.Source != utils.BALANCE_TABLE {
  702. excelDetail.Button = excel2.GetExcelInfoOpButton(sysUser, excelDetail.SysUserId, excelDetail.Source, excelDetail.HaveOperaAuth)
  703. }
  704. br.Ret = 200
  705. br.Success = true
  706. br.Msg = "获取成功"
  707. br.Data = excelDetail
  708. }
  709. // Edit
  710. // @Title 编辑表格接口
  711. // @Description 新增表格接口
  712. // @Param request body request.EditExcelInfoReq true "type json string"
  713. // @Success 200 {object} response.AddExcelInfoResp
  714. // @router /excel_info/edit [post]
  715. func (c *ExcelInfoController) Edit() {
  716. br := new(models.BaseResponse).Init()
  717. defer func() {
  718. c.Data["json"] = br
  719. c.ServeJSON()
  720. }()
  721. sysUser := c.SysUser
  722. if sysUser == nil {
  723. br.Msg = "请登录"
  724. br.ErrMsg = "请登录,SysUser Is Empty"
  725. br.Ret = 408
  726. return
  727. }
  728. deleteCache := true
  729. var req request.EditExcelInfoReq
  730. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  731. if err != nil {
  732. br.Msg = "参数解析异常!"
  733. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  734. return
  735. }
  736. if req.ExcelInfoId <= 0 {
  737. br.Msg = "请传入ETA表格标识!"
  738. return
  739. }
  740. cacheKey := "CACHE_TABLE_INFO_EDIT_" + strconv.Itoa(req.ExcelInfoId)
  741. defer func() {
  742. if deleteCache {
  743. _ = utils.Rc.Delete(cacheKey)
  744. }
  745. }()
  746. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  747. deleteCache = false
  748. br.Msg = "系统处理中,请稍后重试!"
  749. br.ErrMsg = "系统处理中,请稍后重试!"
  750. return
  751. }
  752. req.ExcelName = strings.Trim(req.ExcelName, " ")
  753. if req.ExcelName == "" {
  754. br.Msg = "请填写表格名称!"
  755. br.IsSendEmail = false
  756. return
  757. }
  758. excelInfo, err := excel3.GetExcelInfoById(req.ExcelInfoId)
  759. if err != nil {
  760. br.Msg = "获取ETA表格失败"
  761. br.ErrMsg = "获取ETA表格失败,Err:" + err.Error()
  762. return
  763. }
  764. if req.ExcelClassifyId <= 0 && excelInfo.ParentId == 0 {
  765. br.Msg = "分类参数错误!"
  766. br.IsSendEmail = false
  767. return
  768. }
  769. checkExcelInfoId := excelInfo.ExcelInfoId
  770. checkExcelInfo := excelInfo
  771. if excelInfo.Source == utils.BALANCE_TABLE {
  772. checkExcelInfoId = excelInfo.ExcelInfoId
  773. if excelInfo.BalanceType == 1 {
  774. checkExcelInfoId = excelInfo.RelExcelInfoId
  775. } else {
  776. if excelInfo.ParentId > 0 {
  777. checkExcelInfoId = excelInfo.ParentId
  778. }
  779. }
  780. if checkExcelInfoId != excelInfo.ExcelInfoId {
  781. checkExcelInfo, err = excel3.GetExcelInfoById(checkExcelInfoId)
  782. if err != nil {
  783. br.Msg = "获取平衡表格信息失败"
  784. br.ErrMsg = "获取平衡表格信息失败,Err:" + err.Error()
  785. return
  786. }
  787. }
  788. }
  789. // 标记编辑状态
  790. markRet, err := services.UpdateExcelEditMark(checkExcelInfoId, sysUser.AdminId, 1, sysUser.RealName)
  791. if err != nil {
  792. br.Msg = "查询标记状态失败"
  793. br.ErrMsg = "查询标记状态失败,Err:" + err.Error()
  794. return
  795. }
  796. if markRet.Status == 1 {
  797. br.Ret = 200
  798. br.Success = true
  799. br.Data = markRet
  800. return
  801. }
  802. if req.ExcelClassifyId > 0 {
  803. excelClassify, e := excel3.GetExcelClassifyById(req.ExcelClassifyId)
  804. if e != nil {
  805. if utils.IsErrNoRow(e) {
  806. br.Msg = "分类不存在"
  807. br.ErrMsg = "分类不存在"
  808. br.IsSendEmail = false
  809. return
  810. }
  811. br.Msg = "获取分类信息失败"
  812. br.ErrMsg = "获取分类信息失败,Err:" + e.Error()
  813. return
  814. }
  815. if excelClassify == nil {
  816. br.Msg = "分类不存在"
  817. br.ErrMsg = "分类不存在"
  818. br.IsSendEmail = false
  819. return
  820. }
  821. }
  822. if excelInfo.ParentId > 0 {
  823. parentExcelInfo, e := excel3.GetExcelInfoById(excelInfo.ParentId)
  824. if e != nil {
  825. if utils.IsErrNoRow(e) {
  826. br.Msg = "父级ETA表格被删除,请刷新页面"
  827. return
  828. }
  829. br.Msg = "获取父级ETA表格信息失败"
  830. br.ErrMsg = "获取父级ETA表格信息失败,Err:" + e.Error()
  831. return
  832. }
  833. req.ExcelClassifyId = parentExcelInfo.ExcelClassifyId
  834. }
  835. //判断表格是否存在
  836. var condition string
  837. var pars []interface{}
  838. condition += " AND excel_info_id != ? "
  839. pars = append(pars, req.ExcelInfoId)
  840. condition += " AND excel_classify_id=? AND parent_id=?"
  841. pars = append(pars, req.ExcelClassifyId, excelInfo.ParentId)
  842. condition += " AND excel_name=? "
  843. pars = append(pars, req.ExcelName)
  844. // 获取分类下是否存在该表格名称
  845. count, err := excel3.GetExcelInfoCountByCondition(condition, pars)
  846. if err != nil {
  847. br.Msg = "ETA判断表格名称是否存在失败"
  848. br.ErrMsg = "判断ETA表格名称是否存在失败,Err:" + err.Error()
  849. return
  850. }
  851. if count > 0 {
  852. br.Msg = "ETA表格名称已存在,请重新填写"
  853. br.IsSendEmail = false
  854. return
  855. }
  856. // 操作权限校验
  857. {
  858. // 数据权限
  859. haveOperaAuth, err := data_manage_permission.CheckExcelPermissionByExcelInfoId(checkExcelInfo.ExcelInfoId, checkExcelInfo.ExcelClassifyId, checkExcelInfo.IsJoinPermission, c.SysUser.AdminId)
  860. if err != nil {
  861. br.Msg = "获取ETA表格失败"
  862. br.ErrMsg = "获取ETA表格权限失败,Err:" + err.Error()
  863. return
  864. }
  865. var button excel3.ExcelInfoDetailButton
  866. if checkExcelInfo.Source == utils.BALANCE_TABLE {
  867. button = excel2.GetBalanceExcelInfoOpButton(sysUser.AdminId, checkExcelInfo.SysUserId, haveOperaAuth, checkExcelInfo.ExcelInfoId)
  868. } else {
  869. button = excel2.GetExcelInfoOpButton(sysUser, excelInfo.SysUserId, excelInfo.Source, haveOperaAuth)
  870. }
  871. if !button.OpButton {
  872. br.Msg = "无操作权限"
  873. br.IsSendEmail = false
  874. return
  875. }
  876. }
  877. // 引用的指标id
  878. edbInfoIdList := make([]int, 0)
  879. content := req.Content
  880. contentFlag := false
  881. if req.Content != excelInfo.Content {
  882. contentFlag = true
  883. }
  884. balanceTableData := make([][]request.MixedTableCellDataReq, 0)
  885. switch excelInfo.Source {
  886. case utils.TIME_TABLE: // 自定义表格
  887. jsonStrByte, err := json.Marshal(req.TableData)
  888. if err != nil {
  889. br.Msg = "时间序列表格数据获取失败"
  890. br.ErrMsg = "时间序列表格数据获取失败,Err:" + err.Error()
  891. return
  892. }
  893. var tableData request.TableDataReq
  894. err = json.Unmarshal(jsonStrByte, &tableData)
  895. if err != nil {
  896. br.Msg = "时间序列表格数据获取失败"
  897. br.ErrMsg = "时间序列表格数据获取失败,Err:" + err.Error()
  898. return
  899. }
  900. edbInfoIdList = tableData.EdbInfoIdList
  901. tableDataConfig, err := excel2.GetTableDataConfig(tableData)
  902. if err != nil {
  903. br.Msg = "时间序列表格数据获取失败"
  904. br.ErrMsg = "时间序列表格数据获取失败,Err:" + err.Error()
  905. return
  906. }
  907. tableDataConfig.DecimalConfig = tableData.DecimalConfig
  908. contentByte, err := json.Marshal(tableDataConfig)
  909. if err != nil {
  910. br.Msg = "时间序列表格数据获取失败"
  911. br.ErrMsg = "时间序列表格数据获取失败,Err:" + err.Error()
  912. return
  913. }
  914. content = string(contentByte)
  915. case utils.MIXED_TABLE, utils.BALANCE_TABLE: // 混合表格, 平衡表
  916. contentByte, err := json.Marshal(req.TableData)
  917. if err != nil {
  918. br.Msg = "混合表格数据获取失败"
  919. br.ErrMsg = "混合表格数据获取后,转json失败,Err:" + err.Error()
  920. return
  921. }
  922. content = string(contentByte)
  923. var result request.MixedTableReq
  924. err = json.Unmarshal(contentByte, &result)
  925. if err != nil {
  926. br.Msg = "获取失败"
  927. br.ErrMsg = "表格json转结构体失败,Err:" + err.Error()
  928. return
  929. }
  930. newResult, err, errMsg := excel2.GetMixedTableCellData(result, c.Lang)
  931. if err != nil {
  932. br.Msg = "获取失败"
  933. if errMsg != `` {
  934. br.Msg = errMsg
  935. }
  936. br.ErrMsg = "获取最新的数据失败,Err:" + err.Error()
  937. return
  938. }
  939. balanceTableData = newResult
  940. edbInfoIdMap := make(map[int]int)
  941. for _, tmpV := range newResult {
  942. for _, v := range tmpV {
  943. if v.EdbInfoId > 0 {
  944. if _, ok := edbInfoIdMap[v.EdbInfoId]; !ok {
  945. edbInfoIdMap[v.EdbInfoId] = v.EdbInfoId
  946. edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
  947. }
  948. }
  949. }
  950. }
  951. }
  952. excelInfo.ModifyTime = time.Now()
  953. excelInfo.ExcelName = req.ExcelName
  954. excelInfo.ExcelType = req.ExcelType
  955. excelInfo.ExcelClassifyId = req.ExcelClassifyId
  956. excelInfo.ExcelImage = req.ExcelImage
  957. excelInfo.UpdateUserId = sysUser.AdminId
  958. excelInfo.UpdateUserRealName = sysUser.RealName
  959. excelInfo.Content = content
  960. excelInfo.SourcesFrom = req.SourcesFrom
  961. // 自动保存时不会传缩略图,也就不更新这个字段
  962. var updateExcelInfoParams []string
  963. if req.ExcelImage != "" {
  964. updateExcelInfoParams = []string{"ModifyTime", "ExcelName", "ExcelType", "ExcelClassifyId", "ExcelImage", "Content", "SourcesFrom"}
  965. } else {
  966. updateExcelInfoParams = []string{"ModifyTime", "ExcelName", "ExcelType", "ExcelClassifyId", "Content", "SourcesFrom"}
  967. }
  968. excelEdbMappingList := make([]*excel3.ExcelEdbMapping, 0)
  969. if len(edbInfoIdList) > 0 {
  970. for _, edbInfoId := range edbInfoIdList {
  971. excelEdbMappingList = append(excelEdbMappingList, &excel3.ExcelEdbMapping{
  972. //ExcelEdbMappingId: 0,
  973. ExcelInfoId: excelInfo.ExcelInfoId,
  974. Source: excelInfo.Source,
  975. EdbInfoId: edbInfoId,
  976. CreateTime: time.Now(),
  977. ModifyTime: time.Now(),
  978. })
  979. }
  980. }
  981. err = excel3.EditExcelInfo(excelInfo, updateExcelInfoParams, excelEdbMappingList)
  982. if err != nil {
  983. br.Msg = "保存失败"
  984. br.ErrMsg = "保存失败,Err:" + err.Error()
  985. return
  986. }
  987. // 更新excel下载地址(默认的EXCEL需要更新,自定义表格不需要更新)
  988. if excelInfo.Source == 1 {
  989. go excel2.UpdateExcelInfoFileUrl(excelInfo)
  990. }
  991. // 更新平衡表图表指标
  992. if excelInfo.Source == utils.BALANCE_TABLE && excelInfo.BalanceType == 1 && contentFlag == true { //静态表更新表数据
  993. err = excel2.SyncBalanceEdbData(excelInfo, balanceTableData)
  994. if err != nil {
  995. br.Msg = "操作失败"
  996. br.ErrMsg = err.Error()
  997. return
  998. }
  999. }
  1000. // 加入草稿
  1001. {
  1002. excelDraftInfo := &excel3.ExcelDraft{
  1003. //ExcelDraftId: 0,
  1004. ExcelId: req.ExcelInfoId,
  1005. Name: req.ExcelName,
  1006. Content: content,
  1007. OpUserId: sysUser.AdminId,
  1008. OpUserName: sysUser.RealName,
  1009. CreateTime: time.Now(),
  1010. }
  1011. err = excel3.AddExcelDraft(excelDraftInfo)
  1012. }
  1013. // 判断是否清除该表引用
  1014. if req.IsColChange {
  1015. err = excel3.DeleteReferencedExcelConfig(excelInfo.UniqueCode)
  1016. if err != nil {
  1017. br.Msg = "操作失败"
  1018. br.ErrMsg = err.Error()
  1019. return
  1020. }
  1021. }
  1022. resp := response.AddExcelInfoResp{
  1023. ExcelInfoId: excelInfo.ExcelInfoId,
  1024. UniqueCode: excelInfo.UniqueCode,
  1025. }
  1026. //新增指标引用记录
  1027. _ = data.SaveExcelEdbInfoRelation(excelInfo.ExcelInfoId, excelInfo.Source, false)
  1028. //删除公共图库那边的缓存
  1029. _ = utils.Rc.Delete(utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL + ":" + excelInfo.UniqueCode)
  1030. br.Ret = 200
  1031. br.Success = true
  1032. br.Msg = "修改成功"
  1033. br.Data = resp
  1034. br.IsAddLog = true
  1035. }
  1036. // Move
  1037. // @Title 移动表格接口
  1038. // @Description 移动表格接口
  1039. // @Param request body request.MoveExcelInfoReq true "type json string"
  1040. // @Success Ret=200 移动成功
  1041. // @router /excel_info/move [post]
  1042. func (c *ExcelInfoController) Move() {
  1043. br := new(models.BaseResponse).Init()
  1044. defer func() {
  1045. c.Data["json"] = br
  1046. c.ServeJSON()
  1047. }()
  1048. /*
  1049. sysUser := c.SysUser
  1050. if sysUser == nil {
  1051. br.Msg = "请登录"
  1052. br.ErrMsg = "请登录,SysUser Is Empty"
  1053. br.Ret = 408
  1054. return
  1055. }
  1056. var req request.MoveExcelInfoReq
  1057. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  1058. if err != nil {
  1059. br.Msg = "参数解析异常!"
  1060. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1061. return
  1062. }
  1063. if req.ExcelInfoId <= 0 {
  1064. br.Msg = "参数错误"
  1065. br.ErrMsg = "表格id小于等于0"
  1066. br.IsSendEmail = false
  1067. return
  1068. }
  1069. if req.ExcelClassifyId <= 0 {
  1070. br.Msg = "请选择分类"
  1071. br.IsSendEmail = false
  1072. return
  1073. }
  1074. //判断分类是否存在
  1075. _, err = excel3.GetExcelClassifyById(req.ExcelClassifyId)
  1076. if err != nil {
  1077. br.Msg = "移动失败"
  1078. br.ErrMsg = "获取分类信息失败" + err.Error()
  1079. if utils.IsErrNoRow(err) {
  1080. br.Msg = "分类已被删除,不可移动,请刷新页面"
  1081. br.ErrMsg = "分类已被删除,不可移动,请刷新页面"
  1082. br.IsSendEmail = false
  1083. }
  1084. return
  1085. }
  1086. // 获取表格信息
  1087. excelInfo, err := excel3.GetExcelInfoById(req.ExcelInfoId)
  1088. if err != nil {
  1089. br.Msg = "移动失败"
  1090. br.ErrMsg = "获取表格信息失败,Err:" + err.Error()
  1091. return
  1092. }
  1093. //如果改变了分类,那么移动该表格数据
  1094. if excelInfo.ExcelClassifyId != req.ExcelClassifyId {
  1095. //查询需要修改的分类下是否存在同一个表格名称
  1096. tmpExcelInfo, tmpErr := excel3.GetExcelInfoByClassifyIdAndName(req.ExcelClassifyId, excelInfo.ExcelName)
  1097. if tmpErr != nil && !utils.IsErrNoRow(tmpErr) {
  1098. br.Msg = "移动失败"
  1099. br.ErrMsg = "移动失败,Err:" + tmpErr.Error()
  1100. return
  1101. }
  1102. if tmpExcelInfo != nil {
  1103. br.Msg = "移动失败,同一个分类下表格名称不允许重复"
  1104. br.ErrMsg = "移动失败,同一个分类下表格名称不允许重复"
  1105. br.IsSendEmail = false
  1106. return
  1107. }
  1108. // 变更表格的所属分类
  1109. excelInfo.ExcelClassifyId = req.ExcelClassifyId
  1110. err = excelInfo.Update([]string{"ExcelClassifyId"})
  1111. if err != nil {
  1112. br.Msg = "移动失败"
  1113. br.ErrMsg = "移动失败,Err:" + err.Error()
  1114. return
  1115. }
  1116. }
  1117. //移动排序
  1118. updateCol := make([]string, 0)
  1119. //如果有传入 上一个兄弟节点分类id
  1120. if req.PrevExcelInfoId > 0 {
  1121. prevExcelInfo, err := excel3.GetExcelInfoById(req.PrevExcelInfoId)
  1122. if err != nil {
  1123. br.Msg = "移动失败"
  1124. br.ErrMsg = "获取上一个兄弟节点分类信息失败,Err:" + err.Error()
  1125. return
  1126. }
  1127. //如果是移动在两个兄弟节点之间
  1128. if req.NextExcelInfoId > 0 {
  1129. //下一个兄弟节点
  1130. nextExcelInfo, err := excel3.GetExcelInfoById(req.NextExcelInfoId)
  1131. if err != nil {
  1132. br.Msg = "移动失败"
  1133. br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error()
  1134. return
  1135. }
  1136. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  1137. if prevExcelInfo.Sort == nextExcelInfo.Sort || prevExcelInfo.Sort == excelInfo.Sort {
  1138. //变更兄弟节点的排序
  1139. updateSortStr := `sort + 2`
  1140. _ = excel3.UpdateExcelInfoSortByClassifyId(prevExcelInfo.ExcelClassifyId, prevExcelInfo.Sort, prevExcelInfo.ExcelInfoId, updateSortStr)
  1141. } else {
  1142. //如果下一个兄弟的排序权重正好是上个兄弟节点下一层,那么需要再加一层了
  1143. if nextExcelInfo.Sort-prevExcelInfo.Sort == 1 {
  1144. //变更兄弟节点的排序
  1145. updateSortStr := `sort + 1`
  1146. _ = excel3.UpdateExcelInfoSortByClassifyId(prevExcelInfo.ExcelClassifyId, prevExcelInfo.Sort, prevExcelInfo.ExcelInfoId, updateSortStr, source)
  1147. }
  1148. }
  1149. }
  1150. // 变更表格在当前分类下的排序
  1151. excelInfo.Sort = prevExcelInfo.Sort + 1
  1152. excelInfo.ModifyTime = time.Now()
  1153. updateCol = append(updateCol, "Sort", "ModifyTime")
  1154. } else {
  1155. firstClassify, err := excel3.GetFirstExcelInfoByClassifyId(req.ExcelClassifyId)
  1156. if err != nil && !utils.IsErrNoRow(err) {
  1157. br.Msg = "移动失败"
  1158. br.ErrMsg = "获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + err.Error()
  1159. return
  1160. }
  1161. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  1162. if firstClassify != nil && firstClassify.ChartClassifyId > 0 && firstClassify.Sort == 0 {
  1163. updateSortStr := ` sort + 1 `
  1164. _ = excel3.UpdateExcelInfoSortByClassifyId(firstClassify.ExcelClassifyId, 0, firstClassify.ExcelInfoId+1, updateSortStr)
  1165. }
  1166. // 变更表格在当前分类下的排序
  1167. excelInfo.Sort = 0 //那就是排在第一位
  1168. excelInfo.ModifyTime = time.Now()
  1169. updateCol = append(updateCol, "Sort", "ModifyTime")
  1170. }
  1171. //更新
  1172. if len(updateCol) > 0 {
  1173. err = excelInfo.Update(updateCol)
  1174. if err != nil {
  1175. br.Msg = "移动失败"
  1176. br.ErrMsg = "修改失败,Err:" + err.Error()
  1177. return
  1178. }
  1179. }
  1180. if err != nil {
  1181. br.Msg = "移动失败"
  1182. br.ErrMsg = "修改失败,Err:" + err.Error()
  1183. return
  1184. }
  1185. //新增操作日志
  1186. //{
  1187. // ExcelLog := new(data_manage.ExcelInfoLog)
  1188. // ExcelLog.ExcelName = ExcelInfo.ExcelName
  1189. // ExcelLog.ExcelInfoId = req.ExcelInfoId
  1190. // ExcelLog.ExcelClassifyId = ExcelInfo.ExcelClassifyId
  1191. // ExcelLog.SysUserId = sysUser.AdminId
  1192. // ExcelLog.SysUserRealName = sysUser.RealName
  1193. // ExcelLog.UniqueCode = ExcelInfo.UniqueCode
  1194. // ExcelLog.CreateTime = time.Now()
  1195. // ExcelLog.Content = string(c.Ctx.Input.RequestBody)
  1196. // ExcelLog.Status = "移动表格"
  1197. // ExcelLog.Method = c.Ctx.Input.URL()
  1198. // go data_manage.AddExcelInfoLog(ExcelLog)
  1199. //}
  1200. */
  1201. br.Ret = 200
  1202. br.Success = true
  1203. br.IsAddLog = true
  1204. br.Msg = "移动成功"
  1205. }
  1206. // Delete
  1207. // @Title 删除表格接口
  1208. // @Description 删除表格接口
  1209. // @Param request body request.DeleteExcelInfoReq true "type json string"
  1210. // @Success Ret=200 删除成功
  1211. // @router /excel_info/delete [post]
  1212. func (c *ExcelInfoController) Delete() {
  1213. br := new(models.BaseResponse).Init()
  1214. defer func() {
  1215. c.Data["json"] = br
  1216. c.ServeJSON()
  1217. }()
  1218. resp := response.AddExcelInfoResp{}
  1219. sysUser := c.SysUser
  1220. if sysUser == nil {
  1221. br.Msg = "请登录"
  1222. br.ErrMsg = "请登录,SysUser Is Empty"
  1223. br.Ret = 408
  1224. return
  1225. }
  1226. var req request.DeleteExcelInfoReq
  1227. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  1228. if err != nil {
  1229. br.Msg = "参数解析异常!"
  1230. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1231. return
  1232. }
  1233. if req.ExcelInfoId <= 0 {
  1234. br.Msg = "参数错误"
  1235. br.ErrMsg = "表格id小于等于0"
  1236. br.IsSendEmail = false
  1237. return
  1238. }
  1239. // 获取表格信息
  1240. excelInfo, err := excel3.GetExcelInfoById(req.ExcelInfoId)
  1241. if err != nil {
  1242. br.Msg = "删除失败"
  1243. br.ErrMsg = "获取表格信息失败,Err:" + err.Error()
  1244. return
  1245. }
  1246. // 删除excel
  1247. err, errMsg, isSendEmail := excel2.Delete(excelInfo, sysUser)
  1248. if err != nil {
  1249. br.Msg = "删除失败"
  1250. if errMsg != `` {
  1251. br.Msg = errMsg
  1252. }
  1253. br.ErrMsg = "删除失败,Err:" + err.Error()
  1254. br.IsSendEmail = isSendEmail
  1255. return
  1256. }
  1257. // 返回下一个表格的信息
  1258. {
  1259. var nextItem *excel3.ExcelInfo
  1260. var condition string
  1261. var pars []interface{}
  1262. condition += " AND excel_classify_id=? AND parent_id=0"
  1263. pars = append(pars, excelInfo.ExcelClassifyId)
  1264. condition += " AND sort>=? "
  1265. pars = append(pars, excelInfo.Sort)
  1266. nextItem, err = excel3.GetNextExcelInfoByCondition(condition, pars)
  1267. if err != nil && !utils.IsErrNoRow(err) {
  1268. br.Msg = "删除失败"
  1269. br.ErrMsg = "获取下一级表格信息失败,Err:" + err.Error()
  1270. return
  1271. }
  1272. // 如果没找到,那么查找下一个分类的第一个表格
  1273. if nextItem == nil {
  1274. currClassifyInfo, err := excel3.GetExcelClassifyById(excelInfo.ExcelClassifyId)
  1275. if err != nil && !utils.IsErrNoRow(err) {
  1276. br.Msg = "删除失败"
  1277. br.ErrMsg = "获取当前表格分类信息失败,Err:" + err.Error()
  1278. return
  1279. }
  1280. //condition += " AND level=3 "
  1281. //pars = append(pars, chartInfo.ExcelClassifyId)
  1282. //var condition string
  1283. //var pars []interface{}
  1284. //condition += " AND sort>=? ORDER BY excel_classify_id ASC LIMIT 1 "
  1285. //pars = append(pars, currClassifyInfo.Sort)
  1286. //
  1287. //classifyItem, err := data_manage.GetNextExcelClassifyByCondition(condition, pars)
  1288. //if err != nil && !utils.IsErrNoRow(err) {
  1289. // br.Msg = "删除失败"
  1290. // br.ErrMsg = "获取下一级表格分类信息失败,Err:" + err.Error()
  1291. // return
  1292. //}
  1293. //if classifyItem != nil {
  1294. // nextItem, err = data_manage.GetNextExcelInfo(excelInfo.ExcelClassifyId,)
  1295. // if err != nil && !utils.IsErrNoRow(err) {
  1296. // br.Msg = "删除失败"
  1297. // br.ErrMsg = "获取下一级表格信息失败,Err:" + err.Error()
  1298. // return
  1299. // }
  1300. //}
  1301. nextItem, err = excel3.GetNextExcelInfo(excelInfo.ExcelClassifyId, currClassifyInfo.Sort, currClassifyInfo.Source)
  1302. if err != nil && !utils.IsErrNoRow(err) {
  1303. br.Msg = "删除失败"
  1304. br.ErrMsg = "获取下一级表格信息失败,Err:" + err.Error()
  1305. return
  1306. }
  1307. }
  1308. // 如果找到下一个表格了,那么就返回
  1309. if nextItem != nil {
  1310. resp = response.AddExcelInfoResp{
  1311. ExcelInfoId: nextItem.ExcelInfoId,
  1312. UniqueCode: nextItem.UniqueCode,
  1313. }
  1314. }
  1315. }
  1316. //删除公共图库那边的缓存
  1317. _ = utils.Rc.Delete(utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL)
  1318. br.Ret = 200
  1319. br.Success = true
  1320. br.IsAddLog = true
  1321. br.Data = resp
  1322. br.Msg = "删除成功"
  1323. }
  1324. // AddDraft
  1325. // @Title 新增表格草稿接口
  1326. // @Description 新增表格草稿接口
  1327. // @Param request body request.AddExcelDraftReq true "type json string"
  1328. // @Success Ret=200 添加成功
  1329. // @router /excel_info/draft/add [post]
  1330. func (c *ExcelInfoController) AddDraft() {
  1331. br := new(models.BaseResponse).Init()
  1332. defer func() {
  1333. c.Data["json"] = br
  1334. c.ServeJSON()
  1335. }()
  1336. sysUser := c.SysUser
  1337. if sysUser == nil {
  1338. br.Msg = "请登录"
  1339. br.ErrMsg = "请登录,SysUser Is Empty"
  1340. br.Ret = 408
  1341. return
  1342. }
  1343. var req request.AddExcelDraftReq
  1344. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  1345. if err != nil {
  1346. br.Msg = "参数解析异常!"
  1347. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1348. return
  1349. }
  1350. req.ExcelName = strings.Trim(req.ExcelName, " ")
  1351. if req.ExcelName == "" {
  1352. br.Msg = "请填写表格名称!"
  1353. br.IsSendEmail = false
  1354. return
  1355. }
  1356. if req.ExcelClassifyId <= 0 {
  1357. br.Msg = "分类参数错误!"
  1358. br.IsSendEmail = false
  1359. return
  1360. }
  1361. excelClassify, err := excel3.GetExcelClassifyById(req.ExcelClassifyId)
  1362. if err != nil {
  1363. if utils.IsErrNoRow(err) {
  1364. br.Msg = "分类不存在"
  1365. br.ErrMsg = "分类不存在"
  1366. br.IsSendEmail = false
  1367. return
  1368. }
  1369. br.Msg = "获取分类信息失败"
  1370. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  1371. return
  1372. }
  1373. if excelClassify == nil {
  1374. br.Msg = "分类不存在"
  1375. br.ErrMsg = "分类不存在"
  1376. br.IsSendEmail = false
  1377. return
  1378. }
  1379. var condition string
  1380. var pars []interface{}
  1381. condition += " AND excel_classify_id=? "
  1382. pars = append(pars, req.ExcelClassifyId)
  1383. condition += " AND excel_name=? "
  1384. pars = append(pars, req.ExcelName)
  1385. // 获取分类下是否存在该表格名称
  1386. _, err = excel3.GetExcelInfoById(req.ExcelInfoId)
  1387. if err != nil {
  1388. br.Msg = "判断表格名称是否存在失败"
  1389. br.ErrMsg = "判断表格名称是否存在失败,Err:" + err.Error()
  1390. return
  1391. }
  1392. excelDraftInfo := &excel3.ExcelDraft{
  1393. //ExcelDraftId: 0,
  1394. ExcelId: req.ExcelInfoId,
  1395. Name: req.ExcelName,
  1396. Content: req.Content,
  1397. OpUserId: sysUser.AdminId,
  1398. OpUserName: sysUser.RealName,
  1399. CreateTime: time.Now(),
  1400. }
  1401. err = excel3.AddExcelDraft(excelDraftInfo)
  1402. if err != nil {
  1403. br.Msg = "保存失败"
  1404. br.ErrMsg = "保存失败,Err:" + err.Error()
  1405. return
  1406. }
  1407. _, err = services.UpdateExcelEditMark(req.ExcelInfoId, sysUser.AdminId, 1, sysUser.RealName)
  1408. if err != nil {
  1409. br.Msg = "查询标记状态失败"
  1410. br.ErrMsg = "查询标记状态失败,Err:" + err.Error()
  1411. return
  1412. }
  1413. br.Ret = 200
  1414. br.Success = true
  1415. br.Msg = "添加成功"
  1416. }
  1417. // GetExcelTableData
  1418. // @Title 获取excel表格的table数据
  1419. // @Description 获取excel表格的table数据接口
  1420. // @Param UniqueCode query string true "表格code"
  1421. // @Param FromScene query int true "场景来源,1:智能研报,2:研报列表;3:英文研报;4:中文PPT;5:英文PPT"
  1422. // @Success 200 {object} response.ExcelTableDetailResp
  1423. // @router /excel_info/table_data [get]
  1424. func (c *ExcelInfoController) GetExcelTableData() {
  1425. br := new(models.BaseResponse).Init()
  1426. defer func() {
  1427. c.Data["json"] = br
  1428. c.ServeJSON()
  1429. }()
  1430. sysUser := c.SysUser
  1431. if sysUser == nil {
  1432. br.Msg = "请登录"
  1433. br.ErrMsg = "请登录,SysUser Is Empty"
  1434. br.Ret = 408
  1435. return
  1436. }
  1437. uniqueCode := c.GetString("UniqueCode")
  1438. fromScene, _ := c.GetInt("FromScene", 0)
  1439. referencedId, _ := c.GetInt("ReferencedId", 0)
  1440. uuid := c.GetString("Uuid")
  1441. var err error
  1442. if uniqueCode == `` {
  1443. br.Msg = "请选择表格"
  1444. br.ErrMsg = "UniqueCode未传"
  1445. br.IsSendEmail = false
  1446. return
  1447. }
  1448. //获取eta表格信息
  1449. excelInfo, err := excel3.GetExcelInfoByUniqueCode(uniqueCode)
  1450. if err != nil {
  1451. br.Msg = "获取失败"
  1452. br.ErrMsg = "获取ETA表格信息失败,Err:" + err.Error()
  1453. if utils.IsErrNoRow(err) {
  1454. br.Msg = "ETA表格被删除,请刷新页面"
  1455. br.ErrMsg = "ETA表格被删除,请刷新页面,Err:" + err.Error()
  1456. br.IsSendEmail = false
  1457. }
  1458. return
  1459. }
  1460. var tableData excel.TableData
  1461. var excelSource, excelSourceEn string
  1462. switch excelInfo.Source {
  1463. case utils.EXCEL_DEFAULT:
  1464. luckySheetData, err := excel.GetLuckySheetData(excelInfo.Content)
  1465. if err != nil {
  1466. br.Msg = "获取失败"
  1467. br.ErrMsg = "获取excel数据失败,Err:" + err.Error()
  1468. return
  1469. }
  1470. tableData, err = luckySheetData.GetTableDataByLuckySheetDataStr(true)
  1471. if err != nil {
  1472. br.Msg = "获取失败"
  1473. br.ErrMsg = "转换成table失败,Err:" + err.Error()
  1474. return
  1475. }
  1476. case utils.TIME_TABLE:
  1477. var tableDataConfig excel2.TableDataConfig
  1478. err = json.Unmarshal([]byte(excelInfo.Content), &tableDataConfig)
  1479. if err != nil {
  1480. br.Msg = "获取失败"
  1481. br.ErrMsg = "表格json转结构体失败,Err:" + err.Error()
  1482. return
  1483. }
  1484. result, err := excel2.GetDataByTableDataConfig(tableDataConfig)
  1485. if err != nil {
  1486. br.Msg = "获取失败"
  1487. br.ErrMsg = "获取最新的表格数据失败,Err:" + err.Error()
  1488. return
  1489. }
  1490. tableData, err = excel.GetTableDataByCustomData(excelInfo.ExcelType, result, c.Lang)
  1491. if err != nil {
  1492. br.Msg = "获取失败"
  1493. br.ErrMsg = "转换成table失败,Err:" + err.Error()
  1494. return
  1495. }
  1496. sourceNameList, sourceNameEnList, err := excel2.GetEdbSourceByEdbInfoIdList(result.EdbInfoIdList)
  1497. if err != nil {
  1498. br.Msg = "自定义表格数据获取失败"
  1499. br.ErrMsg = "自定义表格数据获取失败,Err:" + err.Error()
  1500. return
  1501. }
  1502. excelSource = strings.Join(sourceNameList, ",")
  1503. excelSourceEn = strings.Join(sourceNameEnList, ",")
  1504. case utils.MIXED_TABLE:
  1505. var result request.MixedTableReq
  1506. err = json.Unmarshal([]byte(excelInfo.Content), &result)
  1507. if err != nil {
  1508. br.Msg = "获取失败"
  1509. br.ErrMsg = "表格json转结构体失败,Err:" + err.Error()
  1510. return
  1511. }
  1512. newResult, err, errMsg := excel2.GetMixedTableCellData(result, c.Lang)
  1513. if err != nil {
  1514. br.Msg = "获取失败"
  1515. if errMsg != `` {
  1516. br.Msg = errMsg
  1517. }
  1518. br.ErrMsg = "获取最新的数据失败,Err:" + err.Error()
  1519. return
  1520. }
  1521. tableData, err = excel.GetTableDataByMixedTableData(newResult, true, excelInfo.ExcelInfoId)
  1522. if err != nil {
  1523. br.Msg = "获取失败"
  1524. br.ErrMsg = "转换成table失败,Err:" + err.Error()
  1525. return
  1526. }
  1527. edbInfoIds := make([]int, 0)
  1528. edbInfoIdExist := make(map[int]bool)
  1529. if len(newResult) > 0 {
  1530. for _, t := range newResult {
  1531. for _, v := range t {
  1532. if v.EdbInfoId > 0 && !edbInfoIdExist[v.EdbInfoId] {
  1533. edbInfoIdExist[v.EdbInfoId] = true
  1534. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  1535. }
  1536. }
  1537. }
  1538. }
  1539. sourceNameList, sourceNameEnList, err := excel2.GetEdbSourceByEdbInfoIdList(edbInfoIds)
  1540. if err != nil {
  1541. br.Msg = "自定义表格数据获取失败"
  1542. br.ErrMsg = "自定义表格数据获取失败,Err:" + err.Error()
  1543. return
  1544. }
  1545. excelSource = strings.Join(sourceNameList, ",")
  1546. excelSourceEn = strings.Join(sourceNameEnList, ",")
  1547. }
  1548. tableData = excel.HandleTableCell(tableData)
  1549. // tableData, err = excel.HandleRuleToTableCell(excelInfo.ExcelInfoId, tableData)
  1550. if err != nil {
  1551. utils.FileLog.Info("表格管理规则处理失败,HandleRuleToTableCell err:", err.Error())
  1552. }
  1553. config := response.ExcelTableDetailConfigResp{
  1554. FontSize: 9,
  1555. }
  1556. // 获取配置的字体大小
  1557. confName := models.FromSceneMap[fromScene]
  1558. if confName != `` {
  1559. busConf, err := models.GetBusinessConfByKey(confName)
  1560. if err == nil {
  1561. sizeDeci, err := decimal.NewFromString(busConf.ConfVal)
  1562. if err == nil {
  1563. config.FontSize = int(sizeDeci.IntPart())
  1564. }
  1565. }
  1566. }
  1567. // 获取表格引用
  1568. reference, err := excel3.GetReferencedExcelConfig(referencedId, fromScene, uniqueCode, uuid)
  1569. if err != nil && !utils.IsErrNoRow(err) {
  1570. br.Msg = "获取失败"
  1571. br.ErrMsg = "获取表格引用失败,Err:" + err.Error()
  1572. return
  1573. }
  1574. resp := response.ExcelTableDetailResp{
  1575. UniqueCode: excelInfo.UniqueCode,
  1576. ExcelImage: excelInfo.ExcelImage,
  1577. ExcelName: excelInfo.ExcelName,
  1578. TableInfo: tableData,
  1579. Config: config,
  1580. SourcesFrom: excelInfo.SourcesFrom,
  1581. ExcelSource: excelSource,
  1582. ExcelSourceEn: excelSourceEn,
  1583. ExcelInfoId: excelInfo.ExcelInfoId,
  1584. Source: excelInfo.Source,
  1585. ReferencedExcelConfig: reference,
  1586. }
  1587. br.Ret = 200
  1588. br.Success = true
  1589. br.Msg = "获取成功"
  1590. br.Data = resp
  1591. }
  1592. // Calculate
  1593. // @Title 公式计算接口
  1594. // @Description 公式计算接口
  1595. // @Param request body request.CalculateReq true "type json string"
  1596. // @router /excel_info/table/calculate [post]
  1597. func (c *ExcelInfoController) Calculate() {
  1598. br := new(models.BaseResponse).Init()
  1599. defer func() {
  1600. c.Data["json"] = br
  1601. c.ServeJSON()
  1602. }()
  1603. sysUser := c.SysUser
  1604. if sysUser == nil {
  1605. br.Msg = "请登录"
  1606. br.ErrMsg = "请登录,SysUser Is Empty"
  1607. br.Ret = 408
  1608. return
  1609. }
  1610. var req request.CalculateReq
  1611. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  1612. if err != nil {
  1613. br.Msg = "参数解析异常!"
  1614. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1615. return
  1616. }
  1617. if req.CalculateFormula == "" {
  1618. br.Msg = "请填写公式"
  1619. return
  1620. }
  1621. calculateFormula := req.CalculateFormula
  1622. calculateFormula = strings.TrimPrefix(calculateFormula, "=")
  1623. calculateFormula = strings.Replace(calculateFormula, "(", "(", -1)
  1624. calculateFormula = strings.Replace(calculateFormula, ")", ")", -1)
  1625. calculateFormula = strings.Replace(calculateFormula, ",", ",", -1)
  1626. calculateFormula = strings.Replace(calculateFormula, "。", ".", -1)
  1627. calculateFormula = strings.Replace(calculateFormula, "%", "*0.01", -1)
  1628. formulaFormStr := utils.ReplaceFormula(req.TagMap, calculateFormula)
  1629. //计算公式异常,那么就移除该指标
  1630. if formulaFormStr == `` {
  1631. //removeDateList = append(removeDateList, sk)
  1632. //fmt.Println("异常了")
  1633. br.Msg = "公式异常"
  1634. br.ErrMsg = "公式异常"
  1635. return
  1636. }
  1637. expression := formula.NewExpression(formulaFormStr)
  1638. calResult, err := expression.Evaluate()
  1639. if err != nil {
  1640. br.Msg = "计算失败"
  1641. br.ErrMsg = "计算失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr
  1642. // 分母为0的报错
  1643. if strings.Contains(err.Error(), "divide by zero") {
  1644. br.Msg = "分母不能为0"
  1645. br.ErrMsg = "分母不能为空,计算公式:" + formulaFormStr
  1646. br.IsSendEmail = false
  1647. }
  1648. return
  1649. }
  1650. // 如果计算结果是NAN,那么就提示报错
  1651. if calResult.IsNan() {
  1652. br.Msg = "计算失败"
  1653. br.ErrMsg = "计算失败:计算结果是:NAN;formulaStr:" + formulaFormStr
  1654. return
  1655. }
  1656. // 计算结果格式化
  1657. calFloat, e := calResult.Float64()
  1658. if e != nil {
  1659. br.Msg = "计算失败"
  1660. br.ErrMsg = "计算失败, Err: " + e.Error()
  1661. return
  1662. }
  1663. calVal := utils.FormatTableDataShowValue(calFloat)
  1664. //calVal := calResult.String()
  1665. //if err != nil {
  1666. // br.Msg = "计算失败"
  1667. // br.ErrMsg = "计算失败,结果转 string 失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr
  1668. // return
  1669. //}
  1670. br.Ret = 200
  1671. br.Success = true
  1672. br.Msg = "计算成功"
  1673. br.Data = calVal
  1674. }
  1675. // GetFirstEdbData
  1676. // @Title 获取未来日期数据接口
  1677. // @Description 获取未来日期数据接口
  1678. // @Param EdbInfoId query int true "指标id"
  1679. // @Param Num query int true "默认期数"
  1680. // @Param SortType query string true "如何排序,是正序还是倒序,枚举值:`asc 正序`,`desc 倒序`,不传默认倒序"
  1681. // @router /excel_info/table/first_edb_data_list [get]
  1682. func (c *ExcelInfoController) GetFirstEdbData() {
  1683. br := new(models.BaseResponse).Init()
  1684. defer func() {
  1685. c.Data["json"] = br
  1686. c.ServeJSON()
  1687. }()
  1688. sysUser := c.SysUser
  1689. if sysUser == nil {
  1690. br.Msg = "请登录"
  1691. br.ErrMsg = "请登录,SysUser Is Empty"
  1692. br.Ret = 408
  1693. return
  1694. }
  1695. edbInfoId, _ := c.GetInt("EdbInfoId")
  1696. num, _ := c.GetInt("Num")
  1697. sortType := c.GetString("SortType")
  1698. if edbInfoId <= 0 {
  1699. br.Msg = "请选择指标"
  1700. br.IsSendEmail = false
  1701. return
  1702. }
  1703. if num <= 0 {
  1704. br.Msg = "期数必须大于0"
  1705. br.IsSendEmail = false
  1706. return
  1707. }
  1708. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  1709. if err != nil {
  1710. br.Msg = "获取失败"
  1711. br.ErrMsg = fmt.Sprint("获取指标信息失败,Err:", err.Error())
  1712. return
  1713. }
  1714. dataList, err := excel2.GetFirstEdbDataList(edbInfo, num, []string{}, -1)
  1715. if err != nil {
  1716. br.Msg = "获取失败"
  1717. br.ErrMsg = fmt.Sprint("获取失败,Err:", err.Error())
  1718. return
  1719. }
  1720. // 默认是倒序返回的数据,如果要正序的话,那么翻转下就好了
  1721. if sortType == "asc" {
  1722. for i, j := 0, len(dataList)-1; i < j; i, j = i+1, j-1 {
  1723. dataList[i], dataList[j] = dataList[j], dataList[i]
  1724. }
  1725. }
  1726. sourceNameList, sourceNameEnList, err := excel2.GetEdbSourceByEdbInfoIdList([]int{edbInfoId})
  1727. if err != nil {
  1728. br.Msg = "自定义表格数据获取失败"
  1729. br.ErrMsg = "自定义表格数据获取失败,Err:" + err.Error()
  1730. return
  1731. }
  1732. excelSource := strings.Join(sourceNameList, ",")
  1733. excelSourceEn := strings.Join(sourceNameEnList, ",")
  1734. if len(dataList) == 0 {
  1735. br.Msg = "指标数据异常,请检查"
  1736. return
  1737. }
  1738. br.Ret = 200
  1739. br.Success = true
  1740. br.Msg = "获取成功"
  1741. br.Data = response.TableDataItem{
  1742. EdbInfoId: edbInfoId,
  1743. Data: dataList,
  1744. Decimal: -1,
  1745. ExcelSource: excelSource,
  1746. ExcelSourceEn: excelSourceEn,
  1747. }
  1748. }
  1749. // GetOtherEdbData
  1750. // @Title 获取指定日期数据接口
  1751. // @Description 获取未来日期数据接口
  1752. // @Param request body request.GetOtherEdbDateDataReq true "type json string"
  1753. // @router /excel_info/table/other_edb_data_list [post]
  1754. func (c *ExcelInfoController) GetOtherEdbData() {
  1755. br := new(models.BaseResponse).Init()
  1756. defer func() {
  1757. c.Data["json"] = br
  1758. c.ServeJSON()
  1759. }()
  1760. sysUser := c.SysUser
  1761. if sysUser == nil {
  1762. br.Msg = "请登录"
  1763. br.ErrMsg = "请登录,SysUser Is Empty"
  1764. br.Ret = 408
  1765. return
  1766. }
  1767. var req request.GetOtherEdbDateDataReq
  1768. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  1769. if err != nil {
  1770. br.Msg = "参数解析异常!"
  1771. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1772. return
  1773. }
  1774. if req.EdbInfoId <= 0 {
  1775. br.Msg = "请选择指标"
  1776. br.IsSendEmail = false
  1777. return
  1778. }
  1779. if len(req.DateList) <= 0 {
  1780. br.Msg = "请选择日期"
  1781. br.IsSendEmail = false
  1782. return
  1783. }
  1784. edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  1785. if err != nil {
  1786. br.Msg = "获取失败"
  1787. br.ErrMsg = fmt.Sprint("获取指标信息失败,Err:", err.Error())
  1788. return
  1789. }
  1790. dataList, err := excel2.GetOtherEdbDataListFollowDate(edbInfo, req.DateList, req.DateDecimal)
  1791. if err != nil {
  1792. br.Msg = "获取失败"
  1793. br.ErrMsg = fmt.Sprint("获取失败,Err:", err.Error())
  1794. return
  1795. }
  1796. // 默认是倒序返回的数据,如果要正序的话,那么翻转下就好了
  1797. if req.SortType == "asc" {
  1798. for i, j := 0, len(dataList)-1; i < j; i, j = i+1, j-1 {
  1799. dataList[i], dataList[j] = dataList[j], dataList[i]
  1800. }
  1801. }
  1802. sourceNameList, sourceNameEnList, err := excel2.GetEdbSourceByEdbInfoIdList([]int{req.EdbInfoId})
  1803. if err != nil {
  1804. br.Msg = "自定义表格数据获取失败"
  1805. br.ErrMsg = "自定义表格数据获取失败,Err:" + err.Error()
  1806. return
  1807. }
  1808. excelSource := strings.Join(sourceNameList, ",")
  1809. excelSourceEn := strings.Join(sourceNameEnList, ",")
  1810. br.Ret = 200
  1811. br.Success = true
  1812. br.Msg = "获取成功"
  1813. br.Data = response.TableDataItem{
  1814. EdbInfoId: req.EdbInfoId,
  1815. Data: dataList,
  1816. Decimal: -1,
  1817. ExcelSource: excelSource,
  1818. ExcelSourceEn: excelSourceEn,
  1819. }
  1820. }
  1821. // GetBatchEdbData
  1822. // @Title 批量获取指定日期数据接口
  1823. // @Description 批量获取未来日期数据接口
  1824. // @Param request body request.GetBatchEdbDateDataReq true "type json string"
  1825. // @router /excel_info/table/batch_edb_data_list [post]
  1826. func (c *ExcelInfoController) GetBatchEdbData() {
  1827. br := new(models.BaseResponse).Init()
  1828. defer func() {
  1829. c.Data["json"] = br
  1830. c.ServeJSON()
  1831. }()
  1832. sysUser := c.SysUser
  1833. if sysUser == nil {
  1834. br.Msg = "请登录"
  1835. br.ErrMsg = "请登录,SysUser Is Empty"
  1836. br.Ret = 408
  1837. return
  1838. }
  1839. var req request.GetBatchEdbDateDataReq
  1840. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  1841. if err != nil {
  1842. br.Msg = "参数解析异常!"
  1843. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1844. return
  1845. }
  1846. if len(req.EdbInfoId) == 0 {
  1847. br.Msg = "请选择指标"
  1848. br.IsSendEmail = false
  1849. return
  1850. }
  1851. tableList := make([]*response.TableDataItem, 0)
  1852. var firstDataList []string
  1853. for i, v := range req.EdbInfoId {
  1854. if req.Num <= 0 {
  1855. br.Msg = "期数必须大于0"
  1856. br.IsSendEmail = false
  1857. return
  1858. }
  1859. edbInfo, err := data_manage.GetEdbInfoById(v)
  1860. if err != nil {
  1861. br.Msg = "获取失败"
  1862. br.ErrMsg = fmt.Sprint("获取指标信息失败,Err:", err.Error())
  1863. return
  1864. }
  1865. if len(req.DateList) == 0 {
  1866. req.Num = 12
  1867. }
  1868. if len(firstDataList) == 0 {
  1869. firstDataList = req.DateList
  1870. }
  1871. if len(firstDataList) != 0 && len(firstDataList) != req.Num {
  1872. req.Num = len(firstDataList)
  1873. }
  1874. if i == 0 {
  1875. dataList, err := excel2.GetFirstEdbDataList(edbInfo, req.Num, firstDataList, req.Decimal[i])
  1876. if err != nil {
  1877. br.Msg = "获取失败"
  1878. br.ErrMsg = fmt.Sprint("获取失败,Err:", err.Error())
  1879. return
  1880. }
  1881. // 默认是倒序返回的数据,如果要正序的话,那么翻转下就好了
  1882. if req.SortType == "asc" {
  1883. for i, j := 0, len(dataList)-1; i < j; i, j = i+1, j-1 {
  1884. dataList[i], dataList[j] = dataList[j], dataList[i]
  1885. }
  1886. }
  1887. dataList = excel2.PadFirstEdbDataList(dataList, req.DateList, req.SortType, req.Decimal[i])
  1888. sourceNameList, sourceNameEnList, err := excel2.GetEdbSourceByEdbInfoIdList([]int{v})
  1889. if err != nil {
  1890. br.Msg = "时间序列表格数据获取失败"
  1891. br.ErrMsg = "时间序列表格数据获取失败,Err:" + err.Error()
  1892. return
  1893. }
  1894. excelSource := strings.Join(sourceNameList, ",")
  1895. excelSourceEn := strings.Join(sourceNameEnList, ",")
  1896. if len(dataList) == 0 {
  1897. br.Msg = "指标数据异常,请检查"
  1898. return
  1899. }
  1900. tableList = append(tableList, &response.TableDataItem{
  1901. EdbInfoId: v,
  1902. Data: dataList,
  1903. Decimal: req.Decimal[i],
  1904. ExcelSource: excelSource,
  1905. ExcelSourceEn: excelSourceEn,
  1906. })
  1907. if len(firstDataList) == 0 {
  1908. tmpDataList := make([]string, 0)
  1909. for _, v := range dataList {
  1910. tmpDataList = append(tmpDataList, v.DataTime)
  1911. }
  1912. firstDataList = tmpDataList
  1913. }
  1914. } else {
  1915. dataList, err := excel2.GetOtherEdbDataList(edbInfo, firstDataList, req.Decimal[i])
  1916. if err != nil {
  1917. br.Msg = "获取失败"
  1918. br.ErrMsg = fmt.Sprint("获取失败,Err:", err.Error())
  1919. return
  1920. }
  1921. // 默认是倒序返回的数据,如果要正序的话,那么翻转下就好了
  1922. if req.SortType == "asc" {
  1923. for i, j := 0, len(dataList)-1; i < j; i, j = i+1, j-1 {
  1924. dataList[i], dataList[j] = dataList[j], dataList[i]
  1925. }
  1926. }
  1927. sourceNameList, sourceNameEnList, err := excel2.GetEdbSourceByEdbInfoIdList([]int{v})
  1928. if err != nil {
  1929. br.Msg = "自定义表格数据获取失败"
  1930. br.ErrMsg = "自定义表格数据获取失败,Err:" + err.Error()
  1931. return
  1932. }
  1933. excelSource := strings.Join(sourceNameList, ",")
  1934. excelSourceEn := strings.Join(sourceNameEnList, ",")
  1935. tableList = append(tableList, &response.TableDataItem{
  1936. EdbInfoId: v,
  1937. Decimal: req.Decimal[i],
  1938. Data: dataList,
  1939. ExcelSource: excelSource,
  1940. ExcelSourceEn: excelSourceEn,
  1941. })
  1942. }
  1943. }
  1944. br.Ret = 200
  1945. br.Success = true
  1946. br.Msg = "获取成功"
  1947. br.Data = tableList
  1948. }
  1949. // GetFutureDateData
  1950. // @Title 获取未来日期数据接口
  1951. // @Description 获取未来日期数据接口
  1952. // @Param request body request.GetFutureDateDataReq true "type json string"
  1953. // @router /excel_info/table/future_date_list [post]
  1954. func (c *ExcelInfoController) GetFutureDateData() {
  1955. br := new(models.BaseResponse).Init()
  1956. defer func() {
  1957. c.Data["json"] = br
  1958. c.ServeJSON()
  1959. }()
  1960. sysUser := c.SysUser
  1961. if sysUser == nil {
  1962. br.Msg = "请登录"
  1963. br.ErrMsg = "请登录,SysUser Is Empty"
  1964. br.Ret = 408
  1965. return
  1966. }
  1967. var req request.GetFutureDateDataReq
  1968. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  1969. if err != nil {
  1970. br.Msg = "参数解析异常!"
  1971. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1972. return
  1973. }
  1974. if req.StartDate == `` {
  1975. br.Msg = "开始日期未传"
  1976. br.IsSendEmail = false
  1977. return
  1978. }
  1979. startDate, err := time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local)
  1980. if err != nil {
  1981. br.Msg = "开始日期异常"
  1982. br.ErrMsg = fmt.Sprint("开始日期异常,startDate:", req.StartDate, ";err:", err.Error())
  1983. return
  1984. }
  1985. if len(req.EdbInfoIdList) <= 0 {
  1986. br.Msg = "请选择指标"
  1987. br.IsSendEmail = false
  1988. return
  1989. }
  1990. baseEdbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoIdList[0])
  1991. if err != nil {
  1992. br.Msg = "查找指标失败"
  1993. br.Msg = "查找指标失败,err:" + err.Error()
  1994. return
  1995. }
  1996. // 第一个指标的数据列表
  1997. var firstEdbDataList []*data_manage.EdbDataList
  1998. {
  1999. switch baseEdbInfo.EdbInfoType {
  2000. case 0:
  2001. firstEdbDataList, err = data_manage.GetEdbDataList(baseEdbInfo.Source, baseEdbInfo.SubSource, baseEdbInfo.EdbInfoId, req.StartDate, ``)
  2002. case 1:
  2003. _, firstEdbDataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(baseEdbInfo.EdbInfoId, req.StartDate, ``, false)
  2004. default:
  2005. br.Msg = "指标类型异常"
  2006. br.ErrMsg = fmt.Sprint("获取失败,指标类型异常", baseEdbInfo.EdbInfoType)
  2007. br.IsSendEmail = false
  2008. return
  2009. }
  2010. }
  2011. dateList := make([]time.Time, 0)
  2012. if req.DateType == 1 {
  2013. num := req.Num
  2014. i := 0 // 数据中有的日期数
  2015. for _, v := range firstEdbDataList {
  2016. tmpDate, err := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  2017. if err != nil {
  2018. br.Msg = "结束日期异常"
  2019. br.ErrMsg = fmt.Sprint("结束日期异常,startDate:", req.EndDate, ";err:", err.Error())
  2020. return
  2021. }
  2022. if tmpDate.After(startDate) {
  2023. dateList = append(dateList, tmpDate)
  2024. startDate = tmpDate
  2025. i++
  2026. }
  2027. if i >= num {
  2028. break
  2029. }
  2030. }
  2031. num = num - i
  2032. if num > 0 {
  2033. tmpDateList := utils.GetPredictEdbDayListByNum(startDate, num, baseEdbInfo.Frequency)
  2034. dateList = append(dateList, tmpDateList...)
  2035. }
  2036. } else {
  2037. if req.EndDate == `` {
  2038. br.Msg = "结束日期未传"
  2039. br.IsSendEmail = false
  2040. return
  2041. }
  2042. endDate, err := time.ParseInLocation(utils.FormatDate, req.EndDate, time.Local)
  2043. if err != nil {
  2044. br.Msg = "结束日期异常"
  2045. br.ErrMsg = fmt.Sprint("结束日期异常,startDate:", req.EndDate, ";err:", err.Error())
  2046. return
  2047. }
  2048. for _, v := range firstEdbDataList {
  2049. tmpDate, err := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  2050. if err != nil {
  2051. br.Msg = "结束日期异常"
  2052. br.ErrMsg = fmt.Sprint("结束日期异常,startDate:", req.EndDate, ";err:", err.Error())
  2053. return
  2054. }
  2055. if tmpDate.Equal(endDate) || (tmpDate.Before(endDate) && tmpDate.After(startDate)) {
  2056. dateList = append(dateList, tmpDate)
  2057. startDate = tmpDate
  2058. }
  2059. }
  2060. if startDate.Before(endDate) {
  2061. tmpDateList := utils.GetPredictEdbDayListByEndDate(startDate, endDate, baseEdbInfo.Frequency)
  2062. dateList = append(dateList, tmpDateList...)
  2063. }
  2064. }
  2065. // 获取所有的指标信息
  2066. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  2067. {
  2068. edbInfoList, err := data_manage.GetEdbInfoByIdList(req.EdbInfoIdList)
  2069. if err != nil {
  2070. br.Msg = "获取指标信息失败"
  2071. br.ErrMsg = "获取指标信息失败,err:" + err.Error()
  2072. return
  2073. }
  2074. for _, v := range edbInfoList {
  2075. edbInfoMap[v.EdbInfoId] = v
  2076. }
  2077. }
  2078. result := make([]response.TableDataItem, 0)
  2079. dateStrList := make([]string, 0)
  2080. lenDate := len(dateList)
  2081. if lenDate > 0 {
  2082. for i := lenDate - 1; i >= 0; i-- {
  2083. dateStrList = append(dateStrList, dateList[i].Format(utils.FormatDate))
  2084. }
  2085. for j, v := range req.EdbInfoIdList {
  2086. tmpEdbInfo, ok := edbInfoMap[v]
  2087. if !ok {
  2088. br.Msg = "获取指标信息失败"
  2089. br.ErrMsg = "获取指标信息失败,err:" + err.Error()
  2090. return
  2091. }
  2092. dataList, err := excel2.GetOtherEdbDataList(tmpEdbInfo, dateStrList, req.Decimal[j])
  2093. if err != nil {
  2094. br.Msg = "获取失败"
  2095. br.ErrMsg = fmt.Sprint("获取失败,Err:", err.Error())
  2096. return
  2097. }
  2098. result = append(result, response.TableDataItem{
  2099. EdbInfoId: v,
  2100. Decimal: req.Decimal[j],
  2101. Data: dataList,
  2102. })
  2103. }
  2104. }
  2105. br.Ret = 200
  2106. br.Success = true
  2107. br.Msg = "获取成功"
  2108. br.Data = result
  2109. }
  2110. // GetHistoryDateData
  2111. // @Title 获取历史日期数据接口
  2112. // @Description 获取历史日期数据接口
  2113. // @Param request body request.GetFutureDateDataReq true "type json string"
  2114. // @router /excel_info/table/history_date_list [post]
  2115. func (c *ExcelInfoController) GetHistoryDateData() {
  2116. br := new(models.BaseResponse).Init()
  2117. defer func() {
  2118. c.Data["json"] = br
  2119. c.ServeJSON()
  2120. }()
  2121. sysUser := c.SysUser
  2122. if sysUser == nil {
  2123. br.Msg = "请登录"
  2124. br.ErrMsg = "请登录,SysUser Is Empty"
  2125. br.Ret = 408
  2126. return
  2127. }
  2128. var req request.GetHistoryDateDataReq
  2129. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  2130. if err != nil {
  2131. br.Msg = "参数解析异常!"
  2132. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2133. return
  2134. }
  2135. if req.EndDate == `` {
  2136. br.Msg = "结束日期未传"
  2137. br.IsSendEmail = false
  2138. return
  2139. }
  2140. if len(req.EdbInfoIdList) <= 0 {
  2141. br.Msg = "请选择指标"
  2142. br.IsSendEmail = false
  2143. return
  2144. }
  2145. // 获取所有的指标信息
  2146. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  2147. {
  2148. edbInfoList, err := data_manage.GetEdbInfoByIdList(req.EdbInfoIdList)
  2149. if err != nil {
  2150. br.Msg = "获取指标信息失败"
  2151. br.ErrMsg = "获取指标信息失败,err:" + err.Error()
  2152. return
  2153. }
  2154. for _, v := range edbInfoList {
  2155. edbInfoMap[v.EdbInfoId] = v
  2156. }
  2157. }
  2158. result := make([]response.TableDataItem, 0)
  2159. dateStrList := make([]string, 0)
  2160. // 获取A列指标的历史数据
  2161. {
  2162. tmpEdbInfo, ok := edbInfoMap[req.EdbInfoIdList[0]]
  2163. if !ok {
  2164. br.Msg = "获取指标信息失败"
  2165. br.ErrMsg = "获取指标信息失败,err:" + err.Error()
  2166. return
  2167. }
  2168. firstDataList, err := excel2.GetFirstHistoryEdbDataList(tmpEdbInfo, req.Num, req.EndDate, req.Decimal[0])
  2169. if err != nil {
  2170. br.Msg = "获取失败"
  2171. br.ErrMsg = fmt.Sprint("获取失败,Err:", err.Error())
  2172. return
  2173. }
  2174. result = append(result, response.TableDataItem{
  2175. EdbInfoId: req.EdbInfoIdList[0],
  2176. Decimal: req.Decimal[0],
  2177. Data: firstDataList,
  2178. })
  2179. for _, v := range firstDataList {
  2180. dateStrList = append(dateStrList, v.DataTime)
  2181. }
  2182. }
  2183. if len(dateStrList) > 0 {
  2184. for k, v := range req.EdbInfoIdList {
  2185. if k == 0 {
  2186. continue
  2187. }
  2188. tmpEdbInfo, ok := edbInfoMap[v]
  2189. if !ok {
  2190. br.Msg = "获取指标信息失败"
  2191. br.ErrMsg = "获取指标信息失败,err:" + err.Error()
  2192. return
  2193. }
  2194. dataList, err := excel2.GetOtherEdbDataList(tmpEdbInfo, dateStrList, req.Decimal[k])
  2195. if err != nil {
  2196. br.Msg = "获取失败"
  2197. br.ErrMsg = fmt.Sprint("获取失败,Err:", err.Error())
  2198. return
  2199. }
  2200. result = append(result, response.TableDataItem{
  2201. EdbInfoId: v,
  2202. Decimal: req.Decimal[k],
  2203. Data: dataList,
  2204. })
  2205. }
  2206. }
  2207. br.Ret = 200
  2208. br.Success = true
  2209. br.Msg = "获取成功"
  2210. br.Data = result
  2211. }
  2212. // Refresh
  2213. // @Title 刷新
  2214. // @Description 刷新接口
  2215. // @Param ExcelInfoId query int true "表格id"
  2216. // @router /excel_info/table/refresh [get]
  2217. func (c *ExcelInfoController) Refresh() {
  2218. br := new(models.BaseResponse).Init()
  2219. defer func() {
  2220. if br.ErrMsg == "" {
  2221. br.IsSendEmail = false
  2222. }
  2223. c.Data["json"] = br
  2224. c.ServeJSON()
  2225. }()
  2226. sysUser := c.SysUser
  2227. if sysUser == nil {
  2228. br.Msg = "请登录"
  2229. br.ErrMsg = "请登录,SysUser Is Empty"
  2230. br.Ret = 408
  2231. return
  2232. }
  2233. excelInfoId, _ := c.GetInt("ExcelInfoId")
  2234. chartInfoId, _ := c.GetInt("ChartInfoId")
  2235. if excelInfoId <= 0 && chartInfoId <= 0 {
  2236. br.Msg = "请选择表格"
  2237. br.ErrMsg = "ExcelInfoId未传"
  2238. br.IsSendEmail = false
  2239. return
  2240. }
  2241. // todo 如果请求入参是chart_info_id,则需要获取excel_info_id
  2242. if chartInfoId > 0 && excelInfoId == 0 {
  2243. excelInfo, err := excel3.GetExcelInfoByChartInfoId(chartInfoId)
  2244. if err != nil {
  2245. br.Msg = "请选择表格"
  2246. br.ErrMsg = "未找到对应的表格"
  2247. br.IsSendEmail = false
  2248. return
  2249. }
  2250. excelInfoId = excelInfo.ExcelInfoId
  2251. }
  2252. // 获取数据详情
  2253. excelDetail, errMsg, err := excel2.GetExcelDetailInfoByExcelInfoId(excelInfoId, c.SysUser.AdminId, c.Lang)
  2254. if err != nil {
  2255. br.Msg = errMsg
  2256. br.ErrMsg = err.Error()
  2257. return
  2258. }
  2259. // 操作权限校验
  2260. {
  2261. button := excelDetail.Button
  2262. if excelDetail.Source != utils.BALANCE_TABLE {
  2263. button = excel2.GetExcelInfoOpButton(sysUser, excelDetail.SysUserId, excelDetail.Source, excelDetail.HaveOperaAuth)
  2264. }
  2265. if !button.RefreshButton {
  2266. br.Msg = "无操作权限"
  2267. br.IsSendEmail = false
  2268. return
  2269. }
  2270. }
  2271. // 数据刷新(只有自定义表格有刷新)
  2272. if excelDetail.Source == utils.TIME_TABLE {
  2273. jsonStrByte, err := json.Marshal(excelDetail.TableData)
  2274. if err != nil {
  2275. br.Msg = "自定义表格数据获取失败"
  2276. br.ErrMsg = "自定义表格数据获取失败,转json失败,Err:" + err.Error()
  2277. return
  2278. }
  2279. var tableData request.TableDataReq
  2280. err = json.Unmarshal(jsonStrByte, &tableData)
  2281. if err != nil {
  2282. br.Msg = "自定义表格数据获取失败"
  2283. br.ErrMsg = "自定义表格数据获取失败,json转结构体失败,Err:" + err.Error()
  2284. return
  2285. }
  2286. if len(tableData.EdbInfoIdList) > 0 {
  2287. err, _ = data.EdbInfoRefreshAllFromBaseV3(tableData.EdbInfoIdList, false, true, false)
  2288. if err != nil {
  2289. br.Msg = "刷新失败"
  2290. br.ErrMsg = "刷新失败,Err:" + err.Error()
  2291. return
  2292. }
  2293. }
  2294. }
  2295. // 数据刷新-混合表格
  2296. if excelDetail.Source == utils.MIXED_TABLE {
  2297. // todo 刷新动态表的所有子表中关联的指标数据
  2298. jsonByte, e := json.Marshal(excelDetail.TableData)
  2299. if e != nil {
  2300. br.Msg = "刷新失败"
  2301. br.ErrMsg = "JSON格式化混合表格数据失败, Err: " + e.Error()
  2302. return
  2303. }
  2304. var tableData request.MixedTableReq
  2305. if e = json.Unmarshal(jsonByte, &tableData); e != nil {
  2306. br.Msg = "刷新失败"
  2307. br.ErrMsg = "解析混合表格数据失败, Err: " + e.Error()
  2308. return
  2309. }
  2310. edbInfoIds := make([]int, 0)
  2311. edbInfoIdExist := make(map[int]bool)
  2312. if len(tableData.Data) > 0 {
  2313. for _, t := range tableData.Data {
  2314. for _, v := range t {
  2315. if v.EdbInfoId > 0 && !edbInfoIdExist[v.EdbInfoId] {
  2316. edbInfoIdExist[v.EdbInfoId] = true
  2317. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  2318. }
  2319. }
  2320. }
  2321. }
  2322. if len(edbInfoIds) > 0 {
  2323. e, _ = data.EdbInfoRefreshAllFromBaseV3(edbInfoIds, false, true, false)
  2324. if e != nil {
  2325. br.Msg = "刷新失败"
  2326. br.ErrMsg = "刷新混合表格数据失败, Err: " + err.Error()
  2327. return
  2328. }
  2329. }
  2330. }
  2331. if excelDetail.Source == utils.BALANCE_TABLE {
  2332. err = refreshBalanceTable(excelDetail, c.Lang)
  2333. if err != nil {
  2334. br.Msg = "刷新失败"
  2335. br.ErrMsg = "刷新失败,Err:" + err.Error()
  2336. return
  2337. }
  2338. }
  2339. // 清除缓存
  2340. key := utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL + ":" + excelDetail.UniqueCode
  2341. if utils.Re == nil {
  2342. _ = utils.Rc.Delete(key)
  2343. }
  2344. br.Ret = 200
  2345. br.Success = true
  2346. br.Msg = "刷新成功"
  2347. }
  2348. // Download
  2349. // @Title 获取表格详情
  2350. // @Description 获取表格详情接口
  2351. // @Param UniqueCode query string true "表格code"
  2352. // @Param ExcelInfoId query int true "表格id"
  2353. // @Success 200 {object} data_manage.ExcelInfo
  2354. // @router /excel_info/table/download [get]
  2355. func (c *ExcelInfoController) Download() {
  2356. br := new(models.BaseResponse).Init()
  2357. defer func() {
  2358. c.Data["json"] = br
  2359. c.ServeJSON()
  2360. }()
  2361. sysUser := c.SysUser
  2362. if sysUser == nil {
  2363. br.Msg = "请登录"
  2364. br.ErrMsg = "请登录,SysUser Is Empty"
  2365. br.Ret = 408
  2366. return
  2367. }
  2368. excelInfoId, _ := c.GetInt("ExcelInfoId")
  2369. uniqueCode := c.GetString("UniqueCode")
  2370. if excelInfoId <= 0 && uniqueCode == `` {
  2371. br.Msg = "请选择表格"
  2372. br.ErrMsg = "ExcelInfoId和UniqueCode都未传"
  2373. br.IsSendEmail = false
  2374. return
  2375. }
  2376. var excelInfo *excel3.ExcelInfo
  2377. var err error
  2378. //获取eta表格信息
  2379. if excelInfoId > 0 {
  2380. excelInfo, err = excel3.GetExcelInfoById(excelInfoId)
  2381. } else {
  2382. excelInfo, err = excel3.GetExcelInfoByUniqueCode(uniqueCode)
  2383. }
  2384. if err != nil {
  2385. br.Msg = "获取失败"
  2386. br.ErrMsg = "获取ETA表格信息失败,Err:" + err.Error()
  2387. if utils.IsErrNoRow(err) {
  2388. br.Msg = "ETA表格被删除,请刷新页面"
  2389. br.ErrMsg = "ETA表格被删除,请刷新页面,Err:" + err.Error()
  2390. br.IsSendEmail = false
  2391. }
  2392. return
  2393. }
  2394. if excelInfo.Source == 1 {
  2395. br.Msg = "不是自定义表格"
  2396. br.IsSendEmail = false
  2397. return
  2398. }
  2399. var tableData excel.TableData
  2400. switch excelInfo.Source {
  2401. case utils.EXCEL_DEFAULT:
  2402. br.Msg = "表格类型异常"
  2403. case utils.TIME_TABLE: // 自定义表格
  2404. var tableDataConfig excel2.TableDataConfig
  2405. err = json.Unmarshal([]byte(excelInfo.Content), &tableDataConfig)
  2406. if err != nil {
  2407. br.Msg = "获取失败"
  2408. br.ErrMsg = "表格json转结构体失败,Err:" + err.Error()
  2409. return
  2410. }
  2411. result, err := excel2.GetDataByTableDataConfig(tableDataConfig)
  2412. if err != nil {
  2413. br.Msg = "获取失败"
  2414. br.ErrMsg = "获取最新的表格数据失败,Err:" + err.Error()
  2415. return
  2416. }
  2417. tableData, err = excel.GetTableDataByCustomData(excelInfo.ExcelType, result, c.Lang)
  2418. if err != nil {
  2419. br.Msg = "获取失败"
  2420. br.ErrMsg = "转换成table失败,Err:" + err.Error()
  2421. return
  2422. }
  2423. case utils.MIXED_TABLE: // 混合表格
  2424. var result request.MixedTableReq
  2425. err = json.Unmarshal([]byte(excelInfo.Content), &result)
  2426. if err != nil {
  2427. br.Msg = "获取失败"
  2428. br.ErrMsg = "表格json转结构体失败,Err:" + err.Error()
  2429. return
  2430. }
  2431. newResult, err, errMsg := excel2.GetMixedTableCellData(result, c.Lang)
  2432. if err != nil {
  2433. br.Msg = "获取失败"
  2434. if errMsg != `` {
  2435. br.Msg = errMsg
  2436. }
  2437. br.ErrMsg = "获取最新的数据失败,Err:" + err.Error()
  2438. return
  2439. }
  2440. tableData, err = excel.GetTableDataByMixedTableData(newResult, false, excelInfo.ExcelInfoId)
  2441. if err != nil {
  2442. br.Msg = "获取失败"
  2443. br.ErrMsg = "转换成table失败,Err:" + err.Error()
  2444. return
  2445. }
  2446. case utils.BALANCE_TABLE: // 混合表格
  2447. savePath, fileName, uploadDir, err, errMsg := downloadBalanceTable(excelInfo, c.Lang)
  2448. if err != nil {
  2449. br.Msg = "下载失败"
  2450. if errMsg != `` {
  2451. br.Msg = errMsg
  2452. }
  2453. br.ErrMsg = "获取最新的数据失败,Err:" + err.Error()
  2454. return
  2455. }
  2456. defer func() {
  2457. _ = os.RemoveAll(uploadDir)
  2458. }()
  2459. // todo 删除文件
  2460. c.Ctx.Output.Download(savePath, fileName)
  2461. br.Ret = 200
  2462. br.Success = true
  2463. br.Msg = "获取成功"
  2464. br.Data = excelInfo
  2465. return
  2466. }
  2467. downloadFilePath, err := tableData.ToExcel()
  2468. if err != nil {
  2469. fmt.Println("err:", err)
  2470. return
  2471. }
  2472. defer func() {
  2473. _ = os.Remove(downloadFilePath)
  2474. }()
  2475. downloadFileName := excelInfo.ExcelName + ".xlsx"
  2476. c.Ctx.Output.Download(downloadFilePath, downloadFileName)
  2477. br.Ret = 200
  2478. br.Success = true
  2479. br.Msg = "获取成功"
  2480. br.Data = excelInfo
  2481. }
  2482. // Copy
  2483. // @Title 复制表格
  2484. // @Description 复制表格接口
  2485. // @Param request body request.EditExcelInfoReq true "type json string"
  2486. // @Success 200 {object} response.AddExcelInfoResp
  2487. // @router /excel_info/copy [post]
  2488. func (c *ExcelInfoController) Copy() {
  2489. br := new(models.BaseResponse).Init()
  2490. defer func() {
  2491. c.Data["json"] = br
  2492. c.ServeJSON()
  2493. }()
  2494. sysUser := c.SysUser
  2495. if sysUser == nil {
  2496. br.Msg = "请登录"
  2497. br.ErrMsg = "请登录,SysUser Is Empty"
  2498. br.Ret = 408
  2499. return
  2500. }
  2501. var req request.CopyExcelInfoReq
  2502. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  2503. if err != nil {
  2504. br.Msg = "参数解析异常!"
  2505. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2506. return
  2507. }
  2508. if req.ExcelInfoId <= 0 {
  2509. br.Msg = "请传入ETA表格标识!"
  2510. return
  2511. }
  2512. cacheKey := "CACHE_TABLE_INFO_EDIT_" + strconv.Itoa(req.ExcelInfoId)
  2513. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  2514. br.Msg = "系统处理中,请稍后重试!"
  2515. br.ErrMsg = "系统处理中,请稍后重试!"
  2516. return
  2517. }
  2518. defer func() {
  2519. _ = utils.Rc.Delete(cacheKey)
  2520. }()
  2521. req.ExcelName = strings.Trim(req.ExcelName, " ")
  2522. if req.ExcelName == "" {
  2523. br.Msg = "请填写表格名称!"
  2524. br.IsSendEmail = false
  2525. return
  2526. }
  2527. if req.ExcelClassifyId <= 0 {
  2528. br.Msg = "分类参数错误!"
  2529. br.IsSendEmail = false
  2530. return
  2531. }
  2532. // 获取原ETA表格信息
  2533. oldExcelInfo, err := excel3.GetExcelInfoById(req.ExcelInfoId)
  2534. if err != nil {
  2535. br.Msg = "获取ETA表格失败"
  2536. return
  2537. }
  2538. var (
  2539. excelInfo *excel3.ExcelInfo
  2540. errMsg string
  2541. isSendEmail bool
  2542. )
  2543. if oldExcelInfo.Source == utils.BALANCE_TABLE {
  2544. excelInfo, err, errMsg, isSendEmail = data.CopyBalanceExcel(oldExcelInfo, req.ExcelClassifyId, req.ExcelName, sysUser)
  2545. } else {
  2546. excelInfo, err, errMsg, isSendEmail = excel2.Copy(oldExcelInfo, req.ExcelClassifyId, req.ExcelName, sysUser)
  2547. }
  2548. if err != nil {
  2549. br.Msg = "复制失败"
  2550. if errMsg != `` {
  2551. br.Msg = errMsg
  2552. }
  2553. br.ErrMsg = "复制失败,Err:" + err.Error()
  2554. br.IsSendEmail = isSendEmail
  2555. return
  2556. }
  2557. //新增指标引用记录
  2558. _ = data.SaveExcelEdbInfoRelation(excelInfo.ExcelInfoId, excelInfo.Source, true)
  2559. resp := new(response.AddExcelInfoResp)
  2560. resp.ExcelInfoId = excelInfo.ExcelInfoId
  2561. resp.UniqueCode = excelInfo.UniqueCode
  2562. br.Ret = 200
  2563. br.Success = true
  2564. br.Msg = "保存成功"
  2565. br.Data = resp
  2566. br.IsAddLog = true
  2567. }
  2568. // MarkEditStatus
  2569. // @Title 标记沙盘编辑状态
  2570. // @Description 标记沙盘编辑状态接口
  2571. // @Param request body request.MarkEditSandbox true "type json string"
  2572. // @Success 200 标记成功 ;202 标记成功
  2573. // @router /excel_info/mark [post]
  2574. func (c *ExcelInfoController) MarkEditStatus() {
  2575. br := new(models.BaseResponse).Init()
  2576. defer func() {
  2577. c.Data["json"] = br
  2578. c.ServeJSON()
  2579. }()
  2580. sysUser := c.SysUser
  2581. if sysUser == nil {
  2582. br.Msg = "请登录"
  2583. br.ErrMsg = "请登录,SysUser Is Empty"
  2584. br.Ret = 408
  2585. return
  2586. }
  2587. var req request.MarkEditExcel
  2588. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  2589. if err != nil {
  2590. br.Msg = "参数解析异常!"
  2591. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2592. return
  2593. }
  2594. if req.ExcelInfoId <= 0 {
  2595. br.Msg = "缺少表格编号"
  2596. return
  2597. }
  2598. if req.Status <= 0 {
  2599. br.Msg = "标记状态异常"
  2600. return
  2601. }
  2602. status := 1
  2603. if req.Status == 2 {
  2604. status = 0
  2605. }
  2606. //更新标记key
  2607. data, err := services.UpdateExcelEditMark(req.ExcelInfoId, sysUser.AdminId, status, sysUser.RealName)
  2608. if err != nil {
  2609. br.Msg = err.Error()
  2610. return
  2611. }
  2612. msg := "标记成功"
  2613. br.Ret = 200
  2614. br.Success = true
  2615. br.Msg = msg
  2616. br.Data = data
  2617. }
  2618. // BatchRefresh
  2619. // @Title 批量刷新表格接口
  2620. // @Description 批量刷新图表接口
  2621. // @Param request body excel3.BatchRefreshExcelReq true "type json string"
  2622. // @Success Ret=200 刷新成功
  2623. // @router /excel_info/table/batch_refresh [post]
  2624. func (c *ExcelInfoController) BatchRefresh() {
  2625. br := new(models.BaseResponse).Init()
  2626. defer func() {
  2627. if br.ErrMsg == "" {
  2628. br.IsSendEmail = false
  2629. }
  2630. c.Data["json"] = br
  2631. c.ServeJSON()
  2632. }()
  2633. sysUser := c.SysUser
  2634. if sysUser == nil {
  2635. br.Msg = "请登录"
  2636. br.ErrMsg = "请登录,SysUser Is Empty"
  2637. br.Ret = 408
  2638. return
  2639. }
  2640. var req excel3.BatchRefreshExcelReq
  2641. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  2642. if err != nil {
  2643. br.Msg = "参数解析异常!"
  2644. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2645. return
  2646. }
  2647. if len(req.ExcelCodes) == 0 {
  2648. br.Ret = 200
  2649. br.Success = true
  2650. br.Msg = "刷新成功"
  2651. return
  2652. }
  2653. if req.Source == "" {
  2654. br.Msg = "刷新来源有误"
  2655. return
  2656. }
  2657. if req.PrimaryId <= 0 {
  2658. br.Msg = "刷新对象有误"
  2659. return
  2660. }
  2661. // 获取表格关联的指标IDs
  2662. edbIds, e := excel2.GetEdbIdsFromExcelCodes(req.ExcelCodes, sysUser.AdminId, c.Lang)
  2663. if e != nil {
  2664. br.Msg = "刷新失败"
  2665. br.ErrMsg = "获取表格关联的指标IDs失败, Err: " + e.Error()
  2666. return
  2667. }
  2668. redisKey := data.GetBatchChartRefreshKey(req.Source, req.PrimaryId, req.SubId)
  2669. refreshKeys := make([]string, 0)
  2670. for _, v := range req.ExcelCodes {
  2671. refreshKeys = append(refreshKeys, fmt.Sprint(utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL, v))
  2672. }
  2673. // 刷新相关指标
  2674. syncing, e := data.BatchRefreshEdbByEdbIds(edbIds, redisKey, refreshKeys)
  2675. if e != nil {
  2676. br.Msg = "刷新失败"
  2677. br.ErrMsg = "刷新表格关联指标信息失败,Err:" + err.Error()
  2678. return
  2679. }
  2680. br.Msg = "刷新成功"
  2681. if syncing {
  2682. br.Msg = "表格关联指标较多,请10分钟后刷新页面查看最新数据"
  2683. }
  2684. br.Ret = 200
  2685. br.Success = true
  2686. }
  2687. // GetBatchChartRefreshResult
  2688. // @Title 获取批量刷新表格结果
  2689. // @Description 获取批量刷新表格结果
  2690. // @Param request body excel3.BatchRefreshExcelReq true "type json string"
  2691. // @Success Ret=200 刷新成功
  2692. // @router /excel_info/table/batch_refresh/result [post]
  2693. func (c *ExcelInfoController) GetBatchChartRefreshResult() {
  2694. br := new(models.BaseResponse).Init()
  2695. defer func() {
  2696. c.Data["json"] = br
  2697. c.ServeJSON()
  2698. }()
  2699. sysUser := c.SysUser
  2700. if sysUser == nil {
  2701. br.Msg = "请登录"
  2702. br.ErrMsg = "请登录,SysUser Is Empty"
  2703. br.Ret = 408
  2704. return
  2705. }
  2706. var req excel3.BatchRefreshExcelReq
  2707. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  2708. if err != nil {
  2709. br.Msg = "参数解析异常!"
  2710. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2711. return
  2712. }
  2713. if req.Source == "" {
  2714. br.Msg = "刷新来源有误"
  2715. return
  2716. }
  2717. if req.PrimaryId <= 0 {
  2718. br.Msg = "刷新对象有误"
  2719. return
  2720. }
  2721. // 校验缓存是否存在, 存在说明还在刷新中
  2722. result := true
  2723. redisKey := excel2.GetExcelEdbBatchRefreshKey(req.Source, req.PrimaryId, req.SubId)
  2724. if redisKey != `` {
  2725. // 如果找到了key,那么就是还在更新中
  2726. ok := utils.Rc.IsExist(redisKey)
  2727. if ok {
  2728. result = false
  2729. }
  2730. }
  2731. resp := struct {
  2732. RefreshResult bool `description:"刷新结果"`
  2733. }{
  2734. RefreshResult: result,
  2735. }
  2736. br.Data = resp
  2737. br.Ret = 200
  2738. br.Success = true
  2739. }
  2740. // GetBatchChartRefreshResult
  2741. // @Title 获取批量刷新表格结果
  2742. // @Description 获取批量刷新表格结果
  2743. // @Param EdbInfoId query int true "edb id"
  2744. // @Success Ret=200 刷新成功
  2745. // @router /excel_info/get_edb_source [get]
  2746. func (c *ExcelInfoController) GetEdbSource() {
  2747. br := new(models.BaseResponse).Init()
  2748. defer func() {
  2749. c.Data["json"] = br
  2750. c.ServeJSON()
  2751. }()
  2752. sysUser := c.SysUser
  2753. if sysUser == nil {
  2754. br.Msg = "请登录"
  2755. br.ErrMsg = "请登录,SysUser Is Empty"
  2756. br.Ret = 408
  2757. return
  2758. }
  2759. edbInfoId, _ := c.GetInt("EdbInfoId")
  2760. if edbInfoId <= 0 {
  2761. br.Msg = "请选择指标"
  2762. br.ErrMsg = "请选择指标"
  2763. br.IsSendEmail = false
  2764. return
  2765. }
  2766. sourceNameList, sourceNameEnList, err := excel2.GetEdbSourceByEdbInfoIdList([]int{edbInfoId})
  2767. if err != nil {
  2768. br.Msg = "自定义表格数据获取失败"
  2769. br.ErrMsg = "自定义表格数据获取失败,Err:" + err.Error()
  2770. return
  2771. }
  2772. excelSource := strings.Join(sourceNameList, ",")
  2773. excelSourceEn := strings.Join(sourceNameEnList, ",")
  2774. var resp struct {
  2775. ExcelSource string `description:"表格来源"`
  2776. ExcelSourceEn string `description:"表格来源(英文)"`
  2777. }
  2778. resp.ExcelSource = excelSource
  2779. resp.ExcelSourceEn = excelSourceEn
  2780. br.Data = resp
  2781. br.Ret = 200
  2782. br.Success = true
  2783. }
  2784. // ExcelRule
  2785. // @Title 添加表格规则
  2786. // @Description 添加表格规则
  2787. // @Param request body excel3.BatchRefreshExcelReq true "type json string"
  2788. // @Success Ret=200 刷新成功
  2789. // @router /excel_info/rule/add [post]
  2790. func (c *ExcelInfoController) AddExcelRule() {
  2791. br := new(models.BaseResponse).Init()
  2792. defer func() {
  2793. c.Data["json"] = br
  2794. c.ServeJSON()
  2795. }()
  2796. sysUser := c.SysUser
  2797. if sysUser == nil {
  2798. br.Msg = "请登录"
  2799. br.ErrMsg = "请登录,SysUser Is Empty"
  2800. br.Ret = 408
  2801. return
  2802. }
  2803. var req *request.ExcelRuleMappingReq
  2804. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  2805. if err != nil {
  2806. br.Msg = "参数解析异常!"
  2807. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2808. return
  2809. }
  2810. if req.LeftValue == "" {
  2811. br.Msg = "条件值不能为空"
  2812. return
  2813. }
  2814. if req.Scope == "" {
  2815. br.Msg = "应用选区不能为空"
  2816. return
  2817. }
  2818. if req.FontColor == "" && req.BackgroundColor == "" {
  2819. br.Msg = "字体颜色和背景颜色不能同时为空"
  2820. return
  2821. }
  2822. if req.RuleType == 3 && req.RightValue == "" {
  2823. br.Msg = "条件值不能为空"
  2824. return
  2825. }
  2826. err = excel2.AddExcelRule(req, c.Lang)
  2827. if err != nil {
  2828. br.Msg = "规则添加失败"
  2829. br.ErrMsg = "规则添加失败,Err:" + err.Error()
  2830. return
  2831. }
  2832. br.Msg = "添加成功"
  2833. br.Ret = 200
  2834. br.Success = true
  2835. }
  2836. // EditExcelRule
  2837. // @Title 编辑表格规则
  2838. // @Description 编辑表格规则
  2839. // @Param request body request.ExcelRuleMappingReq true "type json string"
  2840. // @Success Ret=200 刷新成功
  2841. // @router /excel_info/rule/edit [post]
  2842. func (c *ExcelInfoController) EditExcelRule() {
  2843. br := new(models.BaseResponse).Init()
  2844. defer func() {
  2845. c.Data["json"] = br
  2846. c.ServeJSON()
  2847. }()
  2848. sysUser := c.SysUser
  2849. if sysUser == nil {
  2850. br.Msg = "请登录"
  2851. br.ErrMsg = "请登录,SysUser Is Empty"
  2852. br.Ret = 408
  2853. return
  2854. }
  2855. var req *request.ExcelRuleMappingReq
  2856. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  2857. if err != nil {
  2858. br.Msg = "参数解析异常!"
  2859. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2860. return
  2861. }
  2862. if req.LeftValue == "" {
  2863. br.Msg = "条件值不能为空"
  2864. return
  2865. }
  2866. if req.Scope == "" {
  2867. br.Msg = "应用选区不能为空"
  2868. return
  2869. }
  2870. req.FontColor = strings.TrimSpace(req.FontColor)
  2871. req.BackgroundColor = strings.TrimSpace(req.BackgroundColor)
  2872. if req.FontColor == "" && req.BackgroundColor == "" {
  2873. br.Msg = "字体颜色和背景颜色不能同时为空"
  2874. return
  2875. }
  2876. if req.RuleType == 3 && req.RightValue == "" {
  2877. br.Msg = "条件值不能为空"
  2878. return
  2879. }
  2880. if req.ExcelRuleMappingId <= 0 {
  2881. br.Msg = "非法的管理规则ID"
  2882. return
  2883. }
  2884. err = excel2.ModifyExcelRule(req, c.Lang)
  2885. if err != nil {
  2886. br.Msg = "规则编辑失败"
  2887. br.ErrMsg = "规则编辑失败,Err:" + err.Error()
  2888. return
  2889. }
  2890. br.Msg = "编辑成功"
  2891. br.Ret = 200
  2892. br.Success = true
  2893. }
  2894. // DeleteExcelRule
  2895. // @Title 删除表格规则
  2896. // @Description 删除表格规则
  2897. // @Param request body request.ExcelRuleMappingReq true "type json string"
  2898. // @Success Ret=200 刷新成功
  2899. // @router /excel_info/rule/delete [post]
  2900. func (c *ExcelInfoController) DeleteExcelRule() {
  2901. br := new(models.BaseResponse).Init()
  2902. defer func() {
  2903. c.Data["json"] = br
  2904. c.ServeJSON()
  2905. }()
  2906. sysUser := c.SysUser
  2907. if sysUser == nil {
  2908. br.Msg = "请登录"
  2909. br.ErrMsg = "请登录,SysUser Is Empty"
  2910. br.Ret = 408
  2911. return
  2912. }
  2913. var req request.DeleteExcelRuleMappingReq
  2914. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  2915. if err != nil {
  2916. br.Msg = "参数解析异常!"
  2917. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2918. return
  2919. }
  2920. if req.ExcelRuleMappingId <= 0 {
  2921. br.Msg = "非法的管理规则ID"
  2922. return
  2923. }
  2924. err = excel3.DeleteExcelRuleMappingById(req.ExcelRuleMappingId)
  2925. if err != nil {
  2926. br.Msg = "规则删除失败"
  2927. br.ErrMsg = "规则删除失败,Err:" + err.Error()
  2928. return
  2929. }
  2930. br.Msg = "删除成功"
  2931. br.Ret = 200
  2932. br.Success = true
  2933. }
  2934. // GetExcelRuleList
  2935. // @Title 表格规则列表
  2936. // @Description 表格规则列表
  2937. // @Param ExcelInfoId query int true "id"
  2938. // @Success Ret=200 获取成功
  2939. // @router /excel_info/rule/list [get]
  2940. func (c *ExcelInfoController) GetExcelRuleList() {
  2941. br := new(models.BaseResponse).Init()
  2942. defer func() {
  2943. c.Data["json"] = br
  2944. c.ServeJSON()
  2945. }()
  2946. sysUser := c.SysUser
  2947. if sysUser == nil {
  2948. br.Msg = "请登录"
  2949. br.ErrMsg = "请登录,SysUser Is Empty"
  2950. br.Ret = 408
  2951. return
  2952. }
  2953. ExcelInfoId, _ := c.GetInt("ExcelInfoId")
  2954. if ExcelInfoId <= 0 {
  2955. br.Msg = "请选择表格"
  2956. return
  2957. }
  2958. items, err := excel2.GetExcelRuleList(ExcelInfoId)
  2959. if err != nil {
  2960. br.Msg = "管理规则编辑失败"
  2961. br.ErrMsg = "管理规则添加失败,Err:" + err.Error()
  2962. return
  2963. }
  2964. if items.List == nil {
  2965. items.List = []*excel3.ExcelInfoRuleMappingView{}
  2966. }
  2967. br.Data = items
  2968. br.Msg = "获取成功"
  2969. br.Ret = 200
  2970. br.Success = true
  2971. }
  2972. // GetExcelRuleDetail
  2973. // @Title 表格规则详情
  2974. // @Description 表格规则详情
  2975. // @Param ExcelInfoRuleMappingId query int true "id"
  2976. // @Success Ret=200 获取成功
  2977. // @router /excel_info/rule/detail [get]
  2978. func (c *ExcelInfoController) GetExcelRuleDetail() {
  2979. br := new(models.BaseResponse).Init()
  2980. defer func() {
  2981. c.Data["json"] = br
  2982. c.ServeJSON()
  2983. }()
  2984. sysUser := c.SysUser
  2985. if sysUser == nil {
  2986. br.Msg = "请登录"
  2987. br.ErrMsg = "请登录,SysUser Is Empty"
  2988. br.Ret = 408
  2989. return
  2990. }
  2991. excelInfoRuleMappingId, _ := c.GetInt("ExcelInfoRuleMappingId")
  2992. items, err := excel2.GetExcelRuleDetail(excelInfoRuleMappingId)
  2993. if err != nil {
  2994. br.Msg = "管理规则编辑失败"
  2995. br.ErrMsg = "管理规则添加失败,Err:" + err.Error()
  2996. return
  2997. }
  2998. br.Data = items
  2999. br.Msg = "获取成功"
  3000. br.Ret = 200
  3001. br.Success = true
  3002. }
  3003. // GetExcelReferenceDetail
  3004. // @Title 表格引用配置保存
  3005. // @Description 表格引用配置保存
  3006. // @Param ExcelInfoRuleMappingId query int true "id"
  3007. // @Success Ret=200 获取成功
  3008. // @router /excel_info/reference/save [post]
  3009. func (c *ExcelInfoController) SaveExcelReference() {
  3010. br := new(models.BaseResponse).Init()
  3011. defer func() {
  3012. c.Data["json"] = br
  3013. c.ServeJSON()
  3014. }()
  3015. sysUser := c.SysUser
  3016. if sysUser == nil {
  3017. br.Msg = "请登录"
  3018. br.ErrMsg = "请登录,SysUser Is Empty"
  3019. br.Ret = 408
  3020. return
  3021. }
  3022. var req excel3.ExcelReferencesReq
  3023. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  3024. if err != nil {
  3025. br.Msg = "参数解析异常!"
  3026. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  3027. return
  3028. }
  3029. addList := make([]*excel3.ReferencedExcelConfig, 0)
  3030. exist, e := excel3.GetReferencedExcelConfig(req.ReferencedId, req.FromScene, req.UniqueCode, req.Uuid)
  3031. if e != nil && !utils.IsErrNoRow(e) {
  3032. br.Msg = "查找引用失败"
  3033. br.ErrMsg = "查找引用失败,Err:" + e.Error()
  3034. return
  3035. }
  3036. if exist.ReferencedExcelConfigId == 0 {
  3037. item := excel3.ReferencedExcelConfig{
  3038. UniqueCode: req.UniqueCode,
  3039. ReferencedId: req.ReferencedId,
  3040. FromScene: req.FromScene,
  3041. Uuid: req.Uuid,
  3042. WidthList: req.WidthList,
  3043. HeightList: req.HeightList,
  3044. OpUserId: sysUser.AdminId,
  3045. OpUserName: sysUser.RealName,
  3046. CreateTime: time.Now(),
  3047. ModifyTime: time.Now(),
  3048. }
  3049. addList = append(addList, &item)
  3050. } else {
  3051. exist.WidthList = req.WidthList
  3052. exist.HeightList = req.HeightList
  3053. exist.OpUserId = sysUser.AdminId
  3054. exist.OpUserName = sysUser.RealName
  3055. exist.ModifyTime = time.Now()
  3056. err = excel3.UpdateReferencedExcelConfig(&exist)
  3057. if err != nil {
  3058. br.Msg = "更新引用失败"
  3059. br.ErrMsg = "更新引用失败,Err:" + err.Error()
  3060. return
  3061. }
  3062. }
  3063. if len(addList) > 0 {
  3064. err = excel3.AddReferencedExcelConfig(addList)
  3065. if err != nil && !utils.IsErrNoRow(err) {
  3066. br.Msg = "添加引用失败"
  3067. br.ErrMsg = "添加引用失败,Err:" + err.Error()
  3068. return
  3069. }
  3070. }
  3071. excelInfo, err := excel3.GetExcelInfoByUnicode(req.UniqueCode)
  3072. if err != nil {
  3073. br.Msg = "获取表格信息失败"
  3074. br.ErrMsg = "获取表格信息失败,Err:" + err.Error()
  3075. return
  3076. }
  3077. if excelInfo.ExcelInfoId > 0 {
  3078. //删除公共图库那边的缓存
  3079. _ = utils.Rc.Delete(utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL + ":" + excelInfo.UniqueCode)
  3080. }
  3081. br.Msg = "添加成功"
  3082. br.Ret = 200
  3083. br.Success = true
  3084. }