predict_edb_data_calculate_tcz.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. package models
  2. import (
  3. "errors"
  4. "eta/eta_index_lib/utils"
  5. "fmt"
  6. "github.com/beego/beego/v2/client/orm"
  7. "github.com/shopspring/decimal"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. // SavePredictCalculateTcz 同差值
  13. func SavePredictCalculateTcz(reqEdbInfoId, classifyId int, edbName, frequency, unit, formula string, fromEdbInfo *EdbInfo, edbCode, uniqueCode string, sysUserId int, sysUserRealName, lang string) (edbInfo *EdbInfo, latestDateStr string, latestValue float64, err error, errMsg string) {
  14. o := orm.NewOrm()
  15. to, err := o.Begin()
  16. if err != nil {
  17. return
  18. }
  19. defer func() {
  20. if err != nil {
  21. fmt.Println("AddCalculateTcz,Err:" + err.Error())
  22. _ = to.Rollback()
  23. } else {
  24. _ = to.Commit()
  25. }
  26. }()
  27. fmt.Println("reqEdbInfoId:", reqEdbInfoId)
  28. if reqEdbInfoId <= 0 {
  29. edbInfo = &EdbInfo{
  30. //EdbInfoId: 0,
  31. EdbInfoType: 1,
  32. SourceName: "预测同差",
  33. Source: utils.DATA_SOURCE_PREDICT_CALCULATE_TCZ,
  34. EdbCode: edbCode,
  35. EdbName: edbName,
  36. EdbNameSource: edbName,
  37. Frequency: frequency,
  38. Unit: unit,
  39. //StartDate: "",
  40. //EndDate: "",
  41. ClassifyId: classifyId,
  42. SysUserId: sysUserId,
  43. SysUserRealName: sysUserRealName,
  44. UniqueCode: uniqueCode,
  45. CreateTime: time.Now(),
  46. ModifyTime: time.Now(),
  47. MinValue: 0,
  48. MaxValue: 0,
  49. CalculateFormula: formula,
  50. EdbType: 2,
  51. Sort: GetAddEdbMaxSortByClassifyId(classifyId, utils.PREDICT_EDB_INFO_TYPE),
  52. MoveType: 0,
  53. MoveFrequency: "",
  54. NoUpdate: 0,
  55. ServerUrl: "",
  56. EdbNameEn: edbName,
  57. UnitEn: unit,
  58. LatestDate: "",
  59. LatestValue: 0,
  60. ChartImage: "",
  61. }
  62. newEdbInfoId, tmpErr := to.Insert(edbInfo)
  63. if tmpErr != nil {
  64. err = tmpErr
  65. return
  66. }
  67. edbInfo.EdbInfoId = int(newEdbInfoId)
  68. // 添加关联关系
  69. {
  70. calculateMappingItem := &EdbInfoCalculateMapping{
  71. EdbInfoCalculateMappingId: 0,
  72. EdbInfoId: edbInfo.EdbInfoId,
  73. Source: edbInfo.Source,
  74. SourceName: edbInfo.SourceName,
  75. EdbCode: edbCode,
  76. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  77. FromEdbCode: fromEdbInfo.EdbCode,
  78. FromEdbName: fromEdbInfo.EdbName,
  79. FromSource: fromEdbInfo.Source,
  80. FromSourceName: fromEdbInfo.SourceName,
  81. FromTag: "",
  82. Sort: 1,
  83. CreateTime: time.Now(),
  84. ModifyTime: time.Now(),
  85. }
  86. _, err = to.Insert(calculateMappingItem)
  87. if err != nil {
  88. return
  89. }
  90. }
  91. } else {
  92. edbInfo, err = GetEdbInfoById(reqEdbInfoId)
  93. if err != nil {
  94. if err.Error() == utils.ErrNoRow() {
  95. errMsg = `获取指标信息失败`
  96. }
  97. return
  98. }
  99. if edbInfo.EdbInfoType != 1 {
  100. errMsg = `该指标不是预测指标`
  101. err = errors.New(errMsg)
  102. return
  103. }
  104. latestDateStr = edbInfo.LatestDate
  105. latestValue = edbInfo.LatestValue
  106. //修改指标信息
  107. switch lang {
  108. case utils.EnLangVersion:
  109. edbInfo.EdbNameEn = edbName
  110. edbInfo.UnitEn = unit
  111. default:
  112. edbInfo.EdbName = edbName
  113. edbInfo.Unit = unit
  114. edbInfo.EdbNameSource = edbName
  115. }
  116. edbInfo.Frequency = frequency
  117. edbInfo.ClassifyId = classifyId
  118. edbInfo.ModifyTime = time.Now()
  119. _, err = to.Update(edbInfo, "EdbName", "EdbNameSource", "Frequency", "Unit", "ClassifyId", "ModifyTime", "EdbNameEn", "UnitEn")
  120. if err != nil {
  121. return
  122. }
  123. var existCondition string
  124. var existPars []interface{}
  125. existCondition += " AND edb_info_id=? AND from_edb_info_id=? "
  126. existPars = append(existPars, edbInfo.EdbInfoId, fromEdbInfo.EdbInfoId)
  127. //判断计算指标是否被更换
  128. var count int
  129. count, err = GetEdbInfoCalculateCountByCondition(existCondition, existPars)
  130. if err != nil {
  131. err = errors.New("判断指标是否改变失败,Err:" + err.Error())
  132. return
  133. }
  134. if count > 0 { // 指标未被替换,无需重新计算
  135. return
  136. }
  137. //删除,计算指标关联的,基础指标的关联关系
  138. sql := ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? `
  139. _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
  140. if err != nil {
  141. return
  142. }
  143. // 删除该指标所有的同差数据
  144. dataTableName := GetEdbDataTableName(utils.DATA_SOURCE_PREDICT_CALCULATE_TCZ, utils.DATA_SUB_SOURCE_EDB)
  145. fmt.Println("dataTableName:", dataTableName)
  146. deleteSql := ` DELETE FROM %s WHERE edb_info_id=? `
  147. deleteSql = fmt.Sprintf(deleteSql, dataTableName)
  148. _, err = to.Raw(deleteSql, edbInfo.EdbInfoId).Exec()
  149. if err != nil {
  150. return
  151. }
  152. }
  153. edbInfoIdStr := strconv.Itoa(edbInfo.EdbInfoId)
  154. //获取源指标数据
  155. fmt.Println("EdbInfoId:", fromEdbInfo.EdbInfoId)
  156. dataList, err := GetPredictEdbDataListAll(fromEdbInfo, 0)
  157. if err != nil {
  158. return
  159. }
  160. latestDateStr = fromEdbInfo.LatestDate
  161. // 计算数据
  162. var dateArr []string
  163. dataMap := make(map[string]*EdbInfoSearchData)
  164. for _, v := range dataList {
  165. dateArr = append(dateArr, v.DataTime)
  166. dataMap[v.DataTime] = v
  167. }
  168. //fmt.Println("Frequency:", fromEdbInfo.Frequency)
  169. addSql := ` INSERT INTO edb_data_predict_calculate_tcz (edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  170. var isAdd bool
  171. existMap := make(map[string]string)
  172. for _, av := range dateArr {
  173. //fmt.Println("dateArr:", ak, av)
  174. currentItem := dataMap[av]
  175. if currentItem != nil {
  176. //当前日期
  177. currentDate, tmpErr := time.ParseInLocation(utils.FormatDate, av, time.Local)
  178. if tmpErr != nil {
  179. err = tmpErr
  180. return
  181. }
  182. //上一年的日期
  183. preDate := currentDate.AddDate(-1, 0, 0)
  184. preDateStr := preDate.Format(utils.FormatDate)
  185. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期找到
  186. //dataTime, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
  187. if _, ok := existMap[edbCode+av]; !ok {
  188. timestamp := currentDate.UnixNano() / 1e6
  189. timestampStr := fmt.Sprintf("%d", timestamp)
  190. val := TczSub(currentItem.Value, findItem.Value)
  191. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  192. isAdd = true
  193. // 实际数据的值
  194. if fromEdbInfo.LatestDate == av {
  195. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  196. if tmpErr != nil {
  197. return
  198. }
  199. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  200. }
  201. }
  202. existMap[edbCode+av] = av
  203. } else {
  204. if fromEdbInfo.Frequency == "月度" { //向上和向下,各找一个月
  205. nextDateDay := preDate
  206. preDateDay := preDate
  207. for i := 0; i <= 35; i++ {
  208. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  209. if findItem, ok := dataMap[nextDateDayStr]; ok { //下一年同期->下一个月找到
  210. if _, ok := existMap[edbCode+av]; !ok {
  211. timestamp := currentDate.UnixNano() / 1e6
  212. timestampStr := fmt.Sprintf("%d", timestamp)
  213. val := TczSub(currentItem.Value, findItem.Value)
  214. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  215. isAdd = true
  216. // 实际数据的值
  217. if fromEdbInfo.LatestDate == av {
  218. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  219. if tmpErr != nil {
  220. return
  221. }
  222. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  223. }
  224. }
  225. existMap[edbCode+av] = av
  226. break
  227. } else {
  228. preDateDayStr := preDateDay.Format(utils.FormatDate)
  229. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  230. if _, ok := existMap[edbCode+av]; !ok {
  231. timestamp := currentDate.UnixNano() / 1e6
  232. timestampStr := fmt.Sprintf("%d", timestamp)
  233. val := TczSub(currentItem.Value, findItem.Value)
  234. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  235. isAdd = true
  236. // 实际数据的值
  237. if fromEdbInfo.LatestDate == av {
  238. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  239. if tmpErr != nil {
  240. return
  241. }
  242. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  243. }
  244. }
  245. existMap[edbCode+av] = av
  246. break
  247. }
  248. }
  249. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  250. preDateDay = preDateDay.AddDate(0, 0, -1)
  251. }
  252. } else if fromEdbInfo.Frequency == "季度" || fromEdbInfo.Frequency == "年度" {
  253. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期->下一个月找到
  254. if _, ok := existMap[edbCode+av]; !ok {
  255. timestamp := currentDate.UnixNano() / 1e6
  256. timestampStr := fmt.Sprintf("%d", timestamp)
  257. val := TczSub(currentItem.Value, findItem.Value)
  258. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  259. isAdd = true
  260. // 实际数据的值
  261. if fromEdbInfo.LatestDate == av {
  262. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  263. if tmpErr != nil {
  264. return
  265. }
  266. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  267. }
  268. }
  269. existMap[edbCode+av] = av
  270. break
  271. }
  272. } else {
  273. nextDateDay := preDate
  274. preDateDay := preDate
  275. for i := 0; i < 35; i++ {
  276. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  277. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  278. if _, ok := existMap[edbCode+av]; !ok {
  279. timestamp := currentDate.UnixNano() / 1e6
  280. timestampStr := fmt.Sprintf("%d", timestamp)
  281. val := TczSub(currentItem.Value, findItem.Value)
  282. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  283. isAdd = true
  284. // 实际数据的值
  285. if fromEdbInfo.LatestDate == av {
  286. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  287. if tmpErr != nil {
  288. return
  289. }
  290. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  291. }
  292. }
  293. existMap[edbCode+av] = av
  294. break
  295. } else {
  296. preDateDayStr := preDateDay.Format(utils.FormatDate)
  297. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  298. if _, ok := existMap[edbCode+av]; !ok {
  299. timestamp := currentDate.UnixNano() / 1e6
  300. timestampStr := fmt.Sprintf("%d", timestamp)
  301. val := TczSub(currentItem.Value, findItem.Value)
  302. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  303. isAdd = true
  304. // 实际数据的值
  305. if fromEdbInfo.LatestDate == av {
  306. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  307. if tmpErr != nil {
  308. return
  309. }
  310. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  311. }
  312. }
  313. existMap[edbCode+av] = av
  314. break
  315. } else {
  316. }
  317. }
  318. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  319. preDateDay = preDateDay.AddDate(0, 0, -1)
  320. }
  321. }
  322. }
  323. }
  324. }
  325. if isAdd {
  326. addSql = strings.TrimRight(addSql, ",")
  327. _, err = to.Raw(addSql).Exec()
  328. if err != nil {
  329. return
  330. }
  331. }
  332. return
  333. }
  334. // RefreshAllPredictCalculateTcz 刷新全部同差值数据
  335. func RefreshAllPredictCalculateTcz(edbInfoId, source, subSource int, fromEdbInfo *EdbInfo, edbCode, startDate string) (latestDateStr string, latestValue float64, err error) {
  336. o := orm.NewOrm()
  337. to, err := o.Begin()
  338. if err != nil {
  339. return
  340. }
  341. defer func() {
  342. if err != nil {
  343. fmt.Println("RefreshAllCalculateTcz,Err:" + err.Error())
  344. _ = to.Rollback()
  345. } else {
  346. _ = to.Commit()
  347. }
  348. }()
  349. if err != nil {
  350. return
  351. }
  352. edbInfoIdStr := strconv.Itoa(edbInfoId)
  353. // 获取关联指标数据
  354. dataList, err := GetPredictEdbDataListAllByStartDate(fromEdbInfo, 0, startDate)
  355. if err != nil {
  356. return
  357. }
  358. latestDateStr = fromEdbInfo.LatestDate
  359. //计算数据
  360. var dateArr []string
  361. dataMap := make(map[string]*EdbInfoSearchData)
  362. for _, v := range dataList {
  363. dateArr = append(dateArr, v.DataTime)
  364. dataMap[v.DataTime] = v
  365. }
  366. //获取指标所有数据
  367. existDataList := make([]*EdbData, 0)
  368. dataTableName := GetEdbDataTableName(source, subSource)
  369. sql := `SELECT * FROM %s WHERE edb_info_id=? `
  370. sql = fmt.Sprintf(sql, dataTableName)
  371. _, err = o.Raw(sql, edbInfoId).QueryRows(&existDataList)
  372. if err != nil {
  373. return
  374. }
  375. existDataMap := make(map[string]string)
  376. removeDateMap := make(map[string]bool) //需要移除的日期
  377. for _, v := range existDataList {
  378. existDataMap[v.DataTime] = v.Value
  379. removeDateMap[v.DataTime] = true
  380. }
  381. addSql := ` INSERT INTO edb_data_predict_calculate_tcz(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  382. var isAdd bool
  383. existAddDataMap := make(map[string]string)
  384. for _, av := range dateArr {
  385. currentItem := dataMap[av]
  386. if currentItem != nil {
  387. delete(removeDateMap, av)
  388. //当前日期
  389. currentDate, tmpErr := time.ParseInLocation(utils.FormatDate, av, time.Local)
  390. if tmpErr != nil {
  391. err = tmpErr
  392. return
  393. }
  394. //上一年的日期
  395. preDate := currentDate.AddDate(-1, 0, 0)
  396. preDateStr := preDate.Format(utils.FormatDate)
  397. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期找到
  398. //dataTime, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
  399. timestamp := currentDate.UnixNano() / 1e6
  400. timestampStr := fmt.Sprintf("%d", timestamp)
  401. val := TczSub(currentItem.Value, findItem.Value)
  402. if existVal, ok := existDataMap[av]; !ok {
  403. if _, existOk := existAddDataMap[av]; !existOk {
  404. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  405. isAdd = true
  406. // 实际数据的值
  407. if fromEdbInfo.LatestDate == av {
  408. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  409. if tmpErr != nil {
  410. return
  411. }
  412. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  413. }
  414. }
  415. existAddDataMap[av] = av
  416. } else {
  417. existValDecimal, tmpErr := decimal.NewFromString(existVal)
  418. if tmpErr != nil {
  419. err = tmpErr
  420. return
  421. }
  422. existStr := existValDecimal.String()
  423. if existStr != val {
  424. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  425. sql = fmt.Sprintf(sql, dataTableName)
  426. _, err = o.Raw(sql, val, edbInfoId, av).Exec()
  427. if err != nil {
  428. return
  429. }
  430. }
  431. }
  432. utils.FileLog.Info("同期找到:" + av + ";" + preDateStr)
  433. continue
  434. } else {
  435. if fromEdbInfo.Frequency == "月度" { //向上和向下,各找一个月
  436. for i := 0; i <= 35; i++ {
  437. nextDateDay := preDate.AddDate(0, 0, 1)
  438. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  439. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  440. timestamp := currentDate.UnixNano() / 1e6
  441. timestampStr := fmt.Sprintf("%d", timestamp)
  442. val := TczSub(currentItem.Value, findItem.Value)
  443. if existVal, ok := existDataMap[av]; !ok {
  444. if _, existOk := existAddDataMap[av]; !existOk {
  445. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  446. isAdd = true
  447. // 实际数据的值
  448. if fromEdbInfo.LatestDate == av {
  449. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  450. if tmpErr != nil {
  451. return
  452. }
  453. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  454. }
  455. }
  456. existAddDataMap[av] = av
  457. } else {
  458. existValDecimal, tmpErr := decimal.NewFromString(existVal)
  459. if tmpErr != nil {
  460. err = tmpErr
  461. return
  462. }
  463. existStr := existValDecimal.String()
  464. if existStr != val {
  465. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  466. sql = fmt.Sprintf(sql, dataTableName)
  467. _, err = o.Raw(sql, val, edbInfoId, av).Exec()
  468. if err != nil {
  469. return
  470. }
  471. }
  472. }
  473. break
  474. } else {
  475. preDateDay := preDate.AddDate(0, 0, -1)
  476. preDateDayStr := preDateDay.Format(utils.FormatDate)
  477. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  478. timestamp := currentDate.UnixNano() / 1e6
  479. timestampStr := fmt.Sprintf("%d", timestamp)
  480. val := TczSub(currentItem.Value, findItem.Value)
  481. if existVal, ok := existDataMap[av]; !ok {
  482. if _, existOk := existAddDataMap[av]; !existOk {
  483. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  484. isAdd = true
  485. // 实际数据的值
  486. if fromEdbInfo.LatestDate == av {
  487. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  488. if tmpErr != nil {
  489. return
  490. }
  491. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  492. }
  493. }
  494. existAddDataMap[av] = av
  495. } else {
  496. existValDecimal, tmpErr := decimal.NewFromString(existVal)
  497. if tmpErr != nil {
  498. err = tmpErr
  499. return
  500. }
  501. existStr := existValDecimal.String()
  502. if existStr != val {
  503. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  504. sql = fmt.Sprintf(sql, dataTableName)
  505. _, err = o.Raw(sql, val, edbInfoId, av).Exec()
  506. if err != nil {
  507. return
  508. }
  509. }
  510. }
  511. break
  512. }
  513. }
  514. }
  515. } else if fromEdbInfo.Frequency == "季度" || fromEdbInfo.Frequency == "年度" {
  516. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期->下一个月找到
  517. timestamp := currentDate.UnixNano() / 1e6
  518. timestampStr := fmt.Sprintf("%d", timestamp)
  519. val := TczSub(currentItem.Value, findItem.Value)
  520. if existVal, ok := existDataMap[av]; !ok {
  521. if _, existOk := existAddDataMap[av]; !existOk {
  522. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  523. isAdd = true
  524. // 实际数据的值
  525. if fromEdbInfo.LatestDate == av {
  526. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  527. if tmpErr != nil {
  528. return
  529. }
  530. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  531. }
  532. }
  533. existAddDataMap[av] = av
  534. } else {
  535. existValDecimal, tmpErr := decimal.NewFromString(existVal)
  536. if tmpErr != nil {
  537. err = tmpErr
  538. return
  539. }
  540. existStr := existValDecimal.String()
  541. if existStr != val {
  542. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  543. sql = fmt.Sprintf(sql, dataTableName)
  544. _, err = o.Raw(sql, val, edbInfoId, av).Exec()
  545. if err != nil {
  546. return
  547. }
  548. }
  549. }
  550. break
  551. }
  552. } else {
  553. nextDateDay := preDate
  554. preDateDay := preDate
  555. for i := 0; i < 35; i++ {
  556. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  557. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  558. timestamp := currentDate.UnixNano() / 1e6
  559. timestampStr := fmt.Sprintf("%d", timestamp)
  560. val := TczSub(currentItem.Value, findItem.Value)
  561. if existVal, ok := existDataMap[av]; !ok {
  562. if _, existOk := existAddDataMap[av]; !existOk {
  563. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  564. isAdd = true
  565. // 实际数据的值
  566. if fromEdbInfo.LatestDate == av {
  567. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  568. if tmpErr != nil {
  569. return
  570. }
  571. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  572. }
  573. }
  574. existAddDataMap[av] = av
  575. } else {
  576. existValDecimal, tmpErr := decimal.NewFromString(existVal)
  577. if tmpErr != nil {
  578. err = tmpErr
  579. return
  580. }
  581. existStr := existValDecimal.String()
  582. if existStr != val {
  583. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  584. sql = fmt.Sprintf(sql, dataTableName)
  585. _, err = o.Raw(sql, val, edbInfoId, av).Exec()
  586. if err != nil {
  587. return
  588. }
  589. }
  590. }
  591. break
  592. } else {
  593. preDateDayStr := preDateDay.Format(utils.FormatDate)
  594. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  595. timestamp := currentDate.UnixNano() / 1e6
  596. timestampStr := fmt.Sprintf("%d", timestamp)
  597. val := TczSub(currentItem.Value, findItem.Value)
  598. if existVal, ok := existDataMap[av]; !ok {
  599. if _, existOk := existAddDataMap[av]; !existOk {
  600. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  601. isAdd = true
  602. // 实际数据的值
  603. if fromEdbInfo.LatestDate == av {
  604. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  605. if tmpErr != nil {
  606. return
  607. }
  608. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  609. }
  610. }
  611. existAddDataMap[av] = av
  612. } else {
  613. existValDecimal, tmpErr := decimal.NewFromString(existVal)
  614. if tmpErr != nil {
  615. err = tmpErr
  616. return
  617. }
  618. existStr := existValDecimal.String()
  619. if existStr != val {
  620. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  621. sql = fmt.Sprintf(sql, dataTableName)
  622. _, err = o.Raw(sql, val, edbInfoId, av).Exec()
  623. if err != nil {
  624. return
  625. }
  626. }
  627. }
  628. break
  629. }
  630. }
  631. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  632. preDateDay = preDateDay.AddDate(0, 0, -1)
  633. }
  634. }
  635. }
  636. }
  637. }
  638. if len(removeDateMap) > 0 {
  639. removeDateList := make([]string, 0) //需要移除的日期
  640. for k := range removeDateMap {
  641. removeDateList = append(removeDateList, k)
  642. }
  643. removeDateStr := strings.Join(removeDateList, `","`)
  644. removeDateStr = `"` + removeDateStr + `"`
  645. //如果拼接指标变更了,那么需要删除所有的指标数据
  646. tableName := GetEdbDataTableName(source, subSource)
  647. sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time in (%s) `, tableName, removeDateStr)
  648. _, err = to.Raw(sql, edbInfoId).Exec()
  649. if err != nil {
  650. err = fmt.Errorf("删除计算失败的计算指标数据失败,Err:" + err.Error())
  651. return
  652. }
  653. }
  654. if isAdd {
  655. addSql = strings.TrimRight(addSql, ",")
  656. _, err = o.Raw(addSql).Exec()
  657. if err != nil {
  658. return
  659. }
  660. }
  661. return
  662. }