base_from_eia_steo.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package models
  2. import (
  3. "eta/eta_crawler/utils"
  4. "time"
  5. "github.com/beego/beego/v2/client/orm"
  6. "github.com/shopspring/decimal"
  7. )
  8. // BaseFromEiaSteoIndex EiaSteo指标
  9. type BaseFromEiaSteoIndex struct {
  10. BaseFromEiaSteoIndexId int `orm:"column(base_from_eia_steo_index_id);pk"`
  11. BaseFromEiaSteoClassifyId int `description:"指标分类id"`
  12. IndexCode string `description:"指标编码"`
  13. IndexName string `description:"指标名称(中文名称)"`
  14. IndexNameOriginal string `description:"指标名称(原始名称)"`
  15. Frequency string `description:"频度"`
  16. Level int `description:"指标层级(原始层级)"`
  17. Unit string `description:"单位"`
  18. Super string `description:"我也不知道是个啥,反正先存起来,万一用到了呢"`
  19. Precision int `description:"精度,我也不知道会不会用到,反正先存起来,万一用到了呢"`
  20. LastHistorical string `description:"最近的历史记录,我也不知道会不会用到,反正先存起来,万一用到了呢"`
  21. Description string `description:"备注信息,我也不知道会不会用到,反正先存起来,万一用到了呢"`
  22. IsMappable int `description:"是否可映射,我也不知道会不会用到,反正先存起来,万一用到了呢"`
  23. StartDate time.Time `description:"开始日期"`
  24. EndDate time.Time `description:"结束日期"`
  25. ModifyTime time.Time `description:"最新更新时间"`
  26. CreateTime time.Time `description:"创建时间"`
  27. }
  28. // Add 新增指标
  29. func (item *BaseFromEiaSteoIndex) Add() (err error) {
  30. o := orm.NewOrmUsingDB("data")
  31. lastId, err := o.Insert(item)
  32. if err != nil {
  33. return
  34. }
  35. item.BaseFromEiaSteoIndexId = int(lastId)
  36. return
  37. }
  38. // Update 更新作者基础信息
  39. func (item *BaseFromEiaSteoIndex) Update(cols []string) (err error) {
  40. o := orm.NewOrmUsingDB("data")
  41. _, err = o.Update(item, cols...)
  42. return
  43. }
  44. // BaseFromEiaSteoClassify EiaSteo分类
  45. type BaseFromEiaSteoClassify struct {
  46. BaseFromEiaSteoClassifyId int `orm:"column(base_from_eia_steo_classify_id);pk"`
  47. ClassifyName string `description:"分类名称(中文名称)"`
  48. ClassifyNameOriginal string `description:"分类名称(原始名称)"`
  49. ParentId int `description:"父级id"`
  50. Level int `description:"层级"`
  51. ModifyTime time.Time `description:"最新更新时间"`
  52. CreateTime time.Time `description:"创建时间"`
  53. }
  54. // 新增分类
  55. func (item *BaseFromEiaSteoClassify) AddBaseFromEiaSteoClassify() (err error) {
  56. o := orm.NewOrmUsingDB("data")
  57. lastId, err := o.Insert(item)
  58. if err != nil {
  59. return
  60. }
  61. item.BaseFromEiaSteoClassifyId = int(lastId)
  62. return
  63. }
  64. // BaseFromEiaSteoData EiaSteo数据表
  65. type BaseFromEiaSteoData struct {
  66. BaseFromEiaSteoDataId int `orm:"column(base_from_eia_steo_data_id);pk"`
  67. BaseFromEiaSteoIndexId int `description:"指标id"`
  68. IndexCode string `description:"指标编码"`
  69. DataTime time.Time `description:"数据日期"`
  70. Value float64 `description:"数据值"`
  71. ModifyTime time.Time `description:"最新更新时间"`
  72. CreateTime time.Time `description:"创建时间"`
  73. }
  74. // GetBaseFromEiaSteoClassifyAll 获取所有的分类
  75. func GetBaseFromEiaSteoClassifyAll() (list []*BaseFromEiaSteoClassify, err error) {
  76. o := orm.NewOrmUsingDB("data")
  77. sql := `SELECT * FROM base_from_eia_steo_classify`
  78. _, err = o.Raw(sql).QueryRows(&list)
  79. return
  80. }
  81. func GetChildBaseFromEiaSteoClassifyById(classifyId int) (list []*BaseFromEiaSteoClassify, err error) {
  82. o := orm.NewOrmUsingDB("data")
  83. sql := `SELECT * FROM base_from_eia_steo_classify WHERE parent_id = ?`
  84. _, err = o.Raw(sql, classifyId).QueryRows(&list)
  85. return
  86. }
  87. func GetBaseFromEiaSteoClassifyByName(classifyName string) (item *BaseFromEiaSteoClassify, err error) {
  88. o := orm.NewOrmUsingDB("data")
  89. sql := `SELECT * FROM base_from_eia_steo_classify WHERE classify_name = ?`
  90. err = o.Raw(sql, classifyName).QueryRow(&item)
  91. return
  92. }
  93. // GetBaseFromEiaSteoIndexAll 获取所有的指标
  94. func GetBaseFromEiaSteoIndexAll() (list []*BaseFromEiaSteoIndex, err error) {
  95. o := orm.NewOrmUsingDB("data")
  96. sql := `SELECT * FROM base_from_eia_steo_index`
  97. _, err = o.Raw(sql).QueryRows(&list)
  98. return
  99. }
  100. func lastDayOfMonth(year int, month time.Month) string {
  101. firstDay := time.Date(year, month, 1, 0, 0, 0, 0, time.Local)
  102. nextMonth := firstDay.AddDate(0, 1, 0)
  103. return nextMonth.Add(-time.Second * 1).Format(utils.FormatDate)
  104. }
  105. // HandleEiaSteoData 数据处理
  106. func HandleEiaSteoData(dataList map[string]interface{}, indexInfo *BaseFromEiaSteoIndex) (err error) {
  107. o := orm.NewOrmUsingDB("data")
  108. to, err := o.Begin()
  109. if err != nil {
  110. return
  111. }
  112. defer func() {
  113. if err != nil {
  114. to.Rollback()
  115. } else {
  116. to.Commit()
  117. }
  118. }()
  119. var existList []*BaseFromEiaSteoData
  120. sql := `SELECT * FROM base_from_eia_steo_data WHERE index_code = ? order by data_time asc`
  121. _, err = to.Raw(sql, indexInfo.IndexCode).QueryRows(&existList)
  122. if err != nil {
  123. return err
  124. }
  125. existMap := make(map[string]*BaseFromEiaSteoData)
  126. for _, v := range existList {
  127. // 产品要求每个月的最后一天,为了兼容之前每个月第一天的数据
  128. dateTime := lastDayOfMonth(v.DataTime.Year(), v.DataTime.Month())
  129. existMap[dateTime] = v
  130. }
  131. // 待添加的数据列表
  132. addBaseFromEiaSteoDataList := make([]*BaseFromEiaSteoData, 0)
  133. endDate := indexInfo.EndDate
  134. startDate := indexInfo.StartDate
  135. for date, v := range dataList {
  136. // 实际数据
  137. val := v.(float64)
  138. valDecimal := decimal.NewFromFloat(val).Round(4)
  139. val, _ = valDecimal.Float64()
  140. currDate, tmpErr := time.ParseInLocation(utils.FormatMonthDateUnSpace, date, time.Local)
  141. if tmpErr != nil {
  142. err = tmpErr
  143. return
  144. }
  145. initDate := time.Date(2010, time.January, 1, 0, 0, 0, 0, time.Local)
  146. if currDate.Before(initDate) {
  147. continue
  148. }
  149. if currDate.After(endDate) {
  150. endDate = currDate
  151. }
  152. if currDate.Before(startDate) {
  153. startDate = currDate
  154. }
  155. // 产品要求每个月的最后一天
  156. date = lastDayOfMonth(currDate.Year(), currDate.Month())
  157. currDate, _ = time.ParseInLocation(utils.FormatDate, date, time.Local)
  158. // date = currDate.Format(utils.FormatDate)
  159. //fmt.Println("date:", date)
  160. tmpBaseFromEiaSteoData, ok := existMap[date]
  161. if !ok {
  162. addBaseFromEiaSteoDataList = append(addBaseFromEiaSteoDataList, &BaseFromEiaSteoData{
  163. //BaseFromEiaSteoDataId: 0,
  164. BaseFromEiaSteoIndexId: indexInfo.BaseFromEiaSteoIndexId,
  165. IndexCode: indexInfo.IndexCode,
  166. DataTime: currDate,
  167. Value: val,
  168. ModifyTime: time.Now(),
  169. CreateTime: time.Now(),
  170. })
  171. } else {
  172. nowVal := decimal.NewFromFloat(tmpBaseFromEiaSteoData.Value)
  173. //fmt.Println(date, "=========", nowVal.Equal(valDecimal))
  174. var updateCols []string
  175. if !nowVal.Equal(valDecimal) {
  176. tmpBaseFromEiaSteoData.Value = val
  177. tmpBaseFromEiaSteoData.ModifyTime = time.Now()
  178. updateCols = append(updateCols, "value")
  179. updateCols = append(updateCols, "modify_time")
  180. }
  181. if len(updateCols) > 0 {
  182. _, err = to.Update(tmpBaseFromEiaSteoData, updateCols...)
  183. if err != nil {
  184. return
  185. }
  186. }
  187. }
  188. delete(existMap, date)
  189. }
  190. lenAddBaseFromEiaSteoDataList := len(addBaseFromEiaSteoDataList)
  191. if lenAddBaseFromEiaSteoDataList > 0 {
  192. _, err = to.InsertMulti(lenAddBaseFromEiaSteoDataList, addBaseFromEiaSteoDataList)
  193. }
  194. existMapNum := len(existMap)
  195. if existMapNum > 0 {
  196. idList := make([]int, 0)
  197. for _, v := range existMap {
  198. idList = append(idList, v.BaseFromEiaSteoDataId)
  199. }
  200. sql = `DELETE FROM base_from_eia_steo_data WHERE base_from_eia_steo_data_id in(` + utils.GetOrmInReplace(existMapNum) + `) `
  201. _, err = to.Raw(sql, idList).Exec()
  202. if err != nil {
  203. return err
  204. }
  205. }
  206. // 更新指标信息
  207. {
  208. updateParams := make([]string, 0)
  209. indexInfo.ModifyTime = time.Now()
  210. updateParams = append(updateParams, "ModifyTime")
  211. if endDate.After(indexInfo.EndDate) {
  212. indexInfo.EndDate = endDate
  213. updateParams = append(updateParams, "EndDate")
  214. }
  215. if startDate.Before(indexInfo.StartDate) {
  216. indexInfo.StartDate = startDate
  217. updateParams = append(updateParams, "StartDate")
  218. }
  219. _, err = to.Update(indexInfo, updateParams...)
  220. if err != nil {
  221. return
  222. }
  223. }
  224. return
  225. }