excel_info.go 64 KB

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