excel_info.go 85 KB

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