factor_edb_series.go 14 KB

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