chart_info_excel_balance.go 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496
  1. package data
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_api/models/data_manage"
  6. "eta/eta_api/models/data_manage/chart_theme"
  7. excelModel "eta/eta_api/models/data_manage/excel"
  8. "eta/eta_api/models/data_manage/excel/request"
  9. "eta/eta_api/models/system"
  10. "eta/eta_api/services/data/data_manage_permission"
  11. "eta/eta_api/utils"
  12. "fmt"
  13. "math"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. // AddBalanceExcelChart 添加平衡表图表
  19. func AddBalanceExcelChart(req request.AddBalanceTableChartReq, sysUser *system.Admin) (chartInfo *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  20. // 获取表格信息
  21. excelInfo, err := excelModel.GetExcelInfoById(req.ExcelInfoId)
  22. if err != nil {
  23. errMsg = "找不到该EXCEL"
  24. err = fmt.Errorf("找不到该EXCEL!%s", err.Error())
  25. return
  26. }
  27. if excelInfo.Source != utils.BALANCE_TABLE {
  28. errMsg = "EXCEL类型错误!"
  29. err = fmt.Errorf("EXCEL类型错误!")
  30. return
  31. }
  32. if len(req.ChartEdbInfoList) == 0 {
  33. errMsg = "图表数据不能为空!"
  34. err = fmt.Errorf("图表数据不能为空!%s", err.Error())
  35. return
  36. }
  37. chartEdbList := make([]*excelModel.AddChartEdbAndDataItem, 0)
  38. for _, chartEdb := range req.ChartEdbInfoList {
  39. excelEdb := new(excelModel.ExcelChartEdb)
  40. excelEdb.EdbName = chartEdb.EdbName
  41. randStr := utils.GetRandDigit(4)
  42. excelEdb.EdbCode = `T` + time.Now().Format("060102150405") + "_" + randStr
  43. excelEdb.ExcelInfoId = excelInfo.ExcelInfoId
  44. excelEdb.DateSequence = chartEdb.DateSequenceStr
  45. excelEdb.DataSequence = chartEdb.DataSequenceStr
  46. excelEdb.SysUserId = sysUser.AdminId
  47. excelEdb.SysUserRealName = sysUser.RealName
  48. excelEdb.MaxData = chartEdb.MaxData
  49. excelEdb.MinData = chartEdb.MinData
  50. excelEdb.IsOrder = chartEdb.IsOrder
  51. excelEdb.IsAxis = chartEdb.IsAxis
  52. excelEdb.FromTag = chartEdb.FromTag
  53. excelEdb.EdbInfoType = chartEdb.EdbInfoType
  54. excelEdb.LeadValue = chartEdb.LeadValue
  55. excelEdb.LeadUnit = chartEdb.LeadUnit
  56. excelEdb.CreateTime = time.Now()
  57. excelEdb.ModifyTime = time.Now()
  58. var dateList []string
  59. var dataList []float64
  60. if excelInfo.BalanceType == 1 {
  61. // 如果是静态表,则直接使用前端传输的数据落到数据库里
  62. /*dateList, dataList, err, errMsg = utils.HandleEdbSequenceVal(chartEdb.DateSequenceVal, chartEdb.DataSequenceVal)
  63. if err != nil {
  64. errMsg = "时间序列或数据序列异常!"
  65. err = fmt.Errorf("时间序列或数据序列异常!%s", err.Error())
  66. return
  67. }*/
  68. }
  69. // 处理日期列表和值列表
  70. addItem := &excelModel.AddChartEdbAndDataItem{
  71. ChartEdb: excelEdb,
  72. DateList: dateList,
  73. ValList: dataList,
  74. }
  75. chartEdbList = append(chartEdbList, addItem)
  76. }
  77. chartInfo, err, errMsg, isSendEmail = addBalanceExcelChart(req, sysUser.AdminId, sysUser.RealName)
  78. if err != nil {
  79. errMsg = "新增图表失败!"
  80. err = fmt.Errorf("新增图表失败!%s, %s", errMsg, err.Error())
  81. return
  82. }
  83. obj := new(excelModel.ExcelChartEdb)
  84. err = obj.AddChartEdbAndData(chartEdbList, chartInfo, []int{})
  85. if err != nil {
  86. errMsg = "新增图表失败!"
  87. err = fmt.Errorf("新增图表数据失败!%s", err.Error())
  88. return
  89. }
  90. return
  91. }
  92. func addBalanceExcelChart(req request.AddBalanceTableChartReq, sysUserId int, sysUserRealName string) (chartInfo *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  93. isSendEmail = true // 默认错误的时候要发送邮件
  94. req.ChartName = strings.Trim(req.ChartName, " ")
  95. if req.ChartName == "" {
  96. errMsg = "请填写图表名称!"
  97. err = fmt.Errorf(errMsg)
  98. isSendEmail = false
  99. return
  100. }
  101. chartType := req.ChartType
  102. extraConfig := req.ExtraConfig
  103. // todo 查找默认主题设置
  104. // 查找主题类型id
  105. chartThemeType, err := chart_theme.GetChartThemeTypeByChartType(chartType)
  106. if err != nil {
  107. errMsg = "查找主题类型失败!"
  108. err = fmt.Errorf("%s, %s", errMsg, err.Error())
  109. return
  110. }
  111. chartThemeId := chartThemeType.DefaultChartThemeId
  112. // 季节性图表额外配置信息
  113. var seasonExtraConfig string
  114. if len(req.ChartEdbInfoList) <= 0 {
  115. errMsg = "请选择指标!"
  116. err = fmt.Errorf(errMsg)
  117. return
  118. }
  119. if chartType == 2 {
  120. // 处理季节性图表横轴配置
  121. {
  122. if req.SeasonExtraConfig.XEndDate != "" {
  123. if req.SeasonExtraConfig.XStartDate > req.SeasonExtraConfig.XEndDate && req.SeasonExtraConfig.JumpYear != 1 {
  124. errMsg = "季节性图表配置信息异常:横坐标日期配置错误"
  125. err = fmt.Errorf("季节性图表配置信息异常: 横坐标日期配置错误")
  126. return
  127. }
  128. seasonExtra, tErr := json.Marshal(req.SeasonExtraConfig)
  129. if tErr != nil {
  130. errMsg = "季节性图表配置信息异常"
  131. err = fmt.Errorf("季节性图表配置信息异常,Err:" + tErr.Error())
  132. return
  133. }
  134. seasonExtraConfig = string(seasonExtra)
  135. }
  136. }
  137. }
  138. // 图表额外配置
  139. extraConfig, err, errMsg = HandleExtraConfig(chartType, extraConfig)
  140. if err != nil {
  141. if errMsg == `` {
  142. errMsg = "指标异常!"
  143. }
  144. err = fmt.Errorf("指标异常!Err:" + err.Error())
  145. return
  146. }
  147. //判断图表是否存在
  148. var condition string
  149. var pars []interface{}
  150. // 图表名称在不同图分类下不允许重复 需求调整时间:2022年11月07日09:47:07
  151. condition += " AND chart_classify_id=0 "
  152. condition += " AND chart_name=? AND source = ? "
  153. pars = append(pars, req.ChartName, utils.CHART_SOURCE_BALANCE_EXCEL)
  154. count, err := data_manage.GetChartInfoCountByCondition(condition, pars)
  155. if err != nil {
  156. errMsg = "判断图表名称是否存在失败"
  157. err = fmt.Errorf("判断图表名称是否存在失败,Err:" + err.Error())
  158. return
  159. }
  160. if count > 0 {
  161. errMsg = "图表已存在,请重新填写"
  162. err = fmt.Errorf("判断图表名称是否存在失败")
  163. isSendEmail = false
  164. return
  165. }
  166. chartInfo = new(data_manage.ChartInfo)
  167. chartInfo.ChartName = req.ChartName
  168. //chartInfo.EdbInfoIds = edbInfoIdStr
  169. //chartInfo.ChartClassifyId = req.ChartClassifyId
  170. chartInfo.SysUserId = sysUserId
  171. chartInfo.SysUserRealName = sysUserRealName
  172. chartInfo.CreateTime = time.Now()
  173. chartInfo.ModifyTime = time.Now()
  174. chartInfo.IsSetName = 0
  175. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  176. chartInfo.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
  177. // todo 判断是否需要重新计算用户的start_date
  178. chartInfo.DateType = 3
  179. if chartType == 0 {
  180. chartType = 1
  181. }
  182. chartInfo.ChartType = chartType
  183. calendar := req.Calendar
  184. if calendar == "" {
  185. calendar = "公历"
  186. }
  187. chartInfo.Calendar = calendar
  188. /* chartInfo.StartDate = req.StartDate
  189. chartInfo.EndDate = req.EndDate
  190. chartInfo.SeasonStartDate = req.StartDate
  191. chartInfo.SeasonEndDate = req.EndDate*/
  192. chartInfo.LeftMin = req.LeftMin
  193. chartInfo.LeftMax = req.LeftMax
  194. chartInfo.RightMin = req.RightMin
  195. chartInfo.RightMax = req.RightMax
  196. chartInfo.Right2Min = req.Right2Min
  197. chartInfo.Right2Max = req.Right2Max
  198. chartInfo.MinMaxSave = req.MinMaxSave
  199. //chartInfo.Disabled = disableVal
  200. //chartInfo.BarConfig = barChartConf
  201. chartInfo.ExtraConfig = extraConfig
  202. chartInfo.SeasonExtraConfig = seasonExtraConfig
  203. //chartInfo.StartYear = req.StartYear
  204. chartInfo.Source = utils.CHART_SOURCE_BALANCE_EXCEL
  205. chartInfo.ChartThemeId = chartThemeId
  206. chartInfo.SourcesFrom = req.SourcesFrom
  207. /* chartInfo.Instructions = req.Instructions
  208. chartInfo.MarkersLines = req.MarkersLines
  209. chartInfo.MarkersAreas = req.MarkersAreas
  210. chartInfo.Unit = req.Unit
  211. chartInfo.UnitEn = req.UnitEn*/
  212. /*newId, err := data_manage.AddChartInfo(chartInfo)
  213. if err != nil {
  214. errMsg = `保存失败`
  215. err = fmt.Errorf("保存失败,Err:" + err.Error())
  216. return
  217. }
  218. chartInfo.ChartInfoId = int(newId)*/
  219. //添加es数据
  220. //go EsAddOrEditChartInfo(chartInfo.ChartInfoId)
  221. return
  222. }
  223. // EditBalanceExcelChart 添加平衡表图表
  224. func EditBalanceExcelChart(req request.AddBalanceTableChartReq, sysUser *system.Admin) (chartInfo *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  225. // 获取表格信息
  226. excelInfo, err := excelModel.GetExcelInfoById(req.ExcelInfoId)
  227. if err != nil {
  228. errMsg = "找不到该EXCEL"
  229. err = fmt.Errorf("找不到该EXCEL!%s", err.Error())
  230. return
  231. }
  232. if excelInfo.Source != utils.BALANCE_TABLE {
  233. errMsg = "EXCEL类型错误!"
  234. err = fmt.Errorf("EXCEL类型错误!")
  235. return
  236. }
  237. if len(req.ChartEdbInfoList) == 0 {
  238. errMsg = "图表数据不能为空!"
  239. err = fmt.Errorf("图表数据不能为空!%s", err.Error())
  240. return
  241. }
  242. //查询已有的mapping
  243. mapping, err := excelModel.GetExcelChartEdbMappingByChartInfoId(req.ChartInfoId)
  244. if err != nil {
  245. errMsg = "查询图表mapping失败!"
  246. err = fmt.Errorf("查询图表mapping失败!%s", err.Error())
  247. return
  248. }
  249. deleteMap := make(map[int]int)
  250. for _, v := range mapping {
  251. deleteMap[v.ExcelChartEdbId] = v.ExcelChartEdbId
  252. }
  253. // 查询edbIds
  254. chartEdbList := make([]*excelModel.AddChartEdbAndDataItem, 0)
  255. for _, chartEdb := range req.ChartEdbInfoList {
  256. if _, ok := deleteMap[chartEdb.ExcelChartEdbId]; ok {
  257. delete(deleteMap, chartEdb.ExcelChartEdbId)
  258. }
  259. excelChartEdbId := chartEdb.ExcelChartEdbId
  260. excelEdb, tmpErr := excelModel.GetExcelChartEdbById(excelChartEdbId)
  261. if tmpErr != nil {
  262. if tmpErr.Error() == utils.ErrNoRow() {
  263. errMsg = "指标不存在!"
  264. err = errors.New("指标不存在,edbInfoId:" + strconv.Itoa(excelChartEdbId))
  265. return
  266. } else {
  267. errMsg = "获取指标信息失败!"
  268. err = errors.New("获取图表的指标信息失败,Err:" + tmpErr.Error())
  269. return
  270. }
  271. }
  272. if excelEdb == nil {
  273. errMsg = "指标已被删除,请重新选择!"
  274. err = errors.New("指标不存在,ChartInfoId:" + strconv.Itoa(req.ChartInfoId))
  275. return
  276. } else {
  277. if excelEdb.ExcelChartEdbId <= 0 {
  278. errMsg = "指标已被删除,请重新选择!"
  279. err = errors.New("指标不存在,ChartInfoId:" + strconv.Itoa(req.ChartInfoId))
  280. return
  281. }
  282. }
  283. excelEdb.EdbName = chartEdb.EdbName
  284. excelEdb.DateSequence = chartEdb.DateSequenceStr
  285. excelEdb.DataSequence = chartEdb.DataSequenceStr
  286. excelEdb.MaxData = chartEdb.MaxData
  287. excelEdb.MinData = chartEdb.MinData
  288. excelEdb.IsOrder = chartEdb.IsOrder
  289. excelEdb.IsAxis = chartEdb.IsAxis
  290. excelEdb.FromTag = chartEdb.FromTag
  291. excelEdb.EdbInfoType = chartEdb.EdbInfoType
  292. excelEdb.LeadValue = chartEdb.LeadValue
  293. excelEdb.LeadUnit = chartEdb.LeadUnit
  294. excelEdb.ModifyTime = time.Now()
  295. var dateList []string
  296. var dataList []float64
  297. if excelInfo.BalanceType == 1 {
  298. // 如果是静态表,则直接使用前端传输的数据落到数据库里
  299. /*dateList, dataList, err, errMsg = utils.HandleEdbSequenceVal(chartEdb.DateSequenceVal, chartEdb.DataSequenceVal)
  300. if err != nil {
  301. errMsg = "时间序列或数据序列异常!"
  302. err = fmt.Errorf("时间序列或数据序列异常!%s", err.Error())
  303. return
  304. }*/
  305. }
  306. // 处理日期列表和值列表
  307. addItem := &excelModel.AddChartEdbAndDataItem{
  308. ChartEdb: excelEdb,
  309. DateList: dateList,
  310. ValList: dataList,
  311. }
  312. chartEdbList = append(chartEdbList, addItem)
  313. }
  314. chartInfo, err, errMsg, isSendEmail = editBalanceExcelChart(req)
  315. if err != nil {
  316. errMsg = "新增图表失败!"
  317. err = fmt.Errorf("新增图表失败!%s, %s", errMsg, err.Error())
  318. return
  319. }
  320. obj := new(excelModel.ExcelChartEdb)
  321. //删除原先多余的指标
  322. deleteEdbIds := make([]int, 0)
  323. for k, _ := range deleteMap {
  324. deleteEdbIds = append(deleteEdbIds, k)
  325. }
  326. err = obj.AddChartEdbAndData(chartEdbList, chartInfo, deleteEdbIds)
  327. if err != nil {
  328. errMsg = "新增图表失败!"
  329. err = fmt.Errorf("新增图表数据失败!%s", err.Error())
  330. return
  331. }
  332. return
  333. }
  334. func editBalanceExcelChart(req request.AddBalanceTableChartReq) (chartInfo *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  335. isSendEmail = true // 默认错误的时候要发送邮件
  336. chartType := req.ChartType
  337. extraConfig := req.ExtraConfig
  338. chartInfo, err = data_manage.GetChartInfoById(req.ChartInfoId)
  339. if err != nil {
  340. if err.Error() == utils.ErrNoRow() {
  341. errMsg = "图表已被删除,请刷新页面"
  342. err = errors.New(errMsg)
  343. isSendEmail = false
  344. return
  345. }
  346. errMsg = "获取图表信息失败"
  347. err = errors.New("获取图表信息失败,Err:" + err.Error())
  348. return
  349. }
  350. // todo 查找默认主题设置
  351. // todo 查找默认主题设置
  352. // 查找主题类型id
  353. chartThemeType, err := chart_theme.GetChartThemeTypeByChartType(chartType)
  354. if err != nil {
  355. errMsg = "查找主题类型失败!"
  356. err = fmt.Errorf("%s, %s", errMsg, err.Error())
  357. return
  358. }
  359. chartThemeId := chartThemeType.DefaultChartThemeId
  360. // 季节性图表额外配置信息
  361. var seasonExtraConfig string
  362. if chartType == 2 {
  363. // 处理季节性图表横轴配置
  364. {
  365. if req.SeasonExtraConfig.XEndDate != "" {
  366. if req.SeasonExtraConfig.XStartDate > req.SeasonExtraConfig.XEndDate && req.SeasonExtraConfig.JumpYear != 1 {
  367. errMsg = "季节性图表配置信息异常:横坐标日期配置错误"
  368. err = fmt.Errorf("季节性图表配置信息异常: 横坐标日期配置错误")
  369. return
  370. }
  371. seasonExtra, tErr := json.Marshal(req.SeasonExtraConfig)
  372. if tErr != nil {
  373. errMsg = "季节性图表配置信息异常"
  374. err = fmt.Errorf("季节性图表配置信息异常,Err:" + tErr.Error())
  375. return
  376. }
  377. seasonExtraConfig = string(seasonExtra)
  378. }
  379. }
  380. }
  381. /*var extraConfigEdbInfoIdArr []int
  382. extraConfigEdbInfoIdArr, err, errMsg = CheckChartExtraConfig(req.ChartType, req.ExtraConfig)
  383. if err != nil {
  384. err = errors.New("添加失败:" + err.Error())
  385. return
  386. }*/
  387. // 图表额外配置
  388. extraConfig, err, errMsg = HandleExtraConfig(chartType, extraConfig)
  389. if err != nil {
  390. if errMsg == `` {
  391. errMsg = "指标异常!"
  392. }
  393. err = fmt.Errorf("指标异常!Err:" + err.Error())
  394. return
  395. }
  396. //判断图表是否存在
  397. var condition string
  398. var pars []interface{}
  399. condition += " AND chart_info_id<>? "
  400. pars = append(pars, req.ChartInfoId)
  401. // 图表名称在不同图分类下不允许重复 需求调整时间:2022年11月07日09:47:07
  402. condition += " AND chart_classify_id=0 "
  403. condition += " AND chart_name=? AND source = ? "
  404. pars = append(pars, req.ChartName, utils.CHART_SOURCE_BALANCE_EXCEL)
  405. count, err := data_manage.GetChartInfoCountByCondition(condition, pars)
  406. if err != nil {
  407. errMsg = "判断图表名称是否存在失败"
  408. err = fmt.Errorf("判断图表名称是否存在失败,Err:" + err.Error())
  409. return
  410. }
  411. if count > 0 {
  412. errMsg = "图表已存在,请重新填写"
  413. err = fmt.Errorf("判断图表名称是否存在失败")
  414. isSendEmail = false
  415. return
  416. }
  417. // todo 判断是否是禁用的图表
  418. // disableVal := data.CheckIsDisableChart(edbInfoIdArr)
  419. chartInfo.ChartName = req.ChartName
  420. //chartInfo.EdbInfoIds = edbInfoIdStr
  421. //chartInfo.ChartClassifyId = req.ChartClassifyId
  422. chartInfo.ModifyTime = time.Now()
  423. chartInfo.IsSetName = 0
  424. // todo 判断是否需要重新计算用户的start_date
  425. chartInfo.DateType = 3
  426. if chartType == 0 {
  427. chartType = 1
  428. }
  429. chartInfo.ChartType = chartType
  430. calendar := req.Calendar
  431. if calendar == "" {
  432. calendar = "公历"
  433. }
  434. chartInfo.Calendar = calendar
  435. /* chartInfo.StartDate = req.StartDate
  436. chartInfo.EndDate = req.EndDate
  437. chartInfo.SeasonStartDate = req.StartDate
  438. chartInfo.SeasonEndDate = req.EndDate*/
  439. chartInfo.LeftMin = req.LeftMin
  440. chartInfo.LeftMax = req.LeftMax
  441. chartInfo.RightMin = req.RightMin
  442. chartInfo.RightMax = req.RightMax
  443. chartInfo.Right2Min = req.Right2Min
  444. chartInfo.Right2Max = req.Right2Max
  445. chartInfo.MinMaxSave = req.MinMaxSave
  446. //chartInfo.Disabled = disableVal
  447. //chartInfo.BarConfig = barChartConf
  448. chartInfo.ExtraConfig = extraConfig
  449. chartInfo.SeasonExtraConfig = seasonExtraConfig
  450. //chartInfo.StartYear = req.StartYear
  451. chartInfo.Source = utils.CHART_SOURCE_BALANCE_EXCEL
  452. chartInfo.ChartThemeId = chartThemeId
  453. chartInfo.SourcesFrom = req.SourcesFrom
  454. /* chartInfo.Instructions = req.Instructions
  455. chartInfo.MarkersLines = req.MarkersLines
  456. chartInfo.MarkersAreas = req.MarkersAreas
  457. chartInfo.Unit = req.Unit
  458. chartInfo.UnitEn = req.UnitEn*/
  459. /*newId, err := data_manage.AddChartInfo(chartInfo)
  460. if err != nil {
  461. errMsg = `保存失败`
  462. err = fmt.Errorf("保存失败,Err:" + err.Error())
  463. return
  464. }
  465. chartInfo.ChartInfoId = int(newId)*/
  466. //添加es数据
  467. //go EsAddOrEditChartInfo(chartInfo.ChartInfoId)
  468. return
  469. }
  470. func GetBalanceExcelChartDetail(chartInfo *data_manage.ChartInfoView, mappingListTmp []*excelModel.ExcelChartEdb, sysUser *system.Admin, dataListMap map[int][]*data_manage.EdbDataList) (resp *data_manage.ChartInfoDetailResp, err error, errMsg string) {
  471. // todo 图表权限处理
  472. // 图表数据权限
  473. {
  474. // 已授权分类id
  475. permissionChartIdList, permissionClassifyIdList, e := data_manage_permission.GetUserChartAndClassifyPermissionList(sysUser.AdminId, chartInfo.ChartInfoId, chartInfo.ChartClassifyId)
  476. if e != nil {
  477. errMsg = "获取失败"
  478. err = fmt.Errorf("获取已授权分类id数据失败,Err:" + err.Error())
  479. return
  480. }
  481. chartInfo.HaveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(chartInfo.IsJoinPermission, 0, chartInfo.ChartInfoId, chartInfo.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
  482. }
  483. chartInfoId := chartInfo.ChartInfoId
  484. resp = new(data_manage.ChartInfoDetailResp)
  485. // 获取主题样式
  486. chartTheme, err := GetChartThemeConfig(chartInfo.ChartThemeId, 1, chartInfo.ChartType)
  487. if err != nil {
  488. errMsg = "获取失败"
  489. err = fmt.Errorf(" 获取主题信息失败 Err:%s", err.Error())
  490. return
  491. }
  492. chartInfo.ChartThemeStyle = chartTheme.Config
  493. chartInfo.ChartThemeId = chartTheme.ChartThemeId
  494. dateType := chartInfo.DateType
  495. fmt.Println("dateType:", dateType)
  496. chartType := chartInfo.ChartType
  497. startDate := chartInfo.StartDate
  498. endDate := chartInfo.EndDate
  499. seasonStartDate := chartInfo.SeasonStartDate
  500. seasonEndDate := chartInfo.SeasonEndDate
  501. //startYear := chartInfo.StartYear
  502. calendar := chartInfo.Calendar
  503. if calendar == "" {
  504. calendar = "公历"
  505. }
  506. mappingList := make([]*data_manage.ChartEdbInfoMapping, 0)
  507. //循环组装映射关系
  508. for _, v := range mappingListTmp {
  509. mapping := &data_manage.ChartEdbInfoMapping{
  510. EdbInfoId: v.ExcelChartEdbId,
  511. SourceName: "",
  512. Source: 0,
  513. SubSource: 0,
  514. EdbCode: v.EdbCode,
  515. EdbName: v.EdbName,
  516. EdbAliasName: v.EdbName,
  517. EdbNameEn: "",
  518. EdbAliasNameEn: "",
  519. EdbType: 0,
  520. Frequency: "",
  521. FrequencyEn: "",
  522. Unit: "",
  523. UnitEn: "",
  524. StartDate: "",
  525. EndDate: "",
  526. ModifyTime: v.ModifyTime.Format(utils.FormatDateTime),
  527. ChartEdbMappingId: v.ExcelChartEdbId,
  528. ChartInfoId: chartInfoId,
  529. MaxData: v.MaxData,
  530. MinData: v.MinData,
  531. IsOrder: v.IsOrder,
  532. IsAxis: v.IsAxis,
  533. //EdbInfoType: 0,
  534. //EdbInfoCategoryType: 0,
  535. LeadValue: v.LeadValue,
  536. LeadUnit: v.LeadUnit,
  537. LeadUnitEn: "",
  538. ChartStyle: "",
  539. ChartColor: "",
  540. PredictChartColor: "",
  541. ChartWidth: 0,
  542. ChartType: 0,
  543. LatestDate: "",
  544. LatestValue: 0,
  545. MoveLatestDate: "",
  546. UniqueCode: "",
  547. MinValue: 0,
  548. MaxValue: 0,
  549. DataList: nil,
  550. IsNullData: false,
  551. MappingSource: 0,
  552. RegionType: "",
  553. ClassifyId: 0,
  554. IsConvert: 0,
  555. ConvertType: 0,
  556. ConvertValue: 0,
  557. ConvertUnit: "",
  558. ConvertEnUnit: "",
  559. IsJoinPermission: 0,
  560. HaveOperaAuth: true,
  561. }
  562. mappingList = append(mappingList, mapping)
  563. }
  564. if chartType == 2 {
  565. startDate = seasonStartDate
  566. endDate = seasonEndDate
  567. if dateType <= 0 {
  568. if startDate != "" {
  569. dateType = 5
  570. } else {
  571. dateType = utils.DateTypeNYears
  572. }
  573. }
  574. } else {
  575. if dateType <= 0 {
  576. dateType = 3
  577. }
  578. }
  579. /*yearMax := 0
  580. if dateType == utils.DateTypeNYears {
  581. for _, v := range mappingList {
  582. if v.LatestDate != "" {
  583. lastDateT, tErr := time.Parse(utils.FormatDate, v.LatestDate)
  584. if tErr != nil {
  585. br.Msg = "获取失败"
  586. br.ErrMsg = "获取图表日期信息失败,Err:" + tErr.Error()
  587. return
  588. }
  589. if lastDateT.Year() > yearMax {
  590. yearMax = lastDateT.Year()
  591. }
  592. }
  593. }
  594. }
  595. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, yearMax)*/
  596. if chartInfo.ChartType == 2 {
  597. chartInfo.StartDate = startDate
  598. chartInfo.EndDate = endDate
  599. }
  600. // 图表额外数据参数
  601. extraConfigStr := chartInfo.ExtraConfig
  602. // 柱方图的一些配置
  603. var barConfig data_manage.BarChartInfoReq
  604. if chartInfo != nil && chartInfo.ChartType == 7 {
  605. if chartInfo.BarConfig == `` {
  606. err = fmt.Errorf("柱方图未配置")
  607. errMsg = "柱方图未配置"
  608. return
  609. }
  610. err = json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  611. if err != nil {
  612. err = fmt.Errorf("柱方图配置异常 json.Unmarshal Err:%s", err.Error())
  613. errMsg = "柱方图配置异常"
  614. return
  615. }
  616. extraConfigStr = chartInfo.BarConfig
  617. }
  618. // 获取表格数据
  619. excelChartInfoDataShow := new(ExcelChartInfoDataShow)
  620. excelChartInfoDataShow.DataListMap = dataListMap
  621. if chartInfo.HaveOperaAuth {
  622. // 获取图表中的指标数据
  623. edbList, xEdbIdValue, yDataList, dataResp, e, msg := GetChartEdbDataV2(chartInfoId, chartType, calendar, startDate, endDate, mappingList, extraConfigStr, chartInfo.SeasonExtraConfig, excelChartInfoDataShow)
  624. if e != nil {
  625. err = fmt.Errorf("获取图表,指标数据失败,Err:%s", e.Error())
  626. errMsg = msg
  627. return
  628. }
  629. // 单位
  630. if chartType == utils.CHART_TYPE_BAR && len(yDataList) > 0 {
  631. chartInfo.Unit = yDataList[0].Unit
  632. chartInfo.UnitEn = yDataList[0].UnitEn
  633. }
  634. warnEdbList := make([]string, 0)
  635. for _, v := range edbList {
  636. if v.IsNullData {
  637. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  638. }
  639. }
  640. if len(warnEdbList) > 0 {
  641. chartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  642. }
  643. if chartInfoId > 0 && chartInfo != nil {
  644. //判断是否加入我的图库
  645. {
  646. var myChartCondition string
  647. var myChartPars []interface{}
  648. myChartCondition += ` AND a.admin_id=? `
  649. myChartPars = append(myChartPars, sysUser.AdminId)
  650. myChartCondition += ` AND a.chart_info_id=? `
  651. myChartPars = append(myChartPars, chartInfo.ChartInfoId)
  652. myChartList, e := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
  653. if e != nil && e.Error() != utils.ErrNoRow() {
  654. errMsg = "获取失败"
  655. err = fmt.Errorf("获取我的图表信息失败,GetMyChartByCondition,Err:" + e.Error())
  656. return
  657. }
  658. if myChartList != nil && len(myChartList) > 0 {
  659. chartInfo.IsAdd = true
  660. chartInfo.MyChartId = myChartList[0].MyChartId
  661. chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
  662. }
  663. }
  664. }
  665. //判断是否需要展示英文标识
  666. chartInfo.IsEnChart = CheckIsEnChart(chartInfo.ChartNameEn, edbList, chartInfo.Source, chartInfo.ChartType)
  667. // 图表的指标来源
  668. sourceNameList, sourceNameEnList := GetEdbSourceByEdbInfoIdList(edbList)
  669. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  670. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  671. // todo 指标权限
  672. {
  673. for _, item := range edbList {
  674. // 数据权限
  675. item.HaveOperaAuth = true
  676. }
  677. }
  678. resp.EdbInfoList = edbList
  679. resp.XEdbIdValue = xEdbIdValue
  680. resp.YDataList = yDataList
  681. resp.DataResp = dataResp
  682. } else {
  683. resp.EdbInfoList = mappingList
  684. }
  685. //图表操作权限
  686. chartInfo.IsEdit = CheckOpChartPermission(sysUser, chartInfo.SysUserId, chartInfo.HaveOperaAuth)
  687. chartInfo.Button = data_manage.ChartViewButton{
  688. IsEdit: chartInfo.IsEdit,
  689. IsEnChart: chartInfo.IsEnChart,
  690. IsAdd: chartInfo.IsAdd,
  691. IsCopy: true,
  692. IsSetName: chartInfo.IsSetName,
  693. }
  694. resp.ChartInfo = chartInfo
  695. resp.BarChartInfo = barConfig
  696. return
  697. }
  698. // GetBalanceExcelEdbDataMapList 获取指标最后的基础数据
  699. func GetBalanceExcelEdbDataMapList(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*data_manage.ChartEdbInfoMapping, seasonExtraConfig string, dataListMap map[int][]*data_manage.EdbDataList) (edbDataListMap map[int][]*data_manage.EdbDataList, edbList []*data_manage.ChartEdbInfoMapping, err error) {
  700. // 指标对应的所有数据
  701. edbDataListMap = make(map[int][]*data_manage.EdbDataList)
  702. for _, v := range mappingList {
  703. //fmt.Println("v:", v.EdbInfoId)
  704. item := new(data_manage.ChartEdbInfoMapping)
  705. item.EdbInfoId = v.EdbInfoId
  706. item.SourceName = v.SourceName
  707. item.Source = v.Source
  708. item.EdbCode = v.EdbCode
  709. item.EdbName = v.EdbName
  710. item.EdbNameEn = v.EdbNameEn
  711. item.Frequency = v.Frequency
  712. item.EdbType = v.EdbType
  713. item.FrequencyEn = GetFrequencyEn(v.Frequency)
  714. if v.Unit != `无` {
  715. item.Unit = v.Unit
  716. }
  717. item.UnitEn = v.UnitEn
  718. item.StartDate = v.StartDate
  719. item.EndDate = v.EndDate
  720. item.ModifyTime = v.ModifyTime
  721. item.EdbInfoCategoryType = v.EdbInfoCategoryType
  722. item.PredictChartColor = v.PredictChartColor
  723. item.ClassifyId = v.ClassifyId
  724. if chartInfoId <= 0 {
  725. item.IsAxis = 1
  726. item.LeadValue = 0
  727. item.LeadUnit = ""
  728. item.ChartEdbMappingId = 0
  729. item.ChartInfoId = 0
  730. item.IsOrder = false
  731. item.EdbInfoType = 1
  732. item.ChartStyle = ""
  733. item.ChartColor = ""
  734. item.ChartWidth = 0
  735. item.MaxData = v.MaxValue
  736. item.MinData = v.MinValue
  737. } else {
  738. item.IsAxis = v.IsAxis
  739. item.EdbInfoType = v.EdbInfoType
  740. item.LeadValue = v.LeadValue
  741. item.LeadUnit = v.LeadUnit
  742. item.LeadUnitEn = GetLeadUnitEn(v.LeadUnit)
  743. item.ChartEdbMappingId = v.ChartEdbMappingId
  744. item.ChartInfoId = v.ChartInfoId
  745. item.ChartStyle = v.ChartStyle
  746. item.ChartColor = v.ChartColor
  747. item.ChartWidth = v.ChartWidth
  748. item.IsOrder = v.IsOrder
  749. item.MaxData = v.MaxData
  750. item.MinData = v.MinData
  751. }
  752. item.LatestValue = v.LatestValue
  753. item.LatestDate = v.LatestDate
  754. item.UniqueCode = v.UniqueCode
  755. item.MoveLatestDate = v.LatestDate
  756. item.EdbAliasName = v.EdbAliasName
  757. item.IsConvert = v.IsConvert
  758. item.ConvertType = v.ConvertType
  759. item.ConvertValue = v.ConvertValue
  760. item.ConvertUnit = v.ConvertUnit
  761. item.ConvertEnUnit = v.ConvertEnUnit
  762. item.IsJoinPermission = v.IsJoinPermission
  763. var startDateReal string
  764. var diffSeconds int64
  765. if chartType == 2 { //季节性图
  766. startDateReal = startDate
  767. } else {
  768. if v.EdbInfoType == 0 && v.LeadUnit != "" && v.LeadValue > 0 { //领先指标
  769. var startTimeRealTemp time.Time
  770. startDateParse, _ := time.Parse(utils.FormatDate, startDate)
  771. switch v.LeadUnit {
  772. case "天":
  773. startTimeRealTemp = startDateParse.AddDate(0, 0, -v.LeadValue)
  774. case "月":
  775. startTimeRealTemp = startDateParse.AddDate(0, -v.LeadValue, 0)
  776. case "季":
  777. startTimeRealTemp = startDateParse.AddDate(0, -3*v.LeadValue, 0)
  778. case "周":
  779. startTimeRealTemp = startDateParse.AddDate(0, 0, -7*v.LeadValue)
  780. case "年":
  781. startTimeRealTemp = startDateParse.AddDate(-v.LeadValue, 0, 0)
  782. }
  783. if startTimeRealTemp.Before(startDateParse) {
  784. startDateReal = startTimeRealTemp.Format(utils.FormatDate)
  785. diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6
  786. } else {
  787. startDateReal = startDate
  788. diffSeconds = 0
  789. }
  790. // 预测指标的开始日期也要偏移
  791. {
  792. day, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, startDate, startDateReal)
  793. if tmpErr != nil {
  794. err = tmpErr
  795. return
  796. }
  797. moveLatestDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, item.MoveLatestDate, time.Local)
  798. if tmpErr != nil {
  799. err = tmpErr
  800. return
  801. }
  802. item.MoveLatestDate = moveLatestDateTime.AddDate(0, 0, day).Format(utils.FormatDate)
  803. }
  804. } else {
  805. startDateReal = startDate
  806. }
  807. }
  808. //fmt.Println("line 1011 chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  809. calendarPreYear := 0
  810. if calendar == "农历" {
  811. newStartDateReal, err := time.Parse(utils.FormatDate, startDateReal)
  812. if err != nil {
  813. fmt.Println("time.Parse:" + err.Error())
  814. }
  815. calendarPreYear = newStartDateReal.Year() - 1
  816. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  817. startDateReal = newStartDateReal.Format(utils.FormatDate)
  818. }
  819. dataList := make([]*data_manage.EdbDataList, 0)
  820. dataListTmp, ok := dataListMap[v.EdbInfoId]
  821. if ok {
  822. dataList = dataListTmp
  823. } else {
  824. err = errors.New(fmt.Sprint("获取失败,指标类型异常", v.EdbInfoId))
  825. return
  826. }
  827. if v.IsConvert == 1 {
  828. switch v.ConvertType {
  829. case 1:
  830. for i, data := range dataList {
  831. dataList[i].Value = data.Value * v.ConvertValue
  832. }
  833. //item.MaxData = item.MaxData * v.ConvertValue
  834. //item.MinData = item.MinData * v.ConvertValue
  835. case 2:
  836. for i, data := range dataList {
  837. dataList[i].Value = data.Value / v.ConvertValue
  838. }
  839. //item.MaxData = item.MaxData / v.ConvertValue
  840. //item.MinData = item.MinData / v.ConvertValue
  841. case 3:
  842. for i, data := range dataList {
  843. if data.Value <= 0 {
  844. err = errors.New("数据中含有负数或0,无法对数运算")
  845. return
  846. }
  847. dataList[i].Value = math.Log(data.Value) / math.Log(v.ConvertValue)
  848. }
  849. //item.MaxData = math.Log(item.MaxData) / math.Log(v.ConvertValue)
  850. //item.MinData = math.Log(item.MinData) / math.Log(v.ConvertValue)
  851. }
  852. }
  853. edbDataListMap[v.EdbInfoId] = dataList
  854. if diffSeconds != 0 && v.EdbInfoType == 0 {
  855. dataListLen := len(dataList)
  856. for i := 0; i < dataListLen; i++ {
  857. dataList[i].DataTimestamp = dataList[i].DataTimestamp - diffSeconds
  858. }
  859. }
  860. if chartType == 2 {
  861. latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
  862. if tmpErr != nil {
  863. //item.DataList = dataList
  864. item.IsNullData = true
  865. edbList = append(edbList, item)
  866. continue
  867. }
  868. if calendar == "农历" {
  869. if len(dataList) <= 0 {
  870. result := new(data_manage.EdbDataResult)
  871. item.DataList = result
  872. } else {
  873. result, tmpErr := data_manage.AddCalculateQuarterV6(dataList)
  874. if tmpErr != nil {
  875. err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
  876. return
  877. }
  878. quarterDataList, tErr := GetSeasonEdbInfoDataListByXDateNong(result, latestDate, seasonExtraConfig, calendarPreYear)
  879. if tErr != nil {
  880. err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
  881. return
  882. }
  883. item.DataList = quarterDataList
  884. }
  885. } else {
  886. quarterDataList, tErr := GetSeasonEdbInfoDataListByXDate(dataList, latestDate, seasonExtraConfig)
  887. if tErr != nil {
  888. err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
  889. return
  890. }
  891. item.DataList = quarterDataList
  892. }
  893. } else if chartType == 7 || chartType == utils.CHART_TYPE_RADAR { //柱方图
  894. //item.DataList = dataList
  895. } else {
  896. item.DataList = dataList
  897. }
  898. edbList = append(edbList, item)
  899. }
  900. return
  901. }
  902. func CheckBalanceChartCacheAndPermission(chartInfo *data_manage.ChartInfoView, isCache bool, sysUser *system.Admin) (resp *data_manage.ChartInfoDetailFromUniqueCodeResp, isOk bool, msg, errMsg string) {
  903. //判断是否存在缓存,如果存在缓存,那么直接从缓存中获取
  904. adminId := sysUser.AdminId
  905. // todo 图表数据权限
  906. // 已授权分类id
  907. permissionChartIdList, permissionClassifyIdList, err := data_manage_permission.GetUserChartAndClassifyPermissionList(adminId, chartInfo.ChartInfoId, chartInfo.ChartClassifyId)
  908. if err != nil {
  909. errMsg = "获取已授权分类id数据失败,Err:" + err.Error()
  910. return
  911. }
  912. defer func() {
  913. if isOk {
  914. // 图表权限校验
  915. {
  916. resp.ChartInfo.HaveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(chartInfo.IsJoinPermission, 0, chartInfo.ChartInfoId, chartInfo.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
  917. //图表操作权限
  918. chartInfo.IsEdit = CheckOpChartPermission(sysUser, chartInfo.SysUserId, resp.ChartInfo.HaveOperaAuth)
  919. chartInfo.Button = data_manage.ChartViewButton{
  920. IsEdit: chartInfo.IsEdit,
  921. IsEnChart: chartInfo.IsEnChart,
  922. IsAdd: chartInfo.IsAdd,
  923. IsCopy: true,
  924. IsSetName: chartInfo.IsSetName,
  925. }
  926. //if !resp.ChartInfo.HaveOperaAuth {
  927. // for _, v := range resp.EdbInfoList {
  928. // v.DataList = nil
  929. // }
  930. // resp.DataResp = nil
  931. // resp.XEdbIdValue = []int{}
  932. // resp.YDataList = []data_manage.YData{}
  933. //}
  934. }
  935. // todo 是否需要返回平衡表指标权限即判断平衡表的表格数据权限
  936. {
  937. // 查询图表关联的表格ID
  938. excelInfo, e := excelModel.GetExcelInfoByChartInfoId(chartInfo.ChartInfoId)
  939. if e != nil {
  940. errMsg = "获取图表关联的表格ID失败,Err:" + e.Error()
  941. err = e
  942. return
  943. }
  944. checkExcelInfo := excelInfo
  945. if excelInfo.Source == utils.BALANCE_TABLE {
  946. checkExcelInfoId := excelInfo.ExcelInfoId
  947. if excelInfo.BalanceType == 1 {
  948. checkExcelInfoId = excelInfo.RelExcelInfoId
  949. } else {
  950. if excelInfo.ParentId > 0 {
  951. checkExcelInfoId = excelInfo.ParentId
  952. }
  953. }
  954. if checkExcelInfoId != excelInfo.ExcelInfoId {
  955. checkExcelInfo, err = excelModel.GetExcelInfoById(excelInfo.ExcelInfoId)
  956. if err != nil {
  957. errMsg = "获取平衡表格信息失败"
  958. err = errors.New("获取平衡表格信息失败,Err:" + err.Error())
  959. return
  960. }
  961. }
  962. }
  963. haveOperaAuth, e := data_manage_permission.CheckExcelPermissionByExcelInfoId(checkExcelInfo.ExcelInfoId, checkExcelInfo.ExcelClassifyId, checkExcelInfo.IsJoinPermission, adminId)
  964. if e != nil {
  965. errMsg = "获取平衡表格数据权限失败"
  966. err = errors.New("获取表格权限信息失败,Err" + e.Error())
  967. return
  968. }
  969. // 数据权限
  970. for _, v := range resp.EdbInfoList {
  971. v.HaveOperaAuth = haveOperaAuth
  972. }
  973. }
  974. }
  975. }()
  976. key := GetChartInfoDataKey(chartInfo.ChartInfoId)
  977. if utils.Re == nil && isCache {
  978. if utils.Re == nil && utils.Rc.IsExist(key) {
  979. if redisData, err1 := utils.Rc.RedisBytes(key); err1 == nil {
  980. err := json.Unmarshal(redisData, &resp)
  981. if err != nil || resp == nil {
  982. return
  983. }
  984. // 这里跟当前用户相关的信息重新查询写入resp, 不使用缓存中的
  985. var myCond string
  986. var myPars []interface{}
  987. myCond += ` AND a.admin_id=? `
  988. myPars = append(myPars, adminId)
  989. myCond += ` AND a.chart_info_id=? `
  990. myPars = append(myPars, chartInfo.ChartInfoId)
  991. myList, err := data_manage.GetMyChartByCondition(myCond, myPars)
  992. if err != nil && err.Error() != utils.ErrNoRow() {
  993. msg = "获取失败"
  994. errMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  995. return
  996. }
  997. resp.ChartInfo.IsAdd = false
  998. resp.ChartInfo.MyChartId = 0
  999. resp.ChartInfo.MyChartClassifyId = ""
  1000. if myList != nil && len(myList) > 0 {
  1001. resp.ChartInfo.IsAdd = true
  1002. resp.ChartInfo.MyChartId = myList[0].MyChartId
  1003. resp.ChartInfo.MyChartClassifyId = myList[0].MyChartClassifyId
  1004. }
  1005. //判断是否加入我的图库
  1006. {
  1007. var myChartCondition string
  1008. var myChartPars []interface{}
  1009. myChartCondition += ` AND a.admin_id=? `
  1010. myChartPars = append(myChartPars, adminId)
  1011. myChartCondition += ` AND a.chart_info_id=? `
  1012. myChartPars = append(myChartPars, chartInfo.ChartInfoId)
  1013. myChartList, err := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
  1014. if err != nil && err.Error() != utils.ErrNoRow() {
  1015. msg = "获取失败"
  1016. errMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  1017. return
  1018. }
  1019. if myChartList != nil && len(myChartList) > 0 {
  1020. chartInfo.IsAdd = true
  1021. chartInfo.MyChartId = myChartList[0].MyChartId
  1022. chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
  1023. }
  1024. }
  1025. isOk = true
  1026. fmt.Println("source redis")
  1027. return
  1028. }
  1029. }
  1030. }
  1031. return
  1032. }
  1033. // GetBalanceExcelSeasonChartLegendPreview 获取预览的季节性图例
  1034. func GetBalanceExcelSeasonChartLegendPreview(dataList []*data_manage.EdbDataList, calendar string, seasonExtraConfigReq data_manage.SeasonExtraItem) (quarterDataList data_manage.QuarterDataList, err error, errMsg string) {
  1035. seasonExtraConfig := ""
  1036. if seasonExtraConfigReq.XEndDate != "" {
  1037. if seasonExtraConfigReq.XStartDate > seasonExtraConfigReq.XEndDate && seasonExtraConfigReq.JumpYear != 1 {
  1038. errMsg = "季节性图表配置信息异常:横坐标日期配置错误"
  1039. err = fmt.Errorf("季节性图表配置信息异常: 横坐标日期配置错误")
  1040. return
  1041. }
  1042. seasonExtra, tErr := json.Marshal(seasonExtraConfigReq)
  1043. if tErr != nil {
  1044. errMsg = "季节性图表配置信息异常"
  1045. err = fmt.Errorf("季节性图表配置信息异常,Err:" + tErr.Error())
  1046. return
  1047. }
  1048. seasonExtraConfig = string(seasonExtra)
  1049. }
  1050. var latestDate time.Time //最新日期
  1051. startDate, endDate, _, _ := getBalanceDataListStartDateAndValue(dataList)
  1052. calendarPreYear := 0
  1053. startDateReal := startDate
  1054. latestDate, _ = time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  1055. if calendar == "农历" {
  1056. newStartDateReal, e := time.Parse(utils.FormatDate, startDateReal)
  1057. if e != nil {
  1058. err = fmt.Errorf("开始时间解析失败 time.Parse:" + e.Error())
  1059. return
  1060. }
  1061. calendarPreYear = newStartDateReal.Year() - 1
  1062. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  1063. startDateReal = newStartDateReal.Format(utils.FormatDate)
  1064. }
  1065. if calendar == "农历" {
  1066. if len(dataList) <= 0 {
  1067. return
  1068. } else {
  1069. result, tmpErr := data_manage.AddCalculateQuarterV6(dataList)
  1070. if tmpErr != nil {
  1071. err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
  1072. return
  1073. }
  1074. quarterDataList, err = GetSeasonEdbInfoDataListByXDateNong(result, latestDate, seasonExtraConfig, calendarPreYear)
  1075. if err != nil {
  1076. err = errors.New("获取季节性图表数据失败,Err:" + err.Error())
  1077. return
  1078. }
  1079. }
  1080. } else {
  1081. quarterDataList, err = GetSeasonEdbInfoDataListByXDate(dataList, latestDate, seasonExtraConfig)
  1082. if err != nil {
  1083. err = errors.New("获取季节性图表数据失败,Err:" + err.Error())
  1084. return
  1085. }
  1086. }
  1087. return
  1088. }
  1089. func getBalanceDataListStartDateAndValue(dataList []*data_manage.EdbDataList) (startDate, endDate string, startVal, endVal float64) {
  1090. if len(dataList) == 0 {
  1091. return
  1092. }
  1093. startDate = dataList[0].DataTime
  1094. startVal = dataList[0].Value
  1095. endDate = dataList[len(dataList)-1].DataTime
  1096. endVal = dataList[len(dataList)-1].Value
  1097. for _, v := range dataList {
  1098. if v.DataTime < startDate {
  1099. startDate = v.DataTime
  1100. startVal = v.Value
  1101. }
  1102. if v.DataTime > endDate {
  1103. endDate = v.DataTime
  1104. endVal = v.Value
  1105. }
  1106. }
  1107. return
  1108. }
  1109. // AddBalanceStaticExcel 另存为和存为静态表以及子表的复制都调用该接口
  1110. func AddBalanceStaticExcel(oldExcelInfo *excelModel.ExcelInfo, versionName string, sysUser *system.Admin, parentId, relExcelInfoId, balanceType int, childExcelList []*excelModel.ExcelInfo) (excelInfo *excelModel.ExcelInfo, err error, errMsg string, isSendEmail bool) {
  1111. isSendEmail = true
  1112. versionName = utils.TrimLRStr(versionName)
  1113. // todo 操作权限校验
  1114. {
  1115. // 数据权限
  1116. /*haveOperaAuth, tmpErr := data_manage_permission.CheckExcelPermissionByExcelInfoId(oldExcelInfo.ExcelInfoId, oldExcelInfo.ExcelClassifyId, oldExcelInfo.IsJoinPermission, sysUser.AdminId)
  1117. if err != nil {
  1118. errMsg = "获取ETA表格权限失败"
  1119. err = errors.New("获取ETA表格权限失败,Err:" + tmpErr.Error())
  1120. return
  1121. }
  1122. button := GetExcelInfoOpButton(sysUser, oldExcelInfo.SysUserId, oldExcelInfo.Source, haveOperaAuth)
  1123. if !button.CopyButton {
  1124. errMsg = "无操作权限"
  1125. err = errors.New(errMsg)
  1126. isSendEmail = false
  1127. return
  1128. }*/
  1129. }
  1130. // 检验分类下是否存在该表格名称
  1131. {
  1132. var condition string
  1133. var pars []interface{}
  1134. condition += " AND rel_excel_info_id=? "
  1135. pars = append(pars, oldExcelInfo.ExcelInfoId)
  1136. condition += " AND version_name=? "
  1137. pars = append(pars, versionName)
  1138. count, tmpErr := excelModel.GetExcelInfoCountByCondition(condition, pars)
  1139. if tmpErr != nil {
  1140. errMsg = "查询版本号失败"
  1141. err = tmpErr
  1142. return
  1143. }
  1144. if count > 0 {
  1145. errMsg = "表格版本号已存在,请重新填写表格名称"
  1146. err = errors.New(errMsg)
  1147. isSendEmail = false
  1148. return
  1149. }
  1150. }
  1151. // 表格信息
  1152. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  1153. excelName := oldExcelInfo.ExcelName
  1154. if parentId == 0 {
  1155. excelName += "(" + versionName + ")"
  1156. }
  1157. // 处理表格内容, 静态表去除动态指标关联
  1158. content := oldExcelInfo.Content
  1159. excelInfo = &excelModel.ExcelInfo{
  1160. //ExcelInfoId: 0,
  1161. ExcelName: excelName,
  1162. Source: oldExcelInfo.Source,
  1163. ExcelType: oldExcelInfo.ExcelType,
  1164. UniqueCode: utils.MD5(utils.EXCEL_DATA_PREFIX + "_" + timestamp),
  1165. ExcelClassifyId: oldExcelInfo.ExcelClassifyId,
  1166. SysUserId: sysUser.AdminId,
  1167. SysUserRealName: sysUser.RealName,
  1168. Content: content,
  1169. ExcelImage: oldExcelInfo.ExcelImage,
  1170. FileUrl: oldExcelInfo.FileUrl,
  1171. ParentId: parentId,
  1172. RelExcelInfoId: relExcelInfoId,
  1173. VersionName: versionName,
  1174. UpdateUserId: sysUser.AdminId,
  1175. UpdateUserRealName: sysUser.AdminName,
  1176. BalanceType: balanceType,
  1177. Sort: oldExcelInfo.Sort,
  1178. IsDelete: 0,
  1179. ModifyTime: time.Now(),
  1180. CreateTime: time.Now(),
  1181. }
  1182. excelEdbMappingList := make([]*excelModel.ExcelEdbMapping, 0)
  1183. if balanceType == 1 {
  1184. // 获取excel与指标的关系表
  1185. excelEdbMappingList, err = excelModel.GetAllExcelEdbMappingByExcelInfoId(excelInfo.ExcelInfoId)
  1186. if err != nil {
  1187. errMsg = "获取失败"
  1188. return
  1189. }
  1190. for k, v := range excelEdbMappingList {
  1191. v.ExcelEdbMappingId = 0
  1192. v.ExcelInfoId = 0
  1193. excelEdbMappingList[k] = v
  1194. }
  1195. }
  1196. var childExcel *excelModel.ExcelInfo
  1197. err = excelModel.AddExcelInfo(excelInfo, excelEdbMappingList, childExcel)
  1198. if err != nil {
  1199. errMsg = "保存失败"
  1200. return
  1201. }
  1202. if parentId == 0 && len(childExcelList) > 0 {
  1203. for _, childExcelInfo := range childExcelList {
  1204. _, err, errMsg, isSendEmail = AddBalanceStaticExcel(childExcelInfo, versionName, sysUser, excelInfo.ExcelInfoId, relExcelInfoId, balanceType, []*excelModel.ExcelInfo{})
  1205. if err != nil {
  1206. return
  1207. }
  1208. }
  1209. } else if parentId > 0 {
  1210. // 如果复制的是动态表的子表,则同步复制关联的图表
  1211. // 查出所有的chart_list, 同步复制图表和图表指标
  1212. // 相关联指标
  1213. mappingListTmp, e := excelModel.GetExcelChartEdbMappingByExcelInfoId(oldExcelInfo.ExcelInfoId)
  1214. if e != nil {
  1215. errMsg = "获取图表指标失败"
  1216. err = fmt.Errorf(" 获取图表,指标信息失败 Err:%s", e.Error())
  1217. return
  1218. }
  1219. mappingListMap := make(map[int][]*excelModel.ExcelChartEdb, 0)
  1220. charInfoIds := make([]int, 0)
  1221. for k, _ := range mappingListTmp {
  1222. mappingListTmp[k].ExcelInfoId = excelInfo.ExcelInfoId
  1223. mappingListMap[mappingListTmp[k].ChartInfoId] = append(mappingListMap[mappingListTmp[k].ChartInfoId], mappingListTmp[k])
  1224. }
  1225. for k, _ := range mappingListMap {
  1226. charInfoIds = append(charInfoIds, k)
  1227. }
  1228. if len(charInfoIds) > 0 {
  1229. chartInfoList, e := data_manage.GetChartInfoViewByIdList(charInfoIds)
  1230. if e != nil {
  1231. errMsg = "获取失败"
  1232. err = fmt.Errorf(" 获取图表,指标信息失败 Err:%s", e.Error())
  1233. return
  1234. }
  1235. for _, chartInfo := range chartInfoList {
  1236. mappingList, ok := mappingListMap[chartInfo.ChartInfoId]
  1237. if !ok {
  1238. err = fmt.Errorf("未找到图表关联的指标信息")
  1239. return
  1240. }
  1241. err, errMsg, isSendEmail = CopyBalanceExcelChart(chartInfo, mappingList, versionName, sysUser)
  1242. if err != nil {
  1243. err = fmt.Errorf("复制图表失败 Err:%s", err.Error())
  1244. return
  1245. }
  1246. }
  1247. }
  1248. }
  1249. return
  1250. }
  1251. // CopyBalanceExcelChart 复制平衡表图表
  1252. func CopyBalanceExcelChart(oldChartInfo *data_manage.ChartInfoView, oldChartEdbList []*excelModel.ExcelChartEdb, versionName string, sysUser *system.Admin) (err error, errMsg string, isSendEmail bool) {
  1253. chartEdbList := make([]*excelModel.AddChartEdbAndDataItem, 0)
  1254. if versionName != "" {
  1255. oldChartInfo.ChartName += "(" + versionName + ")"
  1256. }
  1257. for _, chartEdb := range oldChartEdbList {
  1258. excelEdb := new(excelModel.ExcelChartEdb)
  1259. excelEdb.EdbName = chartEdb.EdbName
  1260. if versionName != "" {
  1261. excelEdb.EdbName += "(" + versionName + ")"
  1262. }
  1263. randStr := utils.GetRandDigit(4)
  1264. excelEdb.EdbCode = `T` + time.Now().Format("060102150405") + "_" + randStr
  1265. excelEdb.ExcelInfoId = chartEdb.ExcelInfoId
  1266. excelEdb.DateSequence = chartEdb.DateSequence
  1267. excelEdb.DataSequence = chartEdb.DataSequence
  1268. excelEdb.SysUserId = sysUser.AdminId
  1269. excelEdb.SysUserRealName = sysUser.RealName
  1270. excelEdb.MaxData = chartEdb.MaxData
  1271. excelEdb.MinData = chartEdb.MinData
  1272. excelEdb.IsOrder = chartEdb.IsOrder
  1273. excelEdb.IsAxis = chartEdb.IsAxis
  1274. excelEdb.FromTag = chartEdb.FromTag
  1275. excelEdb.EdbInfoType = chartEdb.EdbInfoType
  1276. excelEdb.LeadValue = chartEdb.LeadValue
  1277. excelEdb.LeadUnit = chartEdb.LeadUnit
  1278. excelEdb.CreateTime = time.Now()
  1279. excelEdb.ModifyTime = time.Now()
  1280. var dateList []string
  1281. var dataList []float64
  1282. // todo 是否需要存储静态表的数据
  1283. // 处理日期列表和值列表
  1284. addItem := &excelModel.AddChartEdbAndDataItem{
  1285. ChartEdb: excelEdb,
  1286. DateList: dateList,
  1287. ValList: dataList,
  1288. }
  1289. chartEdbList = append(chartEdbList, addItem)
  1290. }
  1291. chartInfo, err, errMsg, isSendEmail := copyBalanceExcelChart(oldChartInfo, sysUser.AdminId, sysUser.RealName)
  1292. if err != nil {
  1293. errMsg = "新增图表失败!"
  1294. err = fmt.Errorf("新增图表失败!%s, %s", errMsg, err.Error())
  1295. return
  1296. }
  1297. obj := new(excelModel.ExcelChartEdb)
  1298. err = obj.AddChartEdbAndData(chartEdbList, chartInfo, []int{})
  1299. if err != nil {
  1300. errMsg = "新增图表失败!"
  1301. err = fmt.Errorf("新增图表数据失败!%s", err.Error())
  1302. return
  1303. }
  1304. return
  1305. }
  1306. func copyBalanceExcelChart(oldChartInfo *data_manage.ChartInfoView, sysUserId int, sysUserRealName string) (chartInfo *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  1307. isSendEmail = true // 默认错误的时候要发送邮件
  1308. oldChartInfo.ChartName = strings.Trim(oldChartInfo.ChartName, " ")
  1309. if oldChartInfo.ChartName == "" {
  1310. errMsg = "请填写图表名称!"
  1311. err = fmt.Errorf(errMsg)
  1312. isSendEmail = false
  1313. return
  1314. }
  1315. //判断图表是否存在
  1316. var condition string
  1317. var pars []interface{}
  1318. // 图表名称在不同图分类下不允许重复 需求调整时间:2022年11月07日09:47:07
  1319. condition += " AND chart_classify_id=0 "
  1320. condition += " AND chart_name=? AND source = ? "
  1321. pars = append(pars, oldChartInfo.ChartName, utils.CHART_SOURCE_BALANCE_EXCEL)
  1322. count, err := data_manage.GetChartInfoCountByCondition(condition, pars)
  1323. if err != nil {
  1324. errMsg = "判断图表名称是否存在失败"
  1325. err = fmt.Errorf("判断图表名称是否存在失败,Err:" + err.Error())
  1326. return
  1327. }
  1328. if count > 0 {
  1329. /*errMsg = "图表已存在,请重新填写"
  1330. err = fmt.Errorf("判断图表名称是否存在失败")
  1331. isSendEmail = false
  1332. return*/
  1333. oldChartInfo.ChartName += " " + strconv.Itoa(count)
  1334. }
  1335. chartInfo = new(data_manage.ChartInfo)
  1336. chartInfo.ChartName = oldChartInfo.ChartName
  1337. //chartInfo.EdbInfoIds = edbInfoIdStr
  1338. //chartInfo.ChartClassifyId = req.ChartClassifyId
  1339. chartInfo.SysUserId = sysUserId
  1340. chartInfo.SysUserRealName = sysUserRealName
  1341. chartInfo.CreateTime = time.Now()
  1342. chartInfo.ModifyTime = time.Now()
  1343. chartInfo.IsSetName = 0
  1344. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  1345. chartInfo.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
  1346. // todo 判断是否需要重新计算用户的start_date
  1347. chartInfo.DateType = oldChartInfo.DateType
  1348. chartInfo.ChartType = oldChartInfo.ChartType
  1349. chartInfo.Calendar = oldChartInfo.Calendar
  1350. /* chartInfo.StartDate = req.StartDate
  1351. chartInfo.EndDate = req.EndDate
  1352. chartInfo.SeasonStartDate = req.StartDate
  1353. chartInfo.SeasonEndDate = req.EndDate*/
  1354. chartInfo.LeftMin = oldChartInfo.LeftMin
  1355. chartInfo.LeftMax = oldChartInfo.LeftMax
  1356. chartInfo.RightMin = oldChartInfo.RightMin
  1357. chartInfo.RightMax = oldChartInfo.RightMax
  1358. chartInfo.Right2Min = oldChartInfo.Right2Min
  1359. chartInfo.Right2Max = oldChartInfo.Right2Max
  1360. chartInfo.MinMaxSave = oldChartInfo.MinMaxSave
  1361. //chartInfo.Disabled = disableVal
  1362. //chartInfo.BarConfig = barChartConf
  1363. chartInfo.ExtraConfig = oldChartInfo.ExtraConfig
  1364. chartInfo.SeasonExtraConfig = oldChartInfo.SeasonExtraConfig
  1365. //chartInfo.StartYear = req.StartYear
  1366. chartInfo.Source = utils.CHART_SOURCE_BALANCE_EXCEL
  1367. chartInfo.ChartThemeId = oldChartInfo.ChartThemeId
  1368. chartInfo.SourcesFrom = oldChartInfo.SourcesFrom
  1369. /* chartInfo.Instructions = req.Instructions
  1370. chartInfo.MarkersLines = req.MarkersLines
  1371. chartInfo.MarkersAreas = req.MarkersAreas
  1372. chartInfo.Unit = req.Unit
  1373. chartInfo.UnitEn = req.UnitEn*/
  1374. /*newId, err := data_manage.AddChartInfo(chartInfo)
  1375. if err != nil {
  1376. errMsg = `保存失败`
  1377. err = fmt.Errorf("保存失败,Err:" + err.Error())
  1378. return
  1379. }
  1380. chartInfo.ChartInfoId = int(newId)*/
  1381. //添加es数据
  1382. //go EsAddOrEditChartInfo(chartInfo.ChartInfoId)
  1383. return
  1384. }
  1385. // HandleBalanceExcelContentToStatic 将动态表的内容转化为静态表
  1386. func HandleBalanceExcelContentToStatic(oldContent string) (content string, err error) {
  1387. return
  1388. }