edb_data_calculate_ljz.go 18 KB

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