chart_info_excel_balance.go 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814
  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. var dateMax time.Time
  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.After(dateMax) {
  595. dateMax = lastDateT
  596. }
  597. }
  598. }
  599. }
  600. if chartInfo.ChartType == utils.CHART_TYPE_SEASON && chartInfo.DateType == utils.DateTypeNYears {
  601. dateMax = time.Date(dateMax.Year()+1, 1, 1, 0, 0, 0, 0, time.Local)
  602. }
  603. startDate, endDate = utils.GetDateByDateTypeV2(dateType, startDate, endDate, startYear, dateMax)
  604. if chartInfo.ChartType == 2 {
  605. chartInfo.StartDate = startDate
  606. chartInfo.EndDate = endDate
  607. }
  608. // 图表额外数据参数
  609. extraConfigStr := chartInfo.ExtraConfig
  610. // 柱方图的一些配置
  611. var barConfig data_manage.BarChartInfoReq
  612. if chartInfo != nil && chartInfo.ChartType == 7 {
  613. if chartInfo.BarConfig == `` {
  614. err = fmt.Errorf("柱方图未配置")
  615. errMsg = "柱方图未配置"
  616. return
  617. }
  618. err = json.Unmarshal([]byte(chartInfo.BarConfig), &barConfig)
  619. if err != nil {
  620. err = fmt.Errorf("柱方图配置异常 json.Unmarshal Err:%s", err.Error())
  621. errMsg = "柱方图配置异常"
  622. return
  623. }
  624. extraConfigStr = chartInfo.BarConfig
  625. }
  626. // 获取表格数据
  627. excelChartInfoDataShow := new(ExcelChartInfoDataShow)
  628. excelChartInfoDataShow.DataListMap = dataListMap
  629. if chartInfo.HaveOperaAuth {
  630. // 获取图表中的指标数据
  631. edbList, xEdbIdValue, yDataList, dataResp, e, msg := GetChartEdbDataV2(chartInfoId, chartType, calendar, startDate, endDate, mappingList, extraConfigStr, chartInfo.SeasonExtraConfig, excelChartInfoDataShow)
  632. if e != nil {
  633. err = fmt.Errorf("获取图表,指标数据失败,Err:%s", e.Error())
  634. errMsg = msg
  635. return
  636. }
  637. // 单位
  638. if chartType == utils.CHART_TYPE_BAR && len(yDataList) > 0 {
  639. chartInfo.Unit = yDataList[0].Unit
  640. chartInfo.UnitEn = yDataList[0].UnitEn
  641. }
  642. warnEdbList := make([]string, 0)
  643. for _, v := range edbList {
  644. if v.IsNullData {
  645. warnEdbList = append(warnEdbList, v.EdbName+"("+v.EdbCode+")")
  646. }
  647. }
  648. if len(warnEdbList) > 0 {
  649. chartInfo.WarnMsg = `图表引用指标异常,异常指标:` + strings.Join(warnEdbList, ",")
  650. }
  651. if chartInfoId > 0 && chartInfo != nil {
  652. //判断是否加入我的图库
  653. {
  654. var myChartCondition string
  655. var myChartPars []interface{}
  656. myChartCondition += ` AND a.admin_id=? `
  657. myChartPars = append(myChartPars, sysUser.AdminId)
  658. myChartCondition += ` AND a.chart_info_id=? `
  659. myChartPars = append(myChartPars, chartInfo.ChartInfoId)
  660. myChartList, e := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
  661. if e != nil && e.Error() != utils.ErrNoRow() {
  662. errMsg = "获取失败"
  663. err = fmt.Errorf("获取我的图表信息失败,GetMyChartByCondition,Err:" + e.Error())
  664. return
  665. }
  666. if myChartList != nil && len(myChartList) > 0 {
  667. chartInfo.IsAdd = true
  668. chartInfo.MyChartId = myChartList[0].MyChartId
  669. chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
  670. }
  671. }
  672. }
  673. //判断是否需要展示英文标识
  674. chartInfo.IsEnChart = CheckIsEnChart(chartInfo.ChartNameEn, edbList, chartInfo.Source, chartInfo.ChartType)
  675. // 图表的指标来源
  676. sourceNameList, sourceNameEnList := GetEdbSourceByEdbInfoIdList(edbList)
  677. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  678. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  679. // todo 指标权限
  680. {
  681. for _, item := range edbList {
  682. // 数据权限
  683. item.HaveOperaAuth = true
  684. }
  685. }
  686. resp.EdbInfoList = edbList
  687. resp.XEdbIdValue = xEdbIdValue
  688. resp.YDataList = yDataList
  689. resp.DataResp = dataResp
  690. } else {
  691. resp.EdbInfoList = mappingList
  692. }
  693. // todo 平衡表图表操作权限平衡表表格的操作权限保持一致,前端未用到该按钮权限,故不处理
  694. chartInfo.IsEdit, _ = GetBalanceExcelInfoOpButtonByChartInfoId(sysUser.AdminId, chartInfo.ChartInfoId, chartInfo.HaveOperaAuth)
  695. chartInfo.Button = data_manage.ChartViewButton{
  696. IsEdit: chartInfo.IsEdit,
  697. IsEnChart: chartInfo.IsEnChart,
  698. IsAdd: chartInfo.IsAdd,
  699. IsCopy: true,
  700. IsSetName: chartInfo.IsSetName,
  701. }
  702. resp.ChartInfo = chartInfo
  703. resp.BarChartInfo = barConfig
  704. return
  705. }
  706. // GetBalanceExcelEdbDataMapList 获取指标最后的基础数据
  707. 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) {
  708. // 指标对应的所有数据
  709. edbDataListMap = make(map[int][]*data_manage.EdbDataList)
  710. for _, v := range mappingList {
  711. //fmt.Println("v:", v.EdbInfoId)
  712. item := new(data_manage.ChartEdbInfoMapping)
  713. item.EdbInfoId = v.EdbInfoId
  714. item.SourceName = v.SourceName
  715. item.Source = v.Source
  716. item.EdbCode = v.EdbCode
  717. item.EdbName = v.EdbName
  718. item.EdbNameEn = v.EdbNameEn
  719. item.Frequency = v.Frequency
  720. item.EdbType = v.EdbType
  721. item.FrequencyEn = GetFrequencyEn(v.Frequency)
  722. if v.Unit != `无` {
  723. item.Unit = v.Unit
  724. }
  725. item.UnitEn = v.UnitEn
  726. item.StartDate = v.StartDate
  727. item.EndDate = v.EndDate
  728. item.ModifyTime = v.ModifyTime
  729. item.EdbInfoCategoryType = v.EdbInfoCategoryType
  730. item.PredictChartColor = v.PredictChartColor
  731. item.ClassifyId = v.ClassifyId
  732. if chartInfoId <= 0 {
  733. item.IsAxis = 1
  734. item.LeadValue = 0
  735. item.LeadUnit = ""
  736. item.ChartEdbMappingId = 0
  737. item.ChartInfoId = 0
  738. item.IsOrder = false
  739. item.EdbInfoType = 1
  740. item.ChartStyle = ""
  741. item.ChartColor = ""
  742. item.ChartWidth = 1
  743. item.MaxData = v.MaxValue
  744. item.MinData = v.MinValue
  745. } else {
  746. item.IsAxis = v.IsAxis
  747. item.EdbInfoType = v.EdbInfoType
  748. item.LeadValue = v.LeadValue
  749. item.LeadUnit = v.LeadUnit
  750. item.LeadUnitEn = GetLeadUnitEn(v.LeadUnit)
  751. item.ChartEdbMappingId = v.ChartEdbMappingId
  752. item.ChartInfoId = v.ChartInfoId
  753. item.ChartStyle = v.ChartStyle
  754. item.ChartColor = v.ChartColor
  755. item.ChartWidth = v.ChartWidth
  756. item.IsOrder = v.IsOrder
  757. item.MaxData = v.MaxData
  758. item.MinData = v.MinData
  759. }
  760. item.LatestValue = v.LatestValue
  761. item.LatestDate = v.LatestDate
  762. item.UniqueCode = v.UniqueCode
  763. item.MoveLatestDate = v.LatestDate
  764. item.EdbAliasName = v.EdbAliasName
  765. item.IsConvert = v.IsConvert
  766. item.ConvertType = v.ConvertType
  767. item.ConvertValue = v.ConvertValue
  768. item.ConvertUnit = v.ConvertUnit
  769. item.ConvertEnUnit = v.ConvertEnUnit
  770. item.IsJoinPermission = v.IsJoinPermission
  771. var startDateReal string
  772. var diffSeconds int64
  773. if chartType == 2 { //季节性图
  774. startDateReal = startDate
  775. if len(mappingList) > 1 {
  776. item.IsAxis = v.IsAxis
  777. }
  778. } else {
  779. if v.EdbInfoType == 0 && v.LeadUnit != "" && v.LeadValue > 0 { //领先指标
  780. var startTimeRealTemp time.Time
  781. startDateParse, _ := time.Parse(utils.FormatDate, startDate)
  782. switch v.LeadUnit {
  783. case "天":
  784. startTimeRealTemp = startDateParse.AddDate(0, 0, -v.LeadValue)
  785. case "月":
  786. startTimeRealTemp = startDateParse.AddDate(0, -v.LeadValue, 0)
  787. case "季":
  788. startTimeRealTemp = startDateParse.AddDate(0, -3*v.LeadValue, 0)
  789. case "周":
  790. startTimeRealTemp = startDateParse.AddDate(0, 0, -7*v.LeadValue)
  791. case "年":
  792. startTimeRealTemp = startDateParse.AddDate(-v.LeadValue, 0, 0)
  793. }
  794. if startTimeRealTemp.Before(startDateParse) {
  795. startDateReal = startTimeRealTemp.Format(utils.FormatDate)
  796. diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6
  797. } else {
  798. startDateReal = startDate
  799. diffSeconds = 0
  800. }
  801. // 预测指标的开始日期也要偏移
  802. {
  803. day, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, startDate, startDateReal)
  804. if tmpErr != nil {
  805. err = tmpErr
  806. return
  807. }
  808. moveLatestDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, item.MoveLatestDate, time.Local)
  809. if tmpErr != nil {
  810. err = tmpErr
  811. return
  812. }
  813. item.MoveLatestDate = moveLatestDateTime.AddDate(0, 0, day).Format(utils.FormatDate)
  814. }
  815. } else {
  816. startDateReal = startDate
  817. }
  818. }
  819. //fmt.Println("line 1011 chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  820. calendarPreYear := 0
  821. if calendar == "农历" {
  822. newStartDateReal, e := time.Parse(utils.FormatDate, startDateReal)
  823. if e != nil {
  824. err = fmt.Errorf("时间解析 time.Parse(%s, %s) error: %v", utils.FormatDate, startDateReal, e)
  825. return
  826. }
  827. calendarPreYear = newStartDateReal.Year() - 1
  828. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  829. startDateReal = newStartDateReal.Format(utils.FormatDate)
  830. }
  831. dataList := make([]*data_manage.EdbDataList, 0)
  832. dataListTmp, ok := dataListMap[v.EdbInfoId]
  833. if ok {
  834. dataList = dataListTmp
  835. // 对dataList 根据dataTimestamp 进行排序
  836. sort.Slice(dataList, func(i, j int) bool {
  837. return dataList[i].DataTimestamp < dataList[j].DataTimestamp
  838. })
  839. } else {
  840. //err = errors.New(fmt.Sprint("获取失败,指标类型异常", v.EdbInfoId))
  841. utils.FileLog.Info(fmt.Sprintf("获取失败,指标数据异常 %d", v.EdbInfoId))
  842. }
  843. if v.IsConvert == 1 {
  844. switch v.ConvertType {
  845. case 1:
  846. for i, data := range dataList {
  847. dataList[i].Value = data.Value * v.ConvertValue
  848. }
  849. //item.MaxData = item.MaxData * v.ConvertValue
  850. //item.MinData = item.MinData * v.ConvertValue
  851. case 2:
  852. for i, data := range dataList {
  853. dataList[i].Value = data.Value / v.ConvertValue
  854. }
  855. //item.MaxData = item.MaxData / v.ConvertValue
  856. //item.MinData = item.MinData / v.ConvertValue
  857. case 3:
  858. for i, data := range dataList {
  859. if data.Value <= 0 {
  860. err = errors.New("数据中含有负数或0,无法对数运算")
  861. return
  862. }
  863. dataList[i].Value = math.Log(data.Value) / math.Log(v.ConvertValue)
  864. }
  865. //item.MaxData = math.Log(item.MaxData) / math.Log(v.ConvertValue)
  866. //item.MinData = math.Log(item.MinData) / math.Log(v.ConvertValue)
  867. }
  868. }
  869. edbDataListMap[v.EdbInfoId] = dataList
  870. if diffSeconds != 0 && v.EdbInfoType == 0 {
  871. dataListLen := len(dataList)
  872. for i := 0; i < dataListLen; i++ {
  873. dataList[i].DataTimestamp = dataList[i].DataTimestamp - diffSeconds
  874. }
  875. }
  876. if chartType == 2 && item.IsAxis == 1 {
  877. latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
  878. if tmpErr != nil {
  879. //item.DataList = dataList
  880. item.IsNullData = true
  881. edbList = append(edbList, item)
  882. continue
  883. }
  884. if calendar == "农历" {
  885. if len(dataList) <= 0 {
  886. result := new(data_manage.EdbDataResult)
  887. item.DataList = result
  888. } else {
  889. result, tmpErr := data_manage.AddCalculateQuarterV6(dataList)
  890. if tmpErr != nil {
  891. err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
  892. return
  893. }
  894. quarterDataList, tErr := GetSeasonEdbInfoDataListByXDateNong(result, latestDate, seasonExtraConfig, calendarPreYear)
  895. if tErr != nil {
  896. err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
  897. return
  898. }
  899. item.DataList = quarterDataList
  900. }
  901. } else {
  902. quarterDataList, tErr := GetSeasonEdbInfoDataListByXDate(dataList, latestDate, seasonExtraConfig)
  903. if tErr != nil {
  904. err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
  905. return
  906. }
  907. item.DataList = quarterDataList
  908. }
  909. } else if chartType == 2 && item.IsAxis == 0 {
  910. // 右轴数据处理
  911. xStartDate := "01-01"
  912. jumpYear := 0
  913. var seasonExtra data_manage.SeasonExtraItem
  914. if seasonExtraConfig != "" {
  915. err = json.Unmarshal([]byte(seasonExtraConfig), &seasonExtra)
  916. if err != nil {
  917. return
  918. }
  919. }
  920. if seasonExtra.XStartDate != "" {
  921. xStartDate = seasonExtra.XStartDate
  922. jumpYear = seasonExtra.JumpYear
  923. }
  924. length := len(dataList)
  925. if length == 0 {
  926. return
  927. }
  928. latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
  929. if tmpErr != nil {
  930. //item.DataList = dataList
  931. item.IsNullData = true
  932. edbList = append(edbList, item)
  933. continue
  934. err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + v.LatestDate))
  935. return
  936. }
  937. var rightAxisDate time.Time
  938. if jumpYear == 1 {
  939. latestDate = latestDate.AddDate(-1, 0, 0)
  940. latestDateStr := fmt.Sprintf("%d-%s", latestDate.Year(), xStartDate)
  941. rightAxisDate, err = time.Parse(utils.FormatDate, latestDateStr)
  942. if err != nil {
  943. return
  944. }
  945. }
  946. newDataList := make([]*data_manage.EdbDataList, 0)
  947. for _, v := range dataList {
  948. dataTime, e := time.Parse(utils.FormatDate, v.DataTime)
  949. if e != nil {
  950. err = errors.New("季节性图处理右轴指标数据转换日期失败,Err:" + e.Error())
  951. return
  952. }
  953. if dataTime.Equal(rightAxisDate) || dataTime.After(rightAxisDate) {
  954. newDataList = append(newDataList, v)
  955. }
  956. }
  957. item.DataList = newDataList
  958. } else if chartType == 7 || chartType == utils.CHART_TYPE_RADAR { //柱方图
  959. //item.DataList = dataList
  960. } else {
  961. item.DataList = dataList
  962. }
  963. edbList = append(edbList, item)
  964. }
  965. return
  966. }
  967. func CheckBalanceChartCacheAndPermission(chartInfo *data_manage.ChartInfoView, isCache bool, sysUser *system.Admin) (resp *data_manage.ChartInfoDetailFromUniqueCodeResp, isOk bool, msg, errMsg string) {
  968. //判断是否存在缓存,如果存在缓存,那么直接从缓存中获取
  969. adminId := sysUser.AdminId
  970. // todo 图表数据权限
  971. // 已授权分类id
  972. permissionChartIdList, permissionClassifyIdList, err := data_manage_permission.GetUserChartAndClassifyPermissionList(adminId, chartInfo.ChartInfoId, chartInfo.ChartClassifyId)
  973. if err != nil {
  974. errMsg = "获取已授权分类id数据失败,Err:" + err.Error()
  975. return
  976. }
  977. defer func() {
  978. if isOk {
  979. // 图表权限校验
  980. {
  981. resp.ChartInfo.HaveOperaAuth = data_manage_permission.CheckChartPermissionByPermissionIdList(chartInfo.IsJoinPermission, 0, chartInfo.ChartInfoId, chartInfo.ChartClassifyId, permissionChartIdList, permissionClassifyIdList)
  982. //图表操作权限
  983. chartInfo.IsEdit = CheckOpChartPermission(sysUser, chartInfo.SysUserId, resp.ChartInfo.HaveOperaAuth)
  984. chartInfo.Button = data_manage.ChartViewButton{
  985. IsEdit: chartInfo.IsEdit,
  986. IsEnChart: chartInfo.IsEnChart,
  987. IsAdd: chartInfo.IsAdd,
  988. IsCopy: true,
  989. IsSetName: chartInfo.IsSetName,
  990. }
  991. //if !resp.ChartInfo.HaveOperaAuth {
  992. // for _, v := range resp.EdbInfoList {
  993. // v.DataList = nil
  994. // }
  995. // resp.DataResp = nil
  996. // resp.XEdbIdValue = []int{}
  997. // resp.YDataList = []data_manage.YData{}
  998. //}
  999. }
  1000. // todo 是否需要返回平衡表指标权限即判断平衡表的表格数据权限
  1001. {
  1002. // 查询图表关联的表格ID
  1003. excelInfo, e := excelModel.GetExcelInfoByChartInfoId(chartInfo.ChartInfoId)
  1004. if e != nil {
  1005. errMsg = "获取图表关联的表格ID失败,Err:" + e.Error()
  1006. err = e
  1007. return
  1008. }
  1009. checkExcelInfo := excelInfo
  1010. if excelInfo.Source == utils.BALANCE_TABLE {
  1011. checkExcelInfoId := excelInfo.ExcelInfoId
  1012. if excelInfo.BalanceType == 1 {
  1013. checkExcelInfoId = excelInfo.RelExcelInfoId
  1014. } else {
  1015. if excelInfo.ParentId > 0 {
  1016. checkExcelInfoId = excelInfo.ParentId
  1017. }
  1018. }
  1019. if checkExcelInfoId != excelInfo.ExcelInfoId {
  1020. checkExcelInfo, err = excelModel.GetExcelInfoById(checkExcelInfoId)
  1021. if err != nil {
  1022. errMsg = "获取平衡表格信息失败"
  1023. err = errors.New("获取平衡表格信息失败,Err:" + err.Error())
  1024. return
  1025. }
  1026. }
  1027. }
  1028. haveOperaAuth, e := data_manage_permission.CheckExcelPermissionByExcelInfoId(checkExcelInfo.ExcelInfoId, checkExcelInfo.ExcelClassifyId, checkExcelInfo.IsJoinPermission, adminId)
  1029. if e != nil {
  1030. errMsg = "获取平衡表格数据权限失败"
  1031. err = errors.New("获取表格权限信息失败,Err" + e.Error())
  1032. return
  1033. }
  1034. // 数据权限
  1035. for _, v := range resp.EdbInfoList {
  1036. v.HaveOperaAuth = haveOperaAuth
  1037. }
  1038. }
  1039. }
  1040. }()
  1041. key := GetChartInfoDataKey(chartInfo.ChartInfoId)
  1042. if utils.Re == nil && isCache {
  1043. if utils.Re == nil && utils.Rc.IsExist(key) {
  1044. if redisData, err1 := utils.Rc.RedisBytes(key); err1 == nil {
  1045. err := json.Unmarshal(redisData, &resp)
  1046. if err != nil || resp == nil {
  1047. return
  1048. }
  1049. // 这里跟当前用户相关的信息重新查询写入resp, 不使用缓存中的
  1050. var myCond string
  1051. var myPars []interface{}
  1052. myCond += ` AND a.admin_id=? `
  1053. myPars = append(myPars, adminId)
  1054. myCond += ` AND a.chart_info_id=? `
  1055. myPars = append(myPars, chartInfo.ChartInfoId)
  1056. myList, err := data_manage.GetMyChartByCondition(myCond, myPars)
  1057. if err != nil && err.Error() != utils.ErrNoRow() {
  1058. msg = "获取失败"
  1059. errMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  1060. return
  1061. }
  1062. resp.ChartInfo.IsAdd = false
  1063. resp.ChartInfo.MyChartId = 0
  1064. resp.ChartInfo.MyChartClassifyId = ""
  1065. if myList != nil && len(myList) > 0 {
  1066. resp.ChartInfo.IsAdd = true
  1067. resp.ChartInfo.MyChartId = myList[0].MyChartId
  1068. resp.ChartInfo.MyChartClassifyId = myList[0].MyChartClassifyId
  1069. }
  1070. //判断是否加入我的图库
  1071. {
  1072. var myChartCondition string
  1073. var myChartPars []interface{}
  1074. myChartCondition += ` AND a.admin_id=? `
  1075. myChartPars = append(myChartPars, adminId)
  1076. myChartCondition += ` AND a.chart_info_id=? `
  1077. myChartPars = append(myChartPars, chartInfo.ChartInfoId)
  1078. myChartList, err := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
  1079. if err != nil && err.Error() != utils.ErrNoRow() {
  1080. msg = "获取失败"
  1081. errMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  1082. return
  1083. }
  1084. if myChartList != nil && len(myChartList) > 0 {
  1085. chartInfo.IsAdd = true
  1086. chartInfo.MyChartId = myChartList[0].MyChartId
  1087. chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
  1088. }
  1089. }
  1090. isOk = true
  1091. fmt.Println("source redis")
  1092. return
  1093. }
  1094. }
  1095. }
  1096. return
  1097. }
  1098. // GetBalanceExcelSeasonChartLegendPreview 获取预览的季节性图例
  1099. func GetBalanceExcelSeasonChartLegendPreview(dataList []*data_manage.EdbDataList, calendar string, seasonExtraConfigReq data_manage.SeasonExtraItem) (quarterDataList data_manage.QuarterDataList, err error, errMsg string) {
  1100. seasonExtraConfig := ""
  1101. if seasonExtraConfigReq.XEndDate != "" {
  1102. if seasonExtraConfigReq.XStartDate > seasonExtraConfigReq.XEndDate && seasonExtraConfigReq.JumpYear != 1 {
  1103. errMsg = "季节性图表配置信息异常:横坐标日期配置错误"
  1104. err = fmt.Errorf("季节性图表配置信息异常: 横坐标日期配置错误")
  1105. return
  1106. }
  1107. seasonExtra, tErr := json.Marshal(seasonExtraConfigReq)
  1108. if tErr != nil {
  1109. errMsg = "季节性图表配置信息异常"
  1110. err = fmt.Errorf("季节性图表配置信息异常,Err:" + tErr.Error())
  1111. return
  1112. }
  1113. seasonExtraConfig = string(seasonExtra)
  1114. }
  1115. var latestDate time.Time //最新日期
  1116. startDate, endDate, _, _, _, _ := getBalanceDataListStartDateAndValue(dataList)
  1117. calendarPreYear := 0
  1118. startDateReal := startDate
  1119. latestDate, _ = time.ParseInLocation(utils.FormatDate, endDate, time.Local)
  1120. if calendar == "农历" {
  1121. newStartDateReal, e := time.Parse(utils.FormatDate, startDateReal)
  1122. if e != nil {
  1123. err = fmt.Errorf("开始时间解析失败 time.Parse:" + e.Error())
  1124. return
  1125. }
  1126. calendarPreYear = newStartDateReal.Year() - 1
  1127. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  1128. startDateReal = newStartDateReal.Format(utils.FormatDate)
  1129. }
  1130. if calendar == "农历" {
  1131. if len(dataList) <= 0 {
  1132. return
  1133. } else {
  1134. result, tmpErr := data_manage.AddCalculateQuarterV6(dataList)
  1135. if tmpErr != nil {
  1136. err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
  1137. return
  1138. }
  1139. quarterDataList, err = GetSeasonEdbInfoDataListByXDateNong(result, latestDate, seasonExtraConfig, calendarPreYear)
  1140. if err != nil {
  1141. err = errors.New("获取季节性图表数据失败,Err:" + err.Error())
  1142. return
  1143. }
  1144. }
  1145. } else {
  1146. quarterDataList, err = GetSeasonEdbInfoDataListByXDate(dataList, latestDate, seasonExtraConfig)
  1147. if err != nil {
  1148. err = errors.New("获取季节性图表数据失败,Err:" + err.Error())
  1149. return
  1150. }
  1151. }
  1152. return
  1153. }
  1154. func getBalanceDataListStartDateAndValue(dataList []*data_manage.EdbDataList) (startDate, endDate string, startVal, endVal, maxVal, minVal float64) {
  1155. if len(dataList) == 0 {
  1156. return
  1157. }
  1158. startDate = dataList[0].DataTime
  1159. startVal = dataList[0].Value
  1160. maxVal = dataList[0].Value
  1161. minVal = dataList[0].Value
  1162. endDate = dataList[len(dataList)-1].DataTime
  1163. endVal = dataList[len(dataList)-1].Value
  1164. for _, v := range dataList {
  1165. if v.DataTime < startDate {
  1166. startDate = v.DataTime
  1167. startVal = v.Value
  1168. }
  1169. if v.DataTime > endDate {
  1170. endDate = v.DataTime
  1171. endVal = v.Value
  1172. }
  1173. if v.Value > maxVal {
  1174. maxVal = v.Value
  1175. }
  1176. if v.Value < minVal {
  1177. minVal = v.Value
  1178. }
  1179. }
  1180. return
  1181. }
  1182. // AddBalanceStaticExcel 另存为和存为静态表以及子表的复制都调用该接口
  1183. 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) {
  1184. isSendEmail = true
  1185. versionName = utils.TrimLRStr(versionName)
  1186. // 检验分类下是否存在该表格名称
  1187. // todo 检验动态表分类下表格名称是否存在
  1188. if balanceType == 1 && versionName != "" {
  1189. var condition string
  1190. var pars []interface{}
  1191. condition += " AND rel_excel_info_id=? AND balance_type=1 AND parent_id=0"
  1192. pars = append(pars, oldExcelInfo.ExcelInfoId)
  1193. condition += " AND version_name=? "
  1194. pars = append(pars, versionName)
  1195. count, tmpErr := excelModel.GetExcelInfoCountByCondition(condition, pars)
  1196. if tmpErr != nil {
  1197. errMsg = "查询版本名称失败"
  1198. err = tmpErr
  1199. return
  1200. }
  1201. if count > 0 {
  1202. errMsg = "表格版本名称已存在,请重新填写版本名称"
  1203. err = errors.New(errMsg)
  1204. isSendEmail = false
  1205. return
  1206. }
  1207. }
  1208. // 表格信息
  1209. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  1210. excelName := oldExcelInfo.ExcelName
  1211. if pingNameFlag && versionName != "" && parentId == 0 {
  1212. excelName += "(" + versionName + ")"
  1213. }
  1214. // 处理表格内容, 静态表去除动态指标关联
  1215. content := oldExcelInfo.Content
  1216. excelInfo = &excelModel.ExcelInfo{
  1217. //ExcelInfoId: 0,
  1218. ExcelName: excelName,
  1219. Source: oldExcelInfo.Source,
  1220. ExcelType: oldExcelInfo.ExcelType,
  1221. UniqueCode: utils.MD5(utils.EXCEL_DATA_PREFIX + "_" + timestamp),
  1222. ExcelClassifyId: excelClassifyId,
  1223. SysUserId: sysUser.AdminId,
  1224. SysUserRealName: sysUser.RealName,
  1225. Content: content,
  1226. ExcelImage: oldExcelInfo.ExcelImage,
  1227. FileUrl: oldExcelInfo.FileUrl,
  1228. ParentId: parentId,
  1229. RelExcelInfoId: relExcelInfoId,
  1230. VersionName: versionName,
  1231. UpdateUserId: sysUser.AdminId,
  1232. UpdateUserRealName: sysUser.RealName,
  1233. BalanceType: balanceType,
  1234. Sort: oldExcelInfo.Sort,
  1235. IsDelete: 0,
  1236. ModifyTime: time.Now(),
  1237. CreateTime: time.Now(),
  1238. }
  1239. excelEdbMappingList := make([]*excelModel.ExcelEdbMapping, 0)
  1240. if balanceType == 0 {
  1241. // 复制动态表才需要获取原excel与指标的关系表
  1242. excelEdbMappingListTmp, e := excelModel.GetAllExcelEdbMappingByExcelInfoId(oldExcelInfo.ExcelInfoId)
  1243. if e != nil {
  1244. err = e
  1245. errMsg = "获取失败"
  1246. return
  1247. }
  1248. for _, v := range excelEdbMappingListTmp {
  1249. tmp := new(excelModel.ExcelEdbMapping)
  1250. tmp.EdbInfoId = v.EdbInfoId
  1251. tmp.Source = utils.BALANCE_TABLE
  1252. tmp.CreateTime = time.Now()
  1253. tmp.ModifyTime = time.Now()
  1254. excelEdbMappingList = append(excelEdbMappingList, tmp)
  1255. }
  1256. }
  1257. var childExcel *excelModel.ExcelInfo
  1258. err = excelModel.AddExcelInfo(excelInfo, excelEdbMappingList, childExcel)
  1259. if err != nil {
  1260. errMsg = "保存失败"
  1261. return
  1262. }
  1263. if parentId == 0 && len(childExcelList) > 0 {
  1264. for _, childExcelInfo := range childExcelList {
  1265. _, err, errMsg, isSendEmail = AddBalanceStaticExcel(childExcelInfo, excelClassifyId, versionName, sysUser, excelInfo.ExcelInfoId, relExcelInfoId, balanceType, []*excelModel.ExcelInfo{}, pingNameFlag, excelDataMap)
  1266. if err != nil {
  1267. return
  1268. }
  1269. }
  1270. } else if parentId > 0 {
  1271. // 如果复制的是动态表的子表,则同步复制关联的图表
  1272. // 查出所有的chart_list, 同步复制图表和图表指标
  1273. // 相关联指标
  1274. mappingListTmp, e := excelModel.GetExcelChartEdbMappingByExcelInfoId(oldExcelInfo.ExcelInfoId)
  1275. if e != nil {
  1276. errMsg = "获取图表指标失败"
  1277. err = fmt.Errorf(" 获取图表,指标信息失败 Err:%s", e.Error())
  1278. return
  1279. }
  1280. mappingListMap := make(map[int][]*excelModel.ExcelChartEdb, 0)
  1281. charInfoIds := make([]int, 0)
  1282. for k, _ := range mappingListTmp {
  1283. mappingListTmp[k].ExcelInfoId = excelInfo.ExcelInfoId
  1284. mappingListMap[mappingListTmp[k].ChartInfoId] = append(mappingListMap[mappingListTmp[k].ChartInfoId], mappingListTmp[k])
  1285. }
  1286. for k, _ := range mappingListMap {
  1287. charInfoIds = append(charInfoIds, k)
  1288. }
  1289. if len(charInfoIds) > 0 {
  1290. chartInfoList, e := data_manage.GetChartInfoViewByIdList(charInfoIds)
  1291. if e != nil {
  1292. errMsg = "获取失败"
  1293. err = fmt.Errorf(" 获取图表,指标信息失败 Err:%s", e.Error())
  1294. return
  1295. }
  1296. for _, chartInfo := range chartInfoList {
  1297. mappingList, ok := mappingListMap[chartInfo.ChartInfoId]
  1298. if !ok {
  1299. err = fmt.Errorf("未找到图表关联的指标信息")
  1300. return
  1301. }
  1302. chartEdbDataMap, ok := excelDataMap[chartInfo.ChartInfoId]
  1303. if !ok && oldExcelInfo.BalanceType == 1 { //
  1304. err = fmt.Errorf("未找到图表关联的指标信息")
  1305. return
  1306. }
  1307. err, errMsg, isSendEmail = CopyBalanceExcelChart(chartInfo, mappingList, versionName, sysUser, pingNameFlag, chartEdbDataMap)
  1308. if err != nil {
  1309. err = fmt.Errorf("复制图表失败 Err:%s", err.Error())
  1310. return
  1311. }
  1312. }
  1313. }
  1314. }
  1315. return
  1316. }
  1317. // CopyBalanceExcelChart 复制平衡表图表
  1318. 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) {
  1319. chartEdbList := make([]*excelModel.AddChartEdbAndDataItem, 0)
  1320. if pingNameFlag && versionName != "" {
  1321. oldChartInfo.ChartName += "(" + versionName + ")"
  1322. }
  1323. for _, chartEdb := range oldChartEdbList {
  1324. excelEdb := new(excelModel.ExcelChartEdb)
  1325. excelEdb.EdbName = chartEdb.EdbName
  1326. if pingNameFlag && versionName != "" {
  1327. excelEdb.EdbName += "(" + versionName + ")"
  1328. }
  1329. excelEdb.EdbNameEn = excelEdb.EdbName
  1330. excelEdb.Unit = chartEdb.Unit
  1331. excelEdb.UnitEn = excelEdb.Unit
  1332. randStr := utils.GetRandDigit(4)
  1333. excelEdb.EdbCode = `T` + time.Now().Format("060102150405") + "_" + randStr
  1334. excelEdb.ExcelInfoId = chartEdb.ExcelInfoId
  1335. excelEdb.DateSequence = chartEdb.DateSequence
  1336. excelEdb.DataSequence = chartEdb.DataSequence
  1337. excelEdb.SysUserId = sysUser.AdminId
  1338. excelEdb.SysUserRealName = sysUser.RealName
  1339. excelEdb.MaxData = chartEdb.MaxData
  1340. excelEdb.MinData = chartEdb.MinData
  1341. excelEdb.IsOrder = chartEdb.IsOrder
  1342. excelEdb.IsAxis = chartEdb.IsAxis
  1343. excelEdb.FromTag = chartEdb.FromTag
  1344. excelEdb.EdbInfoType = chartEdb.EdbInfoType
  1345. excelEdb.LeadValue = chartEdb.LeadValue
  1346. excelEdb.LeadUnit = chartEdb.LeadUnit
  1347. excelEdb.ChartWidth = 1
  1348. excelEdb.CreateTime = time.Now()
  1349. excelEdb.ModifyTime = time.Now()
  1350. var dataList []*excelModel.ExcelChartData
  1351. tmpList, ok := dataListMap[chartEdb.ExcelChartEdbId]
  1352. if ok {
  1353. for _, l := range tmpList {
  1354. tmp := &excelModel.ExcelChartData{
  1355. DataTime: l.DataTime,
  1356. Value: l.Value,
  1357. DataTimestamp: l.DataTimestamp,
  1358. }
  1359. dataList = append(dataList, tmp)
  1360. }
  1361. }
  1362. // 处理日期列表和值列表
  1363. addItem := &excelModel.AddChartEdbAndDataItem{
  1364. ChartEdb: excelEdb,
  1365. DataList: dataList,
  1366. }
  1367. chartEdbList = append(chartEdbList, addItem)
  1368. }
  1369. chartInfo, err, errMsg, isSendEmail := copyBalanceExcelChart(oldChartInfo, sysUser.AdminId, sysUser.RealName)
  1370. if err != nil {
  1371. errMsg = "新增图表失败!"
  1372. err = fmt.Errorf("新增图表失败!%s, %s", errMsg, err.Error())
  1373. return
  1374. }
  1375. obj := new(excelModel.ExcelChartEdb)
  1376. err = obj.AddChartEdbAndData(chartEdbList, chartInfo, []int{})
  1377. if err != nil {
  1378. errMsg = "新增图表失败!"
  1379. err = fmt.Errorf("新增图表数据失败!%s", err.Error())
  1380. return
  1381. }
  1382. return
  1383. }
  1384. func copyBalanceExcelChart(oldChartInfo *data_manage.ChartInfoView, sysUserId int, sysUserRealName string) (chartInfo *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  1385. isSendEmail = true // 默认错误的时候要发送邮件
  1386. oldChartInfo.ChartName = strings.Trim(oldChartInfo.ChartName, " ")
  1387. if oldChartInfo.ChartName == "" {
  1388. errMsg = "请填写图表名称!"
  1389. err = fmt.Errorf(errMsg)
  1390. isSendEmail = false
  1391. return
  1392. }
  1393. //判断图表是否存在
  1394. var condition string
  1395. var pars []interface{}
  1396. // 图表名称在不同图分类下不允许重复 需求调整时间:2022年11月07日09:47:07
  1397. condition += " AND chart_classify_id=0 "
  1398. condition += " AND chart_name=? AND source = ? "
  1399. pars = append(pars, oldChartInfo.ChartName, utils.CHART_SOURCE_BALANCE_EXCEL)
  1400. count, err := data_manage.GetChartInfoCountByCondition(condition, pars)
  1401. if err != nil {
  1402. errMsg = "判断图表名称是否存在失败"
  1403. err = fmt.Errorf("判断图表名称是否存在失败,Err:" + err.Error())
  1404. return
  1405. }
  1406. if count > 0 {
  1407. /*errMsg = "图表已存在,请重新填写"
  1408. err = fmt.Errorf("判断图表名称是否存在失败")
  1409. isSendEmail = false
  1410. return*/
  1411. oldChartInfo.ChartName += " " + strconv.Itoa(count)
  1412. }
  1413. chartInfo = new(data_manage.ChartInfo)
  1414. chartInfo.ChartName = oldChartInfo.ChartName
  1415. chartInfo.ChartNameEn = oldChartInfo.ChartName
  1416. //chartInfo.EdbInfoIds = edbInfoIdStr
  1417. //chartInfo.ChartClassifyId = req.ChartClassifyId
  1418. chartInfo.SysUserId = sysUserId
  1419. chartInfo.SysUserRealName = sysUserRealName
  1420. chartInfo.CreateTime = time.Now()
  1421. chartInfo.ModifyTime = time.Now()
  1422. chartInfo.IsSetName = 0
  1423. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  1424. chartInfo.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
  1425. // todo 判断是否需要重新计算用户的start_date
  1426. chartInfo.DateType = oldChartInfo.DateType
  1427. chartInfo.ChartType = oldChartInfo.ChartType
  1428. chartInfo.Calendar = oldChartInfo.Calendar
  1429. /* chartInfo.StartDate = req.StartDate
  1430. chartInfo.EndDate = req.EndDate
  1431. chartInfo.SeasonStartDate = req.StartDate
  1432. chartInfo.SeasonEndDate = req.EndDate*/
  1433. chartInfo.LeftMin = oldChartInfo.LeftMin
  1434. chartInfo.LeftMax = oldChartInfo.LeftMax
  1435. chartInfo.RightMin = oldChartInfo.RightMin
  1436. chartInfo.RightMax = oldChartInfo.RightMax
  1437. chartInfo.Right2Min = oldChartInfo.Right2Min
  1438. chartInfo.Right2Max = oldChartInfo.Right2Max
  1439. chartInfo.MinMaxSave = oldChartInfo.MinMaxSave
  1440. //chartInfo.Disabled = disableVal
  1441. //chartInfo.BarConfig = barChartConf
  1442. chartInfo.ExtraConfig = oldChartInfo.ExtraConfig
  1443. chartInfo.SeasonExtraConfig = oldChartInfo.SeasonExtraConfig
  1444. //chartInfo.StartYear = req.StartYear
  1445. chartInfo.Source = utils.CHART_SOURCE_BALANCE_EXCEL
  1446. chartInfo.ChartThemeId = oldChartInfo.ChartThemeId
  1447. chartInfo.SourcesFrom = oldChartInfo.SourcesFrom
  1448. /* chartInfo.Instructions = req.Instructions
  1449. chartInfo.MarkersLines = req.MarkersLines
  1450. chartInfo.MarkersAreas = req.MarkersAreas
  1451. chartInfo.Unit = req.Unit
  1452. chartInfo.UnitEn = req.UnitEn*/
  1453. /*newId, err := data_manage.AddChartInfo(chartInfo)
  1454. if err != nil {
  1455. errMsg = `保存失败`
  1456. err = fmt.Errorf("保存失败,Err:" + err.Error())
  1457. return
  1458. }
  1459. chartInfo.ChartInfoId = int(newId)*/
  1460. //添加es数据
  1461. //go EsAddOrEditChartInfo(chartInfo.ChartInfoId)
  1462. return
  1463. }
  1464. // CopyBalanceExcel 动态平衡表另存为
  1465. func CopyBalanceExcel(oldExcelInfo *excelModel.ExcelInfo, excelClassifyId int, excelName string, sysUser *system.Admin) (excelInfo *excelModel.ExcelInfo, err error, errMsg string, isSendEmail bool) {
  1466. if oldExcelInfo.ParentId != 0 && oldExcelInfo.BalanceType != 0 {
  1467. errMsg = "平衡表类型错误"
  1468. err = fmt.Errorf("平衡表类型错误 ")
  1469. return
  1470. }
  1471. // 查询excel表格名称是否已经存在
  1472. // 检验分类下是否存在该表格名称
  1473. {
  1474. var nameCondition string
  1475. var namePars []interface{}
  1476. nameCondition += " AND excel_classify_id=? AND parent_id=?"
  1477. namePars = append(namePars, excelClassifyId, 0)
  1478. nameCondition += " AND excel_name=? "
  1479. namePars = append(namePars, excelName)
  1480. count, tmpErr := excelModel.GetExcelInfoCountByCondition(nameCondition, namePars)
  1481. if tmpErr != nil {
  1482. errMsg = "判断表格名称是否存在失败"
  1483. err = tmpErr
  1484. return
  1485. }
  1486. if count > 0 {
  1487. errMsg = "表格名称已存在,请重新填写表格名称"
  1488. err = errors.New(errMsg)
  1489. isSendEmail = false
  1490. return
  1491. }
  1492. }
  1493. oldExcelInfo.ExcelName = excelName
  1494. // 先复制动态表,再复制动态子表,复制静态表,再复制静态子表
  1495. condition := " AND parent_id = ? AND balance_type = 0 "
  1496. var pars []interface{}
  1497. pars = append(pars, oldExcelInfo.ExcelInfoId)
  1498. childExcelList, err := excelModel.GetExcelInfoListByCondition(condition, pars)
  1499. if err != nil {
  1500. errMsg = "获取子表失败"
  1501. err = fmt.Errorf("获取子表失败 %s", err.Error())
  1502. return
  1503. }
  1504. // 查询出每个子表的内容,并将内容转为静态版本
  1505. excelDataMap := make(map[int]map[int][]*data_manage.EdbDataList)
  1506. excelInfo, err, errMsg, isSendEmail = AddBalanceStaticExcel(oldExcelInfo, excelClassifyId, "", sysUser, 0, 0, 0, childExcelList, false, excelDataMap)
  1507. if err != nil {
  1508. errMsg = "复制动态表失败"
  1509. err = fmt.Errorf("复制动态表失败 %s", err.Error())
  1510. return
  1511. }
  1512. //复制静态表
  1513. staticCondition := " AND parent_id = 0 AND balance_type = 1 AND rel_excel_info_id=? "
  1514. var staticPars []interface{}
  1515. staticPars = append(staticPars, oldExcelInfo.ExcelInfoId)
  1516. staticExcelList, err := excelModel.GetExcelInfoListByCondition(staticCondition, staticPars)
  1517. if err != nil {
  1518. errMsg = "获取子表失败"
  1519. err = fmt.Errorf("获取子表失败 %s", err.Error())
  1520. return
  1521. }
  1522. for _, staticExcelInfo := range staticExcelList {
  1523. cCondition := " AND parent_id = ? AND balance_type = 1"
  1524. var cPars []interface{}
  1525. cPars = append(cPars, staticExcelInfo.ExcelInfoId)
  1526. childList, e := excelModel.GetExcelInfoListByCondition(cCondition, cPars)
  1527. if e != nil {
  1528. errMsg = "获取子表失败"
  1529. err = fmt.Errorf("获取子表失败 %s", err.Error())
  1530. return
  1531. }
  1532. excelDataMap = make(map[int]map[int][]*data_manage.EdbDataList)
  1533. for _, childExcelInfo := range childList {
  1534. //得到表格数据并落库
  1535. tmpDataList, e := excelModel.GetExcelChartDataByExcelInfoId(childExcelInfo.ExcelInfoId)
  1536. if e != nil {
  1537. err = fmt.Errorf(" 获取图表,指标信息失败 Err:%s", err.Error())
  1538. return
  1539. }
  1540. if len(tmpDataList) > 0 {
  1541. for _, v := range tmpDataList {
  1542. child, ok := excelDataMap[v.ChartInfoId]
  1543. if !ok {
  1544. child = make(map[int][]*data_manage.EdbDataList)
  1545. }
  1546. tmp := &data_manage.EdbDataList{
  1547. DataTime: v.DataTime,
  1548. DataTimestamp: v.DataTimestamp,
  1549. Value: v.Value,
  1550. }
  1551. child[v.ExcelChartEdbId] = append(child[v.ExcelChartEdbId], tmp)
  1552. excelDataMap[v.ChartInfoId] = child
  1553. }
  1554. }
  1555. }
  1556. _, err, errMsg, isSendEmail = AddBalanceStaticExcel(staticExcelInfo, excelClassifyId, staticExcelInfo.VersionName, sysUser, 0, excelInfo.ExcelInfoId, 1, childList, false, excelDataMap)
  1557. if err != nil {
  1558. errMsg = "复制动态表失败"
  1559. err = fmt.Errorf("复制动态表失败 %s", err.Error())
  1560. return
  1561. }
  1562. }
  1563. return
  1564. }
  1565. func TransferChartEdbToEdbMappingFormat(chartInfoId, chartType int, mappingListTmp []*excelModel.ExcelChartEdb, dataListMap map[int][]*data_manage.EdbDataList) (mappingList []*data_manage.ChartEdbInfoMapping, err error) {
  1566. mappingList = make([]*data_manage.ChartEdbInfoMapping, 0)
  1567. //循环组装映射关系
  1568. for _, v := range mappingListTmp {
  1569. dataList := make([]*data_manage.EdbDataList, 0)
  1570. dataListTmp, ok := dataListMap[v.ExcelChartEdbId]
  1571. if ok {
  1572. dataList = dataListTmp
  1573. } else {
  1574. //err = errors.New(fmt.Sprint("获取失败,指标类型异常", v.ExcelChartEdbId))
  1575. utils.FileLog.Info(fmt.Sprintf("获取失败,指标数据异常 %d", v.ExcelChartEdbId))
  1576. }
  1577. startDateStr, endDateStr, _, endVal, maxValue, minValue := getBalanceDataListStartDateAndValue(dataList)
  1578. mapping := &data_manage.ChartEdbInfoMapping{
  1579. EdbInfoId: v.ExcelChartEdbId,
  1580. SourceName: "平衡表",
  1581. Source: 0,
  1582. SubSource: 0,
  1583. EdbCode: v.EdbCode,
  1584. EdbName: v.EdbName,
  1585. EdbAliasName: v.EdbName,
  1586. EdbNameEn: v.EdbNameEn,
  1587. EdbAliasNameEn: "",
  1588. EdbType: 0,
  1589. Frequency: "",
  1590. FrequencyEn: "",
  1591. Unit: v.Unit,
  1592. UnitEn: v.UnitEn,
  1593. StartDate: startDateStr,
  1594. EndDate: endDateStr,
  1595. ModifyTime: v.ModifyTime.Format(utils.FormatDateTime),
  1596. ChartEdbMappingId: v.ExcelChartEdbId,
  1597. ChartInfoId: chartInfoId,
  1598. MaxData: v.MaxData,
  1599. MinData: v.MinData,
  1600. IsOrder: v.IsOrder,
  1601. IsAxis: v.IsAxis,
  1602. EdbInfoType: v.EdbInfoType,
  1603. //EdbInfoCategoryType: 0,
  1604. LeadValue: v.LeadValue,
  1605. LeadUnit: v.LeadUnit,
  1606. LeadUnitEn: "",
  1607. ChartStyle: "",
  1608. ChartColor: "",
  1609. PredictChartColor: "",
  1610. ChartWidth: v.ChartWidth,
  1611. ChartType: chartType,
  1612. LatestDate: endDateStr,
  1613. LatestValue: endVal,
  1614. MoveLatestDate: "",
  1615. UniqueCode: "",
  1616. MinValue: minValue,
  1617. MaxValue: maxValue,
  1618. DataList: nil,
  1619. IsNullData: false,
  1620. MappingSource: 0,
  1621. RegionType: "",
  1622. ClassifyId: 0,
  1623. IsConvert: 0,
  1624. ConvertType: 0,
  1625. ConvertValue: 0,
  1626. ConvertUnit: "",
  1627. ConvertEnUnit: "",
  1628. IsJoinPermission: 0,
  1629. HaveOperaAuth: true,
  1630. }
  1631. mappingList = append(mappingList, mapping)
  1632. }
  1633. return
  1634. }
  1635. // GetBalanceExcelInfoOpButtonByChartInfoId 获取图表权限
  1636. func GetBalanceExcelInfoOpButtonByChartInfoId(sysUserId, chartInfoId int, chartHaveOperaAuth bool) (opButton bool, err error) {
  1637. // 如果没有数据权限,那么直接返回
  1638. if !chartHaveOperaAuth {
  1639. return
  1640. }
  1641. // 获取表格信息
  1642. excelInfo, e := excelModel.GetExcelInfoByChartInfoId(chartInfoId)
  1643. if e != nil {
  1644. err = fmt.Errorf("获取ETA表格失败 Err %s", e.Error())
  1645. return
  1646. }
  1647. checkExcelInfo := excelInfo
  1648. if excelInfo.Source == utils.BALANCE_TABLE {
  1649. checkExcelInfoId := excelInfo.ExcelInfoId
  1650. if excelInfo.BalanceType == 1 {
  1651. checkExcelInfoId = excelInfo.RelExcelInfoId
  1652. } else {
  1653. if excelInfo.ParentId > 0 {
  1654. checkExcelInfoId = excelInfo.ParentId
  1655. }
  1656. }
  1657. if checkExcelInfoId != excelInfo.ExcelInfoId {
  1658. checkExcelInfo, e = excelModel.GetExcelInfoById(checkExcelInfoId)
  1659. if e != nil {
  1660. err = fmt.Errorf("获取平衡表格信息失败,Err:" + e.Error())
  1661. return
  1662. }
  1663. }
  1664. } else {
  1665. return
  1666. }
  1667. // 数据权限
  1668. haveOperaAuth, e := data_manage_permission.CheckExcelPermissionByExcelInfoId(checkExcelInfo.ExcelInfoId, checkExcelInfo.ExcelClassifyId, checkExcelInfo.IsJoinPermission, sysUserId)
  1669. if e != nil {
  1670. err = fmt.Errorf("获取表格权限信息失败,Err" + e.Error())
  1671. return
  1672. }
  1673. // 如果没有数据权限,那么直接返回
  1674. if !haveOperaAuth {
  1675. return
  1676. }
  1677. //button := GetBalanceExcelInfoOpButton(sysUserId, checkExcelInfo.SysUserId, haveOperaAuth, checkExcelInfo.ExcelInfoId)
  1678. if sysUserId == checkExcelInfo.SysUserId {
  1679. opButton = true
  1680. } else {
  1681. obj := new(excelModel.ExcelWorker)
  1682. workerList, e := obj.GetByExcelInfoId(checkExcelInfo.ExcelInfoId)
  1683. if e == nil {
  1684. for _, v := range workerList {
  1685. if v.SysUserId == sysUserId {
  1686. opButton = true
  1687. break
  1688. }
  1689. }
  1690. }
  1691. }
  1692. return
  1693. }