excel_info.go 58 KB

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