excel_info.go 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "github.com/yidane/formula"
  7. "hongze/hz_eta_api/controllers"
  8. "hongze/hz_eta_api/models"
  9. "hongze/hz_eta_api/models/data_manage"
  10. "hongze/hz_eta_api/models/data_manage/request"
  11. "hongze/hz_eta_api/models/data_manage/response"
  12. "hongze/hz_eta_api/services"
  13. "hongze/hz_eta_api/services/alarm_msg"
  14. "hongze/hz_eta_api/services/data"
  15. "hongze/hz_eta_api/services/excel"
  16. "hongze/hz_eta_api/utils"
  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 (this *ExcelInfoController) Add() {
  33. br := new(models.BaseResponse).Init()
  34. defer func() {
  35. this.Data["json"] = br
  36. this.ServeJSON()
  37. }()
  38. sysUser := this.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(this.Ctx.Input.RequestBody)
  56. return
  57. }
  58. var req request.AddExcelInfoReq
  59. err := json.Unmarshal(this.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(this.Ctx.Input.RequestBody),
  198. // Status: "新增表格",
  199. // Method: this.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 (this *ExcelInfoController) List() {
  220. br := new(models.BaseResponse).Init()
  221. defer func() {
  222. this.Data["json"] = br
  223. this.ServeJSON()
  224. }()
  225. sysUser := this.SysUser
  226. if sysUser == nil {
  227. br.Msg = "请登录"
  228. br.ErrMsg = "请登录,SysUser Is Empty"
  229. br.Ret = 408
  230. return
  231. }
  232. excelClassifyId, _ := this.GetInt("ExcelClassifyId")
  233. pageSize, _ := this.GetInt("PageSize")
  234. currentIndex, _ := this.GetInt("CurrentIndex")
  235. keyword := this.GetString("Keyword")
  236. adminId, _ := this.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 (this *ExcelInfoController) Detail() {
  303. br := new(models.BaseResponse).Init()
  304. defer func() {
  305. this.Data["json"] = br
  306. this.ServeJSON()
  307. }()
  308. sysUser := this.SysUser
  309. if sysUser == nil {
  310. br.Msg = "请登录"
  311. br.ErrMsg = "请登录,SysUser Is Empty"
  312. br.Ret = 408
  313. return
  314. }
  315. excelInfoId, _ := this.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 (this *ExcelInfoController) Edit() {
  343. br := new(models.BaseResponse).Init()
  344. defer func() {
  345. this.Data["json"] = br
  346. this.ServeJSON()
  347. }()
  348. sysUser := this.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(this.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(this.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 (this *ExcelInfoController) Move() {
  532. br := new(models.BaseResponse).Init()
  533. defer func() {
  534. this.Data["json"] = br
  535. this.ServeJSON()
  536. }()
  537. sysUser := this.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(this.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(this.Ctx.Input.RequestBody)
  684. // ExcelLog.Status = "移动表格"
  685. // ExcelLog.Method = this.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 (this *ExcelInfoController) Delete() {
  700. br := new(models.BaseResponse).Init()
  701. defer func() {
  702. this.Data["json"] = br
  703. this.ServeJSON()
  704. }()
  705. resp := response.AddExcelInfoResp{}
  706. sysUser := this.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(this.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 (this *ExcelInfoController) AddDraft() {
  825. br := new(models.BaseResponse).Init()
  826. defer func() {
  827. this.Data["json"] = br
  828. this.ServeJSON()
  829. }()
  830. sysUser := this.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(this.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 (this *ExcelInfoController) GetExcelTableData() {
  912. br := new(models.BaseResponse).Init()
  913. defer func() {
  914. this.Data["json"] = br
  915. this.ServeJSON()
  916. }()
  917. sysUser := this.SysUser
  918. if sysUser == nil {
  919. br.Msg = "请登录"
  920. br.ErrMsg = "请登录,SysUser Is Empty"
  921. br.Ret = 408
  922. return
  923. }
  924. uniqueCode := this.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. //上传到阿里云
  1036. resourceUrl, err := services.UploadAliyunV2(fileName, downloadFilePath)
  1037. if err != nil {
  1038. return
  1039. }
  1040. excelInfo.FileUrl = resourceUrl
  1041. err = excelInfo.Update([]string{"FileUrl"})
  1042. }
  1043. //func init() {
  1044. //
  1045. // excelInfo, err := data_manage.GetExcelInfoById(52)
  1046. // if err != nil {
  1047. // fmt.Println("err:", err)
  1048. // return
  1049. // }
  1050. //
  1051. // luckySheetData, err := excel.GetLuckySheetData(excelInfo.Content)
  1052. // if err != nil {
  1053. // fmt.Println("err:", err)
  1054. // return
  1055. // }
  1056. // //_, err = luckySheetData.GetTableDataByLuckySheetDataStr()
  1057. // downloadFilePath, err := luckySheetData.ToExcel()
  1058. // if err != nil {
  1059. // fmt.Println("err:", err)
  1060. // return
  1061. // }
  1062. // fmt.Println(downloadFilePath)
  1063. //
  1064. // //fmt.Println(excel.GetColumnName(988, 283))
  1065. //}
  1066. // Calculate
  1067. // @Title 公式计算接口
  1068. // @Description 公式计算接口
  1069. // @Param request body request.CalculateReq true "type json string"
  1070. // @router /excel_info/table/calculate [post]
  1071. func (this *ExcelInfoController) Calculate() {
  1072. br := new(models.BaseResponse).Init()
  1073. defer func() {
  1074. this.Data["json"] = br
  1075. this.ServeJSON()
  1076. }()
  1077. sysUser := this.SysUser
  1078. if sysUser == nil {
  1079. br.Msg = "请登录"
  1080. br.ErrMsg = "请登录,SysUser Is Empty"
  1081. br.Ret = 408
  1082. return
  1083. }
  1084. var req request.CalculateReq
  1085. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1086. if err != nil {
  1087. br.Msg = "参数解析异常!"
  1088. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1089. return
  1090. }
  1091. if req.CalculateFormula == "" {
  1092. br.Msg = "请填写公式"
  1093. return
  1094. }
  1095. calculateFormula := req.CalculateFormula
  1096. calculateFormula = strings.TrimPrefix(calculateFormula, "=")
  1097. calculateFormula = strings.Replace(calculateFormula, "(", "(", -1)
  1098. calculateFormula = strings.Replace(calculateFormula, ")", ")", -1)
  1099. calculateFormula = strings.Replace(calculateFormula, ",", ",", -1)
  1100. calculateFormula = strings.Replace(calculateFormula, "。", ".", -1)
  1101. calculateFormula = strings.Replace(calculateFormula, "%", "*0.01", -1)
  1102. formulaFormStr := utils.ReplaceFormula(req.TagMap, calculateFormula)
  1103. //计算公式异常,那么就移除该指标
  1104. if formulaFormStr == `` {
  1105. //removeDateList = append(removeDateList, sk)
  1106. //fmt.Println("异常了")
  1107. br.Msg = "公式异常"
  1108. br.ErrMsg = "公式异常"
  1109. return
  1110. }
  1111. expression := formula.NewExpression(formulaFormStr)
  1112. calResult, err := expression.Evaluate()
  1113. if err != nil {
  1114. br.Msg = "计算失败"
  1115. br.ErrMsg = "计算失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr
  1116. // 分母为0的报错
  1117. if strings.Contains(err.Error(), "divide by zero") {
  1118. br.Msg = "分母不能为0"
  1119. br.ErrMsg = "分母不能为空,计算公式:" + formulaFormStr
  1120. br.IsSendEmail = false
  1121. }
  1122. return
  1123. }
  1124. // 如果计算结果是NAN,那么就提示报错
  1125. if calResult.IsNan() {
  1126. br.Msg = "计算失败"
  1127. br.ErrMsg = "计算失败:计算结果是:NAN;formulaStr:" + formulaFormStr
  1128. return
  1129. }
  1130. calVal := calResult.String()
  1131. if err != nil {
  1132. br.Msg = "计算失败"
  1133. br.ErrMsg = "计算失败,结果转 string 失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr
  1134. return
  1135. }
  1136. br.Ret = 200
  1137. br.Success = true
  1138. br.Msg = "计算成功"
  1139. br.Data = calVal
  1140. }
  1141. // GetFirstEdbData
  1142. // @Title 获取未来日期数据接口
  1143. // @Description 获取未来日期数据接口
  1144. // @Param EdbInfoId query int true "指标id"
  1145. // @Param Num query int true "默认期数"
  1146. // @router /excel_info/table/first_edb_data_list [get]
  1147. func (this *ExcelInfoController) GetFirstEdbData() {
  1148. br := new(models.BaseResponse).Init()
  1149. defer func() {
  1150. this.Data["json"] = br
  1151. this.ServeJSON()
  1152. }()
  1153. sysUser := this.SysUser
  1154. if sysUser == nil {
  1155. br.Msg = "请登录"
  1156. br.ErrMsg = "请登录,SysUser Is Empty"
  1157. br.Ret = 408
  1158. return
  1159. }
  1160. edbInfoId, _ := this.GetInt("EdbInfoId")
  1161. num, _ := this.GetInt("Num")
  1162. if edbInfoId <= 0 {
  1163. br.Msg = "请选择指标"
  1164. br.IsSendEmail = false
  1165. return
  1166. }
  1167. if num <= 0 {
  1168. br.Msg = "期数必须大于0"
  1169. br.IsSendEmail = false
  1170. return
  1171. }
  1172. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  1173. if err != nil {
  1174. br.Msg = "获取失败"
  1175. br.ErrMsg = fmt.Sprint("获取指标信息失败,Err:", err.Error())
  1176. return
  1177. }
  1178. dataList, err := data.GetFirstEdbDataList(edbInfo, num, []string{})
  1179. if err != nil {
  1180. br.Msg = "获取失败"
  1181. br.ErrMsg = fmt.Sprint("获取失败,Err:", err.Error())
  1182. return
  1183. }
  1184. br.Ret = 200
  1185. br.Success = true
  1186. br.Msg = "获取成功"
  1187. br.Data = response.TableDataItem{
  1188. EdbInfoId: edbInfoId,
  1189. Data: dataList,
  1190. }
  1191. }
  1192. // GetOtherEdbData
  1193. // @Title 获取指定日期数据接口
  1194. // @Description 获取未来日期数据接口
  1195. // @Param request body request.GetOtherEdbDateDataReq true "type json string"
  1196. // @router /excel_info/table/other_edb_data_list [post]
  1197. func (this *ExcelInfoController) GetOtherEdbData() {
  1198. br := new(models.BaseResponse).Init()
  1199. defer func() {
  1200. this.Data["json"] = br
  1201. this.ServeJSON()
  1202. }()
  1203. sysUser := this.SysUser
  1204. if sysUser == nil {
  1205. br.Msg = "请登录"
  1206. br.ErrMsg = "请登录,SysUser Is Empty"
  1207. br.Ret = 408
  1208. return
  1209. }
  1210. var req request.GetOtherEdbDateDataReq
  1211. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1212. if err != nil {
  1213. br.Msg = "参数解析异常!"
  1214. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1215. return
  1216. }
  1217. if req.EdbInfoId <= 0 {
  1218. br.Msg = "请选择指标"
  1219. br.IsSendEmail = false
  1220. return
  1221. }
  1222. if len(req.DateList) <= 0 {
  1223. br.Msg = "请选择日期"
  1224. br.IsSendEmail = false
  1225. return
  1226. }
  1227. edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  1228. if err != nil {
  1229. br.Msg = "获取失败"
  1230. br.ErrMsg = fmt.Sprint("获取指标信息失败,Err:", err.Error())
  1231. return
  1232. }
  1233. dataList, err := data.GetOtherEdbDataList(edbInfo, req.DateList)
  1234. if err != nil {
  1235. br.Msg = "获取失败"
  1236. br.ErrMsg = fmt.Sprint("获取失败,Err:", err.Error())
  1237. return
  1238. }
  1239. br.Ret = 200
  1240. br.Success = true
  1241. br.Msg = "获取成功"
  1242. br.Data = response.TableDataItem{
  1243. EdbInfoId: req.EdbInfoId,
  1244. Data: dataList,
  1245. }
  1246. }
  1247. // GetFutureDateData
  1248. // @Title 获取未来日期数据接口
  1249. // @Description 获取未来日期数据接口
  1250. // @Param request body request.GetFutureDateDataReq true "type json string"
  1251. // @router /excel_info/table/future_date_list [post]
  1252. func (this *ExcelInfoController) GetFutureDateData() {
  1253. br := new(models.BaseResponse).Init()
  1254. defer func() {
  1255. this.Data["json"] = br
  1256. this.ServeJSON()
  1257. }()
  1258. sysUser := this.SysUser
  1259. if sysUser == nil {
  1260. br.Msg = "请登录"
  1261. br.ErrMsg = "请登录,SysUser Is Empty"
  1262. br.Ret = 408
  1263. return
  1264. }
  1265. var req request.GetFutureDateDataReq
  1266. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1267. if err != nil {
  1268. br.Msg = "参数解析异常!"
  1269. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1270. return
  1271. }
  1272. if req.StartDate == `` {
  1273. br.Msg = "开始日期未传"
  1274. br.IsSendEmail = false
  1275. return
  1276. }
  1277. startDate, err := time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local)
  1278. if err != nil {
  1279. br.Msg = "开始日期异常"
  1280. br.ErrMsg = fmt.Sprint("开始日期异常,startDate:", req.StartDate, ";err:", err.Error())
  1281. return
  1282. }
  1283. if len(req.EdbInfoIdList) <= 0 {
  1284. br.Msg = "请选择指标"
  1285. br.IsSendEmail = false
  1286. return
  1287. }
  1288. baseEdbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoIdList[0])
  1289. if err != nil {
  1290. br.Msg = "查找指标失败"
  1291. br.Msg = "查找指标失败,err:" + err.Error()
  1292. return
  1293. }
  1294. // 第一个指标的数据列表
  1295. var firstEdbDataList []*data_manage.EdbDataList
  1296. {
  1297. switch baseEdbInfo.EdbInfoType {
  1298. case 0:
  1299. firstEdbDataList, err = data_manage.GetEdbDataList(baseEdbInfo.Source, baseEdbInfo.EdbInfoId, req.StartDate, ``)
  1300. case 1:
  1301. _, firstEdbDataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(baseEdbInfo.EdbInfoId, req.StartDate, ``, false)
  1302. default:
  1303. br.Msg = "指标类型异常"
  1304. br.ErrMsg = fmt.Sprint("获取失败,指标类型异常", baseEdbInfo.EdbInfoType)
  1305. br.IsSendEmail = false
  1306. return
  1307. }
  1308. }
  1309. dateList := make([]time.Time, 0)
  1310. if req.DateType == 1 {
  1311. num := req.Num
  1312. i := 0 // 数据中有的日期数
  1313. for _, v := range firstEdbDataList {
  1314. tmpDate, err := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  1315. if err != nil {
  1316. br.Msg = "结束日期异常"
  1317. br.ErrMsg = fmt.Sprint("结束日期异常,startDate:", req.EndDate, ";err:", err.Error())
  1318. return
  1319. }
  1320. if tmpDate.After(startDate) {
  1321. dateList = append(dateList, tmpDate)
  1322. startDate = tmpDate
  1323. i++
  1324. }
  1325. if i >= num {
  1326. break
  1327. }
  1328. }
  1329. num = num - i
  1330. if num > 0 {
  1331. tmpDateList := utils.GetPredictEdbDayListByNum(startDate, num, baseEdbInfo.Frequency)
  1332. dateList = append(dateList, tmpDateList...)
  1333. }
  1334. } else {
  1335. if req.EndDate == `` {
  1336. br.Msg = "结束日期未传"
  1337. br.IsSendEmail = false
  1338. return
  1339. }
  1340. endDate, err := time.ParseInLocation(utils.FormatDate, req.EndDate, time.Local)
  1341. if err != nil {
  1342. br.Msg = "结束日期异常"
  1343. br.ErrMsg = fmt.Sprint("结束日期异常,startDate:", req.EndDate, ";err:", err.Error())
  1344. return
  1345. }
  1346. for _, v := range firstEdbDataList {
  1347. tmpDate, err := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  1348. if err != nil {
  1349. br.Msg = "结束日期异常"
  1350. br.ErrMsg = fmt.Sprint("结束日期异常,startDate:", req.EndDate, ";err:", err.Error())
  1351. return
  1352. }
  1353. if tmpDate.Equal(endDate) || (tmpDate.Before(endDate) && tmpDate.After(startDate)) {
  1354. dateList = append(dateList, tmpDate)
  1355. startDate = tmpDate
  1356. }
  1357. }
  1358. if startDate.Before(endDate) {
  1359. tmpDateList := utils.GetPredictEdbDayListByEndDate(startDate, endDate, baseEdbInfo.Frequency)
  1360. dateList = append(dateList, tmpDateList...)
  1361. }
  1362. }
  1363. // 获取所有的指标信息
  1364. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  1365. {
  1366. edbInfoList, err := data_manage.GetEdbInfoByIdList(req.EdbInfoIdList)
  1367. if err != nil {
  1368. br.Msg = "获取指标信息失败"
  1369. br.ErrMsg = "获取指标信息失败,err:" + err.Error()
  1370. return
  1371. }
  1372. for _, v := range edbInfoList {
  1373. edbInfoMap[v.EdbInfoId] = v
  1374. }
  1375. }
  1376. result := make([]response.TableDataItem, 0)
  1377. dateStrList := make([]string, 0)
  1378. lenDate := len(dateList)
  1379. if lenDate > 0 {
  1380. for i := lenDate - 1; i >= 0; i-- {
  1381. dateStrList = append(dateStrList, dateList[i].Format(utils.FormatDate))
  1382. }
  1383. for _, v := range req.EdbInfoIdList {
  1384. tmpEdbInfo, ok := edbInfoMap[v]
  1385. if !ok {
  1386. br.Msg = "获取指标信息失败"
  1387. br.ErrMsg = "获取指标信息失败,err:" + err.Error()
  1388. return
  1389. }
  1390. dataList, err := data.GetOtherEdbDataList(tmpEdbInfo, dateStrList)
  1391. if err != nil {
  1392. br.Msg = "获取失败"
  1393. br.ErrMsg = fmt.Sprint("获取失败,Err:", err.Error())
  1394. return
  1395. }
  1396. result = append(result, response.TableDataItem{
  1397. EdbInfoId: v,
  1398. Data: dataList,
  1399. })
  1400. }
  1401. }
  1402. br.Ret = 200
  1403. br.Success = true
  1404. br.Msg = "获取成功"
  1405. br.Data = result
  1406. }
  1407. // GetHistoryDateData
  1408. // @Title 获取历史日期数据接口
  1409. // @Description 获取历史日期数据接口
  1410. // @Param request body request.GetFutureDateDataReq true "type json string"
  1411. // @router /excel_info/table/history_date_list [post]
  1412. func (this *ExcelInfoController) GetHistoryDateData() {
  1413. br := new(models.BaseResponse).Init()
  1414. defer func() {
  1415. this.Data["json"] = br
  1416. this.ServeJSON()
  1417. }()
  1418. sysUser := this.SysUser
  1419. if sysUser == nil {
  1420. br.Msg = "请登录"
  1421. br.ErrMsg = "请登录,SysUser Is Empty"
  1422. br.Ret = 408
  1423. return
  1424. }
  1425. var req request.GetHistoryDateDataReq
  1426. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1427. if err != nil {
  1428. br.Msg = "参数解析异常!"
  1429. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1430. return
  1431. }
  1432. if req.EndDate == `` {
  1433. br.Msg = "结束日期未传"
  1434. br.IsSendEmail = false
  1435. return
  1436. }
  1437. if len(req.EdbInfoIdList) <= 0 {
  1438. br.Msg = "请选择指标"
  1439. br.IsSendEmail = false
  1440. return
  1441. }
  1442. // 获取所有的指标信息
  1443. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  1444. {
  1445. edbInfoList, err := data_manage.GetEdbInfoByIdList(req.EdbInfoIdList)
  1446. if err != nil {
  1447. br.Msg = "获取指标信息失败"
  1448. br.ErrMsg = "获取指标信息失败,err:" + err.Error()
  1449. return
  1450. }
  1451. for _, v := range edbInfoList {
  1452. edbInfoMap[v.EdbInfoId] = v
  1453. }
  1454. }
  1455. result := make([]response.TableDataItem, 0)
  1456. dateStrList := make([]string, 0)
  1457. // 获取A列指标的历史数据
  1458. {
  1459. tmpEdbInfo, ok := edbInfoMap[req.EdbInfoIdList[0]]
  1460. if !ok {
  1461. br.Msg = "获取指标信息失败"
  1462. br.ErrMsg = "获取指标信息失败,err:" + err.Error()
  1463. return
  1464. }
  1465. firstDataList, err := data.GetFirstHistoryEdbDataList(tmpEdbInfo, req.Num, req.EndDate)
  1466. if err != nil {
  1467. br.Msg = "获取失败"
  1468. br.ErrMsg = fmt.Sprint("获取失败,Err:", err.Error())
  1469. return
  1470. }
  1471. result = append(result, response.TableDataItem{
  1472. EdbInfoId: req.EdbInfoIdList[0],
  1473. Data: firstDataList,
  1474. })
  1475. for _, v := range firstDataList {
  1476. dateStrList = append(dateStrList, v.DataTime)
  1477. }
  1478. }
  1479. if len(dateStrList) > 0 {
  1480. for k, v := range req.EdbInfoIdList {
  1481. if k == 0 {
  1482. continue
  1483. }
  1484. tmpEdbInfo, ok := edbInfoMap[v]
  1485. if !ok {
  1486. br.Msg = "获取指标信息失败"
  1487. br.ErrMsg = "获取指标信息失败,err:" + err.Error()
  1488. return
  1489. }
  1490. dataList, err := data.GetOtherEdbDataList(tmpEdbInfo, dateStrList)
  1491. if err != nil {
  1492. br.Msg = "获取失败"
  1493. br.ErrMsg = fmt.Sprint("获取失败,Err:", err.Error())
  1494. return
  1495. }
  1496. result = append(result, response.TableDataItem{
  1497. EdbInfoId: v,
  1498. Data: dataList,
  1499. })
  1500. }
  1501. }
  1502. br.Ret = 200
  1503. br.Success = true
  1504. br.Msg = "获取成功"
  1505. br.Data = result
  1506. }
  1507. // Refresh
  1508. // @Title 刷新
  1509. // @Description 刷新接口
  1510. // @Param ExcelInfoId query int true "表格id"
  1511. // @router /excel_info/table/refresh [get]
  1512. func (this *ExcelInfoController) Refresh() {
  1513. br := new(models.BaseResponse).Init()
  1514. defer func() {
  1515. this.Data["json"] = br
  1516. this.ServeJSON()
  1517. }()
  1518. sysUser := this.SysUser
  1519. if sysUser == nil {
  1520. br.Msg = "请登录"
  1521. br.ErrMsg = "请登录,SysUser Is Empty"
  1522. br.Ret = 408
  1523. return
  1524. }
  1525. excelInfoId, _ := this.GetInt("ExcelInfoId")
  1526. if excelInfoId <= 0 {
  1527. br.Msg = "请选择表格"
  1528. br.ErrMsg = "ExcelInfoId未传"
  1529. br.IsSendEmail = false
  1530. return
  1531. }
  1532. // 获取数据详情
  1533. excelDetail, errMsg, err := data.GetExcelDetailInfoByExcelInfoId(excelInfoId)
  1534. if err != nil {
  1535. br.Msg = errMsg
  1536. br.ErrMsg = err.Error()
  1537. return
  1538. }
  1539. // 操作权限校验
  1540. {
  1541. button := data.GetExcelInfoOpButton(sysUser, excelDetail.SysUserId, excelDetail.Source)
  1542. if !button.RefreshButton {
  1543. br.Msg = "无操作权限"
  1544. br.Msg = "无操作权限"
  1545. br.IsSendEmail = false
  1546. return
  1547. }
  1548. }
  1549. // 数据刷新(只有自定义表格有刷新)
  1550. if excelDetail.Source == 2 {
  1551. jsonStrByte, err := json.Marshal(excelDetail.TableData)
  1552. if err != nil {
  1553. br.Msg = "自定义表格数据获取失败"
  1554. br.ErrMsg = "自定义表格数据获取失败,转json失败,Err:" + err.Error()
  1555. return
  1556. }
  1557. var tableData request.TableDataReq
  1558. err = json.Unmarshal(jsonStrByte, &tableData)
  1559. if err != nil {
  1560. br.Msg = "自定义表格数据获取失败"
  1561. br.ErrMsg = "自定义表格数据获取失败,json转结构体失败,Err:" + err.Error()
  1562. return
  1563. }
  1564. if len(tableData.EdbInfoIdList) > 0 {
  1565. err = data.EdbInfoRefreshAllFromBaseV3(tableData.EdbInfoIdList, false)
  1566. if err != nil {
  1567. br.Msg = "刷新失败"
  1568. br.ErrMsg = "刷新失败,Err:" + err.Error()
  1569. return
  1570. }
  1571. }
  1572. }
  1573. br.Ret = 200
  1574. br.Success = true
  1575. br.Msg = "刷新成功"
  1576. }
  1577. // Download
  1578. // @Title 获取表格详情
  1579. // @Description 获取表格详情接口
  1580. // @Param UniqueCode query string true "表格code"
  1581. // @Param ExcelInfoId query int true "表格id"
  1582. // @Success 200 {object} data_manage.ExcelInfo
  1583. // @router /excel_info/table/download [get]
  1584. func (this *ExcelInfoController) Download() {
  1585. br := new(models.BaseResponse).Init()
  1586. defer func() {
  1587. this.Data["json"] = br
  1588. this.ServeJSON()
  1589. }()
  1590. sysUser := this.SysUser
  1591. if sysUser == nil {
  1592. br.Msg = "请登录"
  1593. br.ErrMsg = "请登录,SysUser Is Empty"
  1594. br.Ret = 408
  1595. return
  1596. }
  1597. excelInfoId, _ := this.GetInt("ExcelInfoId")
  1598. uniqueCode := this.GetString("UniqueCode")
  1599. if excelInfoId <= 0 && uniqueCode == `` {
  1600. br.Msg = "请选择表格"
  1601. br.ErrMsg = "ExcelInfoId和UniqueCode都未传"
  1602. br.IsSendEmail = false
  1603. return
  1604. }
  1605. var excelInfo *data_manage.ExcelInfo
  1606. var err error
  1607. //获取eta表格信息
  1608. if excelInfoId > 0 {
  1609. excelInfo, err = data_manage.GetExcelInfoById(excelInfoId)
  1610. } else {
  1611. excelInfo, err = data_manage.GetExcelInfoByUniqueCode(uniqueCode)
  1612. }
  1613. if err != nil {
  1614. br.Msg = "获取失败"
  1615. br.ErrMsg = "获取ETA表格信息失败,Err:" + err.Error()
  1616. if err.Error() == utils.ErrNoRow() {
  1617. br.Msg = "ETA表格被删除,请刷新页面"
  1618. br.ErrMsg = "ETA表格被删除,请刷新页面,Err:" + err.Error()
  1619. br.IsSendEmail = false
  1620. }
  1621. return
  1622. }
  1623. if excelInfo.Source == 1 {
  1624. br.Msg = "不是自定义表格"
  1625. br.IsSendEmail = false
  1626. return
  1627. }
  1628. var tableData excel.TableData
  1629. switch excelInfo.Source {
  1630. case 1:
  1631. br.Msg = "表格类型异常"
  1632. case 2: // 自定义表格
  1633. var tableDataConfig data.TableDataConfig
  1634. err = json.Unmarshal([]byte(excelInfo.Content), &tableDataConfig)
  1635. if err != nil {
  1636. br.Msg = "获取失败"
  1637. br.ErrMsg = "表格json转结构体失败,Err:" + err.Error()
  1638. return
  1639. }
  1640. result, err := data.GetDataByTableDataConfig(tableDataConfig)
  1641. if err != nil {
  1642. br.Msg = "获取失败"
  1643. br.ErrMsg = "获取最新的表格数据失败,Err:" + err.Error()
  1644. return
  1645. }
  1646. tableData, err = excel.GetTableDataByCustomData(excelInfo.ExcelType, result)
  1647. if err != nil {
  1648. br.Msg = "获取失败"
  1649. br.ErrMsg = "转换成table失败,Err:" + err.Error()
  1650. return
  1651. }
  1652. case 3: // 混合表格
  1653. var result request.MixedTableReq
  1654. err = json.Unmarshal([]byte(excelInfo.Content), &result)
  1655. if err != nil {
  1656. br.Msg = "获取失败"
  1657. br.ErrMsg = "表格json转结构体失败,Err:" + err.Error()
  1658. return
  1659. }
  1660. newResult, tmpErr := data.GetMixedTableCellData(result.Data)
  1661. if tmpErr != nil {
  1662. br.Msg = "获取失败"
  1663. br.ErrMsg = "获取最新的数据失败,Err:" + err.Error()
  1664. return
  1665. }
  1666. tableData, err = excel.GetTableDataByMixedTableData(newResult)
  1667. if err != nil {
  1668. br.Msg = "获取失败"
  1669. br.ErrMsg = "转换成table失败,Err:" + err.Error()
  1670. return
  1671. }
  1672. }
  1673. downloadFilePath, err := tableData.ToExcel()
  1674. if err != nil {
  1675. fmt.Println("err:", err)
  1676. return
  1677. }
  1678. defer func() {
  1679. os.Remove(downloadFilePath)
  1680. }()
  1681. downloadFileName := excelInfo.ExcelName + ".xlsx"
  1682. this.Ctx.Output.Download(downloadFilePath, downloadFileName)
  1683. br.Ret = 200
  1684. br.Success = true
  1685. br.Msg = "获取成功"
  1686. br.Data = excelInfo
  1687. }
  1688. // Copy
  1689. // @Title 复制表格
  1690. // @Description 复制表格接口
  1691. // @Param request body request.EditExcelInfoReq true "type json string"
  1692. // @Success 200 {object} response.AddExcelInfoResp
  1693. // @router /excel_info/copy [post]
  1694. func (this *ExcelInfoController) Copy() {
  1695. br := new(models.BaseResponse).Init()
  1696. defer func() {
  1697. this.Data["json"] = br
  1698. this.ServeJSON()
  1699. }()
  1700. sysUser := this.SysUser
  1701. if sysUser == nil {
  1702. br.Msg = "请登录"
  1703. br.ErrMsg = "请登录,SysUser Is Empty"
  1704. br.Ret = 408
  1705. return
  1706. }
  1707. deleteCache := true
  1708. var req request.CopyExcelInfoReq
  1709. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1710. if err != nil {
  1711. br.Msg = "参数解析异常!"
  1712. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1713. return
  1714. }
  1715. if req.ExcelInfoId <= 0 {
  1716. br.Msg = "请传入ETA表格标识!"
  1717. return
  1718. }
  1719. cacheKey := "CACHE_TABLE_INFO_EDIT_" + strconv.Itoa(req.ExcelInfoId)
  1720. defer func() {
  1721. if deleteCache {
  1722. _ = utils.Rc.Delete(cacheKey)
  1723. }
  1724. }()
  1725. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  1726. deleteCache = false
  1727. br.Msg = "系统处理中,请稍后重试!"
  1728. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  1729. return
  1730. }
  1731. req.ExcelName = strings.Trim(req.ExcelName, " ")
  1732. if req.ExcelName == "" {
  1733. br.Msg = "请填写表格名称!"
  1734. br.IsSendEmail = false
  1735. return
  1736. }
  1737. if req.ExcelClassifyId <= 0 {
  1738. br.Msg = "分类参数错误!"
  1739. br.IsSendEmail = false
  1740. return
  1741. }
  1742. excelClassify, err := data_manage.GetExcelClassifyById(req.ExcelClassifyId)
  1743. if err != nil {
  1744. if err.Error() == utils.ErrNoRow() {
  1745. br.Msg = "分类不存在"
  1746. br.ErrMsg = "分类不存在"
  1747. br.IsSendEmail = false
  1748. return
  1749. }
  1750. br.Msg = "获取分类信息失败"
  1751. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  1752. return
  1753. }
  1754. if excelClassify == nil {
  1755. br.Msg = "分类不存在"
  1756. br.ErrMsg = "分类不存在"
  1757. br.IsSendEmail = false
  1758. return
  1759. }
  1760. // 获取原ETA表格信息
  1761. oldExcelInfo, err := data_manage.GetExcelInfoById(req.ExcelInfoId)
  1762. if err != nil {
  1763. br.Msg = "获取ETA表格失败"
  1764. br.ErrMsg = "获取ETA表格失败,Err:" + err.Error()
  1765. return
  1766. }
  1767. // 操作权限校验
  1768. {
  1769. button := data.GetExcelInfoOpButton(sysUser, oldExcelInfo.SysUserId, oldExcelInfo.Source)
  1770. if !button.CopyButton {
  1771. br.Msg = "无操作权限"
  1772. br.Msg = "无操作权限"
  1773. br.IsSendEmail = false
  1774. return
  1775. }
  1776. }
  1777. // 检验分类下是否存在该表格名称
  1778. {
  1779. var condition string
  1780. var pars []interface{}
  1781. condition += " AND excel_classify_id=? "
  1782. pars = append(pars, req.ExcelClassifyId)
  1783. condition += " AND excel_name=? "
  1784. pars = append(pars, req.ExcelName)
  1785. count, err := data_manage.GetExcelInfoCountByCondition(condition, pars)
  1786. if err != nil {
  1787. br.Msg = "判断表格名称是否存在失败"
  1788. br.ErrMsg = "判断表格名称是否存在失败,Err:" + err.Error()
  1789. return
  1790. }
  1791. if count > 0 {
  1792. br.Msg = "表格名称已存在,请重新填写表格名称"
  1793. br.IsSendEmail = false
  1794. return
  1795. }
  1796. }
  1797. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  1798. excelInfo := &data_manage.ExcelInfo{
  1799. //ExcelInfoId: 0,
  1800. ExcelName: req.ExcelName,
  1801. Source: oldExcelInfo.Source,
  1802. ExcelType: oldExcelInfo.ExcelType,
  1803. UniqueCode: utils.MD5(utils.EXCEL_DATA_PREFIX + "_" + timestamp),
  1804. ExcelClassifyId: req.ExcelClassifyId,
  1805. SysUserId: sysUser.AdminId,
  1806. SysUserRealName: sysUser.RealName,
  1807. Content: oldExcelInfo.Content,
  1808. ExcelImage: oldExcelInfo.ExcelImage,
  1809. Sort: 0,
  1810. IsDelete: 0,
  1811. ModifyTime: time.Now(),
  1812. CreateTime: time.Now(),
  1813. }
  1814. err = data_manage.AddExcelInfo(excelInfo)
  1815. if err != nil {
  1816. br.Msg = "保存失败"
  1817. br.ErrMsg = "保存失败,Err:" + err.Error()
  1818. return
  1819. }
  1820. // 更新excel下载地址(默认的EXCEL需要更新,自定义表格不需要更新)
  1821. if excelInfo.Source == 1 {
  1822. go UpdateExcelInfoFileUrl(excelInfo)
  1823. }
  1824. resp := new(response.AddExcelInfoResp)
  1825. resp.ExcelInfoId = excelInfo.ExcelInfoId
  1826. resp.UniqueCode = excelInfo.UniqueCode
  1827. br.Ret = 200
  1828. br.Success = true
  1829. br.Msg = "保存成功"
  1830. br.Data = resp
  1831. br.IsAddLog = true
  1832. }
  1833. //func init() {
  1834. // //list, err := data.GetFirstEdbDataList(102739, 12)
  1835. // list, err := data.GetFirstEdbDataList(101323, 12)
  1836. // if err != nil {
  1837. // fmt.Println(err)
  1838. // return
  1839. // }
  1840. //
  1841. // fmt.Println("START")
  1842. // dateList := make([]string, 0)
  1843. // for _, v := range list {
  1844. // fmt.Println(v)
  1845. // dateList = append(dateList, v.DataTime)
  1846. // }
  1847. //
  1848. // fmt.Println("End")
  1849. //
  1850. // {
  1851. // list, err := data.GetOtherEdbDataList(102227, dateList)
  1852. // if err != nil {
  1853. // fmt.Println(err)
  1854. // return
  1855. // }
  1856. //
  1857. // for _, v := range list {
  1858. // fmt.Println(v)
  1859. // }
  1860. // }
  1861. //}
  1862. //func init() {
  1863. // 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":[]}]]}`
  1864. // 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":[]}]]}`
  1865. //
  1866. // //var req request.TableDataReq
  1867. // //err := json.Unmarshal([]byte(str), &req)
  1868. // //if err != nil {
  1869. // // fmt.Println(err)
  1870. // // return
  1871. // //}
  1872. // //tableDataConfig, err := data.GetTableDataConfig(req)
  1873. // //if err != nil {
  1874. // // fmt.Println(err)
  1875. // // return
  1876. // //}
  1877. // var tableDataConfig data.TableDataConfig
  1878. // err := json.Unmarshal([]byte(str), &tableDataConfig)
  1879. // if err != nil {
  1880. // fmt.Println(err)
  1881. // return
  1882. // }
  1883. //
  1884. // fmt.Println(tableDataConfig)
  1885. //
  1886. // result, err := data.GetDataByTableDataConfig(tableDataConfig)
  1887. // if err != nil {
  1888. // fmt.Println(err)
  1889. // return
  1890. // }
  1891. // fmt.Println(result)
  1892. // //byteStr, _ := json.Marshal(result)
  1893. // //utils.FileLog.Info(string(byteStr))
  1894. //}