base_from_icpi.go 7.2 KB

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