excel_info.go 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758
  1. package excel
  2. import (
  3. "archive/zip"
  4. "encoding/json"
  5. "eta/eta_mobile/controllers"
  6. "eta/eta_mobile/models"
  7. "eta/eta_mobile/models/data_manage"
  8. excel3 "eta/eta_mobile/models/data_manage/excel"
  9. "eta/eta_mobile/models/data_manage/excel/request"
  10. "eta/eta_mobile/models/data_manage/excel/response"
  11. "eta/eta_mobile/services"
  12. "eta/eta_mobile/services/data"
  13. "eta/eta_mobile/services/data/data_manage_permission"
  14. excel2 "eta/eta_mobile/services/data/excel"
  15. "eta/eta_mobile/services/excel"
  16. "eta/eta_mobile/utils"
  17. "fmt"
  18. "github.com/rdlucklib/rdluck_tools/paging"
  19. "github.com/shopspring/decimal"
  20. "github.com/yidane/formula"
  21. "io"
  22. "os"
  23. "path/filepath"
  24. "strconv"
  25. "strings"
  26. "time"
  27. )
  28. // ExcelInfoController ETA表格管理
  29. type ExcelInfoController struct {
  30. controllers.BaseAuthController
  31. }
  32. // Add
  33. // @Title 新增表格接口
  34. // @Description 新增表格接口
  35. // @Param request body request.AddExcelInfoReq true "type json string"
  36. // @Success 200 {object} response.AddExcelInfoResp
  37. // @router /excel_info/add [post]
  38. func (c *ExcelInfoController) Add() {
  39. br := new(models.BaseResponse).Init()
  40. defer func() {
  41. c.Data["json"] = br
  42. c.ServeJSON()
  43. }()
  44. sysUser := c.SysUser
  45. if sysUser == nil {
  46. br.Msg = "请登录"
  47. br.ErrMsg = "请登录,SysUser Is Empty"
  48. br.Ret = 408
  49. return
  50. }
  51. deleteCache := true
  52. cacheKey := "CACHE_EXCEL_TABLE_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  53. defer func() {
  54. if deleteCache {
  55. _ = utils.Rc.Delete(cacheKey)
  56. }
  57. }()
  58. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  59. deleteCache = false
  60. br.Msg = "系统处理中,请稍后重试!"
  61. br.ErrMsg = "系统处理中,请稍后重试!"
  62. return
  63. }
  64. var req request.AddExcelInfoReq
  65. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  66. if err != nil {
  67. br.Msg = "参数解析异常!"
  68. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  69. return
  70. }
  71. req.ExcelName = strings.Trim(req.ExcelName, " ")
  72. if req.ExcelName == "" {
  73. br.Msg = "请填写表格名称!"
  74. br.IsSendEmail = false
  75. return
  76. }
  77. if req.ExcelClassifyId <= 0 {
  78. br.Msg = "分类参数错误!"
  79. br.IsSendEmail = false
  80. return
  81. }
  82. excelClassify, err := excel3.GetExcelClassifyById(req.ExcelClassifyId)
  83. if err != nil {
  84. if err.Error() == utils.ErrNoRow() {
  85. br.Msg = "分类不存在"
  86. br.ErrMsg = "分类不存在"
  87. br.IsSendEmail = false
  88. return
  89. }
  90. br.Msg = "获取分类信息失败"
  91. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  92. return
  93. }
  94. if excelClassify == nil {
  95. br.Msg = "分类不存在"
  96. br.ErrMsg = "分类不存在"
  97. br.IsSendEmail = false
  98. return
  99. }
  100. var condition string
  101. var pars []interface{}
  102. condition += " AND excel_classify_id=? "
  103. pars = append(pars, req.ExcelClassifyId)
  104. condition += " AND excel_name=? "
  105. pars = append(pars, req.ExcelName)
  106. // 获取分类下是否存在该表格名称
  107. count, err := excel3.GetExcelInfoCountByCondition(condition, pars)
  108. if err != nil {
  109. br.Msg = "判断表格名称是否存在失败"
  110. br.ErrMsg = "判断表格名称是否存在失败,Err:" + err.Error()
  111. return
  112. }
  113. if count > 0 {
  114. br.Msg = "表格名称已存在,请重新填写表格名称"
  115. br.IsSendEmail = false
  116. return
  117. }
  118. // 默认的excel类型是1
  119. if req.Source == 0 {
  120. req.Source = 1
  121. }
  122. content := req.Content
  123. // 引用的指标id
  124. edbInfoIdList := make([]int, 0)
  125. // 自定义表格
  126. if req.Source == 2 {
  127. jsonStrByte, err := json.Marshal(req.TableData)
  128. if err != nil {
  129. br.Msg = "自定义表格数据获取失败"
  130. br.ErrMsg = "自定义表格数据获取失败,转json失败,Err:" + err.Error()
  131. return
  132. }
  133. var tableData request.TableDataReq
  134. err = json.Unmarshal(jsonStrByte, &tableData)
  135. if err != nil {
  136. br.Msg = "自定义表格数据获取失败"
  137. br.ErrMsg = "自定义表格数据获取失败,json转结构体失败,Err:" + err.Error()
  138. return
  139. }
  140. tableDataConfig, err := excel2.GetTableDataConfig(tableData)
  141. if err != nil {
  142. br.Msg = "自定义表格数据获取失败"
  143. br.ErrMsg = "自定义表格数据获取失败,Err:" + err.Error()
  144. return
  145. }
  146. edbInfoIdList = tableDataConfig.EdbInfoIdList
  147. contentByte, err := json.Marshal(tableDataConfig)
  148. if err != nil {
  149. br.Msg = "自定义表格数据获取失败"
  150. br.ErrMsg = "自定义表格数据获取后,转json失败,Err:" + err.Error()
  151. return
  152. }
  153. content = string(contentByte)
  154. }
  155. // 混合表格
  156. if req.Source == 3 {
  157. contentByte, err := json.Marshal(req.TableData)
  158. if err != nil {
  159. br.Msg = "自定义表格数据获取失败"
  160. br.ErrMsg = "自定义表格数据获取后,转json失败,Err:" + err.Error()
  161. return
  162. }
  163. content = string(contentByte)
  164. var result request.MixedTableReq
  165. err = json.Unmarshal(contentByte, &result)
  166. if err != nil {
  167. br.Msg = "获取失败"
  168. br.ErrMsg = "表格json转结构体失败,Err:" + err.Error()
  169. return
  170. }
  171. newResult, err, errMsg := excel2.GetMixedTableCellData(result, c.Lang)
  172. if err != nil {
  173. br.Msg = "获取失败"
  174. if errMsg != `` {
  175. br.Msg = errMsg
  176. }
  177. br.ErrMsg = "获取最新的数据失败,Err:" + err.Error()
  178. return
  179. }
  180. edbInfoIdMap := make(map[int]int)
  181. for _, tmpV := range newResult {
  182. for _, v := range tmpV {
  183. if v.EdbInfoId > 0 {
  184. if _, ok := edbInfoIdMap[v.EdbInfoId]; !ok {
  185. edbInfoIdMap[v.EdbInfoId] = v.EdbInfoId
  186. edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
  187. }
  188. }
  189. }
  190. }
  191. }
  192. //获取该层级下最大的排序数
  193. maxSort, err := excel2.GetExcelClassifyMaxSort(req.ExcelClassifyId, req.Source)
  194. if err != nil {
  195. br.Msg = "获取失败"
  196. br.ErrMsg = "查询排序信息失败,Err:" + err.Error()
  197. return
  198. }
  199. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  200. excelInfo := &excel3.ExcelInfo{
  201. //ExcelInfoId: 0,
  202. ExcelName: req.ExcelName,
  203. Source: req.Source,
  204. ExcelType: req.ExcelType,
  205. UniqueCode: utils.MD5(utils.EXCEL_DATA_PREFIX + "_" + timestamp),
  206. ExcelClassifyId: req.ExcelClassifyId,
  207. SysUserId: sysUser.AdminId,
  208. SysUserRealName: sysUser.RealName,
  209. Content: content,
  210. ExcelImage: req.ExcelImage,
  211. Sort: maxSort + 1,
  212. IsDelete: 0,
  213. ModifyTime: time.Now(),
  214. CreateTime: time.Now(),
  215. SourcesFrom: req.SourcesFrom,
  216. }
  217. excelEdbMappingList := make([]*excel3.ExcelEdbMapping, 0)
  218. if len(edbInfoIdList) > 0 {
  219. for _, edbInfoId := range edbInfoIdList {
  220. excelEdbMappingList = append(excelEdbMappingList, &excel3.ExcelEdbMapping{
  221. //ExcelEdbMappingId: 0,
  222. //ExcelInfoId: 0,
  223. Source: excelInfo.Source,
  224. EdbInfoId: edbInfoId,
  225. CreateTime: time.Now(),
  226. ModifyTime: time.Now(),
  227. })
  228. }
  229. }
  230. err = excel3.AddExcelInfo(excelInfo, excelEdbMappingList)
  231. if err != nil {
  232. br.Msg = "保存失败"
  233. br.ErrMsg = "保存失败,Err:" + err.Error()
  234. return
  235. }
  236. // 更新excel下载地址(默认的EXCEL需要更新,自定义表格不需要更新)
  237. if req.Source == 1 {
  238. go excel2.UpdateExcelInfoFileUrl(excelInfo)
  239. }
  240. _, err = services.UpdateExcelEditMark(excelInfo.ExcelInfoId, sysUser.AdminId, 1, sysUser.RealName)
  241. if err != nil {
  242. br.Msg = "查询标记状态失败"
  243. br.ErrMsg = "查询标记状态失败,Err:" + err.Error()
  244. return
  245. }
  246. resp := new(response.AddExcelInfoResp)
  247. resp.ExcelInfoId = excelInfo.ExcelInfoId
  248. resp.UniqueCode = excelInfo.UniqueCode
  249. //新增操作日志
  250. //{
  251. // excelLog := &data_manage.ExcelInfoLog{
  252. // //ExcelInfoLogId: 0,
  253. // ExcelInfoId: excelInfo.ExcelInfoId,
  254. // ExcelName: req.ExcelName,
  255. // ExcelClassifyId: req.ExcelClassifyId,
  256. // SysUserId: sysUser.AdminId,
  257. // SysUserRealName: sysUser.RealName,
  258. // UniqueCode: excelInfo.UniqueCode,
  259. // CreateTime: time.Now(),
  260. // Content: string(c.Ctx.Input.RequestBody),
  261. // Status: "新增表格",
  262. // Method: c.Ctx.Input.URI(),
  263. // }
  264. // go data_manage.AddExcelInfoLog(excelLog)
  265. //}
  266. br.Ret = 200
  267. br.Success = true
  268. br.Msg = "保存成功"
  269. br.Data = resp
  270. br.IsAddLog = true
  271. }
  272. // List
  273. // @Title ETA表格列表接口
  274. // @Description ETA表格列表接口
  275. // @Param PageSize query int true "每页数据条数"
  276. // @Param CurrentIndex query int true "当前页页码,从1开始"
  277. // @Param ExcelClassifyId query int true "分类id"
  278. // @Param Keyword query string true "搜索关键词"
  279. // @Param AdminId query int false "创建人id"
  280. // @Param Source query int true "格来源,1:excel插件的表格,2:自定义表格,3:混合表格,默认:1"
  281. // @Param IsShowMe query bool true "是否只看我的,true、false"
  282. // @Success 200 {object} response.ExcelListResp
  283. // @router /excel_info/list [get]
  284. func (c *ExcelInfoController) List() {
  285. br := new(models.BaseResponse).Init()
  286. defer func() {
  287. c.Data["json"] = br
  288. c.ServeJSON()
  289. }()
  290. sysUser := c.SysUser
  291. if sysUser == nil {
  292. br.Msg = "请登录"
  293. br.ErrMsg = "请登录,SysUser Is Empty"
  294. br.Ret = 408
  295. return
  296. }
  297. excelClassifyId, _ := c.GetInt("ExcelClassifyId")
  298. pageSize, _ := c.GetInt("PageSize")
  299. currentIndex, _ := c.GetInt("CurrentIndex")
  300. keyword := c.GetString("Keyword")
  301. adminId, _ := c.GetInt("AdminId")
  302. source, _ := c.GetInt("Source")
  303. var total int
  304. page := paging.GetPaging(currentIndex, pageSize, total)
  305. var startSize int
  306. if pageSize <= 0 {
  307. pageSize = utils.PageSize20
  308. }
  309. if currentIndex <= 0 {
  310. currentIndex = 1
  311. }
  312. startSize = paging.StartIndex(currentIndex, pageSize)
  313. var condition string
  314. var pars []interface{}
  315. // 如果没有传的话,那就是代表前三种表格
  316. if source <= 0 {
  317. condition += " AND source in (?,?,?) "
  318. pars = append(pars, utils.EXCEL_DEFAULT, utils.TIME_TABLE, utils.MIXED_TABLE)
  319. } else {
  320. condition += " AND source = ? "
  321. pars = append(pars, source)
  322. if source == utils.BALANCE_TABLE { //平衡表的列表只显示动态表的一级表(不显示子表和静态表)
  323. condition += " AND parent_id = 0 AND balance_type=0 "
  324. }
  325. }
  326. // 筛选分类
  327. if excelClassifyId > 0 {
  328. _, err := excel3.GetExcelClassifyById(excelClassifyId)
  329. if err != nil && err.Error() != utils.ErrNoRow() {
  330. br.Msg = "获取表格信息失败"
  331. br.ErrMsg = "获取信息失败,GetExcelClassify,Err:" + err.Error()
  332. return
  333. }
  334. childClassify, e, _ := excel2.GetChildClassifyByClassifyId(excelClassifyId, source)
  335. if e != nil && e.Error() != utils.ErrNoRow() {
  336. br.Msg = "获取失败"
  337. br.ErrMsg = "获取分类信息失败, GetEdbClassify,Err:" + e.Error()
  338. return
  339. }
  340. if len(childClassify) == 0 {
  341. condition += " AND excel_classify_id = ? "
  342. pars = append(pars, excelClassifyId)
  343. } else {
  344. classifyIds := []int{excelClassifyId}
  345. for _, v := range childClassify {
  346. classifyIds = append(classifyIds, v.ExcelClassifyId)
  347. }
  348. condition += fmt.Sprintf(` AND excel_classify_id IN (%s) `, utils.GetOrmInReplace(len(classifyIds)))
  349. pars = append(pars, classifyIds)
  350. }
  351. }
  352. if keyword != "" {
  353. condition += ` AND ( excel_name LIKE ? )`
  354. pars = utils.GetLikeKeywordPars(pars, keyword, 1)
  355. }
  356. if adminId > 0 {
  357. condition += " AND sys_user_id = ? "
  358. pars = append(pars, adminId)
  359. }
  360. //只看我的
  361. isShowMe, _ := c.GetBool("IsShowMe")
  362. // 获取所有有权限的指标和分类
  363. permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserExcelAndClassifyPermissionList(c.SysUser.AdminId, 0, 0)
  364. if err != nil {
  365. br.Msg = "获取失败"
  366. br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  367. return
  368. }
  369. hasCheck := make(map[int]bool)
  370. if isShowMe {
  371. if source == utils.BALANCE_TABLE { //平衡表的,显示同时需要显示协作人相关的图表
  372. //找到当前协作人相关的表格ID
  373. obj := new(excel3.ExcelWorker)
  374. existList, err := obj.GetBySysUserId(sysUser.AdminId)
  375. if err != nil {
  376. br.Msg = "获取表格协作人失败!"
  377. br.ErrMsg = "获取表格协作人失败,Err:" + err.Error()
  378. return
  379. }
  380. var excelIds []int
  381. newCondition := condition
  382. newPars := pars
  383. if len(existList) > 0 {
  384. for _, v := range existList {
  385. excelIds = append(excelIds, v.ExcelInfoId)
  386. }
  387. newCondition += fmt.Sprintf(` AND ( excel_info_id IN (%s) or sys_user_id = ?)`, utils.GetOrmInReplace(len(excelIds)))
  388. newPars = append(newPars, excelIds, sysUser.AdminId)
  389. } else {
  390. newCondition += ` AND sys_user_id = ? `
  391. newPars = append(newPars, sysUser.AdminId)
  392. }
  393. //获取表格信息
  394. tmpList, e := excel3.GetNoContentExcelListByConditionNoPage(newCondition, newPars)
  395. if e != nil && e.Error() != utils.ErrNoRow() {
  396. br.Success = true
  397. br.Msg = "获取表格信息失败"
  398. br.ErrMsg = "获取表格信息失败,Err:" + e.Error()
  399. return
  400. }
  401. classifyIdListTmp := make([]int, 0)
  402. for _, v := range tmpList {
  403. classifyIdListTmp = append(classifyIdListTmp, v.ExcelClassifyId)
  404. }
  405. classifyMap := make(map[int]*excel3.ExcelClassify)
  406. // 分类信息
  407. if len(classifyIdListTmp) > 0 {
  408. classifyListTmp, e := excel3.GetClassifyByIdList(classifyIdListTmp)
  409. if e != nil {
  410. br.Msg = "获取表格分类信息失败"
  411. br.ErrMsg = "获取表格分类列表数据失败,Err:" + e.Error()
  412. return
  413. }
  414. for _, v := range classifyListTmp {
  415. classifyMap[v.ExcelClassifyId] = v
  416. }
  417. }
  418. excelIds = make([]int, 0)
  419. for _, v := range tmpList {
  420. // 数据权限
  421. if classifyInfo, ok := classifyMap[v.ExcelClassifyId]; ok {
  422. v.HaveOperaAuth = data_manage_permission.CheckExcelPermissionByPermissionIdList(v.IsJoinPermission, classifyInfo.IsJoinPermission, v.ExcelInfoId, v.ExcelClassifyId, permissionEdbIdList, permissionClassifyIdList)
  423. if v.HaveOperaAuth {
  424. excelIds = append(excelIds, v.ExcelInfoId)
  425. }
  426. hasCheck[v.ExcelInfoId] = v.HaveOperaAuth
  427. }
  428. }
  429. if len(excelIds) > 0 {
  430. condition += fmt.Sprintf(` AND excel_info_id IN (%s)`, utils.GetOrmInReplace(len(excelIds)))
  431. pars = append(pars, excelIds)
  432. } else {
  433. list := make([]*excel3.MyExcelInfoList, 0)
  434. resp := response.ExcelListResp{
  435. Paging: page,
  436. List: list,
  437. }
  438. br.Ret = 200
  439. br.Success = true
  440. br.Msg = "获取成功"
  441. br.Data = resp
  442. return
  443. }
  444. } else {
  445. condition += " AND sys_user_id = ? "
  446. pars = append(pars, sysUser.AdminId)
  447. }
  448. }
  449. //获取表格信息
  450. list, err := excel3.GetNoContentExcelListByCondition(condition, pars, startSize, pageSize)
  451. if err != nil && err.Error() != utils.ErrNoRow() {
  452. br.Success = true
  453. br.Msg = "获取表格信息失败"
  454. br.ErrMsg = "获取表格信息失败,Err:" + err.Error()
  455. return
  456. }
  457. lenList := len(list)
  458. if list == nil || lenList <= 0 || (err != nil && err.Error() == utils.ErrNoRow()) {
  459. list = make([]*excel3.MyExcelInfoList, 0)
  460. }
  461. if lenList > 0 {
  462. classifyIdList := make([]int, 0)
  463. for _, v := range list {
  464. classifyIdList = append(classifyIdList, v.ExcelClassifyId)
  465. }
  466. classifyMap := make(map[int]*excel3.ExcelClassify)
  467. // 分类信息
  468. {
  469. classifyList, err := excel3.GetClassifyByIdList(classifyIdList)
  470. if err != nil {
  471. br.Msg = "获取表格列表信息失败"
  472. br.ErrMsg = "获取表格分类列表数据失败,Err:" + err.Error()
  473. return
  474. }
  475. for _, v := range classifyList {
  476. classifyMap[v.ExcelClassifyId] = v
  477. }
  478. }
  479. for k, v := range list {
  480. // 数据权限
  481. if authCheck, ok1 := hasCheck[v.ExcelInfoId]; ok1 {
  482. v.HaveOperaAuth = authCheck
  483. } else {
  484. if classifyInfo, ok := classifyMap[v.ExcelClassifyId]; ok {
  485. v.HaveOperaAuth = data_manage_permission.CheckExcelPermissionByPermissionIdList(v.IsJoinPermission, classifyInfo.IsJoinPermission, v.ExcelInfoId, v.ExcelClassifyId, permissionEdbIdList, permissionClassifyIdList)
  486. }
  487. }
  488. if v.Source == utils.BALANCE_TABLE {
  489. //处理按钮权限和编辑状态
  490. markStatus, err := services.UpdateExcelEditMark(v.ExcelInfoId, sysUser.AdminId, 2, sysUser.RealName)
  491. if err != nil {
  492. br.Msg = "查询标记状态失败"
  493. br.ErrMsg = "查询标记状态失败,Err:" + err.Error()
  494. return
  495. }
  496. if markStatus.Status == 0 {
  497. list[k].CanEdit = true
  498. } else {
  499. list[k].Editor = markStatus.Editor
  500. }
  501. // excel表格按钮权限
  502. list[k].Button = excel2.GetBalanceExcelInfoOpButton(sysUser.AdminId, v.SysUserId, v.HaveOperaAuth, v.ExcelInfoId)
  503. }
  504. }
  505. }
  506. // 总数据量
  507. dataCount, err := excel3.GetExcelListCountByCondition(condition, pars)
  508. if err != nil && err.Error() != utils.ErrNoRow() {
  509. br.Msg = "获取表格列表信息失败"
  510. br.ErrMsg = "获取表格列表数据总数失败,Err:" + err.Error()
  511. return
  512. }
  513. page = paging.GetPaging(currentIndex, pageSize, dataCount)
  514. resp := response.ExcelListResp{
  515. Paging: page,
  516. List: list,
  517. }
  518. br.Ret = 200
  519. br.Success = true
  520. br.Msg = "获取成功"
  521. br.Data = resp
  522. }
  523. // Detail
  524. // @Title 获取表格详情
  525. // @Description 获取表格详情接口
  526. // @Param ExcelInfoId query int true "表格id"
  527. // @Success 200 {object} data_manage.ExcelInfo
  528. // @router /excel_info/detail [get]
  529. func (c *ExcelInfoController) Detail() {
  530. br := new(models.BaseResponse).Init()
  531. defer func() {
  532. c.Data["json"] = br
  533. c.ServeJSON()
  534. }()
  535. sysUser := c.SysUser
  536. if sysUser == nil {
  537. br.Msg = "请登录"
  538. br.ErrMsg = "请登录,SysUser Is Empty"
  539. br.Ret = 408
  540. return
  541. }
  542. excelInfoId, _ := c.GetInt("ExcelInfoId")
  543. if excelInfoId <= 0 {
  544. br.Msg = "请选择表格"
  545. br.ErrMsg = "ExcelInfoId未传"
  546. br.IsSendEmail = false
  547. return
  548. }
  549. // 获取数据详情
  550. excelDetail, errMsg, err := excel2.GetExcelDetailInfoByExcelInfoId(excelInfoId, c.SysUser.AdminId, c.Lang)
  551. if err != nil {
  552. br.Msg = errMsg
  553. br.ErrMsg = err.Error()
  554. return
  555. }
  556. // 编辑状态
  557. markStatus, err := services.UpdateExcelEditMark(excelInfoId, sysUser.AdminId, 2, sysUser.RealName)
  558. if err != nil {
  559. br.Msg = "查询标记状态失败"
  560. br.ErrMsg = "查询标记状态失败,Err:" + err.Error()
  561. return
  562. }
  563. if markStatus.Status == 0 {
  564. excelDetail.CanEdit = true
  565. } else {
  566. excelDetail.Editor = markStatus.Editor
  567. }
  568. // excel表格按钮权限
  569. excelDetail.Button = excel2.GetExcelInfoOpButton(sysUser, excelDetail.SysUserId, excelDetail.Source, excelDetail.HaveOperaAuth)
  570. br.Ret = 200
  571. br.Success = true
  572. br.Msg = "获取成功"
  573. br.Data = excelDetail
  574. }
  575. // Edit
  576. // @Title 编辑表格接口
  577. // @Description 新增表格接口
  578. // @Param request body request.EditExcelInfoReq true "type json string"
  579. // @Success 200 {object} response.AddExcelInfoResp
  580. // @router /excel_info/edit [post]
  581. func (c *ExcelInfoController) Edit() {
  582. br := new(models.BaseResponse).Init()
  583. defer func() {
  584. c.Data["json"] = br
  585. c.ServeJSON()
  586. }()
  587. sysUser := c.SysUser
  588. if sysUser == nil {
  589. br.Msg = "请登录"
  590. br.ErrMsg = "请登录,SysUser Is Empty"
  591. br.Ret = 408
  592. return
  593. }
  594. deleteCache := true
  595. var req request.EditExcelInfoReq
  596. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  597. if err != nil {
  598. br.Msg = "参数解析异常!"
  599. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  600. return
  601. }
  602. if req.ExcelInfoId <= 0 {
  603. br.Msg = "请传入ETA表格标识!"
  604. return
  605. }
  606. cacheKey := "CACHE_TABLE_INFO_EDIT_" + strconv.Itoa(req.ExcelInfoId)
  607. defer func() {
  608. if deleteCache {
  609. _ = utils.Rc.Delete(cacheKey)
  610. }
  611. }()
  612. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  613. deleteCache = false
  614. br.Msg = "系统处理中,请稍后重试!"
  615. br.ErrMsg = "系统处理中,请稍后重试!"
  616. return
  617. }
  618. req.ExcelName = strings.Trim(req.ExcelName, " ")
  619. if req.ExcelName == "" {
  620. br.Msg = "请填写表格名称!"
  621. br.IsSendEmail = false
  622. return
  623. }
  624. if req.ExcelClassifyId <= 0 {
  625. br.Msg = "分类参数错误!"
  626. br.IsSendEmail = false
  627. return
  628. }
  629. // 标记编辑状态
  630. markRet, err := services.UpdateExcelEditMark(req.ExcelInfoId, sysUser.AdminId, 1, sysUser.RealName)
  631. if err != nil {
  632. br.Msg = "查询标记状态失败"
  633. br.ErrMsg = "查询标记状态失败,Err:" + err.Error()
  634. return
  635. }
  636. if markRet.Status == 1 {
  637. br.Ret = 200
  638. br.Success = true
  639. br.Data = markRet
  640. return
  641. }
  642. excelClassify, err := excel3.GetExcelClassifyById(req.ExcelClassifyId)
  643. if err != nil {
  644. if err.Error() == utils.ErrNoRow() {
  645. br.Msg = "分类不存在"
  646. br.ErrMsg = "分类不存在"
  647. br.IsSendEmail = false
  648. return
  649. }
  650. br.Msg = "获取分类信息失败"
  651. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  652. return
  653. }
  654. if excelClassify == nil {
  655. br.Msg = "分类不存在"
  656. br.ErrMsg = "分类不存在"
  657. br.IsSendEmail = false
  658. return
  659. }
  660. //判断表格是否存在
  661. var condition string
  662. var pars []interface{}
  663. condition += " AND excel_info_id != ? "
  664. pars = append(pars, req.ExcelInfoId)
  665. condition += " AND excel_classify_id=? "
  666. pars = append(pars, req.ExcelClassifyId)
  667. condition += " AND excel_name=? "
  668. pars = append(pars, req.ExcelName)
  669. // 获取分类下是否存在该表格名称
  670. count, err := excel3.GetExcelInfoCountByCondition(condition, pars)
  671. if err != nil {
  672. br.Msg = "ETA判断表格名称是否存在失败"
  673. br.ErrMsg = "判断ETA表格名称是否存在失败,Err:" + err.Error()
  674. return
  675. }
  676. if count > 0 {
  677. br.Msg = "ETA表格名称已存在,请重新填写"
  678. br.IsSendEmail = false
  679. return
  680. }
  681. excelInfo, err := excel3.GetExcelInfoById(req.ExcelInfoId)
  682. if err != nil {
  683. br.Msg = "获取ETA表格失败"
  684. br.ErrMsg = "获取ETA表格失败,Err:" + err.Error()
  685. return
  686. }
  687. // 操作权限校验
  688. {
  689. // 数据权限
  690. haveOperaAuth, err := data_manage_permission.CheckExcelPermissionByExcelInfoId(excelInfo.ExcelInfoId, excelInfo.ExcelClassifyId, excelInfo.IsJoinPermission, c.SysUser.AdminId)
  691. if err != nil {
  692. br.Msg = "获取ETA表格失败"
  693. br.ErrMsg = "获取ETA表格权限失败,Err:" + err.Error()
  694. return
  695. }
  696. button := excel2.GetExcelInfoOpButton(sysUser, excelInfo.SysUserId, excelInfo.Source, haveOperaAuth)
  697. if !button.OpButton {
  698. br.Msg = "无操作权限"
  699. br.Msg = "无操作权限"
  700. br.IsSendEmail = false
  701. return
  702. }
  703. }
  704. // 引用的指标id
  705. edbInfoIdList := make([]int, 0)
  706. content := req.Content
  707. switch excelInfo.Source {
  708. case utils.TIME_TABLE: // 自定义表格
  709. jsonStrByte, err := json.Marshal(req.TableData)
  710. if err != nil {
  711. br.Msg = "自定义表格数据获取失败"
  712. br.ErrMsg = "自定义表格数据获取失败,转json失败,Err:" + err.Error()
  713. return
  714. }
  715. var tableData request.TableDataReq
  716. err = json.Unmarshal(jsonStrByte, &tableData)
  717. if err != nil {
  718. br.Msg = "自定义表格数据获取失败"
  719. br.ErrMsg = "自定义表格数据获取失败,json转结构体失败,Err:" + err.Error()
  720. return
  721. }
  722. edbInfoIdList = tableData.EdbInfoIdList
  723. tableDataConfig, err := excel2.GetTableDataConfig(tableData)
  724. if err != nil {
  725. br.Msg = "自定义表格数据获取失败"
  726. br.ErrMsg = "自定义表格数据获取失败,Err:" + err.Error()
  727. return
  728. }
  729. contentByte, err := json.Marshal(tableDataConfig)
  730. if err != nil {
  731. br.Msg = "自定义表格数据获取失败"
  732. br.ErrMsg = "自定义表格数据获取后,转json失败,Err:" + err.Error()
  733. return
  734. }
  735. content = string(contentByte)
  736. case utils.MIXED_TABLE: // 混合表格
  737. contentByte, err := json.Marshal(req.TableData)
  738. if err != nil {
  739. br.Msg = "混合表格数据获取失败"
  740. br.ErrMsg = "混合表格数据获取后,转json失败,Err:" + err.Error()
  741. return
  742. }
  743. content = string(contentByte)
  744. var result request.MixedTableReq
  745. err = json.Unmarshal(contentByte, &result)
  746. if err != nil {
  747. br.Msg = "获取失败"
  748. br.ErrMsg = "表格json转结构体失败,Err:" + err.Error()
  749. return
  750. }
  751. newResult, err, errMsg := excel2.GetMixedTableCellData(result, c.Lang)
  752. if err != nil {
  753. br.Msg = "获取失败"
  754. if errMsg != `` {
  755. br.Msg = errMsg
  756. }
  757. br.ErrMsg = "获取最新的数据失败,Err:" + err.Error()
  758. return
  759. }
  760. edbInfoIdMap := make(map[int]int)
  761. for _, tmpV := range newResult {
  762. for _, v := range tmpV {
  763. if v.EdbInfoId > 0 {
  764. if _, ok := edbInfoIdMap[v.EdbInfoId]; !ok {
  765. edbInfoIdMap[v.EdbInfoId] = v.EdbInfoId
  766. edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
  767. }
  768. }
  769. }
  770. }
  771. }
  772. excelInfo.ModifyTime = time.Now()
  773. excelInfo.ExcelName = req.ExcelName
  774. excelInfo.ExcelType = req.ExcelType
  775. excelInfo.ExcelClassifyId = req.ExcelClassifyId
  776. excelInfo.ExcelImage = req.ExcelImage
  777. excelInfo.Content = content
  778. excelInfo.SourcesFrom = req.SourcesFrom
  779. // 自动保存时不会传缩略图,也就不更新这个字段
  780. var updateExcelInfoParams []string
  781. if req.ExcelImage != "" {
  782. updateExcelInfoParams = []string{"ModifyTime", "ExcelName", "ExcelType", "ExcelClassifyId", "ExcelImage", "Content", "SourcesFrom"}
  783. } else {
  784. updateExcelInfoParams = []string{"ModifyTime", "ExcelName", "ExcelType", "ExcelClassifyId", "Content", "SourcesFrom"}
  785. }
  786. excelEdbMappingList := make([]*excel3.ExcelEdbMapping, 0)
  787. if len(edbInfoIdList) > 0 {
  788. for _, edbInfoId := range edbInfoIdList {
  789. excelEdbMappingList = append(excelEdbMappingList, &excel3.ExcelEdbMapping{
  790. //ExcelEdbMappingId: 0,
  791. ExcelInfoId: excelInfo.ExcelInfoId,
  792. Source: excelInfo.Source,
  793. EdbInfoId: edbInfoId,
  794. CreateTime: time.Now(),
  795. ModifyTime: time.Now(),
  796. })
  797. }
  798. }
  799. err = excel3.EditExcelInfo(excelInfo, updateExcelInfoParams, excelEdbMappingList)
  800. if err != nil {
  801. br.Msg = "保存失败"
  802. br.ErrMsg = "保存失败,Err:" + err.Error()
  803. return
  804. }
  805. // 更新excel下载地址(默认的EXCEL需要更新,自定义表格不需要更新)
  806. if excelInfo.Source == 1 {
  807. go excel2.UpdateExcelInfoFileUrl(excelInfo)
  808. }
  809. // 加入草稿
  810. {
  811. excelDraftInfo := &excel3.ExcelDraft{
  812. //ExcelDraftId: 0,
  813. ExcelId: req.ExcelInfoId,
  814. Name: req.ExcelName,
  815. Content: content,
  816. OpUserId: sysUser.AdminId,
  817. OpUserName: sysUser.RealName,
  818. CreateTime: time.Now(),
  819. }
  820. err = excel3.AddExcelDraft(excelDraftInfo)
  821. }
  822. resp := response.AddExcelInfoResp{
  823. ExcelInfoId: excelInfo.ExcelInfoId,
  824. UniqueCode: excelInfo.UniqueCode,
  825. }
  826. //删除公共图库那边的缓存
  827. _ = utils.Rc.Delete(utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL + ":" + excelInfo.UniqueCode)
  828. br.Ret = 200
  829. br.Success = true
  830. br.Msg = "修改成功"
  831. br.Data = resp
  832. br.IsAddLog = true
  833. }
  834. // Move
  835. // @Title 移动表格接口
  836. // @Description 移动表格接口
  837. // @Param request body request.MoveExcelInfoReq true "type json string"
  838. // @Success Ret=200 移动成功
  839. // @router /excel_info/move [post]
  840. func (c *ExcelInfoController) Move() {
  841. br := new(models.BaseResponse).Init()
  842. defer func() {
  843. c.Data["json"] = br
  844. c.ServeJSON()
  845. }()
  846. /*
  847. sysUser := c.SysUser
  848. if sysUser == nil {
  849. br.Msg = "请登录"
  850. br.ErrMsg = "请登录,SysUser Is Empty"
  851. br.Ret = 408
  852. return
  853. }
  854. var req request.MoveExcelInfoReq
  855. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  856. if err != nil {
  857. br.Msg = "参数解析异常!"
  858. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  859. return
  860. }
  861. if req.ExcelInfoId <= 0 {
  862. br.Msg = "参数错误"
  863. br.ErrMsg = "表格id小于等于0"
  864. br.IsSendEmail = false
  865. return
  866. }
  867. if req.ExcelClassifyId <= 0 {
  868. br.Msg = "请选择分类"
  869. br.IsSendEmail = false
  870. return
  871. }
  872. //判断分类是否存在
  873. _, err = excel3.GetExcelClassifyById(req.ExcelClassifyId)
  874. if err != nil {
  875. br.Msg = "移动失败"
  876. br.ErrMsg = "获取分类信息失败" + err.Error()
  877. if err.Error() == utils.ErrNoRow() {
  878. br.Msg = "分类已被删除,不可移动,请刷新页面"
  879. br.ErrMsg = "分类已被删除,不可移动,请刷新页面"
  880. br.IsSendEmail = false
  881. }
  882. return
  883. }
  884. // 获取表格信息
  885. excelInfo, err := excel3.GetExcelInfoById(req.ExcelInfoId)
  886. if err != nil {
  887. br.Msg = "移动失败"
  888. br.ErrMsg = "获取表格信息失败,Err:" + err.Error()
  889. return
  890. }
  891. //如果改变了分类,那么移动该表格数据
  892. if excelInfo.ExcelClassifyId != req.ExcelClassifyId {
  893. //查询需要修改的分类下是否存在同一个表格名称
  894. tmpExcelInfo, tmpErr := excel3.GetExcelInfoByClassifyIdAndName(req.ExcelClassifyId, excelInfo.ExcelName)
  895. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  896. br.Msg = "移动失败"
  897. br.ErrMsg = "移动失败,Err:" + tmpErr.Error()
  898. return
  899. }
  900. if tmpExcelInfo != nil {
  901. br.Msg = "移动失败,同一个分类下表格名称不允许重复"
  902. br.ErrMsg = "移动失败,同一个分类下表格名称不允许重复"
  903. br.IsSendEmail = false
  904. return
  905. }
  906. // 变更表格的所属分类
  907. excelInfo.ExcelClassifyId = req.ExcelClassifyId
  908. err = excelInfo.Update([]string{"ExcelClassifyId"})
  909. if err != nil {
  910. br.Msg = "移动失败"
  911. br.ErrMsg = "移动失败,Err:" + err.Error()
  912. return
  913. }
  914. }
  915. //移动排序
  916. updateCol := make([]string, 0)
  917. //如果有传入 上一个兄弟节点分类id
  918. if req.PrevExcelInfoId > 0 {
  919. prevExcelInfo, err := excel3.GetExcelInfoById(req.PrevExcelInfoId)
  920. if err != nil {
  921. br.Msg = "移动失败"
  922. br.ErrMsg = "获取上一个兄弟节点分类信息失败,Err:" + err.Error()
  923. return
  924. }
  925. //如果是移动在两个兄弟节点之间
  926. if req.NextExcelInfoId > 0 {
  927. //下一个兄弟节点
  928. nextExcelInfo, err := excel3.GetExcelInfoById(req.NextExcelInfoId)
  929. if err != nil {
  930. br.Msg = "移动失败"
  931. br.ErrMsg = "获取下一个兄弟节点分类信息失败,Err:" + err.Error()
  932. return
  933. }
  934. //如果上一个兄弟与下一个兄弟的排序权重是一致的,那么需要将下一个兄弟(以及下个兄弟的同样排序权重)的排序权重+2,自己变成上一个兄弟的排序权重+1
  935. if prevExcelInfo.Sort == nextExcelInfo.Sort || prevExcelInfo.Sort == excelInfo.Sort {
  936. //变更兄弟节点的排序
  937. updateSortStr := `sort + 2`
  938. _ = excel3.UpdateExcelInfoSortByClassifyId(prevExcelInfo.ExcelClassifyId, prevExcelInfo.Sort, prevExcelInfo.ExcelInfoId, updateSortStr)
  939. } else {
  940. //如果下一个兄弟的排序权重正好是上个兄弟节点下一层,那么需要再加一层了
  941. if nextExcelInfo.Sort-prevExcelInfo.Sort == 1 {
  942. //变更兄弟节点的排序
  943. updateSortStr := `sort + 1`
  944. _ = excel3.UpdateExcelInfoSortByClassifyId(prevExcelInfo.ExcelClassifyId, prevExcelInfo.Sort, prevExcelInfo.ExcelInfoId, updateSortStr, source)
  945. }
  946. }
  947. }
  948. // 变更表格在当前分类下的排序
  949. excelInfo.Sort = prevExcelInfo.Sort + 1
  950. excelInfo.ModifyTime = time.Now()
  951. updateCol = append(updateCol, "Sort", "ModifyTime")
  952. } else {
  953. firstClassify, err := excel3.GetFirstExcelInfoByClassifyId(req.ExcelClassifyId)
  954. if err != nil && err.Error() != utils.ErrNoRow() {
  955. br.Msg = "移动失败"
  956. br.ErrMsg = "获取获取当前父级分类下的排序第一条的分类信息失败,Err:" + err.Error()
  957. return
  958. }
  959. //如果该分类下存在其他分类,且第一个其他分类的排序等于0,那么需要调整排序
  960. if firstClassify != nil && firstClassify.Sort == 0 {
  961. updateSortStr := ` sort + 1 `
  962. _ = excel3.UpdateExcelInfoSortByClassifyId(firstClassify.ExcelClassifyId, 0, firstClassify.ExcelInfoId+1, updateSortStr)
  963. }
  964. // 变更表格在当前分类下的排序
  965. excelInfo.Sort = 0 //那就是排在第一位
  966. excelInfo.ModifyTime = time.Now()
  967. updateCol = append(updateCol, "Sort", "ModifyTime")
  968. }
  969. //更新
  970. if len(updateCol) > 0 {
  971. err = excelInfo.Update(updateCol)
  972. if err != nil {
  973. br.Msg = "移动失败"
  974. br.ErrMsg = "修改失败,Err:" + err.Error()
  975. return
  976. }
  977. }
  978. if err != nil {
  979. br.Msg = "移动失败"
  980. br.ErrMsg = "修改失败,Err:" + err.Error()
  981. return
  982. }
  983. //新增操作日志
  984. //{
  985. // ExcelLog := new(data_manage.ExcelInfoLog)
  986. // ExcelLog.ExcelName = ExcelInfo.ExcelName
  987. // ExcelLog.ExcelInfoId = req.ExcelInfoId
  988. // ExcelLog.ExcelClassifyId = ExcelInfo.ExcelClassifyId
  989. // ExcelLog.SysUserId = sysUser.AdminId
  990. // ExcelLog.SysUserRealName = sysUser.RealName
  991. // ExcelLog.UniqueCode = ExcelInfo.UniqueCode
  992. // ExcelLog.CreateTime = time.Now()
  993. // ExcelLog.Content = string(c.Ctx.Input.RequestBody)
  994. // ExcelLog.Status = "移动表格"
  995. // ExcelLog.Method = c.Ctx.Input.URL()
  996. // go data_manage.AddExcelInfoLog(ExcelLog)
  997. //}
  998. */
  999. br.Ret = 200
  1000. br.Success = true
  1001. br.IsAddLog = true
  1002. br.Msg = "移动成功"
  1003. }
  1004. // Delete
  1005. // @Title 删除表格接口
  1006. // @Description 删除表格接口
  1007. // @Param request body request.DeleteExcelInfoReq true "type json string"
  1008. // @Success Ret=200 删除成功
  1009. // @router /excel_info/delete [post]
  1010. func (c *ExcelInfoController) Delete() {
  1011. br := new(models.BaseResponse).Init()
  1012. defer func() {
  1013. c.Data["json"] = br
  1014. c.ServeJSON()
  1015. }()
  1016. resp := response.AddExcelInfoResp{}
  1017. sysUser := c.SysUser
  1018. if sysUser == nil {
  1019. br.Msg = "请登录"
  1020. br.ErrMsg = "请登录,SysUser Is Empty"
  1021. br.Ret = 408
  1022. return
  1023. }
  1024. var req request.DeleteExcelInfoReq
  1025. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  1026. if err != nil {
  1027. br.Msg = "参数解析异常!"
  1028. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1029. return
  1030. }
  1031. if req.ExcelInfoId <= 0 {
  1032. br.Msg = "参数错误"
  1033. br.ErrMsg = "表格id小于等于0"
  1034. br.IsSendEmail = false
  1035. return
  1036. }
  1037. // 获取表格信息
  1038. excelInfo, err := excel3.GetExcelInfoById(req.ExcelInfoId)
  1039. if err != nil {
  1040. br.Msg = "删除失败"
  1041. br.ErrMsg = "获取表格信息失败,Err:" + err.Error()
  1042. return
  1043. }
  1044. // 删除excel
  1045. err, errMsg, isSendEmail := excel2.Delete(excelInfo, sysUser)
  1046. if err != nil {
  1047. br.Msg = "删除失败"
  1048. if errMsg != `` {
  1049. br.Msg = errMsg
  1050. }
  1051. br.ErrMsg = "删除失败,Err:" + err.Error()
  1052. br.IsSendEmail = isSendEmail
  1053. return
  1054. }
  1055. // 返回下一个表格的信息
  1056. {
  1057. var nextItem *excel3.ExcelInfo
  1058. var condition string
  1059. var pars []interface{}
  1060. condition += " AND excel_classify_id=? "
  1061. pars = append(pars, excelInfo.ExcelClassifyId)
  1062. condition += " AND sort>=? "
  1063. pars = append(pars, excelInfo.Sort)
  1064. nextItem, err = excel3.GetNextExcelInfoByCondition(condition, pars)
  1065. if err != nil && err.Error() != utils.ErrNoRow() {
  1066. br.Msg = "删除失败"
  1067. br.ErrMsg = "获取下一级表格信息失败,Err:" + err.Error()
  1068. return
  1069. }
  1070. // 如果没找到,那么查找下一个分类的第一个表格
  1071. if nextItem == nil {
  1072. currClassifyInfo, err := excel3.GetExcelClassifyById(excelInfo.ExcelClassifyId)
  1073. if err != nil && err.Error() != utils.ErrNoRow() {
  1074. br.Msg = "删除失败"
  1075. br.ErrMsg = "获取当前表格分类信息失败,Err:" + err.Error()
  1076. return
  1077. }
  1078. //condition += " AND level=3 "
  1079. //pars = append(pars, chartInfo.ExcelClassifyId)
  1080. //var condition string
  1081. //var pars []interface{}
  1082. //condition += " AND sort>=? ORDER BY excel_classify_id ASC LIMIT 1 "
  1083. //pars = append(pars, currClassifyInfo.Sort)
  1084. //
  1085. //classifyItem, err := data_manage.GetNextExcelClassifyByCondition(condition, pars)
  1086. //if err != nil && err.Error() != utils.ErrNoRow() {
  1087. // br.Msg = "删除失败"
  1088. // br.ErrMsg = "获取下一级表格分类信息失败,Err:" + err.Error()
  1089. // return
  1090. //}
  1091. //if classifyItem != nil {
  1092. // nextItem, err = data_manage.GetNextExcelInfo(excelInfo.ExcelClassifyId,)
  1093. // if err != nil && err.Error() != utils.ErrNoRow() {
  1094. // br.Msg = "删除失败"
  1095. // br.ErrMsg = "获取下一级表格信息失败,Err:" + err.Error()
  1096. // return
  1097. // }
  1098. //}
  1099. nextItem, err = excel3.GetNextExcelInfo(excelInfo.ExcelClassifyId, currClassifyInfo.Sort, currClassifyInfo.Source)
  1100. if err != nil && err.Error() != utils.ErrNoRow() {
  1101. br.Msg = "删除失败"
  1102. br.ErrMsg = "获取下一级表格信息失败,Err:" + err.Error()
  1103. return
  1104. }
  1105. }
  1106. // 如果找到下一个表格了,那么就返回
  1107. if nextItem != nil {
  1108. resp = response.AddExcelInfoResp{
  1109. ExcelInfoId: nextItem.ExcelInfoId,
  1110. UniqueCode: nextItem.UniqueCode,
  1111. }
  1112. }
  1113. }
  1114. //删除公共图库那边的缓存
  1115. _ = utils.Rc.Delete(utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL)
  1116. br.Ret = 200
  1117. br.Success = true
  1118. br.IsAddLog = true
  1119. br.Data = resp
  1120. br.Msg = "删除成功"
  1121. }
  1122. // AddDraft
  1123. // @Title 新增表格草稿接口
  1124. // @Description 新增表格草稿接口
  1125. // @Param request body request.AddExcelDraftReq true "type json string"
  1126. // @Success Ret=200 添加成功
  1127. // @router /excel_info/draft/add [post]
  1128. func (c *ExcelInfoController) AddDraft() {
  1129. br := new(models.BaseResponse).Init()
  1130. defer func() {
  1131. c.Data["json"] = br
  1132. c.ServeJSON()
  1133. }()
  1134. sysUser := c.SysUser
  1135. if sysUser == nil {
  1136. br.Msg = "请登录"
  1137. br.ErrMsg = "请登录,SysUser Is Empty"
  1138. br.Ret = 408
  1139. return
  1140. }
  1141. var req request.AddExcelDraftReq
  1142. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  1143. if err != nil {
  1144. br.Msg = "参数解析异常!"
  1145. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1146. return
  1147. }
  1148. req.ExcelName = strings.Trim(req.ExcelName, " ")
  1149. if req.ExcelName == "" {
  1150. br.Msg = "请填写表格名称!"
  1151. br.IsSendEmail = false
  1152. return
  1153. }
  1154. if req.ExcelClassifyId <= 0 {
  1155. br.Msg = "分类参数错误!"
  1156. br.IsSendEmail = false
  1157. return
  1158. }
  1159. excelClassify, err := excel3.GetExcelClassifyById(req.ExcelClassifyId)
  1160. if err != nil {
  1161. if err.Error() == utils.ErrNoRow() {
  1162. br.Msg = "分类不存在"
  1163. br.ErrMsg = "分类不存在"
  1164. br.IsSendEmail = false
  1165. return
  1166. }
  1167. br.Msg = "获取分类信息失败"
  1168. br.ErrMsg = "获取分类信息失败,Err:" + err.Error()
  1169. return
  1170. }
  1171. if excelClassify == nil {
  1172. br.Msg = "分类不存在"
  1173. br.ErrMsg = "分类不存在"
  1174. br.IsSendEmail = false
  1175. return
  1176. }
  1177. var condition string
  1178. var pars []interface{}
  1179. condition += " AND excel_classify_id=? "
  1180. pars = append(pars, req.ExcelClassifyId)
  1181. condition += " AND excel_name=? "
  1182. pars = append(pars, req.ExcelName)
  1183. // 获取分类下是否存在该表格名称
  1184. _, err = excel3.GetExcelInfoById(req.ExcelInfoId)
  1185. if err != nil {
  1186. br.Msg = "判断表格名称是否存在失败"
  1187. br.ErrMsg = "判断表格名称是否存在失败,Err:" + err.Error()
  1188. return
  1189. }
  1190. excelDraftInfo := &excel3.ExcelDraft{
  1191. //ExcelDraftId: 0,
  1192. ExcelId: req.ExcelInfoId,
  1193. Name: req.ExcelName,
  1194. Content: req.Content,
  1195. OpUserId: sysUser.AdminId,
  1196. OpUserName: sysUser.RealName,
  1197. CreateTime: time.Now(),
  1198. }
  1199. err = excel3.AddExcelDraft(excelDraftInfo)
  1200. if err != nil {
  1201. br.Msg = "保存失败"
  1202. br.ErrMsg = "保存失败,Err:" + err.Error()
  1203. return
  1204. }
  1205. _, err = services.UpdateExcelEditMark(req.ExcelInfoId, sysUser.AdminId, 1, sysUser.RealName)
  1206. if err != nil {
  1207. br.Msg = "查询标记状态失败"
  1208. br.ErrMsg = "查询标记状态失败,Err:" + err.Error()
  1209. return
  1210. }
  1211. br.Ret = 200
  1212. br.Success = true
  1213. br.Msg = "添加成功"
  1214. }
  1215. // GetExcelTableData
  1216. // @Title 获取excel表格的table数据
  1217. // @Description 获取excel表格的table数据接口
  1218. // @Param UniqueCode query string true "表格code"
  1219. // @Param FromScene query int true "场景来源,1:智能研报,2:研报列表;3:英文研报;4:中文PPT;5:英文PPT"
  1220. // @Success 200 {object} response.ExcelTableDetailResp
  1221. // @router /excel_info/table_data [get]
  1222. func (c *ExcelInfoController) GetExcelTableData() {
  1223. br := new(models.BaseResponse).Init()
  1224. defer func() {
  1225. c.Data["json"] = br
  1226. c.ServeJSON()
  1227. }()
  1228. sysUser := c.SysUser
  1229. if sysUser == nil {
  1230. br.Msg = "请登录"
  1231. br.ErrMsg = "请登录,SysUser Is Empty"
  1232. br.Ret = 408
  1233. return
  1234. }
  1235. uniqueCode := c.GetString("UniqueCode")
  1236. fromScene, _ := c.GetInt("FromScene", 0)
  1237. var err error
  1238. if uniqueCode == `` {
  1239. br.Msg = "请选择表格"
  1240. br.ErrMsg = "UniqueCode未传"
  1241. br.IsSendEmail = false
  1242. return
  1243. }
  1244. //获取eta表格信息
  1245. excelInfo, err := excel3.GetExcelInfoByUniqueCode(uniqueCode)
  1246. if err != nil {
  1247. br.Msg = "获取失败"
  1248. br.ErrMsg = "获取ETA表格信息失败,Err:" + err.Error()
  1249. if err.Error() == utils.ErrNoRow() {
  1250. br.Msg = "ETA表格被删除,请刷新页面"
  1251. br.ErrMsg = "ETA表格被删除,请刷新页面,Err:" + err.Error()
  1252. br.IsSendEmail = false
  1253. }
  1254. return
  1255. }
  1256. var tableData excel.TableData
  1257. var excelSource, excelSourceEn string
  1258. switch excelInfo.Source {
  1259. case utils.EXCEL_DEFAULT:
  1260. luckySheetData, err := excel.GetLuckySheetData(excelInfo.Content)
  1261. if err != nil {
  1262. br.Msg = "获取失败"
  1263. br.ErrMsg = "获取excel数据失败,Err:" + err.Error()
  1264. return
  1265. }
  1266. tableData, err = luckySheetData.GetTableDataByLuckySheetDataStr(true)
  1267. if err != nil {
  1268. br.Msg = "获取失败"
  1269. br.ErrMsg = "转换成table失败,Err:" + err.Error()
  1270. return
  1271. }
  1272. case utils.TIME_TABLE:
  1273. var tableDataConfig excel2.TableDataConfig
  1274. err = json.Unmarshal([]byte(excelInfo.Content), &tableDataConfig)
  1275. if err != nil {
  1276. br.Msg = "获取失败"
  1277. br.ErrMsg = "表格json转结构体失败,Err:" + err.Error()
  1278. return
  1279. }
  1280. result, err := excel2.GetDataByTableDataConfig(tableDataConfig)
  1281. if err != nil {
  1282. br.Msg = "获取失败"
  1283. br.ErrMsg = "获取最新的表格数据失败,Err:" + err.Error()
  1284. return
  1285. }
  1286. tableData, err = excel.GetTableDataByCustomData(excelInfo.ExcelType, result)
  1287. if err != nil {
  1288. br.Msg = "获取失败"
  1289. br.ErrMsg = "转换成table失败,Err:" + err.Error()
  1290. return
  1291. }
  1292. sourceNameList, sourceNameEnList, err := excel2.GetEdbSourceByEdbInfoIdList(result.EdbInfoIdList)
  1293. if err != nil {
  1294. br.Msg = "自定义表格数据获取失败"
  1295. br.ErrMsg = "自定义表格数据获取失败,Err:" + err.Error()
  1296. return
  1297. }
  1298. excelSource = strings.Join(sourceNameList, ",")
  1299. excelSourceEn = strings.Join(sourceNameEnList, ",")
  1300. case utils.MIXED_TABLE:
  1301. var result request.MixedTableReq
  1302. err = json.Unmarshal([]byte(excelInfo.Content), &result)
  1303. if err != nil {
  1304. br.Msg = "获取失败"
  1305. br.ErrMsg = "表格json转结构体失败,Err:" + err.Error()
  1306. return
  1307. }
  1308. newResult, err, errMsg := excel2.GetMixedTableCellData(result, c.Lang)
  1309. if err != nil {
  1310. br.Msg = "获取失败"
  1311. if errMsg != `` {
  1312. br.Msg = errMsg
  1313. }
  1314. br.ErrMsg = "获取最新的数据失败,Err:" + err.Error()
  1315. return
  1316. }
  1317. tableData, err = excel.GetTableDataByMixedTableData(newResult, true)
  1318. if err != nil {
  1319. br.Msg = "获取失败"
  1320. br.ErrMsg = "转换成table失败,Err:" + err.Error()
  1321. return
  1322. }
  1323. edbInfoIds := make([]int, 0)
  1324. edbInfoIdExist := make(map[int]bool)
  1325. if len(newResult) > 0 {
  1326. for _, t := range newResult {
  1327. for _, v := range t {
  1328. if v.EdbInfoId > 0 && !edbInfoIdExist[v.EdbInfoId] {
  1329. edbInfoIdExist[v.EdbInfoId] = true
  1330. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  1331. }
  1332. }
  1333. }
  1334. }
  1335. sourceNameList, sourceNameEnList, err := excel2.GetEdbSourceByEdbInfoIdList(edbInfoIds)
  1336. if err != nil {
  1337. br.Msg = "自定义表格数据获取失败"
  1338. br.ErrMsg = "自定义表格数据获取失败,Err:" + err.Error()
  1339. return
  1340. }
  1341. excelSource = strings.Join(sourceNameList, ",")
  1342. excelSourceEn = strings.Join(sourceNameEnList, ",")
  1343. }
  1344. tableData = excel.HandleTableCell(tableData)
  1345. config := response.ExcelTableDetailConfigResp{
  1346. FontSize: 9,
  1347. }
  1348. // 获取配置的字体大小
  1349. confName := models.FromSceneMap[fromScene]
  1350. if confName != `` {
  1351. busConf, err := models.GetBusinessConfByKey(confName)
  1352. if err == nil {
  1353. sizeDeci, err := decimal.NewFromString(busConf.ConfVal)
  1354. if err == nil {
  1355. config.FontSize = int(sizeDeci.IntPart())
  1356. }
  1357. }
  1358. }
  1359. resp := response.ExcelTableDetailResp{
  1360. UniqueCode: excelInfo.UniqueCode,
  1361. ExcelImage: excelInfo.ExcelImage,
  1362. ExcelName: excelInfo.ExcelName,
  1363. TableInfo: tableData,
  1364. Config: config,
  1365. SourcesFrom: excelInfo.SourcesFrom,
  1366. ExcelSource: excelSource,
  1367. ExcelSourceEn: excelSourceEn,
  1368. }
  1369. br.Ret = 200
  1370. br.Success = true
  1371. br.Msg = "获取成功"
  1372. br.Data = resp
  1373. }
  1374. //func init() {
  1375. //
  1376. // excelInfo, err := data_manage.GetExcelInfoById(52)
  1377. // if err != nil {
  1378. // fmt.Println("err:", err)
  1379. // return
  1380. // }
  1381. //
  1382. // luckySheetData, err := excel.GetLuckySheetData(excelInfo.Content)
  1383. // if err != nil {
  1384. // fmt.Println("err:", err)
  1385. // return
  1386. // }
  1387. // //_, err = luckySheetData.GetTableDataByLuckySheetDataStr()
  1388. // downloadFilePath, err := luckySheetData.ToExcel()
  1389. // if err != nil {
  1390. // fmt.Println("err:", err)
  1391. // return
  1392. // }
  1393. // fmt.Println(downloadFilePath)
  1394. //
  1395. // //fmt.Println(excel.GetColumnName(988, 283))
  1396. //}
  1397. // Calculate
  1398. // @Title 公式计算接口
  1399. // @Description 公式计算接口
  1400. // @Param request body request.CalculateReq true "type json string"
  1401. // @router /excel_info/table/calculate [post]
  1402. func (c *ExcelInfoController) Calculate() {
  1403. br := new(models.BaseResponse).Init()
  1404. defer func() {
  1405. c.Data["json"] = br
  1406. c.ServeJSON()
  1407. }()
  1408. sysUser := c.SysUser
  1409. if sysUser == nil {
  1410. br.Msg = "请登录"
  1411. br.ErrMsg = "请登录,SysUser Is Empty"
  1412. br.Ret = 408
  1413. return
  1414. }
  1415. var req request.CalculateReq
  1416. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  1417. if err != nil {
  1418. br.Msg = "参数解析异常!"
  1419. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1420. return
  1421. }
  1422. if req.CalculateFormula == "" {
  1423. br.Msg = "请填写公式"
  1424. return
  1425. }
  1426. calculateFormula := req.CalculateFormula
  1427. calculateFormula = strings.TrimPrefix(calculateFormula, "=")
  1428. calculateFormula = strings.Replace(calculateFormula, "(", "(", -1)
  1429. calculateFormula = strings.Replace(calculateFormula, ")", ")", -1)
  1430. calculateFormula = strings.Replace(calculateFormula, ",", ",", -1)
  1431. calculateFormula = strings.Replace(calculateFormula, "。", ".", -1)
  1432. calculateFormula = strings.Replace(calculateFormula, "%", "*0.01", -1)
  1433. formulaFormStr := utils.ReplaceFormula(req.TagMap, calculateFormula)
  1434. //计算公式异常,那么就移除该指标
  1435. if formulaFormStr == `` {
  1436. //removeDateList = append(removeDateList, sk)
  1437. //fmt.Println("异常了")
  1438. br.Msg = "公式异常"
  1439. br.ErrMsg = "公式异常"
  1440. return
  1441. }
  1442. expression := formula.NewExpression(formulaFormStr)
  1443. calResult, err := expression.Evaluate()
  1444. if err != nil {
  1445. br.Msg = "计算失败"
  1446. br.ErrMsg = "计算失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr
  1447. // 分母为0的报错
  1448. if strings.Contains(err.Error(), "divide by zero") {
  1449. br.Msg = "分母不能为0"
  1450. br.ErrMsg = "分母不能为空,计算公式:" + formulaFormStr
  1451. br.IsSendEmail = false
  1452. }
  1453. return
  1454. }
  1455. // 如果计算结果是NAN,那么就提示报错
  1456. if calResult.IsNan() {
  1457. br.Msg = "计算失败"
  1458. br.ErrMsg = "计算失败:计算结果是:NAN;formulaStr:" + formulaFormStr
  1459. return
  1460. }
  1461. // 计算结果格式化
  1462. calFloat, e := calResult.Float64()
  1463. if e != nil {
  1464. br.Msg = "计算失败"
  1465. br.ErrMsg = "计算失败, Err: " + e.Error()
  1466. return
  1467. }
  1468. calVal := utils.FormatTableDataShowValue(calFloat)
  1469. //calVal := calResult.String()
  1470. //if err != nil {
  1471. // br.Msg = "计算失败"
  1472. // br.ErrMsg = "计算失败,结果转 string 失败:Err:" + err.Error() + ";formulaStr:" + formulaFormStr
  1473. // return
  1474. //}
  1475. br.Ret = 200
  1476. br.Success = true
  1477. br.Msg = "计算成功"
  1478. br.Data = calVal
  1479. }
  1480. // GetFirstEdbData
  1481. // @Title 获取未来日期数据接口
  1482. // @Description 获取未来日期数据接口
  1483. // @Param EdbInfoId query int true "指标id"
  1484. // @Param Num query int true "默认期数"
  1485. // @Param SortType query string true "如何排序,是正序还是倒序,枚举值:`asc 正序`,`desc 倒序`,不传默认倒序"
  1486. // @router /excel_info/table/first_edb_data_list [get]
  1487. func (c *ExcelInfoController) GetFirstEdbData() {
  1488. br := new(models.BaseResponse).Init()
  1489. defer func() {
  1490. c.Data["json"] = br
  1491. c.ServeJSON()
  1492. }()
  1493. sysUser := c.SysUser
  1494. if sysUser == nil {
  1495. br.Msg = "请登录"
  1496. br.ErrMsg = "请登录,SysUser Is Empty"
  1497. br.Ret = 408
  1498. return
  1499. }
  1500. edbInfoId, _ := c.GetInt("EdbInfoId")
  1501. num, _ := c.GetInt("Num")
  1502. sortType := c.GetString("SortType")
  1503. if edbInfoId <= 0 {
  1504. br.Msg = "请选择指标"
  1505. br.IsSendEmail = false
  1506. return
  1507. }
  1508. if num <= 0 {
  1509. br.Msg = "期数必须大于0"
  1510. br.IsSendEmail = false
  1511. return
  1512. }
  1513. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  1514. if err != nil {
  1515. br.Msg = "获取失败"
  1516. br.ErrMsg = fmt.Sprint("获取指标信息失败,Err:", err.Error())
  1517. return
  1518. }
  1519. dataList, err := excel2.GetFirstEdbDataList(edbInfo, num, []string{})
  1520. if err != nil {
  1521. br.Msg = "获取失败"
  1522. br.ErrMsg = fmt.Sprint("获取失败,Err:", err.Error())
  1523. return
  1524. }
  1525. // 默认是倒序返回的数据,如果要正序的话,那么翻转下就好了
  1526. if sortType == "asc" {
  1527. for i, j := 0, len(dataList)-1; i < j; i, j = i+1, j-1 {
  1528. dataList[i], dataList[j] = dataList[j], dataList[i]
  1529. }
  1530. }
  1531. sourceNameList, sourceNameEnList, err := excel2.GetEdbSourceByEdbInfoIdList([]int{edbInfoId})
  1532. if err != nil {
  1533. br.Msg = "自定义表格数据获取失败"
  1534. br.ErrMsg = "自定义表格数据获取失败,Err:" + err.Error()
  1535. return
  1536. }
  1537. excelSource := strings.Join(sourceNameList, ",")
  1538. excelSourceEn := strings.Join(sourceNameEnList, ",")
  1539. br.Ret = 200
  1540. br.Success = true
  1541. br.Msg = "获取成功"
  1542. br.Data = response.TableDataItem{
  1543. EdbInfoId: edbInfoId,
  1544. Data: dataList,
  1545. ExcelSource: excelSource,
  1546. ExcelSourceEn: excelSourceEn,
  1547. }
  1548. }
  1549. // GetOtherEdbData
  1550. // @Title 获取指定日期数据接口
  1551. // @Description 获取未来日期数据接口
  1552. // @Param request body request.GetOtherEdbDateDataReq true "type json string"
  1553. // @router /excel_info/table/other_edb_data_list [post]
  1554. func (c *ExcelInfoController) GetOtherEdbData() {
  1555. br := new(models.BaseResponse).Init()
  1556. defer func() {
  1557. c.Data["json"] = br
  1558. c.ServeJSON()
  1559. }()
  1560. sysUser := c.SysUser
  1561. if sysUser == nil {
  1562. br.Msg = "请登录"
  1563. br.ErrMsg = "请登录,SysUser Is Empty"
  1564. br.Ret = 408
  1565. return
  1566. }
  1567. var req request.GetOtherEdbDateDataReq
  1568. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  1569. if err != nil {
  1570. br.Msg = "参数解析异常!"
  1571. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1572. return
  1573. }
  1574. if req.EdbInfoId <= 0 {
  1575. br.Msg = "请选择指标"
  1576. br.IsSendEmail = false
  1577. return
  1578. }
  1579. if len(req.DateList) <= 0 {
  1580. br.Msg = "请选择日期"
  1581. br.IsSendEmail = false
  1582. return
  1583. }
  1584. edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  1585. if err != nil {
  1586. br.Msg = "获取失败"
  1587. br.ErrMsg = fmt.Sprint("获取指标信息失败,Err:", err.Error())
  1588. return
  1589. }
  1590. dataList, err := excel2.GetOtherEdbDataList(edbInfo, req.DateList)
  1591. if err != nil {
  1592. br.Msg = "获取失败"
  1593. br.ErrMsg = fmt.Sprint("获取失败,Err:", err.Error())
  1594. return
  1595. }
  1596. // 默认是倒序返回的数据,如果要正序的话,那么翻转下就好了
  1597. if req.SortType == "asc" {
  1598. for i, j := 0, len(dataList)-1; i < j; i, j = i+1, j-1 {
  1599. dataList[i], dataList[j] = dataList[j], dataList[i]
  1600. }
  1601. }
  1602. sourceNameList, sourceNameEnList, err := excel2.GetEdbSourceByEdbInfoIdList([]int{req.EdbInfoId})
  1603. if err != nil {
  1604. br.Msg = "自定义表格数据获取失败"
  1605. br.ErrMsg = "自定义表格数据获取失败,Err:" + err.Error()
  1606. return
  1607. }
  1608. excelSource := strings.Join(sourceNameList, ",")
  1609. excelSourceEn := strings.Join(sourceNameEnList, ",")
  1610. br.Ret = 200
  1611. br.Success = true
  1612. br.Msg = "获取成功"
  1613. br.Data = response.TableDataItem{
  1614. EdbInfoId: req.EdbInfoId,
  1615. Data: dataList,
  1616. ExcelSource: excelSource,
  1617. ExcelSourceEn: excelSourceEn,
  1618. }
  1619. }
  1620. // GetFutureDateData
  1621. // @Title 获取未来日期数据接口
  1622. // @Description 获取未来日期数据接口
  1623. // @Param request body request.GetFutureDateDataReq true "type json string"
  1624. // @router /excel_info/table/future_date_list [post]
  1625. func (c *ExcelInfoController) GetFutureDateData() {
  1626. br := new(models.BaseResponse).Init()
  1627. defer func() {
  1628. c.Data["json"] = br
  1629. c.ServeJSON()
  1630. }()
  1631. sysUser := c.SysUser
  1632. if sysUser == nil {
  1633. br.Msg = "请登录"
  1634. br.ErrMsg = "请登录,SysUser Is Empty"
  1635. br.Ret = 408
  1636. return
  1637. }
  1638. var req request.GetFutureDateDataReq
  1639. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  1640. if err != nil {
  1641. br.Msg = "参数解析异常!"
  1642. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1643. return
  1644. }
  1645. if req.StartDate == `` {
  1646. br.Msg = "开始日期未传"
  1647. br.IsSendEmail = false
  1648. return
  1649. }
  1650. startDate, err := time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local)
  1651. if err != nil {
  1652. br.Msg = "开始日期异常"
  1653. br.ErrMsg = fmt.Sprint("开始日期异常,startDate:", req.StartDate, ";err:", err.Error())
  1654. return
  1655. }
  1656. if len(req.EdbInfoIdList) <= 0 {
  1657. br.Msg = "请选择指标"
  1658. br.IsSendEmail = false
  1659. return
  1660. }
  1661. baseEdbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoIdList[0])
  1662. if err != nil {
  1663. br.Msg = "查找指标失败"
  1664. br.Msg = "查找指标失败,err:" + err.Error()
  1665. return
  1666. }
  1667. // 第一个指标的数据列表
  1668. var firstEdbDataList []*data_manage.EdbDataList
  1669. {
  1670. switch baseEdbInfo.EdbInfoType {
  1671. case 0:
  1672. firstEdbDataList, err = data_manage.GetEdbDataList(baseEdbInfo.Source, baseEdbInfo.SubSource, baseEdbInfo.EdbInfoId, req.StartDate, ``)
  1673. case 1:
  1674. _, firstEdbDataList, _, _, err, _ = data.GetPredictDataListByPredictEdbInfoId(baseEdbInfo.EdbInfoId, req.StartDate, ``, false)
  1675. default:
  1676. br.Msg = "指标类型异常"
  1677. br.ErrMsg = fmt.Sprint("获取失败,指标类型异常", baseEdbInfo.EdbInfoType)
  1678. br.IsSendEmail = false
  1679. return
  1680. }
  1681. }
  1682. dateList := make([]time.Time, 0)
  1683. if req.DateType == 1 {
  1684. num := req.Num
  1685. i := 0 // 数据中有的日期数
  1686. for _, v := range firstEdbDataList {
  1687. tmpDate, err := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  1688. if err != nil {
  1689. br.Msg = "结束日期异常"
  1690. br.ErrMsg = fmt.Sprint("结束日期异常,startDate:", req.EndDate, ";err:", err.Error())
  1691. return
  1692. }
  1693. if tmpDate.After(startDate) {
  1694. dateList = append(dateList, tmpDate)
  1695. startDate = tmpDate
  1696. i++
  1697. }
  1698. if i >= num {
  1699. break
  1700. }
  1701. }
  1702. num = num - i
  1703. if num > 0 {
  1704. tmpDateList := utils.GetPredictEdbDayListByNum(startDate, num, baseEdbInfo.Frequency)
  1705. dateList = append(dateList, tmpDateList...)
  1706. }
  1707. } else {
  1708. if req.EndDate == `` {
  1709. br.Msg = "结束日期未传"
  1710. br.IsSendEmail = false
  1711. return
  1712. }
  1713. endDate, err := time.ParseInLocation(utils.FormatDate, req.EndDate, time.Local)
  1714. if err != nil {
  1715. br.Msg = "结束日期异常"
  1716. br.ErrMsg = fmt.Sprint("结束日期异常,startDate:", req.EndDate, ";err:", err.Error())
  1717. return
  1718. }
  1719. for _, v := range firstEdbDataList {
  1720. tmpDate, err := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  1721. if err != nil {
  1722. br.Msg = "结束日期异常"
  1723. br.ErrMsg = fmt.Sprint("结束日期异常,startDate:", req.EndDate, ";err:", err.Error())
  1724. return
  1725. }
  1726. if tmpDate.Equal(endDate) || (tmpDate.Before(endDate) && tmpDate.After(startDate)) {
  1727. dateList = append(dateList, tmpDate)
  1728. startDate = tmpDate
  1729. }
  1730. }
  1731. if startDate.Before(endDate) {
  1732. tmpDateList := utils.GetPredictEdbDayListByEndDate(startDate, endDate, baseEdbInfo.Frequency)
  1733. dateList = append(dateList, tmpDateList...)
  1734. }
  1735. }
  1736. // 获取所有的指标信息
  1737. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  1738. {
  1739. edbInfoList, err := data_manage.GetEdbInfoByIdList(req.EdbInfoIdList)
  1740. if err != nil {
  1741. br.Msg = "获取指标信息失败"
  1742. br.ErrMsg = "获取指标信息失败,err:" + err.Error()
  1743. return
  1744. }
  1745. for _, v := range edbInfoList {
  1746. edbInfoMap[v.EdbInfoId] = v
  1747. }
  1748. }
  1749. result := make([]response.TableDataItem, 0)
  1750. dateStrList := make([]string, 0)
  1751. lenDate := len(dateList)
  1752. if lenDate > 0 {
  1753. for i := lenDate - 1; i >= 0; i-- {
  1754. dateStrList = append(dateStrList, dateList[i].Format(utils.FormatDate))
  1755. }
  1756. for _, v := range req.EdbInfoIdList {
  1757. tmpEdbInfo, ok := edbInfoMap[v]
  1758. if !ok {
  1759. br.Msg = "获取指标信息失败"
  1760. br.ErrMsg = "获取指标信息失败,err:" + err.Error()
  1761. return
  1762. }
  1763. dataList, err := excel2.GetOtherEdbDataList(tmpEdbInfo, dateStrList)
  1764. if err != nil {
  1765. br.Msg = "获取失败"
  1766. br.ErrMsg = fmt.Sprint("获取失败,Err:", err.Error())
  1767. return
  1768. }
  1769. result = append(result, response.TableDataItem{
  1770. EdbInfoId: v,
  1771. Data: dataList,
  1772. })
  1773. }
  1774. }
  1775. br.Ret = 200
  1776. br.Success = true
  1777. br.Msg = "获取成功"
  1778. br.Data = result
  1779. }
  1780. // GetHistoryDateData
  1781. // @Title 获取历史日期数据接口
  1782. // @Description 获取历史日期数据接口
  1783. // @Param request body request.GetFutureDateDataReq true "type json string"
  1784. // @router /excel_info/table/history_date_list [post]
  1785. func (c *ExcelInfoController) GetHistoryDateData() {
  1786. br := new(models.BaseResponse).Init()
  1787. defer func() {
  1788. c.Data["json"] = br
  1789. c.ServeJSON()
  1790. }()
  1791. sysUser := c.SysUser
  1792. if sysUser == nil {
  1793. br.Msg = "请登录"
  1794. br.ErrMsg = "请登录,SysUser Is Empty"
  1795. br.Ret = 408
  1796. return
  1797. }
  1798. var req request.GetHistoryDateDataReq
  1799. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  1800. if err != nil {
  1801. br.Msg = "参数解析异常!"
  1802. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1803. return
  1804. }
  1805. if req.EndDate == `` {
  1806. br.Msg = "结束日期未传"
  1807. br.IsSendEmail = false
  1808. return
  1809. }
  1810. if len(req.EdbInfoIdList) <= 0 {
  1811. br.Msg = "请选择指标"
  1812. br.IsSendEmail = false
  1813. return
  1814. }
  1815. // 获取所有的指标信息
  1816. edbInfoMap := make(map[int]*data_manage.EdbInfo)
  1817. {
  1818. edbInfoList, err := data_manage.GetEdbInfoByIdList(req.EdbInfoIdList)
  1819. if err != nil {
  1820. br.Msg = "获取指标信息失败"
  1821. br.ErrMsg = "获取指标信息失败,err:" + err.Error()
  1822. return
  1823. }
  1824. for _, v := range edbInfoList {
  1825. edbInfoMap[v.EdbInfoId] = v
  1826. }
  1827. }
  1828. result := make([]response.TableDataItem, 0)
  1829. dateStrList := make([]string, 0)
  1830. // 获取A列指标的历史数据
  1831. {
  1832. tmpEdbInfo, ok := edbInfoMap[req.EdbInfoIdList[0]]
  1833. if !ok {
  1834. br.Msg = "获取指标信息失败"
  1835. br.ErrMsg = "获取指标信息失败,err:" + err.Error()
  1836. return
  1837. }
  1838. firstDataList, err := excel2.GetFirstHistoryEdbDataList(tmpEdbInfo, req.Num, req.EndDate)
  1839. if err != nil {
  1840. br.Msg = "获取失败"
  1841. br.ErrMsg = fmt.Sprint("获取失败,Err:", err.Error())
  1842. return
  1843. }
  1844. result = append(result, response.TableDataItem{
  1845. EdbInfoId: req.EdbInfoIdList[0],
  1846. Data: firstDataList,
  1847. })
  1848. for _, v := range firstDataList {
  1849. dateStrList = append(dateStrList, v.DataTime)
  1850. }
  1851. }
  1852. if len(dateStrList) > 0 {
  1853. for k, v := range req.EdbInfoIdList {
  1854. if k == 0 {
  1855. continue
  1856. }
  1857. tmpEdbInfo, ok := edbInfoMap[v]
  1858. if !ok {
  1859. br.Msg = "获取指标信息失败"
  1860. br.ErrMsg = "获取指标信息失败,err:" + err.Error()
  1861. return
  1862. }
  1863. dataList, err := excel2.GetOtherEdbDataList(tmpEdbInfo, dateStrList)
  1864. if err != nil {
  1865. br.Msg = "获取失败"
  1866. br.ErrMsg = fmt.Sprint("获取失败,Err:", err.Error())
  1867. return
  1868. }
  1869. result = append(result, response.TableDataItem{
  1870. EdbInfoId: v,
  1871. Data: dataList,
  1872. })
  1873. }
  1874. }
  1875. br.Ret = 200
  1876. br.Success = true
  1877. br.Msg = "获取成功"
  1878. br.Data = result
  1879. }
  1880. // Refresh
  1881. // @Title 刷新
  1882. // @Description 刷新接口
  1883. // @Param ExcelInfoId query int true "表格id"
  1884. // @router /excel_info/table/refresh [get]
  1885. func (c *ExcelInfoController) Refresh() {
  1886. br := new(models.BaseResponse).Init()
  1887. defer func() {
  1888. if br.ErrMsg == "" {
  1889. br.IsSendEmail = false
  1890. }
  1891. c.Data["json"] = br
  1892. c.ServeJSON()
  1893. }()
  1894. sysUser := c.SysUser
  1895. if sysUser == nil {
  1896. br.Msg = "请登录"
  1897. br.ErrMsg = "请登录,SysUser Is Empty"
  1898. br.Ret = 408
  1899. return
  1900. }
  1901. excelInfoId, _ := c.GetInt("ExcelInfoId")
  1902. chartInfoId, _ := c.GetInt("ChartInfoId")
  1903. if excelInfoId <= 0 && chartInfoId <= 0 {
  1904. br.Msg = "请选择表格"
  1905. br.ErrMsg = "ExcelInfoId未传"
  1906. br.IsSendEmail = false
  1907. return
  1908. }
  1909. // todo 如果请求入参是chart_info_id,则需要获取excel_info_id
  1910. if chartInfoId > 0 && excelInfoId == 0 {
  1911. excelInfo, err := excel3.GetExcelInfoByChartInfoId(chartInfoId)
  1912. if err != nil {
  1913. br.Msg = "请选择表格"
  1914. br.ErrMsg = "未找到对应的表格"
  1915. br.IsSendEmail = false
  1916. return
  1917. }
  1918. excelInfoId = excelInfo.ExcelInfoId
  1919. }
  1920. // 获取数据详情
  1921. excelDetail, errMsg, err := excel2.GetExcelDetailInfoByExcelInfoId(excelInfoId, c.SysUser.AdminId, c.Lang)
  1922. if err != nil {
  1923. br.Msg = errMsg
  1924. br.ErrMsg = err.Error()
  1925. return
  1926. }
  1927. // 操作权限校验
  1928. {
  1929. button := excelDetail.Button
  1930. if excelDetail.Source != utils.BALANCE_TABLE {
  1931. button = excel2.GetExcelInfoOpButton(sysUser, excelDetail.SysUserId, excelDetail.Source, excelDetail.HaveOperaAuth)
  1932. }
  1933. if !button.RefreshButton {
  1934. br.Msg = "无操作权限"
  1935. br.IsSendEmail = false
  1936. return
  1937. }
  1938. }
  1939. // 数据刷新(只有自定义表格有刷新)
  1940. if excelDetail.Source == utils.TIME_TABLE {
  1941. jsonStrByte, err := json.Marshal(excelDetail.TableData)
  1942. if err != nil {
  1943. br.Msg = "自定义表格数据获取失败"
  1944. br.ErrMsg = "自定义表格数据获取失败,转json失败,Err:" + err.Error()
  1945. return
  1946. }
  1947. var tableData request.TableDataReq
  1948. err = json.Unmarshal(jsonStrByte, &tableData)
  1949. if err != nil {
  1950. br.Msg = "自定义表格数据获取失败"
  1951. br.ErrMsg = "自定义表格数据获取失败,json转结构体失败,Err:" + err.Error()
  1952. return
  1953. }
  1954. if len(tableData.EdbInfoIdList) > 0 {
  1955. err, _ = data.EdbInfoRefreshAllFromBaseV3(tableData.EdbInfoIdList, false, true, false)
  1956. if err != nil {
  1957. br.Msg = "刷新失败"
  1958. br.ErrMsg = "刷新失败,Err:" + err.Error()
  1959. return
  1960. }
  1961. }
  1962. }
  1963. // 数据刷新-混合表格
  1964. if excelDetail.Source == utils.MIXED_TABLE {
  1965. // todo 刷新动态表的所有子表中关联的指标数据
  1966. jsonByte, e := json.Marshal(excelDetail.TableData)
  1967. if e != nil {
  1968. br.Msg = "刷新失败"
  1969. br.ErrMsg = "JSON格式化混合表格数据失败, Err: " + e.Error()
  1970. return
  1971. }
  1972. var tableData request.MixedTableReq
  1973. if e = json.Unmarshal(jsonByte, &tableData); e != nil {
  1974. br.Msg = "刷新失败"
  1975. br.ErrMsg = "解析混合表格数据失败, Err: " + e.Error()
  1976. return
  1977. }
  1978. edbInfoIds := make([]int, 0)
  1979. edbInfoIdExist := make(map[int]bool)
  1980. if len(tableData.Data) > 0 {
  1981. for _, t := range tableData.Data {
  1982. for _, v := range t {
  1983. if v.EdbInfoId > 0 && !edbInfoIdExist[v.EdbInfoId] {
  1984. edbInfoIdExist[v.EdbInfoId] = true
  1985. edbInfoIds = append(edbInfoIds, v.EdbInfoId)
  1986. }
  1987. }
  1988. }
  1989. }
  1990. if len(edbInfoIds) > 0 {
  1991. e, _ = data.EdbInfoRefreshAllFromBaseV3(edbInfoIds, false, true, false)
  1992. if e != nil {
  1993. br.Msg = "刷新失败"
  1994. br.ErrMsg = "刷新混合表格数据失败, Err: " + err.Error()
  1995. return
  1996. }
  1997. }
  1998. }
  1999. if excelDetail.Source == utils.BALANCE_TABLE {
  2000. err = refreshBalanceTable(excelDetail, c.Lang)
  2001. if err != nil {
  2002. br.Msg = "刷新失败"
  2003. br.ErrMsg = "刷新失败,Err:" + err.Error()
  2004. return
  2005. }
  2006. }
  2007. // 清除缓存
  2008. key := utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL + ":" + excelDetail.UniqueCode
  2009. if utils.Re == nil {
  2010. _ = utils.Rc.Delete(key)
  2011. }
  2012. br.Ret = 200
  2013. br.Success = true
  2014. br.Msg = "刷新成功"
  2015. }
  2016. // Download
  2017. // @Title 获取表格详情
  2018. // @Description 获取表格详情接口
  2019. // @Param UniqueCode query string true "表格code"
  2020. // @Param ExcelInfoId query int true "表格id"
  2021. // @Success 200 {object} data_manage.ExcelInfo
  2022. // @router /excel_info/table/download [get]
  2023. func (c *ExcelInfoController) Download() {
  2024. br := new(models.BaseResponse).Init()
  2025. defer func() {
  2026. c.Data["json"] = br
  2027. c.ServeJSON()
  2028. }()
  2029. sysUser := c.SysUser
  2030. if sysUser == nil {
  2031. br.Msg = "请登录"
  2032. br.ErrMsg = "请登录,SysUser Is Empty"
  2033. br.Ret = 408
  2034. return
  2035. }
  2036. excelInfoId, _ := c.GetInt("ExcelInfoId")
  2037. uniqueCode := c.GetString("UniqueCode")
  2038. if excelInfoId <= 0 && uniqueCode == `` {
  2039. br.Msg = "请选择表格"
  2040. br.ErrMsg = "ExcelInfoId和UniqueCode都未传"
  2041. br.IsSendEmail = false
  2042. return
  2043. }
  2044. var excelInfo *excel3.ExcelInfo
  2045. var err error
  2046. //获取eta表格信息
  2047. if excelInfoId > 0 {
  2048. excelInfo, err = excel3.GetExcelInfoById(excelInfoId)
  2049. } else {
  2050. excelInfo, err = excel3.GetExcelInfoByUniqueCode(uniqueCode)
  2051. }
  2052. if err != nil {
  2053. br.Msg = "获取失败"
  2054. br.ErrMsg = "获取ETA表格信息失败,Err:" + err.Error()
  2055. if err.Error() == utils.ErrNoRow() {
  2056. br.Msg = "ETA表格被删除,请刷新页面"
  2057. br.ErrMsg = "ETA表格被删除,请刷新页面,Err:" + err.Error()
  2058. br.IsSendEmail = false
  2059. }
  2060. return
  2061. }
  2062. if excelInfo.Source == 1 {
  2063. br.Msg = "不是自定义表格"
  2064. br.IsSendEmail = false
  2065. return
  2066. }
  2067. var tableData excel.TableData
  2068. switch excelInfo.Source {
  2069. case utils.EXCEL_DEFAULT:
  2070. br.Msg = "表格类型异常"
  2071. case utils.TIME_TABLE: // 自定义表格
  2072. var tableDataConfig excel2.TableDataConfig
  2073. err = json.Unmarshal([]byte(excelInfo.Content), &tableDataConfig)
  2074. if err != nil {
  2075. br.Msg = "获取失败"
  2076. br.ErrMsg = "表格json转结构体失败,Err:" + err.Error()
  2077. return
  2078. }
  2079. result, err := excel2.GetDataByTableDataConfig(tableDataConfig)
  2080. if err != nil {
  2081. br.Msg = "获取失败"
  2082. br.ErrMsg = "获取最新的表格数据失败,Err:" + err.Error()
  2083. return
  2084. }
  2085. tableData, err = excel.GetTableDataByCustomData(excelInfo.ExcelType, result)
  2086. if err != nil {
  2087. br.Msg = "获取失败"
  2088. br.ErrMsg = "转换成table失败,Err:" + err.Error()
  2089. return
  2090. }
  2091. case utils.MIXED_TABLE: // 混合表格
  2092. var result request.MixedTableReq
  2093. err = json.Unmarshal([]byte(excelInfo.Content), &result)
  2094. if err != nil {
  2095. br.Msg = "获取失败"
  2096. br.ErrMsg = "表格json转结构体失败,Err:" + err.Error()
  2097. return
  2098. }
  2099. newResult, err, errMsg := excel2.GetMixedTableCellData(result, c.Lang)
  2100. if err != nil {
  2101. br.Msg = "获取失败"
  2102. if errMsg != `` {
  2103. br.Msg = errMsg
  2104. }
  2105. br.ErrMsg = "获取最新的数据失败,Err:" + err.Error()
  2106. return
  2107. }
  2108. tableData, err = excel.GetTableDataByMixedTableData(newResult, false)
  2109. if err != nil {
  2110. br.Msg = "获取失败"
  2111. br.ErrMsg = "转换成table失败,Err:" + err.Error()
  2112. return
  2113. }
  2114. case utils.BALANCE_TABLE: // 混合表格
  2115. savePath, fileName, uploadDir, err, errMsg := downloadBalanceTable(excelInfo, c.Lang)
  2116. if err != nil {
  2117. br.Msg = "下载失败"
  2118. if errMsg != `` {
  2119. br.Msg = errMsg
  2120. }
  2121. br.ErrMsg = "获取最新的数据失败,Err:" + err.Error()
  2122. return
  2123. }
  2124. defer func() {
  2125. _ = os.RemoveAll(uploadDir)
  2126. }()
  2127. // todo 删除文件
  2128. c.Ctx.Output.Download(savePath, fileName)
  2129. br.Ret = 200
  2130. br.Success = true
  2131. br.Msg = "获取成功"
  2132. br.Data = excelInfo
  2133. return
  2134. }
  2135. downloadFilePath, err := tableData.ToExcel()
  2136. if err != nil {
  2137. fmt.Println("err:", err)
  2138. return
  2139. }
  2140. defer func() {
  2141. _ = os.Remove(downloadFilePath)
  2142. }()
  2143. downloadFileName := excelInfo.ExcelName + ".xlsx"
  2144. c.Ctx.Output.Download(downloadFilePath, downloadFileName)
  2145. br.Ret = 200
  2146. br.Success = true
  2147. br.Msg = "获取成功"
  2148. br.Data = excelInfo
  2149. }
  2150. // Copy
  2151. // @Title 复制表格
  2152. // @Description 复制表格接口
  2153. // @Param request body request.EditExcelInfoReq true "type json string"
  2154. // @Success 200 {object} response.AddExcelInfoResp
  2155. // @router /excel_info/copy [post]
  2156. func (c *ExcelInfoController) Copy() {
  2157. br := new(models.BaseResponse).Init()
  2158. defer func() {
  2159. c.Data["json"] = br
  2160. c.ServeJSON()
  2161. }()
  2162. sysUser := c.SysUser
  2163. if sysUser == nil {
  2164. br.Msg = "请登录"
  2165. br.ErrMsg = "请登录,SysUser Is Empty"
  2166. br.Ret = 408
  2167. return
  2168. }
  2169. var req request.CopyExcelInfoReq
  2170. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  2171. if err != nil {
  2172. br.Msg = "参数解析异常!"
  2173. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2174. return
  2175. }
  2176. if req.ExcelInfoId <= 0 {
  2177. br.Msg = "请传入ETA表格标识!"
  2178. return
  2179. }
  2180. cacheKey := "CACHE_TABLE_INFO_EDIT_" + strconv.Itoa(req.ExcelInfoId)
  2181. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  2182. br.Msg = "系统处理中,请稍后重试!"
  2183. br.ErrMsg = "系统处理中,请稍后重试!"
  2184. return
  2185. }
  2186. defer func() {
  2187. _ = utils.Rc.Delete(cacheKey)
  2188. }()
  2189. req.ExcelName = strings.Trim(req.ExcelName, " ")
  2190. if req.ExcelName == "" {
  2191. br.Msg = "请填写表格名称!"
  2192. br.IsSendEmail = false
  2193. return
  2194. }
  2195. if req.ExcelClassifyId <= 0 {
  2196. br.Msg = "分类参数错误!"
  2197. br.IsSendEmail = false
  2198. return
  2199. }
  2200. excelInfo, err, errMsg, isSendEmail := excel2.Copy(req.ExcelInfoId, req.ExcelClassifyId, req.ExcelName, sysUser)
  2201. if err != nil {
  2202. br.Msg = "复制失败"
  2203. if errMsg != `` {
  2204. br.Msg = errMsg
  2205. }
  2206. br.ErrMsg = "复制失败,Err:" + err.Error()
  2207. br.IsSendEmail = isSendEmail
  2208. return
  2209. }
  2210. resp := new(response.AddExcelInfoResp)
  2211. resp.ExcelInfoId = excelInfo.ExcelInfoId
  2212. resp.UniqueCode = excelInfo.UniqueCode
  2213. br.Ret = 200
  2214. br.Success = true
  2215. br.Msg = "保存成功"
  2216. br.Data = resp
  2217. br.IsAddLog = true
  2218. }
  2219. //func init() {
  2220. // //list, err := data.GetFirstEdbDataList(102739, 12)
  2221. // list, err := data.GetFirstEdbDataList(101323, 12)
  2222. // if err != nil {
  2223. // fmt.Println(err)
  2224. // return
  2225. // }
  2226. //
  2227. // fmt.Println("START")
  2228. // dateList := make([]string, 0)
  2229. // for _, v := range list {
  2230. // fmt.Println(v)
  2231. // dateList = append(dateList, v.DataTime)
  2232. // }
  2233. //
  2234. // fmt.Println("End")
  2235. //
  2236. // {
  2237. // list, err := data.GetOtherEdbDataList(102227, dateList)
  2238. // if err != nil {
  2239. // fmt.Println(err)
  2240. // return
  2241. // }
  2242. //
  2243. // for _, v := range list {
  2244. // fmt.Println(v)
  2245. // }
  2246. // }
  2247. //}
  2248. //func init() {
  2249. // 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":[]}]]}`
  2250. // 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":[]}]]}`
  2251. //
  2252. // //var req request.TableDataReq
  2253. // //err := json.Unmarshal([]byte(str), &req)
  2254. // //if err != nil {
  2255. // // fmt.Println(err)
  2256. // // return
  2257. // //}
  2258. // //tableDataConfig, err := data.GetTableDataConfig(req)
  2259. // //if err != nil {
  2260. // // fmt.Println(err)
  2261. // // return
  2262. // //}
  2263. // var tableDataConfig data.TableDataConfig
  2264. // err := json.Unmarshal([]byte(str), &tableDataConfig)
  2265. // if err != nil {
  2266. // fmt.Println(err)
  2267. // return
  2268. // }
  2269. //
  2270. // fmt.Println(tableDataConfig)
  2271. //
  2272. // result, err := data.GetDataByTableDataConfig(tableDataConfig)
  2273. // if err != nil {
  2274. // fmt.Println(err)
  2275. // return
  2276. // }
  2277. // fmt.Println(result)
  2278. // //byteStr, _ := json.Marshal(result)
  2279. // //utils.FileLog.Info(string(byteStr))
  2280. //}
  2281. //func init() {
  2282. // startNow := time.Now()
  2283. // fmt.Println(startNow.Format(utils.FormatDateTime))
  2284. // startTime := startNow.Unix()
  2285. // //filePath := `static/tmpFile/test/化工23.05.24.xlsx`
  2286. // filePath := `static/tmpFile/1. TAROUND_202308010201-500.xlsx`
  2287. // //filePath := `static/tmpFile/化工23.05.24.xml`
  2288. // //filePath := `static/tmpFile/化工23.05.24/xl/workbook.xml`
  2289. // //filePath := `static/tmpFile/1. TAROUND_202308010201.xlsx`
  2290. //
  2291. // //filePath := `static/tmpFile/1. TAROUND_202308010201-2.xlsx`
  2292. //
  2293. // //filePath := `static/tmpFile/5. Outage_Values_Daily.xlsx`
  2294. // //filePath := `static/tmpFile/1. TAROUND_202308010201 - 副本.xlsx`
  2295. // excel.ConvToLuckySheet(filePath)
  2296. // cz := time.Now().Unix() - startTime
  2297. // fmt.Printf("处理完需要的时间:%d秒;约耗时:%d分%d秒", cz, cz/60, cz%60)
  2298. // fmt.Println()
  2299. //
  2300. // //sheetName := `日度`
  2301. // //sheetName := `月度`
  2302. // //fmt.Println(excel.GetDefaultSheetIndex(filePath, sheetName))
  2303. //
  2304. // //decompressZip(filePath)
  2305. //}
  2306. // src: 需要解压的zip文件
  2307. func decompressZip(src string) error {
  2308. archive, err := zip.OpenReader(src)
  2309. if err != nil {
  2310. return err
  2311. }
  2312. dir := filepath.Dir(src) + "/zip"
  2313. defer archive.Close()
  2314. for _, f := range archive.File {
  2315. filePath := filepath.Join(dir, f.Name)
  2316. if f.FileInfo().IsDir() {
  2317. os.MkdirAll(filePath, os.ModePerm)
  2318. continue
  2319. }
  2320. if err := os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil {
  2321. return fmt.Errorf("failed to make directory (%v)", err)
  2322. }
  2323. dstFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
  2324. if err != nil {
  2325. return fmt.Errorf("failed to create file (%v)", err)
  2326. }
  2327. fileInArchive, err := f.Open()
  2328. if err != nil {
  2329. return fmt.Errorf("failed to open file in zip (%v)", err)
  2330. }
  2331. if _, err := io.Copy(dstFile, fileInArchive); err != nil {
  2332. return fmt.Errorf("failed to copy file in zip (%v)", err)
  2333. }
  2334. dstFile.Close()
  2335. fileInArchive.Close()
  2336. }
  2337. return nil
  2338. }
  2339. // MarkEditStatus
  2340. // @Title 标记沙盘编辑状态
  2341. // @Description 标记沙盘编辑状态接口
  2342. // @Param request body request.MarkEditSandbox true "type json string"
  2343. // @Success 200 标记成功 ;202 标记成功
  2344. // @router /excel_info/mark [post]
  2345. func (c *ExcelInfoController) MarkEditStatus() {
  2346. br := new(models.BaseResponse).Init()
  2347. defer func() {
  2348. c.Data["json"] = br
  2349. c.ServeJSON()
  2350. }()
  2351. sysUser := c.SysUser
  2352. if sysUser == nil {
  2353. br.Msg = "请登录"
  2354. br.ErrMsg = "请登录,SysUser Is Empty"
  2355. br.Ret = 408
  2356. return
  2357. }
  2358. var req request.MarkEditExcel
  2359. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  2360. if err != nil {
  2361. br.Msg = "参数解析异常!"
  2362. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2363. return
  2364. }
  2365. if req.ExcelInfoId <= 0 {
  2366. br.Msg = "缺少表格编号"
  2367. return
  2368. }
  2369. if req.Status <= 0 {
  2370. br.Msg = "标记状态异常"
  2371. return
  2372. }
  2373. status := 1
  2374. if req.Status == 2 {
  2375. status = 0
  2376. }
  2377. //更新标记key
  2378. data, err := services.UpdateExcelEditMark(req.ExcelInfoId, sysUser.AdminId, status, sysUser.RealName)
  2379. if err != nil {
  2380. br.Msg = err.Error()
  2381. return
  2382. }
  2383. msg := "标记成功"
  2384. br.Ret = 200
  2385. br.Success = true
  2386. br.Msg = msg
  2387. br.Data = data
  2388. }
  2389. // BatchRefresh
  2390. // @Title 批量刷新表格接口
  2391. // @Description 批量刷新图表接口
  2392. // @Param request body excel3.BatchRefreshExcelReq true "type json string"
  2393. // @Success Ret=200 刷新成功
  2394. // @router /excel_info/table/batch_refresh [post]
  2395. func (c *ExcelInfoController) BatchRefresh() {
  2396. br := new(models.BaseResponse).Init()
  2397. defer func() {
  2398. if br.ErrMsg == "" {
  2399. br.IsSendEmail = false
  2400. }
  2401. c.Data["json"] = br
  2402. c.ServeJSON()
  2403. }()
  2404. sysUser := c.SysUser
  2405. if sysUser == nil {
  2406. br.Msg = "请登录"
  2407. br.ErrMsg = "请登录,SysUser Is Empty"
  2408. br.Ret = 408
  2409. return
  2410. }
  2411. var req excel3.BatchRefreshExcelReq
  2412. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  2413. if err != nil {
  2414. br.Msg = "参数解析异常!"
  2415. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2416. return
  2417. }
  2418. if len(req.ExcelCodes) == 0 {
  2419. br.Ret = 200
  2420. br.Success = true
  2421. br.Msg = "刷新成功"
  2422. return
  2423. }
  2424. // 获取表格关联的指标IDs
  2425. edbIds, e := excel2.GetEdbIdsFromExcelCodes(req.ExcelCodes, sysUser.AdminId, c.Lang)
  2426. if e != nil {
  2427. br.Msg = "刷新失败"
  2428. br.ErrMsg = "获取表格关联的指标IDs失败, Err: " + e.Error()
  2429. return
  2430. }
  2431. redisKey := data.GetBatchChartRefreshKey(req.Source, req.ReportId, req.ReportChapterId)
  2432. refreshKeys := make([]string, 0)
  2433. for _, v := range req.ExcelCodes {
  2434. refreshKeys = append(refreshKeys, fmt.Sprint(utils.HZ_CHART_LIB_EXCEL_TABLE_DETAIL, v))
  2435. }
  2436. // 刷新相关指标
  2437. syncing, e := data.BatchRefreshEdbByEdbIds(edbIds, redisKey, refreshKeys)
  2438. if e != nil {
  2439. br.Msg = "刷新失败"
  2440. br.ErrMsg = "刷新表格关联指标信息失败,Err:" + err.Error()
  2441. return
  2442. }
  2443. br.Msg = "刷新成功"
  2444. if syncing {
  2445. br.Msg = "表格关联指标较多,请10分钟后刷新页面查看最新数据"
  2446. }
  2447. br.Ret = 200
  2448. br.Success = true
  2449. }
  2450. // GetBatchChartRefreshResult
  2451. // @Title 获取批量刷新表格结果
  2452. // @Description 获取批量刷新表格结果
  2453. // @Param request body excel3.BatchRefreshExcelReq true "type json string"
  2454. // @Success Ret=200 刷新成功
  2455. // @router /excel_info/table/batch_refresh/result [post]
  2456. func (c *ExcelInfoController) GetBatchChartRefreshResult() {
  2457. br := new(models.BaseResponse).Init()
  2458. defer func() {
  2459. c.Data["json"] = br
  2460. c.ServeJSON()
  2461. }()
  2462. sysUser := c.SysUser
  2463. if sysUser == nil {
  2464. br.Msg = "请登录"
  2465. br.ErrMsg = "请登录,SysUser Is Empty"
  2466. br.Ret = 408
  2467. return
  2468. }
  2469. var req excel3.BatchRefreshExcelReq
  2470. err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
  2471. if err != nil {
  2472. br.Msg = "参数解析异常!"
  2473. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2474. return
  2475. }
  2476. // 校验缓存是否存在, 存在说明还在刷新中
  2477. result := true
  2478. redisKey := excel2.GetExcelEdbBatchRefreshKey(req.Source, req.ReportId, req.ReportChapterId)
  2479. if redisKey != `` {
  2480. // 如果找到了key,那么就是还在更新中
  2481. ok := utils.Rc.IsExist(redisKey)
  2482. if ok {
  2483. result = false
  2484. }
  2485. }
  2486. resp := struct {
  2487. RefreshResult bool `description:"刷新结果"`
  2488. }{
  2489. RefreshResult: result,
  2490. }
  2491. br.Data = resp
  2492. br.Ret = 200
  2493. br.Success = true
  2494. }
  2495. // GetBatchChartRefreshResult
  2496. // @Title 获取批量刷新表格结果
  2497. // @Description 获取批量刷新表格结果
  2498. // @Param EdbInfoId query int true "edb id"
  2499. // @Success Ret=200 刷新成功
  2500. // @router /excel_info/get_edb_source [get]
  2501. func (c *ExcelInfoController) GetEdbSource() {
  2502. br := new(models.BaseResponse).Init()
  2503. defer func() {
  2504. c.Data["json"] = br
  2505. c.ServeJSON()
  2506. }()
  2507. sysUser := c.SysUser
  2508. if sysUser == nil {
  2509. br.Msg = "请登录"
  2510. br.ErrMsg = "请登录,SysUser Is Empty"
  2511. br.Ret = 408
  2512. return
  2513. }
  2514. edbInfoId, _ := c.GetInt("EdbInfoId")
  2515. if edbInfoId <= 0 {
  2516. br.Msg = "请选择指标"
  2517. br.ErrMsg = "请选择指标"
  2518. br.IsSendEmail = false
  2519. return
  2520. }
  2521. sourceNameList, sourceNameEnList, err := excel2.GetEdbSourceByEdbInfoIdList([]int{edbInfoId})
  2522. if err != nil {
  2523. br.Msg = "自定义表格数据获取失败"
  2524. br.ErrMsg = "自定义表格数据获取失败,Err:" + err.Error()
  2525. return
  2526. }
  2527. excelSource := strings.Join(sourceNameList, ",")
  2528. excelSourceEn := strings.Join(sourceNameEnList, ",")
  2529. var resp struct {
  2530. ExcelSource string `description:"表格来源"`
  2531. ExcelSourceEn string `description:"表格来源(英文)"`
  2532. }
  2533. resp.ExcelSource = excelSource
  2534. resp.ExcelSourceEn = excelSourceEn
  2535. br.Data = resp
  2536. br.Ret = 200
  2537. br.Success = true
  2538. }