base_from_gie.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. package models
  2. import (
  3. "eta_gn/eta_index_lib/global"
  4. "eta_gn/eta_index_lib/utils"
  5. "fmt"
  6. "strconv"
  7. "strings"
  8. "time"
  9. )
  10. //type BaseFromTradeEicIndex struct {
  11. // BaseFromEicIndexId int `orm:"column(base_from_eic_index_id);pk"`
  12. // Country string
  13. // Type string
  14. // EicCode string
  15. // ShortName string
  16. // Name string
  17. // Status string
  18. // GasDayStartedOn string
  19. // GasInStorage string
  20. // GasInStorageCode string
  21. // Full string
  22. // FullCode string
  23. // Trend string
  24. // TrendCode string
  25. // Injection string
  26. // InjectionCode string
  27. // Withdrawal string
  28. // WithdrawalCode string
  29. // WorkingGasVolume string
  30. // WorkingGasVolumeCode string
  31. // InjectionCapacity string
  32. // InjectionCapacityCode string
  33. // WithdrawalCapacity string
  34. // WithdrawalCapacityCode string
  35. // Info string
  36. // CreateTime time.Time
  37. // ModifyTime time.Time
  38. //}
  39. type BaseFromTradeEicIndex struct {
  40. BaseFromEicIndexId int `gorm:"column:base_from_eic_index_id;primaryKey"`
  41. Country string `gorm:"column:country"`
  42. Type string `gorm:"column:type"`
  43. EicCode string `gorm:"column:eic_code"`
  44. ShortName string `gorm:"column:short_name"`
  45. Name string `gorm:"column:name"`
  46. Status string `gorm:"column:status"`
  47. GasDayStartedOn string `gorm:"column:gas_day_started_on"`
  48. GasInStorage string `gorm:"column:gas_in_storage"`
  49. GasInStorageCode string `gorm:"column:gas_in_storage_code"`
  50. Full string `gorm:"column:full"`
  51. FullCode string `gorm:"column:full_code"`
  52. Trend string `gorm:"column:trend"`
  53. TrendCode string `gorm:"column:trend_code"`
  54. Injection string `gorm:"column:injection"`
  55. InjectionCode string `gorm:"column:injection_code"`
  56. Withdrawal string `gorm:"column:withdrawal"`
  57. WithdrawalCode string `gorm:"column:withdrawal_code"`
  58. WorkingGasVolume string `gorm:"column:working_gas_volume"`
  59. WorkingGasVolumeCode string `gorm:"column:working_gas_volume_code"`
  60. InjectionCapacity string `gorm:"column:injection_capacity"`
  61. InjectionCapacityCode string `gorm:"column:injection_capacity_code"`
  62. WithdrawalCapacity string `gorm:"column:withdrawal_capacity"`
  63. WithdrawalCapacityCode string `gorm:"column:withdrawal_capacity_code"`
  64. Info string `gorm:"column:info"`
  65. CreateTime time.Time `gorm:"column:create_time"`
  66. ModifyTime time.Time `gorm:"column:modify_time"`
  67. }
  68. func (m *BaseFromTradeEicIndex) TableName() string {
  69. return "base_from_eic_index"
  70. }
  71. //type BaseFromTradeEicIndexV2 struct {
  72. // BaseFromEicIndexId int `orm:"column(base_from_eic_index_id);pk"`
  73. // Type string
  74. // EicCode string
  75. // Name string
  76. // Status string
  77. // GasDayStart string
  78. // GasInStorage string
  79. // GasInStorageCode string
  80. // Consumption string
  81. // ConsumptionCode string
  82. // ConsumptionFull string
  83. // ConsumptionFullCode string
  84. // Full string
  85. // FullCode string
  86. // Trend string
  87. // TrendCode string
  88. // Injection string
  89. // InjectionCode string
  90. // Withdrawal string
  91. // WithdrawalCode string
  92. // WorkingGasVolume string
  93. // WorkingGasVolumeCode string
  94. // InjectionCapacity string
  95. // InjectionCapacityCode string
  96. // WithdrawalCapacity string
  97. // WithdrawalCapacityCode string
  98. // Info string
  99. // Parent string
  100. // CreateTime time.Time
  101. // ModifyTime time.Time
  102. //}
  103. type BaseFromTradeEicIndexV2 struct {
  104. BaseFromEicIndexId int `gorm:"column:base_from_eic_index_id;primaryKey"`
  105. Type string `gorm:"column:type"`
  106. EicCode string `gorm:"column:eic_code"`
  107. Name string `gorm:"column:name"`
  108. Status string `gorm:"column:status"`
  109. GasDayStart string `gorm:"column:gas_day_start"`
  110. GasInStorage string `gorm:"column:gas_in_storage"`
  111. GasInStorageCode string `gorm:"column:gas_in_storage_code"`
  112. Consumption string `gorm:"column:consumption"`
  113. ConsumptionCode string `gorm:"column:consumption_code"`
  114. ConsumptionFull string `gorm:"column:consumption_full"`
  115. ConsumptionFullCode string `gorm:"column:consumption_full_code"`
  116. Full string `gorm:"column:full"`
  117. FullCode string `gorm:"column:full_code"`
  118. Trend string `gorm:"column:trend"`
  119. TrendCode string `gorm:"column:trend_code"`
  120. Injection string `gorm:"column:injection"`
  121. InjectionCode string `gorm:"column:injection_code"`
  122. Withdrawal string `gorm:"column:withdrawal"`
  123. WithdrawalCode string `gorm:"column:withdrawal_code"`
  124. WorkingGasVolume string `gorm:"column:working_gas_volume"`
  125. WorkingGasVolumeCode string `gorm:"column:working_gas_volume_code"`
  126. InjectionCapacity string `gorm:"column:injection_capacity"`
  127. InjectionCapacityCode string `gorm:"column:injection_capacity_code"`
  128. WithdrawalCapacity string `gorm:"column:withdrawal_capacity"`
  129. WithdrawalCapacityCode string `gorm:"column:withdrawal_capacity_code"`
  130. Info string `gorm:"column:info"`
  131. Parent string `gorm:"column:parent"`
  132. CreateTime time.Time `gorm:"column:create_time"`
  133. ModifyTime time.Time `gorm:"column:modify_time"`
  134. }
  135. func (m *BaseFromTradeEicIndexV2) TableName() string {
  136. return "base_from_eic_index"
  137. }
  138. func GetBaseFromEicDataAllByIndexCode(indexCode, suffix string) (list []*BaseFromTradeEicIndexV2, err error) {
  139. //o := orm.NewOrm()
  140. var name string
  141. if suffix == "" {
  142. name = "eic_code"
  143. } else if suffix == "GS" {
  144. name = "gas_in_storage_code"
  145. } else if suffix == "C" {
  146. name = "consumption_code"
  147. } else if suffix == "CF" {
  148. name = "consumption_full_code"
  149. } else if suffix == "F" {
  150. name = "full_code"
  151. } else if suffix == "T" {
  152. name = "trend_code"
  153. } else if suffix == "In" {
  154. name = "injection_code"
  155. } else if suffix == "Out" {
  156. name = "withdrawal_code"
  157. } else if suffix == "WGV" {
  158. name = "working_gas_volume_code"
  159. } else if suffix == "IC" {
  160. name = "injection_capacity_code"
  161. } else if suffix == "WC" {
  162. name = "withdrawal_capacity_code"
  163. }
  164. sql := `SELECT * FROM base_from_trade_eic_index_v2 WHERE %s=? `
  165. sql = fmt.Sprintf(sql, name)
  166. //_, err = o.Raw(sql, indexCode).QueryRows(&list)
  167. err = global.DEFAULT_DmSQL.Raw(sql, indexCode).Find(&list).Error
  168. return
  169. }
  170. func GetGieDataByTradeCode(condition string, pars []interface{}) (item []*BaseFromTradeEicIndex, err error) {
  171. sql := ` SELECT * FROM base_from_trade_eic_index WHERE 1=1 `
  172. //o := orm.NewOrm()
  173. if condition != "" {
  174. sql += condition
  175. }
  176. sql += ` ORDER BY gas_day_started_on DESC `
  177. fmt.Println(sql, pars)
  178. //_, err = o.Raw(sql, pars).QueryRows(&item)
  179. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&item).Error
  180. return
  181. }
  182. func GetGieDataByTradeCodeV2(condition string, pars []interface{}) (item []*BaseFromTradeEicIndexV2, err error) {
  183. sql := ` SELECT * FROM base_from_trade_eic_index_v2 WHERE 1=1 `
  184. //o := orm.NewOrm()
  185. if condition != "" {
  186. sql += condition
  187. }
  188. sql += ` ORDER BY gas_day_start DESC `
  189. fmt.Println(sql, pars)
  190. //_, err = o.Raw(sql, pars).QueryRows(&item)
  191. err = global.DEFAULT_DmSQL.Raw(sql, pars...).Find(&item).Error
  192. return
  193. }
  194. // 新增欧洲天然气指标数据
  195. func AddEdbDataFromGie(edbCode string) (err error) {
  196. var suffix string
  197. l := len(edbCode)
  198. if strings.Contains(edbCode[l-2:], "GS") {
  199. suffix = "GS"
  200. } else if strings.Contains(edbCode[l-2:], "CF") {
  201. suffix = "CF"
  202. } else if strings.Contains(edbCode[l-1:], "T") {
  203. suffix = "T"
  204. } else if strings.Contains(edbCode[l-2:], "In") {
  205. suffix = "In"
  206. } else if strings.Contains(edbCode[l-3:], "Out") {
  207. suffix = "Out"
  208. } else if strings.Contains(edbCode[l-3:], "WGV") {
  209. suffix = "WGV"
  210. } else if strings.Contains(edbCode[l-2:], "IC") {
  211. suffix = "IC"
  212. } else if strings.Contains(edbCode[l-2:], "WC") {
  213. suffix = "WC"
  214. } else if strings.Contains(edbCode[l-1:], "F") {
  215. suffix = "F"
  216. } else if strings.Contains(edbCode[l-1:], "C") {
  217. suffix = "C"
  218. } else {
  219. suffix = ""
  220. }
  221. //o := orm.NewOrm()
  222. eicBaseDataAll, err := GetBaseFromEicDataAllByIndexCode(edbCode, suffix)
  223. if err != nil && err.Error() != utils.ErrNoRow() {
  224. fmt.Println("GetBaseFromEicDataAllByIndexCode err:", err)
  225. return
  226. }
  227. var isAdd bool
  228. addSql := ` INSERT INTO edb_data_gie(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  229. existMap := make(map[string]string)
  230. for _, sv := range eicBaseDataAll {
  231. eDate := sv.GasDayStart
  232. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  233. if err != nil {
  234. fmt.Println("time.Parse Err:" + eDate)
  235. return err
  236. }
  237. timestamp := dataTime.UnixNano() / 1e6
  238. timeStr := fmt.Sprintf("%d", timestamp)
  239. //var name string
  240. if _, ok := existMap[eDate]; !ok {
  241. if suffix == "GS" {
  242. //name = "gas_in_storage"
  243. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.GasInStorage)
  244. existMap[eDate] = sv.GasInStorage
  245. } else if suffix == "C" {
  246. //name = "consumption"
  247. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.Full)
  248. existMap[eDate] = sv.Consumption
  249. } else if suffix == "CF" {
  250. //name = "consumption_full"
  251. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.Full)
  252. existMap[eDate] = sv.ConsumptionFull
  253. } else if suffix == "F" {
  254. //name = "full"
  255. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.Full)
  256. existMap[eDate] = sv.Full
  257. } else if suffix == "T" {
  258. //name = "trend"
  259. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.Trend)
  260. existMap[eDate] = sv.Trend
  261. } else if suffix == "In" {
  262. //name = "injection"
  263. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.Injection)
  264. existMap[eDate] = sv.Injection
  265. } else if suffix == "Out" {
  266. //name = "withdrawal"
  267. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.Withdrawal)
  268. existMap[eDate] = sv.Withdrawal
  269. } else if suffix == "WGV" {
  270. //name = "working_gas_volume"
  271. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.WorkingGasVolume)
  272. existMap[eDate] = sv.WorkingGasVolume
  273. } else if suffix == "IC" {
  274. //name = "injection_capacity"
  275. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.InjectionCapacity)
  276. existMap[eDate] = sv.InjectionCapacity
  277. } else if suffix == "WC" {
  278. //name = "withdrawal_capacity"
  279. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.WithdrawalCapacity)
  280. existMap[eDate] = sv.WithdrawalCapacity
  281. }
  282. isAdd = true
  283. }
  284. }
  285. if isAdd {
  286. addSql = strings.TrimRight(addSql, ",")
  287. utils.FileLog.Info("addSql:" + addSql)
  288. //_, err = o.Raw(addSql).Exec()
  289. err = global.DEFAULT_DmSQL.Exec(addSql).Error
  290. if err != nil {
  291. fmt.Println("addSql err:", err)
  292. return err
  293. }
  294. }
  295. return
  296. }
  297. // 刷新欧洲天然气指标数据
  298. func RefreshEdbDataFromGie(edbInfoId int, edbCode, startDate string) (err error) {
  299. source := utils.DATA_SOURCE_GIE
  300. subSource := utils.DATA_SUB_SOURCE_EDB
  301. //o := orm.NewOrm()
  302. if err != nil {
  303. return
  304. }
  305. var suffix string
  306. l := len(edbCode)
  307. if strings.Contains(edbCode[l-2:], "GS") {
  308. suffix = "GS"
  309. } else if strings.Contains(edbCode[l-2:], "CF") {
  310. suffix = "CF"
  311. } else if strings.Contains(edbCode[l-1:], "T") {
  312. suffix = "T"
  313. } else if strings.Contains(edbCode[l-2:], "In") {
  314. suffix = "In"
  315. } else if strings.Contains(edbCode[l-3:], "Out") {
  316. suffix = "Out"
  317. } else if strings.Contains(edbCode[l-3:], "WGV") {
  318. suffix = "WGV"
  319. } else if strings.Contains(edbCode[l-2:], "IC") {
  320. suffix = "IC"
  321. } else if strings.Contains(edbCode[l-2:], "WC") {
  322. suffix = "WC"
  323. } else if strings.Contains(edbCode[l-1:], "F") {
  324. suffix = "F"
  325. } else if strings.Contains(edbCode[l-1:], "C") {
  326. suffix = "C"
  327. } else {
  328. suffix = ""
  329. }
  330. edbInfoIdStr := strconv.Itoa(edbInfoId)
  331. //计算数据
  332. var condition string
  333. var pars []interface{}
  334. if edbCode != "" {
  335. condition += " AND eic_code=? "
  336. pars = append(pars, edbCode[:l-len(suffix)])
  337. }
  338. if startDate != "" {
  339. condition += " AND gas_day_start>=? "
  340. pars = append(pars, startDate)
  341. }
  342. eicDataList, err := GetGieDataByTradeCodeV2(condition, pars)
  343. if err != nil {
  344. return
  345. }
  346. fmt.Println("all eicDataList", len(eicDataList))
  347. // 真实数据的最大日期 , 插入规则配置的日期
  348. var realDataMaxDate, edbDataInsertConfigDate time.Time
  349. var edbDataInsertConfig *EdbDataInsertConfig
  350. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  351. {
  352. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  353. if err != nil && err.Error() != utils.ErrNoRow() {
  354. return
  355. }
  356. if edbDataInsertConfig != nil {
  357. edbDataInsertConfigDate = edbDataInsertConfig.Date
  358. }
  359. }
  360. //获取指标所有数据
  361. var existCondition string
  362. var existPars []interface{}
  363. existCondition += " AND edb_info_id=? "
  364. existPars = append(existPars, edbInfoId)
  365. if startDate != "" {
  366. existCondition += " AND data_time>=? "
  367. existPars = append(existPars, startDate)
  368. }
  369. existList, err := GetEdbDataByCondition(source, subSource, existCondition, existPars)
  370. if err != nil {
  371. return err
  372. }
  373. existMap := make(map[string]*EdbInfoSearchData)
  374. for _, v := range existList {
  375. existMap[v.DataTime] = v
  376. }
  377. addSql := ` INSERT INTO edb_data_gie(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  378. var isAdd bool
  379. dataMap := make(map[string]interface{})
  380. for _, v := range eicDataList {
  381. var value string
  382. if suffix == "GS" {
  383. value = v.GasInStorage
  384. } else if suffix == "C" {
  385. value = v.Consumption
  386. } else if suffix == "CF" {
  387. value = v.ConsumptionFull
  388. } else if suffix == "F" {
  389. value = v.Full
  390. } else if suffix == "T" {
  391. value = v.Trend
  392. } else if suffix == "In" {
  393. value = v.Injection
  394. } else if suffix == "Out" {
  395. value = v.Withdrawal
  396. } else if suffix == "WGV" {
  397. value = v.WorkingGasVolume
  398. } else if suffix == "IC" {
  399. value = v.InjectionCapacity
  400. } else if suffix == "WC" {
  401. value = v.WithdrawalCapacity
  402. }
  403. item := v
  404. itemValue := value
  405. eDate := item.GasDayStart
  406. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  407. if err != nil {
  408. return err
  409. }
  410. if findItem, ok := existMap[v.GasDayStart]; !ok {
  411. sValue := itemValue
  412. if sValue != "" {
  413. timestamp := dataTime.UnixNano() / 1e6
  414. timeStr := fmt.Sprintf("%d", timestamp)
  415. saveValue := sValue
  416. if _, ok := dataMap[eDate]; !ok {
  417. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
  418. isAdd = true
  419. }
  420. }
  421. } else {
  422. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != itemValue {
  423. err = ModifyEdbDataById(source, subSource, findItem.EdbDataId, itemValue)
  424. if err != nil {
  425. return err
  426. }
  427. }
  428. }
  429. dataMap[v.GasDayStart] = v.GasDayStart
  430. // 下面代码主要目的是处理掉手动插入的数据判断
  431. {
  432. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  433. realDataMaxDate = dataTime
  434. }
  435. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  436. isFindConfigDateRealData = true
  437. }
  438. }
  439. }
  440. // 处理手工数据补充的配置
  441. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, subSource, existMap, isFindConfigDateRealData)
  442. if isAdd {
  443. addSql = strings.TrimRight(addSql, ",")
  444. //_, err = o.Raw(addSql).Exec()
  445. err = global.DEFAULT_DmSQL.Exec(addSql).Error
  446. if err != nil {
  447. return err
  448. }
  449. }
  450. return
  451. }