predict_edb_data_calculate_zjpj.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. package models
  2. import (
  3. "errors"
  4. "eta/eta_index_lib/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. "strings"
  8. "time"
  9. )
  10. // EdbDataPredictCalculateZjpj 直接拼接数据结构体
  11. type EdbDataPredictCalculateZjpj struct {
  12. EdbDataId int `orm:"column(edb_data_id);pk"`
  13. EdbInfoId int
  14. EdbCode string
  15. DataTime string
  16. Value float64
  17. Status int
  18. CreateTime time.Time
  19. ModifyTime time.Time
  20. DataTimestamp int64
  21. }
  22. // SavePredictCalculateZjpj 新增直接拼接数据
  23. func SavePredictCalculateZjpj(req *EdbInfoCalculateBatchSaveReq, firstEdbInfo, secondEdbInfo *EdbInfo, edbCode, uniqueCode string, sysUserId int, sysUserRealName string) (edbInfo *EdbInfo, latestDateStr string, latestValue float64, err error) {
  24. o := orm.NewOrm()
  25. to, err := o.Begin()
  26. if err != nil {
  27. return
  28. }
  29. defer func() {
  30. if err != nil {
  31. fmt.Println("SavePredictCalculateZjpj,Err:" + err.Error())
  32. _ = to.Rollback()
  33. } else {
  34. _ = to.Commit()
  35. }
  36. }()
  37. var existItemA, existItemB *EdbInfoCalculateMapping
  38. if req.EdbInfoId <= 0 {
  39. edbInfo = &EdbInfo{
  40. EdbInfoType: 1,
  41. SourceName: "预测直接拼接",
  42. Source: utils.DATA_SOURCE_PREDICT_CALCULATE_ZJPJ,
  43. EdbCode: edbCode,
  44. EdbName: req.EdbName,
  45. EdbNameSource: req.EdbName,
  46. Frequency: req.Frequency,
  47. Unit: req.Unit,
  48. StartDate: firstEdbInfo.StartDate,
  49. EndDate: firstEdbInfo.EndDate,
  50. ClassifyId: req.ClassifyId,
  51. SysUserId: sysUserId,
  52. SysUserRealName: sysUserRealName,
  53. UniqueCode: uniqueCode,
  54. CreateTime: time.Now(),
  55. ModifyTime: time.Now(),
  56. CalculateFormula: req.Formula,
  57. EdbType: 2,
  58. }
  59. newEdbInfoId, tmpErr := to.Insert(edbInfo)
  60. if tmpErr != nil {
  61. err = tmpErr
  62. return
  63. }
  64. edbInfo.EdbInfoId = int(newEdbInfoId)
  65. //关联关系
  66. //第一个指标
  67. {
  68. existItemA = &EdbInfoCalculateMapping{
  69. EdbInfoCalculateMappingId: 0,
  70. EdbInfoId: edbInfo.EdbInfoId,
  71. Source: edbInfo.Source,
  72. SourceName: edbInfo.SourceName,
  73. EdbCode: edbInfo.EdbCode,
  74. FromEdbInfoId: firstEdbInfo.EdbInfoId,
  75. FromEdbCode: firstEdbInfo.EdbCode,
  76. FromEdbName: firstEdbInfo.EdbName,
  77. FromSource: firstEdbInfo.Source,
  78. FromSourceName: firstEdbInfo.SourceName,
  79. FromTag: "A",
  80. Sort: 1,
  81. CreateTime: time.Now(),
  82. ModifyTime: time.Now(),
  83. }
  84. insertId, tmpErr := to.Insert(existItemA)
  85. if tmpErr != nil {
  86. err = tmpErr
  87. return
  88. }
  89. existItemA.EdbInfoCalculateMappingId = int(insertId)
  90. }
  91. //第二个指标
  92. {
  93. existItemB = &EdbInfoCalculateMapping{
  94. EdbInfoCalculateMappingId: 0,
  95. EdbInfoId: edbInfo.EdbInfoId,
  96. Source: edbInfo.Source,
  97. SourceName: edbInfo.SourceName,
  98. EdbCode: edbInfo.EdbCode,
  99. FromEdbInfoId: secondEdbInfo.EdbInfoId,
  100. FromEdbCode: secondEdbInfo.EdbCode,
  101. FromEdbName: secondEdbInfo.EdbName,
  102. FromSource: secondEdbInfo.Source,
  103. FromSourceName: secondEdbInfo.SourceName,
  104. FromTag: "B",
  105. Sort: 1,
  106. CreateTime: time.Now(),
  107. ModifyTime: time.Now(),
  108. }
  109. insertId, tmpErr := to.Insert(existItemB)
  110. if tmpErr != nil {
  111. err = tmpErr
  112. return
  113. }
  114. existItemB.EdbInfoCalculateMappingId = int(insertId)
  115. }
  116. } else {
  117. edbInfo, err = GetEdbInfoById(req.EdbInfoId)
  118. if err != nil {
  119. return
  120. }
  121. latestDateStr = edbInfo.LatestDate
  122. latestValue = edbInfo.LatestValue
  123. nowEdbInfo := *edbInfo // 现在的指标信息
  124. //修改指标信息
  125. edbInfo.EdbName = req.EdbName
  126. edbInfo.EdbNameSource = req.EdbName
  127. edbInfo.Frequency = req.Frequency
  128. edbInfo.Unit = req.Unit
  129. edbInfo.ClassifyId = req.ClassifyId
  130. edbInfo.CalculateFormula = req.Formula
  131. edbInfo.ModifyTime = time.Now()
  132. _, err = to.Update(edbInfo, "EdbName", "EdbNameSource", "Frequency", "Unit", "ClassifyId", "CalculateFormula", "ModifyTime")
  133. if err != nil {
  134. return
  135. }
  136. var existCondition string
  137. var existPars []interface{}
  138. existCondition += " AND edb_info_id=? "
  139. existPars = append(existPars, edbInfo.EdbInfoId)
  140. //查询出所有的关联指标
  141. var existList []*EdbInfoCalculateMapping
  142. existList, err = GetEdbInfoCalculateListByCondition(existCondition, existPars)
  143. if err != nil {
  144. err = fmt.Errorf("判断指标是否改变失败,Err:" + err.Error())
  145. return
  146. }
  147. for _, existItem := range existList {
  148. if existItem.FromTag == "A" {
  149. existItemA = existItem
  150. } else if existItem.FromTag == "B" {
  151. existItemB = existItem
  152. }
  153. }
  154. if existItemA == nil {
  155. err = errors.New("原拼接日期之前的指标不存在")
  156. return
  157. }
  158. if existItemB == nil {
  159. err = errors.New("原拼接日期之后的指标不存在")
  160. return
  161. }
  162. // 是否需要删除数据重新计算
  163. isNeedCalculateData := false
  164. // 如果截止日期变更,那么需要重新计算
  165. if req.Formula != nowEdbInfo.CalculateFormula {
  166. isNeedCalculateData = true
  167. }
  168. //第一个指标数据
  169. {
  170. // 如果指标变了,那么需要删除关系,并重新计算
  171. if existItemA.FromEdbInfoId != firstEdbInfo.EdbInfoId {
  172. //删除之前的A指标关联关系
  173. sql := ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? and from_edb_info_id = ?`
  174. _, err = to.Raw(sql, edbInfo.EdbInfoId, existItemA.FromEdbInfoId).Exec()
  175. if err != nil {
  176. err = fmt.Errorf("删除拼接日期之前的指标关联关系失败,Err:" + err.Error())
  177. return
  178. }
  179. }
  180. }
  181. //第二个指标数据
  182. {
  183. // 如果指标变了,那么需要删除关系,并重新计算
  184. if existItemB.FromEdbInfoId != secondEdbInfo.EdbInfoId {
  185. //删除之前的B指标关联关系
  186. sql := ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? and from_edb_info_id = ?`
  187. _, err = to.Raw(sql, edbInfo.EdbInfoId, existItemB.FromEdbInfoId).Exec()
  188. if err != nil {
  189. err = fmt.Errorf("删除拼接日期之后的指标关联关系失败,Err:" + err.Error())
  190. return
  191. }
  192. }
  193. }
  194. if existItemA.FromEdbInfoId != firstEdbInfo.EdbInfoId {
  195. //添加新的指标关系
  196. {
  197. existItemA = &EdbInfoCalculateMapping{
  198. EdbInfoCalculateMappingId: 0,
  199. EdbInfoId: edbInfo.EdbInfoId,
  200. Source: edbInfo.Source,
  201. SourceName: edbInfo.SourceName,
  202. EdbCode: edbInfo.EdbCode,
  203. FromEdbInfoId: firstEdbInfo.EdbInfoId,
  204. FromEdbCode: firstEdbInfo.EdbCode,
  205. FromEdbName: firstEdbInfo.EdbName,
  206. FromSource: firstEdbInfo.Source,
  207. FromSourceName: firstEdbInfo.SourceName,
  208. FromTag: "A",
  209. Sort: 1,
  210. CreateTime: time.Now(),
  211. ModifyTime: time.Now(),
  212. }
  213. insertId, tmpErr := to.Insert(existItemA)
  214. if tmpErr != nil {
  215. err = tmpErr
  216. return
  217. }
  218. existItemA.EdbInfoCalculateMappingId = int(insertId)
  219. isNeedCalculateData = true
  220. }
  221. }
  222. if existItemB.FromEdbInfoId != secondEdbInfo.EdbInfoId {
  223. // 添加新的指标关联关系
  224. existItemB = &EdbInfoCalculateMapping{
  225. EdbInfoCalculateMappingId: 0,
  226. EdbInfoId: edbInfo.EdbInfoId,
  227. Source: edbInfo.Source,
  228. SourceName: edbInfo.SourceName,
  229. EdbCode: edbInfo.EdbCode,
  230. FromEdbInfoId: secondEdbInfo.EdbInfoId,
  231. FromEdbCode: secondEdbInfo.EdbCode,
  232. FromEdbName: secondEdbInfo.EdbName,
  233. FromSource: secondEdbInfo.Source,
  234. FromSourceName: secondEdbInfo.SourceName,
  235. FromTag: "B",
  236. Sort: 2,
  237. CreateTime: time.Now(),
  238. ModifyTime: time.Now(),
  239. }
  240. insertId, tmpErr := to.Insert(existItemB)
  241. if tmpErr != nil {
  242. err = tmpErr
  243. return
  244. }
  245. existItemB.EdbInfoCalculateMappingId = int(insertId)
  246. isNeedCalculateData = true
  247. }
  248. // 如果需要重新计算,那么先删除所有的指标数据,然后再重新计算
  249. if isNeedCalculateData {
  250. // 删除之前所有的指标数据
  251. tableName := GetEdbDataTableName(edbInfo.Source)
  252. sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? `, tableName)
  253. _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
  254. if err != nil {
  255. err = fmt.Errorf("删除历史数据失败,Err:" + err.Error())
  256. return
  257. }
  258. } else {
  259. return
  260. }
  261. }
  262. //拼接数据
  263. latestDateStr, latestValue, err = refreshAllPredictCalculateZjpj(to, edbInfo, firstEdbInfo, secondEdbInfo)
  264. return
  265. }
  266. // RefreshAllPredictCalculateZjpj 刷新所有 直接拼接 数据
  267. func RefreshAllPredictCalculateZjpj(edbInfo *EdbInfo) (latestDateStr string, latestValue float64, err error) {
  268. o := orm.NewOrm()
  269. to, err := o.Begin()
  270. defer func() {
  271. if err != nil {
  272. fmt.Println("refreshAllPredictCalculateZjpj,Err:" + err.Error())
  273. _ = to.Rollback()
  274. } else {
  275. _ = to.Commit()
  276. }
  277. }()
  278. //查询关联指标信息
  279. var existCondition string
  280. var existPars []interface{}
  281. existCondition += " AND edb_info_id=? "
  282. existPars = append(existPars, edbInfo.EdbInfoId)
  283. existList, err := GetEdbInfoCalculateListByCondition(existCondition, existPars)
  284. if err != nil {
  285. err = fmt.Errorf("判断指标是否改变失败,Err:" + err.Error())
  286. return
  287. }
  288. var existItemA, existItemB *EdbInfoCalculateMapping
  289. for _, existItem := range existList {
  290. if existItem.FromTag == "A" {
  291. existItemA = existItem
  292. } else if existItem.FromTag == "B" {
  293. existItemB = existItem
  294. }
  295. }
  296. if existItemA == nil {
  297. err = errors.New("原拼接日期之前的指标不存在")
  298. return
  299. }
  300. if existItemB == nil {
  301. err = errors.New("原拼接日期之后的指标不存在")
  302. return
  303. }
  304. fromEdbInfo, err := GetEdbInfoById(existItemA.FromEdbInfoId)
  305. if err != nil {
  306. err = fmt.Errorf("GetEdbInfoById Err:" + err.Error())
  307. return
  308. }
  309. secondEdbInfo, err := GetEdbInfoById(existItemB.FromEdbInfoId)
  310. if err != nil {
  311. err = fmt.Errorf("GetEdbInfoById Err:" + err.Error())
  312. return
  313. }
  314. // 刷新数据
  315. latestDateStr, latestValue, err = refreshAllPredictCalculateZjpj(to, edbInfo, fromEdbInfo, secondEdbInfo)
  316. return
  317. }
  318. // refreshAllPredictCalculateZjpj 刷新所有 直接拼接 数据
  319. func refreshAllPredictCalculateZjpj(to orm.TxOrmer, edbInfo, firstEdbInfo, secondEdbInfo *EdbInfo) (latestDateStr string, latestValue float64, err error) {
  320. //查询当前指标现有的数据
  321. var dataList []*EdbDataPredictCalculateZjpj
  322. sql := ` SELECT * FROM edb_data_predict_calculate_zjpj WHERE edb_info_id=? ORDER BY data_time DESC `
  323. _, err = to.Raw(sql, edbInfo.EdbInfoId).QueryRows(&dataList)
  324. if err != nil {
  325. return
  326. }
  327. if edbInfo.CalculateFormula <= secondEdbInfo.LatestDate {
  328. latestDateStr = secondEdbInfo.LatestDate
  329. } else {
  330. if edbInfo.CalculateFormula >= firstEdbInfo.LatestDate {
  331. latestDateStr = firstEdbInfo.LatestDate
  332. } else {
  333. latestDateStr = edbInfo.CalculateFormula
  334. }
  335. }
  336. var dateArr []string
  337. dataMap := make(map[string]*EdbDataPredictCalculateZjpj)
  338. removeDataTimeMap := make(map[string]int) //需要移除的日期数据
  339. for _, v := range dataList {
  340. dateArr = append(dateArr, v.DataTime)
  341. dataMap[v.DataTime] = v
  342. removeDataTimeMap[v.DataTime] = 1
  343. }
  344. addDataList := make([]*EdbDataPredictCalculateZjpj, 0)
  345. //第一个指标
  346. {
  347. var firstDataList []*EdbInfoSearchData
  348. firstDataList, err = GetPredictEdbDataListAllByStartDate(firstEdbInfo, 0, "")
  349. if err != nil {
  350. return
  351. }
  352. for _, v := range firstDataList {
  353. if v.DataTime >= edbInfo.CalculateFormula {
  354. continue
  355. }
  356. //校验待删除日期数据里面是否存在该元素,如果存在的话,那么移除该元素
  357. if _, ok := removeDataTimeMap[v.DataTime]; ok {
  358. delete(removeDataTimeMap, v.DataTime)
  359. }
  360. if latestDateStr == v.DataTime {
  361. latestValue = v.Value
  362. }
  363. //时间戳
  364. if edbData, ok := dataMap[v.DataTime]; ok {
  365. if edbData.Value != v.Value {
  366. //更新指标数据
  367. edbData.Value = v.Value
  368. _, _ = to.Update(edbData, "Value")
  369. }
  370. } else {
  371. //时间戳
  372. currentDate, _ := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  373. timestamp := currentDate.UnixNano() / 1e6
  374. edbDataZjpj := &EdbDataPredictCalculateZjpj{
  375. EdbInfoId: edbInfo.EdbInfoId,
  376. EdbCode: edbInfo.EdbCode,
  377. DataTime: v.DataTime,
  378. Value: v.Value,
  379. Status: 1,
  380. CreateTime: time.Now(),
  381. ModifyTime: time.Now(),
  382. DataTimestamp: timestamp,
  383. }
  384. addDataList = append(addDataList, edbDataZjpj)
  385. }
  386. }
  387. }
  388. //第二个指标
  389. {
  390. /*condition := ``
  391. pars := make([]interface{}, 0)
  392. condition += " AND data_time >= ? AND edb_info_id = ? "
  393. pars = append(pars, edbInfo.CalculateFormula, existItemB.FromEdbInfoId)
  394. //第二个指标的数据列表
  395. secondDataList, tmpErr := GetEdbDataListAllByTo(to, condition, pars, existItemB.FromSource, 0)
  396. if tmpErr != nil {
  397. return tmpErr
  398. }*/
  399. var secondDataList []*EdbInfoSearchData
  400. secondDataList, err = GetPredictEdbDataListAllByStartDate(secondEdbInfo, 0, edbInfo.CalculateFormula)
  401. if err != nil {
  402. return
  403. }
  404. for _, v := range secondDataList {
  405. //校验待删除日期数据里面是否存在该元素,如果存在的话,那么移除该元素
  406. if _, ok := removeDataTimeMap[v.DataTime]; ok {
  407. delete(removeDataTimeMap, v.DataTime)
  408. }
  409. if latestDateStr == v.DataTime {
  410. latestValue = v.Value
  411. }
  412. if edbData, ok := dataMap[v.DataTime]; ok {
  413. if edbData.Value != v.Value {
  414. //更新指标数据
  415. edbData.Value = v.Value
  416. edbData.ModifyTime = time.Now()
  417. _, tmpErr := to.Update(edbData, "Value", "ModifyTime")
  418. if tmpErr != nil {
  419. fmt.Println("tmpErr:", tmpErr)
  420. err = tmpErr
  421. return
  422. }
  423. }
  424. } else {
  425. //时间戳
  426. currentDate, _ := time.ParseInLocation(utils.FormatDate, v.DataTime, time.Local)
  427. timestamp := currentDate.UnixNano() / 1e6
  428. edbDataZjpj := &EdbDataPredictCalculateZjpj{
  429. EdbInfoId: edbInfo.EdbInfoId,
  430. EdbCode: edbInfo.EdbCode,
  431. DataTime: v.DataTime,
  432. Value: v.Value,
  433. Status: 1,
  434. CreateTime: time.Now(),
  435. ModifyTime: time.Now(),
  436. DataTimestamp: timestamp,
  437. }
  438. addDataList = append(addDataList, edbDataZjpj)
  439. }
  440. }
  441. }
  442. //删除已经不存在的累计同比拼接指标数据(由于同比值当日的数据删除了)
  443. {
  444. removeDateList := make([]string, 0)
  445. for dateTime := range removeDataTimeMap {
  446. removeDateList = append(removeDateList, dateTime)
  447. }
  448. if len(removeDateList) > 0 {
  449. removeDateStr := strings.Join(removeDateList, `","`)
  450. removeDateStr = `"` + removeDateStr + `"`
  451. //如果拼接指标变更了,那么需要删除所有的指标数据
  452. tableName := GetEdbDataTableName(edbInfo.Source)
  453. sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time in (%s) `, tableName, removeDateStr)
  454. _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
  455. if err != nil {
  456. err = fmt.Errorf("删除不存在的直接拼接指标数据失败,Err:" + err.Error())
  457. return
  458. }
  459. }
  460. }
  461. //数据入库
  462. if len(addDataList) > 0 {
  463. _, tmpErr := to.InsertMulti(len(addDataList), addDataList)
  464. if tmpErr != nil {
  465. err = tmpErr
  466. return
  467. }
  468. }
  469. return
  470. }