base_from_gie.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. o := orm.NewOrm()
  230. if err != nil {
  231. return
  232. }
  233. var suffix string
  234. l := len(edbCode)
  235. if strings.Contains(edbCode[l-2:], "GS") {
  236. suffix = "GS"
  237. } else if strings.Contains(edbCode[l-2:], "CF") {
  238. suffix = "CF"
  239. } else if strings.Contains(edbCode[l-1:], "T") {
  240. suffix = "T"
  241. } else if strings.Contains(edbCode[l-2:], "In") {
  242. suffix = "In"
  243. } else if strings.Contains(edbCode[l-3:], "Out") {
  244. suffix = "Out"
  245. } else if strings.Contains(edbCode[l-3:], "WGV") {
  246. suffix = "WGV"
  247. } else if strings.Contains(edbCode[l-2:], "IC") {
  248. suffix = "IC"
  249. } else if strings.Contains(edbCode[l-2:], "WC") {
  250. suffix = "WC"
  251. } else if strings.Contains(edbCode[l-1:], "F") {
  252. suffix = "F"
  253. } else if strings.Contains(edbCode[l-1:], "C") {
  254. suffix = "C"
  255. } else {
  256. suffix = ""
  257. }
  258. edbInfoIdStr := strconv.Itoa(edbInfoId)
  259. //计算数据
  260. var condition string
  261. var pars []interface{}
  262. if edbCode != "" {
  263. condition += " AND eic_code=? "
  264. pars = append(pars, edbCode[:l-len(suffix)])
  265. }
  266. if startDate != "" {
  267. condition += " AND gas_day_start>=? "
  268. pars = append(pars, startDate)
  269. }
  270. eicDataList, err := GetGieDataByTradeCodeV2(condition, pars)
  271. if err != nil {
  272. return
  273. }
  274. fmt.Println("all eicDataList", len(eicDataList))
  275. // 真实数据的最大日期 , 插入规则配置的日期
  276. var realDataMaxDate, edbDataInsertConfigDate time.Time
  277. var edbDataInsertConfig *EdbDataInsertConfig
  278. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  279. {
  280. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  281. if err != nil && err.Error() != utils.ErrNoRow() {
  282. return
  283. }
  284. if edbDataInsertConfig != nil {
  285. edbDataInsertConfigDate = edbDataInsertConfig.Date
  286. }
  287. }
  288. //获取指标所有数据
  289. var existCondition string
  290. var existPars []interface{}
  291. existCondition += " AND edb_info_id=? "
  292. existPars = append(existPars, edbInfoId)
  293. if startDate != "" {
  294. existCondition += " AND data_time>=? "
  295. existPars = append(existPars, startDate)
  296. }
  297. existList, err := GetEdbDataByCondition(source, existCondition, existPars)
  298. if err != nil {
  299. return err
  300. }
  301. existMap := make(map[string]*EdbInfoSearchData)
  302. for _, v := range existList {
  303. existMap[v.DataTime] = v
  304. }
  305. addSql := ` INSERT INTO edb_data_gie(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  306. var isAdd bool
  307. dataMap := make(map[string]interface{})
  308. for _, v := range eicDataList {
  309. var value string
  310. if suffix == "GS" {
  311. value = v.GasInStorage
  312. } else if suffix == "C" {
  313. value = v.Consumption
  314. } else if suffix == "CF" {
  315. value = v.ConsumptionFull
  316. } else if suffix == "F" {
  317. value = v.Full
  318. } else if suffix == "T" {
  319. value = v.Trend
  320. } else if suffix == "In" {
  321. value = v.Injection
  322. } else if suffix == "Out" {
  323. value = v.Withdrawal
  324. } else if suffix == "WGV" {
  325. value = v.WorkingGasVolume
  326. } else if suffix == "IC" {
  327. value = v.InjectionCapacity
  328. } else if suffix == "WC" {
  329. value = v.WithdrawalCapacity
  330. }
  331. item := v
  332. itemValue := value
  333. eDate := item.GasDayStart
  334. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  335. if err != nil {
  336. return err
  337. }
  338. if findItem, ok := existMap[v.GasDayStart]; !ok {
  339. sValue := itemValue
  340. if sValue != "" {
  341. timestamp := dataTime.UnixNano() / 1e6
  342. timeStr := fmt.Sprintf("%d", timestamp)
  343. saveValue := sValue
  344. if _, ok := dataMap[eDate]; !ok {
  345. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
  346. isAdd = true
  347. }
  348. }
  349. } else {
  350. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != itemValue {
  351. err = ModifyEdbDataById(source, findItem.EdbDataId, itemValue)
  352. if err != nil {
  353. return err
  354. }
  355. }
  356. }
  357. dataMap[v.GasDayStart] = v.GasDayStart
  358. // 下面代码主要目的是处理掉手动插入的数据判断
  359. {
  360. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  361. realDataMaxDate = dataTime
  362. }
  363. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  364. isFindConfigDateRealData = true
  365. }
  366. }
  367. }
  368. // 处理手工数据补充的配置
  369. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, existMap, isFindConfigDateRealData)
  370. if isAdd {
  371. addSql = strings.TrimRight(addSql, ",")
  372. _, err = o.Raw(addSql).Exec()
  373. if err != nil {
  374. return err
  375. }
  376. }
  377. return
  378. }