predict_edb_data_calculate_tbz.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. package models
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "github.com/shopspring/decimal"
  7. "hongze/hongze_edb_lib/utils"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. // SavePredictCalculateTbz 新增/编辑 预测同比指标
  13. func SavePredictCalculateTbz(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("AddCalculateTbz,Err:" + err.Error())
  22. _ = to.Rollback()
  23. } else {
  24. _ = to.Commit()
  25. }
  26. }()
  27. if reqEdbInfoId <= 0 {
  28. edbInfo = &EdbInfo{
  29. //EdbInfoId: 0,
  30. EdbInfoType: 1,
  31. SourceName: "预测同比",
  32. Source: utils.DATA_SOURCE_PREDICT_CALCULATE_TBZ,
  33. EdbCode: edbCode,
  34. EdbName: edbName,
  35. EdbNameSource: edbName,
  36. Frequency: frequency,
  37. Unit: unit,
  38. //StartDate: "",
  39. //EndDate: "",
  40. ClassifyId: classifyId,
  41. SysUserId: sysUserId,
  42. SysUserRealName: sysUserRealName,
  43. UniqueCode: uniqueCode,
  44. CreateTime: time.Now(),
  45. ModifyTime: time.Now(),
  46. MinValue: 0,
  47. MaxValue: 0,
  48. CalculateFormula: formula,
  49. EdbType: 2,
  50. Sort: 0,
  51. MoveType: 0,
  52. MoveFrequency: "",
  53. NoUpdate: 0,
  54. ServerUrl: "",
  55. EdbNameEn: "",
  56. UnitEn: "",
  57. LatestDate: "",
  58. LatestValue: 0,
  59. ChartImage: "",
  60. }
  61. newEdbInfoId, tmpErr := to.Insert(edbInfo)
  62. if tmpErr != nil {
  63. err = tmpErr
  64. return
  65. }
  66. edbInfo.EdbInfoId = int(newEdbInfoId)
  67. // 添加关联关系
  68. {
  69. calculateMappingItem := &EdbInfoCalculateMapping{
  70. EdbInfoCalculateMappingId: 0,
  71. EdbInfoId: edbInfo.EdbInfoId,
  72. Source: edbInfo.Source,
  73. SourceName: edbInfo.SourceName,
  74. EdbCode: edbCode,
  75. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  76. FromEdbCode: fromEdbInfo.EdbCode,
  77. FromEdbName: fromEdbInfo.EdbName,
  78. FromSource: fromEdbInfo.Source,
  79. FromSourceName: fromEdbInfo.SourceName,
  80. FromTag: "",
  81. Sort: 1,
  82. CreateTime: time.Now(),
  83. ModifyTime: time.Now(),
  84. }
  85. _, err = to.Insert(calculateMappingItem)
  86. if err != nil {
  87. return
  88. }
  89. }
  90. } else {
  91. edbInfo, err = GetEdbInfoById(reqEdbInfoId)
  92. if err != nil {
  93. if err.Error() == utils.ErrNoRow() {
  94. errMsg = `获取指标信息失败`
  95. }
  96. return
  97. }
  98. if edbInfo.EdbInfoType != 1 {
  99. errMsg = `该指标不是预测指标`
  100. err = errors.New(errMsg)
  101. return
  102. }
  103. latestDateStr = edbInfo.LatestDate
  104. latestValue = edbInfo.LatestValue
  105. //修改指标信息
  106. edbInfo.EdbName = edbName
  107. edbInfo.EdbNameSource = edbName
  108. edbInfo.Frequency = frequency
  109. edbInfo.Unit = unit
  110. edbInfo.ClassifyId = classifyId
  111. edbInfo.ModifyTime = time.Now()
  112. _, err = to.Update(edbInfo, "EdbName", "EdbNameSource", "Frequency", "Unit", "ClassifyId", "ModifyTime")
  113. if err != nil {
  114. return
  115. }
  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. var count int
  122. count, err = GetEdbInfoCalculateCountByCondition(existCondition, existPars)
  123. if err != nil {
  124. err = errors.New("判断指标是否改变失败,Err:" + err.Error())
  125. return
  126. }
  127. if count > 0 { // 指标未被替换,无需重新计算
  128. return
  129. }
  130. //删除,计算指标关联的,基础指标的关联关系
  131. sql := ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? `
  132. _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
  133. if err != nil {
  134. return
  135. }
  136. // 删除该指标所有的同比数据
  137. dataTableName := GetEdbDataTableName(utils.DATA_SOURCE_PREDICT_CALCULATE_TBZ)
  138. deleteSql := ` DELETE FROM %s WHERE edb_info_id=? `
  139. deleteSql = fmt.Sprintf(deleteSql, dataTableName)
  140. _, err = to.Raw(deleteSql, edbInfo.EdbInfoId).Exec()
  141. if err != nil {
  142. return
  143. }
  144. }
  145. edbInfoIdStr := strconv.Itoa(edbInfo.EdbInfoId)
  146. // 获取源指标数据
  147. dataList, err := GetPredictEdbDataListAll(fromEdbInfo, 0)
  148. if err != nil {
  149. return
  150. }
  151. latestDateStr = fromEdbInfo.LatestDate
  152. // 计算数据
  153. var dateArr []string
  154. dataMap := make(map[string]*EdbInfoSearchData)
  155. for _, v := range dataList {
  156. dateArr = append(dateArr, v.DataTime)
  157. dataMap[v.DataTime] = v
  158. }
  159. addSql := ` INSERT INTO edb_data_predict_calculate_tbz(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  160. var isAdd bool
  161. existMap := make(map[string]string)
  162. for _, av := range dateArr {
  163. //fmt.Println(ak, av)
  164. currentItem := dataMap[av]
  165. if currentItem != nil {
  166. //当前日期
  167. currentDate, tmpErr := time.ParseInLocation(utils.FormatDate, av, time.Local)
  168. if tmpErr != nil {
  169. err = tmpErr
  170. return
  171. }
  172. //上一年的日期
  173. preDate := currentDate.AddDate(-1, 0, 0)
  174. preDateStr := preDate.Format(utils.FormatDate)
  175. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期找到
  176. //dataTime, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
  177. if _, ok := existMap[edbCode+av]; !ok {
  178. timestamp := currentDate.UnixNano() / 1e6
  179. timestampStr := fmt.Sprintf("%d", timestamp)
  180. val := TbzDiv(currentItem.Value, findItem.Value)
  181. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  182. isAdd = true
  183. utils.FileLog.Info("同期找到:" + av + ";" + preDateStr)
  184. // 实际数据的值
  185. if fromEdbInfo.LatestDate == av {
  186. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  187. if tmpErr != nil {
  188. return
  189. }
  190. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  191. }
  192. }
  193. existMap[edbCode+av] = av
  194. } else {
  195. if fromEdbInfo.Frequency == "月度" { //向上和向下,各找一个月
  196. nextDateDay := preDate
  197. preDateDay := preDate
  198. for i := 0; i <= 35; i++ {
  199. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  200. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  201. if _, ok := existMap[edbCode+av]; !ok {
  202. timestamp := currentDate.UnixNano() / 1e6
  203. timestampStr := fmt.Sprintf("%d", timestamp)
  204. val := TbzDiv(currentItem.Value, findItem.Value)
  205. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  206. isAdd = true
  207. // 实际数据的值
  208. if fromEdbInfo.LatestDate == av {
  209. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  210. if tmpErr != nil {
  211. return
  212. }
  213. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  214. }
  215. }
  216. existMap[edbCode+av] = av
  217. break
  218. } else {
  219. preDateDayStr := preDateDay.Format(utils.FormatDate)
  220. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  221. if _, ok := existMap[edbCode+av]; !ok {
  222. timestamp := currentDate.UnixNano() / 1e6
  223. timestampStr := fmt.Sprintf("%d", timestamp)
  224. val := TbzDiv(currentItem.Value, findItem.Value)
  225. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  226. isAdd = true
  227. // 实际数据的值
  228. if fromEdbInfo.LatestDate == av {
  229. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  230. if tmpErr != nil {
  231. return
  232. }
  233. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  234. }
  235. }
  236. existMap[edbCode+av] = av
  237. break
  238. }
  239. }
  240. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  241. preDateDay = preDateDay.AddDate(0, 0, -1)
  242. }
  243. } else if fromEdbInfo.Frequency == "季度" || fromEdbInfo.Frequency == "年度" {
  244. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期->下一个月找到
  245. if _, ok := existMap[edbCode+av]; !ok {
  246. timestamp := currentDate.UnixNano() / 1e6
  247. timestampStr := fmt.Sprintf("%d", timestamp)
  248. val := TbzDiv(currentItem.Value, findItem.Value)
  249. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  250. isAdd = true
  251. // 实际数据的值
  252. if fromEdbInfo.LatestDate == av {
  253. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  254. if tmpErr != nil {
  255. return
  256. }
  257. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  258. }
  259. }
  260. existMap[edbCode+av] = av
  261. break
  262. }
  263. } else {
  264. nextDateDay := preDate
  265. preDateDay := preDate
  266. for i := 0; i < 35; i++ {
  267. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  268. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  269. if _, ok := existMap[edbCode+av]; !ok {
  270. timestamp := currentDate.UnixNano() / 1e6
  271. timestampStr := fmt.Sprintf("%d", timestamp)
  272. val := TbzDiv(currentItem.Value, findItem.Value)
  273. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  274. isAdd = true
  275. // 实际数据的值
  276. if fromEdbInfo.LatestDate == av {
  277. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  278. if tmpErr != nil {
  279. return
  280. }
  281. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  282. }
  283. }
  284. existMap[edbCode+av] = av
  285. break
  286. } else {
  287. preDateDayStr := preDateDay.Format(utils.FormatDate)
  288. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  289. if _, ok := existMap[edbCode+av]; !ok {
  290. timestamp := currentDate.UnixNano() / 1e6
  291. timestampStr := fmt.Sprintf("%d", timestamp)
  292. val := TbzDiv(currentItem.Value, findItem.Value)
  293. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  294. isAdd = true
  295. // 实际数据的值
  296. if fromEdbInfo.LatestDate == av {
  297. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  298. if tmpErr != nil {
  299. return
  300. }
  301. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  302. }
  303. }
  304. existMap[edbCode+av] = av
  305. break
  306. } else {
  307. //fmt.Println("pre not find:", preDateStr, "i:", i)
  308. }
  309. }
  310. nextDateDay = nextDateDay.AddDate(0, 0, 1)
  311. preDateDay = preDateDay.AddDate(0, 0, -1)
  312. }
  313. }
  314. }
  315. }
  316. }
  317. if isAdd {
  318. addSql = strings.TrimRight(addSql, ",")
  319. _, err = to.Raw(addSql).Exec()
  320. if err != nil {
  321. return
  322. }
  323. }
  324. return
  325. }
  326. // RefreshAllPredictCalculateTbz 刷新所有同比值数据
  327. func RefreshAllPredictCalculateTbz(edbInfoId, source int, fromEdbInfo *EdbInfo, edbCode, startDate string) (latestDateStr string, latestValue float64, err error) {
  328. o := orm.NewOrm()
  329. to, err := o.Begin()
  330. if err != nil {
  331. return
  332. }
  333. defer func() {
  334. if err != nil {
  335. fmt.Println("RefreshAllPredictCalculateTbz,Err:" + err.Error())
  336. _ = to.Rollback()
  337. } else {
  338. _ = to.Commit()
  339. }
  340. }()
  341. if err != nil {
  342. return
  343. }
  344. edbInfoIdStr := strconv.Itoa(edbInfoId)
  345. // 获取关联指标数据
  346. dataList, err := GetPredictEdbDataListAllByStartDate(fromEdbInfo, 1, startDate)
  347. if err != nil {
  348. return
  349. }
  350. latestDateStr = fromEdbInfo.LatestDate
  351. //计算数据
  352. var dateArr []string
  353. dataMap := make(map[string]*EdbInfoSearchData)
  354. for _, v := range dataList {
  355. dateArr = append(dateArr, v.DataTime)
  356. dataMap[v.DataTime] = v
  357. }
  358. fmt.Println("source:", source)
  359. //获取指标所有数据
  360. existDataList := make([]*EdbData, 0)
  361. dataTableName := GetEdbDataTableName(source)
  362. fmt.Println("dataTableName:", dataTableName)
  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. fmt.Println("existDataMap:", existDataMap)
  374. addSql := ` INSERT INTO edb_data_predict_calculate_tbz (edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  375. var isAdd bool
  376. existAddDataMap := make(map[string]string)
  377. for _, av := range dateArr {
  378. currentItem := dataMap[av]
  379. if currentItem != nil {
  380. //当前日期
  381. currentDate, tmpErr := time.ParseInLocation(utils.FormatDate, av, time.Local)
  382. if tmpErr != nil {
  383. err = tmpErr
  384. return
  385. }
  386. //上一年的日期
  387. preDate := currentDate.AddDate(-1, 0, 0)
  388. preDateStr := preDate.Format(utils.FormatDate)
  389. if findItem, ok := dataMap[preDateStr]; ok { //上一年同期找到
  390. //dataTime, _ := time.ParseInLocation(utils.FormatDate, date, time.Local)
  391. timestamp := currentDate.UnixNano() / 1e6
  392. timestampStr := fmt.Sprintf("%d", timestamp)
  393. val := TbzDiv(currentItem.Value, findItem.Value)
  394. if existVal, ok := existDataMap[av]; !ok {
  395. if _, existOk := existAddDataMap[av]; !existOk {
  396. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  397. isAdd = true
  398. // 实际数据的值
  399. if fromEdbInfo.LatestDate == av {
  400. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  401. if tmpErr != nil {
  402. return
  403. }
  404. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  405. }
  406. }
  407. existAddDataMap[av] = av
  408. } else {
  409. existValDecimal, tmpErr := decimal.NewFromString(existVal)
  410. if tmpErr != nil {
  411. err = tmpErr
  412. return
  413. }
  414. existStr := existValDecimal.String()
  415. if existStr != val {
  416. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  417. sql = fmt.Sprintf(sql, dataTableName)
  418. _, err = o.Raw(sql, val, edbInfoId, av).Exec()
  419. if err != nil {
  420. return
  421. }
  422. }
  423. }
  424. continue
  425. } else {
  426. if fromEdbInfo.Frequency == "月度" { //向上和向下,各找一个月
  427. for i := 0; i <= 35; i++ {
  428. nextDateDay := preDate.AddDate(0, 0, i)
  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 := TbzDiv(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, -i)
  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 := TbzDiv(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 := TbzDiv(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. nextDateDay := preDate.AddDate(0, 0, 1)
  545. nextDateDayStr := nextDateDay.Format(utils.FormatDate)
  546. preDateDay := preDate.AddDate(0, 0, -1)
  547. preDateDayStr := preDateDay.Format(utils.FormatDate)
  548. for i := 0; i < 35; i++ {
  549. if i >= 1 {
  550. nextDateDay = nextDateDay.AddDate(0, 0, i)
  551. nextDateDayStr = nextDateDay.Format(utils.FormatDate)
  552. }
  553. if findItem, ok := dataMap[nextDateDayStr]; ok { //上一年同期->下一个月找到
  554. timestamp := currentDate.UnixNano() / 1e6
  555. timestampStr := fmt.Sprintf("%d", timestamp)
  556. val := TbzDiv(currentItem.Value, findItem.Value)
  557. if existVal, ok := existDataMap[av]; !ok {
  558. if _, existOk := existAddDataMap[av]; !existOk {
  559. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  560. isAdd = true
  561. // 实际数据的值
  562. if fromEdbInfo.LatestDate == av {
  563. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  564. if tmpErr != nil {
  565. return
  566. }
  567. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  568. }
  569. }
  570. existAddDataMap[av] = av
  571. } else {
  572. existValDecimal, tmpErr := decimal.NewFromString(existVal)
  573. if tmpErr != nil {
  574. err = tmpErr
  575. return
  576. }
  577. existStr := existValDecimal.String()
  578. if existStr != val {
  579. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  580. sql = fmt.Sprintf(sql, dataTableName)
  581. _, err = o.Raw(sql, val, edbInfoId, av).Exec()
  582. if err != nil {
  583. return
  584. }
  585. }
  586. }
  587. break
  588. } else {
  589. if i >= 1 {
  590. preDateDay = preDateDay.AddDate(0, 0, -i)
  591. preDateDayStr = preDateDay.Format(utils.FormatDate)
  592. }
  593. if findItem, ok := dataMap[preDateDayStr]; ok { //上一年同期->上一个月找到
  594. timestamp := currentDate.UnixNano() / 1e6
  595. timestampStr := fmt.Sprintf("%d", timestamp)
  596. val := TbzDiv(currentItem.Value, findItem.Value)
  597. if existVal, ok := existDataMap[av]; !ok {
  598. if _, existOk := existAddDataMap[av]; !existOk {
  599. addSql += GetAddSql(edbInfoIdStr, edbCode, av, timestampStr, val)
  600. isAdd = true
  601. // 实际数据的值
  602. if fromEdbInfo.LatestDate == av {
  603. latestValueDecimal, tmpErr := decimal.NewFromString(val)
  604. if tmpErr != nil {
  605. return
  606. }
  607. latestValue, _ = latestValueDecimal.Truncate(4).Float64() //保留4位小数
  608. }
  609. }
  610. existAddDataMap[av] = av
  611. } else {
  612. existValDecimal, tmpErr := decimal.NewFromString(existVal)
  613. if tmpErr != nil {
  614. err = tmpErr
  615. return
  616. }
  617. existStr := existValDecimal.String()
  618. if existStr != val {
  619. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  620. sql = fmt.Sprintf(sql, dataTableName)
  621. _, err = o.Raw(sql, val, edbInfoId, av).Exec()
  622. if err != nil {
  623. return
  624. }
  625. }
  626. }
  627. break
  628. }
  629. }
  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. }