base_from_gie.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. package models
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hongze_edb_lib/utils"
  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. func GetBaseFromEicDataAllByIndexCode(indexCode, suffix string) (list []*BaseFromTradeEicIndex, err error) {
  40. o := orm.NewOrm()
  41. var name string
  42. if suffix == "" {
  43. name = "eic_code"
  44. } else if suffix == "GS" {
  45. name = "gas_in_storage_code"
  46. } else if suffix == "F" {
  47. name = "full_code"
  48. } else if suffix == "T" {
  49. name = "trend_code"
  50. } else if suffix == "In" {
  51. name = "injection_code"
  52. } else if suffix == "Out" {
  53. name = "withdrawal_code"
  54. } else if suffix == "WGV" {
  55. name = "working_gas_volume_code"
  56. } else if suffix == "IC" {
  57. name = "injection_capacity_code"
  58. } else if suffix == "WC" {
  59. name = "withdrawal_capacity_code"
  60. }
  61. sql := `SELECT * FROM base_from_trade_eic_index WHERE %s=? `
  62. sql = fmt.Sprintf(sql, name)
  63. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  64. return
  65. }
  66. func GetGieDataByTradeCode(condition string, pars []interface{}) (item []*BaseFromTradeEicIndex, err error) {
  67. sql := ` SELECT * FROM base_from_trade_eic_index WHERE 1=1 `
  68. o := orm.NewOrm()
  69. if condition != "" {
  70. sql += condition
  71. }
  72. sql += ` ORDER BY gas_day_started_on DESC `
  73. fmt.Println(sql, pars)
  74. _, err = o.Raw(sql, pars).QueryRows(&item)
  75. return
  76. }
  77. //新增上期能源指标数据
  78. func AddEdbDataFromGie(edbCode string) (err error) {
  79. var suffix string
  80. l := len(edbCode)
  81. if strings.Contains(edbCode[l-2:], "GS") {
  82. suffix = "GS"
  83. } else if strings.Contains(edbCode[l-1:], "F") {
  84. suffix = "F"
  85. } else if strings.Contains(edbCode[l-1:], "T") {
  86. suffix = "T"
  87. } else if strings.Contains(edbCode[l-2:], "In") {
  88. suffix = "In"
  89. } else if strings.Contains(edbCode[l-3:], "Out") {
  90. suffix = "Out"
  91. } else if strings.Contains(edbCode[l-3:], "WGV") {
  92. suffix = "WGV"
  93. } else if strings.Contains(edbCode[l-2:], "IC") {
  94. suffix = "IC"
  95. } else if strings.Contains(edbCode[l-2:], "WC") {
  96. suffix = "WC"
  97. } else {
  98. suffix = ""
  99. }
  100. o := orm.NewOrm()
  101. eicBaseDataAll, err := GetBaseFromEicDataAllByIndexCode(edbCode, suffix)
  102. if err != nil && err.Error() != utils.ErrNoRow() {
  103. fmt.Println("GetBaseFromEicDataAllByIndexCode err:", err)
  104. return
  105. }
  106. var isAdd bool
  107. addSql := ` INSERT INTO edb_data_gie(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  108. existMap := make(map[string]string)
  109. for _, sv := range eicBaseDataAll {
  110. eDate := sv.GasDayStartedOn
  111. dataTime, err := time.Parse(utils.FormatDate, eDate)
  112. if err != nil {
  113. fmt.Println("time.Parse Err:" + eDate)
  114. return err
  115. }
  116. timestamp := dataTime.UnixNano() / 1e6
  117. timeStr := fmt.Sprintf("%d", timestamp)
  118. //var name string
  119. if _, ok := existMap[eDate]; !ok {
  120. if suffix == "GS" {
  121. //name = "gas_in_storage"
  122. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.GasInStorage)
  123. existMap[eDate] = sv.GasInStorage
  124. } else if suffix == "F" {
  125. //name = "full"
  126. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.Full)
  127. existMap[eDate] = sv.Full
  128. } else if suffix == "T" {
  129. //name = "trend"
  130. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.Trend)
  131. existMap[eDate] = sv.Trend
  132. } else if suffix == "In" {
  133. //name = "injection"
  134. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.Injection)
  135. existMap[eDate] = sv.Injection
  136. } else if suffix == "Out" {
  137. //name = "withdrawal"
  138. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.Withdrawal)
  139. existMap[eDate] = sv.Withdrawal
  140. } else if suffix == "WGV" {
  141. //name = "working_gas_volume"
  142. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.WorkingGasVolume)
  143. existMap[eDate] = sv.WorkingGasVolume
  144. } else if suffix == "IC" {
  145. //name = "injection_capacity"
  146. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.InjectionCapacity)
  147. existMap[eDate] = sv.InjectionCapacity
  148. } else if suffix == "WC" {
  149. //name = "withdrawal_capacity"
  150. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.WithdrawalCapacity)
  151. existMap[eDate] = sv.WithdrawalCapacity
  152. }
  153. isAdd = true
  154. }
  155. }
  156. if isAdd {
  157. addSql = strings.TrimRight(addSql, ",")
  158. utils.FileLog.Info("addSql:" + addSql)
  159. _, err = o.Raw(addSql).Exec()
  160. if err != nil {
  161. fmt.Println("addSql err:", err)
  162. return err
  163. }
  164. }
  165. return
  166. }
  167. //刷新欧洲天然气指标数据
  168. func RefreshEdbDataFromGie(edbInfoId int, edbCode, startDate string) (err error) {
  169. source := utils.DATA_SOURCE_GIE
  170. o := orm.NewOrm()
  171. if err != nil {
  172. return
  173. }
  174. var suffix string
  175. l := len(edbCode)
  176. if strings.Contains(edbCode[l-2:], "GS") {
  177. suffix = "GS"
  178. } else if strings.Contains(edbCode[l-1:], "F") {
  179. suffix = "F"
  180. } else if strings.Contains(edbCode[l-1:], "T") {
  181. suffix = "T"
  182. } else if strings.Contains(edbCode[l-2:], "In") {
  183. suffix = "In"
  184. } else if strings.Contains(edbCode[l-3:], "Out") {
  185. suffix = "Out"
  186. } else if strings.Contains(edbCode[l-3:], "WGV") {
  187. suffix = "WGV"
  188. } else if strings.Contains(edbCode[l-2:], "IC") {
  189. suffix = "IC"
  190. } else if strings.Contains(edbCode[l-2:], "WC") {
  191. suffix = "WC"
  192. } else {
  193. suffix = ""
  194. }
  195. edbInfoIdStr := strconv.Itoa(edbInfoId)
  196. //计算数据
  197. var condition string
  198. var pars []interface{}
  199. if edbCode != "" {
  200. condition += " AND eic_code=? "
  201. pars = append(pars, edbCode[:l-len(suffix)])
  202. }
  203. if startDate != "" {
  204. condition += " AND gas_day_started_on>=? "
  205. pars = append(pars, startDate)
  206. }
  207. eicDataList, err := GetGieDataByTradeCode(condition, pars)
  208. fmt.Println("all eicDataList", len(eicDataList))
  209. //获取指标所有数据
  210. var existCondition string
  211. var existPars []interface{}
  212. existCondition += " AND edb_info_id=? "
  213. existPars = append(existPars, edbInfoId)
  214. if startDate != "" {
  215. existCondition += " AND data_time>=? "
  216. existPars = append(existPars, startDate)
  217. }
  218. existList, err := GetEdbDataByCondition(source, existCondition, existPars)
  219. if err != nil {
  220. return err
  221. }
  222. existMap := make(map[string]*EdbInfoSearchData)
  223. for _, v := range existList {
  224. existMap[v.DataTime] = v
  225. }
  226. addSql := ` INSERT INTO edb_data_gie(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  227. var isAdd bool
  228. dataMap := make(map[string]interface{})
  229. for _, v := range eicDataList {
  230. var value string
  231. if suffix == "GS" {
  232. value = v.GasInStorage
  233. } else if suffix == "F" {
  234. value = v.Full
  235. } else if suffix == "T" {
  236. value = v.Trend
  237. } else if suffix == "In" {
  238. value = v.Injection
  239. } else if suffix == "Out" {
  240. value = v.Withdrawal
  241. } else if suffix == "WGV" {
  242. value = v.WorkingGasVolume
  243. } else if suffix == "IC" {
  244. value = v.InjectionCapacity
  245. } else if suffix == "WC" {
  246. value = v.WithdrawalCapacity
  247. }
  248. item := v
  249. itemValue := value
  250. if findItem, ok := existMap[v.GasDayStartedOn]; !ok {
  251. eDate := item.GasDayStartedOn
  252. sValue := itemValue
  253. if sValue != "" {
  254. dataTime, err := time.Parse(utils.FormatDate, eDate)
  255. if err != nil {
  256. return err
  257. }
  258. timestamp := dataTime.UnixNano() / 1e6
  259. timeStr := fmt.Sprintf("%d", timestamp)
  260. saveValue := sValue
  261. if _, ok := dataMap[eDate]; !ok {
  262. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
  263. isAdd = true
  264. }
  265. }
  266. } else {
  267. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != itemValue {
  268. err = ModifyEdbDataById(source, findItem.EdbDataId, itemValue)
  269. if err != nil {
  270. return err
  271. }
  272. }
  273. }
  274. dataMap[v.GasDayStartedOn] = v.GasDayStartedOn
  275. }
  276. if isAdd {
  277. addSql = strings.TrimRight(addSql, ",")
  278. _, err = o.Raw(addSql).Exec()
  279. if err != nil {
  280. return err
  281. }
  282. }
  283. return
  284. }