predict_edb_data_calculate_tcz.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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 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: 0,
  52. MoveType: 0,
  53. MoveFrequency: "",
  54. NoUpdate: 0,
  55. ServerUrl: "",
  56. EdbNameEn: "",
  57. UnitEn: "",
  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. edbInfo.EdbName = edbName
  108. edbInfo.EdbNameSource = edbName
  109. edbInfo.Frequency = frequency
  110. edbInfo.Unit = unit
  111. edbInfo.ClassifyId = classifyId
  112. edbInfo.ModifyTime = time.Now()
  113. _, err = to.Update(edbInfo, "EdbName", "EdbNameSource", "Frequency", "Unit", "ClassifyId", "ModifyTime")
  114. if err != nil {
  115. return
  116. }
  117. var existCondition string
  118. var existPars []interface{}
  119. existCondition += " AND edb_info_id=? AND from_edb_info_id=? "
  120. existPars = append(existPars, edbInfo.EdbInfoId, fromEdbInfo.EdbInfoId)
  121. //判断计算指标是否被更换
  122. var count int
  123. count, err = GetEdbInfoCalculateCountByCondition(existCondition, existPars)
  124. if err != nil {
  125. err = errors.New("判断指标是否改变失败,Err:" + err.Error())
  126. return
  127. }
  128. if count > 0 { // 指标未被替换,无需重新计算
  129. return
  130. }
  131. //删除,计算指标关联的,基础指标的关联关系
  132. sql := ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? `
  133. _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
  134. if err != nil {
  135. return
  136. }
  137. // 删除该指标所有的同差数据
  138. dataTableName := GetEdbDataTableName(utils.DATA_SOURCE_PREDICT_CALCULATE_TCZ)
  139. fmt.Println("dataTableName:", dataTableName)
  140. deleteSql := ` DELETE FROM %s WHERE edb_info_id=? `
  141. deleteSql = fmt.Sprintf(deleteSql, dataTableName)
  142. _, err = to.Raw(deleteSql, edbInfo.EdbInfoId).Exec()
  143. if err != nil {
  144. return
  145. }
  146. }
  147. edbInfoIdStr := strconv.Itoa(edbInfo.EdbInfoId)
  148. //获取源指标数据
  149. fmt.Println("EdbInfoId:", fromEdbInfo.EdbInfoId)
  150. dataList, err := GetPredictEdbDataListAll(fromEdbInfo, 0)
  151. if err != nil {
  152. return
  153. }
  154. latestDateStr = fromEdbInfo.LatestDate
  155. // 计算数据
  156. var dateArr []string
  157. dataMap := make(map[string]*EdbInfoSearchData)
  158. for _, v := range dataList {
  159. dateArr = append(dateArr, v.DataTime)
  160. dataMap[v.DataTime] = v
  161. }
  162. //fmt.Println("Frequency:", fromEdbInfo.Frequency)
  163. addSql := ` INSERT INTO edb_data_predict_calculate_tcz (edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  164. var isAdd bool
  165. existMap := make(map[string]string)
  166. for _, av := range dateArr {
  167. //fmt.Println("dateArr:", ak, av)
  168. currentItem := dataMap[av]
  169. if currentItem != nil {
  170. //当前日期
  171. currentDate, tmpErr := time.ParseInLocation(utils.FormatDate, av, time.Local)
  172. if tmpErr != nil {
  173. err = tmpErr
  174. return
  175. }
  176. //上一年的日期
  177. preDate := currentDate.AddDate(-1, 0, 0)
  178. preDateStr := preDate.Format(utils.FormatDate)
  179. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期找到
  180. //dataTime, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
  181. if _, ok := existMap[edbCode+av]; !ok {
  182. timestamp := currentDate.UnixNano() / 1e6
  183. timestampStr := fmt.Sprintf("%d", timestamp)
  184. val := TczSub(currentItem.Value, findItem.Value)
  185. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  186. isAdd = true
  187. // 实际数据的值
  188. if fromEdbInfo.LatestDate == av {
  189. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  190. if tmpErr != nil {
  191. return
  192. }
  193. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  194. }
  195. }
  196. existMap[edbCode+av] = av
  197. } else {
  198. if fromEdbInfo.Frequency == "月度" { //向上和向下,各找一个月
  199. nextDateDay := preDate
  200. preDateDay := preDate
  201. for i := 0; i <= 35; i++ {
  202. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  203. if findItem, ok := dataMap[nextDateDayStr]; ok { //下一年同期->下一个月找到
  204. if _, ok := existMap[edbCode+av]; !ok {
  205. timestamp := currentDate.UnixNano() / 1e6
  206. timestampStr := fmt.Sprintf("%d", timestamp)
  207. val := TczSub(currentItem.Value, findItem.Value)
  208. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  209. isAdd = true
  210. // 实际数据的值
  211. if fromEdbInfo.LatestDate == av {
  212. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  213. if tmpErr != nil {
  214. return
  215. }
  216. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  217. }
  218. }
  219. existMap[edbCode+av] = av
  220. break
  221. } else {
  222. preDateDayStr := preDateDay.Format(utils.FormatDate)
  223. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  224. if _, ok := existMap[edbCode+av]; !ok {
  225. timestamp := currentDate.UnixNano() / 1e6
  226. timestampStr := fmt.Sprintf("%d", timestamp)
  227. val := TczSub(currentItem.Value, findItem.Value)
  228. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  229. isAdd = true
  230. // 实际数据的值
  231. if fromEdbInfo.LatestDate == av {
  232. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  233. if tmpErr != nil {
  234. return
  235. }
  236. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  237. }
  238. }
  239. existMap[edbCode+av] = av
  240. break
  241. }
  242. }
  243. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  244. preDateDay = preDateDay.AddDate(0, 0, -1)
  245. }
  246. } else if fromEdbInfo.Frequency == "季度" || fromEdbInfo.Frequency == "年度" {
  247. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期->下一个月找到
  248. if _, ok := existMap[edbCode+av]; !ok {
  249. timestamp := currentDate.UnixNano() / 1e6
  250. timestampStr := fmt.Sprintf("%d", timestamp)
  251. val := TczSub(currentItem.Value, findItem.Value)
  252. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  253. isAdd = true
  254. // 实际数据的值
  255. if fromEdbInfo.LatestDate == av {
  256. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  257. if tmpErr != nil {
  258. return
  259. }
  260. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  261. }
  262. }
  263. existMap[edbCode+av] = av
  264. break
  265. }
  266. } else {
  267. nextDateDay := preDate
  268. preDateDay := preDate
  269. for i := 0; i < 35; i++ {
  270. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  271. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  272. if _, ok := existMap[edbCode+av]; !ok {
  273. timestamp := currentDate.UnixNano() / 1e6
  274. timestampStr := fmt.Sprintf("%d", timestamp)
  275. val := TczSub(currentItem.Value, findItem.Value)
  276. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  277. isAdd = true
  278. // 实际数据的值
  279. if fromEdbInfo.LatestDate == av {
  280. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  281. if tmpErr != nil {
  282. return
  283. }
  284. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  285. }
  286. }
  287. existMap[edbCode+av] = av
  288. break
  289. } else {
  290. preDateDayStr := preDateDay.Format(utils.FormatDate)
  291. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  292. if _, ok := existMap[edbCode+av]; !ok {
  293. timestamp := currentDate.UnixNano() / 1e6
  294. timestampStr := fmt.Sprintf("%d", timestamp)
  295. val := TczSub(currentItem.Value, findItem.Value)
  296. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  297. isAdd = true
  298. // 实际数据的值
  299. if fromEdbInfo.LatestDate == av {
  300. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  301. if tmpErr != nil {
  302. return
  303. }
  304. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  305. }
  306. }
  307. existMap[edbCode+av] = av
  308. break
  309. } else {
  310. }
  311. }
  312. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  313. preDateDay = preDateDay.AddDate(0, 0, -1)
  314. }
  315. }
  316. }
  317. }
  318. }
  319. if isAdd {
  320. addSql = strings.TrimRight(addSql, ",")
  321. _, err = to.Raw(addSql).Exec()
  322. if err != nil {
  323. return
  324. }
  325. }
  326. return
  327. }
  328. // RefreshAllPredictCalculateTcz 刷新全部同差值数据
  329. func RefreshAllPredictCalculateTcz(edbInfoId, source int, fromEdbInfo *EdbInfo, edbCode, startDate string) (latestDateStr string, latestValue float64, err error) {
  330. o := orm.NewOrm()
  331. to, err := o.Begin()
  332. if err != nil {
  333. return
  334. }
  335. defer func() {
  336. if err != nil {
  337. fmt.Println("RefreshAllCalculateTcz,Err:" + err.Error())
  338. _ = to.Rollback()
  339. } else {
  340. _ = to.Commit()
  341. }
  342. }()
  343. if err != nil {
  344. return
  345. }
  346. edbInfoIdStr := strconv.Itoa(edbInfoId)
  347. // 获取关联指标数据
  348. dataList, err := GetPredictEdbDataListAllByStartDate(fromEdbInfo, 0, startDate)
  349. if err != nil {
  350. return
  351. }
  352. latestDateStr = fromEdbInfo.LatestDate
  353. //计算数据
  354. var dateArr []string
  355. dataMap := make(map[string]*EdbInfoSearchData)
  356. for _, v := range dataList {
  357. dateArr = append(dateArr, v.DataTime)
  358. dataMap[v.DataTime] = v
  359. }
  360. //获取指标所有数据
  361. existDataList := make([]*EdbData, 0)
  362. dataTableName := GetEdbDataTableName(source)
  363. sql := `SELECT * FROM %s WHERE edb_info_id=? `
  364. sql = fmt.Sprintf(sql, dataTableName)
  365. _, err = o.Raw(sql, edbInfoId).QueryRows(&existDataList)
  366. if err != nil {
  367. return
  368. }
  369. existDataMap := make(map[string]string)
  370. for _, v := range existDataList {
  371. existDataMap[v.DataTime] = v.Value
  372. }
  373. addSql := ` INSERT INTO edb_data_predict_calculate_tcz(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  374. var isAdd bool
  375. existAddDataMap := make(map[string]string)
  376. for _, av := range dateArr {
  377. currentItem := dataMap[av]
  378. if currentItem != nil {
  379. //当前日期
  380. currentDate, tmpErr := time.ParseInLocation(utils.FormatDate, av, time.Local)
  381. if tmpErr != nil {
  382. err = tmpErr
  383. return
  384. }
  385. //上一年的日期
  386. preDate := currentDate.AddDate(-1, 0, 0)
  387. preDateStr := preDate.Format(utils.FormatDate)
  388. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期找到
  389. //dataTime, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
  390. timestamp := currentDate.UnixNano() / 1e6
  391. timestampStr := fmt.Sprintf("%d", timestamp)
  392. val := TczSub(currentItem.Value, findItem.Value)
  393. if existVal, ok := existDataMap[av]; !ok {
  394. if _, existOk := existAddDataMap[av]; !existOk {
  395. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  396. isAdd = true
  397. // 实际数据的值
  398. if fromEdbInfo.LatestDate == av {
  399. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  400. if tmpErr != nil {
  401. return
  402. }
  403. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  404. }
  405. }
  406. existAddDataMap[av] = av
  407. } else {
  408. existValDecimal, tmpErr := decimal.NewFromString(existVal)
  409. if tmpErr != nil {
  410. err = tmpErr
  411. return
  412. }
  413. existStr := existValDecimal.String()
  414. if existStr != val {
  415. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  416. sql = fmt.Sprintf(sql, dataTableName)
  417. _, err = o.Raw(sql, val, edbInfoId, av).Exec()
  418. if err != nil {
  419. return
  420. }
  421. }
  422. }
  423. utils.FileLog.Info("同期找到:" + av + ";" + preDateStr)
  424. continue
  425. } else {
  426. if fromEdbInfo.Frequency == "月度" { //向上和向下,各找一个月
  427. for i := 0; i <= 35; i++ {
  428. nextDateDay := preDate.AddDate(0, 0, 1)
  429. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  430. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  431. timestamp := currentDate.UnixNano() / 1e6
  432. timestampStr := fmt.Sprintf("%d", timestamp)
  433. val := TczSub(currentItem.Value, findItem.Value)
  434. if existVal, ok := existDataMap[av]; !ok {
  435. if _, existOk := existAddDataMap[av]; !existOk {
  436. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  437. isAdd = true
  438. // 实际数据的值
  439. if fromEdbInfo.LatestDate == av {
  440. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  441. if tmpErr != nil {
  442. return
  443. }
  444. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  445. }
  446. }
  447. existAddDataMap[av] = av
  448. } else {
  449. existValDecimal, tmpErr := decimal.NewFromString(existVal)
  450. if tmpErr != nil {
  451. err = tmpErr
  452. return
  453. }
  454. existStr := existValDecimal.String()
  455. if existStr != val {
  456. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  457. sql = fmt.Sprintf(sql, dataTableName)
  458. _, err = o.Raw(sql, val, edbInfoId, av).Exec()
  459. if err != nil {
  460. return
  461. }
  462. }
  463. }
  464. break
  465. } else {
  466. preDateDay := preDate.AddDate(0, 0, -1)
  467. preDateDayStr := preDateDay.Format(utils.FormatDate)
  468. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  469. timestamp := currentDate.UnixNano() / 1e6
  470. timestampStr := fmt.Sprintf("%d", timestamp)
  471. val := TczSub(currentItem.Value, findItem.Value)
  472. if existVal, ok := existDataMap[av]; !ok {
  473. if _, existOk := existAddDataMap[av]; !existOk {
  474. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  475. isAdd = true
  476. // 实际数据的值
  477. if fromEdbInfo.LatestDate == av {
  478. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  479. if tmpErr != nil {
  480. return
  481. }
  482. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  483. }
  484. }
  485. existAddDataMap[av] = av
  486. } else {
  487. existValDecimal, tmpErr := decimal.NewFromString(existVal)
  488. if tmpErr != nil {
  489. err = tmpErr
  490. return
  491. }
  492. existStr := existValDecimal.String()
  493. if existStr != val {
  494. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  495. sql = fmt.Sprintf(sql, dataTableName)
  496. _, err = o.Raw(sql, val, edbInfoId, av).Exec()
  497. if err != nil {
  498. return
  499. }
  500. }
  501. }
  502. break
  503. }
  504. }
  505. }
  506. } else if fromEdbInfo.Frequency == "季度" || fromEdbInfo.Frequency == "年度" {
  507. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期->下一个月找到
  508. timestamp := currentDate.UnixNano() / 1e6
  509. timestampStr := fmt.Sprintf("%d", timestamp)
  510. val := TczSub(currentItem.Value, findItem.Value)
  511. if existVal, ok := existDataMap[av]; !ok {
  512. if _, existOk := existAddDataMap[av]; !existOk {
  513. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  514. isAdd = true
  515. // 实际数据的值
  516. if fromEdbInfo.LatestDate == av {
  517. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  518. if tmpErr != nil {
  519. return
  520. }
  521. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  522. }
  523. }
  524. existAddDataMap[av] = av
  525. } else {
  526. existValDecimal, tmpErr := decimal.NewFromString(existVal)
  527. if tmpErr != nil {
  528. err = tmpErr
  529. return
  530. }
  531. existStr := existValDecimal.String()
  532. if existStr != val {
  533. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  534. sql = fmt.Sprintf(sql, dataTableName)
  535. _, err = o.Raw(sql, val, edbInfoId, av).Exec()
  536. if err != nil {
  537. return
  538. }
  539. }
  540. }
  541. break
  542. }
  543. } else {
  544. for i := 0; i < 35; i++ {
  545. nextDateDay := preDate.AddDate(0, 0, 1)
  546. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  547. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  548. timestamp := currentDate.UnixNano() / 1e6
  549. timestampStr := fmt.Sprintf("%d", timestamp)
  550. val := TczSub(currentItem.Value, findItem.Value)
  551. if existVal, ok := existDataMap[av]; !ok {
  552. if _, existOk := existAddDataMap[av]; !existOk {
  553. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  554. isAdd = true
  555. // 实际数据的值
  556. if fromEdbInfo.LatestDate == av {
  557. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  558. if tmpErr != nil {
  559. return
  560. }
  561. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  562. }
  563. }
  564. existAddDataMap[av] = av
  565. } else {
  566. existValDecimal, tmpErr := decimal.NewFromString(existVal)
  567. if tmpErr != nil {
  568. err = tmpErr
  569. return
  570. }
  571. existStr := existValDecimal.String()
  572. if existStr != val {
  573. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  574. sql = fmt.Sprintf(sql, dataTableName)
  575. _, err = o.Raw(sql, val, edbInfoId, av).Exec()
  576. if err != nil {
  577. return
  578. }
  579. }
  580. }
  581. break
  582. } else {
  583. preDateDay := preDate.AddDate(0, 0, -1)
  584. preDateDayStr := preDateDay.Format(utils.FormatDate)
  585. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  586. timestamp := currentDate.UnixNano() / 1e6
  587. timestampStr := fmt.Sprintf("%d", timestamp)
  588. val := TczSub(currentItem.Value, findItem.Value)
  589. if existVal, ok := existDataMap[av]; !ok {
  590. if _, existOk := existAddDataMap[av]; !existOk {
  591. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  592. isAdd = true
  593. // 实际数据的值
  594. if fromEdbInfo.LatestDate == av {
  595. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  596. if tmpErr != nil {
  597. return
  598. }
  599. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  600. }
  601. }
  602. existAddDataMap[av] = av
  603. } else {
  604. existValDecimal, tmpErr := decimal.NewFromString(existVal)
  605. if tmpErr != nil {
  606. err = tmpErr
  607. return
  608. }
  609. existStr := existValDecimal.String()
  610. if existStr != val {
  611. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  612. sql = fmt.Sprintf(sql, dataTableName)
  613. _, err = o.Raw(sql, val, edbInfoId, av).Exec()
  614. if err != nil {
  615. return
  616. }
  617. }
  618. }
  619. break
  620. }
  621. }
  622. }
  623. }
  624. }
  625. }
  626. }
  627. if isAdd {
  628. addSql = strings.TrimRight(addSql, ",")
  629. _, err = o.Raw(addSql).Exec()
  630. if err != nil {
  631. return
  632. }
  633. }
  634. return
  635. }