excel_info.go 74 KB

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