base_from_gie.go 11 KB

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