chart_info_excel_balance.go 50 KB

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