base_from_national_statistics.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. // BaseFromNationalStatisticsData 国家统计局-指标数据表
  11. type BaseFromNationalStatisticsData struct {
  12. BaseFromNationalStatisticsDataId int `orm:"column(base_from_national_statistics_data_id);pk"`
  13. BaseFromNationalStatisticsIndexId int
  14. IndexCode string
  15. DataTime string
  16. Value string
  17. CreateTime time.Time
  18. ModifyTime time.Time
  19. }
  20. // GetBaseFromNationalStatisticsDataByCondition 获取指标值
  21. func GetBaseFromNationalStatisticsDataByCondition(condition string, pars []interface{}) (list []*BaseFromNationalStatisticsData, err error) {
  22. o := orm.NewOrm()
  23. sql := `SELECT * FROM base_from_national_statistics_data WHERE 1=1 `
  24. if condition != "" {
  25. sql += condition
  26. }
  27. _, err = o.Raw(sql, pars).QueryRows(&list)
  28. return
  29. }
  30. // AddEdbDataFromNationalStatistics 新增国家统计局指标数据
  31. func AddEdbDataFromNationalStatistics(edbCode string) (err error) {
  32. o := orm.NewOrm()
  33. var condition string
  34. var pars []interface{}
  35. if edbCode != "" {
  36. condition += ` AND index_code = ? `
  37. pars = append(pars, edbCode)
  38. }
  39. dataList, err := GetBaseFromNationalStatisticsDataByCondition(condition, pars)
  40. if err != nil && err.Error() != utils.ErrNoRow() {
  41. return
  42. }
  43. var isAdd bool
  44. addSql := `INSERT INTO edb_data_national_statistics(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  45. existMap := make(map[string]string)
  46. for _, sv := range dataList {
  47. eDate := sv.DataTime
  48. dataTime, err := time.Parse(utils.FormatDate, eDate)
  49. if err != nil {
  50. return err
  51. }
  52. timestamp := dataTime.UnixNano() / 1e6
  53. timeStr := fmt.Sprintf("%d", timestamp)
  54. if _, ok := existMap[eDate]; !ok {
  55. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.Value)
  56. isAdd = true
  57. }
  58. existMap[eDate] = sv.Value
  59. }
  60. if isAdd {
  61. addSql = strings.TrimRight(addSql, ",")
  62. utils.FileLog.Info("addSql:" + addSql)
  63. _, err = o.Raw(addSql).Exec()
  64. if err != nil {
  65. return err
  66. }
  67. }
  68. return
  69. }
  70. // RefreshEdbDataFromNationalStatistics 刷新国家统计局指标数据
  71. func RefreshEdbDataFromNationalStatistics(edbInfoId int, edbCode, startDate string) (err error) {
  72. source := utils.DATA_SOURCE_NATIONAL_STATISTICS
  73. o := orm.NewOrm()
  74. if err != nil {
  75. return
  76. }
  77. edbInfoIdStr := strconv.Itoa(edbInfoId)
  78. var condition string
  79. var pars []interface{}
  80. if edbCode != "" {
  81. condition += ` AND index_code = ? `
  82. pars = append(pars, edbCode)
  83. }
  84. if startDate != "" {
  85. condition += ` AND data_time >= ? `
  86. pars = append(pars, startDate)
  87. }
  88. dataList, err := GetBaseFromNationalStatisticsDataByCondition(condition, pars)
  89. if err != nil {
  90. return
  91. }
  92. // 真实数据的最大日期 , 插入规则配置的日期
  93. var realDataMaxDate, edbDataInsertConfigDate time.Time
  94. var edbDataInsertConfig *EdbDataInsertConfig
  95. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  96. {
  97. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  98. if err != nil && err.Error() != utils.ErrNoRow() {
  99. return
  100. }
  101. if edbDataInsertConfig != nil {
  102. edbDataInsertConfigDate = edbDataInsertConfig.Date
  103. }
  104. }
  105. var existCondition string
  106. var existPars []interface{}
  107. existCondition += ` AND edb_info_id = ? `
  108. existPars = append(existPars, edbInfoId)
  109. if startDate != "" {
  110. existCondition += ` AND data_time >= ? `
  111. existPars = append(existPars, startDate)
  112. }
  113. existList, err := GetEdbDataByCondition(source, existCondition, existPars)
  114. if err != nil {
  115. return err
  116. }
  117. existMap := make(map[string]*EdbInfoSearchData)
  118. for _, v := range existList {
  119. existMap[v.DataTime] = v
  120. }
  121. addSql := ` INSERT INTO edb_data_national_statistics(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  122. var isAdd bool
  123. for _, v := range dataList {
  124. item := v
  125. eDate := item.DataTime
  126. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  127. if err != nil {
  128. return err
  129. }
  130. if findItem, ok := existMap[v.DataTime]; !ok {
  131. sValue := item.Value
  132. timestamp := dataTime.UnixNano() / 1e6
  133. timeStr := fmt.Sprintf("%d", timestamp)
  134. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, sValue)
  135. isAdd = true
  136. } else {
  137. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != item.Value {
  138. err = ModifyEdbDataById(source, findItem.EdbDataId, item.Value)
  139. if err != nil {
  140. return err
  141. }
  142. }
  143. }
  144. // 下面代码主要目的是处理掉手动插入的数据判断
  145. {
  146. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  147. realDataMaxDate = dataTime
  148. }
  149. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  150. isFindConfigDateRealData = true
  151. }
  152. }
  153. }
  154. // 处理手工数据补充的配置
  155. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, existMap, isFindConfigDateRealData)
  156. if isAdd {
  157. addSql = strings.TrimRight(addSql, ",")
  158. _, err = o.Raw(addSql).Exec()
  159. if err != nil {
  160. fmt.Println("RefreshEdbDataFromNationalStatistics add Err", err.Error())
  161. return
  162. }
  163. }
  164. return
  165. }