chart_info_excel_balance.go 59 KB

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