excel_info.go 78 KB

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