excel_info.go 86 KB

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