excel_info.go 103 KB

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