excel_info.go 89 KB

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