residual_analysis_service.go 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130
  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].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. // 重新获取指标数据 产品要求需要和计算指标-拟合残差逻辑保持一致
  467. fullDataList, err := data_manage.GetEdbDataList(v.Source, v.SubSource, v.EdbInfoId, "", "")
  468. if err != nil {
  469. return nil, nil, nil, fmt.Errorf("获取指标数据失败,Err:%s", err.Error())
  470. }
  471. if v.EdbInfoId == req.EdbInfoIdB {
  472. edbInfoMapping.LeadValue = req.LeadValue
  473. edbInfoMapping.LeadUnit = req.LeadFrequency
  474. edbInfoMapping.EdbInfoType = req.IndexType
  475. edbInfoMapping.IsOrder = req.IsOrder
  476. edbInfoMapping.IsAxis = 0
  477. edbInfoMapping.ChartColor = `#F00`
  478. edbInfoMapping.ChartWidth = 1
  479. // 领先指标 dataList进行数据处理
  480. if req.IndexType == 2 {
  481. if req.LeadValue < 0 {
  482. return nil, nil, nil, fmt.Errorf("领先值不能小于0")
  483. } else if req.LeadValue > 0 {
  484. edbInfoMapping.EdbName = v.EdbName + "(领先" + strconv.Itoa(req.LeadValue) + req.LeadFrequency + ")"
  485. for _, indexData := range dataList {
  486. switch req.LeadFrequency {
  487. case "天":
  488. indexData.DataTime = utils.GetNextDayN(indexData.DataTime, req.LeadValue)
  489. case "周":
  490. indexData.DataTime = utils.GetNextDayN(indexData.DataTime, req.LeadValue*7)
  491. case "月":
  492. indexData.DataTime = utils.TimeToString(utils.AddDate(utils.StringToTime(indexData.DataTime), 0, req.LeadValue), utils.YearMonthDay)
  493. case "季":
  494. indexData.DataTime = utils.TimeToString(utils.AddDate(utils.StringToTime(indexData.DataTime), 0, req.LeadValue*3), utils.YearMonthDay)
  495. case "年":
  496. indexData.DataTime = utils.TimeToString(utils.AddDate(utils.StringToTime(indexData.DataTime), req.LeadValue, 0), utils.YearMonthDay)
  497. }
  498. indexData.DataTimestamp = utils.StringToTime(indexData.DataTime).UnixMilli()
  499. }
  500. }
  501. }
  502. edbInfoMappingB.DataList = dataList
  503. fullBDataList = fullDataList
  504. } else {
  505. edbInfoMappingA.DataList = dataList
  506. fullADataList = fullDataList
  507. }
  508. edbInfoMapping.EdbInfoId = v.EdbInfoId
  509. edbInfoMapping.EdbCode = v.EdbCode
  510. edbInfoMapping.Unit = v.Unit
  511. edbInfoMapping.Frequency = v.Frequency
  512. edbInfoMapping.Source = v.Source
  513. edbInfoMapping.SourceName = v.SourceName
  514. edbInfoMapping.MinValue = v.MinValue
  515. edbInfoMapping.MaxValue = v.MaxValue
  516. edbInfoMapping.LatestDate = v.LatestDate
  517. edbInfoMapping.LatestValue = v.LatestValue
  518. edbInfoMapping.DataList = dataList
  519. originalEdbList = append(originalEdbList, edbInfoMapping)
  520. }
  521. return originalEdbList, fullADataList, fullBDataList, nil
  522. }
  523. func ContrastPreview(indexCode string, startTime string, endTime string) (residual_analysis_model.ResidualAnalysisChartEdbInfoMapping, error) {
  524. var condition string
  525. var pars []interface{}
  526. if indexCode != "" {
  527. condition += " and edb_code=?"
  528. pars = append(pars, indexCode)
  529. }
  530. edbInfo, err := data_manage.GetEdbInfoByCondition(condition, pars)
  531. if err != nil {
  532. return residual_analysis_model.ResidualAnalysisChartEdbInfoMapping{}, err
  533. }
  534. if edbInfo == nil {
  535. return residual_analysis_model.ResidualAnalysisChartEdbInfoMapping{}, fmt.Errorf("指标不存在")
  536. }
  537. dataList, err := data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, startTime, endTime)
  538. var resp residual_analysis_model.ResidualAnalysisChartEdbInfoMapping
  539. resp.EdbInfoId = edbInfo.EdbInfoId
  540. resp.EdbCode = edbInfo.EdbCode
  541. resp.ChartColor = "#F00"
  542. resp.SourceName = edbInfo.SourceName
  543. resp.EdbName = edbInfo.EdbName
  544. resp.Unit = edbInfo.Unit
  545. resp.Frequency = edbInfo.Frequency
  546. resp.MinValue = edbInfo.MinValue
  547. resp.MaxValue = edbInfo.MaxValue
  548. resp.DataList = dataList
  549. return resp, nil
  550. }
  551. func SaveResidualAnalysis(req residual_analysis_model.ResidualAnalysisIndexSaveReq, sysUser *system.Admin) error {
  552. // 验证分类是否存在
  553. classifyCount, err := data_manage.GetEdbClassifyCountById(req.ClassifyId)
  554. if err != nil {
  555. return err
  556. }
  557. if classifyCount <= 0 {
  558. return fmt.Errorf("分类不存在")
  559. }
  560. // 校验名称是否重复
  561. var condition string
  562. var pars []interface{}
  563. condition += " and source = ? AND edb_name=?"
  564. pars = append(pars, req.Source, req.EdbName)
  565. edbInfoByCondition, err := data_manage.GetEdbInfoByCondition(condition, pars)
  566. if err != nil && err.Error() != utils.ErrNoRow() {
  567. return err
  568. }
  569. // 获取指标数据最大值 最小值 最后更新时间 最后更新时间对应的值 开始时间 最后时间
  570. var indexMax, indexMin, indexLatestValue float64
  571. var indexLatestDate, startDate string
  572. if len(req.DataList) > 0 {
  573. latestTime, _ := time.Parse(utils.YearMonthDay, req.DataList[0].DataTime)
  574. firstTime, _ := time.Parse(utils.YearMonthDay, req.DataList[0].DataTime)
  575. for _, data := range req.DataList {
  576. // 比较最大值
  577. if data.Value > indexMax {
  578. indexMax = data.Value
  579. }
  580. // 比较最小值
  581. if data.Value < indexMin {
  582. indexMin = data.Value
  583. }
  584. // 比较最新时间和对应值
  585. currentTime, err := time.Parse(utils.YearMonthDay, data.DataTime)
  586. if err != nil {
  587. // 时间解析失败,跳过此项
  588. continue
  589. }
  590. // 如果当前时间更晚
  591. if !currentTime.Before(latestTime) {
  592. latestTime = currentTime
  593. indexLatestDate = data.DataTime
  594. indexLatestValue = data.Value
  595. }
  596. // 取最小时间
  597. if !currentTime.After(firstTime) {
  598. firstTime = currentTime
  599. startDate = data.DataTime
  600. }
  601. }
  602. }
  603. // 更新保存指标和配置的映射关系
  604. mappingList, err := residual_analysis_model.GetConfigMappingListByConfigId(req.ConfigId)
  605. if err != nil {
  606. return err
  607. }
  608. // 判断是更新还是修改 看指标配置映射中,是否存在对应指标 存在 则更新 不存在 则新增
  609. var edbInfoMapping residual_analysis_model.CalculateResidualAnalysisConfigMapping
  610. for _, mapping := range mappingList {
  611. if req.IndexType == mapping.IndexType && req.IndexType != 0 {
  612. edbInfoMapping = mapping
  613. }
  614. }
  615. var edbInfoId int64
  616. var edbCode string
  617. // 更新or新增
  618. if edbInfoMapping.EdbInfoId > 0 {
  619. // 查询指标库指标
  620. edbInfo, err := data_manage.GetEdbInfoById(int(edbInfoMapping.EdbInfoId))
  621. if err != nil {
  622. return err
  623. }
  624. if edbInfo == nil {
  625. return fmt.Errorf("指标不存在")
  626. }
  627. edbInfoId = int64(edbInfo.EdbInfoId)
  628. edbCode = edbInfo.EdbCode
  629. if edbInfoByCondition != nil && edbInfoByCondition.EdbInfoId != edbInfo.EdbInfoId {
  630. return fmt.Errorf("指标名称重复")
  631. }
  632. // 须补充更新指标最大值,最小值,数据最新时间,数据最新值
  633. edbInfo.StartDate = startDate
  634. edbInfo.EndDate = indexLatestDate
  635. edbInfo.MaxValue = indexMax
  636. edbInfo.MinValue = indexMin
  637. edbInfo.LatestDate = indexLatestDate
  638. edbInfo.LatestValue = indexLatestValue
  639. edbInfo.Unit = req.Unit
  640. edbInfo.Frequency = req.Frequency
  641. edbInfo.ClassifyId = req.ClassifyId
  642. edbInfo.EdbName = req.EdbName
  643. err = edbInfo.Update([]string{"min_value", "max_value", "latest_date", "latest_value", "unit", "frequency", "classify_id", "edb_name"})
  644. if err != nil {
  645. return err
  646. }
  647. // 删除对应得指标数据
  648. err = residual_analysis_model.DeleteResidualAnalysisDataByEdbCode(edbInfo.EdbCode)
  649. if err != nil {
  650. return fmt.Errorf("删除指标数据失败")
  651. }
  652. } else {
  653. if edbInfoByCondition != nil {
  654. return fmt.Errorf("指标名称重复")
  655. }
  656. // 新增指标
  657. edbCode, err = utils.GenerateEdbCode(1, "")
  658. if err != nil {
  659. return err
  660. }
  661. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  662. edbInfoId, err = data_manage.AddEdbInfo(&data_manage.EdbInfo{
  663. EdbCode: edbCode,
  664. UniqueCode: utils.MD5(utils.CHART_PREFIX + "_" + timestamp),
  665. EdbName: req.EdbName,
  666. EdbNameEn: req.EdbNameEn,
  667. ClassifyId: req.ClassifyId,
  668. EdbType: 2,
  669. Unit: req.Unit,
  670. UnitEn: req.UnitEn,
  671. Frequency: req.Frequency,
  672. Source: req.Source,
  673. SourceName: "残差分析",
  674. Calendar: req.Calendar,
  675. SysUserRealName: sysUser.RealName,
  676. SysUserId: sysUser.AdminId,
  677. StartDate: startDate,
  678. EndDate: indexLatestDate,
  679. LatestDate: indexLatestDate,
  680. LatestValue: indexLatestValue,
  681. MinValue: indexMin,
  682. MaxValue: indexMax,
  683. CreateTime: time.Now(),
  684. ModifyTime: time.Now(),
  685. })
  686. if err != nil {
  687. return err
  688. }
  689. // 新增指标配置关系
  690. _, err = residual_analysis_model.SaveConfigMapping(residual_analysis_model.CalculateResidualAnalysisConfigMapping{
  691. CalculateResidualAnalysisConfigId: req.ConfigId,
  692. EdbInfoId: edbInfoId,
  693. ResidualType: req.ResidualType,
  694. IndexType: req.IndexType,
  695. CreateTime: time.Now(),
  696. ModifyTime: time.Now(),
  697. })
  698. if err != nil {
  699. return err
  700. }
  701. // 保存溯源信息
  702. {
  703. fromEdbInfoList, err := data_manage.GetEdbInfoByIdList([]int{req.EdbInfoIdA, req.EdbInfoIdB})
  704. if err != nil {
  705. return fmt.Errorf("获取失败")
  706. }
  707. edbCalculateMappingInfoList := make([]*data_manage.EdbInfoCalculateMapping, 0, len(fromEdbInfoList))
  708. for _, fromEdbInfo := range fromEdbInfoList {
  709. // 创建新对象并直接使用指针
  710. edbCalculateMappingInfo := &data_manage.EdbInfoCalculateMapping{
  711. EdbInfoId: int(edbInfoId),
  712. Source: req.Source,
  713. SourceName: "残差分析",
  714. EdbCode: edbCode,
  715. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  716. FromEdbCode: fromEdbInfo.EdbCode,
  717. FromEdbName: fromEdbInfo.EdbName,
  718. FromSource: fromEdbInfo.Source,
  719. FromSourceName: fromEdbInfo.SourceName,
  720. CreateTime: time.Now(),
  721. ModifyTime: time.Now(),
  722. }
  723. edbCalculateMappingInfoList = append(edbCalculateMappingInfoList, edbCalculateMappingInfo)
  724. }
  725. err = data_manage.AddEdbInfoCalculateMappingMulti(edbCalculateMappingInfoList)
  726. if err != nil {
  727. return fmt.Errorf("保存溯源信息失败,Err:" + err.Error())
  728. }
  729. }
  730. }
  731. // 新增数据
  732. for i := range req.DataList {
  733. req.DataList[i].EdbDataId = 0
  734. req.DataList[i].EdbInfoId = int(edbInfoId)
  735. req.DataList[i].EdbCode = edbCode
  736. req.DataList[i].CreateTime = time.Now()
  737. req.DataList[i].ModifyTime = time.Now()
  738. }
  739. _, err = residual_analysis_model.AddResidualAnalysisData(req.DataList)
  740. if err != nil {
  741. return err
  742. }
  743. // 新增自变量 因变量与配置得关系 配置中不存在该指标,则新增
  744. var indexMap = make(map[int64]residual_analysis_model.CalculateResidualAnalysisConfigMapping)
  745. for _, mapping := range mappingList {
  746. indexMap[mapping.EdbInfoId] = mapping
  747. }
  748. if _, ok := indexMap[int64(req.EdbInfoIdA)]; !ok {
  749. _, err = residual_analysis_model.SaveConfigMapping(residual_analysis_model.CalculateResidualAnalysisConfigMapping{
  750. CalculateResidualAnalysisConfigId: req.ConfigId,
  751. EdbInfoId: int64(req.EdbInfoIdA),
  752. ResidualType: req.ResidualType,
  753. IndexType: 3,
  754. CreateTime: time.Now(),
  755. ModifyTime: time.Now(),
  756. })
  757. if err != nil {
  758. return err
  759. }
  760. }
  761. if _, ok := indexMap[int64(req.EdbInfoIdB)]; !ok {
  762. _, err = residual_analysis_model.SaveConfigMapping(residual_analysis_model.CalculateResidualAnalysisConfigMapping{
  763. CalculateResidualAnalysisConfigId: req.ConfigId,
  764. EdbInfoId: int64(req.EdbInfoIdB),
  765. ResidualType: req.ResidualType,
  766. IndexType: 4,
  767. CreateTime: time.Now(),
  768. ModifyTime: time.Now(),
  769. })
  770. if err != nil {
  771. return err
  772. }
  773. }
  774. //添加es
  775. data.AddOrEditEdbInfoToEs(int(edbInfoId))
  776. return nil
  777. }
  778. func ResidualAnalysisDetail(edbInfoId int) (residual_analysis_model.ResidualAnalysisDetailResp, error) {
  779. // 通过指标配置映射表 拿到配置id,再获取关联的所有指标信息
  780. var condition string
  781. var pars []interface{}
  782. condition += " and edb_info_id=?"
  783. pars = append(pars, edbInfoId)
  784. mappingList, err := residual_analysis_model.GetConfigMappingListByCondition(condition, pars)
  785. if err != nil {
  786. return residual_analysis_model.ResidualAnalysisDetailResp{}, err
  787. }
  788. if len(mappingList) <= 0 {
  789. return residual_analysis_model.ResidualAnalysisDetailResp{}, fmt.Errorf("指标不存在")
  790. }
  791. mapping := mappingList[0]
  792. configMappingList, err := residual_analysis_model.GetConfigMappingListByConfigId(mapping.CalculateResidualAnalysisConfigId)
  793. if err != nil {
  794. return residual_analysis_model.ResidualAnalysisDetailResp{}, err
  795. }
  796. var edbInfoIdList []int64
  797. var edbInfoMap = make(map[int64]residual_analysis_model.CalculateResidualAnalysisConfigMapping)
  798. var mappgingFlag = false
  799. var residualFlag = false
  800. for _, v := range configMappingList {
  801. edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
  802. edbInfoMap[v.EdbInfoId] = v
  803. if v.IndexType == 1 {
  804. mappgingFlag = true
  805. } else if v.IndexType == 2 {
  806. residualFlag = true
  807. }
  808. }
  809. condition = ""
  810. pars = []interface{}{}
  811. condition += ` and edb_info_id in(` + utils.GetOrmInReplace(len(edbInfoIdList)) + `)`
  812. for _, id := range edbInfoIdList {
  813. pars = append(pars, id)
  814. }
  815. edbInfoList, err := data_manage.GetEdbInfoListByCond(condition, pars)
  816. if err != nil {
  817. return residual_analysis_model.ResidualAnalysisDetailResp{}, err
  818. }
  819. // 获取配置
  820. configInfo, err := residual_analysis_model.GetResidualAnalysisConfigById(mapping.CalculateResidualAnalysisConfigId)
  821. if err != nil {
  822. return residual_analysis_model.ResidualAnalysisDetailResp{}, err
  823. }
  824. var edbInfoListResp []*residual_analysis_model.DetailEdbInfoList
  825. var dependentEdbInfo residual_analysis_model.DetailEdbInfoList
  826. var independentEdbInfo residual_analysis_model.DetailEdbInfoList
  827. for _, edbInfo := range edbInfoList {
  828. var indexType int
  829. if _, ok := edbInfoMap[int64(edbInfo.EdbInfoId)]; ok {
  830. indexType = edbInfoMap[int64(edbInfo.EdbInfoId)].IndexType
  831. }
  832. info := residual_analysis_model.DetailEdbInfoList{
  833. EdbInfoId: edbInfo.EdbInfoId,
  834. EdbInfoType: edbInfo.EdbInfoType,
  835. IndexType: indexType,
  836. SourceName: edbInfo.SourceName,
  837. Source: edbInfo.Source,
  838. EdbCode: edbInfo.EdbCode,
  839. EdbName: edbInfo.EdbName,
  840. EdbNameEn: edbInfo.EdbNameEn,
  841. Unit: edbInfo.Unit,
  842. UnitEn: edbInfo.UnitEn,
  843. Frequency: edbInfo.Frequency,
  844. FrequencyEn: edbInfo.FrequencyEn,
  845. ClassifyId: edbInfo.ClassifyId,
  846. }
  847. edbInfoListResp = append(edbInfoListResp, &info)
  848. if indexType == 3 {
  849. dependentEdbInfo = info
  850. } else if indexType == 4 {
  851. independentEdbInfo = info
  852. }
  853. }
  854. // 补充表格中 映射指标或者残差指标
  855. if mappgingFlag && !residualFlag {
  856. info := residual_analysis_model.DetailEdbInfoList{
  857. IndexType: 2,
  858. EdbName: independentEdbInfo.EdbName + "映射残差/" + dependentEdbInfo.EdbName,
  859. EdbNameEn: dependentEdbInfo.EdbNameEn,
  860. Unit: dependentEdbInfo.Unit,
  861. UnitEn: dependentEdbInfo.UnitEn,
  862. Frequency: dependentEdbInfo.Frequency,
  863. FrequencyEn: dependentEdbInfo.FrequencyEn,
  864. ClassifyId: dependentEdbInfo.ClassifyId,
  865. }
  866. edbInfoListResp = append(edbInfoListResp, &info)
  867. } else if !mappgingFlag && residualFlag {
  868. info := residual_analysis_model.DetailEdbInfoList{
  869. IndexType: 1,
  870. EdbName: dependentEdbInfo.EdbName + "映射" + independentEdbInfo.EdbName,
  871. EdbNameEn: dependentEdbInfo.EdbNameEn,
  872. Unit: dependentEdbInfo.Unit,
  873. UnitEn: dependentEdbInfo.UnitEn,
  874. Frequency: dependentEdbInfo.Frequency,
  875. FrequencyEn: dependentEdbInfo.FrequencyEn,
  876. ClassifyId: dependentEdbInfo.ClassifyId,
  877. }
  878. edbInfoListResp = append(edbInfoListResp, &info)
  879. }
  880. resp := residual_analysis_model.ResidualAnalysisDetailResp{
  881. ConfigInfo: &configInfo,
  882. EdbInfoList: edbInfoListResp,
  883. ResidualType: mapping.ResidualType,
  884. }
  885. return resp, nil
  886. }
  887. func SaveResidualAnalysisConfig(req residual_analysis_model.ResidualAnalysisReq, sysUser *system.Admin) (int64, error) {
  888. config := residual_analysis_model.ResidualAnalysisConfigVo{
  889. ResidualType: req.ResidualType,
  890. DateType: req.DateType,
  891. StartDate: req.StartDate,
  892. EndDate: req.EndDate,
  893. IsOrder: req.IsOrder,
  894. IndexType: req.IndexType,
  895. LeadValue: req.LeadValue,
  896. LeadFrequency: req.LeadFrequency,
  897. LeftIndexMin: req.LeftIndexMin,
  898. LeftIndexMax: req.LeftIndexMax,
  899. RightIndexMin: req.RightIndexMin,
  900. RightIndexMax: req.RightIndexMax,
  901. ResidualIndexMin: req.ResidualIndexMin,
  902. ResidualIndexMax: req.ResidualIndexMax,
  903. ContrastIndexMin: req.ContrastIndexMin,
  904. ContrastIndexMax: req.ContrastIndexMax,
  905. }
  906. // 转换为json格式
  907. configJson, err := json.Marshal(config)
  908. if err != nil {
  909. return 0, err
  910. }
  911. // 新增or更新
  912. /*
  913. var condition string
  914. var pars []interface{}
  915. if req.EdbInfoId > 0 {
  916. condition += " and edb_info_id=?"
  917. pars = append(pars, req.EdbInfoId)
  918. configMappings, err := residual_analysis_model.GetConfigMappingListByCondition(condition, pars)
  919. if err != nil {
  920. return 0, err
  921. }
  922. if len(configMappings) > 0 {
  923. mapping := configMappings[0]
  924. configInfo, err := residual_analysis_model.GetResidualAnalysisConfigById(mapping.CalculateResidualAnalysisConfigId)
  925. if err != nil {
  926. return 0, err
  927. }
  928. configInfo.Config = string(configJson)
  929. err = residual_analysis_model.UpdateResidualAnalysisConfig(configInfo)
  930. if err != nil {
  931. return 0, err
  932. }
  933. }
  934. }*/
  935. var configId int64
  936. if req.ConfigId > 0 {
  937. configInfo, err := residual_analysis_model.GetResidualAnalysisConfigById(req.ConfigId)
  938. if err != nil {
  939. return 0, err
  940. }
  941. if configInfo.CalculateResidualAnalysisConfigId == 0 {
  942. return 0, fmt.Errorf("未找到配置信息")
  943. }
  944. configId = int64(configInfo.CalculateResidualAnalysisConfigId)
  945. configInfo.Config = string(configJson)
  946. err = residual_analysis_model.UpdateResidualAnalysisConfig(configInfo)
  947. if err != nil {
  948. return 0, err
  949. }
  950. } else {
  951. analysisConfig := residual_analysis_model.CalculateResidualAnalysisConfig{
  952. Config: string(configJson),
  953. SysUserId: sysUser.AdminId,
  954. CreateTime: time.Now(),
  955. ModifyTime: time.Now(),
  956. }
  957. configId, err = residual_analysis_model.SaveResidualAnalysisConfig(analysisConfig)
  958. if err != nil {
  959. return 0, err
  960. }
  961. }
  962. return configId, nil
  963. }
  964. func CheckResidualAnalysisExist(configId int) (int64, error) {
  965. configMappingList, err := residual_analysis_model.GetConfigMappingListByConfigId(configId)
  966. if err != nil {
  967. return 0, err
  968. }
  969. var configMapping residual_analysis_model.CalculateResidualAnalysisConfigMapping
  970. for _, mapping := range configMappingList {
  971. if mapping.IndexType == 2 {
  972. configMapping = mapping
  973. }
  974. }
  975. return configMapping.EdbInfoId, nil
  976. }