residual_analysis_service.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  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/utils"
  8. "fmt"
  9. "math"
  10. "strconv"
  11. "time"
  12. )
  13. func ResidualAnalysisPreview(req residual_analysis_model.ResidualAnalysisReq) (residual_analysis_model.ResidualAnalysisResp, error) {
  14. mappingList, err := data_manage.GetChartEdbMappingListByEdbInfoIdList([]int{req.EdbInfoIdA, req.EdbInfoIdB})
  15. if err != nil {
  16. return residual_analysis_model.ResidualAnalysisResp{}, fmt.Errorf("获取图表,指标信息失败,Err:%s", err.Error())
  17. }
  18. var edbInfoMappingA, edbInfoMappingB *data_manage.ChartEdbInfoMapping
  19. for _, v := range mappingList {
  20. if v.EdbInfoId == req.EdbInfoIdA {
  21. edbInfoMappingA = v
  22. }
  23. if v.EdbInfoId == req.EdbInfoIdB {
  24. edbInfoMappingB = v
  25. }
  26. }
  27. if edbInfoMappingA == nil {
  28. return residual_analysis_model.ResidualAnalysisResp{}, fmt.Errorf("指标A不存在", nil)
  29. }
  30. if edbInfoMappingB == nil {
  31. return residual_analysis_model.ResidualAnalysisResp{}, fmt.Errorf("指标B不存在", nil)
  32. }
  33. // 时间处理
  34. var startDate, endDate string
  35. switch req.DateType {
  36. case 0:
  37. startDate = req.StartDate
  38. endDate = req.EndDate
  39. case 1:
  40. startDate = req.StartDate
  41. endDate = ""
  42. default:
  43. startDate = utils.GetPreYearTime(req.DateType)
  44. endDate = ""
  45. }
  46. resp := residual_analysis_model.ResidualAnalysisResp{}
  47. // 原始图表信息
  48. originalEdbList := make([]residual_analysis_model.ResidualAnalysisChartEdbInfoMapping, 0)
  49. originalEdbList, err = fillOriginalChart(req, mappingList, startDate, endDate, edbInfoMappingA, edbInfoMappingB, originalEdbList)
  50. if err != nil {
  51. return residual_analysis_model.ResidualAnalysisResp{}, err
  52. }
  53. originalChartInfo := createChartInfoResp(req, startDate, endDate, edbInfoMappingA.EdbName+"与"+edbInfoMappingB.EdbName)
  54. resp.OriginalChartData = residual_analysis_model.ChartResp{
  55. ChartInfo: originalChartInfo,
  56. EdbInfoList: originalEdbList,
  57. }
  58. // 如果只需要第一张图表的数据 直接返回,避免继续处理
  59. if req.QueryType == 1 {
  60. return resp, nil
  61. }
  62. dataAList, ok := edbInfoMappingA.DataList.([]*data_manage.EdbDataList)
  63. if !ok {
  64. return residual_analysis_model.ResidualAnalysisResp{}, fmt.Errorf("数据类型转换失败", nil)
  65. }
  66. indexADataMap := map[string]*data_manage.EdbDataList{}
  67. for _, indexData := range dataAList {
  68. indexADataMap[indexData.DataTime] = indexData
  69. }
  70. // 映射图表信息
  71. mappingEdbList, a, b, r, err := fillMappingChartInfo(req, edbInfoMappingA, edbInfoMappingB, originalEdbList, indexADataMap)
  72. if err != nil {
  73. return residual_analysis_model.ResidualAnalysisResp{}, err
  74. }
  75. mappingChartInfo := createChartInfoResp(req, startDate, endDate, edbInfoMappingA.EdbName+"与"+edbInfoMappingB.EdbName+"映射"+edbInfoMappingA.EdbName)
  76. resp.MappingChartData = residual_analysis_model.ChartResp{
  77. ChartInfo: mappingChartInfo,
  78. EdbInfoList: mappingEdbList,
  79. }
  80. // 残差图表信息
  81. residualEdbList, R2, err := fillResidualChartInfo(edbInfoMappingA, edbInfoMappingB, mappingEdbList)
  82. if err != nil {
  83. return residual_analysis_model.ResidualAnalysisResp{}, err
  84. }
  85. residualChartInfo := createChartInfoResp(req, startDate, endDate, edbInfoMappingA.EdbName+"与"+edbInfoMappingA.EdbName+"映射残差/"+edbInfoMappingB.EdbName)
  86. resp.ResidualChartData = residual_analysis_model.ChartResp{
  87. ChartInfo: residualChartInfo,
  88. EdbInfoList: residualEdbList,
  89. }
  90. if req.ResidualType == 2 {
  91. resp.A = a
  92. resp.B = b
  93. resp.R = r
  94. resp.R2 = R2
  95. }
  96. return resp, nil
  97. }
  98. func createChartInfoResp(req residual_analysis_model.ResidualAnalysisReq, startDate, endDate, chartName string) residual_analysis_model.ResidualAnalysisChartInfo {
  99. return residual_analysis_model.ResidualAnalysisChartInfo{
  100. Calendar: `公历`,
  101. Source: utils.CHART_SOURCE_DEFAULT,
  102. DateType: req.DateType,
  103. StartDate: startDate,
  104. EndDate: endDate,
  105. ChartType: utils.CHART_TYPE_CURVE,
  106. ChartName: chartName,
  107. }
  108. }
  109. func fillResidualChartInfo(edbInfoMappingA *data_manage.ChartEdbInfoMapping, edbInfoMappingB *data_manage.ChartEdbInfoMapping, mappingEdbList []residual_analysis_model.ResidualAnalysisChartEdbInfoMapping) ([]residual_analysis_model.ResidualAnalysisChartEdbInfoMapping, float64, error) {
  110. // 计算公式 映射残差 = 因变量指标 - 映射指标
  111. var edbInfoA, edbInfoB residual_analysis_model.ResidualAnalysisChartEdbInfoMapping
  112. if mappingEdbList[0].EdbInfoId == edbInfoMappingA.EdbInfoId {
  113. edbInfoA = mappingEdbList[0]
  114. edbInfoB = mappingEdbList[1]
  115. } else {
  116. edbInfoA = mappingEdbList[1]
  117. edbInfoB = mappingEdbList[0]
  118. }
  119. dataAList, ok := edbInfoA.DataList.([]*data_manage.EdbDataList)
  120. if !ok {
  121. return nil, 0, fmt.Errorf("数据类型转换失败", nil)
  122. }
  123. edbData := dataAList
  124. dataBList, ok := edbInfoB.DataList.([]*data_manage.EdbDataList)
  125. if !ok {
  126. return nil, 0, fmt.Errorf("数据类型转换失败", nil)
  127. }
  128. var indexDataBMap = make(map[string]*data_manage.EdbDataList)
  129. for _, data := range dataBList {
  130. indexDataBMap[data.DataTime] = data
  131. }
  132. // 求R2
  133. var valueB, sumValueA, averageValueA, residualQuadraticSum, totalQuadraticSum, R2 float64
  134. for _, indexData := range edbData {
  135. // 因变量的值总和
  136. sumValueA += indexData.Value
  137. }
  138. // 因变量平均值
  139. averageValueA = sumValueA / float64(len(edbData))
  140. for _, indexData := range edbData {
  141. if dataB, ok := indexDataBMap[indexData.DataTime]; ok {
  142. valueB = dataB.Value
  143. } else {
  144. valueB = 0
  145. }
  146. // 总因变量平方和
  147. totalQuadraticSum += math.Pow(indexData.Value-averageValueA, 2)
  148. // 补全残差值
  149. indexData.Value = indexData.Value - valueB
  150. // 残差平方和
  151. residualQuadraticSum += math.Pow(indexData.Value, 2)
  152. }
  153. // 计算R2 公式:R2=1-SSE/SST R2越大,越符合线性 R2 = 1 - 残差平方和/总平方和
  154. R2 = 1 - residualQuadraticSum/totalQuadraticSum
  155. mappingEdb := make([]residual_analysis_model.ResidualAnalysisChartEdbInfoMapping, len(mappingEdbList))
  156. copy(mappingEdb, mappingEdbList)
  157. for i, mapping := range mappingEdb {
  158. if mapping.EdbInfoId != edbInfoMappingA.EdbInfoId {
  159. mappingEdb[i].DataList = edbData
  160. mappingEdb[i].EdbName = edbInfoMappingA.EdbName + "映射残差/" + edbInfoMappingB.EdbName
  161. mappingEdb[i].IsAxis = 1
  162. } else {
  163. mappingEdb[i].IsAxis = 0
  164. }
  165. }
  166. return mappingEdb, R2, nil
  167. }
  168. 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) ([]residual_analysis_model.ResidualAnalysisChartEdbInfoMapping, float64, float64, float64, error) {
  169. // 计算公式:Y=aX+b,Y为映射后的指标,X为自变量指标
  170. // 正序:a=(L2-L1)/(R2-R1) b=L2-R2*a
  171. // 逆序:a=(L2-L1)/(R1-R2) b=L2-R1*a
  172. // L2:左轴下限 R2:右轴上限 L1:左轴上限 R1:右轴下限
  173. var a, b, r float64
  174. // 映射残差 计算a,b
  175. if req.ResidualType == 1 {
  176. if req.IsOrder {
  177. a = (req.LeftIndexMax - req.LeftIndexMin) / (req.RightIndexMin - req.RightIndexMax)
  178. b = req.LeftIndexMax - req.RightIndexMin*a
  179. } else {
  180. a = (req.LeftIndexMax - req.LeftIndexMin) / (req.RightIndexMax - req.RightIndexMin)
  181. b = req.LeftIndexMax - req.RightIndexMax*a
  182. }
  183. }
  184. dataList, ok := edbInfoMappingB.DataList.([]*data_manage.EdbDataList)
  185. if !ok {
  186. return nil, a, b, r, fmt.Errorf("数据类型转换失败", nil)
  187. }
  188. // 领先指标 dataList进行数据处理
  189. if req.IndexType == 2 {
  190. if req.LeadValue < 0 {
  191. return nil, a, b, r, fmt.Errorf("领先值不能小于0", nil)
  192. } else if req.LeadValue > 0 {
  193. for _, indexData := range dataList {
  194. switch req.LeadFrequency {
  195. case "天":
  196. indexData.DataTime = utils.GetNextDayN(indexData.DataTime, req.LeadValue)
  197. case "周":
  198. indexData.DataTime = utils.GetNextDayN(indexData.DataTime, req.LeadValue*7)
  199. case "月":
  200. indexData.DataTime = utils.TimeToString(utils.AddDate(utils.StringToTime(indexData.DataTime), 0, req.LeadValue), utils.YearMonthDay)
  201. case "季":
  202. indexData.DataTime = utils.TimeToString(utils.AddDate(utils.StringToTime(indexData.DataTime), 0, req.LeadValue*3), utils.YearMonthDay)
  203. case "年":
  204. indexData.DataTime = utils.TimeToString(utils.AddDate(utils.StringToTime(indexData.DataTime), req.LeadValue, 0), utils.YearMonthDay)
  205. }
  206. }
  207. }
  208. }
  209. // 指标B数据补充
  210. for index := 0; index < len(dataList)-1; index++ {
  211. // 获取当前数据和下一个数据
  212. beforeIndexData := dataList[index]
  213. afterIndexData := dataList[index+1]
  214. // 从最早时间开始,补充时间为自然日
  215. if utils.IsMoreThanOneDay(beforeIndexData.DataTime, afterIndexData.DataTime) {
  216. // 创建补充数据
  217. replenishIndexData := data_manage.EdbDataList{
  218. DataTime: utils.GetNextDay(beforeIndexData.DataTime),
  219. DataTimestamp: time.Now().UnixMilli(),
  220. Value: beforeIndexData.Value,
  221. }
  222. // 将补充数据插入到数据列表
  223. dataList = append(dataList, &replenishIndexData)
  224. }
  225. }
  226. // 拟合残差 计算a,b
  227. var coordinateList []utils.Coordinate
  228. if req.ResidualType == 2 {
  229. for _, indexData := range dataList {
  230. if _, ok := indexADataMap[indexData.DataTime]; ok {
  231. coordinate := utils.Coordinate{
  232. X: indexData.Value,
  233. Y: indexADataMap[indexData.DataTime].Value,
  234. }
  235. coordinateList = append(coordinateList, coordinate)
  236. }
  237. }
  238. a, b = utils.GetLinearResult(coordinateList)
  239. r = utils.CalculationDecisive(coordinateList)
  240. }
  241. // 根据指标A的时间key,在B的映射指标中筛选出对应的值
  242. var dataBList []*data_manage.EdbDataList
  243. for _, indexData := range dataList {
  244. if _, ok := indexADataMap[indexData.DataTime]; ok {
  245. indexDataCopy := *indexData
  246. // 计算指标B映射值
  247. indexDataCopy.Value = math.Round((a*indexData.Value+b)*10000) / 10000
  248. // 将副本添加到 dataBList
  249. dataBList = append(dataBList, &indexDataCopy)
  250. }
  251. }
  252. mappingEdbList := make([]residual_analysis_model.ResidualAnalysisChartEdbInfoMapping, len(originalEdbList))
  253. copy(mappingEdbList, originalEdbList)
  254. for i, mapping := range mappingEdbList {
  255. if mapping.EdbInfoId != req.EdbInfoIdA {
  256. mappingEdbList[i].EdbInfoId = 0
  257. mappingEdbList[i].EdbCode = ""
  258. mappingEdbList[i].IsAxis = 1
  259. mappingEdbList[i].EdbName = edbInfoMappingB.EdbName + "映射" + edbInfoMappingA.EdbName
  260. mappingEdbList[i].DataList = dataBList
  261. }
  262. }
  263. return mappingEdbList, a, b, r, nil
  264. }
  265. 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, error) {
  266. for _, v := range mappingList {
  267. var edbInfoMapping residual_analysis_model.ResidualAnalysisChartEdbInfoMapping
  268. edbInfoMapping.EdbInfoType = 1
  269. edbInfoMapping.IsOrder = false
  270. edbInfoMapping.IsAxis = 1
  271. edbInfoMapping.ChartColor = `#00F`
  272. edbInfoMapping.ChartWidth = 3
  273. // 获取图表中的指标数据
  274. dataList, err := data_manage.GetEdbDataList(v.Source, v.SubSource, v.EdbInfoId, startDate, endDate)
  275. if err != nil {
  276. return nil, fmt.Errorf("获取指标数据失败,Err:%s", err.Error())
  277. }
  278. if v.EdbInfoId == req.EdbInfoIdB {
  279. edbInfoMapping.LeadValue = req.LeadValue
  280. edbInfoMapping.LeadUnit = req.LeadFrequency
  281. edbInfoMapping.EdbInfoType = req.IndexType
  282. edbInfoMapping.IsOrder = req.IsOrder
  283. edbInfoMapping.IsAxis = 0
  284. edbInfoMapping.ChartColor = `#F00`
  285. edbInfoMapping.ChartWidth = 1
  286. edbInfoMappingB.DataList = dataList
  287. } else {
  288. edbInfoMappingA.DataList = dataList
  289. }
  290. edbInfoMapping.EdbInfoId = v.EdbInfoId
  291. edbInfoMapping.EdbName = v.EdbName
  292. edbInfoMapping.EdbCode = v.EdbCode
  293. edbInfoMapping.Unit = v.Unit
  294. edbInfoMapping.Frequency = v.Frequency
  295. edbInfoMapping.Source = v.Source
  296. edbInfoMapping.SourceName = v.SourceName
  297. edbInfoMapping.MinValue = v.MinValue
  298. edbInfoMapping.MaxValue = v.MaxValue
  299. edbInfoMapping.LatestDate = v.LatestDate
  300. edbInfoMapping.LatestValue = v.LatestValue
  301. edbInfoMapping.DataList = dataList
  302. originalEdbList = append(originalEdbList, edbInfoMapping)
  303. }
  304. return originalEdbList, nil
  305. }
  306. func ContrastPreview(indexCode string) (residual_analysis_model.ResidualAnalysisChartEdbInfoMapping, error) {
  307. var condition string
  308. var pars []interface{}
  309. if indexCode != "" {
  310. condition += " and edb_code=?"
  311. pars = append(pars, indexCode)
  312. }
  313. edbInfo, err := data_manage.GetEdbInfoByCondition(condition, pars)
  314. if err != nil {
  315. return residual_analysis_model.ResidualAnalysisChartEdbInfoMapping{}, err
  316. }
  317. if edbInfo == nil {
  318. return residual_analysis_model.ResidualAnalysisChartEdbInfoMapping{}, fmt.Errorf("指标不存在")
  319. }
  320. dataList, err := data_manage.GetEdbDataList(edbInfo.Source, edbInfo.SubSource, edbInfo.EdbInfoId, "", "")
  321. var resp residual_analysis_model.ResidualAnalysisChartEdbInfoMapping
  322. resp.EdbInfoId = edbInfo.EdbInfoId
  323. resp.EdbCode = edbInfo.EdbCode
  324. resp.SourceName = edbInfo.SourceName
  325. resp.EdbName = edbInfo.EdbName
  326. resp.Unit = edbInfo.Unit
  327. resp.Frequency = edbInfo.Frequency
  328. resp.MinValue = edbInfo.MinValue
  329. resp.MaxValue = edbInfo.MaxValue
  330. resp.DataList = dataList
  331. return resp, nil
  332. }
  333. func SaveResidualAnalysis(req residual_analysis_model.ResidualAnalysisIndexSaveReq, sysUser *system.Admin) error {
  334. // 验证分类是否存在
  335. classifyCount, err := data_manage.GetEdbClassifyCountById(req.ClassifyId)
  336. if err != nil {
  337. return err
  338. }
  339. if classifyCount <= 0 {
  340. return fmt.Errorf("分类不存在", nil)
  341. }
  342. // 校验名称是否重复
  343. var condition string
  344. var pars []interface{}
  345. condition += " and source = ? AND edb_name=?"
  346. pars = append(pars, req.Source, req.EdbName)
  347. edbInfoByCondition, err := data_manage.GetEdbInfoByCondition(condition, pars)
  348. if err != nil && err.Error() != utils.ErrNoRow() {
  349. return err
  350. }
  351. // 获取指标数据最大值 最小值 最后更新时间 最后更新时间对应的值
  352. var indexMax, indexMin, indexLatestValue float64
  353. var indexLatestDate string
  354. latestTime, _ := time.Parse(utils.YearMonthDay, req.DataList[0].DataTime)
  355. for _, data := range req.DataList {
  356. // 比较最大值
  357. if data.Value > indexMax {
  358. indexMax = data.Value
  359. }
  360. // 比较最小值
  361. if data.Value < indexMin {
  362. indexMin = data.Value
  363. }
  364. // 比较最新时间和对应值
  365. currentTime, err := time.Parse(utils.YearMonthDay, data.DataTime)
  366. if err != nil {
  367. // 时间解析失败,跳过此项
  368. continue
  369. }
  370. // 如果当前时间更晚
  371. if currentTime.After(latestTime) {
  372. latestTime = currentTime
  373. indexLatestDate = data.DataTime
  374. indexLatestValue = data.Value
  375. }
  376. }
  377. // 更新保存指标和配置的映射关系
  378. mappingList, err := residual_analysis_model.GetConfigMappingListByConfigId(req.ConfigId)
  379. if err != nil {
  380. return err
  381. }
  382. // 判断是更新还是修改 看指标配置映射中,是否存在对应指标 存在 则更新 不存在 则新增
  383. var edbInfoMapping residual_analysis_model.CalculateResidualAnalysisConfigMapping
  384. for _, mapping := range mappingList {
  385. if req.IndexType == mapping.IndexType && req.IndexType != 0 {
  386. edbInfoMapping = mapping
  387. }
  388. }
  389. var edbInfoId int64
  390. var edbCode string
  391. // 更新or新增
  392. if edbInfoMapping.EdbInfoId > 0 {
  393. // 查询指标库指标
  394. edbInfo, err := data_manage.GetEdbInfoById(int(edbInfoMapping.EdbInfoId))
  395. if err != nil {
  396. return err
  397. }
  398. if edbInfo == nil {
  399. return fmt.Errorf("指标不存在", nil)
  400. }
  401. edbInfoId = int64(edbInfo.EdbInfoId)
  402. edbCode = edbInfo.EdbCode
  403. if edbInfoByCondition.EdbInfoId != edbInfo.EdbInfoId {
  404. return fmt.Errorf("指标名称重复", nil)
  405. }
  406. // 须补充更新指标最大值,最小值,数据最新时间,数据最新值
  407. edbInfo.MaxValue = indexMax
  408. edbInfo.MinValue = indexMin
  409. edbInfo.LatestDate = indexLatestDate
  410. edbInfo.LatestValue = indexLatestValue
  411. err = edbInfo.Update([]string{"min_value", "max_value", "latest_date", "latest_value"})
  412. if err != nil {
  413. return err
  414. }
  415. // 删除对应得指标数据
  416. err = residual_analysis_model.DeleteResidualAnalysisDataByEdbCode(req.EdbCode)
  417. if err != nil {
  418. return fmt.Errorf("删除指标数据失败", nil)
  419. }
  420. } else {
  421. if edbInfoByCondition != nil {
  422. return fmt.Errorf("指标名称重复", nil)
  423. }
  424. // 新增指标
  425. edbCode, err = utils.GenerateEdbCode(1, "")
  426. if err != nil {
  427. return err
  428. }
  429. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  430. edbInfoId, err = data_manage.AddEdbInfo(&data_manage.EdbInfo{
  431. EdbCode: edbCode,
  432. UniqueCode: utils.MD5(utils.CHART_PREFIX + "_" + timestamp),
  433. EdbName: req.EdbName,
  434. EdbNameEn: req.EdbNameEn,
  435. ClassifyId: req.ClassifyId,
  436. EdbType: req.EdbType,
  437. Unit: req.Unit,
  438. UnitEn: req.UnitEn,
  439. Frequency: req.Frequency,
  440. Source: req.Source,
  441. SourceName: "残差分析",
  442. Calendar: req.Calendar,
  443. SysUserRealName: sysUser.RealName,
  444. SysUserId: sysUser.AdminId,
  445. LatestDate: indexLatestDate,
  446. LatestValue: indexLatestValue,
  447. CreateTime: time.Now(),
  448. ModifyTime: time.Now(),
  449. })
  450. if err != nil {
  451. return err
  452. }
  453. }
  454. // 新增数据
  455. for i := range req.DataList {
  456. req.DataList[i].EdbDataId = 0
  457. req.DataList[i].EdbInfoId = int(edbInfoId)
  458. req.DataList[i].EdbCode = edbCode
  459. }
  460. _, err = residual_analysis_model.AddResidualAnalysisData(req.DataList)
  461. if err != nil {
  462. return err
  463. }
  464. var indexMap map[int64]residual_analysis_model.CalculateResidualAnalysisConfigMapping
  465. for _, mapping := range mappingList {
  466. indexMap[mapping.EdbInfoId] = mapping
  467. }
  468. if _, ok := indexMap[edbInfoId]; !ok {
  469. _, err = residual_analysis_model.SaveConfigMapping(residual_analysis_model.CalculateResidualAnalysisConfigMapping{
  470. CalculateResidualAnalysisConfigId: req.ConfigId,
  471. EdbInfoId: edbInfoId,
  472. ResidualType: req.ResidualType,
  473. IndexType: req.IndexType,
  474. CreateTime: time.Now(),
  475. ModifyTime: time.Now(),
  476. })
  477. if err != nil {
  478. return err
  479. }
  480. }
  481. if _, ok := indexMap[int64(req.EdbInfoIdA)]; !ok {
  482. _, err = residual_analysis_model.SaveConfigMapping(residual_analysis_model.CalculateResidualAnalysisConfigMapping{
  483. CalculateResidualAnalysisConfigId: req.ConfigId,
  484. EdbInfoId: int64(req.EdbInfoIdA),
  485. ResidualType: req.ResidualType,
  486. IndexType: 3,
  487. CreateTime: time.Now(),
  488. ModifyTime: time.Now(),
  489. })
  490. if err != nil {
  491. return err
  492. }
  493. }
  494. if _, ok := indexMap[int64(req.EdbInfoIdB)]; !ok {
  495. _, err = residual_analysis_model.SaveConfigMapping(residual_analysis_model.CalculateResidualAnalysisConfigMapping{
  496. CalculateResidualAnalysisConfigId: req.ConfigId,
  497. EdbInfoId: int64(req.EdbInfoIdB),
  498. ResidualType: req.ResidualType,
  499. IndexType: 4,
  500. CreateTime: time.Now(),
  501. ModifyTime: time.Now(),
  502. })
  503. if err != nil {
  504. return err
  505. }
  506. }
  507. return nil
  508. }
  509. func ResidualAnalysisDetail(edbInfoId int) (residual_analysis_model.ResidualAnalysisDetailResp, error) {
  510. // 通过指标配置映射表 拿到配置id,再获取关联的所有指标信息
  511. var condition string
  512. var pars []interface{}
  513. condition += " and edb_info_id=?"
  514. pars = append(pars, edbInfoId)
  515. mappingList, err := residual_analysis_model.GetConfigMappingListByCondition(condition, pars)
  516. if err != nil {
  517. return residual_analysis_model.ResidualAnalysisDetailResp{}, err
  518. }
  519. if len(mappingList) <= 0 {
  520. return residual_analysis_model.ResidualAnalysisDetailResp{}, fmt.Errorf("指标不存在", nil)
  521. }
  522. mapping := mappingList[0]
  523. configMappingList, err := residual_analysis_model.GetConfigMappingListByConfigId(mapping.CalculateResidualAnalysisConfigId)
  524. if err != nil {
  525. return residual_analysis_model.ResidualAnalysisDetailResp{}, err
  526. }
  527. var edbInfoIdList []int64
  528. var edbInfoMap = make(map[int64]residual_analysis_model.CalculateResidualAnalysisConfigMapping)
  529. var mappgingFlag = false
  530. var residualFlag = false
  531. for _, v := range configMappingList {
  532. edbInfoIdList = append(edbInfoIdList, v.EdbInfoId)
  533. edbInfoMap[v.EdbInfoId] = v
  534. if v.IndexType == 1 {
  535. mappgingFlag = true
  536. } else if v.IndexType == 2 {
  537. residualFlag = true
  538. }
  539. }
  540. condition = ""
  541. pars = []interface{}{}
  542. condition += ` and edb_info_id in(` + utils.GetOrmInReplace(len(edbInfoIdList)) + `)`
  543. for _, id := range edbInfoIdList {
  544. pars = append(pars, id)
  545. }
  546. edbInfoList, err := data_manage.GetEdbInfoListByCond(condition, pars)
  547. if err != nil {
  548. return residual_analysis_model.ResidualAnalysisDetailResp{}, err
  549. }
  550. // 获取配置
  551. configInfo, err := residual_analysis_model.GetResidualAnalysisConfigById(mapping.CalculateResidualAnalysisConfigId)
  552. if err != nil {
  553. return residual_analysis_model.ResidualAnalysisDetailResp{}, err
  554. }
  555. var edbInfoListResp []*residual_analysis_model.DetailEdbInfoList
  556. var dependentEdbInfo residual_analysis_model.DetailEdbInfoList
  557. var independentEdbInfo residual_analysis_model.DetailEdbInfoList
  558. for _, edbInfo := range edbInfoList {
  559. var indexType int
  560. if _, ok := edbInfoMap[int64(edbInfo.EdbInfoId)]; ok {
  561. indexType = edbInfoMap[int64(edbInfo.EdbInfoId)].IndexType
  562. }
  563. info := residual_analysis_model.DetailEdbInfoList{
  564. EdbInfoId: edbInfo.EdbInfoId,
  565. EdbInfoType: edbInfo.EdbInfoType,
  566. IndexType: indexType,
  567. SourceName: edbInfo.SourceName,
  568. Source: edbInfo.Source,
  569. EdbCode: edbInfo.EdbCode,
  570. EdbName: edbInfo.EdbName,
  571. EdbNameEn: edbInfo.EdbNameEn,
  572. Unit: edbInfo.Unit,
  573. UnitEn: edbInfo.UnitEn,
  574. Frequency: edbInfo.Frequency,
  575. FrequencyEn: edbInfo.FrequencyEn,
  576. ClassifyId: edbInfo.ClassifyId,
  577. }
  578. edbInfoListResp = append(edbInfoListResp, &info)
  579. if indexType == 3 {
  580. dependentEdbInfo = info
  581. } else if indexType == 4 {
  582. independentEdbInfo = info
  583. }
  584. }
  585. // 补充表格中 映射指标或者残差指标
  586. if mappgingFlag && !residualFlag {
  587. info := residual_analysis_model.DetailEdbInfoList{
  588. IndexType: 2,
  589. EdbName: independentEdbInfo.EdbName + "映射残差/" + dependentEdbInfo.EdbName,
  590. EdbNameEn: dependentEdbInfo.EdbNameEn,
  591. Unit: dependentEdbInfo.Unit,
  592. UnitEn: dependentEdbInfo.UnitEn,
  593. Frequency: dependentEdbInfo.Frequency,
  594. FrequencyEn: dependentEdbInfo.FrequencyEn,
  595. ClassifyId: dependentEdbInfo.ClassifyId,
  596. }
  597. edbInfoListResp = append(edbInfoListResp, &info)
  598. } else if !mappgingFlag && residualFlag {
  599. info := residual_analysis_model.DetailEdbInfoList{
  600. IndexType: 1,
  601. EdbName: dependentEdbInfo.EdbName + "映射" + independentEdbInfo.EdbName,
  602. EdbNameEn: dependentEdbInfo.EdbNameEn,
  603. Unit: dependentEdbInfo.Unit,
  604. UnitEn: dependentEdbInfo.UnitEn,
  605. Frequency: dependentEdbInfo.Frequency,
  606. FrequencyEn: dependentEdbInfo.FrequencyEn,
  607. ClassifyId: dependentEdbInfo.ClassifyId,
  608. }
  609. edbInfoListResp = append(edbInfoListResp, &info)
  610. }
  611. resp := residual_analysis_model.ResidualAnalysisDetailResp{
  612. ConfigInfo: &configInfo,
  613. EdbInfoList: edbInfoListResp,
  614. }
  615. return resp, nil
  616. }
  617. func SaveResidualAnalysisConfig(req residual_analysis_model.ResidualAnalysisReq, sysUser *system.Admin) (int64, error) {
  618. config := residual_analysis_model.ResidualAnalysisConfigVo{
  619. DateType: req.DateType,
  620. StartDate: req.StartDate,
  621. EndDate: req.EndDate,
  622. IsOrder: req.IsOrder,
  623. IndexType: req.IndexType,
  624. LeadValue: req.LeadValue,
  625. LeadFrequency: req.LeadFrequency,
  626. LeftIndexMin: req.LeftIndexMin,
  627. LeftIndexMax: req.LeftIndexMax,
  628. RightIndexMin: req.RightIndexMin,
  629. RightIndexMax: req.RightIndexMax,
  630. ResidualIndexMin: req.ResidualIndexMin,
  631. ResidualIndexMax: req.ResidualIndexMax,
  632. ContrastIndexMin: req.ContrastIndexMin,
  633. ContrastIndexMax: req.ContrastIndexMax,
  634. }
  635. // 转换为json格式
  636. configJson, err := json.Marshal(config)
  637. if err != nil {
  638. return 0, err
  639. }
  640. var condition string
  641. var pars []interface{}
  642. // 新增or更新
  643. if req.EdbInfoId > 0 {
  644. condition += " and edb_info_id=?"
  645. pars = append(pars, req.EdbInfoId)
  646. configMappings, err := residual_analysis_model.GetConfigMappingListByCondition(condition, pars)
  647. if err != nil {
  648. return 0, err
  649. }
  650. if len(configMappings) > 0 {
  651. mapping := configMappings[0]
  652. configInfo, err := residual_analysis_model.GetResidualAnalysisConfigById(mapping.CalculateResidualAnalysisConfigId)
  653. if err != nil {
  654. return 0, err
  655. }
  656. configInfo.Config = string(configJson)
  657. err = residual_analysis_model.UpdateResidualAnalysisConfig(configInfo)
  658. if err != nil {
  659. return 0, err
  660. }
  661. }
  662. }
  663. analysisConfig := residual_analysis_model.CalculateResidualAnalysisConfig{
  664. Config: string(configJson),
  665. SysUserId: sysUser.AdminId,
  666. CreateTime: time.Now(),
  667. ModifyTime: time.Now(),
  668. }
  669. configId, err := residual_analysis_model.SaveResidualAnalysisConfig(analysisConfig)
  670. if err != nil {
  671. return 0, err
  672. }
  673. return configId, nil
  674. }
  675. func CheckResidualAnalysisExist(configId int) (int64, error) {
  676. configMappingList, err := residual_analysis_model.GetConfigMappingListByConfigId(configId)
  677. if err != nil {
  678. return 0, err
  679. }
  680. var configMapping residual_analysis_model.CalculateResidualAnalysisConfigMapping
  681. for _, mapping := range configMappingList {
  682. if mapping.IndexType == 2 {
  683. configMapping = mapping
  684. }
  685. }
  686. return configMapping.EdbInfoId, nil
  687. }