base_from_eia_steo.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. package models
  2. import (
  3. "eta/eta_index_lib/utils"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "github.com/beego/beego/v2/client/orm"
  9. )
  10. //钢联
  11. type EiaSteoData struct {
  12. InputValue float64 `orm:"column(value)" description:"值"`
  13. DataTime string `orm:"column(data_time)" description:"日期"`
  14. }
  15. func GetEiaSteoDataByCondition(condition string, pars []interface{}) (item []*EiaSteoData, err error) {
  16. sql1 := ` SELECT * FROM base_from_eia_steo_data WHERE 1=1 `
  17. o := orm.NewOrm()
  18. if condition != "" {
  19. sql1 += condition
  20. }
  21. sql := `select * from (` + sql1 + ` having 1 order by modify_time DESC ) tmp GROUP BY data_time ORDER BY data_time DESC `
  22. _, err = o.Raw(sql, pars).QueryRows(&item)
  23. return
  24. }
  25. // AddEdbDataFromEiaSteo 新增钢联指标数据
  26. func AddEdbDataFromEiaSteo(edbCode string) (err error) {
  27. o := orm.NewOrm()
  28. var condition string
  29. var pars []interface{}
  30. if edbCode != "" {
  31. condition += " AND index_code = ? "
  32. pars = append(pars, edbCode)
  33. }
  34. EiaSteoDataList, err := GetEiaSteoDataByCondition(condition, pars)
  35. if err != nil {
  36. return
  37. }
  38. dataLen := len(EiaSteoDataList)
  39. existMap := make(map[string]string)
  40. if dataLen > 0 {
  41. var isAdd bool
  42. addSql := ` INSERT INTO edb_data_eia_steo (edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  43. for i := 0; i < dataLen; i++ {
  44. item := EiaSteoDataList[i]
  45. eDate := item.DataTime
  46. sValue := utils.SubFloatToString(item.InputValue, 30)
  47. if sValue != "" {
  48. if _, ok := existMap[eDate]; !ok {
  49. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  50. if err != nil {
  51. return err
  52. }
  53. timestamp := dataTime.UnixNano() / 1e6
  54. timeStr := fmt.Sprintf("%d", timestamp)
  55. addSql += GetAddSql("0", edbCode, eDate, timeStr, sValue)
  56. isAdd = true
  57. }
  58. }
  59. existMap[eDate] = eDate
  60. }
  61. if isAdd {
  62. addSql = strings.TrimRight(addSql, ",")
  63. utils.FileLog.Info("addSql:" + addSql)
  64. _, err = o.Raw(addSql).Exec()
  65. if err != nil {
  66. return err
  67. }
  68. }
  69. }
  70. return
  71. }
  72. // RefreshEdbDataFromEiaSteo 刷新钢联指标数据
  73. func RefreshEdbDataFromEiaSteo(edbInfoId int, edbCode, startDate string) (err error) {
  74. source := utils.DATA_SOURCE_EIA_STEO
  75. o := orm.NewOrm()
  76. if err != nil {
  77. return
  78. }
  79. edbInfoIdStr := strconv.Itoa(edbInfoId)
  80. //计算数据
  81. var condition string
  82. var pars []interface{}
  83. if edbCode != "" {
  84. condition += " AND index_code=? "
  85. pars = append(pars, edbCode)
  86. }
  87. if startDate != "" {
  88. condition += " AND data_time>=? "
  89. pars = append(pars, startDate)
  90. }
  91. EiaSteoDataList, err := GetEiaSteoDataByCondition(condition, pars)
  92. if err != nil {
  93. return
  94. }
  95. // 真实数据的最大日期 , 插入规则配置的日期
  96. var realDataMaxDate, edbDataInsertConfigDate time.Time
  97. var edbDataInsertConfig *EdbDataInsertConfig
  98. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  99. {
  100. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  101. if err != nil && err.Error() != utils.ErrNoRow() {
  102. return
  103. }
  104. if edbDataInsertConfig != nil {
  105. edbDataInsertConfigDate = edbDataInsertConfig.Date
  106. }
  107. }
  108. var existCondition string
  109. var existPars []interface{}
  110. existCondition += " AND edb_info_id=? "
  111. existPars = append(existPars, edbInfoId)
  112. if startDate != "" {
  113. existCondition += " AND data_time>=? "
  114. existPars = append(existPars, startDate)
  115. }
  116. //获取指标所有数据
  117. existList, err := GetEdbDataByCondition(source, existCondition, existPars)
  118. if err != nil {
  119. return err
  120. }
  121. existMap := make(map[string]*EdbInfoSearchData)
  122. for _, v := range existList {
  123. existMap[v.DataTime] = v
  124. }
  125. addSql := ` INSERT INTO edb_data_eia_steo(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  126. var isAdd bool
  127. addMap := make(map[string]string)
  128. for _, v := range EiaSteoDataList {
  129. item := v
  130. eDate := item.DataTime
  131. sValue := utils.SubFloatToString(item.InputValue, 30)
  132. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  133. if err != nil {
  134. return err
  135. }
  136. if findItem, ok := existMap[v.DataTime]; !ok {
  137. if sValue != "" {
  138. timestamp := dataTime.UnixNano() / 1e6
  139. timeStr := fmt.Sprintf("%d", timestamp)
  140. saveValue := sValue
  141. if _, addOk := addMap[eDate]; !addOk {
  142. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
  143. isAdd = true
  144. }
  145. }
  146. } else {
  147. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != sValue {
  148. err = ModifyEdbDataById(source, findItem.EdbDataId, sValue)
  149. if err != nil {
  150. return err
  151. }
  152. }
  153. }
  154. addMap[v.DataTime] = v.DataTime
  155. // 下面代码主要目的是处理掉手动插入的数据判断
  156. {
  157. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  158. realDataMaxDate = dataTime
  159. }
  160. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  161. isFindConfigDateRealData = true
  162. }
  163. }
  164. }
  165. // 处理手工数据补充的配置
  166. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, existMap, isFindConfigDateRealData)
  167. if isAdd {
  168. addSql = strings.TrimRight(addSql, ",")
  169. _, err = o.Raw(addSql).Exec()
  170. if err != nil {
  171. return err
  172. }
  173. }
  174. return
  175. }