residual_analysis_service.go 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  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. LatestDate: indexLatestDate,
  649. LatestValue: indexLatestValue,
  650. MinValue: indexMin,
  651. MaxValue: indexMax,
  652. CreateTime: time.Now(),
  653. ModifyTime: time.Now(),
  654. })
  655. if err != nil {
  656. return err
  657. }
  658. // 新增指标配置关系
  659. _, err = residual_analysis_model.SaveConfigMapping(residual_analysis_model.CalculateResidualAnalysisConfigMapping{
  660. CalculateResidualAnalysisConfigId: req.ConfigId,
  661. EdbInfoId: edbInfoId,
  662. ResidualType: req.ResidualType,
  663. IndexType: req.IndexType,
  664. CreateTime: time.Now(),
  665. ModifyTime: time.Now(),
  666. })
  667. if err != nil {
  668. return err
  669. }
  670. // 保存溯源信息
  671. {
  672. fromEdbInfoList, err := data_manage.GetEdbInfoByIdList([]int{req.EdbInfoIdA, req.EdbInfoIdB})
  673. if err != nil {
  674. return fmt.Errorf("获取失败")
  675. }
  676. edbCalculateMappingInfoList := make([]*data_manage.EdbInfoCalculateMapping, 0, len(fromEdbInfoList))
  677. for _, fromEdbInfo := range fromEdbInfoList {
  678. // 创建新对象并直接使用指针
  679. edbCalculateMappingInfo := &data_manage.EdbInfoCalculateMapping{
  680. EdbInfoId: int(edbInfoId),
  681. Source: req.Source,
  682. SourceName: "残差分析",
  683. EdbCode: edbCode,
  684. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  685. FromEdbCode: fromEdbInfo.EdbCode,
  686. FromEdbName: fromEdbInfo.EdbName,
  687. FromSource: fromEdbInfo.Source,
  688. FromSourceName: fromEdbInfo.SourceName,
  689. CreateTime: time.Now(),
  690. ModifyTime: time.Now(),
  691. }
  692. edbCalculateMappingInfoList = append(edbCalculateMappingInfoList, edbCalculateMappingInfo)
  693. }
  694. err = data_manage.AddEdbInfoCalculateMappingMulti(edbCalculateMappingInfoList)
  695. if err != nil {
  696. return fmt.Errorf("保存溯源信息失败,Err:" + err.Error())
  697. }
  698. }
  699. }
  700. // 新增数据
  701. for i := range req.DataList {
  702. req.DataList[i].EdbDataId = 0
  703. req.DataList[i].EdbInfoId = int(edbInfoId)
  704. req.DataList[i].EdbCode = edbCode
  705. }
  706. _, err = residual_analysis_model.AddResidualAnalysisData(req.DataList)
  707. if err != nil {
  708. return err
  709. }
  710. // 新增自变量 因变量与配置得关系 配置中不存在该指标,则新增
  711. var indexMap = make(map[int64]residual_analysis_model.CalculateResidualAnalysisConfigMapping)
  712. for _, mapping := range mappingList {
  713. indexMap[mapping.EdbInfoId] = mapping
  714. }
  715. if _, ok := indexMap[int64(req.EdbInfoIdA)]; !ok {
  716. _, err = residual_analysis_model.SaveConfigMapping(residual_analysis_model.CalculateResidualAnalysisConfigMapping{
  717. CalculateResidualAnalysisConfigId: req.ConfigId,
  718. EdbInfoId: int64(req.EdbInfoIdA),
  719. ResidualType: req.ResidualType,
  720. IndexType: 3,
  721. CreateTime: time.Now(),
  722. ModifyTime: time.Now(),
  723. })
  724. if err != nil {
  725. return err
  726. }
  727. }
  728. if _, ok := indexMap[int64(req.EdbInfoIdB)]; !ok {
  729. _, err = residual_analysis_model.SaveConfigMapping(residual_analysis_model.CalculateResidualAnalysisConfigMapping{
  730. CalculateResidualAnalysisConfigId: req.ConfigId,
  731. EdbInfoId: int64(req.EdbInfoIdB),
  732. ResidualType: req.ResidualType,
  733. IndexType: 4,
  734. CreateTime: time.Now(),
  735. ModifyTime: time.Now(),
  736. })
  737. if err != nil {
  738. return err
  739. }
  740. }
  741. //添加es
  742. data.AddOrEditEdbInfoToEs(int(edbInfoId))
  743. return nil
  744. }
  745. func ResidualAnalysisDetail(edbInfoId int) (residual_analysis_model.ResidualAnalysisDetailResp, error) {
  746. // 通过指标配置映射表 拿到配置id,再获取关联的所有指标信息
  747. var condition string
  748. var pars []interface{}
  749. condition += " and edb_info_id=?"
  750. pars = append(pars, edbInfoId)
  751. mappingList, err := residual_analysis_model.GetConfigMappingListByCondition(condition, pars)
  752. if err != nil {
  753. return residual_analysis_model.ResidualAnalysisDetailResp{}, err
  754. }
  755. if len(mappingList) <= 0 {
  756. return residual_analysis_model.ResidualAnalysisDetailResp{}, fmt.Errorf("指标不存在")
  757. }
  758. mapping := mappingList[0]
  759. configMappingList, err := residual_analysis_model.GetConfigMappingListByConfigId(mapping.CalculateResidualAnalysisConfigId)
  760. if err != nil {
  761. return residual_analysis_model.ResidualAnalysisDetailResp{}, err
  762. }
  763. var edbInfoIdList []int64
  764. var edbInfoMap = make(map[int64]residual_analysis_model.CalculateResidualAnalysisConfigMapping)
  765. var mappgingFlag = false
  766. var residualFlag = false
  767. for _, v := range configMappingList {
  768. edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
  769. edbInfoMap[v.EdbInfoId] = v
  770. if v.IndexType == 1 {
  771. mappgingFlag = true
  772. } else if v.IndexType == 2 {
  773. residualFlag = true
  774. }
  775. }
  776. condition = ""
  777. pars = []interface{}{}
  778. condition += ` and edb_info_id in(` + utils.GetOrmInReplace(len(edbInfoIdList)) + `)`
  779. for _, id := range edbInfoIdList {
  780. pars = append(pars, id)
  781. }
  782. edbInfoList, err := data_manage.GetEdbInfoListByCond(condition, pars)
  783. if err != nil {
  784. return residual_analysis_model.ResidualAnalysisDetailResp{}, err
  785. }
  786. // 获取配置
  787. configInfo, err := residual_analysis_model.GetResidualAnalysisConfigById(mapping.CalculateResidualAnalysisConfigId)
  788. if err != nil {
  789. return residual_analysis_model.ResidualAnalysisDetailResp{}, err
  790. }
  791. var edbInfoListResp []*residual_analysis_model.DetailEdbInfoList
  792. var dependentEdbInfo residual_analysis_model.DetailEdbInfoList
  793. var independentEdbInfo residual_analysis_model.DetailEdbInfoList
  794. for _, edbInfo := range edbInfoList {
  795. var indexType int
  796. if _, ok := edbInfoMap[int64(edbInfo.EdbInfoId)]; ok {
  797. indexType = edbInfoMap[int64(edbInfo.EdbInfoId)].IndexType
  798. }
  799. info := residual_analysis_model.DetailEdbInfoList{
  800. EdbInfoId: edbInfo.EdbInfoId,
  801. EdbInfoType: edbInfo.EdbInfoType,
  802. IndexType: indexType,
  803. SourceName: edbInfo.SourceName,
  804. Source: edbInfo.Source,
  805. EdbCode: edbInfo.EdbCode,
  806. EdbName: edbInfo.EdbName,
  807. EdbNameEn: edbInfo.EdbNameEn,
  808. Unit: edbInfo.Unit,
  809. UnitEn: edbInfo.UnitEn,
  810. Frequency: edbInfo.Frequency,
  811. FrequencyEn: edbInfo.FrequencyEn,
  812. ClassifyId: edbInfo.ClassifyId,
  813. }
  814. edbInfoListResp = append(edbInfoListResp, &info)
  815. if indexType == 3 {
  816. dependentEdbInfo = info
  817. } else if indexType == 4 {
  818. independentEdbInfo = info
  819. }
  820. }
  821. // 补充表格中 映射指标或者残差指标
  822. if mappgingFlag && !residualFlag {
  823. info := residual_analysis_model.DetailEdbInfoList{
  824. IndexType: 2,
  825. EdbName: independentEdbInfo.EdbName + "映射残差/" + dependentEdbInfo.EdbName,
  826. EdbNameEn: dependentEdbInfo.EdbNameEn,
  827. Unit: dependentEdbInfo.Unit,
  828. UnitEn: dependentEdbInfo.UnitEn,
  829. Frequency: dependentEdbInfo.Frequency,
  830. FrequencyEn: dependentEdbInfo.FrequencyEn,
  831. ClassifyId: dependentEdbInfo.ClassifyId,
  832. }
  833. edbInfoListResp = append(edbInfoListResp, &info)
  834. } else if !mappgingFlag && residualFlag {
  835. info := residual_analysis_model.DetailEdbInfoList{
  836. IndexType: 1,
  837. EdbName: dependentEdbInfo.EdbName + "映射" + independentEdbInfo.EdbName,
  838. EdbNameEn: dependentEdbInfo.EdbNameEn,
  839. Unit: dependentEdbInfo.Unit,
  840. UnitEn: dependentEdbInfo.UnitEn,
  841. Frequency: dependentEdbInfo.Frequency,
  842. FrequencyEn: dependentEdbInfo.FrequencyEn,
  843. ClassifyId: dependentEdbInfo.ClassifyId,
  844. }
  845. edbInfoListResp = append(edbInfoListResp, &info)
  846. }
  847. resp := residual_analysis_model.ResidualAnalysisDetailResp{
  848. ConfigInfo: &configInfo,
  849. EdbInfoList: edbInfoListResp,
  850. ResidualType: mapping.ResidualType,
  851. }
  852. return resp, nil
  853. }
  854. func SaveResidualAnalysisConfig(req residual_analysis_model.ResidualAnalysisReq, sysUser *system.Admin) (int64, error) {
  855. config := residual_analysis_model.ResidualAnalysisConfigVo{
  856. DateType: req.DateType,
  857. StartDate: req.StartDate,
  858. EndDate: req.EndDate,
  859. IsOrder: req.IsOrder,
  860. IndexType: req.IndexType,
  861. LeadValue: req.LeadValue,
  862. LeadFrequency: req.LeadFrequency,
  863. LeftIndexMin: req.LeftIndexMin,
  864. LeftIndexMax: req.LeftIndexMax,
  865. RightIndexMin: req.RightIndexMin,
  866. RightIndexMax: req.RightIndexMax,
  867. ResidualIndexMin: req.ResidualIndexMin,
  868. ResidualIndexMax: req.ResidualIndexMax,
  869. ContrastIndexMin: req.ContrastIndexMin,
  870. ContrastIndexMax: req.ContrastIndexMax,
  871. }
  872. // 转换为json格式
  873. configJson, err := json.Marshal(config)
  874. if err != nil {
  875. return 0, err
  876. }
  877. // 新增or更新
  878. /*
  879. var condition string
  880. var pars []interface{}
  881. if req.EdbInfoId > 0 {
  882. condition += " and edb_info_id=?"
  883. pars = append(pars, req.EdbInfoId)
  884. configMappings, err := residual_analysis_model.GetConfigMappingListByCondition(condition, pars)
  885. if err != nil {
  886. return 0, err
  887. }
  888. if len(configMappings) > 0 {
  889. mapping := configMappings[0]
  890. configInfo, err := residual_analysis_model.GetResidualAnalysisConfigById(mapping.CalculateResidualAnalysisConfigId)
  891. if err != nil {
  892. return 0, err
  893. }
  894. configInfo.Config = string(configJson)
  895. err = residual_analysis_model.UpdateResidualAnalysisConfig(configInfo)
  896. if err != nil {
  897. return 0, err
  898. }
  899. }
  900. }*/
  901. var configId int64
  902. if req.ConfigId > 0 {
  903. configInfo, err := residual_analysis_model.GetResidualAnalysisConfigById(req.ConfigId)
  904. if err != nil {
  905. return 0, err
  906. }
  907. if configInfo.CalculateResidualAnalysisConfigId == 0 {
  908. return 0, fmt.Errorf("未找到配置信息")
  909. }
  910. configId = int64(configInfo.CalculateResidualAnalysisConfigId)
  911. configInfo.Config = string(configJson)
  912. err = residual_analysis_model.UpdateResidualAnalysisConfig(configInfo)
  913. if err != nil {
  914. return 0, err
  915. }
  916. } else {
  917. analysisConfig := residual_analysis_model.CalculateResidualAnalysisConfig{
  918. Config: string(configJson),
  919. SysUserId: sysUser.AdminId,
  920. CreateTime: time.Now(),
  921. ModifyTime: time.Now(),
  922. }
  923. configId, err = residual_analysis_model.SaveResidualAnalysisConfig(analysisConfig)
  924. if err != nil {
  925. return 0, err
  926. }
  927. }
  928. return configId, nil
  929. }
  930. func CheckResidualAnalysisExist(configId int) (int64, error) {
  931. configMappingList, err := residual_analysis_model.GetConfigMappingListByConfigId(configId)
  932. if err != nil {
  933. return 0, err
  934. }
  935. var configMapping residual_analysis_model.CalculateResidualAnalysisConfigMapping
  936. for _, mapping := range configMappingList {
  937. if mapping.IndexType == 2 {
  938. configMapping = mapping
  939. }
  940. }
  941. return configMapping.EdbInfoId, nil
  942. }