base_from_gie.go 12 KB

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