base_from_eia_steo.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. subSource := utils.DATA_SUB_SOURCE_EDB
  76. o := orm.NewOrm()
  77. if err != nil {
  78. return
  79. }
  80. edbInfoIdStr := strconv.Itoa(edbInfoId)
  81. //计算数据
  82. var condition string
  83. var pars []interface{}
  84. if edbCode != "" {
  85. condition += " AND index_code=? "
  86. pars = append(pars, edbCode)
  87. }
  88. if startDate != "" {
  89. condition += " AND data_time>=? "
  90. pars = append(pars, startDate)
  91. }
  92. EiaSteoDataList, err := GetEiaSteoDataByCondition(condition, pars)
  93. if err != nil {
  94. return
  95. }
  96. // 真实数据的最大日期 , 插入规则配置的日期
  97. var realDataMaxDate, edbDataInsertConfigDate time.Time
  98. var edbDataInsertConfig *EdbDataInsertConfig
  99. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  100. {
  101. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  102. if err != nil && err.Error() != utils.ErrNoRow() {
  103. return
  104. }
  105. if edbDataInsertConfig != nil {
  106. edbDataInsertConfigDate = edbDataInsertConfig.Date
  107. }
  108. }
  109. var existCondition string
  110. var existPars []interface{}
  111. existCondition += " AND edb_info_id=? "
  112. existPars = append(existPars, edbInfoId)
  113. if startDate != "" {
  114. existCondition += " AND data_time>=? "
  115. existPars = append(existPars, startDate)
  116. }
  117. //获取指标所有数据
  118. existList, err := GetEdbDataByCondition(source, subSource, existCondition, existPars)
  119. if err != nil {
  120. return err
  121. }
  122. existMap := make(map[string]*EdbInfoSearchData)
  123. for _, v := range existList {
  124. existMap[v.DataTime] = v
  125. }
  126. addSql := ` INSERT INTO edb_data_eia_steo(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  127. var isAdd bool
  128. addMap := make(map[string]string)
  129. for _, v := range EiaSteoDataList {
  130. item := v
  131. eDate := item.DataTime
  132. sValue := utils.SubFloatToString(item.InputValue, 30)
  133. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  134. if err != nil {
  135. return err
  136. }
  137. if findItem, ok := existMap[v.DataTime]; !ok {
  138. if sValue != "" {
  139. timestamp := dataTime.UnixNano() / 1e6
  140. timeStr := fmt.Sprintf("%d", timestamp)
  141. saveValue := sValue
  142. if _, addOk := addMap[eDate]; !addOk {
  143. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
  144. isAdd = true
  145. }
  146. }
  147. } else {
  148. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != sValue {
  149. err = ModifyEdbDataById(source, subSource, findItem.EdbDataId, sValue)
  150. if err != nil {
  151. return err
  152. }
  153. }
  154. }
  155. addMap[v.DataTime] = v.DataTime
  156. // 下面代码主要目的是处理掉手动插入的数据判断
  157. {
  158. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  159. realDataMaxDate = dataTime
  160. }
  161. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  162. isFindConfigDateRealData = true
  163. }
  164. }
  165. }
  166. // 处理手工数据补充的配置
  167. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, subSource, existMap, isFindConfigDateRealData)
  168. if isAdd {
  169. addSql = strings.TrimRight(addSql, ",")
  170. _, err = o.Raw(addSql).Exec()
  171. if err != nil {
  172. return err
  173. }
  174. }
  175. return
  176. }