edb_info_calculate_bp.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. package data_manage
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/beego/beego/v2/client/orm"
  6. "github.com/shopspring/decimal"
  7. "hongze/hz_crm_api/utils"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. type EdbInfoCalculateBp struct {
  13. EdbInfoCalculateBpId int `orm:"column(edb_info_calculate_bp_id);pk"`
  14. EdbInfoId int `description:"指标id"`
  15. EdbCode string `description:"指标编码"`
  16. FromEdbInfoId int `description:"计算指标id"`
  17. FromEdbCode string `description:"计算指标编码"`
  18. FromEdbName string `description:"计算指标名称"`
  19. FromSource int `description:"计算指标来源"`
  20. FromSourceName string `description:"计算指标来源名称"`
  21. FromTag string `description:"来源指标标签"`
  22. Sort int `description:"计算指标名称排序"`
  23. CreateTime time.Time `description:"创建时间"`
  24. ModifyTime time.Time `description:"修改时间"`
  25. }
  26. // 变频
  27. func AddCalculateBp(req *EdbInfoCalculateBatchSaveReq, fromEdbInfo *EdbInfo, edbCode, uniqueCode string, sysUserId int, sysUserRealName string) (edbInfoId int, err error) {
  28. fmt.Println("AddCalculateBp")
  29. o := orm.NewOrmUsingDB("data")
  30. to, err := o.Begin()
  31. if err != nil {
  32. return
  33. }
  34. defer func() {
  35. if err != nil {
  36. fmt.Println("AddCalculateBp,Err:" + err.Error())
  37. _ = to.Rollback()
  38. } else {
  39. _ = to.Commit()
  40. }
  41. }()
  42. if req.EdbInfoId <= 0 {
  43. edbInfo := new(EdbInfo)
  44. edbInfo.Source = utils.DATA_SOURCE_CALCULATE_BP
  45. edbInfo.SourceName = "变频"
  46. edbInfo.EdbCode = edbCode
  47. edbInfo.EdbName = req.EdbName
  48. edbInfo.EdbNameSource = req.EdbName
  49. edbInfo.Frequency = req.Frequency
  50. edbInfo.Unit = req.Unit
  51. edbInfo.ClassifyId = req.ClassifyId
  52. edbInfo.SysUserId = sysUserId
  53. edbInfo.SysUserRealName = sysUserRealName
  54. edbInfo.CreateTime = time.Now()
  55. edbInfo.ModifyTime = time.Now()
  56. edbInfo.UniqueCode = uniqueCode
  57. edbInfo.CalculateFormula = req.Formula
  58. edbInfo.EdbType = 2
  59. newEdbInfoId, tmpErr := to.Insert(edbInfo)
  60. if tmpErr != nil {
  61. return edbInfoId, tmpErr
  62. }
  63. edbInfoId = int(newEdbInfoId)
  64. //关联关系
  65. {
  66. calculateMappingItem := new(EdbInfoCalculateMapping)
  67. calculateMappingItem.CreateTime = time.Now()
  68. calculateMappingItem.ModifyTime = time.Now()
  69. calculateMappingItem.Sort = 1
  70. calculateMappingItem.EdbCode = edbCode
  71. calculateMappingItem.EdbInfoId = edbInfoId
  72. calculateMappingItem.FromEdbInfoId = fromEdbInfo.EdbInfoId
  73. calculateMappingItem.FromEdbCode = fromEdbInfo.EdbCode
  74. calculateMappingItem.FromEdbName = fromEdbInfo.EdbName
  75. calculateMappingItem.FromSource = fromEdbInfo.Source
  76. calculateMappingItem.FromSourceName = fromEdbInfo.SourceName
  77. calculateMappingItem.FromTag = ""
  78. calculateMappingItem.Source = edbInfo.Source
  79. calculateMappingItem.SourceName = edbInfo.SourceName
  80. _, err = to.Insert(calculateMappingItem)
  81. if err != nil {
  82. return
  83. }
  84. }
  85. } else {
  86. edbInfoId = req.EdbInfoId
  87. dataTableName := GetEdbDataTableName(utils.DATA_SOURCE_CALCULATE_BP)
  88. fmt.Println("dataTableName:" + dataTableName)
  89. deleteSql := ` DELETE FROM %s WHERE edb_info_id=? `
  90. deleteSql = fmt.Sprintf(deleteSql, dataTableName)
  91. _, err = to.Raw(deleteSql, req.EdbInfoId).Exec()
  92. if err != nil {
  93. return 0, err
  94. }
  95. }
  96. edbInfoIdStr := strconv.Itoa(edbInfoId)
  97. fmt.Println("edbInfoIdStr:" + edbInfoIdStr)
  98. //计算数据
  99. var condition string
  100. var pars []interface{}
  101. condition += " AND edb_info_id=? "
  102. if req.EdbInfoId <= 0 {
  103. pars = append(pars, req.FromEdbInfoId)
  104. } else {
  105. pars = append(pars, fromEdbInfo.EdbInfoId)
  106. }
  107. dataList, err := GetEdbDataListAll(condition, pars, fromEdbInfo.Source, 0)
  108. if err != nil {
  109. return edbInfoId, err
  110. }
  111. addSql := ` INSERT INTO edb_data_calculate_bp(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  112. var isAdd bool
  113. existMap := make(map[string]string)
  114. dataLen := len(dataList)
  115. fmt.Println("dataLen:", dataLen)
  116. for i := 0; i < dataLen; i++ {
  117. //当期
  118. currentItem := dataList[i]
  119. currentDate, _ := time.Parse(utils.FormatDate, currentItem.DataTime)
  120. var day int
  121. var preItem *EdbInfoSearchData
  122. var preDate time.Time
  123. if i == 0 {
  124. day = int(time.Now().Sub(currentDate).Hours() / float64(24))
  125. preDate = time.Now()
  126. } else {
  127. j := i - 1
  128. if j < dataLen {
  129. preItem = dataList[j]
  130. preDate, _ = time.Parse(utils.FormatDate, preItem.DataTime)
  131. day = int(preDate.Sub(currentDate).Hours() / float64(24))
  132. utils.FileLog.Info("preItem.DataTime:" + preItem.DataTime + ";currentItem.DataTime" + currentItem.DataTime)
  133. }
  134. }
  135. for k := 0; k <= day; k++ {
  136. needDay := preDate.AddDate(0, 0, -k)
  137. needDayStr := needDay.Format(utils.FormatDate)
  138. existKey := edbCode + needDayStr
  139. if _, ok := existMap[existKey]; !ok {
  140. timestamp := needDay.UnixNano() / 1e6
  141. timestampStr := fmt.Sprintf("%d", timestamp)
  142. valStr := decimal.NewFromFloat(currentItem.Value).String()
  143. addSql += GetAddSql(edbInfoIdStr, edbCode, needDayStr, timestampStr, valStr)
  144. isAdd = true
  145. }
  146. existMap[existKey] = needDayStr
  147. }
  148. existKey := edbCode + currentItem.DataTime
  149. if _, ok := existMap[existKey]; !ok {
  150. currentDate, _ := time.Parse(utils.FormatDate, currentItem.DataTime)
  151. timestamp := currentDate.UnixNano() / 1e6
  152. timestampStr := fmt.Sprintf("%d", timestamp)
  153. valStr := decimal.NewFromFloat(currentItem.Value).String()
  154. addSql += GetAddSql(edbInfoIdStr, edbCode, currentItem.DataTime, timestampStr, valStr)
  155. isAdd = true
  156. }
  157. existMap[existKey] = currentItem.DataTime
  158. }
  159. if isAdd {
  160. addSql = strings.TrimRight(addSql, ",")
  161. _, err = to.Raw(addSql).Exec()
  162. if err != nil {
  163. return edbInfoId, err
  164. }
  165. }
  166. return
  167. }
  168. // 环比值
  169. func EditCalculateBp(req *EdbInfoCalculateBatchEditReq, fromEdbInfo *EdbInfo, edbCode string) (edbInfoId int, err error) {
  170. edbInfoId = req.EdbInfoId
  171. o := orm.NewOrmUsingDB("data")
  172. to, err := o.Begin()
  173. if err != nil {
  174. return
  175. }
  176. defer func() {
  177. if err != nil {
  178. _ = to.Rollback()
  179. } else {
  180. _ = to.Commit()
  181. }
  182. }()
  183. //修改指标信息
  184. sql := ` UPDATE edb_info
  185. SET
  186. edb_name =?,
  187. edb_name_source=?,
  188. frequency = ?,
  189. unit = ?,
  190. classify_id = ?,
  191. modify_time = NOW()
  192. WHERE edb_info_id = ? `
  193. _, err = to.Raw(sql, req.EdbName, req.EdbName, req.Frequency, req.Unit, req.ClassifyId, edbInfoId).Exec()
  194. if err != nil {
  195. return
  196. }
  197. var existCondition string
  198. var existPars []interface{}
  199. existCondition += " AND edb_info_id=? "
  200. existPars = append(existPars, edbInfoId)
  201. existCondition += " AND from_edb_info_id=? "
  202. existPars = append(existPars, req.FromEdbInfoId)
  203. //判断计算指标是否被更换
  204. count, err := GetEdbInfoCalculateCountByCondition(req.Source, existCondition, existPars)
  205. if err != nil {
  206. err = errors.New("判断指标是否改变失败,Err:" + err.Error())
  207. return
  208. }
  209. if count <= 0 {
  210. //删除指标关联计算指标
  211. //sql := ` DELETE FROM edb_info_calculate_bp WHERE edb_info_id = ? `
  212. //_, err = to.Raw(sql, edbInfoId).Exec()
  213. //if err != nil {
  214. // return
  215. //}
  216. //calculateItem := new(EdbInfoCalculateHbz)
  217. //calculateItem.CreateTime = time.Now()
  218. //calculateItem.ModifyTime = time.Now()
  219. //calculateItem.Sort = 1
  220. //calculateItem.EdbCode = edbCode
  221. //calculateItem.EdbInfoId = edbInfoId
  222. //calculateItem.FromEdbInfoId = fromEdbInfo.EdbInfoId
  223. //calculateItem.FromEdbCode = fromEdbInfo.EdbCode
  224. //calculateItem.FromEdbName = fromEdbInfo.EdbName
  225. //calculateItem.FromSource = fromEdbInfo.Source
  226. //calculateItem.FromSourceName = fromEdbInfo.SourceName
  227. //
  228. //_, err = to.Insert(calculateItem)
  229. //if err != nil {
  230. // return
  231. //}
  232. //删除,计算指标关联的,基础指标的关联关系
  233. sql = ` DELETE FROM edb_info_calculate_mapping WHERE edb_info_id = ? `
  234. _, err = to.Raw(sql, edbInfoId).Exec()
  235. if err != nil {
  236. err = errors.New("删除计算指标关联关系失败,Err:" + err.Error())
  237. return
  238. }
  239. //清空原有数据
  240. sql = ` DELETE FROM edb_data_calculate_bp WHERE edb_info_id = ? `
  241. _, err = to.Raw(sql, edbInfoId).Exec()
  242. if err != nil {
  243. return edbInfoId, err
  244. }
  245. //关联关系
  246. {
  247. calculateMappingItem := new(EdbInfoCalculateMapping)
  248. calculateMappingItem.CreateTime = time.Now()
  249. calculateMappingItem.ModifyTime = time.Now()
  250. calculateMappingItem.Sort = 1
  251. calculateMappingItem.EdbCode = edbCode
  252. calculateMappingItem.EdbInfoId = edbInfoId
  253. calculateMappingItem.FromEdbInfoId = fromEdbInfo.EdbInfoId
  254. calculateMappingItem.FromEdbCode = fromEdbInfo.EdbCode
  255. calculateMappingItem.FromEdbName = fromEdbInfo.EdbName
  256. calculateMappingItem.FromSource = fromEdbInfo.Source
  257. calculateMappingItem.FromSourceName = fromEdbInfo.SourceName
  258. calculateMappingItem.FromTag = ""
  259. calculateMappingItem.Source = utils.DATA_SOURCE_CALCULATE_BP
  260. calculateMappingItem.SourceName = "变频"
  261. _, err = to.Insert(calculateMappingItem)
  262. if err != nil {
  263. return
  264. }
  265. }
  266. edbInfoIdStr := strconv.Itoa(edbInfoId)
  267. fmt.Println("edbInfoIdStr:" + edbInfoIdStr)
  268. //计算数据
  269. var condition string
  270. var pars []interface{}
  271. condition += " AND edb_info_id=? "
  272. pars = append(pars, req.FromEdbInfoId)
  273. fmt.Println("EdbInfoId:", req.FromEdbInfoId)
  274. dataList, err := GetEdbDataListAll(condition, pars, fromEdbInfo.Source, 0)
  275. if err != nil {
  276. return edbInfoId, err
  277. }
  278. addSql := ` INSERT INTO edb_data_calculate_bp(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  279. var isAdd bool
  280. existMap := make(map[string]string)
  281. dataLen := len(dataList)
  282. for i := 0; i < dataLen; i++ {
  283. //当期
  284. currentItem := dataList[i]
  285. currentDate, _ := time.Parse(utils.FormatDate, currentItem.DataTime)
  286. var day int
  287. var preItem *EdbInfoSearchData
  288. var preDate time.Time
  289. if i == 0 {
  290. day = int(time.Now().Sub(currentDate).Hours() / float64(24))
  291. preDate = time.Now()
  292. } else {
  293. j := i + 1
  294. if j < dataLen {
  295. preItem = dataList[j]
  296. preDate, _ = time.Parse(utils.FormatDate, preItem.DataTime)
  297. day = int(currentDate.Sub(preDate).Hours() / float64(24))
  298. }
  299. }
  300. for k := 0; k <= day; k++ {
  301. needDay := preDate.AddDate(0, 0, -k)
  302. needDayStr := needDay.Format(utils.FormatDate)
  303. existKey := edbCode + needDayStr
  304. if _, ok := existMap[existKey]; !ok {
  305. timestamp := needDay.UnixNano() / 1e6
  306. timestampStr := fmt.Sprintf("%d", timestamp)
  307. valStr := decimal.NewFromFloat(currentItem.Value).String()
  308. addSql += GetAddSql(edbInfoIdStr, edbCode, needDayStr, timestampStr, valStr)
  309. }
  310. existMap[existKey] = needDayStr
  311. }
  312. existKey := edbCode + currentItem.DataTime
  313. if _, ok := existMap[existKey]; !ok {
  314. currentDate, _ := time.Parse(utils.FormatDate, currentItem.DataTime)
  315. timestamp := currentDate.UnixNano() / 1e6
  316. timestampStr := fmt.Sprintf("%d", timestamp)
  317. valStr := decimal.NewFromFloat(currentItem.Value).String()
  318. addSql += GetAddSql(edbInfoIdStr, edbCode, currentItem.DataTime, timestampStr, valStr)
  319. }
  320. existMap[existKey] = currentItem.DataTime
  321. }
  322. if isAdd {
  323. addSql = strings.TrimRight(addSql, ",")
  324. _, err = to.Raw(addSql).Exec()
  325. if err != nil {
  326. return edbInfoId, err
  327. }
  328. }
  329. }
  330. return
  331. }
  332. // 变频
  333. func RefreshCalculateBp(edbInfoId int, fromEdbInfo *EdbInfo, edbCode, startDate, endDate string) (err error) {
  334. o := orm.NewOrmUsingDB("data")
  335. to, err := o.Begin()
  336. if err != nil {
  337. return
  338. }
  339. defer func() {
  340. if err != nil {
  341. _ = to.Rollback()
  342. } else {
  343. _ = to.Commit()
  344. }
  345. }()
  346. if err != nil {
  347. return
  348. }
  349. edbInfoIdStr := strconv.Itoa(edbInfoId)
  350. //计算数据
  351. //计算数据
  352. var condition string
  353. var pars []interface{}
  354. condition += " AND edb_info_id=? "
  355. pars = append(pars, fromEdbInfo.EdbInfoId)
  356. if startDate != "" {
  357. condition += " AND data_time>=? "
  358. pars = append(pars, startDate)
  359. }
  360. if endDate != "" {
  361. condition += " AND data_time<=? "
  362. pars = append(pars, endDate)
  363. }
  364. dataList, err := GetEdbDataListAll(condition, pars, fromEdbInfo.Source, 0)
  365. if err != nil {
  366. return err
  367. }
  368. existMap := make(map[string]string)
  369. dataLen := len(dataList)
  370. addSql := ` INSERT INTO edb_data_calculate_bp(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  371. var isAdd bool
  372. for i := 0; i < dataLen; i++ {
  373. //当期
  374. currentItem := dataList[i]
  375. currentDate, _ := time.Parse(utils.FormatDate, currentItem.DataTime)
  376. var day int
  377. var preItem *EdbInfoSearchData
  378. var preDate time.Time
  379. if i == 0 {
  380. day = int(time.Now().Sub(currentDate).Hours() / float64(24))
  381. preDate = time.Now()
  382. } else {
  383. j := i + 1
  384. if j < dataLen {
  385. preItem = dataList[j]
  386. preDate, _ = time.Parse(utils.FormatDate, preItem.DataTime)
  387. day = int(currentDate.Sub(preDate).Hours() / float64(24))
  388. }
  389. }
  390. for k := 0; k <= day; k++ {
  391. needDay := preDate.AddDate(0, 0, -k)
  392. needDayStr := needDay.Format(utils.FormatDate)
  393. existKey := edbCode + needDayStr
  394. if _, ok := existMap[existKey]; !ok {
  395. timestamp := needDay.UnixNano() / 1e6
  396. timestampStr := fmt.Sprintf("%d", timestamp)
  397. valStr := decimal.NewFromFloat(currentItem.Value).String()
  398. addSql += GetAddSql(edbInfoIdStr, edbCode, needDayStr, timestampStr, valStr)
  399. }
  400. existMap[existKey] = needDayStr
  401. }
  402. existKey := edbCode + currentItem.DataTime
  403. if _, ok := existMap[existKey]; !ok {
  404. currentDate, _ := time.Parse(utils.FormatDate, currentItem.DataTime)
  405. timestamp := currentDate.UnixNano() / 1e6
  406. timestampStr := fmt.Sprintf("%d", timestamp)
  407. valStr := decimal.NewFromFloat(currentItem.Value).String()
  408. addSql += GetAddSql(edbInfoIdStr, edbCode, currentItem.DataTime, timestampStr, valStr)
  409. }
  410. existMap[existKey] = currentItem.DataTime
  411. }
  412. if isAdd {
  413. addSql = strings.TrimRight(addSql, ",")
  414. _, err = to.Raw(addSql).Exec()
  415. if err != nil {
  416. return err
  417. }
  418. }
  419. return
  420. }
  421. type EdbInfoCalculateBpDetail struct {
  422. EdbInfoCalculateBpId int `orm:"column(edb_info_calculate_bp_id);pk"`
  423. EdbInfoId int `description:"指标id"`
  424. EdbCode string `description:"指标编码"`
  425. FromEdbInfoId int `description:"计算指标id"`
  426. FromEdbCode string `description:"计算指标编码"`
  427. FromEdbName string `description:"计算指标名称"`
  428. FromSource int `description:"计算指标来源"`
  429. FromSourceName string `description:"计算指标来源名称"`
  430. FromTag string `description:"来源指标标签"`
  431. Sort int `description:"计算指标名称排序"`
  432. CreateTime time.Time `description:"创建时间"`
  433. ModifyTime time.Time `description:"修改时间"`
  434. StartDate string `description:"开始日期"`
  435. EndDate string `description:"结束日期"`
  436. }
  437. func GetEdbInfoCalculateBpDetail(edbInfoId int) (item *EdbInfoCalculateTbzDetail, err error) {
  438. o := orm.NewOrmUsingDB("data")
  439. sql := ` SELECT a.*,b.start_date,b.end_date FROM edb_info_calculate_mapping AS a
  440. INNER JOIN edb_info AS b ON a.from_edb_info_id=b.edb_info_id
  441. WHERE a.edb_info_id=? `
  442. err = o.Raw(sql, edbInfoId).QueryRow(&item)
  443. return
  444. }
  445. // 刷新所有变频数据
  446. func RefreshAllCalculateBp(edbInfoId, source int, fromEdbInfo *EdbInfo, edbCode, startDate, endDate string) (err error) {
  447. o := orm.NewOrmUsingDB("data")
  448. to, err := o.Begin()
  449. if err != nil {
  450. return
  451. }
  452. defer func() {
  453. if err != nil {
  454. _ = to.Rollback()
  455. } else {
  456. _ = to.Commit()
  457. }
  458. }()
  459. if err != nil {
  460. return
  461. }
  462. edbInfoIdStr := strconv.Itoa(edbInfoId)
  463. //计算数据
  464. //计算数据
  465. var condition string
  466. var pars []interface{}
  467. condition += " AND edb_info_id=? "
  468. pars = append(pars, fromEdbInfo.EdbInfoId)
  469. if startDate != "" {
  470. condition += " AND data_time>=? "
  471. pars = append(pars, startDate)
  472. }
  473. if endDate != "" {
  474. condition += " AND data_time<=? "
  475. pars = append(pars, endDate)
  476. }
  477. dataList, err := GetEdbDataListAll(condition, pars, fromEdbInfo.Source, 0)
  478. if err != nil {
  479. return err
  480. }
  481. var dateArr []string
  482. dataMap := make(map[string]*EdbInfoSearchData)
  483. for _, v := range dataList {
  484. dateArr = append(dateArr, v.DataTime)
  485. dataMap[v.DataTime] = v
  486. }
  487. //获取指标所有数据
  488. existDataList := make([]*EdbDataBase, 0)
  489. dataTableName := GetEdbDataTableName(source)
  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. for _, v := range existDataList {
  498. existDataMap[v.DataTime] = v.Value
  499. }
  500. addSql := ` INSERT INTO edb_data_calculate_bp(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  501. var isAdd bool
  502. existMap := make(map[string]string)
  503. dataLen := len(dataList)
  504. for i := 0; i < dataLen; i++ {
  505. //当期
  506. currentItem := dataList[i]
  507. currentDate, _ := time.Parse(utils.FormatDate, currentItem.DataTime)
  508. var day int
  509. var preItem *EdbInfoSearchData
  510. var preDate time.Time
  511. if i == 0 {
  512. day = int(time.Now().Sub(currentDate).Hours() / float64(24))
  513. preDate = time.Now()
  514. } else {
  515. j := i + 1
  516. if j < dataLen {
  517. preItem = dataList[j]
  518. preDate, _ = time.Parse(utils.FormatDate, preItem.DataTime)
  519. day = int(currentDate.Sub(preDate).Hours() / float64(24))
  520. }
  521. }
  522. for k := 0; k <= day; k++ {
  523. needDay := preDate.AddDate(0, 0, -k)
  524. needDayStr := needDay.Format(utils.FormatDate)
  525. existKey := edbCode + needDayStr
  526. if _, ok := existMap[existKey]; !ok {
  527. timestamp := needDay.UnixNano() / 1e6
  528. timestampStr := fmt.Sprintf("%d", timestamp)
  529. valStr := decimal.NewFromFloat(currentItem.Value).String()
  530. if existVal, ok := existDataMap[needDayStr]; !ok {
  531. addSql += GetAddSql(edbInfoIdStr, edbCode, needDayStr, timestampStr, valStr)
  532. } else {
  533. if existVal != valStr {
  534. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  535. sql = fmt.Sprintf(sql, dataTableName)
  536. _, err = to.Raw(sql, valStr, edbInfoId, needDay).Exec()
  537. if err != nil {
  538. return err
  539. }
  540. }
  541. }
  542. }
  543. existMap[existKey] = needDayStr
  544. }
  545. existKey := edbCode + currentItem.DataTime
  546. if _, ok := existMap[existKey]; !ok {
  547. currentDate, _ := time.Parse(utils.FormatDate, currentItem.DataTime)
  548. timestamp := currentDate.UnixNano() / 1e6
  549. timestampStr := fmt.Sprintf("%d", timestamp)
  550. valStr := decimal.NewFromFloat(currentItem.Value).String()
  551. if existVal, ok := existDataMap[currentItem.DataTime]; !ok {
  552. addSql += GetAddSql(edbInfoIdStr, edbCode, currentItem.DataTime, timestampStr, valStr)
  553. } else {
  554. if existVal != valStr {
  555. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  556. sql = fmt.Sprintf(sql, dataTableName)
  557. _, err = to.Raw(sql, valStr, edbInfoId, currentItem.DataTime).Exec()
  558. if err != nil {
  559. return err
  560. }
  561. }
  562. }
  563. }
  564. existMap[existKey] = currentItem.DataTime
  565. }
  566. if isAdd {
  567. addSql = strings.TrimRight(addSql, ",")
  568. _, err = to.Raw(addSql).Exec()
  569. if err != nil {
  570. return err
  571. }
  572. }
  573. return
  574. }