base_from_national_statistics.go 5.2 KB

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