factor_edb_series.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta/eta_chart_lib/global"
  5. "eta/eta_chart_lib/utils"
  6. "fmt"
  7. "strings"
  8. "time"
  9. )
  10. const (
  11. FactorEdbSeriesCalculateNone = 0
  12. FactorEdbSeriesCalculating = 1
  13. FactorEdbSeriesCalculated = 2
  14. )
  15. // FactorEdbSeries 因子指标系列表
  16. type FactorEdbSeries struct {
  17. //FactorEdbSeriesId int `orm:"column(factor_edb_series_id);pk"`
  18. FactorEdbSeriesId int `gorm:"column:factor_edb_series_id;primaryKey"`
  19. SeriesName string `description:"系列名称"`
  20. EdbInfoType int `description:"关联指标类型:0-普通指标;1-预测指标"`
  21. CalculateStep string `description:"计算步骤-JSON"`
  22. CalculateState int `description:"计算状态: 0-无计算; 1-计算中; 2-计算完成"`
  23. CreateTime time.Time `description:"创建时间"`
  24. ModifyTime time.Time `description:"修改时间"`
  25. }
  26. func (m *FactorEdbSeries) TableName() string {
  27. return "factor_edb_series"
  28. }
  29. type FactorEdbSeriesCols struct {
  30. PrimaryId string
  31. SeriesName string
  32. EdbInfoType string
  33. CalculateStep string
  34. CalculateState string
  35. CreateTime string
  36. ModifyTime string
  37. }
  38. func (m *FactorEdbSeries) Cols() FactorEdbSeriesCols {
  39. return FactorEdbSeriesCols{
  40. PrimaryId: "factor_edb_series_id",
  41. SeriesName: "series_name",
  42. EdbInfoType: "edb_info_type",
  43. CalculateStep: "calculate_step",
  44. CalculateState: "calculate_state",
  45. CreateTime: "create_time",
  46. ModifyTime: "modify_time",
  47. }
  48. }
  49. func (m *FactorEdbSeries) Create() (err error) {
  50. //o := orm.NewOrmUsingDB("data")
  51. //id, err := o.Insert(m)
  52. //if err != nil {
  53. // return
  54. //}
  55. //m.FactorEdbSeriesId = int(id)
  56. err = global.DbMap[utils.DbNameIndex].Create(&m).Error
  57. return
  58. }
  59. func (m *FactorEdbSeries) CreateMulti(items []*FactorEdbSeries) (err error) {
  60. if len(items) == 0 {
  61. return
  62. }
  63. //o := orm.NewOrmUsingDB("data")
  64. //_, err = o.InsertMulti(len(items), items)
  65. err = global.DbMap[utils.DbNameIndex].CreateInBatches(items, len(items)).Error
  66. return
  67. }
  68. func (m *FactorEdbSeries) Update(cols []string) (err error) {
  69. //o := orm.NewOrmUsingDB("data")
  70. //_, err = o.Update(m, cols...)
  71. err = global.DbMap[utils.DbNameIndex].Model(&m).Select(cols).Updates(&m).Error
  72. return
  73. }
  74. func (m *FactorEdbSeries) Remove() (err error) {
  75. //o := orm.NewOrmUsingDB("data")
  76. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  77. //_, err = o.Raw(sql, m.FactorEdbSeriesId).Exec()
  78. err = global.DbMap[utils.DbNameIndex].Exec(sql, m.FactorEdbSeriesId).Error
  79. return
  80. }
  81. func (m *FactorEdbSeries) MultiRemove(ids []int) (err error) {
  82. if len(ids) == 0 {
  83. return
  84. }
  85. //o := orm.NewOrmUsingDB("data")
  86. //sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN (%s)`, m.TableName(), m.Cols().PrimaryId, utils.GetOrmInReplace(len(ids)))
  87. //_, err = o.Raw(sql, ids).Exec()
  88. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s IN ?`, m.TableName(), m.Cols().PrimaryId)
  89. err = global.DbMap[utils.DbNameIndex].Exec(sql, ids).Error
  90. return
  91. }
  92. func (m *FactorEdbSeries) GetItemById(id int) (item *FactorEdbSeries, err error) {
  93. //o := orm.NewOrmUsingDB("data")
  94. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  95. //err = o.Raw(sql, id).QueryRow(&item)
  96. err = global.DbMap[utils.DbNameIndex].Raw(sql, id).First(&item).Error
  97. return
  98. }
  99. func (m *FactorEdbSeries) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *FactorEdbSeries, err error) {
  100. //o := orm.NewOrmUsingDB("data")
  101. order := ``
  102. if orderRule != "" {
  103. order = ` ORDER BY ` + orderRule
  104. }
  105. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
  106. err = global.DbMap[utils.DbNameIndex].Raw(sql, pars...).First(&item).Error
  107. //err = o.Raw(sql, pars).QueryRow(&item)
  108. return
  109. }
  110. func (m *FactorEdbSeries) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  111. //o := orm.NewOrmUsingDB("data")
  112. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  113. //err = o.Raw(sql, pars).QueryRow(&count)
  114. err = global.DbMap[utils.DbNameIndex].Raw(sql, pars...).Scan(&count).Error
  115. return
  116. }
  117. func (m *FactorEdbSeries) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*FactorEdbSeries, err error) {
  118. //o := orm.NewOrmUsingDB("data")
  119. fields := strings.Join(fieldArr, ",")
  120. if len(fieldArr) == 0 {
  121. fields = `*`
  122. }
  123. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  124. if orderRule != "" {
  125. order = ` ORDER BY ` + orderRule
  126. }
  127. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  128. err = global.DbMap[utils.DbNameIndex].Raw(sql, pars...).Find(&items).Error
  129. //_, err = o.Raw(sql, pars).QueryRows(&items)
  130. return
  131. }
  132. func (m *FactorEdbSeries) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*FactorEdbSeries, err error) {
  133. //o := orm.NewOrmUsingDB("data")
  134. fields := strings.Join(fieldArr, ",")
  135. if len(fieldArr) == 0 {
  136. fields = `*`
  137. }
  138. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  139. if orderRule != "" {
  140. order = ` ORDER BY ` + orderRule
  141. }
  142. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  143. //_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  144. pars = append(pars, startSize, pageSize)
  145. err = global.DbMap[utils.DbNameIndex].Raw(sql, pars...).Find(&items).Error
  146. return
  147. }
  148. // FactorEdbSeriesItem 多因子系列信息
  149. type FactorEdbSeriesItem struct {
  150. SeriesId int `description:"多因子系列ID"`
  151. SeriesName string `description:"系列名称"`
  152. EdbInfoType int `description:"关联指标类型:0-普通指标;1-预测指标"`
  153. CalculateStep []FactorEdbSeriesCalculatePars `description:"计算步骤-JSON"`
  154. CreateTime string `description:"创建时间"`
  155. ModifyTime string `description:"修改时间"`
  156. }
  157. func (m *FactorEdbSeries) Format2Item() (item *FactorEdbSeriesItem) {
  158. item = new(FactorEdbSeriesItem)
  159. item.SeriesId = m.FactorEdbSeriesId
  160. item.SeriesName = m.SeriesName
  161. item.EdbInfoType = m.EdbInfoType
  162. if m.CalculateStep != "" {
  163. _ = json.Unmarshal([]byte(m.CalculateStep), &item.CalculateStep)
  164. }
  165. item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, m.CreateTime)
  166. item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, m.ModifyTime)
  167. return
  168. }
  169. // FactorEdbSeriesCalculatePars 计算参数
  170. type FactorEdbSeriesCalculatePars struct {
  171. Formula interface{} `description:"N值/移动天数/指数修匀alpha值/计算公式等"`
  172. Calendar string `description:"公历/农历"`
  173. Frequency string `description:"需要转换的频度"`
  174. MoveType int `description:"移动方式: 1-领先(默认); 2-滞后"`
  175. MoveFrequency string `description:"移动频度"`
  176. FromFrequency string `description:"来源的频度"`
  177. Source int `description:"计算方式来源(不是指标来源)"`
  178. Sort int `description:"计算顺序"`
  179. }
  180. // CreateSeriesAndMapping 新增系列和指标关联
  181. func (m *FactorEdbSeries) CreateSeriesAndMapping(item *FactorEdbSeries, mappings []*FactorEdbSeriesMapping) (seriesId int, err error) {
  182. if item == nil {
  183. err = fmt.Errorf("series is nil")
  184. return
  185. }
  186. //o := orm.NewOrmUsingDB("data")
  187. //tx, e := o.Begin()
  188. tx := global.DbMap[utils.DbNameIndex].Begin()
  189. //if e != nil {
  190. // err = fmt.Errorf("orm begin err: %v", e)
  191. // return
  192. //}
  193. defer func() {
  194. if err != nil {
  195. _ = tx.Rollback()
  196. return
  197. }
  198. _ = tx.Commit()
  199. }()
  200. //id, e := tx.Insert(item)
  201. e := tx.Create(&item).Error
  202. if e != nil {
  203. err = fmt.Errorf("insert series err: %v", e)
  204. return
  205. }
  206. //seriesId = int(id)
  207. //item.FactorEdbSeriesId = seriesId
  208. if len(mappings) > 0 {
  209. for _, v := range mappings {
  210. v.FactorEdbSeriesId = seriesId
  211. }
  212. e = tx.CreateInBatches(mappings, 200).Error
  213. //_, e = tx.InsertMulti(200, mappings)
  214. if e != nil {
  215. err = fmt.Errorf("insert multi mapping err: %v", e)
  216. return
  217. }
  218. }
  219. return
  220. }
  221. // EditSeriesAndMapping 编辑系列和指标关联
  222. func (m *FactorEdbSeries) EditSeriesAndMapping(item *FactorEdbSeries, mappings []*FactorEdbSeriesMapping, updateCols []string) (err error) {
  223. if item == nil {
  224. err = fmt.Errorf("series is nil")
  225. return
  226. }
  227. //o := orm.NewOrmUsingDB("data")
  228. //tx, e := o.Begin()
  229. //if e != nil {
  230. // err = fmt.Errorf("orm begin err: %v", e)
  231. // return
  232. //}
  233. tx := global.DbMap[utils.DbNameIndex].Begin()
  234. defer func() {
  235. if err != nil {
  236. _ = tx.Rollback()
  237. return
  238. }
  239. _ = tx.Commit()
  240. }()
  241. //_, e = tx.Update(item, updateCols...)
  242. e := tx.Model(&item).Select(updateCols).Updates(&item).Error
  243. if e != nil {
  244. err = fmt.Errorf("update series err: %v", e)
  245. return
  246. }
  247. // 清除原指标关联
  248. mappingOb := new(FactorEdbSeriesMapping)
  249. cond := fmt.Sprintf("%s = ?", mappingOb.Cols().FactorEdbSeriesId)
  250. pars := make([]interface{}, 0)
  251. pars = append(pars, item.FactorEdbSeriesId)
  252. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, mappingOb.TableName(), cond)
  253. //_, e = tx.Raw(sql, pars).Exec()
  254. e = tx.Exec(sql, pars...).Error
  255. if e != nil {
  256. err = fmt.Errorf("remove mapping err: %v", e)
  257. return
  258. }
  259. if len(mappings) > 0 {
  260. for _, v := range mappings {
  261. v.FactorEdbSeriesId = item.FactorEdbSeriesId
  262. }
  263. //_, e = tx.InsertMulti(200, mappings)
  264. e = tx.CreateInBatches(mappings, 200).Error
  265. if e != nil {
  266. err = fmt.Errorf("insert multi mapping err: %v", e)
  267. return
  268. }
  269. }
  270. return
  271. }
  272. // FactorEdbSeriesStepCalculateResp 批量计算响应
  273. type FactorEdbSeriesStepCalculateResp struct {
  274. SeriesId int `description:"多因子指标系列ID"`
  275. Fail []FactorEdbSeriesStepCalculateResult `description:"计算失败的指标"`
  276. Success []FactorEdbSeriesStepCalculateResult `description:"计算成功的指标"`
  277. }
  278. // FactorEdbSeriesStepCalculateResult 批量计算结果
  279. type FactorEdbSeriesStepCalculateResult struct {
  280. EdbInfoId int `description:"指标ID"`
  281. EdbCode string `description:"指标编码"`
  282. Msg string `description:"提示信息"`
  283. ErrMsg string `description:"错误信息"`
  284. }
  285. // FactorEdbSeriesDetail 因子指标系列-详情
  286. type FactorEdbSeriesDetail struct {
  287. *FactorEdbSeriesItem
  288. EdbMappings []*FactorEdbSeriesMappingItem
  289. }
  290. // FactorEdbSeriesCorrelationMatrixResp 因子指标系列-相关性矩阵响应
  291. type FactorEdbSeriesCorrelationMatrixResp struct {
  292. Fail []FactorEdbSeriesCorrelationMatrixItem `description:"计算失败的指标"`
  293. Success []FactorEdbSeriesCorrelationMatrixItem `description:"计算成功的指标"`
  294. }
  295. // FactorEdbSeriesCorrelationMatrixItem 因子指标系列-相关性矩阵信息
  296. type FactorEdbSeriesCorrelationMatrixItem struct {
  297. SeriesId int `description:"因子指标系列ID"`
  298. EdbInfoId int `description:"指标ID"`
  299. EdbCode string `description:"指标编码"`
  300. EdbName string `description:"指标名称"`
  301. Values []FactorEdbSeriesCorrelationMatrixValues `description:"X轴和Y轴数据"`
  302. Msg string `description:"提示信息"`
  303. ErrMsg string `description:"错误信息"`
  304. Used bool `description:"是否选中"`
  305. SourceName string `description:"指标来源名称"`
  306. }
  307. // FactorEdbSeriesCorrelationMatrixValues 因子指标系列-相关性矩阵XY值
  308. type FactorEdbSeriesCorrelationMatrixValues struct {
  309. XData int `description:"X轴数据"`
  310. YData float64 `description:"Y轴数据"`
  311. }
  312. // FactorEdbSeriesCorrelationMatrixOrder 排序规则[0 1 2 3 -1 -2 -3]
  313. type FactorEdbSeriesCorrelationMatrixOrder []FactorEdbSeriesCorrelationMatrixValues
  314. func (a FactorEdbSeriesCorrelationMatrixOrder) Len() int {
  315. return len(a)
  316. }
  317. func (a FactorEdbSeriesCorrelationMatrixOrder) Swap(i, j int) {
  318. a[i], a[j] = a[j], a[i]
  319. }
  320. func (a FactorEdbSeriesCorrelationMatrixOrder) Less(i, j int) bool {
  321. // 非负数优先
  322. if a[i].XData >= 0 && a[j].XData < 0 {
  323. return true
  324. }
  325. if a[i].XData < 0 && a[j].XData >= 0 {
  326. return false
  327. }
  328. // 非负数升序排序
  329. if a[i].XData >= 0 {
  330. return a[i].XData < a[j].XData
  331. }
  332. // 负数按绝对值的降序排序(即数值的升序)
  333. return a[i].XData > a[j].XData
  334. }