base_from_sci99.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. // BaseFromSci99Index 代表卓创资讯-原始指标表的结构
  11. type BaseFromSci99Index struct {
  12. BaseFromSciIndexId int `orm:"column(base_from_sci_index_id);pk"` // 主键,自动递增
  13. IndexCode string // 指标编码
  14. IndexName string // 指标名称
  15. ClassifyId int // 分类Id
  16. Unit string // 单位
  17. Frequency string // 频度
  18. Describe string // 指标描述
  19. CreateTime time.Time // 创建时间
  20. ModifyTime time.Time // 修改时间
  21. }
  22. // BaseFromSci99Data 代表卓创资讯-原始指标数据表的结构
  23. type BaseFromSci99DataItem struct {
  24. BaseFromSciDataId int `orm:"column(base_from_sci_data_id);pk"` // 主键,自动递增
  25. BaseFromSciIndexId int // 指标id
  26. IndexCode string // 指标编码
  27. DataTime string // 数据日期
  28. Value string // 数据值
  29. CreateTime time.Time // 创建时间
  30. ModifyTime time.Time // 修改时间
  31. }
  32. // BaseFromSci99Data 代表卓创资讯-原始指标数据表的结构
  33. type BaseFromSci99Data struct {
  34. BaseFromSciDataId int `orm:"column(base_from_sci_data_id);pk"` // 主键,自动递增
  35. BaseFromSciIndexId int // 指标id
  36. IndexCode string // 指标编码
  37. DataTime string // 数据日期
  38. Value float64 // 数据值
  39. CreateTime time.Time // 创建时间
  40. ModifyTime time.Time // 修改时间
  41. }
  42. // BaseFromSci99Classify 代表卓创资讯-原始指标分类表的结构
  43. type BaseFromSci99Classify struct {
  44. BaseFromSciClassifyId int `orm:"column(base_from_sci_classify_id);pk"` // 主键,自动递增
  45. ClassifyName string // 分类名称
  46. Sort int // 排序
  47. CreateTime time.Time // 创建时间
  48. ModifyTime time.Time // 修改时间
  49. }
  50. func GetBaseFromSci99DataDataByCondition(condition string, pars []interface{}) (item []*BaseFromIcpiData, err error) {
  51. sql := ` SELECT * FROM base_from_sci99_data WHERE 1=1 `
  52. o := orm.NewOrm()
  53. if condition != "" {
  54. sql += condition
  55. }
  56. sql += ` ORDER BY data_time DESC `
  57. _, err = o.Raw(sql, pars).QueryRows(&item)
  58. return
  59. }
  60. // 添加数据
  61. func AddBaseFromSci99Index(item *BaseFromSci99Index) (lastId int64, err error) {
  62. o := orm.NewOrm()
  63. lastId, err = o.Insert(item)
  64. return
  65. }
  66. func AddBaseFromSci99Classify(item *BaseFromSci99Classify) (lastId int64, err error) {
  67. o := orm.NewOrm()
  68. lastId, err = o.Insert(item)
  69. return
  70. }
  71. func AddBaseFromSci99DataMulti(item []*BaseFromSci99Data) (err error) {
  72. o := orm.NewOrm()
  73. _, err = o.InsertMulti(1000, item)
  74. return
  75. }
  76. func GetBaseFromSci99Index() (list []*BaseFromSci99Index, err error) {
  77. o := orm.NewOrm()
  78. sql := `SELECT * FROM base_from_sci99_index `
  79. _, err = o.Raw(sql).QueryRows(&list)
  80. return
  81. }
  82. func GetBaseFromSci99DataByIndexCode(indexCode string) (items []*BaseFromSci99DataItem, err error) {
  83. o := orm.NewOrm()
  84. sql := `SELECT * FROM base_from_sci99_data WHERE index_code=? `
  85. _, err = o.Raw(sql, indexCode).QueryRows(&items)
  86. return
  87. }
  88. // 新增卓创资讯指标数据
  89. func AddEdbDataFromSci99(edbCode string) (err error) {
  90. o := orm.NewOrm()
  91. dataAll, err := GetBaseFromSci99DataByIndexCode(edbCode)
  92. if err != nil && err.Error() != utils.ErrNoRow() {
  93. return
  94. }
  95. var isAdd bool
  96. addSql := ` INSERT INTO edb_data_sci99(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  97. existMap := make(map[string]string)
  98. for _, sv := range dataAll {
  99. eDate := sv.DataTime
  100. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  101. if err != nil {
  102. fmt.Println("time.Parse Err:" + eDate)
  103. return err
  104. }
  105. timestamp := dataTime.UnixNano() / 1e6
  106. timeStr := fmt.Sprintf("%d", timestamp)
  107. if _, ok := existMap[eDate]; !ok {
  108. addSql += GetAddSql("0", edbCode, eDate, timeStr, sv.Value)
  109. isAdd = true
  110. }
  111. }
  112. if isAdd {
  113. addSql = strings.TrimRight(addSql, ",")
  114. utils.FileLog.Info("addSql:" + addSql)
  115. _, err = o.Raw(addSql).Exec()
  116. if err != nil {
  117. return err
  118. }
  119. }
  120. return
  121. }
  122. // 刷新卓创资讯指标数据
  123. func RefreshEdbDataFromSci99(edbInfoId int, edbCode, startDate string) (err error) {
  124. source := utils.DATA_SOURCE_SCI99
  125. subSource := utils.DATA_SUB_SOURCE_EDB
  126. o := orm.NewOrm()
  127. if err != nil {
  128. return
  129. }
  130. edbInfoIdStr := strconv.Itoa(edbInfoId)
  131. //计算数据
  132. var condition string
  133. var pars []interface{}
  134. if edbCode != "" {
  135. condition += " AND index_code=? "
  136. pars = append(pars, edbCode)
  137. }
  138. if startDate != "" {
  139. condition += " AND data_time>=? "
  140. pars = append(pars, startDate)
  141. }
  142. dataList, err := GetBaseFromSci99DataDataByCondition(condition, pars)
  143. if err != nil {
  144. return
  145. }
  146. // 真实数据的最大日期 , 插入规则配置的日期
  147. var realDataMaxDate, edbDataInsertConfigDate time.Time
  148. var edbDataInsertConfig *EdbDataInsertConfig
  149. var isFindConfigDateRealData bool //是否找到配置日期的实际数据的值
  150. {
  151. edbDataInsertConfig, err = GetEdbDataInsertConfigByEdbId(edbInfoId)
  152. if err != nil && err.Error() != utils.ErrNoRow() {
  153. return
  154. }
  155. if edbDataInsertConfig != nil {
  156. edbDataInsertConfigDate = edbDataInsertConfig.Date
  157. }
  158. }
  159. //获取指标所有数据
  160. var existCondition string
  161. var existPars []interface{}
  162. existCondition += " AND edb_info_id=? "
  163. existPars = append(existPars, edbInfoId)
  164. if startDate != "" {
  165. existCondition += " AND data_time>=? "
  166. existPars = append(existPars, startDate)
  167. }
  168. existList, err := GetEdbDataByCondition(source, subSource, existCondition, existPars)
  169. if err != nil {
  170. return err
  171. }
  172. existMap := make(map[string]*EdbInfoSearchData)
  173. for _, v := range existList {
  174. existMap[v.DataTime] = v
  175. }
  176. addSql := ` INSERT INTO edb_data_sci99(edb_info_id,edb_code,data_time,value,create_time,modify_time,data_timestamp) values `
  177. var isAdd bool
  178. for _, v := range dataList {
  179. item := v
  180. itemValue := v.Value
  181. eDate := item.DataTime
  182. dataTime, err := time.ParseInLocation(utils.FormatDate, eDate, time.Local)
  183. if err != nil {
  184. return err
  185. }
  186. if _, ok := existMap[v.DataTime]; !ok {
  187. sValue := itemValue
  188. if sValue != "" {
  189. timestamp := dataTime.UnixNano() / 1e6
  190. timeStr := fmt.Sprintf("%d", timestamp)
  191. saveValue := sValue
  192. if findItem, ok := existMap[eDate]; !ok {
  193. addSql += GetAddSql(edbInfoIdStr, edbCode, eDate, timeStr, saveValue)
  194. isAdd = true
  195. } else {
  196. if findItem != nil && utils.SubFloatToString(findItem.Value, 30) != sValue {
  197. err = ModifyEdbDataById(source, subSource, findItem.EdbDataId, sValue)
  198. if err != nil {
  199. return err
  200. }
  201. }
  202. }
  203. }
  204. }
  205. // 下面代码主要目的是处理掉手动插入的数据判断
  206. {
  207. if realDataMaxDate.IsZero() || dataTime.After(realDataMaxDate) {
  208. realDataMaxDate = dataTime
  209. }
  210. if edbDataInsertConfigDate.IsZero() || dataTime.Equal(edbDataInsertConfigDate) {
  211. isFindConfigDateRealData = true
  212. }
  213. }
  214. }
  215. // 处理手工数据补充的配置
  216. HandleConfigInsertEdbData(realDataMaxDate, edbDataInsertConfig, edbInfoId, source, subSource, existMap, isFindConfigDateRealData)
  217. if isAdd {
  218. addSql = strings.TrimRight(addSql, ",")
  219. _, err = o.Raw(addSql).Exec()
  220. if err != nil {
  221. return err
  222. }
  223. }
  224. return
  225. }
  226. func GetBaseFromSci99Classify() (list []*BaseFromSci99Classify, err error) {
  227. o := orm.NewOrm()
  228. sql := `SELECT * FROM base_from_sci99_classify `
  229. _, err = o.Raw(sql).QueryRows(&list)
  230. return
  231. }
  232. func GetBaseFromTradeSci99DataAll(indexCode string) (list []*BaseFromSci99Data, err error) {
  233. o := orm.NewOrm()
  234. sql := `SELECT * FROM base_from_sci99_data where index_code=?`
  235. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  236. return
  237. }
  238. func GetBaseFromTradeSci99LatestData(indexCode string) (date time.Time, err error) {
  239. o := orm.NewOrm()
  240. sql := `SELECT data_time FROM base_from_sci99_data where index_code=? ORDER BY data_time DESC limit 1 `
  241. err = o.Raw(sql, indexCode).QueryRow(&date)
  242. return
  243. }
  244. // UpdateBaseFromSci99Data
  245. func UpdateBaseFromSci99Data(value float64, indexCode, dataTime string) (err error) {
  246. o := orm.NewOrm()
  247. sql := `UPDATE base_from_sci99_data SET value=?,modify_time=NOW() WHERE index_code = ? AND data_time = ? `
  248. _, err = o.Raw(sql, value, indexCode, dataTime).Exec()
  249. return
  250. }