edb_data_calculate_ljz.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. package models
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_index_lib/utils"
  6. "fmt"
  7. "github.com/beego/beego/v2/client/orm"
  8. "github.com/shopspring/decimal"
  9. "reflect"
  10. "strconv"
  11. "strings"
  12. "time"
  13. )
  14. // Ljz 累计值
  15. type Ljz struct {
  16. }
  17. // Add 添加
  18. func (obj Ljz) Add(params AddCalculateBatchParams) (edbInfo *EdbInfo, err error, errMsg string) {
  19. req := params.Req
  20. fromEdbInfo := params.FromEdbInfo
  21. edbCode := params.EdbCode
  22. frequencyInt := utils.CheckFrequency(fromEdbInfo.Frequency, req.Frequency)
  23. if frequencyInt < 0 {
  24. errMsg = "频度异常,不允许低频转为高频"
  25. err = errors.New(errMsg)
  26. return
  27. }
  28. if frequencyInt == 0 {
  29. errMsg = "频度异常,不允许同频计算累计值"
  30. err = errors.New(errMsg)
  31. return
  32. }
  33. o := orm.NewOrm()
  34. to, err := o.Begin()
  35. if err != nil {
  36. return
  37. }
  38. defer func() {
  39. if err != nil {
  40. _ = to.Rollback()
  41. fmt.Println(reflect.TypeOf(obj).Name(), ";Add,Err:"+err.Error())
  42. } else {
  43. _ = to.Commit()
  44. }
  45. }()
  46. edbInfo = &EdbInfo{
  47. //EdbInfoId: 0,
  48. SourceName: obj.GetSourceName(),
  49. Source: obj.GetSource(),
  50. EdbCode: edbCode,
  51. EdbName: req.EdbName,
  52. EdbNameSource: req.EdbName,
  53. Frequency: req.Frequency,
  54. Unit: req.Unit,
  55. StartDate: "",
  56. EndDate: "",
  57. ClassifyId: req.ClassifyId,
  58. SysUserId: params.SysUserId,
  59. SysUserRealName: params.SysUserRealName,
  60. UniqueCode: params.UniqueCode,
  61. CreateTime: time.Now(),
  62. ModifyTime: time.Now(),
  63. MinValue: 0,
  64. MaxValue: 0,
  65. CalculateFormula: req.Formula,
  66. EdbType: obj.GetEdbType(),
  67. Sort: 0,
  68. MoveType: 0,
  69. MoveFrequency: "",
  70. NoUpdate: 0,
  71. ServerUrl: "",
  72. EdbInfoType: 0,
  73. EdbNameEn: req.EdbName,
  74. UnitEn: req.Unit,
  75. LatestDate: "",
  76. LatestValue: 0,
  77. ChartImage: "",
  78. Calendar: "",
  79. Extra: req.Extra,
  80. }
  81. newEdbInfoId, tmpErr := to.Insert(edbInfo)
  82. if tmpErr != nil {
  83. err = tmpErr
  84. return
  85. }
  86. edbInfo.EdbInfoId = int(newEdbInfoId)
  87. //关联关系
  88. {
  89. calculateMappingItem := new(EdbInfoCalculateMapping)
  90. calculateMappingItem.CreateTime = time.Now()
  91. calculateMappingItem.ModifyTime = time.Now()
  92. calculateMappingItem.Sort = 1
  93. calculateMappingItem.EdbCode = edbCode
  94. calculateMappingItem.EdbInfoId = edbInfo.EdbInfoId
  95. calculateMappingItem.FromEdbInfoId = fromEdbInfo.EdbInfoId
  96. calculateMappingItem.FromEdbCode = fromEdbInfo.EdbCode
  97. calculateMappingItem.FromEdbName = fromEdbInfo.EdbName
  98. calculateMappingItem.FromSource = fromEdbInfo.Source
  99. calculateMappingItem.FromSourceName = fromEdbInfo.SourceName
  100. calculateMappingItem.FromTag = ""
  101. calculateMappingItem.Source = edbInfo.Source
  102. calculateMappingItem.SourceName = edbInfo.SourceName
  103. calculateMappingItem.FromSubSource = fromEdbInfo.SubSource
  104. _, err = to.Insert(calculateMappingItem)
  105. if err != nil {
  106. return
  107. }
  108. }
  109. //计算数据
  110. err = obj.refresh(to, edbInfo.EdbInfoId, edbInfo.Source, edbInfo.SubSource, edbInfo, fromEdbInfo, edbInfo.EdbCode, "", edbInfo.Extra)
  111. return
  112. }
  113. // Edit 编辑
  114. func (obj Ljz) Edit(params EditCalculateBatchParams) (err error, errMsg string) {
  115. req := params.Req
  116. edbInfo := params.EdbInfo
  117. fromEdbInfo := params.FromEdbInfo
  118. frequencyInt := utils.CheckFrequency(fromEdbInfo.Frequency, req.Frequency)
  119. if frequencyInt < 0 {
  120. errMsg = "频度异常,不允许低频转为高频"
  121. err = errors.New(errMsg)
  122. return
  123. }
  124. if frequencyInt == 0 {
  125. errMsg = "频度异常,不允许同频计算累计值"
  126. err = errors.New(errMsg)
  127. return
  128. }
  129. o := orm.NewOrm()
  130. to, err := o.Begin()
  131. if err != nil {
  132. return
  133. }
  134. defer func() {
  135. if err != nil {
  136. _ = to.Rollback()
  137. fmt.Println(reflect.TypeOf(obj).Name(), ";Edit,Err:"+err.Error())
  138. } else {
  139. _ = to.Commit()
  140. }
  141. }()
  142. tableName := GetEdbDataTableName(edbInfo.Source, edbInfo.SubSource)
  143. var isRecalculate bool
  144. if edbInfo.Frequency != req.Frequency || edbInfo.Extra != req.Extra {
  145. isRecalculate = true
  146. }
  147. //修改指标信息
  148. edbInfo.EdbName = req.EdbName
  149. edbInfo.EdbNameSource = req.EdbName
  150. edbInfo.Frequency = req.Frequency
  151. edbInfo.Unit = req.Unit
  152. edbInfo.ClassifyId = req.ClassifyId
  153. edbInfo.Extra = req.Extra
  154. edbInfo.EdbNameEn = req.EdbNameEn
  155. edbInfo.UnitEn = req.UnitEn
  156. edbInfo.ModifyTime = time.Now()
  157. _, err = to.Update(edbInfo, "EdbName", "EdbNameSource", "Frequency", "Unit", "ClassifyId", "ModifyTime", "Extra", "EdbNameEn", "UnitEn")
  158. if err != nil {
  159. return
  160. }
  161. var existCondition string
  162. var existPars []interface{}
  163. existCondition += " AND edb_info_id=? AND from_edb_info_id=? "
  164. existPars = append(existPars, edbInfo.EdbInfoId, req.FromEdbInfoId)
  165. //判断计算指标是否被更换
  166. count, err := GetEdbInfoCalculateCountByCondition(existCondition, existPars)
  167. if err != nil {
  168. err = errors.New("判断指标是否改变失败,Err:" + err.Error())
  169. return
  170. }
  171. if count > 0 { // 指标未被替换,无需删除关联数据
  172. // todo 频度被换了,需要重新计算
  173. if isRecalculate {
  174. err = obj.refresh(to, edbInfo.EdbInfoId, edbInfo.Source, edbInfo.SubSource, edbInfo, fromEdbInfo, edbInfo.EdbCode, "", req.Extra)
  175. }
  176. return
  177. }
  178. //删除,计算指标关联的,基础指标的关联关系
  179. sql := ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? `
  180. _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
  181. if err != nil {
  182. return
  183. }
  184. //清空原有数据
  185. sql = ` DELETE FROM ` + tableName + ` WHERE edb_info_id = ? `
  186. _, err = to.Raw(sql, edbInfo.EdbInfoId).Exec()
  187. if err != nil {
  188. return
  189. }
  190. //关联关系
  191. {
  192. calculateMappingItem := &EdbInfoCalculateMapping{
  193. EdbInfoCalculateMappingId: 0,
  194. EdbInfoId: edbInfo.EdbInfoId,
  195. Source: obj.GetSource(),
  196. SourceName: obj.GetSourceName(),
  197. EdbCode: edbInfo.EdbCode,
  198. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  199. FromEdbCode: fromEdbInfo.EdbCode,
  200. FromEdbName: fromEdbInfo.EdbName,
  201. FromSource: fromEdbInfo.Source,
  202. FromSourceName: fromEdbInfo.SourceName,
  203. FromTag: "",
  204. Sort: 1,
  205. CreateTime: time.Now(),
  206. ModifyTime: time.Now(),
  207. FromSubSource: fromEdbInfo.SubSource,
  208. }
  209. _, err = to.Insert(calculateMappingItem)
  210. if err != nil {
  211. return
  212. }
  213. }
  214. //计算数据
  215. err = obj.refresh(to, edbInfo.EdbInfoId, edbInfo.Source, edbInfo.SubSource, edbInfo, fromEdbInfo, edbInfo.EdbCode, "", edbInfo.Extra)
  216. return
  217. }
  218. // Refresh 刷新
  219. func (obj Ljz) Refresh(params RefreshParams) (err error, errMsg string) {
  220. calculateMapping, err := GetEdbInfoCalculateMappingDetail(params.EdbInfo.EdbInfoId)
  221. if err != nil {
  222. errMsg = "GetEdbInfoCalculateLjzDetail Err:" + err.Error()
  223. return
  224. }
  225. fromEdbInfo, err := GetEdbInfoById(calculateMapping.FromEdbInfoId)
  226. if err != nil {
  227. errMsg = "GetEdbInfoById Err:" + err.Error()
  228. return
  229. }
  230. o := orm.NewOrm()
  231. to, err := o.Begin()
  232. if err != nil {
  233. return
  234. }
  235. defer func() {
  236. if err != nil {
  237. _ = to.Rollback()
  238. fmt.Println(reflect.TypeOf(obj).Name(), ";Refresh,Err:"+err.Error())
  239. } else {
  240. _ = to.Commit()
  241. }
  242. }()
  243. // 计算数据
  244. err = obj.refresh(to, params.EdbInfo.EdbInfoId, params.EdbInfo.Source, params.EdbInfo.SubSource, params.EdbInfo, fromEdbInfo, params.EdbInfo.EdbCode, params.StartDate, params.EdbInfo.Extra)
  245. return
  246. }
  247. // GetSource 获取来源编码id
  248. func (obj Ljz) GetSource() int {
  249. return utils.DATA_SOURCE_CALCULATE_LJZ
  250. }
  251. // GetSourceName 获取来源名称
  252. func (obj Ljz) GetSourceName() string {
  253. return utils.DATA_SOURCE_NAME_CALCULATE_LJZ
  254. }
  255. // GetEdbType 获取指标类型
  256. func (obj Ljz) GetEdbType() int {
  257. return utils.CALCULATE_EDB_TYPE
  258. }
  259. func (obj Ljz) refresh(to orm.TxOrmer, edbInfoId, source, subSource int, edbInfo, fromEdbInfo *EdbInfo, edbCode, startDate string, extra string) (err error) {
  260. dataTableName := GetEdbDataTableName(source, subSource)
  261. edbInfoIdStr := strconv.Itoa(edbInfoId)
  262. //计算数据
  263. var isWeekData bool // 是否周度数据,如果是周度数据的话,是需要变频的,最后结果还需要除以7
  264. // 周度数据需要先变成日度的
  265. if fromEdbInfo.Frequency == `周度` {
  266. isWeekData = true
  267. }
  268. dataList, err := GetEdbDataListAllByTo(to, fromEdbInfo.Source, fromEdbInfo.SubSource, FindEdbDataListAllCond{
  269. EdbInfoId: fromEdbInfo.EdbInfoId,
  270. }, 1)
  271. if err != nil {
  272. return err
  273. }
  274. fromEdbDataMap := make(map[string]float64)
  275. if isWeekData {
  276. dataList, err = HandleDataByLinearRegression(dataList, fromEdbDataMap)
  277. if err != nil {
  278. return
  279. }
  280. }
  281. var lastValType int
  282. if extra != "" {
  283. var lastValConfig CalculateLjzEdbExtra
  284. err = json.Unmarshal([]byte(extra), &lastValConfig)
  285. if err != nil {
  286. err = fmt.Errorf("refreshAllCalculate,extra解析失败,Err:%s", err.Error())
  287. return
  288. }
  289. lastValType = lastValConfig.LastValType
  290. }
  291. //日度转周度:日期选周五,计算上周六到本周五的日度值的加总,最新日期为最新值对应的周五。
  292. //日度转旬度:日期选每个旬的最后一天,计算当旬所有日度值的加总,最新日期为最新值对应当旬的最后一天。
  293. //日度转月度:日期选每个月最后一天,计算当月所有日度值的加总,最新日期为最新值对应当月最后一天。
  294. //日度转季度、年度:方法类似转月度。
  295. //周度转月度/季度/年度:将周度值转成日度,空值用插值法插值,计算当月/当季/当年所有值的加总,然后除以7。
  296. //月度转季度/年度: 当季/当年月度值相加。
  297. dateList := make([]time.Time, 0)
  298. valueMap := make(map[time.Time]float64)
  299. switch edbInfo.Frequency {
  300. case "年度":
  301. yearMap := make(map[int]float64)
  302. yearList := make([]int, 0)
  303. var lastNewDate time.Time
  304. for _, item := range dataList {
  305. itemDate, tmpErr := time.ParseInLocation(utils.FormatDate, item.DataTime, time.Local)
  306. if tmpErr != nil {
  307. err = tmpErr
  308. return
  309. }
  310. year := itemDate.Year()
  311. yearVal, ok := yearMap[year]
  312. if ok {
  313. yearMap[year] = item.Value + yearVal
  314. } else {
  315. yearList = append(yearList, year)
  316. yearMap[year] = item.Value
  317. }
  318. }
  319. for _, v := range yearList {
  320. currTime := time.Date(v, 12, 31, 0, 0, 0, 0, time.Local)
  321. dateList = append(dateList, currTime)
  322. valueMap[currTime] = yearMap[v]
  323. lastNewDate = currTime
  324. }
  325. // 根据配置处理最新值, 1 表示均值填充
  326. if lastValType == 1 {
  327. lastItem := dataList[len(dataList)-1]
  328. // 最后一天的累计值
  329. initVal := valueMap[lastNewDate]
  330. itemDate, tmpErr := time.ParseInLocation(utils.FormatDate, lastItem.DataTime, time.Local)
  331. if tmpErr != nil {
  332. err = tmpErr
  333. return
  334. }
  335. //获取当季的总天数, 当季最后一天减去当季第一天
  336. // 获取当年的总天数
  337. startDateT := time.Date(itemDate.Year(), 1, 1, 0, 0, 0, 0, time.Local)
  338. allDays := int(lastNewDate.Sub(startDateT).Hours() / 24)
  339. //获取距离当年第一天的天数
  340. days := int(itemDate.Sub(startDateT).Hours() / 24)
  341. daysT := decimal.NewFromInt(int64(days))
  342. initValAndMonthT := decimal.NewFromFloat(initVal * float64(allDays))
  343. val, _ := initValAndMonthT.Div(daysT).RoundCeil(4).Float64()
  344. fmt.Printf("最新值 计算公式:%d*%f/(%d) = %f\n", allDays, initVal, days, val)
  345. valueMap[lastNewDate] = val
  346. }
  347. case "半年度":
  348. yearMonthMap := make(map[string]float64)
  349. yearMonthList := make([]string, 0)
  350. var lastNewDate time.Time
  351. for _, item := range dataList {
  352. itemDate, tmpErr := time.ParseInLocation(utils.FormatDate, item.DataTime, time.Local)
  353. if tmpErr != nil {
  354. err = tmpErr
  355. return
  356. }
  357. year := itemDate.Year()
  358. var tmpK string
  359. if itemDate.Month() <= 6 {
  360. tmpK = fmt.Sprint(year, "06")
  361. } else {
  362. tmpK = fmt.Sprint(year, "12")
  363. }
  364. yearVal, ok := yearMonthMap[tmpK]
  365. if ok {
  366. yearMonthMap[tmpK] = item.Value + yearVal
  367. } else {
  368. yearMonthList = append(yearMonthList, tmpK)
  369. yearMonthMap[tmpK] = item.Value
  370. }
  371. }
  372. for _, v := range yearMonthList {
  373. currTime, tmpErr := time.ParseInLocation(utils.FormatYearMonthUnSpace, v, time.Local)
  374. if tmpErr != nil {
  375. err = tmpErr
  376. return
  377. }
  378. currTime = currTime.AddDate(0, 1, -1)
  379. dateList = append(dateList, currTime)
  380. valueMap[currTime] = yearMonthMap[v]
  381. lastNewDate = currTime
  382. }
  383. // 根据配置处理最新值, 1 表示均值填充
  384. if lastValType == 1 {
  385. lastItem := dataList[len(dataList)-1]
  386. // 最后一天的累计值
  387. initVal := valueMap[lastNewDate]
  388. itemDate, tmpErr := time.ParseInLocation(utils.FormatDate, lastItem.DataTime, time.Local)
  389. if tmpErr != nil {
  390. err = tmpErr
  391. return
  392. }
  393. //获取上半年或者下半年的总天数, 半年度最后一天减去半年度第一天
  394. startDateT := lastNewDate.AddDate(0, -6, 0)
  395. allDays := int(lastNewDate.Sub(startDateT).Hours() / 24)
  396. //获取距离半年度第一天的天数
  397. days := int(itemDate.Sub(startDateT).Hours() / 24)
  398. daysT := decimal.NewFromInt(int64(days))
  399. initValAndMonthT := decimal.NewFromFloat(initVal * float64(allDays))
  400. val, _ := initValAndMonthT.Div(daysT).RoundCeil(4).Float64()
  401. fmt.Printf("最新值 计算公式:%d*%f/(%d) = %f\n", allDays, initVal, days, val)
  402. valueMap[lastNewDate] = val
  403. }
  404. case "季度":
  405. yearMonthMap := make(map[string]float64)
  406. yearMonthList := make([]string, 0)
  407. var lastNewDate time.Time
  408. for _, item := range dataList {
  409. itemDate, tmpErr := time.ParseInLocation(utils.FormatDate, item.DataTime, time.Local)
  410. if tmpErr != nil {
  411. err = tmpErr
  412. return
  413. }
  414. year := itemDate.Year()
  415. var tmpK string
  416. if itemDate.Month() <= 3 {
  417. tmpK = fmt.Sprint(year, "03")
  418. } else if itemDate.Month() <= 6 {
  419. tmpK = fmt.Sprint(year, "06")
  420. } else if itemDate.Month() <= 9 {
  421. tmpK = fmt.Sprint(year, "09")
  422. } else {
  423. tmpK = fmt.Sprint(year, "12")
  424. }
  425. yearVal, ok := yearMonthMap[tmpK]
  426. if ok {
  427. yearMonthMap[tmpK] = item.Value + yearVal
  428. } else {
  429. yearMonthList = append(yearMonthList, tmpK)
  430. yearMonthMap[tmpK] = item.Value
  431. }
  432. }
  433. for _, v := range yearMonthList {
  434. currTime, tmpErr := time.ParseInLocation(utils.FormatYearMonthUnSpace, v, time.Local)
  435. if tmpErr != nil {
  436. err = tmpErr
  437. return
  438. }
  439. currTime = currTime.AddDate(0, 1, -1)
  440. dateList = append(dateList, currTime)
  441. valueMap[currTime] = yearMonthMap[v]
  442. lastNewDate = currTime
  443. }
  444. // 根据配置处理最新值, 1 表示均值填充
  445. if lastValType == 1 {
  446. lastItem := dataList[len(dataList)-1]
  447. // 最后一天的累计值
  448. initVal := valueMap[lastNewDate]
  449. itemDate, tmpErr := time.ParseInLocation(utils.FormatDate, lastItem.DataTime, time.Local)
  450. if tmpErr != nil {
  451. err = tmpErr
  452. return
  453. }
  454. //获取当季的总天数, 当季最后一天减去当季第一天
  455. startDateT := lastNewDate.AddDate(0, -3, 0)
  456. allDays := int(lastNewDate.Sub(startDateT).Hours() / 24)
  457. //获取距离当季第一天的天数
  458. days := int(itemDate.Sub(startDateT).Hours() / 24)
  459. daysT := decimal.NewFromInt(int64(days))
  460. initValAndMonthT := decimal.NewFromFloat(initVal * float64(allDays))
  461. val, _ := initValAndMonthT.Div(daysT).RoundCeil(4).Float64()
  462. fmt.Printf("最新值 计算公式:%d*%f/(%d) = %f\n", allDays, initVal, days, val)
  463. valueMap[lastNewDate] = val
  464. }
  465. case "月度":
  466. yearMonthMap := make(map[string]float64)
  467. yearMonthList := make([]string, 0)
  468. var lastNewDate time.Time
  469. for _, item := range dataList {
  470. itemDate, tmpErr := time.ParseInLocation(utils.FormatDate, item.DataTime, time.Local)
  471. if tmpErr != nil {
  472. err = tmpErr
  473. return
  474. }
  475. year := itemDate.Year()
  476. var tmpK string
  477. tmpK = fmt.Sprint(year*100 + int(itemDate.Month()))
  478. yearVal, ok := yearMonthMap[tmpK]
  479. if ok {
  480. yearMonthMap[tmpK] = item.Value + yearVal
  481. fmt.Printf("%s 累加值:%f = %.6f + %.6f\n", tmpK, yearMonthMap[tmpK], item.Value, yearVal)
  482. } else {
  483. yearMonthList = append(yearMonthList, tmpK)
  484. yearMonthMap[tmpK] = item.Value
  485. }
  486. }
  487. for _, v := range yearMonthList {
  488. currTime, tmpErr := time.ParseInLocation(utils.FormatYearMonthUnSpace, v, time.Local)
  489. if tmpErr != nil {
  490. err = tmpErr
  491. return
  492. }
  493. currTime = currTime.AddDate(0, 1, -1)
  494. dateList = append(dateList, currTime)
  495. valueMap[currTime] = yearMonthMap[v]
  496. lastNewDate = currTime
  497. }
  498. // 根据配置处理最新值, 1 表示均值填充
  499. if lastValType == 1 {
  500. lastItem := dataList[len(dataList)-1]
  501. // 最后一天的累计值
  502. initVal := valueMap[lastNewDate]
  503. itemDate, tmpErr := time.ParseInLocation(utils.FormatDate, lastItem.DataTime, time.Local)
  504. if tmpErr != nil {
  505. err = tmpErr
  506. return
  507. }
  508. //获取当月的天数
  509. monthDays := lastNewDate.Day()
  510. //获取自然日的天数
  511. days := itemDate.Day()
  512. daysT := decimal.NewFromInt(int64(days))
  513. initValAndMonthT := decimal.NewFromFloat(initVal * float64(monthDays))
  514. val, _ := initValAndMonthT.Div(daysT).RoundCeil(4).Float64()
  515. fmt.Printf("最新值 计算公式:%d*%f/(%d) = %f\n", monthDays, initVal, days, val)
  516. valueMap[lastNewDate] = val
  517. }
  518. case "旬度":
  519. tmpDateDataMap := make(map[time.Time]float64)
  520. tmpDateList := make([]time.Time, 0)
  521. for _, item := range dataList {
  522. itemDate, tmpErr := time.ParseInLocation(utils.FormatDate, item.DataTime, time.Local)
  523. if tmpErr != nil {
  524. err = tmpErr
  525. return
  526. }
  527. dayInt := itemDate.Year()*100 + int(itemDate.Month())
  528. var currTime time.Time
  529. if itemDate.Day() <= 10 {
  530. //本月上旬
  531. tmpK := fmt.Sprint(dayInt, "10")
  532. currTime, err = time.ParseInLocation(utils.FormatDateUnSpace, tmpK, time.Local)
  533. if err != nil {
  534. return
  535. }
  536. } else if itemDate.Day() <= 20 {
  537. // 本月中旬
  538. tmpK := fmt.Sprint(dayInt, "20")
  539. currTime, err = time.ParseInLocation(utils.FormatDateUnSpace, tmpK, time.Local)
  540. if err != nil {
  541. return
  542. }
  543. } else {
  544. // 本月下旬
  545. currTime, err = time.ParseInLocation(utils.FormatYearMonthUnSpace, fmt.Sprint(dayInt), time.Local)
  546. if err != nil {
  547. return
  548. }
  549. currTime = currTime.AddDate(0, 1, -1)
  550. }
  551. yearVal, ok := tmpDateDataMap[currTime]
  552. if ok {
  553. tmpDateDataMap[currTime] = item.Value + yearVal
  554. } else {
  555. tmpDateList = append(tmpDateList, currTime)
  556. tmpDateDataMap[currTime] = item.Value
  557. }
  558. }
  559. for _, currTime := range tmpDateList {
  560. dateList = append(dateList, currTime)
  561. valueMap[currTime] = tmpDateDataMap[currTime]
  562. }
  563. case "周度":
  564. tmpDateDataMap := make(map[time.Time]float64)
  565. tmpDateList := make([]time.Time, 0)
  566. for _, item := range dataList {
  567. itemDate, tmpErr := time.ParseInLocation(utils.FormatDate, item.DataTime, time.Local)
  568. if tmpErr != nil {
  569. err = tmpErr
  570. return
  571. }
  572. var currTime time.Time
  573. // 周六周日,这是下一个周五的数据
  574. if itemDate.Weekday() == 0 {
  575. currTime = itemDate.AddDate(0, 0, 5)
  576. } else if itemDate.Weekday() == 6 {
  577. currTime = itemDate.AddDate(0, 0, 6)
  578. } else {
  579. currTime = itemDate.AddDate(0, 0, 5-int(itemDate.Weekday()))
  580. }
  581. yearVal, ok := tmpDateDataMap[currTime]
  582. if ok {
  583. tmpDateDataMap[currTime] = item.Value + yearVal
  584. } else {
  585. tmpDateList = append(tmpDateList, currTime)
  586. tmpDateDataMap[currTime] = item.Value
  587. }
  588. }
  589. for _, currTime := range tmpDateList {
  590. dateList = append(dateList, currTime)
  591. valueMap[currTime] = tmpDateDataMap[currTime]
  592. }
  593. }
  594. //获取指标所有数据
  595. existDataList := make([]*EdbData, 0)
  596. sql := `SELECT * FROM %s WHERE edb_info_id=? `
  597. sql = fmt.Sprintf(sql, dataTableName)
  598. _, err = to.Raw(sql, edbInfoId).QueryRows(&existDataList)
  599. if err != nil {
  600. return err
  601. }
  602. existDataMap := make(map[string]string)
  603. removeDataTimeMap := make(map[string]int) //需要移除的日期数据
  604. for _, v := range existDataList {
  605. existDataMap[v.DataTime] = v.Value
  606. removeDataTimeMap[v.DataTime] = 1
  607. }
  608. needAddDateMap := make(map[time.Time]int)
  609. addSql := ` INSERT INTO ` + dataTableName + `(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  610. var isAdd bool
  611. for _, currTime := range dateList {
  612. currDateStr := currTime.Format(utils.FormatDate)
  613. existVal, ok := existDataMap[currDateStr]
  614. tmpVal, ok2 := valueMap[currTime]
  615. if !ok2 {
  616. err = errors.New("数据异常,date:" + currDateStr)
  617. return
  618. }
  619. var saveValue string
  620. if isWeekData { //周度指标转的话,最后结果要除以7
  621. saveValue = decimal.NewFromFloat(tmpVal).Div(decimal.NewFromInt(7)).Round(4).String()
  622. } else {
  623. saveValue = decimal.NewFromFloat(tmpVal).Round(4).String()
  624. }
  625. // 如果库中已经存在该数据的话,那么就进行值的变更操作
  626. if ok {
  627. //校验待删除日期数据里面是否存在该元素,如果存在的话,那么移除该日期
  628. delete(removeDataTimeMap, currDateStr)
  629. if existVal != saveValue {
  630. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  631. sql = fmt.Sprintf(sql, dataTableName)
  632. _, err = to.Raw(sql, saveValue, edbInfoId, currDateStr).Exec()
  633. if err != nil {
  634. return
  635. }
  636. }
  637. continue
  638. }
  639. // 库中不存在该日期的数据
  640. timestamp := currTime.UnixNano() / 1e6
  641. timeStr := fmt.Sprintf("%d", timestamp)
  642. if _, existOk := needAddDateMap[currTime]; !existOk {
  643. addSql += GetAddSql(edbInfoIdStr, edbCode, currDateStr, timeStr, saveValue)
  644. isAdd = true
  645. }
  646. needAddDateMap[currTime] = 1
  647. }
  648. //删除已经不存在的指标数据(由于该指标当日的数据删除了)
  649. {
  650. removeDateList := make([]string, 0)
  651. for dateTime := range removeDataTimeMap {
  652. removeDateList = append(removeDateList, dateTime)
  653. }
  654. removeNum := len(removeDateList)
  655. if removeNum > 0 {
  656. sql := fmt.Sprintf(` DELETE FROM %s WHERE edb_info_id = ? and data_time in (`+utils.GetOrmInReplace(removeNum)+`) `, dataTableName)
  657. _, err = to.Raw(sql, edbInfo.EdbInfoId, removeDateList).Exec()
  658. if err != nil {
  659. fmt.Println(reflect.TypeOf(obj).Name(), " add data ;delete Err", err.Error())
  660. err = fmt.Errorf("删除不存在的指标数据失败,Err:" + err.Error())
  661. return
  662. }
  663. }
  664. }
  665. if isAdd {
  666. addSql = strings.TrimRight(addSql, ",")
  667. _, err = to.Raw(addSql).Exec()
  668. if err != nil {
  669. fmt.Println(reflect.TypeOf(obj).Name(), " add data Err", err.Error())
  670. return
  671. }
  672. }
  673. return
  674. }