base_from_icpi.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. type BaseFromIcpiIndex struct {
  11. BaseFromIcpiIndexId int `orm:"column(base_from_icpi_index_id);pk"`
  12. BaseFromIcpiClassifyId int `description:"分类id"`
  13. IndexCode string `description:"指标编码"`
  14. IndexName string `description:"指标名称"`
  15. Frequency string `description:"频度"`
  16. StartDate time.Time `description:"开始日期"`
  17. EndDate time.Time `description:"结束日期"`
  18. CreateTime time.Time `description:"创建时间"`
  19. ModifyTime time.Time `description:"修改时间"`
  20. }
  21. type BaseFromIcpiData struct {
  22. BaseFromIcpiDataId int `orm:"column(base_from_icpi_data_id);pk"`
  23. BaseFromIcpiIndexId int `description:"指标id"`
  24. IndexCode string `description:"指标编码"`
  25. DataTime string `description:"日期"`
  26. Value string `description:"值"`
  27. CreateTime time.Time `description:"创建时间"`
  28. ModifyTime time.Time `description:"修改时间"`
  29. }
  30. type BaseFromIcpiClassify struct {
  31. BaseFromIcpiClassifyId int `orm:"column(base_from_icpi_classify_id);pk"`
  32. ClassifyName string `description:"分类名称"`
  33. ClassifyNameEn string `description:"英文名称"`
  34. ParentId int `description:"上级id"`
  35. CreateTime time.Time `description:"创建时间"`
  36. ModifyTime time.Time `description:"修改时间"`
  37. }
  38. func GetBaseFromIcpiDataByIndexCode(indexCode string) (items []*BaseFromIcpiData, err error) {
  39. o := orm.NewOrm()
  40. sql := `SELECT * FROM base_from_icpi_data WHERE index_code=? `
  41. _, err = o.Raw(sql, indexCode).QueryRows(&items)
  42. return
  43. }
  44. func GetBaseFromIcpiDataDataByCondition(condition string, pars []interface{}) (item []*BaseFromIcpiData, err error) {
  45. sql := ` SELECT * FROM base_from_icpi_data WHERE 1=1 `
  46. o := orm.NewOrm()
  47. if condition != "" {
  48. sql += condition
  49. }
  50. sql += ` ORDER BY data_time DESC `
  51. _, err = o.Raw(sql, pars).QueryRows(&item)
  52. return
  53. }
  54. // 新增广期所指标数据
  55. func AddEdbDataFromIcpi(edbCode string) (err error) {
  56. o := orm.NewOrm()
  57. dataAll, err := GetBaseFromIcpiDataByIndexCode(edbCode)
  58. if err != nil && err.Error() != utils.ErrNoRow() {
  59. return
  60. }
  61. var isAdd bool
  62. addSql := ` INSERT INTO edb_data_icpi(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  63. existMap := make(map[string]string)
  64. for _, sv := range dataAll {
  65. eDate := sv.DataTime
  66. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  67. if err != nil {
  68. fmt.Println("time.Parse Err:" + eDate)
  69. return err
  70. }
  71. timestamp := dataTime.UnixNano() / 1e6
  72. timeStr := fmt.Sprintf("%d", timestamp)
  73. if _, ok := existMap[eDate]; !ok {
  74. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.Value)
  75. isAdd = true
  76. }
  77. }
  78. if isAdd {
  79. addSql = strings.TrimRight(addSql, ",")
  80. utils.FileLog.Info("addSql:" + addSql)
  81. _, err = o.Raw(addSql).Exec()
  82. if err != nil {
  83. return err
  84. }
  85. }
  86. return
  87. }
  88. // 刷新广期所指标数据
  89. func RefreshEdbDataFromIcpi(edbInfoId int, edbCode, startDate string) (err error) {
  90. source := utils.DATA_SOURCE_ICPI
  91. subSource := utils.DATA_SUB_SOURCE_EDB
  92. o := orm.NewOrm()
  93. if err != nil {
  94. return
  95. }
  96. edbInfoIdStr := strconv.Itoa(edbInfoId)
  97. //计算数据
  98. var condition string
  99. var pars []interface{}
  100. if edbCode != "" {
  101. condition += " AND index_code=? "
  102. pars = append(pars, edbCode)
  103. }
  104. if startDate != "" {
  105. condition += " AND data_time>=? "
  106. pars = append(pars, startDate)
  107. }
  108. dataList, err := GetBaseFromIcpiDataDataByCondition(condition, pars)
  109. if err != nil {
  110. return
  111. }
  112. // 真实数据的最大日期 , 插入规则配置的日期
  113. var realDataMaxDate, edbDataInsertConfigDate time.Time
  114. var edbDataInsertConfig *EdbDataInsertConfig
  115. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  116. {
  117. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  118. if err != nil && err.Error() != utils.ErrNoRow() {
  119. return
  120. }
  121. if edbDataInsertConfig != nil {
  122. edbDataInsertConfigDate = edbDataInsertConfig.Date
  123. }
  124. }
  125. //获取指标所有数据
  126. var existCondition string
  127. var existPars []interface{}
  128. existCondition += " AND edb_info_id=? "
  129. existPars = append(existPars, edbInfoId)
  130. if startDate != "" {
  131. existCondition += " AND data_time>=? "
  132. existPars = append(existPars, startDate)
  133. }
  134. existList, err := GetEdbDataByCondition(source, subSource, existCondition, existPars)
  135. if err != nil {
  136. return err
  137. }
  138. existMap := make(map[string]*EdbInfoSearchData)
  139. for _, v := range existList {
  140. existMap[v.DataTime] = v
  141. }
  142. addSql := ` INSERT INTO edb_data_icpi(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  143. var isAdd bool
  144. for _, v := range dataList {
  145. item := v
  146. itemValue := v.Value
  147. eDate := item.DataTime
  148. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  149. if err != nil {
  150. return err
  151. }
  152. if _, ok := existMap[v.DataTime]; !ok {
  153. sValue := itemValue
  154. if sValue != "" {
  155. timestamp := dataTime.UnixNano() / 1e6
  156. timeStr := fmt.Sprintf("%d", timestamp)
  157. saveValue := sValue
  158. if findItem, ok := existMap[eDate]; !ok {
  159. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
  160. isAdd = true
  161. } else {
  162. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != sValue {
  163. err = ModifyEdbDataById(source, subSource, findItem.EdbDataId, sValue)
  164. if err != nil {
  165. return err
  166. }
  167. }
  168. }
  169. }
  170. }
  171. // 下面代码主要目的是处理掉手动插入的数据判断
  172. {
  173. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  174. realDataMaxDate = dataTime
  175. }
  176. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  177. isFindConfigDateRealData = true
  178. }
  179. }
  180. }
  181. // 处理手工数据补充的配置
  182. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, subSource, existMap, isFindConfigDateRealData)
  183. if isAdd {
  184. addSql = strings.TrimRight(addSql, ",")
  185. _, err = o.Raw(addSql).Exec()
  186. if err != nil {
  187. return err
  188. }
  189. }
  190. return
  191. }