residual_analysis_service.go 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097
  1. package residual_analysis_service
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/models/data_manage"
  5. "eta/eta_api/models/residual_analysis_model"
  6. "eta/eta_api/models/system"
  7. "eta/eta_api/services/data"
  8. "eta/eta_api/utils"
  9. "fmt"
  10. "math"
  11. "sort"
  12. "strconv"
  13. "time"
  14. )
  15. func ResidualAnalysisPreview(req residual_analysis_model.ResidualAnalysisReq) (residual_analysis_model.ResidualAnalysisResp, error) {
  16. mappingList, err := data_manage.GetChartEdbMappingListByEdbInfoIdList([]int{req.EdbInfoIdA, req.EdbInfoIdB})
  17. if err != nil {
  18. return residual_analysis_model.ResidualAnalysisResp{}, fmt.Errorf("获取图表,指标信息失败,Err:%s", err.Error())
  19. }
  20. var edbInfoMappingA, edbInfoMappingB *data_manage.ChartEdbInfoMapping
  21. for _, v := range mappingList {
  22. if v.Unit == "无" {
  23. v.Unit = ""
  24. }
  25. if v.EdbInfoId == req.EdbInfoIdA {
  26. edbInfoMappingA = v
  27. }
  28. if v.EdbInfoId == req.EdbInfoIdB {
  29. edbInfoMappingB = v
  30. }
  31. }
  32. if edbInfoMappingA == nil {
  33. return residual_analysis_model.ResidualAnalysisResp{}, fmt.Errorf("指标A不存在")
  34. }
  35. if edbInfoMappingB == nil {
  36. return residual_analysis_model.ResidualAnalysisResp{}, fmt.Errorf("指标B不存在")
  37. }
  38. // 时间处理
  39. var startDate, endDate string
  40. switch req.DateType {
  41. case 0:
  42. startDate = req.StartDate
  43. endDate = req.EndDate
  44. case 1:
  45. startDate = req.StartDate
  46. endDate = ""
  47. default:
  48. startDate = utils.GetPreYearTime(req.DateType)
  49. endDate = ""
  50. }
  51. resp := residual_analysis_model.ResidualAnalysisResp{}
  52. // 原始图表信息
  53. originalEdbList := make([]residual_analysis_model.ResidualAnalysisChartEdbInfoMapping, 0)
  54. originalEdbList, fullADataList, fullBDataList, err := fillOriginalChart(req, mappingList, startDate, endDate, edbInfoMappingA, edbInfoMappingB, originalEdbList)
  55. if err != nil {
  56. return residual_analysis_model.ResidualAnalysisResp{}, err
  57. }
  58. originalChartInfo := createChartInfoResp(req, startDate, endDate, edbInfoMappingA.EdbName+"与"+edbInfoMappingB.EdbName)
  59. resp.OriginalChartData = residual_analysis_model.ChartResp{
  60. ChartInfo: originalChartInfo,
  61. EdbInfoList: originalEdbList,
  62. }
  63. // 如果只需要第一张图表的数据 直接返回,避免继续处理
  64. if req.QueryType == 1 {
  65. return resp, nil
  66. }
  67. dataAList, ok := edbInfoMappingA.DataList.([]*data_manage.EdbDataList)
  68. if !ok {
  69. return residual_analysis_model.ResidualAnalysisResp{}, fmt.Errorf("数据类型转换失败")
  70. }
  71. indexADataMap := map[string]*data_manage.EdbDataList{}
  72. for _, indexData := range dataAList {
  73. indexADataMap[indexData.DataTime] = indexData
  74. }
  75. // 映射图表信息
  76. mappingEdbList, a, b, r, err := fillMappingChartInfo(req, edbInfoMappingA, edbInfoMappingB, originalEdbList, indexADataMap, startDate, endDate, fullADataList, fullBDataList)
  77. if err != nil {
  78. return residual_analysis_model.ResidualAnalysisResp{}, err
  79. }
  80. mappingChartInfo := createChartInfoResp(req, startDate, endDate, edbInfoMappingA.EdbName+"与"+edbInfoMappingB.EdbName+"映射"+edbInfoMappingA.EdbName)
  81. resp.MappingChartData = residual_analysis_model.ChartResp{
  82. ChartInfo: mappingChartInfo,
  83. EdbInfoList: mappingEdbList,
  84. }
  85. // 残差图表信息
  86. residualEdbList, R2, err := fillResidualChartInfo(req, edbInfoMappingA, edbInfoMappingB, mappingEdbList)
  87. if err != nil {
  88. return residual_analysis_model.ResidualAnalysisResp{}, err
  89. }
  90. residualChartInfo := createChartInfoResp(req, startDate, endDate, edbInfoMappingA.EdbName+"与"+edbInfoMappingA.EdbName+"映射残差/"+edbInfoMappingB.EdbName)
  91. resp.ResidualChartData = residual_analysis_model.ChartResp{
  92. ChartInfo: residualChartInfo,
  93. EdbInfoList: residualEdbList,
  94. }
  95. if req.ResidualType == 2 {
  96. resp.A = math.Round(a*10000) / 10000
  97. resp.B = math.Round(b*10000) / 10000
  98. resp.R = math.Round(r*10000) / 10000
  99. resp.R2 = math.Round(R2*10000) / 10000
  100. }
  101. return resp, nil
  102. }
  103. func createChartInfoResp(req residual_analysis_model.ResidualAnalysisReq, startDate, endDate, chartName string) residual_analysis_model.ResidualAnalysisChartInfo {
  104. return residual_analysis_model.ResidualAnalysisChartInfo{
  105. Calendar: `公历`,
  106. Source: utils.CHART_SOURCE_DEFAULT,
  107. DateType: req.DateType,
  108. StartDate: startDate,
  109. EndDate: endDate,
  110. ChartType: utils.CHART_TYPE_CURVE,
  111. ChartName: chartName,
  112. }
  113. }
  114. func fillResidualChartInfo(req residual_analysis_model.ResidualAnalysisReq, edbInfoMappingA *data_manage.ChartEdbInfoMapping, edbInfoMappingB *data_manage.ChartEdbInfoMapping, mappingEdbList []residual_analysis_model.ResidualAnalysisChartEdbInfoMapping) ([]residual_analysis_model.ResidualAnalysisChartEdbInfoMapping, float64, error) {
  115. // 计算公式 映射残差 = 因变量指标 - 映射指标
  116. var edbInfoA, edbInfoB residual_analysis_model.ResidualAnalysisChartEdbInfoMapping
  117. if mappingEdbList[0].EdbInfoId == edbInfoMappingA.EdbInfoId {
  118. edbInfoA = mappingEdbList[0]
  119. edbInfoB = mappingEdbList[1]
  120. } else {
  121. edbInfoA = mappingEdbList[1]
  122. edbInfoB = mappingEdbList[0]
  123. }
  124. dataAList, ok := edbInfoA.DataList.([]*data_manage.EdbDataList)
  125. if !ok {
  126. return nil, 0, fmt.Errorf("数据类型转换失败")
  127. }
  128. edbData := make([]*data_manage.EdbDataList, len(dataAList))
  129. for i, data := range dataAList {
  130. edbData[i] = &data_manage.EdbDataList{
  131. Value: data.Value, // 确保为每个元素创建一个新的对象
  132. DataTimestamp: data.DataTimestamp,
  133. DataTime: data.DataTime,
  134. EdbInfoId: data.EdbInfoId,
  135. EdbDataId: data.EdbDataId,
  136. }
  137. }
  138. dataBList, ok := edbInfoB.DataList.([]*data_manage.EdbDataList)
  139. if !ok {
  140. return nil, 0, fmt.Errorf("数据类型转换失败")
  141. }
  142. // 映射指标开始时间
  143. var startTime string
  144. if len(dataBList) > 0 {
  145. startTime = dataBList[0].DataTime
  146. }
  147. var indexDataBMap = make(map[string]*data_manage.EdbDataList)
  148. for _, data := range dataBList {
  149. indexDataBMap[data.DataTime] = data
  150. }
  151. // 求R2
  152. var valueB, sumValueA, averageValueA, residualQuadraticSum, totalQuadraticSum, R2 float64
  153. for _, indexData := range edbData {
  154. // 因变量的值总和
  155. sumValueA += indexData.Value
  156. }
  157. // 因变量平均值
  158. averageValueA = sumValueA / float64(len(edbData))
  159. var indexMax, indexMin float64
  160. var edbDataResp []*data_manage.EdbDataList
  161. if len(edbData) > 0 {
  162. indexMax = edbData[0].Value
  163. indexMin = edbData[0].Value
  164. for _, indexData := range edbData {
  165. if dataB, ok := indexDataBMap[indexData.DataTime]; ok {
  166. valueB = dataB.Value
  167. } else {
  168. continue
  169. }
  170. // 总因变量平方和
  171. totalQuadraticSum += math.Pow(indexData.Value-averageValueA, 2)
  172. // 补全残差值
  173. indexData.Value = math.Round((indexData.Value-valueB)*10000) / 10000
  174. // 残差平方和
  175. residualQuadraticSum += math.Pow(indexData.Value, 2)
  176. if indexData.Value > indexMax {
  177. indexMax = indexData.Value
  178. }
  179. if indexData.Value < indexMin {
  180. indexMin = indexData.Value
  181. }
  182. // 获取映射指标之后的数据
  183. if startTime != "" && utils.CompareDate(startTime, indexData.DataTime) {
  184. edbDataResp = append(edbDataResp, indexData)
  185. }
  186. }
  187. }
  188. // 计算R2 公式:R2=1-SSE/SST R2越大,越符合线性 R2 = 1 - 残差平方和/总平方和
  189. R2 = 1 - residualQuadraticSum/totalQuadraticSum
  190. mappingEdb := make([]residual_analysis_model.ResidualAnalysisChartEdbInfoMapping, len(mappingEdbList))
  191. copy(mappingEdb, mappingEdbList)
  192. for i, mapping := range mappingEdb {
  193. if mapping.EdbInfoId != edbInfoMappingA.EdbInfoId {
  194. mappingEdb[i].DataList = edbDataResp
  195. mappingEdb[i].EdbName = edbInfoMappingA.EdbName + "映射残差/" + edbInfoMappingB.EdbName
  196. if req.IndexType == 2 {
  197. if req.LeadValue > 0 {
  198. mappingEdb[i].EdbName = edbInfoMappingA.EdbName + "映射残差/" + edbInfoMappingB.EdbName + "(领先" + strconv.Itoa(req.LeadValue) + req.LeadFrequency + ")"
  199. }
  200. }
  201. mappingEdb[i].IsAxis = 1
  202. mappingEdb[i].ChartColor = `#00F`
  203. mappingEdb[i].IsOrder = false
  204. mappingEdb[i].MinValue = indexMin
  205. mappingEdb[i].MaxValue = indexMax
  206. } else {
  207. mappingEdb[i].IsAxis = 0
  208. mappingEdb[i].ChartColor = `#F00`
  209. }
  210. }
  211. return mappingEdb, R2, nil
  212. }
  213. func fillMappingChartInfo(req residual_analysis_model.ResidualAnalysisReq, edbInfoMappingA *data_manage.ChartEdbInfoMapping, edbInfoMappingB *data_manage.ChartEdbInfoMapping, originalEdbList []residual_analysis_model.ResidualAnalysisChartEdbInfoMapping, indexADataMap map[string]*data_manage.EdbDataList, startDate string, endDate string, fullADataList []*data_manage.EdbDataList, fullBDataList []*data_manage.EdbDataList) ([]residual_analysis_model.ResidualAnalysisChartEdbInfoMapping, float64, float64, float64, error) {
  214. // 计算公式:Y=aX+b,Y为映射后的指标,X为自变量指标
  215. // 正序:a=(L2-L1)/(R2-R1) b=L2-R2*a
  216. // 逆序:a=(L2-L1)/(R1-R2) b=L2-R1*a
  217. // L2:左轴下限 R2:右轴上限 L1:左轴上限 R1:右轴下限
  218. var a, b, r float64
  219. // 映射残差 计算a,b
  220. if req.ResidualType == 1 {
  221. if req.IsOrder {
  222. a = (req.LeftIndexMax - req.LeftIndexMin) / (req.RightIndexMin - req.RightIndexMax)
  223. b = req.LeftIndexMax - req.RightIndexMin*a
  224. } else {
  225. a = (req.LeftIndexMax - req.LeftIndexMin) / (req.RightIndexMax - req.RightIndexMin)
  226. b = req.LeftIndexMax - req.RightIndexMax*a
  227. }
  228. }
  229. //dataAList := edbInfoMappingA.DataList.([]*data_manage.EdbDataList)
  230. dataList, ok := edbInfoMappingB.DataList.([]*data_manage.EdbDataList)
  231. if !ok {
  232. return nil, a, b, r, fmt.Errorf("数据类型转换失败")
  233. }
  234. // 指标B数据补充
  235. // 新建一个切片来保存补充的数据
  236. var replenishDataList []*data_manage.EdbDataList
  237. for index := 0; index < len(dataList)-1; index++ {
  238. // 获取当前数据和下一个数据
  239. beforeIndexData := dataList[index]
  240. afterIndexData := dataList[index+1]
  241. // 从最早时间开始,补充时间为自然日
  242. for utils.IsMoreThanOneDay(beforeIndexData.DataTime, afterIndexData.DataTime) {
  243. // 创建补充数据
  244. nextDay := utils.GetNextDay(beforeIndexData.DataTime)
  245. toTime := utils.StringToTime(nextDay)
  246. replenishIndexData := data_manage.EdbDataList{
  247. DataTime: nextDay, // 计算下一个自然日
  248. DataTimestamp: toTime.UnixMilli(),
  249. Value: beforeIndexData.Value, // 可以选择使用前一天的值,或者其他逻辑来计算值
  250. }
  251. // 将补充数据加入补充数据列表
  252. replenishDataList = append(replenishDataList, &replenishIndexData)
  253. // 更新 beforeIndexData 为新创建的补充数据
  254. beforeIndexData = &replenishIndexData
  255. }
  256. }
  257. // 将补充数据插入原始数据列表
  258. dataList = append(dataList, replenishDataList...)
  259. // 排序
  260. sort.Sort(ByDataTime(dataList))
  261. // 拟合残差 计算a,b
  262. var coordinateList []utils.Coordinate
  263. var replenishADataList []*data_manage.EdbDataList
  264. var replenishBDataList []*data_manage.EdbDataList
  265. if req.ResidualType == 2 {
  266. //
  267. // 因变量指标也转换为日度
  268. for index := 0; index < len(fullADataList)-1; index++ {
  269. // 获取当前数据和下一个数据
  270. beforeIndexData := fullADataList[index]
  271. afterIndexData := fullADataList[index+1]
  272. replenishADataList = append(replenishADataList, beforeIndexData)
  273. // 从最早时间开始,补充时间为自然日
  274. if utils.IsMoreThanOneDay(beforeIndexData.DataTime, afterIndexData.DataTime) {
  275. for {
  276. // 创建补充数据
  277. nextDay := utils.GetNextDay(beforeIndexData.DataTime)
  278. toTime := utils.StringToTime(nextDay)
  279. replenishIndexData := data_manage.EdbDataList{
  280. DataTime: nextDay, // 计算下一个自然日
  281. DataTimestamp: toTime.UnixMilli(),
  282. Value: beforeIndexData.Value, // 可以选择使用前一天的值,或者其他逻辑来计算值
  283. }
  284. // 将补充数据加入补充数据列表
  285. replenishADataList = append(replenishADataList, &replenishIndexData)
  286. // 更新 beforeIndexData 为新创建的补充数据
  287. beforeIndexData = &replenishIndexData
  288. // 检查是否还需要继续补充数据
  289. if !utils.IsMoreThanOneDay(beforeIndexData.DataTime, afterIndexData.DataTime) {
  290. break
  291. }
  292. }
  293. }
  294. }
  295. replenishADataList = append(replenishADataList, fullADataList[len(fullADataList)-1])
  296. // 自变量指标也转换为日度
  297. for index := 0; index < len(fullBDataList)-1; index++ {
  298. // 获取当前数据和下一个数据
  299. beforeIndexData := fullBDataList[index]
  300. afterIndexData := fullBDataList[index+1]
  301. replenishBDataList = append(replenishBDataList, beforeIndexData)
  302. // 从最早时间开始,补充时间为自然日
  303. if utils.IsMoreThanOneDay(beforeIndexData.DataTime, afterIndexData.DataTime) {
  304. for {
  305. // 创建补充数据
  306. nextDay := utils.GetNextDay(beforeIndexData.DataTime)
  307. toTime := utils.StringToTime(nextDay)
  308. replenishIndexData := data_manage.EdbDataList{
  309. DataTime: nextDay, // 计算下一个自然日
  310. DataTimestamp: toTime.UnixMilli(),
  311. Value: beforeIndexData.Value, // 可以选择使用前一天的值,或者其他逻辑来计算值
  312. }
  313. // 将补充数据加入补充数据列表
  314. replenishBDataList = append(replenishBDataList, &replenishIndexData)
  315. // 更新 beforeIndexData 为新创建的补充数据
  316. beforeIndexData = &replenishIndexData
  317. // 检查是否还需要继续补充数据
  318. if !utils.IsMoreThanOneDay(beforeIndexData.DataTime, afterIndexData.DataTime) {
  319. break
  320. }
  321. }
  322. }
  323. }
  324. replenishBDataList = append(replenishBDataList, fullBDataList[len(fullBDataList)-1])
  325. // replenishADataList --> map
  326. replenishADataMap := make(map[string]*data_manage.EdbDataList)
  327. for _, indexData := range replenishADataList {
  328. if (utils.StringToTime(indexData.DataTime).After(utils.StringToTime(startDate)) || utils.StringToTime(indexData.DataTime).Equal(utils.StringToTime(startDate))) && (endDate == "" || utils.StringToTime(indexData.DataTime).Before(utils.StringToTime(endDate))) {
  329. replenishADataMap[indexData.DataTime] = indexData
  330. }
  331. }
  332. for _, indexData := range replenishBDataList {
  333. if _, ok = replenishADataMap[indexData.DataTime]; ok {
  334. coordinate := utils.Coordinate{
  335. X: indexData.Value,
  336. Y: replenishADataMap[indexData.DataTime].Value,
  337. }
  338. coordinateList = append(coordinateList, coordinate)
  339. }
  340. }
  341. a, b = utils.GetLinearResult(coordinateList)
  342. r = utils.ComputeCorrelation(coordinateList)
  343. }
  344. // 填充映射指标值 使得时间长度一致
  345. dataList = FillDataBList(dataList, edbInfoMappingA)
  346. // 根据指标A的时间key,在B的映射指标中筛选出对应的值
  347. var dataBList []*data_manage.EdbDataList
  348. var indexMax, indexMin float64
  349. if len(dataList) > 0 {
  350. indexMax = dataList[0].Value
  351. indexMin = dataList[0].Value
  352. for _, indexData := range dataList {
  353. if _, ok := indexADataMap[indexData.DataTime]; ok {
  354. indexDataCopy := *indexData
  355. // 计算指标B映射值
  356. indexDataCopy.Value = math.Round((a*indexData.Value+b)*10000) / 10000
  357. // 比较最大值
  358. if indexData.Value > indexMax {
  359. indexMax = indexData.Value
  360. }
  361. // 比较最小值
  362. if indexData.Value < indexMin {
  363. indexMin = indexData.Value
  364. }
  365. // 将副本添加到 dataBList
  366. dataBList = append(dataBList, &indexDataCopy)
  367. }
  368. }
  369. }
  370. mappingEdbList := make([]residual_analysis_model.ResidualAnalysisChartEdbInfoMapping, len(originalEdbList))
  371. copy(mappingEdbList, originalEdbList)
  372. for i, mapping := range mappingEdbList {
  373. if mapping.EdbInfoId != req.EdbInfoIdA {
  374. mappingEdbList[i].EdbInfoId = 0
  375. mappingEdbList[i].EdbCode = ""
  376. mappingEdbList[i].IsAxis = 1
  377. mappingEdbList[i].EdbName = edbInfoMappingB.EdbName + "映射" + edbInfoMappingA.EdbName
  378. if req.IndexType == 2 {
  379. if req.LeadValue > 0 {
  380. mappingEdbList[i].EdbName = edbInfoMappingB.EdbName + "映射" + edbInfoMappingA.EdbName + "(领先" + strconv.Itoa(req.LeadValue) + req.LeadFrequency + ")"
  381. }
  382. }
  383. mappingEdbList[i].DataList = dataBList
  384. mappingEdbList[i].MinValue = indexMin
  385. mappingEdbList[i].MaxValue = indexMax
  386. }
  387. }
  388. return mappingEdbList, a, b, r, nil
  389. }
  390. // FillDataBList 填充B的数据 使得与A的时间保持一致
  391. func FillDataBList(dataList []*data_manage.EdbDataList, edbInfoMappingA *data_manage.ChartEdbInfoMapping) []*data_manage.EdbDataList {
  392. dataAList, ok := edbInfoMappingA.DataList.([]*data_manage.EdbDataList)
  393. if !ok {
  394. return nil
  395. }
  396. for utils.StringToTime(dataList[len(dataList)-1].DataTime).Before(utils.StringToTime(dataAList[len(dataAList)-1].DataTime)) {
  397. // 使用A的时间填充时间差
  398. timeDiff := utils.GetNextDayN(dataList[len(dataList)-1].DataTime, 1)
  399. // 创建新的数据点并填充 前值填充
  400. newDataPoint := &data_manage.EdbDataList{
  401. DataTime: timeDiff,
  402. Value: dataList[len(dataList)-1].Value,
  403. DataTimestamp: utils.StringToTime(timeDiff).UnixMilli(),
  404. }
  405. // 将新数据点添加到dataList末尾
  406. dataList = append(dataList, newDataPoint)
  407. }
  408. return dataList
  409. }
  410. type ByDataTime []*data_manage.EdbDataList
  411. func (a ByDataTime) Len() int {
  412. return len(a)
  413. }
  414. func (a ByDataTime) Swap(i, j int) {
  415. a[i], a[j] = a[j], a[i]
  416. }
  417. func (a ByDataTime) Less(i, j int) bool {
  418. t1 := utils.StringToTime(a[i].DataTime)
  419. t2 := utils.StringToTime(a[j].DataTime)
  420. return t1.Before(t2)
  421. }
  422. func fillOriginalChart(req residual_analysis_model.ResidualAnalysisReq, mappingList []*data_manage.ChartEdbInfoMapping, startDate string, endDate string, edbInfoMappingA *data_manage.ChartEdbInfoMapping, edbInfoMappingB *data_manage.ChartEdbInfoMapping, originalEdbList []residual_analysis_model.ResidualAnalysisChartEdbInfoMapping) ([]residual_analysis_model.ResidualAnalysisChartEdbInfoMapping, []*data_manage.EdbDataList, []*data_manage.EdbDataList, error) {
  423. var fullADataList, fullBDataList []*data_manage.EdbDataList
  424. for _, v := range mappingList {
  425. var edbInfoMapping residual_analysis_model.ResidualAnalysisChartEdbInfoMapping
  426. edbInfoMapping.EdbInfoType = 1
  427. edbInfoMapping.IsOrder = false
  428. edbInfoMapping.IsAxis = 1
  429. edbInfoMapping.ChartColor = `#00F`
  430. edbInfoMapping.ChartWidth = 3
  431. edbInfoMapping.EdbName = v.EdbName
  432. // 获取图表中的指标数据
  433. dataList, err := data_manage.GetEdbDataList(v.Source, v.SubSource, v.EdbInfoId, startDate, endDate)
  434. if err != nil {
  435. return nil, nil, nil, fmt.Errorf("获取指标数据失败,Err:%s", err.Error())
  436. }
  437. // 重新获取指标数据 产品要求需要和计算指标-拟合残差逻辑保持一致
  438. fullDataList, err := data_manage.GetEdbDataList(v.Source, v.SubSource, v.EdbInfoId, "", "")
  439. if err != nil {
  440. return nil, nil, nil, fmt.Errorf("获取指标数据失败,Err:%s", err.Error())
  441. }
  442. if v.EdbInfoId == req.EdbInfoIdB {
  443. edbInfoMapping.LeadValue = req.LeadValue
  444. edbInfoMapping.LeadUnit = req.LeadFrequency
  445. edbInfoMapping.EdbInfoType = req.IndexType
  446. edbInfoMapping.IsOrder = req.IsOrder
  447. edbInfoMapping.IsAxis = 0
  448. edbInfoMapping.ChartColor = `#F00`
  449. edbInfoMapping.ChartWidth = 1
  450. // 领先指标 dataList进行数据处理
  451. if req.IndexType == 2 {
  452. if req.LeadValue < 0 {
  453. return nil, nil, nil, fmt.Errorf("领先值不能小于0")
  454. } else if req.LeadValue > 0 {
  455. edbInfoMapping.EdbName = v.EdbName + "(领先" + strconv.Itoa(req.LeadValue) + req.LeadFrequency + ")"
  456. for _, indexData := range dataList {
  457. switch req.LeadFrequency {
  458. case "天":
  459. indexData.DataTime = utils.GetNextDayN(indexData.DataTime, req.LeadValue)
  460. case "周":
  461. indexData.DataTime = utils.GetNextDayN(indexData.DataTime, req.LeadValue*7)
  462. case "月":
  463. indexData.DataTime = utils.TimeToString(utils.AddDate(utils.StringToTime(indexData.DataTime), 0, req.LeadValue), utils.YearMonthDay)
  464. case "季":
  465. indexData.DataTime = utils.TimeToString(utils.AddDate(utils.StringToTime(indexData.DataTime), 0, req.LeadValue*3), utils.YearMonthDay)
  466. case "年":
  467. indexData.DataTime = utils.TimeToString(utils.AddDate(utils.StringToTime(indexData.DataTime), req.LeadValue, 0), utils.YearMonthDay)
  468. }
  469. indexData.DataTimestamp = utils.StringToTime(indexData.DataTime).UnixMilli()
  470. }
  471. }
  472. }
  473. edbInfoMappingB.DataList = dataList
  474. fullBDataList = fullDataList
  475. } else {
  476. edbInfoMappingA.DataList = dataList
  477. fullADataList = fullDataList
  478. }
  479. edbInfoMapping.EdbInfoId = v.EdbInfoId
  480. edbInfoMapping.EdbCode = v.EdbCode
  481. edbInfoMapping.Unit = v.Unit
  482. edbInfoMapping.Frequency = v.Frequency
  483. edbInfoMapping.Source = v.Source
  484. edbInfoMapping.SourceName = v.SourceName
  485. edbInfoMapping.MinValue = v.MinValue
  486. edbInfoMapping.MaxValue = v.MaxValue
  487. edbInfoMapping.LatestDate = v.LatestDate
  488. edbInfoMapping.LatestValue = v.LatestValue
  489. edbInfoMapping.DataList = dataList
  490. originalEdbList = append(originalEdbList, edbInfoMapping)
  491. }
  492. return originalEdbList, fullADataList, fullBDataList, nil
  493. }
  494. func ContrastPreview(indexCode string) (residual_analysis_model.ResidualAnalysisChartEdbInfoMapping, error) {
  495. var condition string
  496. var pars []interface{}
  497. if indexCode != "" {
  498. condition += " and edb_code=?"
  499. pars = append(pars, indexCode)
  500. }
  501. edbInfo, err := data_manage.GetEdbInfoByCondition(condition, pars)
  502. if err != nil {
  503. return residual_analysis_model.ResidualAnalysisChartEdbInfoMapping{}, err
  504. }
  505. if edbInfo == nil {
  506. return residual_analysis_model.ResidualAnalysisChartEdbInfoMapping{}, fmt.Errorf("指标不存在")
  507. }
  508. dataList, err := data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, "", "")
  509. var resp residual_analysis_model.ResidualAnalysisChartEdbInfoMapping
  510. resp.EdbInfoId = edbInfo.EdbInfoId
  511. resp.EdbCode = edbInfo.EdbCode
  512. resp.ChartColor = "#F00"
  513. resp.SourceName = edbInfo.SourceName
  514. resp.EdbName = edbInfo.EdbName
  515. resp.Unit = edbInfo.Unit
  516. resp.Frequency = edbInfo.Frequency
  517. resp.MinValue = edbInfo.MinValue
  518. resp.MaxValue = edbInfo.MaxValue
  519. resp.DataList = dataList
  520. return resp, nil
  521. }
  522. func SaveResidualAnalysis(req residual_analysis_model.ResidualAnalysisIndexSaveReq, sysUser *system.Admin) error {
  523. // 验证分类是否存在
  524. classifyCount, err := data_manage.GetEdbClassifyCountById(req.ClassifyId)
  525. if err != nil {
  526. return err
  527. }
  528. if classifyCount <= 0 {
  529. return fmt.Errorf("分类不存在")
  530. }
  531. // 校验名称是否重复
  532. var condition string
  533. var pars []interface{}
  534. condition += " and source = ? AND edb_name=?"
  535. pars = append(pars, req.Source, req.EdbName)
  536. edbInfoByCondition, err := data_manage.GetEdbInfoByCondition(condition, pars)
  537. if err != nil && err.Error() != utils.ErrNoRow() {
  538. return err
  539. }
  540. // 获取指标数据最大值 最小值 最后更新时间 最后更新时间对应的值 开始时间 最后时间
  541. var indexMax, indexMin, indexLatestValue float64
  542. var indexLatestDate, startDate string
  543. if len(req.DataList) > 0 {
  544. latestTime, _ := time.Parse(utils.YearMonthDay, req.DataList[0].DataTime)
  545. firstTime, _ := time.Parse(utils.YearMonthDay, req.DataList[0].DataTime)
  546. for _, data := range req.DataList {
  547. // 比较最大值
  548. if data.Value > indexMax {
  549. indexMax = data.Value
  550. }
  551. // 比较最小值
  552. if data.Value < indexMin {
  553. indexMin = data.Value
  554. }
  555. // 比较最新时间和对应值
  556. currentTime, err := time.Parse(utils.YearMonthDay, data.DataTime)
  557. if err != nil {
  558. // 时间解析失败,跳过此项
  559. continue
  560. }
  561. // 如果当前时间更晚
  562. if currentTime.After(latestTime) {
  563. latestTime = currentTime
  564. indexLatestDate = data.DataTime
  565. indexLatestValue = data.Value
  566. }
  567. // 取最小时间
  568. if currentTime.Before(firstTime) {
  569. firstTime = currentTime
  570. startDate = data.DataTime
  571. }
  572. }
  573. }
  574. // 更新保存指标和配置的映射关系
  575. mappingList, err := residual_analysis_model.GetConfigMappingListByConfigId(req.ConfigId)
  576. if err != nil {
  577. return err
  578. }
  579. // 判断是更新还是修改 看指标配置映射中,是否存在对应指标 存在 则更新 不存在 则新增
  580. var edbInfoMapping residual_analysis_model.CalculateResidualAnalysisConfigMapping
  581. for _, mapping := range mappingList {
  582. if req.IndexType == mapping.IndexType && req.IndexType != 0 {
  583. edbInfoMapping = mapping
  584. }
  585. }
  586. var edbInfoId int64
  587. var edbCode string
  588. // 更新or新增
  589. if edbInfoMapping.EdbInfoId > 0 {
  590. // 查询指标库指标
  591. edbInfo, err := data_manage.GetEdbInfoById(int(edbInfoMapping.EdbInfoId))
  592. if err != nil {
  593. return err
  594. }
  595. if edbInfo == nil {
  596. return fmt.Errorf("指标不存在")
  597. }
  598. edbInfoId = int64(edbInfo.EdbInfoId)
  599. edbCode = edbInfo.EdbCode
  600. if edbInfoByCondition != nil && edbInfoByCondition.EdbInfoId != edbInfo.EdbInfoId {
  601. return fmt.Errorf("指标名称重复")
  602. }
  603. // 须补充更新指标最大值,最小值,数据最新时间,数据最新值
  604. edbInfo.StartDate = startDate
  605. edbInfo.EndDate = indexLatestDate
  606. edbInfo.MaxValue = indexMax
  607. edbInfo.MinValue = indexMin
  608. edbInfo.LatestDate = indexLatestDate
  609. edbInfo.LatestValue = indexLatestValue
  610. edbInfo.Unit = req.Unit
  611. edbInfo.Frequency = req.Frequency
  612. edbInfo.ClassifyId = req.ClassifyId
  613. edbInfo.EdbName = req.EdbName
  614. err = edbInfo.Update([]string{"min_value", "max_value", "latest_date", "latest_value", "unit", "frequency", "classify_id", "edb_name"})
  615. if err != nil {
  616. return err
  617. }
  618. // 删除对应得指标数据
  619. err = residual_analysis_model.DeleteResidualAnalysisDataByEdbCode(edbInfo.EdbCode)
  620. if err != nil {
  621. return fmt.Errorf("删除指标数据失败")
  622. }
  623. } else {
  624. if edbInfoByCondition != nil {
  625. return fmt.Errorf("指标名称重复")
  626. }
  627. // 新增指标
  628. edbCode, err = utils.GenerateEdbCode(1, "")
  629. if err != nil {
  630. return err
  631. }
  632. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  633. edbInfoId, err = data_manage.AddEdbInfo(&data_manage.EdbInfo{
  634. EdbCode: edbCode,
  635. UniqueCode: utils.MD5(utils.CHART_PREFIX + "_" + timestamp),
  636. EdbName: req.EdbName,
  637. EdbNameEn: req.EdbNameEn,
  638. ClassifyId: req.ClassifyId,
  639. EdbType: req.EdbType,
  640. Unit: req.Unit,
  641. UnitEn: req.UnitEn,
  642. Frequency: req.Frequency,
  643. Source: req.Source,
  644. SourceName: "残差分析",
  645. Calendar: req.Calendar,
  646. SysUserRealName: sysUser.RealName,
  647. SysUserId: sysUser.AdminId,
  648. StartDate: startDate,
  649. EndDate: indexLatestDate,
  650. LatestDate: indexLatestDate,
  651. LatestValue: indexLatestValue,
  652. MinValue: indexMin,
  653. MaxValue: indexMax,
  654. CreateTime: time.Now(),
  655. ModifyTime: time.Now(),
  656. })
  657. if err != nil {
  658. return err
  659. }
  660. // 新增指标配置关系
  661. _, err = residual_analysis_model.SaveConfigMapping(residual_analysis_model.CalculateResidualAnalysisConfigMapping{
  662. CalculateResidualAnalysisConfigId: req.ConfigId,
  663. EdbInfoId: edbInfoId,
  664. ResidualType: req.ResidualType,
  665. IndexType: req.IndexType,
  666. CreateTime: time.Now(),
  667. ModifyTime: time.Now(),
  668. })
  669. if err != nil {
  670. return err
  671. }
  672. // 保存溯源信息
  673. {
  674. fromEdbInfoList, err := data_manage.GetEdbInfoByIdList([]int{req.EdbInfoIdA, req.EdbInfoIdB})
  675. if err != nil {
  676. return fmt.Errorf("获取失败")
  677. }
  678. edbCalculateMappingInfoList := make([]*data_manage.EdbInfoCalculateMapping, 0, len(fromEdbInfoList))
  679. for _, fromEdbInfo := range fromEdbInfoList {
  680. // 创建新对象并直接使用指针
  681. edbCalculateMappingInfo := &data_manage.EdbInfoCalculateMapping{
  682. EdbInfoId: int(edbInfoId),
  683. Source: req.Source,
  684. SourceName: "残差分析",
  685. EdbCode: edbCode,
  686. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  687. FromEdbCode: fromEdbInfo.EdbCode,
  688. FromEdbName: fromEdbInfo.EdbName,
  689. FromSource: fromEdbInfo.Source,
  690. FromSourceName: fromEdbInfo.SourceName,
  691. CreateTime: time.Now(),
  692. ModifyTime: time.Now(),
  693. }
  694. edbCalculateMappingInfoList = append(edbCalculateMappingInfoList, edbCalculateMappingInfo)
  695. }
  696. err = data_manage.AddEdbInfoCalculateMappingMulti(edbCalculateMappingInfoList)
  697. if err != nil {
  698. return fmt.Errorf("保存溯源信息失败,Err:" + err.Error())
  699. }
  700. }
  701. }
  702. // 新增数据
  703. for i := range req.DataList {
  704. req.DataList[i].EdbDataId = 0
  705. req.DataList[i].EdbInfoId = int(edbInfoId)
  706. req.DataList[i].EdbCode = edbCode
  707. }
  708. _, err = residual_analysis_model.AddResidualAnalysisData(req.DataList)
  709. if err != nil {
  710. return err
  711. }
  712. // 新增自变量 因变量与配置得关系 配置中不存在该指标,则新增
  713. var indexMap = make(map[int64]residual_analysis_model.CalculateResidualAnalysisConfigMapping)
  714. for _, mapping := range mappingList {
  715. indexMap[mapping.EdbInfoId] = mapping
  716. }
  717. if _, ok := indexMap[int64(req.EdbInfoIdA)]; !ok {
  718. _, err = residual_analysis_model.SaveConfigMapping(residual_analysis_model.CalculateResidualAnalysisConfigMapping{
  719. CalculateResidualAnalysisConfigId: req.ConfigId,
  720. EdbInfoId: int64(req.EdbInfoIdA),
  721. ResidualType: req.ResidualType,
  722. IndexType: 3,
  723. CreateTime: time.Now(),
  724. ModifyTime: time.Now(),
  725. })
  726. if err != nil {
  727. return err
  728. }
  729. }
  730. if _, ok := indexMap[int64(req.EdbInfoIdB)]; !ok {
  731. _, err = residual_analysis_model.SaveConfigMapping(residual_analysis_model.CalculateResidualAnalysisConfigMapping{
  732. CalculateResidualAnalysisConfigId: req.ConfigId,
  733. EdbInfoId: int64(req.EdbInfoIdB),
  734. ResidualType: req.ResidualType,
  735. IndexType: 4,
  736. CreateTime: time.Now(),
  737. ModifyTime: time.Now(),
  738. })
  739. if err != nil {
  740. return err
  741. }
  742. }
  743. //添加es
  744. data.AddOrEditEdbInfoToEs(int(edbInfoId))
  745. return nil
  746. }
  747. func ResidualAnalysisDetail(edbInfoId int) (residual_analysis_model.ResidualAnalysisDetailResp, error) {
  748. // 通过指标配置映射表 拿到配置id,再获取关联的所有指标信息
  749. var condition string
  750. var pars []interface{}
  751. condition += " and edb_info_id=?"
  752. pars = append(pars, edbInfoId)
  753. mappingList, err := residual_analysis_model.GetConfigMappingListByCondition(condition, pars)
  754. if err != nil {
  755. return residual_analysis_model.ResidualAnalysisDetailResp{}, err
  756. }
  757. if len(mappingList) <= 0 {
  758. return residual_analysis_model.ResidualAnalysisDetailResp{}, fmt.Errorf("指标不存在")
  759. }
  760. mapping := mappingList[0]
  761. configMappingList, err := residual_analysis_model.GetConfigMappingListByConfigId(mapping.CalculateResidualAnalysisConfigId)
  762. if err != nil {
  763. return residual_analysis_model.ResidualAnalysisDetailResp{}, err
  764. }
  765. var edbInfoIdList []int64
  766. var edbInfoMap = make(map[int64]residual_analysis_model.CalculateResidualAnalysisConfigMapping)
  767. var mappgingFlag = false
  768. var residualFlag = false
  769. for _, v := range configMappingList {
  770. edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
  771. edbInfoMap[v.EdbInfoId] = v
  772. if v.IndexType == 1 {
  773. mappgingFlag = true
  774. } else if v.IndexType == 2 {
  775. residualFlag = true
  776. }
  777. }
  778. condition = ""
  779. pars = []interface{}{}
  780. condition += ` and edb_info_id in(` + utils.GetOrmInReplace(len(edbInfoIdList)) + `)`
  781. for _, id := range edbInfoIdList {
  782. pars = append(pars, id)
  783. }
  784. edbInfoList, err := data_manage.GetEdbInfoListByCond(condition, pars)
  785. if err != nil {
  786. return residual_analysis_model.ResidualAnalysisDetailResp{}, err
  787. }
  788. // 获取配置
  789. configInfo, err := residual_analysis_model.GetResidualAnalysisConfigById(mapping.CalculateResidualAnalysisConfigId)
  790. if err != nil {
  791. return residual_analysis_model.ResidualAnalysisDetailResp{}, err
  792. }
  793. var edbInfoListResp []*residual_analysis_model.DetailEdbInfoList
  794. var dependentEdbInfo residual_analysis_model.DetailEdbInfoList
  795. var independentEdbInfo residual_analysis_model.DetailEdbInfoList
  796. for _, edbInfo := range edbInfoList {
  797. var indexType int
  798. if _, ok := edbInfoMap[int64(edbInfo.EdbInfoId)]; ok {
  799. indexType = edbInfoMap[int64(edbInfo.EdbInfoId)].IndexType
  800. }
  801. info := residual_analysis_model.DetailEdbInfoList{
  802. EdbInfoId: edbInfo.EdbInfoId,
  803. EdbInfoType: edbInfo.EdbInfoType,
  804. IndexType: indexType,
  805. SourceName: edbInfo.SourceName,
  806. Source: edbInfo.Source,
  807. EdbCode: edbInfo.EdbCode,
  808. EdbName: edbInfo.EdbName,
  809. EdbNameEn: edbInfo.EdbNameEn,
  810. Unit: edbInfo.Unit,
  811. UnitEn: edbInfo.UnitEn,
  812. Frequency: edbInfo.Frequency,
  813. FrequencyEn: edbInfo.FrequencyEn,
  814. ClassifyId: edbInfo.ClassifyId,
  815. }
  816. edbInfoListResp = append(edbInfoListResp, &info)
  817. if indexType == 3 {
  818. dependentEdbInfo = info
  819. } else if indexType == 4 {
  820. independentEdbInfo = info
  821. }
  822. }
  823. // 补充表格中 映射指标或者残差指标
  824. if mappgingFlag && !residualFlag {
  825. info := residual_analysis_model.DetailEdbInfoList{
  826. IndexType: 2,
  827. EdbName: independentEdbInfo.EdbName + "映射残差/" + dependentEdbInfo.EdbName,
  828. EdbNameEn: dependentEdbInfo.EdbNameEn,
  829. Unit: dependentEdbInfo.Unit,
  830. UnitEn: dependentEdbInfo.UnitEn,
  831. Frequency: dependentEdbInfo.Frequency,
  832. FrequencyEn: dependentEdbInfo.FrequencyEn,
  833. ClassifyId: dependentEdbInfo.ClassifyId,
  834. }
  835. edbInfoListResp = append(edbInfoListResp, &info)
  836. } else if !mappgingFlag && residualFlag {
  837. info := residual_analysis_model.DetailEdbInfoList{
  838. IndexType: 1,
  839. EdbName: dependentEdbInfo.EdbName + "映射" + independentEdbInfo.EdbName,
  840. EdbNameEn: dependentEdbInfo.EdbNameEn,
  841. Unit: dependentEdbInfo.Unit,
  842. UnitEn: dependentEdbInfo.UnitEn,
  843. Frequency: dependentEdbInfo.Frequency,
  844. FrequencyEn: dependentEdbInfo.FrequencyEn,
  845. ClassifyId: dependentEdbInfo.ClassifyId,
  846. }
  847. edbInfoListResp = append(edbInfoListResp, &info)
  848. }
  849. resp := residual_analysis_model.ResidualAnalysisDetailResp{
  850. ConfigInfo: &configInfo,
  851. EdbInfoList: edbInfoListResp,
  852. ResidualType: mapping.ResidualType,
  853. }
  854. return resp, nil
  855. }
  856. func SaveResidualAnalysisConfig(req residual_analysis_model.ResidualAnalysisReq, sysUser *system.Admin) (int64, error) {
  857. config := residual_analysis_model.ResidualAnalysisConfigVo{
  858. DateType: req.DateType,
  859. StartDate: req.StartDate,
  860. EndDate: req.EndDate,
  861. IsOrder: req.IsOrder,
  862. IndexType: req.IndexType,
  863. LeadValue: req.LeadValue,
  864. LeadFrequency: req.LeadFrequency,
  865. LeftIndexMin: req.LeftIndexMin,
  866. LeftIndexMax: req.LeftIndexMax,
  867. RightIndexMin: req.RightIndexMin,
  868. RightIndexMax: req.RightIndexMax,
  869. ResidualIndexMin: req.ResidualIndexMin,
  870. ResidualIndexMax: req.ResidualIndexMax,
  871. ContrastIndexMin: req.ContrastIndexMin,
  872. ContrastIndexMax: req.ContrastIndexMax,
  873. }
  874. // 转换为json格式
  875. configJson, err := json.Marshal(config)
  876. if err != nil {
  877. return 0, err
  878. }
  879. // 新增or更新
  880. /*
  881. var condition string
  882. var pars []interface{}
  883. if req.EdbInfoId > 0 {
  884. condition += " and edb_info_id=?"
  885. pars = append(pars, req.EdbInfoId)
  886. configMappings, err := residual_analysis_model.GetConfigMappingListByCondition(condition, pars)
  887. if err != nil {
  888. return 0, err
  889. }
  890. if len(configMappings) > 0 {
  891. mapping := configMappings[0]
  892. configInfo, err := residual_analysis_model.GetResidualAnalysisConfigById(mapping.CalculateResidualAnalysisConfigId)
  893. if err != nil {
  894. return 0, err
  895. }
  896. configInfo.Config = string(configJson)
  897. err = residual_analysis_model.UpdateResidualAnalysisConfig(configInfo)
  898. if err != nil {
  899. return 0, err
  900. }
  901. }
  902. }*/
  903. var configId int64
  904. if req.ConfigId > 0 {
  905. configInfo, err := residual_analysis_model.GetResidualAnalysisConfigById(req.ConfigId)
  906. if err != nil {
  907. return 0, err
  908. }
  909. if configInfo.CalculateResidualAnalysisConfigId == 0 {
  910. return 0, fmt.Errorf("未找到配置信息")
  911. }
  912. configId = int64(configInfo.CalculateResidualAnalysisConfigId)
  913. configInfo.Config = string(configJson)
  914. err = residual_analysis_model.UpdateResidualAnalysisConfig(configInfo)
  915. if err != nil {
  916. return 0, err
  917. }
  918. } else {
  919. analysisConfig := residual_analysis_model.CalculateResidualAnalysisConfig{
  920. Config: string(configJson),
  921. SysUserId: sysUser.AdminId,
  922. CreateTime: time.Now(),
  923. ModifyTime: time.Now(),
  924. }
  925. configId, err = residual_analysis_model.SaveResidualAnalysisConfig(analysisConfig)
  926. if err != nil {
  927. return 0, err
  928. }
  929. }
  930. return configId, nil
  931. }
  932. func CheckResidualAnalysisExist(configId int) (int64, error) {
  933. configMappingList, err := residual_analysis_model.GetConfigMappingListByConfigId(configId)
  934. if err != nil {
  935. return 0, err
  936. }
  937. var configMapping residual_analysis_model.CalculateResidualAnalysisConfigMapping
  938. for _, mapping := range configMappingList {
  939. if mapping.IndexType == 2 {
  940. configMapping = mapping
  941. }
  942. }
  943. return configMapping.EdbInfoId, nil
  944. }