excel_info.go 84 KB

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