residual_analysis_service.go 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176
  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 -1:
  42. startDate = req.StartDate
  43. endDate = req.EndDate
  44. case 0:
  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].ChartWidth = 3
  205. mappingEdb[i].MinValue = indexMin
  206. mappingEdb[i].MaxValue = indexMax
  207. mappingEdb[i].LeftIndexMin = req.RightIndexMin
  208. mappingEdb[i].LeftIndexMax = req.RightIndexMax
  209. mappingEdb[i].RightIndexMin = req.LeftIndexMin
  210. mappingEdb[i].RightIndexMax = req.LeftIndexMax
  211. } else {
  212. mappingEdb[i].IsAxis = 0
  213. mappingEdb[i].ChartColor = `#F00`
  214. mappingEdb[i].ChartWidth = 1
  215. mappingEdb[i].LeftIndexMin = req.RightIndexMin
  216. mappingEdb[i].LeftIndexMax = req.RightIndexMax
  217. mappingEdb[i].RightIndexMin = req.LeftIndexMin
  218. mappingEdb[i].RightIndexMax = req.LeftIndexMax
  219. }
  220. }
  221. reverse(mappingEdb)
  222. return mappingEdb, R2, nil
  223. }
  224. // 双指针法
  225. func reverse(slice []residual_analysis_model.ResidualAnalysisChartEdbInfoMapping) {
  226. left, right := 0, len(slice)-1
  227. for left < right {
  228. // 交换元素
  229. slice[left], slice[right] = slice[right], slice[left]
  230. left++
  231. right--
  232. }
  233. }
  234. 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) {
  235. // 计算公式:Y=aX+b,Y为映射后的指标,X为自变量指标
  236. // 正序:a=(L2-L1)/(R2-R1) b=L2-R2*a
  237. // 逆序:a=(L2-L1)/(R1-R2) b=L2-R1*a
  238. // L2:左轴下限 R2:右轴上限 L1:左轴上限 R1:右轴下限
  239. var a, b, r float64
  240. // 映射残差 计算a,b
  241. if req.ResidualType == 1 {
  242. if req.IsOrder {
  243. a = (req.LeftIndexMax - req.LeftIndexMin) / (req.RightIndexMin - req.RightIndexMax)
  244. b = req.LeftIndexMax - req.RightIndexMin*a
  245. } else {
  246. a = (req.LeftIndexMax - req.LeftIndexMin) / (req.RightIndexMax - req.RightIndexMin)
  247. b = req.LeftIndexMax - req.RightIndexMax*a
  248. }
  249. }
  250. //dataAList := edbInfoMappingA.DataList.([]*data_manage.EdbDataList)
  251. dataList, ok := edbInfoMappingB.DataList.([]*data_manage.EdbDataList)
  252. if !ok {
  253. return nil, a, b, r, fmt.Errorf("数据类型转换失败")
  254. }
  255. // 指标B数据补充
  256. // 新建一个切片来保存补充的数据
  257. var replenishDataList []*data_manage.EdbDataList
  258. for index := 0; index < len(dataList)-1; index++ {
  259. // 获取当前数据和下一个数据
  260. beforeIndexData := dataList[index]
  261. afterIndexData := dataList[index+1]
  262. // 从最早时间开始,补充时间为自然日
  263. for utils.IsMoreThanOneDay(beforeIndexData.DataTime, afterIndexData.DataTime) {
  264. // 创建补充数据
  265. nextDay := utils.GetNextDay(beforeIndexData.DataTime)
  266. toTime := utils.StringToTime(nextDay)
  267. replenishIndexData := data_manage.EdbDataList{
  268. DataTime: nextDay, // 计算下一个自然日
  269. DataTimestamp: toTime.UnixMilli(),
  270. Value: beforeIndexData.Value, // 可以选择使用前一天的值,或者其他逻辑来计算值
  271. }
  272. // 将补充数据加入补充数据列表
  273. replenishDataList = append(replenishDataList, &replenishIndexData)
  274. // 更新 beforeIndexData 为新创建的补充数据
  275. beforeIndexData = &replenishIndexData
  276. }
  277. }
  278. // 将补充数据插入原始数据列表
  279. dataList = append(dataList, replenishDataList...)
  280. // 排序
  281. sort.Sort(ByDataTime(dataList))
  282. // 拟合残差 计算a,b
  283. var coordinateList []utils.Coordinate
  284. var replenishADataList []*data_manage.EdbDataList
  285. var replenishBDataList []*data_manage.EdbDataList
  286. if req.ResidualType == 2 {
  287. //
  288. // 因变量指标也转换为日度
  289. for index := 0; index < len(fullADataList)-1; index++ {
  290. // 获取当前数据和下一个数据
  291. beforeIndexData := fullADataList[index]
  292. afterIndexData := fullADataList[index+1]
  293. replenishADataList = append(replenishADataList, beforeIndexData)
  294. // 从最早时间开始,补充时间为自然日
  295. if utils.IsMoreThanOneDay(beforeIndexData.DataTime, afterIndexData.DataTime) {
  296. for {
  297. // 创建补充数据
  298. nextDay := utils.GetNextDay(beforeIndexData.DataTime)
  299. toTime := utils.StringToTime(nextDay)
  300. replenishIndexData := data_manage.EdbDataList{
  301. DataTime: nextDay, // 计算下一个自然日
  302. DataTimestamp: toTime.UnixMilli(),
  303. Value: beforeIndexData.Value, // 可以选择使用前一天的值,或者其他逻辑来计算值
  304. }
  305. // 将补充数据加入补充数据列表
  306. replenishADataList = append(replenishADataList, &replenishIndexData)
  307. // 更新 beforeIndexData 为新创建的补充数据
  308. beforeIndexData = &replenishIndexData
  309. // 检查是否还需要继续补充数据
  310. if !utils.IsMoreThanOneDay(beforeIndexData.DataTime, afterIndexData.DataTime) {
  311. break
  312. }
  313. }
  314. }
  315. }
  316. replenishADataList = append(replenishADataList, fullADataList[len(fullADataList)-1])
  317. // 自变量指标也转换为日度
  318. for index := 0; index < len(fullBDataList)-1; index++ {
  319. // 获取当前数据和下一个数据
  320. beforeIndexData := fullBDataList[index]
  321. afterIndexData := fullBDataList[index+1]
  322. replenishBDataList = append(replenishBDataList, beforeIndexData)
  323. // 从最早时间开始,补充时间为自然日
  324. if utils.IsMoreThanOneDay(beforeIndexData.DataTime, afterIndexData.DataTime) {
  325. for {
  326. // 创建补充数据
  327. nextDay := utils.GetNextDay(beforeIndexData.DataTime)
  328. toTime := utils.StringToTime(nextDay)
  329. replenishIndexData := data_manage.EdbDataList{
  330. DataTime: nextDay, // 计算下一个自然日
  331. DataTimestamp: toTime.UnixMilli(),
  332. Value: beforeIndexData.Value, // 可以选择使用前一天的值,或者其他逻辑来计算值
  333. }
  334. // 将补充数据加入补充数据列表
  335. replenishBDataList = append(replenishBDataList, &replenishIndexData)
  336. // 更新 beforeIndexData 为新创建的补充数据
  337. beforeIndexData = &replenishIndexData
  338. // 检查是否还需要继续补充数据
  339. if !utils.IsMoreThanOneDay(beforeIndexData.DataTime, afterIndexData.DataTime) {
  340. break
  341. }
  342. }
  343. }
  344. }
  345. replenishBDataList = append(replenishBDataList, fullBDataList[len(fullBDataList)-1])
  346. // replenishADataList --> map
  347. replenishADataMap := make(map[string]*data_manage.EdbDataList)
  348. for _, indexData := range replenishADataList {
  349. 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))) {
  350. replenishADataMap[indexData.DataTime] = indexData
  351. }
  352. }
  353. for _, indexData := range replenishBDataList {
  354. if _, ok = replenishADataMap[indexData.DataTime]; ok {
  355. coordinate := utils.Coordinate{
  356. X: indexData.Value,
  357. Y: replenishADataMap[indexData.DataTime].Value,
  358. }
  359. coordinateList = append(coordinateList, coordinate)
  360. }
  361. }
  362. a, b = utils.GetLinearResult(coordinateList)
  363. r = utils.ComputeCorrelation(coordinateList)
  364. }
  365. // 填充映射指标值 使得时间长度一致
  366. dataList = FillDataBList(dataList, edbInfoMappingA)
  367. // 根据指标A的时间key,在B的映射指标中筛选出对应的值
  368. var dataBList []*data_manage.EdbDataList
  369. var indexMax, indexMin float64
  370. if len(dataList) > 0 {
  371. indexMax = dataList[0].Value
  372. indexMin = dataList[0].Value
  373. for _, indexData := range dataList {
  374. if _, ok := indexADataMap[indexData.DataTime]; ok {
  375. indexDataCopy := *indexData
  376. // 计算指标B映射值
  377. indexDataCopy.Value = math.Round((a*indexData.Value+b)*10000) / 10000
  378. // 比较最大值
  379. if indexData.Value > indexMax {
  380. indexMax = indexData.Value
  381. }
  382. // 比较最小值
  383. if indexData.Value < indexMin {
  384. indexMin = indexData.Value
  385. }
  386. // 将副本添加到 dataBList
  387. dataBList = append(dataBList, &indexDataCopy)
  388. }
  389. }
  390. }
  391. mappingEdbList := make([]residual_analysis_model.ResidualAnalysisChartEdbInfoMapping, len(originalEdbList))
  392. copy(mappingEdbList, originalEdbList)
  393. for i, mapping := range mappingEdbList {
  394. if mapping.EdbInfoId != req.EdbInfoIdA {
  395. mappingEdbList[i].EdbInfoId = 0
  396. mappingEdbList[i].EdbCode = ""
  397. mappingEdbList[i].IsAxis = 1
  398. mappingEdbList[i].EdbName = edbInfoMappingB.EdbName + "映射" + edbInfoMappingA.EdbName
  399. if req.IndexType == 2 {
  400. if req.LeadValue > 0 {
  401. mappingEdbList[i].EdbName = edbInfoMappingB.EdbName + "映射" + edbInfoMappingA.EdbName + "(领先" + strconv.Itoa(req.LeadValue) + req.LeadFrequency + ")"
  402. }
  403. }
  404. mappingEdbList[i].DataList = dataBList
  405. mappingEdbList[i].MinValue = indexMin
  406. mappingEdbList[i].MaxValue = indexMax
  407. }
  408. }
  409. return mappingEdbList, a, b, r, nil
  410. }
  411. // FillDataBList 填充B的数据 使得与A的时间保持一致
  412. func FillDataBList(dataList []*data_manage.EdbDataList, edbInfoMappingA *data_manage.ChartEdbInfoMapping) []*data_manage.EdbDataList {
  413. dataAList, ok := edbInfoMappingA.DataList.([]*data_manage.EdbDataList)
  414. if !ok {
  415. return nil
  416. }
  417. for utils.StringToTime(dataList[len(dataList)-1].DataTime).Before(utils.StringToTime(dataAList[len(dataAList)-1].DataTime)) {
  418. // 使用A的时间填充时间差
  419. timeDiff := utils.GetNextDayN(dataList[len(dataList)-1].DataTime, 1)
  420. // 创建新的数据点并填充 前值填充
  421. newDataPoint := &data_manage.EdbDataList{
  422. DataTime: timeDiff,
  423. Value: dataList[len(dataList)-1].Value,
  424. DataTimestamp: utils.StringToTime(timeDiff).UnixMilli(),
  425. }
  426. // 将新数据点添加到dataList末尾
  427. dataList = append(dataList, newDataPoint)
  428. }
  429. return dataList
  430. }
  431. type ByDataTime []*data_manage.EdbDataList
  432. func (a ByDataTime) Len() int {
  433. return len(a)
  434. }
  435. func (a ByDataTime) Swap(i, j int) {
  436. a[i], a[j] = a[j], a[i]
  437. }
  438. func (a ByDataTime) Less(i, j int) bool {
  439. t1 := utils.StringToTime(a[i].DataTime)
  440. t2 := utils.StringToTime(a[j].DataTime)
  441. return t1.Before(t2)
  442. }
  443. 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) {
  444. var fullADataList, fullBDataList []*data_manage.EdbDataList
  445. for _, v := range mappingList {
  446. var edbInfoMapping residual_analysis_model.ResidualAnalysisChartEdbInfoMapping
  447. edbInfoMapping.EdbInfoType = 1
  448. edbInfoMapping.IsOrder = false
  449. edbInfoMapping.IsAxis = 1
  450. edbInfoMapping.ChartColor = `#00F`
  451. edbInfoMapping.ChartWidth = 3
  452. edbInfoMapping.EdbName = v.EdbName
  453. edbInfoMapping.LeftIndexMin = req.LeftIndexMin
  454. edbInfoMapping.LeftIndexMax = req.LeftIndexMax
  455. edbInfoMapping.RightIndexMin = req.RightIndexMin
  456. edbInfoMapping.RightIndexMax = req.RightIndexMax
  457. edbInfoMapping.ResidualIndexMin = req.ResidualIndexMin
  458. edbInfoMapping.ResidualIndexMax = req.ResidualIndexMax
  459. edbInfoMapping.ContrastIndexMin = req.ContrastIndexMin
  460. edbInfoMapping.ContrastIndexMax = req.ContrastIndexMax
  461. // 获取图表中的指标数据
  462. dataList, err := data_manage.GetEdbDataList(v.Source, v.SubSource, v.EdbInfoId, startDate, endDate)
  463. if err != nil {
  464. return nil, nil, nil, fmt.Errorf("获取指标数据失败,Err:%s", err.Error())
  465. }
  466. if dataList == nil && len(dataList) == 0 {
  467. return nil, nil, nil, fmt.Errorf("指标数据为空")
  468. }
  469. // 重新获取指标数据 产品要求需要和计算指标-拟合残差逻辑保持一致
  470. fullDataList, err := data_manage.GetEdbDataList(v.Source, v.SubSource, v.EdbInfoId, "", "")
  471. if err != nil {
  472. return nil, nil, nil, fmt.Errorf("获取指标数据失败,Err:%s", err.Error())
  473. }
  474. if v.EdbInfoId == req.EdbInfoIdB {
  475. edbInfoMapping.LeadValue = req.LeadValue
  476. edbInfoMapping.LeadUnit = req.LeadFrequency
  477. edbInfoMapping.EdbInfoType = req.IndexType
  478. edbInfoMapping.IsOrder = req.IsOrder
  479. edbInfoMapping.IsAxis = 0
  480. edbInfoMapping.ChartColor = `#F00`
  481. edbInfoMapping.ChartWidth = 1
  482. // 领先指标 dataList进行数据处理
  483. if req.IndexType == 2 {
  484. if req.LeadValue < 0 {
  485. return nil, nil, nil, fmt.Errorf("领先值不能小于0")
  486. } else if req.LeadValue > 0 {
  487. edbInfoMapping.EdbName = v.EdbName + "(领先" + strconv.Itoa(req.LeadValue) + req.LeadFrequency + ")"
  488. for _, indexData := range dataList {
  489. switch req.LeadFrequency {
  490. case "天":
  491. indexData.DataTime = utils.GetNextDayN(indexData.DataTime, req.LeadValue)
  492. case "周":
  493. indexData.DataTime = utils.GetNextDayN(indexData.DataTime, req.LeadValue*7)
  494. case "月":
  495. indexData.DataTime = utils.TimeToString(utils.AddDate(utils.StringToTime(indexData.DataTime), 0, req.LeadValue), utils.YearMonthDay)
  496. case "季":
  497. indexData.DataTime = utils.TimeToString(utils.AddDate(utils.StringToTime(indexData.DataTime), 0, req.LeadValue*3), utils.YearMonthDay)
  498. case "年":
  499. indexData.DataTime = utils.TimeToString(utils.AddDate(utils.StringToTime(indexData.DataTime), req.LeadValue, 0), utils.YearMonthDay)
  500. }
  501. indexData.DataTimestamp = utils.StringToTime(indexData.DataTime).UnixMilli()
  502. }
  503. }
  504. }
  505. edbInfoMappingB.DataList = dataList
  506. fullBDataList = fullDataList
  507. } else {
  508. edbInfoMappingA.DataList = dataList
  509. fullADataList = fullDataList
  510. }
  511. edbInfoMapping.EdbInfoId = v.EdbInfoId
  512. edbInfoMapping.EdbCode = v.EdbCode
  513. edbInfoMapping.Unit = v.Unit
  514. edbInfoMapping.Frequency = v.Frequency
  515. edbInfoMapping.Source = v.Source
  516. edbInfoMapping.SourceName = v.SourceName
  517. edbInfoMapping.MinValue = v.MinValue
  518. edbInfoMapping.MaxValue = v.MaxValue
  519. edbInfoMapping.LatestDate = v.LatestDate
  520. edbInfoMapping.LatestValue = v.LatestValue
  521. edbInfoMapping.DataList = dataList
  522. originalEdbList = append(originalEdbList, edbInfoMapping)
  523. }
  524. return originalEdbList, fullADataList, fullBDataList, nil
  525. }
  526. func ContrastPreview(indexCode string, startTime string, endTime string) (residual_analysis_model.ResidualAnalysisChartEdbInfoMapping, error) {
  527. var condition string
  528. var pars []interface{}
  529. if indexCode != "" {
  530. condition += " and edb_code=?"
  531. pars = append(pars, indexCode)
  532. }
  533. edbInfo, err := data_manage.GetEdbInfoByCondition(condition, pars)
  534. if err != nil {
  535. return residual_analysis_model.ResidualAnalysisChartEdbInfoMapping{}, err
  536. }
  537. if edbInfo == nil {
  538. return residual_analysis_model.ResidualAnalysisChartEdbInfoMapping{}, fmt.Errorf("指标不存在")
  539. }
  540. dataList, err := data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, startTime, endTime)
  541. var resp residual_analysis_model.ResidualAnalysisChartEdbInfoMapping
  542. resp.EdbInfoId = edbInfo.EdbInfoId
  543. resp.EdbCode = edbInfo.EdbCode
  544. resp.ChartColor = "#F00"
  545. resp.SourceName = edbInfo.SourceName
  546. resp.EdbName = edbInfo.EdbName
  547. resp.Unit = edbInfo.Unit
  548. resp.Frequency = edbInfo.Frequency
  549. resp.MinValue = edbInfo.MinValue
  550. resp.MaxValue = edbInfo.MaxValue
  551. resp.DataList = dataList
  552. return resp, nil
  553. }
  554. func SaveResidualAnalysis(req residual_analysis_model.ResidualAnalysisIndexSaveReq, sysUser *system.Admin) error {
  555. // 验证分类是否存在
  556. classifyCount, err := data_manage.GetEdbClassifyCountById(req.ClassifyId)
  557. if err != nil {
  558. return err
  559. }
  560. if classifyCount <= 0 {
  561. return fmt.Errorf("分类不存在")
  562. }
  563. // 校验名称是否重复
  564. var condition string
  565. var pars []interface{}
  566. condition += " and source = ? AND edb_name=?"
  567. pars = append(pars, req.Source, req.EdbName)
  568. edbInfoByCondition, err := data_manage.GetEdbInfoByCondition(condition, pars)
  569. if err != nil && err.Error() != utils.ErrNoRow() {
  570. return err
  571. }
  572. // 获取指标数据最大值 最小值 最后更新时间 最后更新时间对应的值 开始时间 最后时间
  573. var indexMax, indexMin, indexLatestValue float64
  574. var indexLatestDate, startDate string
  575. if len(req.DataList) > 0 {
  576. latestTime, _ := time.Parse(utils.YearMonthDay, req.DataList[0].DataTime)
  577. firstTime, _ := time.Parse(utils.YearMonthDay, req.DataList[0].DataTime)
  578. for _, data := range req.DataList {
  579. // 比较最大值
  580. if data.Value > indexMax {
  581. indexMax = data.Value
  582. }
  583. // 比较最小值
  584. if data.Value < indexMin {
  585. indexMin = data.Value
  586. }
  587. // 比较最新时间和对应值
  588. currentTime, err := time.Parse(utils.YearMonthDay, data.DataTime)
  589. if err != nil {
  590. // 时间解析失败,跳过此项
  591. continue
  592. }
  593. // 如果当前时间更晚
  594. if !currentTime.Before(latestTime) {
  595. latestTime = currentTime
  596. indexLatestDate = data.DataTime
  597. indexLatestValue = data.Value
  598. }
  599. // 取最小时间
  600. if !currentTime.After(firstTime) {
  601. firstTime = currentTime
  602. startDate = data.DataTime
  603. }
  604. }
  605. }
  606. // 更新保存指标和配置的映射关系
  607. mappingList, err := residual_analysis_model.GetConfigMappingListByConfigId(req.ConfigId)
  608. if err != nil {
  609. return err
  610. }
  611. // 判断是更新还是修改 看指标配置映射中,是否存在对应指标 存在 则更新 不存在 则新增
  612. var edbInfoMapping residual_analysis_model.CalculateResidualAnalysisConfigMapping
  613. for _, mapping := range mappingList {
  614. if req.IndexType == mapping.IndexType && req.IndexType != 0 {
  615. edbInfoMapping = mapping
  616. }
  617. }
  618. var edbInfoId int64
  619. var edbCode string
  620. // 更新or新增
  621. if edbInfoMapping.EdbInfoId > 0 {
  622. // 查询指标库指标
  623. edbInfo, err := data_manage.GetEdbInfoById(int(edbInfoMapping.EdbInfoId))
  624. if err != nil {
  625. return err
  626. }
  627. if edbInfo == nil {
  628. return fmt.Errorf("指标不存在")
  629. }
  630. edbInfoId = int64(edbInfo.EdbInfoId)
  631. edbCode = edbInfo.EdbCode
  632. if edbInfoByCondition != nil && edbInfoByCondition.EdbInfoId != edbInfo.EdbInfoId {
  633. return fmt.Errorf("指标名称重复")
  634. }
  635. // 须补充更新指标最大值,最小值,数据最新时间,数据最新值
  636. edbInfo.StartDate = startDate
  637. edbInfo.EndDate = indexLatestDate
  638. edbInfo.MaxValue = indexMax
  639. edbInfo.MinValue = indexMin
  640. edbInfo.LatestDate = indexLatestDate
  641. edbInfo.LatestValue = indexLatestValue
  642. edbInfo.Unit = req.Unit
  643. edbInfo.Frequency = req.Frequency
  644. edbInfo.ClassifyId = req.ClassifyId
  645. edbInfo.EdbName = req.EdbName
  646. err = edbInfo.Update([]string{"min_value", "max_value", "latest_date", "latest_value", "unit", "frequency", "classify_id", "edb_name"})
  647. if err != nil {
  648. return err
  649. }
  650. // 删除对应得指标数据
  651. err = residual_analysis_model.DeleteResidualAnalysisDataByEdbCode(edbInfo.EdbCode)
  652. if err != nil {
  653. return fmt.Errorf("删除指标数据失败")
  654. }
  655. } else {
  656. if edbInfoByCondition != nil {
  657. return fmt.Errorf("指标名称重复")
  658. }
  659. // 新增指标
  660. edbCode, err = utils.GenerateEdbCode(1, "")
  661. if err != nil {
  662. return err
  663. }
  664. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  665. edbInfoId, err = data_manage.AddEdbInfo(&data_manage.EdbInfo{
  666. EdbCode: edbCode,
  667. UniqueCode: utils.MD5(utils.CHART_PREFIX + "_" + timestamp),
  668. EdbName: req.EdbName,
  669. EdbNameEn: req.EdbNameEn,
  670. ClassifyId: req.ClassifyId,
  671. EdbType: 2,
  672. Unit: req.Unit,
  673. UnitEn: req.UnitEn,
  674. Frequency: req.Frequency,
  675. Source: req.Source,
  676. SourceName: "残差分析",
  677. Calendar: req.Calendar,
  678. SysUserRealName: sysUser.RealName,
  679. SysUserId: sysUser.AdminId,
  680. StartDate: startDate,
  681. EndDate: indexLatestDate,
  682. LatestDate: indexLatestDate,
  683. LatestValue: indexLatestValue,
  684. MinValue: indexMin,
  685. MaxValue: indexMax,
  686. CreateTime: time.Now(),
  687. ModifyTime: time.Now(),
  688. })
  689. if err != nil {
  690. return err
  691. }
  692. // 新增指标配置关系
  693. _, err = residual_analysis_model.SaveConfigMapping(residual_analysis_model.CalculateResidualAnalysisConfigMapping{
  694. CalculateResidualAnalysisConfigId: req.ConfigId,
  695. EdbInfoId: edbInfoId,
  696. ResidualType: req.ResidualType,
  697. IndexType: req.IndexType,
  698. CreateTime: time.Now(),
  699. ModifyTime: time.Now(),
  700. })
  701. if err != nil {
  702. return err
  703. }
  704. // 保存溯源信息
  705. {
  706. fromEdbInfoList, err := data_manage.GetEdbInfoByIdList([]int{req.EdbInfoIdA, req.EdbInfoIdB})
  707. if err != nil {
  708. return fmt.Errorf("获取失败")
  709. }
  710. edbCalculateMappingInfoList := make([]*data_manage.EdbInfoCalculateMapping, 0, len(fromEdbInfoList))
  711. for _, fromEdbInfo := range fromEdbInfoList {
  712. // 创建新对象并直接使用指针
  713. edbCalculateMappingInfo := &data_manage.EdbInfoCalculateMapping{
  714. EdbInfoId: int(edbInfoId),
  715. Source: req.Source,
  716. SourceName: "残差分析",
  717. EdbCode: edbCode,
  718. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  719. FromEdbCode: fromEdbInfo.EdbCode,
  720. FromEdbName: fromEdbInfo.EdbName,
  721. FromSource: fromEdbInfo.Source,
  722. FromSourceName: fromEdbInfo.SourceName,
  723. CreateTime: time.Now(),
  724. ModifyTime: time.Now(),
  725. }
  726. edbCalculateMappingInfoList = append(edbCalculateMappingInfoList, edbCalculateMappingInfo)
  727. }
  728. err = data_manage.AddEdbInfoCalculateMappingMulti(edbCalculateMappingInfoList)
  729. if err != nil {
  730. return fmt.Errorf("保存溯源信息失败,Err:" + err.Error())
  731. }
  732. }
  733. }
  734. // 新增数据
  735. for i := range req.DataList {
  736. req.DataList[i].EdbDataId = 0
  737. req.DataList[i].EdbInfoId = int(edbInfoId)
  738. req.DataList[i].EdbCode = edbCode
  739. req.DataList[i].CreateTime = time.Now()
  740. req.DataList[i].ModifyTime = time.Now()
  741. }
  742. _, err = residual_analysis_model.AddResidualAnalysisData(req.DataList)
  743. if err != nil {
  744. return err
  745. }
  746. // 新增自变量 因变量与配置得关系 配置中不存在该指标,则新增
  747. var indexMap = make(map[int64]residual_analysis_model.CalculateResidualAnalysisConfigMapping)
  748. for _, mapping := range mappingList {
  749. indexMap[mapping.EdbInfoId] = mapping
  750. }
  751. if _, ok := indexMap[int64(req.EdbInfoIdA)]; !ok {
  752. _, err = residual_analysis_model.SaveConfigMapping(residual_analysis_model.CalculateResidualAnalysisConfigMapping{
  753. CalculateResidualAnalysisConfigId: req.ConfigId,
  754. EdbInfoId: int64(req.EdbInfoIdA),
  755. ResidualType: req.ResidualType,
  756. IndexType: 3,
  757. CreateTime: time.Now(),
  758. ModifyTime: time.Now(),
  759. })
  760. if err != nil {
  761. return err
  762. }
  763. }
  764. if _, ok := indexMap[int64(req.EdbInfoIdB)]; !ok {
  765. _, err = residual_analysis_model.SaveConfigMapping(residual_analysis_model.CalculateResidualAnalysisConfigMapping{
  766. CalculateResidualAnalysisConfigId: req.ConfigId,
  767. EdbInfoId: int64(req.EdbInfoIdB),
  768. ResidualType: req.ResidualType,
  769. IndexType: 4,
  770. CreateTime: time.Now(),
  771. ModifyTime: time.Now(),
  772. })
  773. if err != nil {
  774. return err
  775. }
  776. }
  777. //添加es
  778. data.AddOrEditEdbInfoToEs(int(edbInfoId))
  779. return nil
  780. }
  781. // 刷新残差指标时间戳
  782. /*func init() {
  783. //func main() {
  784. fmt.Println("开始刷新残差指标时间戳")
  785. var condition string
  786. var pars []interface{}
  787. condition = " and edb_data_id > 0 order by edb_data_id asc limit 500"
  788. for {
  789. dataList, err := residual_analysis_model.GetDataListByCondition(condition, pars)
  790. if err != nil {
  791. utils.FileLog.Info("获取数据失败, 错误:" + err.Error())
  792. fmt.Println("获取数据失败, 错误:" + err.Error())
  793. return
  794. }
  795. // 如果没有数据,退出循环
  796. if dataList == nil || len(dataList) == 0 {
  797. utils.FileLog.Info("没有更多数据需要更新")
  798. fmt.Println("没有更多数据需要更新")
  799. break
  800. }
  801. // 更新数据时间戳
  802. for _, data := range dataList {
  803. data.DataTimeStamp = utils.StringToTime(data.DataTime).UnixMilli()
  804. data.ModifyTime = time.Now()
  805. data.CreateTime = time.Now()
  806. data.RefreshDataTimestamp([]string{"create_time", "modify_time", "data_timestamp"})
  807. }
  808. condition = " and edb_data_id > ? order by edb_data_id asc limit 500"
  809. pars = []interface{}{dataList[len(dataList)-1].EdbDataId}
  810. utils.FileLog.Info("刷新残差指标时间戳, 已处理数量:" + strconv.Itoa(len(dataList)))
  811. fmt.Println("刷新残差指标时间戳, 已处理数量:" + strconv.Itoa(len(dataList)))
  812. }
  813. fmt.Println("刷新残差指标时间戳完成")
  814. }*/
  815. func ResidualAnalysisDetail(edbInfoId int) (residual_analysis_model.ResidualAnalysisDetailResp, error) {
  816. // 通过指标配置映射表 拿到配置id,再获取关联的所有指标信息
  817. var condition string
  818. var pars []interface{}
  819. condition += " and edb_info_id=?"
  820. pars = append(pars, edbInfoId)
  821. mappingList, err := residual_analysis_model.GetConfigMappingListByCondition(condition, pars)
  822. if err != nil {
  823. return residual_analysis_model.ResidualAnalysisDetailResp{}, err
  824. }
  825. if len(mappingList) <= 0 {
  826. return residual_analysis_model.ResidualAnalysisDetailResp{}, fmt.Errorf("指标不存在")
  827. }
  828. mapping := mappingList[0]
  829. configMappingList, err := residual_analysis_model.GetConfigMappingListByConfigId(mapping.CalculateResidualAnalysisConfigId)
  830. if err != nil {
  831. return residual_analysis_model.ResidualAnalysisDetailResp{}, err
  832. }
  833. var edbInfoIdList []int64
  834. var edbInfoMap = make(map[int64]residual_analysis_model.CalculateResidualAnalysisConfigMapping)
  835. var mappgingFlag = false
  836. var residualFlag = false
  837. for _, v := range configMappingList {
  838. edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
  839. edbInfoMap[v.EdbInfoId] = v
  840. if v.IndexType == 1 {
  841. mappgingFlag = true
  842. } else if v.IndexType == 2 {
  843. residualFlag = true
  844. }
  845. }
  846. condition = ""
  847. pars = []interface{}{}
  848. condition += ` and edb_info_id in(` + utils.GetOrmInReplace(len(edbInfoIdList)) + `)`
  849. for _, id := range edbInfoIdList {
  850. pars = append(pars, id)
  851. }
  852. edbInfoList, err := data_manage.GetEdbInfoListByCond(condition, pars)
  853. if err != nil {
  854. return residual_analysis_model.ResidualAnalysisDetailResp{}, err
  855. }
  856. // 获取配置
  857. configInfo, err := residual_analysis_model.GetResidualAnalysisConfigById(mapping.CalculateResidualAnalysisConfigId)
  858. if err != nil {
  859. return residual_analysis_model.ResidualAnalysisDetailResp{}, err
  860. }
  861. var edbInfoListResp []*residual_analysis_model.DetailEdbInfoList
  862. var dependentEdbInfo residual_analysis_model.DetailEdbInfoList
  863. var independentEdbInfo residual_analysis_model.DetailEdbInfoList
  864. for _, edbInfo := range edbInfoList {
  865. var indexType int
  866. if _, ok := edbInfoMap[int64(edbInfo.EdbInfoId)]; ok {
  867. indexType = edbInfoMap[int64(edbInfo.EdbInfoId)].IndexType
  868. }
  869. info := residual_analysis_model.DetailEdbInfoList{
  870. EdbInfoId: edbInfo.EdbInfoId,
  871. EdbInfoType: edbInfo.EdbInfoType,
  872. IndexType: indexType,
  873. SourceName: edbInfo.SourceName,
  874. Source: edbInfo.Source,
  875. EdbCode: edbInfo.EdbCode,
  876. EdbName: edbInfo.EdbName,
  877. EdbNameEn: edbInfo.EdbNameEn,
  878. Unit: edbInfo.Unit,
  879. UnitEn: edbInfo.UnitEn,
  880. Frequency: edbInfo.Frequency,
  881. FrequencyEn: edbInfo.FrequencyEn,
  882. ClassifyId: edbInfo.ClassifyId,
  883. }
  884. edbInfoListResp = append(edbInfoListResp, &info)
  885. if indexType == 3 {
  886. dependentEdbInfo = info
  887. } else if indexType == 4 {
  888. independentEdbInfo = info
  889. }
  890. }
  891. // 补充表格中 映射指标或者残差指标
  892. if mappgingFlag && !residualFlag {
  893. info := residual_analysis_model.DetailEdbInfoList{
  894. IndexType: 2,
  895. EdbName: independentEdbInfo.EdbName + "映射残差/" + dependentEdbInfo.EdbName,
  896. EdbNameEn: dependentEdbInfo.EdbNameEn,
  897. Unit: dependentEdbInfo.Unit,
  898. UnitEn: dependentEdbInfo.UnitEn,
  899. Frequency: dependentEdbInfo.Frequency,
  900. FrequencyEn: dependentEdbInfo.FrequencyEn,
  901. ClassifyId: dependentEdbInfo.ClassifyId,
  902. }
  903. edbInfoListResp = append(edbInfoListResp, &info)
  904. } else if !mappgingFlag && residualFlag {
  905. info := residual_analysis_model.DetailEdbInfoList{
  906. IndexType: 1,
  907. EdbName: dependentEdbInfo.EdbName + "映射" + independentEdbInfo.EdbName,
  908. EdbNameEn: dependentEdbInfo.EdbNameEn,
  909. Unit: dependentEdbInfo.Unit,
  910. UnitEn: dependentEdbInfo.UnitEn,
  911. Frequency: dependentEdbInfo.Frequency,
  912. FrequencyEn: dependentEdbInfo.FrequencyEn,
  913. ClassifyId: dependentEdbInfo.ClassifyId,
  914. }
  915. edbInfoListResp = append(edbInfoListResp, &info)
  916. }
  917. resp := residual_analysis_model.ResidualAnalysisDetailResp{
  918. ConfigInfo: &configInfo,
  919. EdbInfoList: edbInfoListResp,
  920. ResidualType: mapping.ResidualType,
  921. }
  922. return resp, nil
  923. }
  924. func SaveResidualAnalysisConfig(req residual_analysis_model.ResidualAnalysisReq, sysUser *system.Admin) (int64, error) {
  925. config := residual_analysis_model.ResidualAnalysisConfigVo{
  926. ResidualType: req.ResidualType,
  927. DateType: req.DateType,
  928. StartDate: req.StartDate,
  929. EndDate: req.EndDate,
  930. IsOrder: req.IsOrder,
  931. IndexType: req.IndexType,
  932. LeadValue: req.LeadValue,
  933. LeadFrequency: req.LeadFrequency,
  934. LeftIndexMin: req.LeftIndexMin,
  935. LeftIndexMax: req.LeftIndexMax,
  936. RightIndexMin: req.RightIndexMin,
  937. RightIndexMax: req.RightIndexMax,
  938. ResidualIndexMin: req.ResidualIndexMin,
  939. ResidualIndexMax: req.ResidualIndexMax,
  940. ContrastIndexMin: req.ContrastIndexMin,
  941. ContrastIndexMax: req.ContrastIndexMax,
  942. }
  943. // 转换为json格式
  944. configJson, err := json.Marshal(config)
  945. if err != nil {
  946. return 0, err
  947. }
  948. // 新增or更新
  949. /*
  950. var condition string
  951. var pars []interface{}
  952. if req.EdbInfoId > 0 {
  953. condition += " and edb_info_id=?"
  954. pars = append(pars, req.EdbInfoId)
  955. configMappings, err := residual_analysis_model.GetConfigMappingListByCondition(condition, pars)
  956. if err != nil {
  957. return 0, err
  958. }
  959. if len(configMappings) > 0 {
  960. mapping := configMappings[0]
  961. configInfo, err := residual_analysis_model.GetResidualAnalysisConfigById(mapping.CalculateResidualAnalysisConfigId)
  962. if err != nil {
  963. return 0, err
  964. }
  965. configInfo.Config = string(configJson)
  966. err = residual_analysis_model.UpdateResidualAnalysisConfig(configInfo)
  967. if err != nil {
  968. return 0, err
  969. }
  970. }
  971. }*/
  972. var configId int64
  973. if req.ConfigId > 0 {
  974. configInfo, err := residual_analysis_model.GetResidualAnalysisConfigById(req.ConfigId)
  975. if err != nil {
  976. return 0, err
  977. }
  978. if configInfo.CalculateResidualAnalysisConfigId == 0 {
  979. return 0, fmt.Errorf("未找到配置信息")
  980. }
  981. configId = int64(configInfo.CalculateResidualAnalysisConfigId)
  982. configInfo.Config = string(configJson)
  983. err = residual_analysis_model.UpdateResidualAnalysisConfig(configInfo)
  984. if err != nil {
  985. return 0, err
  986. }
  987. } else {
  988. analysisConfig := residual_analysis_model.CalculateResidualAnalysisConfig{
  989. Config: string(configJson),
  990. SysUserId: sysUser.AdminId,
  991. CreateTime: time.Now(),
  992. ModifyTime: time.Now(),
  993. }
  994. configId, err = residual_analysis_model.SaveResidualAnalysisConfig(analysisConfig)
  995. if err != nil {
  996. return 0, err
  997. }
  998. }
  999. return configId, nil
  1000. }
  1001. func CheckResidualAnalysisExist(configId int) (int64, error) {
  1002. configMappingList, err := residual_analysis_model.GetConfigMappingListByConfigId(configId)
  1003. if err != nil {
  1004. return 0, err
  1005. }
  1006. var configMapping residual_analysis_model.CalculateResidualAnalysisConfigMapping
  1007. for _, mapping := range configMappingList {
  1008. if mapping.IndexType == 2 {
  1009. configMapping = mapping
  1010. }
  1011. }
  1012. return configMapping.EdbInfoId, nil
  1013. }