excel_info.go 59 KB

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