excel_info.go 60 KB

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