base_from_icpi.go 6.2 KB

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