excel_info.go 89 KB

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