predict_edb_data_calculate_tcz.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. package models
  2. import (
  3. "errors"
  4. "eta_gn/eta_index_lib/global"
  5. "eta_gn/eta_index_lib/utils"
  6. "fmt"
  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. to := global.DEFAULT_DmSQL.Begin()
  15. defer func() {
  16. if err != nil {
  17. to.Rollback()
  18. } else {
  19. to.Commit()
  20. }
  21. }()
  22. fmt.Println("reqEdbInfoId:", reqEdbInfoId)
  23. if reqEdbInfoId <= 0 {
  24. edbInfo = &EdbInfo{
  25. //EdbInfoId: 0,
  26. EdbInfoType: 1,
  27. SourceName: "预测同差",
  28. Source: utils.DATA_SOURCE_PREDICT_CALCULATE_TCZ,
  29. EdbCode: edbCode,
  30. EdbName: edbName,
  31. EdbNameSource: edbName,
  32. Frequency: frequency,
  33. Unit: unit,
  34. //StartDate: "",
  35. //EndDate: "",
  36. ClassifyId: classifyId,
  37. SysUserId: sysUserId,
  38. SysUserRealName: sysUserRealName,
  39. UniqueCode: uniqueCode,
  40. CreateTime: time.Now(),
  41. ModifyTime: time.Now(),
  42. MinValue: 0,
  43. MaxValue: 0,
  44. CalculateFormula: formula,
  45. EdbType: 2,
  46. Sort: GetAddEdbMaxSortByClassifyId(classifyId, utils.PREDICT_EDB_INFO_TYPE),
  47. MoveType: 0,
  48. MoveFrequency: "",
  49. NoUpdate: 0,
  50. ServerUrl: "",
  51. EdbNameEn: edbName,
  52. UnitEn: unit,
  53. LatestDate: "",
  54. LatestValue: 0,
  55. ChartImage: "",
  56. }
  57. tmpErr := to.Create(edbInfo).Error
  58. if tmpErr != nil {
  59. err = tmpErr
  60. return
  61. }
  62. // 添加关联关系
  63. {
  64. calculateMappingItem := &EdbInfoCalculateMapping{
  65. EdbInfoCalculateMappingId: 0,
  66. EdbInfoId: edbInfo.EdbInfoId,
  67. Source: edbInfo.Source,
  68. SourceName: edbInfo.SourceName,
  69. EdbCode: edbCode,
  70. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  71. FromEdbCode: fromEdbInfo.EdbCode,
  72. FromEdbName: fromEdbInfo.EdbName,
  73. FromSource: fromEdbInfo.Source,
  74. FromSourceName: fromEdbInfo.SourceName,
  75. FromTag: "",
  76. Sort: 1,
  77. CreateTime: time.Now(),
  78. ModifyTime: time.Now(),
  79. }
  80. err = to.Create(calculateMappingItem).Error
  81. if err != nil {
  82. return
  83. }
  84. }
  85. } else {
  86. edbInfo, err = GetEdbInfoById(reqEdbInfoId)
  87. if err != nil {
  88. if err.Error() == utils.ErrNoRow() {
  89. errMsg = `获取指标信息失败`
  90. }
  91. return
  92. }
  93. if edbInfo.EdbInfoType != 1 {
  94. errMsg = `该指标不是预测指标`
  95. err = errors.New(errMsg)
  96. return
  97. }
  98. latestDateStr = edbInfo.LatestDate
  99. latestValue = edbInfo.LatestValue
  100. //修改指标信息
  101. switch lang {
  102. case utils.EnLangVersion:
  103. edbInfo.EdbNameEn = edbName
  104. edbInfo.UnitEn = unit
  105. default:
  106. edbInfo.EdbName = edbName
  107. edbInfo.Unit = unit
  108. edbInfo.EdbNameSource = edbName
  109. }
  110. edbInfo.Frequency = frequency
  111. edbInfo.ClassifyId = classifyId
  112. edbInfo.ModifyTime = time.Now()
  113. err = to.Model(edbInfo).Select([]string{"EdbName", "EdbNameSource", "Frequency", "Unit", "ClassifyId", "ModifyTime", "EdbNameEn", "UnitEn"}).Updates(edbInfo).Error
  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.Exec(sql, edbInfo.EdbInfoId).Error
  134. if err != nil {
  135. return
  136. }
  137. // 删除该指标所有的同差数据
  138. dataTableName := GetEdbDataTableName(utils.DATA_SOURCE_PREDICT_CALCULATE_TCZ, utils.DATA_SUB_SOURCE_EDB)
  139. fmt.Println("dataTableName:", dataTableName)
  140. deleteSql := ` DELETE FROM %s WHERE edb_info_id=? `
  141. deleteSql = fmt.Sprintf(deleteSql, dataTableName)
  142. err = to.Exec(deleteSql, edbInfo.EdbInfoId).Error
  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.Exec(addSql).Error
  322. if err != nil {
  323. return
  324. }
  325. }
  326. return
  327. }
  328. // RefreshAllPredictCalculateTcz 刷新全部同差值数据
  329. func RefreshAllPredictCalculateTcz(edbInfoId, source, subSource int, fromEdbInfo *EdbInfo, edbCode, startDate string) (latestDateStr string, latestValue float64, err error) {
  330. to := global.DEFAULT_DmSQL.Begin()
  331. defer func() {
  332. if err != nil {
  333. to.Rollback()
  334. } else {
  335. to.Commit()
  336. }
  337. }()
  338. if err != nil {
  339. return
  340. }
  341. edbInfoIdStr := strconv.Itoa(edbInfoId)
  342. // 获取关联指标数据
  343. dataList, err := GetPredictEdbDataListAllByStartDate(fromEdbInfo, 0, startDate)
  344. if err != nil {
  345. return
  346. }
  347. latestDateStr = fromEdbInfo.LatestDate
  348. //计算数据
  349. var dateArr []string
  350. dataMap := make(map[string]*EdbInfoSearchData)
  351. for _, v := range dataList {
  352. dateArr = append(dateArr, v.DataTime)
  353. dataMap[v.DataTime] = v
  354. }
  355. dataTableName := GetEdbDataTableName(source, subSource)
  356. //获取指标所有数据
  357. existDataList, err := GetAllEdbDataListByTo(to, edbInfoId, source, subSource)
  358. if err != nil {
  359. return
  360. }
  361. existDataMap := make(map[string]string)
  362. for _, v := range existDataList {
  363. existDataMap[v.DataTime] = v.Value
  364. }
  365. addSql := ` INSERT INTO edb_data_predict_calculate_tcz(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  366. var isAdd bool
  367. existAddDataMap := make(map[string]string)
  368. for _, av := range dateArr {
  369. currentItem := dataMap[av]
  370. if currentItem != nil {
  371. //当前日期
  372. currentDate, tmpErr := time.ParseInLocation(utils.FormatDate, av, time.Local)
  373. if tmpErr != nil {
  374. err = tmpErr
  375. return
  376. }
  377. //上一年的日期
  378. preDate := currentDate.AddDate(-1, 0, 0)
  379. preDateStr := preDate.Format(utils.FormatDate)
  380. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期找到
  381. //dataTime, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
  382. timestamp := currentDate.UnixNano() / 1e6
  383. timestampStr := fmt.Sprintf("%d", timestamp)
  384. val := TczSub(currentItem.Value, findItem.Value)
  385. if existVal, ok := existDataMap[av]; !ok {
  386. if _, existOk := existAddDataMap[av]; !existOk {
  387. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  388. isAdd = true
  389. // 实际数据的值
  390. if fromEdbInfo.LatestDate == av {
  391. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  392. if tmpErr != nil {
  393. return
  394. }
  395. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  396. }
  397. }
  398. existAddDataMap[av] = av
  399. } else {
  400. existValDecimal, tmpErr := decimal.NewFromString(existVal)
  401. if tmpErr != nil {
  402. err = tmpErr
  403. return
  404. }
  405. existStr := existValDecimal.String()
  406. if existStr != val {
  407. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  408. sql = fmt.Sprintf(sql, dataTableName)
  409. err = to.Exec(sql, val, edbInfoId, av).Error
  410. if err != nil {
  411. return
  412. }
  413. }
  414. }
  415. utils.FileLog.Info("同期找到:" + av + ";" + preDateStr)
  416. continue
  417. } else {
  418. if fromEdbInfo.Frequency == "月度" { //向上和向下,各找一个月
  419. for i := 0; i <= 35; i++ {
  420. nextDateDay := preDate.AddDate(0, 0, 1)
  421. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  422. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  423. timestamp := currentDate.UnixNano() / 1e6
  424. timestampStr := fmt.Sprintf("%d", timestamp)
  425. val := TczSub(currentItem.Value, findItem.Value)
  426. if existVal, ok := existDataMap[av]; !ok {
  427. if _, existOk := existAddDataMap[av]; !existOk {
  428. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  429. isAdd = true
  430. // 实际数据的值
  431. if fromEdbInfo.LatestDate == av {
  432. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  433. if tmpErr != nil {
  434. return
  435. }
  436. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  437. }
  438. }
  439. existAddDataMap[av] = av
  440. } else {
  441. existValDecimal, tmpErr := decimal.NewFromString(existVal)
  442. if tmpErr != nil {
  443. err = tmpErr
  444. return
  445. }
  446. existStr := existValDecimal.String()
  447. if existStr != val {
  448. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  449. sql = fmt.Sprintf(sql, dataTableName)
  450. err = to.Exec(sql, val, edbInfoId, av).Error
  451. if err != nil {
  452. return
  453. }
  454. }
  455. }
  456. break
  457. } else {
  458. preDateDay := preDate.AddDate(0, 0, -1)
  459. preDateDayStr := preDateDay.Format(utils.FormatDate)
  460. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  461. timestamp := currentDate.UnixNano() / 1e6
  462. timestampStr := fmt.Sprintf("%d", timestamp)
  463. val := TczSub(currentItem.Value, findItem.Value)
  464. if existVal, ok := existDataMap[av]; !ok {
  465. if _, existOk := existAddDataMap[av]; !existOk {
  466. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  467. isAdd = true
  468. // 实际数据的值
  469. if fromEdbInfo.LatestDate == av {
  470. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  471. if tmpErr != nil {
  472. return
  473. }
  474. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  475. }
  476. }
  477. existAddDataMap[av] = av
  478. } else {
  479. existValDecimal, tmpErr := decimal.NewFromString(existVal)
  480. if tmpErr != nil {
  481. err = tmpErr
  482. return
  483. }
  484. existStr := existValDecimal.String()
  485. if existStr != val {
  486. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  487. sql = fmt.Sprintf(sql, dataTableName)
  488. err = to.Exec(sql, val, edbInfoId, av).Error
  489. if err != nil {
  490. return
  491. }
  492. }
  493. }
  494. break
  495. }
  496. }
  497. }
  498. } else if fromEdbInfo.Frequency == "季度" || fromEdbInfo.Frequency == "年度" {
  499. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期->下一个月找到
  500. timestamp := currentDate.UnixNano() / 1e6
  501. timestampStr := fmt.Sprintf("%d", timestamp)
  502. val := TczSub(currentItem.Value, findItem.Value)
  503. if existVal, ok := existDataMap[av]; !ok {
  504. if _, existOk := existAddDataMap[av]; !existOk {
  505. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  506. isAdd = true
  507. // 实际数据的值
  508. if fromEdbInfo.LatestDate == av {
  509. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  510. if tmpErr != nil {
  511. return
  512. }
  513. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  514. }
  515. }
  516. existAddDataMap[av] = av
  517. } else {
  518. existValDecimal, tmpErr := decimal.NewFromString(existVal)
  519. if tmpErr != nil {
  520. err = tmpErr
  521. return
  522. }
  523. existStr := existValDecimal.String()
  524. if existStr != val {
  525. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  526. sql = fmt.Sprintf(sql, dataTableName)
  527. err = to.Exec(sql, val, edbInfoId, av).Error
  528. if err != nil {
  529. return
  530. }
  531. }
  532. }
  533. break
  534. }
  535. } else {
  536. nextDateDay := preDate
  537. preDateDay := preDate
  538. for i := 0; i < 35; i++ {
  539. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  540. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  541. timestamp := currentDate.UnixNano() / 1e6
  542. timestampStr := fmt.Sprintf("%d", timestamp)
  543. val := TczSub(currentItem.Value, findItem.Value)
  544. if existVal, ok := existDataMap[av]; !ok {
  545. if _, existOk := existAddDataMap[av]; !existOk {
  546. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  547. isAdd = true
  548. // 实际数据的值
  549. if fromEdbInfo.LatestDate == av {
  550. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  551. if tmpErr != nil {
  552. return
  553. }
  554. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  555. }
  556. }
  557. existAddDataMap[av] = av
  558. } else {
  559. existValDecimal, tmpErr := decimal.NewFromString(existVal)
  560. if tmpErr != nil {
  561. err = tmpErr
  562. return
  563. }
  564. existStr := existValDecimal.String()
  565. if existStr != val {
  566. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  567. sql = fmt.Sprintf(sql, dataTableName)
  568. err = to.Exec(sql, val, edbInfoId, av).Error
  569. if err != nil {
  570. return
  571. }
  572. }
  573. }
  574. break
  575. } else {
  576. preDateDayStr := preDateDay.Format(utils.FormatDate)
  577. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  578. timestamp := currentDate.UnixNano() / 1e6
  579. timestampStr := fmt.Sprintf("%d", timestamp)
  580. val := TczSub(currentItem.Value, findItem.Value)
  581. if existVal, ok := existDataMap[av]; !ok {
  582. if _, existOk := existAddDataMap[av]; !existOk {
  583. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  584. isAdd = true
  585. // 实际数据的值
  586. if fromEdbInfo.LatestDate == av {
  587. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  588. if tmpErr != nil {
  589. return
  590. }
  591. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  592. }
  593. }
  594. existAddDataMap[av] = av
  595. } else {
  596. existValDecimal, tmpErr := decimal.NewFromString(existVal)
  597. if tmpErr != nil {
  598. err = tmpErr
  599. return
  600. }
  601. existStr := existValDecimal.String()
  602. if existStr != val {
  603. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  604. sql = fmt.Sprintf(sql, dataTableName)
  605. err = to.Exec(sql, val, edbInfoId, av).Error
  606. if err != nil {
  607. return
  608. }
  609. }
  610. }
  611. break
  612. }
  613. }
  614. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  615. preDateDay = preDateDay.AddDate(0, 0, -1)
  616. }
  617. }
  618. }
  619. }
  620. }
  621. if isAdd {
  622. addSql = strings.TrimRight(addSql, ",")
  623. err = to.Exec(addSql).Error
  624. if err != nil {
  625. return
  626. }
  627. }
  628. return
  629. }