predict_edb_data_calculate_tcz.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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. for _, v := range existDataList {
  377. existDataMap[v.DataTime] = v.Value
  378. }
  379. addSql := ` INSERT INTO edb_data_predict_calculate_tcz(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  380. var isAdd bool
  381. existAddDataMap := make(map[string]string)
  382. for _, av := range dateArr {
  383. currentItem := dataMap[av]
  384. if currentItem != nil {
  385. //当前日期
  386. currentDate, tmpErr := time.ParseInLocation(utils.FormatDate, av, time.Local)
  387. if tmpErr != nil {
  388. err = tmpErr
  389. return
  390. }
  391. //上一年的日期
  392. preDate := currentDate.AddDate(-1, 0, 0)
  393. preDateStr := preDate.Format(utils.FormatDate)
  394. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期找到
  395. //dataTime, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
  396. timestamp := currentDate.UnixNano() / 1e6
  397. timestampStr := fmt.Sprintf("%d", timestamp)
  398. val := TczSub(currentItem.Value, findItem.Value)
  399. if existVal, ok := existDataMap[av]; !ok {
  400. if _, existOk := existAddDataMap[av]; !existOk {
  401. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  402. isAdd = true
  403. // 实际数据的值
  404. if fromEdbInfo.LatestDate == av {
  405. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  406. if tmpErr != nil {
  407. return
  408. }
  409. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  410. }
  411. }
  412. existAddDataMap[av] = av
  413. } else {
  414. existValDecimal, tmpErr := decimal.NewFromString(existVal)
  415. if tmpErr != nil {
  416. err = tmpErr
  417. return
  418. }
  419. existStr := existValDecimal.String()
  420. if existStr != val {
  421. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  422. sql = fmt.Sprintf(sql, dataTableName)
  423. _, err = o.Raw(sql, val, edbInfoId, av).Exec()
  424. if err != nil {
  425. return
  426. }
  427. }
  428. }
  429. utils.FileLog.Info("同期找到:" + av + ";" + preDateStr)
  430. continue
  431. } else {
  432. if fromEdbInfo.Frequency == "月度" { //向上和向下,各找一个月
  433. for i := 0; i <= 35; i++ {
  434. nextDateDay := preDate.AddDate(0, 0, 1)
  435. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  436. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  437. timestamp := currentDate.UnixNano() / 1e6
  438. timestampStr := fmt.Sprintf("%d", timestamp)
  439. val := TczSub(currentItem.Value, findItem.Value)
  440. if existVal, ok := existDataMap[av]; !ok {
  441. if _, existOk := existAddDataMap[av]; !existOk {
  442. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  443. isAdd = true
  444. // 实际数据的值
  445. if fromEdbInfo.LatestDate == av {
  446. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  447. if tmpErr != nil {
  448. return
  449. }
  450. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  451. }
  452. }
  453. existAddDataMap[av] = av
  454. } else {
  455. existValDecimal, tmpErr := decimal.NewFromString(existVal)
  456. if tmpErr != nil {
  457. err = tmpErr
  458. return
  459. }
  460. existStr := existValDecimal.String()
  461. if existStr != val {
  462. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  463. sql = fmt.Sprintf(sql, dataTableName)
  464. _, err = o.Raw(sql, val, edbInfoId, av).Exec()
  465. if err != nil {
  466. return
  467. }
  468. }
  469. }
  470. break
  471. } else {
  472. preDateDay := preDate.AddDate(0, 0, -1)
  473. preDateDayStr := preDateDay.Format(utils.FormatDate)
  474. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  475. timestamp := currentDate.UnixNano() / 1e6
  476. timestampStr := fmt.Sprintf("%d", timestamp)
  477. val := TczSub(currentItem.Value, findItem.Value)
  478. if existVal, ok := existDataMap[av]; !ok {
  479. if _, existOk := existAddDataMap[av]; !existOk {
  480. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  481. isAdd = true
  482. // 实际数据的值
  483. if fromEdbInfo.LatestDate == av {
  484. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  485. if tmpErr != nil {
  486. return
  487. }
  488. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  489. }
  490. }
  491. existAddDataMap[av] = av
  492. } else {
  493. existValDecimal, tmpErr := decimal.NewFromString(existVal)
  494. if tmpErr != nil {
  495. err = tmpErr
  496. return
  497. }
  498. existStr := existValDecimal.String()
  499. if existStr != val {
  500. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  501. sql = fmt.Sprintf(sql, dataTableName)
  502. _, err = o.Raw(sql, val, edbInfoId, av).Exec()
  503. if err != nil {
  504. return
  505. }
  506. }
  507. }
  508. break
  509. }
  510. }
  511. }
  512. } else if fromEdbInfo.Frequency == "季度" || fromEdbInfo.Frequency == "年度" {
  513. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期->下一个月找到
  514. timestamp := currentDate.UnixNano() / 1e6
  515. timestampStr := fmt.Sprintf("%d", timestamp)
  516. val := TczSub(currentItem.Value, findItem.Value)
  517. if existVal, ok := existDataMap[av]; !ok {
  518. if _, existOk := existAddDataMap[av]; !existOk {
  519. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  520. isAdd = true
  521. // 实际数据的值
  522. if fromEdbInfo.LatestDate == av {
  523. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  524. if tmpErr != nil {
  525. return
  526. }
  527. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  528. }
  529. }
  530. existAddDataMap[av] = av
  531. } else {
  532. existValDecimal, tmpErr := decimal.NewFromString(existVal)
  533. if tmpErr != nil {
  534. err = tmpErr
  535. return
  536. }
  537. existStr := existValDecimal.String()
  538. if existStr != val {
  539. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  540. sql = fmt.Sprintf(sql, dataTableName)
  541. _, err = o.Raw(sql, val, edbInfoId, av).Exec()
  542. if err != nil {
  543. return
  544. }
  545. }
  546. }
  547. break
  548. }
  549. } else {
  550. nextDateDay := preDate
  551. preDateDay := preDate
  552. for i := 0; i < 35; i++ {
  553. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  554. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  555. timestamp := currentDate.UnixNano() / 1e6
  556. timestampStr := fmt.Sprintf("%d", timestamp)
  557. val := TczSub(currentItem.Value, findItem.Value)
  558. if existVal, ok := existDataMap[av]; !ok {
  559. if _, existOk := existAddDataMap[av]; !existOk {
  560. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  561. isAdd = true
  562. // 实际数据的值
  563. if fromEdbInfo.LatestDate == av {
  564. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  565. if tmpErr != nil {
  566. return
  567. }
  568. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  569. }
  570. }
  571. existAddDataMap[av] = av
  572. } else {
  573. existValDecimal, tmpErr := decimal.NewFromString(existVal)
  574. if tmpErr != nil {
  575. err = tmpErr
  576. return
  577. }
  578. existStr := existValDecimal.String()
  579. if existStr != val {
  580. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  581. sql = fmt.Sprintf(sql, dataTableName)
  582. _, err = o.Raw(sql, val, edbInfoId, av).Exec()
  583. if err != nil {
  584. return
  585. }
  586. }
  587. }
  588. break
  589. } else {
  590. preDateDayStr := preDateDay.Format(utils.FormatDate)
  591. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  592. timestamp := currentDate.UnixNano() / 1e6
  593. timestampStr := fmt.Sprintf("%d", timestamp)
  594. val := TczSub(currentItem.Value, findItem.Value)
  595. if existVal, ok := existDataMap[av]; !ok {
  596. if _, existOk := existAddDataMap[av]; !existOk {
  597. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  598. isAdd = true
  599. // 实际数据的值
  600. if fromEdbInfo.LatestDate == av {
  601. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  602. if tmpErr != nil {
  603. return
  604. }
  605. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  606. }
  607. }
  608. existAddDataMap[av] = av
  609. } else {
  610. existValDecimal, tmpErr := decimal.NewFromString(existVal)
  611. if tmpErr != nil {
  612. err = tmpErr
  613. return
  614. }
  615. existStr := existValDecimal.String()
  616. if existStr != val {
  617. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  618. sql = fmt.Sprintf(sql, dataTableName)
  619. _, err = o.Raw(sql, val, edbInfoId, av).Exec()
  620. if err != nil {
  621. return
  622. }
  623. }
  624. }
  625. break
  626. }
  627. }
  628. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  629. preDateDay = preDateDay.AddDate(0, 0, -1)
  630. }
  631. }
  632. }
  633. }
  634. }
  635. if isAdd {
  636. addSql = strings.TrimRight(addSql, ",")
  637. _, err = o.Raw(addSql).Exec()
  638. if err != nil {
  639. return
  640. }
  641. }
  642. return
  643. }