base_from_gie.go 12 KB

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