excel_info.go 75 KB

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