predict_edb_data_calculate_tbz.go 22 KB

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