excel_info.go 75 KB

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