edb_data_gie.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. package data_manage
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hz_crm_api/utils"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. type GieData struct {
  11. InputValue string `orm:"column(DATA_VALUE)" description:"日期"`
  12. DataTime string `orm:"column(DATA_DATE)" description:"值"`
  13. }
  14. func GetEdbDataGieMaxOrMinDate(edbCode string) (minDate, maxDate string, err error) {
  15. o := orm.NewOrmUsingDB("data")
  16. sql := ` SELECT MIN(data_time) AS minDate,MAX(data_time) AS maxDate FROM edb_data_gie WHERE edb_code=? `
  17. err = o.Raw(sql, edbCode).QueryRow(&minDate, &maxDate)
  18. return
  19. }
  20. func GetEdbDataByGie(edbCode, suffix, startDate, endDate string) (searchItem *EdbInfoSearch, err error) {
  21. o := orm.NewOrmUsingDB("data")
  22. to, err := o.Begin()
  23. if err != nil {
  24. return
  25. }
  26. searchItem = new(EdbInfoSearch)
  27. searchItem.EdbCode = edbCode
  28. eicBaseDataAll, err := GetBaseFromEicDataAllByIndexCode(edbCode, suffix)
  29. if err != nil && err.Error() != utils.ErrNoRow() {
  30. fmt.Println("GetBaseFromEicDataAllByIndexCode err:", err)
  31. return
  32. }
  33. var isAdd bool
  34. addSql := ` INSERT INTO edb_data_gie(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  35. dataList := make([]*EdbInfoSearchData, 0)
  36. existMap := make(map[string]string)
  37. for _, sv := range eicBaseDataAll {
  38. eDate := sv.GasDayStartedOn
  39. dataTime, err := time.Parse(utils.FormatDate, eDate)
  40. if err != nil {
  41. fmt.Println("time.Parse Err:" + eDate)
  42. return nil, err
  43. }
  44. timestamp := dataTime.UnixNano() / 1e6
  45. timeStr := fmt.Sprintf("%d", timestamp)
  46. //var name string
  47. if _, ok := existMap[eDate]; !ok {
  48. if suffix == "GS" {
  49. //name = "gas_in_storage"
  50. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.GasInStorage)
  51. existMap[eDate] = sv.GasInStorage
  52. } else if suffix == "F" {
  53. //name = "full"
  54. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.Full)
  55. existMap[eDate] = sv.Full
  56. } else if suffix == "T" {
  57. //name = "trend"
  58. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.Trend)
  59. existMap[eDate] = sv.Trend
  60. } else if suffix == "In" {
  61. //name = "injection"
  62. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.Injection)
  63. existMap[eDate] = sv.Injection
  64. } else if suffix == "Out" {
  65. //name = "withdrawal"
  66. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.Withdrawal)
  67. existMap[eDate] = sv.Withdrawal
  68. } else if suffix == "WGV" {
  69. //name = "working_gas_volume"
  70. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.WorkingGasVolume)
  71. existMap[eDate] = sv.WorkingGasVolume
  72. } else if suffix == "IC" {
  73. //name = "injection_capacity"
  74. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.InjectionCapacity)
  75. existMap[eDate] = sv.InjectionCapacity
  76. } else if suffix == "WC" {
  77. //name = "withdrawal_capacity"
  78. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.WithdrawalCapacity)
  79. existMap[eDate] = sv.WithdrawalCapacity
  80. }
  81. isAdd = true
  82. }
  83. }
  84. if isAdd {
  85. addSql = strings.TrimRight(addSql, ",")
  86. utils.FileLog.Info("addSql:" + addSql)
  87. _, err = to.Raw(addSql).Exec()
  88. if err != nil {
  89. fmt.Println("addSql err:", err)
  90. return searchItem, err
  91. }
  92. }
  93. if err != nil {
  94. _ = to.Rollback()
  95. } else {
  96. _ = to.Commit()
  97. }
  98. size := utils.EDB_DATA_LIMIT
  99. dataList, err = GetEdbDataAllByEdbCode(edbCode, utils.DATA_SOURCE_GIE, size)
  100. if err != nil {
  101. utils.FileLogData.Info("GetEdbDataGieByCode Err:%s", err.Error())
  102. return searchItem, err
  103. }
  104. minDate, maxDate, err := GetEdbDataGieMaxOrMinDate(edbCode)
  105. if err != nil {
  106. return searchItem, err
  107. }
  108. searchItem.DataList = dataList
  109. searchItem.StartDate = minDate
  110. searchItem.EndDate = maxDate
  111. if searchItem.DataList == nil {
  112. searchItem.DataList = make([]*EdbInfoSearchData, 0)
  113. }
  114. return
  115. }
  116. // RefreshEdbDataByGie 刷新欧洲天然气指标数据
  117. func RefreshEdbDataByGie(edbInfoId int, edbCode, startDate, endDate string) (err error) {
  118. o := orm.NewOrmUsingDB("data")
  119. to, err := o.Begin()
  120. if err != nil {
  121. return
  122. }
  123. defer func() {
  124. if err != nil {
  125. _ = to.Rollback()
  126. } else {
  127. _ = to.Commit()
  128. }
  129. }()
  130. if err != nil {
  131. fmt.Println("refresh err:", err)
  132. return
  133. }
  134. var suffix string
  135. l := len(edbCode)
  136. if strings.Contains(edbCode[l-2:], "GS") {
  137. suffix = "GS"
  138. } else if strings.Contains(edbCode[l-2:], "CF") {
  139. suffix = "CF"
  140. } else if strings.Contains(edbCode[l-1:], "T") {
  141. suffix = "T"
  142. } else if strings.Contains(edbCode[l-2:], "In") {
  143. suffix = "In"
  144. } else if strings.Contains(edbCode[l-3:], "Out") {
  145. suffix = "Out"
  146. } else if strings.Contains(edbCode[l-3:], "WGV") {
  147. suffix = "WGV"
  148. } else if strings.Contains(edbCode[l-2:], "IC") {
  149. suffix = "IC"
  150. } else if strings.Contains(edbCode[l-2:], "WC") {
  151. suffix = "WC"
  152. } else if strings.Contains(edbCode[l-1:], "F") {
  153. suffix = "F"
  154. } else if strings.Contains(edbCode[l-1:], "C") {
  155. suffix = "C"
  156. } else {
  157. suffix = ""
  158. }
  159. edbInfoIdStr := strconv.Itoa(edbInfoId)
  160. //计算数据
  161. var condition string
  162. var pars []interface{}
  163. if edbCode != "" {
  164. condition += " AND eic_code=? "
  165. pars = append(pars, edbCode[:l-len(suffix)])
  166. }
  167. if startDate != "" {
  168. condition += " AND gas_day_started_on>=? "
  169. pars = append(pars, startDate)
  170. }
  171. if endDate != "" {
  172. condition += " AND gas_day_started_on<=? "
  173. pars = append(pars, endDate)
  174. }
  175. eicDataList, err := GetGieDataByTradeCodeV2(condition, pars)
  176. fmt.Println("eicDataList", len(eicDataList))
  177. addSql := ` INSERT INTO edb_data_gie(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  178. var isAdd bool
  179. existMap := make(map[string]string)
  180. for _, v := range eicDataList {
  181. var value string
  182. if suffix == "GS" {
  183. value = v.GasInStorage
  184. } else if suffix == "C" {
  185. value = v.Consumption
  186. } else if suffix == "CF" {
  187. value = v.ConsumptionFull
  188. } else if suffix == "F" {
  189. value = v.Full
  190. } else if suffix == "T" {
  191. value = v.Trend
  192. } else if suffix == "In" {
  193. value = v.Injection
  194. } else if suffix == "Out" {
  195. value = v.Withdrawal
  196. } else if suffix == "WGV" {
  197. value = v.WorkingGasVolume
  198. } else if suffix == "IC" {
  199. value = v.InjectionCapacity
  200. } else if suffix == "WC" {
  201. value = v.WithdrawalCapacity
  202. }
  203. item := v
  204. itemValue := value
  205. if _, ok := existMap[v.GasDayStart]; !ok {
  206. count, err := GetEdbDataGieByCodeAndDate(edbCode, v.GasDayStart)
  207. if err != nil && err.Error() != utils.ErrNoRow() {
  208. return err
  209. }
  210. if count <= 0 {
  211. eDate := item.GasDayStart
  212. sValue := itemValue
  213. if sValue != "" {
  214. dataTime, err := time.Parse(utils.FormatDate, eDate)
  215. if err != nil {
  216. return err
  217. }
  218. timestamp := dataTime.UnixNano() / 1e6
  219. timeStr := fmt.Sprintf("%d", timestamp)
  220. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue)
  221. isAdd = true
  222. }
  223. } else {
  224. err = ModifyEdbDataGie(int64(edbInfoId), v.GasDayStart, value)
  225. if err != nil {
  226. return err
  227. }
  228. }
  229. }
  230. existMap[v.GasDayStart] = value
  231. }
  232. if isAdd {
  233. addSql = strings.TrimRight(addSql, ",")
  234. _, err = to.Raw(addSql).Exec()
  235. if err != nil {
  236. return err
  237. }
  238. }
  239. return
  240. }
  241. // RefreshAllEdbDataByGie 全部刷新欧洲天然气
  242. func RefreshAllEdbDataByGie(edbInfoId, source int, edbCode, startDate, endDate string) (err error) {
  243. o := orm.NewOrmUsingDB("data")
  244. to, err := o.Begin()
  245. if err != nil {
  246. return
  247. }
  248. defer func() {
  249. if err != nil {
  250. _ = to.Rollback()
  251. } else {
  252. _ = to.Commit()
  253. }
  254. }()
  255. if err != nil {
  256. return
  257. }
  258. var suffix string
  259. l := len(edbCode)
  260. if strings.Contains(edbCode[l-2:], "GS") {
  261. suffix = "GS"
  262. } else if strings.Contains(edbCode[l-2:], "CF") {
  263. suffix = "CF"
  264. } else if strings.Contains(edbCode[l-1:], "T") {
  265. suffix = "T"
  266. } else if strings.Contains(edbCode[l-2:], "In") {
  267. suffix = "In"
  268. } else if strings.Contains(edbCode[l-3:], "Out") {
  269. suffix = "Out"
  270. } else if strings.Contains(edbCode[l-3:], "WGV") {
  271. suffix = "WGV"
  272. } else if strings.Contains(edbCode[l-2:], "IC") {
  273. suffix = "IC"
  274. } else if strings.Contains(edbCode[l-2:], "WC") {
  275. suffix = "WC"
  276. } else if strings.Contains(edbCode[l-1:], "F") {
  277. suffix = "F"
  278. } else if strings.Contains(edbCode[l-1:], "C") {
  279. suffix = "C"
  280. } else {
  281. suffix = ""
  282. }
  283. edbInfoIdStr := strconv.Itoa(edbInfoId)
  284. //计算数据
  285. var condition string
  286. var pars []interface{}
  287. if edbCode != "" {
  288. condition += " AND eic_code=? "
  289. pars = append(pars, edbCode[:l-len(suffix)])
  290. }
  291. if startDate != "" {
  292. condition += " AND gas_day_start>=? "
  293. pars = append(pars, startDate)
  294. }
  295. if endDate != "" {
  296. condition += " AND gas_day_start<=? "
  297. pars = append(pars, endDate)
  298. }
  299. eicDataList, err := GetGieDataByTradeCodeV2(condition, pars)
  300. fmt.Println("all eicDataList", len(eicDataList))
  301. //获取指标所有数据
  302. dataList := make([]*EdbDataBase, 0)
  303. dataTableName := GetEdbDataTableName(source)
  304. sql := `SELECT * FROM %s WHERE edb_info_id=? `
  305. sql = fmt.Sprintf(sql, dataTableName)
  306. _, err = to.Raw(sql, edbInfoId).QueryRows(&dataList)
  307. if err != nil {
  308. return err
  309. }
  310. dataMap := make(map[string]string)
  311. for _, v := range dataList {
  312. dataMap[v.DataTime] = v.Value
  313. }
  314. addSql := ` INSERT INTO edb_data_gie(edb_info_id,edb_code,data_time,value,create_time,modify_time,status,data_timestamp) values `
  315. var isAdd bool
  316. existMap := make(map[string]string)
  317. for _, v := range eicDataList {
  318. var value string
  319. if suffix == "GS" {
  320. value = v.GasInStorage
  321. } else if suffix == "C" {
  322. value = v.Consumption
  323. } else if suffix == "CF" {
  324. value = v.ConsumptionFull
  325. } else if suffix == "F" {
  326. value = v.Full
  327. } else if suffix == "T" {
  328. value = v.Trend
  329. } else if suffix == "In" {
  330. value = v.Injection
  331. } else if suffix == "Out" {
  332. value = v.Withdrawal
  333. } else if suffix == "WGV" {
  334. value = v.WorkingGasVolume
  335. } else if suffix == "IC" {
  336. value = v.InjectionCapacity
  337. } else if suffix == "WC" {
  338. value = v.WithdrawalCapacity
  339. }
  340. item := v
  341. itemValue := value
  342. if _, ok := existMap[v.GasDayStart]; !ok {
  343. eDate := item.GasDayStart
  344. sValue := itemValue
  345. if sValue != "" {
  346. dataTime, err := time.Parse(utils.FormatDate, eDate)
  347. if err != nil {
  348. return err
  349. }
  350. timestamp := dataTime.UnixNano() / 1e6
  351. timeStr := fmt.Sprintf("%d", timestamp)
  352. saveValue := sValue
  353. if existVal, ok := dataMap[eDate]; !ok {
  354. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
  355. isAdd = true
  356. } else {
  357. if existVal != saveValue {
  358. sql := ` UPDATE %s SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  359. sql = fmt.Sprintf(sql, dataTableName)
  360. _, err = to.Raw(sql, sValue, edbInfoId, eDate).Exec()
  361. if err != nil {
  362. return err
  363. }
  364. }
  365. }
  366. }
  367. }
  368. existMap[v.GasDayStart] = v.GasDayStart
  369. }
  370. if isAdd {
  371. addSql = strings.TrimRight(addSql, ",")
  372. _, err = to.Raw(addSql).Exec()
  373. if err != nil {
  374. return err
  375. }
  376. }
  377. return
  378. }
  379. // GetBaseInfoFromEicByIndexCode 获取指标信息
  380. func GetBaseInfoFromEicByIndexCode(indexCode, suffix string) (list []*BaseFromTradeEicIndex, err error) {
  381. o := orm.NewOrmUsingDB("data")
  382. sql := `SELECT * FROM base_from_trade_eic_index WHERE %s_code=? `
  383. sql = fmt.Sprintf(sql, suffix)
  384. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  385. return
  386. }
  387. func GetGieDataByTradeCode(condition string, pars []interface{}) (item []*BaseFromTradeEicIndex, err error) {
  388. sql := ` SELECT * FROM base_from_trade_eic_index WHERE 1=1 `
  389. o := orm.NewOrmUsingDB("data")
  390. if condition != "" {
  391. sql += condition
  392. }
  393. sql += ` ORDER BY gas_day_started_on DESC `
  394. fmt.Println(sql, pars)
  395. _, err = o.Raw(sql, pars).QueryRows(&item)
  396. return
  397. }
  398. type EicIndexV2 struct {
  399. BaseFromEicIndexId int `orm:"column(base_from_eic_index_id);pk"`
  400. Type string
  401. EicCode string
  402. Name string
  403. Status string
  404. GasDayStart string
  405. GasInStorage string
  406. GasInStorageCode string
  407. Consumption string
  408. ConsumptionCode string
  409. ConsumptionFull string
  410. ConsumptionFullCode string
  411. Full string
  412. FullCode string
  413. Trend string
  414. TrendCode string
  415. Injection string
  416. InjectionCode string
  417. Withdrawal string
  418. WithdrawalCode string
  419. WorkingGasVolume string
  420. WorkingGasVolumeCode string
  421. InjectionCapacity string
  422. InjectionCapacityCode string
  423. WithdrawalCapacity string
  424. WithdrawalCapacityCode string
  425. Info string
  426. Parent string
  427. CreateTime time.Time
  428. ModifyTime time.Time
  429. Children []BaseFromTradeEicIndexV2
  430. }
  431. func GetGieDataByTradeCodeV2(condition string, pars []interface{}) (item []*EicIndexV2, err error) {
  432. sql := ` SELECT * FROM base_from_trade_eic_index_v2 WHERE 1=1 `
  433. o := orm.NewOrmUsingDB("data")
  434. if condition != "" {
  435. sql += condition
  436. }
  437. sql += ` ORDER BY gas_day_start DESC `
  438. fmt.Println(sql, pars)
  439. _, err = o.Raw(sql, pars).QueryRows(&item)
  440. return
  441. }
  442. func AddEdbDataGieBySql(sqlStr string) (err error) {
  443. o := orm.NewOrmUsingDB("data")
  444. _, err = o.Raw(sqlStr).Exec()
  445. return
  446. }
  447. func GetEdbDataGieByCode(edbCode string) (items []*EdbInfoSearchData, err error) {
  448. o := orm.NewOrmUsingDB("data")
  449. sql := ` SELECT * FROM edb_data_gie WHERE edb_code=? ORDER BY data_time DESC LIMIT ? `
  450. _, err = o.Raw(sql, edbCode, utils.EDB_DATA_LIMIT).QueryRows(&items)
  451. return
  452. }
  453. func GetBaseFromEicDataAllByIndexCode(indexCode, suffix string) (list []*BaseFromTradeEicIndex, err error) {
  454. o := orm.NewOrmUsingDB("data")
  455. var name string
  456. if suffix == "" {
  457. name = "eic_code"
  458. } else if suffix == "GS" {
  459. name = "gas_in_storage_code"
  460. } else if suffix == "C" {
  461. name = "consumption_code"
  462. } else if suffix == "CF" {
  463. name = "consumption_full_code"
  464. } else if suffix == "F" {
  465. name = "full_code"
  466. } else if suffix == "T" {
  467. name = "trend_code"
  468. } else if suffix == "In" {
  469. name = "injection_code"
  470. } else if suffix == "Out" {
  471. name = "withdrawal_code"
  472. } else if suffix == "WGV" {
  473. name = "working_gas_volume_code"
  474. } else if suffix == "IC" {
  475. name = "injection_capacity_code"
  476. } else if suffix == "WC" {
  477. name = "withdrawal_capacity_code"
  478. }
  479. sql := `SELECT * FROM base_from_trade_eic_index_v2 WHERE %s=? `
  480. sql = fmt.Sprintf(sql, name)
  481. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  482. return
  483. }
  484. func GetBaseFromEicDataAllByIndexCodeV2(indexCode, suffix string) (list []*BaseFromTradeEicIndexV2, err error) {
  485. o := orm.NewOrmUsingDB("data")
  486. var name string
  487. if suffix == "" {
  488. name = "eic_code"
  489. } else if suffix == "GS" {
  490. name = "gas_in_storage_code"
  491. } else if suffix == "C" {
  492. name = "consumption_code"
  493. } else if suffix == "CF" {
  494. name = "consumption_full_code"
  495. } else if suffix == "F" {
  496. name = "full_code"
  497. } else if suffix == "T" {
  498. name = "trend_code"
  499. } else if suffix == "In" {
  500. name = "injection_code"
  501. } else if suffix == "Out" {
  502. name = "withdrawal_code"
  503. } else if suffix == "WGV" {
  504. name = "working_gas_volume_code"
  505. } else if suffix == "IC" {
  506. name = "injection_capacity_code"
  507. } else if suffix == "WC" {
  508. name = "withdrawal_capacity_code"
  509. }
  510. sql := `SELECT * FROM base_from_trade_eic_index_v2 WHERE %s=? `
  511. sql = fmt.Sprintf(sql, name)
  512. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  513. return
  514. }
  515. func GetEdbDataGieByCodeAndDate(edbCode string, startDate string) (count int, err error) {
  516. o := orm.NewOrmUsingDB("data")
  517. sql := ` SELECT COUNT(1) AS count FROM edb_data_gie WHERE edb_code=? AND data_time=? `
  518. err = o.Raw(sql, edbCode, startDate).QueryRow(&count)
  519. return
  520. }
  521. func ModifyEdbDataGie(edbInfoId int64, dataTime, value string) (err error) {
  522. o := orm.NewOrmUsingDB("data")
  523. sql := ` UPDATE edb_data_gie SET value=?,modify_time=NOW() WHERE edb_info_id=? AND data_time=? `
  524. _, err = o.Raw(sql, value, edbInfoId, dataTime).Exec()
  525. return
  526. }