factor_edb_series.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta_gn/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" gorm:"primaryKey" `
  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. pars = append(pars, startSize)
  142. pars = append(pars, pageSize)
  143. _, err = o.Raw(sql, pars...).QueryRows(&items)
  144. return
  145. }
  146. // FactorEdbSeriesItem 多因子系列信息
  147. type FactorEdbSeriesItem struct {
  148. SeriesId int `description:"多因子系列ID"`
  149. SeriesName string `description:"系列名称"`
  150. EdbInfoType int `description:"关联指标类型:0-普通指标;1-预测指标"`
  151. CalculateStep []FactorEdbSeriesCalculatePars `description:"计算步骤-JSON"`
  152. CreateTime string `description:"创建时间"`
  153. ModifyTime string `description:"修改时间"`
  154. }
  155. func (m *FactorEdbSeries) Format2Item() (item *FactorEdbSeriesItem) {
  156. item = new(FactorEdbSeriesItem)
  157. item.SeriesId = m.FactorEdbSeriesId
  158. item.SeriesName = m.SeriesName
  159. item.EdbInfoType = m.EdbInfoType
  160. if m.CalculateStep != "" {
  161. _ = json.Unmarshal([]byte(m.CalculateStep), &item.CalculateStep)
  162. }
  163. item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, m.CreateTime)
  164. item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, m.ModifyTime)
  165. return
  166. }
  167. // FactorEdbSeriesCalculatePars 计算参数
  168. type FactorEdbSeriesCalculatePars struct {
  169. Formula interface{} `description:"N值/移动天数/指数修匀alpha值/计算公式等"`
  170. Calendar string `description:"公历/农历"`
  171. Frequency string `description:"需要转换的频度"`
  172. MoveType int `description:"移动方式: 1-领先(默认); 2-滞后"`
  173. MoveFrequency string `description:"移动频度"`
  174. FromFrequency string `description:"来源的频度"`
  175. Source int `description:"计算方式来源(不是指标来源)"`
  176. Sort int `description:"计算顺序"`
  177. }
  178. // CreateSeriesAndMapping 新增系列和指标关联
  179. func (m *FactorEdbSeries) CreateSeriesAndMapping(item *FactorEdbSeries, mappings []*FactorEdbSeriesMapping) (seriesId int, err error) {
  180. if item == nil {
  181. err = fmt.Errorf("series is nil")
  182. return
  183. }
  184. o := orm.NewOrmUsingDB("data")
  185. tx, e := o.Begin()
  186. if e != nil {
  187. err = fmt.Errorf("orm begin err: %v", e)
  188. return
  189. }
  190. defer func() {
  191. if err != nil {
  192. _ = tx.Rollback()
  193. return
  194. }
  195. _ = tx.Commit()
  196. }()
  197. id, e := tx.Insert(item)
  198. if e != nil {
  199. err = fmt.Errorf("insert series err: %v", e)
  200. return
  201. }
  202. seriesId = int(id)
  203. item.FactorEdbSeriesId = seriesId
  204. if len(mappings) > 0 {
  205. for _, v := range mappings {
  206. v.FactorEdbSeriesId = seriesId
  207. }
  208. _, e = tx.InsertMulti(200, mappings)
  209. if e != nil {
  210. err = fmt.Errorf("insert multi mapping err: %v", e)
  211. return
  212. }
  213. }
  214. return
  215. }
  216. // EditSeriesAndMapping 编辑系列和指标关联
  217. func (m *FactorEdbSeries) EditSeriesAndMapping(item *FactorEdbSeries, mappings []*FactorEdbSeriesMapping, updateCols []string) (err error) {
  218. if item == nil {
  219. err = fmt.Errorf("series is nil")
  220. return
  221. }
  222. o := orm.NewOrmUsingDB("data")
  223. tx, e := o.Begin()
  224. if e != nil {
  225. err = fmt.Errorf("orm begin err: %v", e)
  226. return
  227. }
  228. defer func() {
  229. if err != nil {
  230. _ = tx.Rollback()
  231. return
  232. }
  233. _ = tx.Commit()
  234. }()
  235. _, e = tx.Update(item, updateCols...)
  236. if e != nil {
  237. err = fmt.Errorf("update series err: %v", e)
  238. return
  239. }
  240. // 清除原指标关联
  241. mappingOb := new(FactorEdbSeriesMapping)
  242. cond := fmt.Sprintf("%s = ?", mappingOb.Cols().FactorEdbSeriesId)
  243. pars := make([]interface{}, 0)
  244. pars = append(pars, item.FactorEdbSeriesId)
  245. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, mappingOb.TableName(), cond)
  246. _, e = tx.Raw(sql, pars).Exec()
  247. if e != nil {
  248. err = fmt.Errorf("remove mapping err: %v", e)
  249. return
  250. }
  251. if len(mappings) > 0 {
  252. for _, v := range mappings {
  253. v.FactorEdbSeriesId = item.FactorEdbSeriesId
  254. }
  255. _, e = tx.InsertMulti(200, mappings)
  256. if e != nil {
  257. err = fmt.Errorf("insert multi mapping err: %v", e)
  258. return
  259. }
  260. }
  261. return
  262. }
  263. // FactorEdbSeriesStepCalculateResp 批量计算响应
  264. type FactorEdbSeriesStepCalculateResp struct {
  265. SeriesId int `description:"多因子指标系列ID"`
  266. Fail []FactorEdbSeriesStepCalculateResult `description:"计算失败的指标"`
  267. Success []FactorEdbSeriesStepCalculateResult `description:"计算成功的指标"`
  268. }
  269. // FactorEdbSeriesStepCalculateResult 批量计算结果
  270. type FactorEdbSeriesStepCalculateResult struct {
  271. EdbInfoId int `description:"指标ID"`
  272. EdbCode string `description:"指标编码"`
  273. Msg string `description:"提示信息"`
  274. ErrMsg string `description:"错误信息"`
  275. }
  276. // FactorEdbSeriesDetail 因子指标系列-详情
  277. type FactorEdbSeriesDetail struct {
  278. *FactorEdbSeriesItem
  279. EdbMappings []*FactorEdbSeriesMappingItem
  280. }
  281. // FactorEdbSeriesCorrelationMatrixResp 因子指标系列-相关性矩阵响应
  282. type FactorEdbSeriesCorrelationMatrixResp struct {
  283. Fail []FactorEdbSeriesCorrelationMatrixItem `description:"计算失败的指标"`
  284. Success []FactorEdbSeriesCorrelationMatrixItem `description:"计算成功的指标"`
  285. }
  286. // FactorEdbSeriesCorrelationMatrixItem 因子指标系列-相关性矩阵信息
  287. type FactorEdbSeriesCorrelationMatrixItem struct {
  288. SeriesId int `description:"因子指标系列ID"`
  289. EdbInfoId int `description:"指标ID"`
  290. EdbCode string `description:"指标编码"`
  291. EdbName string `description:"指标名称"`
  292. Values []FactorEdbSeriesCorrelationMatrixValues `description:"X轴和Y轴数据"`
  293. Msg string `description:"提示信息"`
  294. ErrMsg string `description:"错误信息"`
  295. Used bool `description:"是否选中"`
  296. SourceName string `description:"指标来源名称"`
  297. SourceNameEn string `description:"英文指标来源名称"`
  298. }
  299. // FactorEdbSeriesCorrelationMatrixValues 因子指标系列-相关性矩阵XY值
  300. type FactorEdbSeriesCorrelationMatrixValues struct {
  301. XData int `description:"X轴数据"`
  302. YData float64 `description:"Y轴数据"`
  303. }
  304. // FactorEdbSeriesCorrelationMatrixOrder 排序规则[0 1 2 3 -1 -2 -3]
  305. type FactorEdbSeriesCorrelationMatrixOrder []FactorEdbSeriesCorrelationMatrixValues
  306. func (a FactorEdbSeriesCorrelationMatrixOrder) Len() int {
  307. return len(a)
  308. }
  309. func (a FactorEdbSeriesCorrelationMatrixOrder) Swap(i, j int) {
  310. a[i], a[j] = a[j], a[i]
  311. }
  312. func (a FactorEdbSeriesCorrelationMatrixOrder) Less(i, j int) bool {
  313. // 非负数优先
  314. if a[i].XData >= 0 && a[j].XData < 0 {
  315. return true
  316. }
  317. if a[i].XData < 0 && a[j].XData >= 0 {
  318. return false
  319. }
  320. // 非负数升序排序
  321. if a[i].XData >= 0 {
  322. return a[i].XData < a[j].XData
  323. }
  324. // 负数按绝对值的降序排序(即数值的升序)
  325. return a[i].XData > a[j].XData
  326. }
  327. // RemoveSeriesAndMappingByFactorEdbSeriesId 删除系列和指标关联
  328. func (m *FactorEdbSeries) RemoveSeriesAndMappingByFactorEdbSeriesId(factorEdbSeriesChartMapping *FactorEdbSeriesChartMapping) (err error) {
  329. o := orm.NewOrmUsingDB("data")
  330. tx, e := o.Begin()
  331. if e != nil {
  332. err = fmt.Errorf("orm begin err: %v", e)
  333. return
  334. }
  335. defer func() {
  336. if err != nil {
  337. _ = tx.Rollback()
  338. return
  339. }
  340. _ = tx.Commit()
  341. }()
  342. factorEdbSeriesId := factorEdbSeriesChartMapping.FactorEdbSeriesId
  343. err = factorEdbSeriesChartMapping.Remove()
  344. if err != nil {
  345. err = fmt.Errorf("factorEdbSeriesChartMapping.delete err: %v", err)
  346. return
  347. }
  348. if factorEdbSeriesId == 0 {
  349. return
  350. }
  351. // 清除原指标关联
  352. seriesOb := new(FactorEdbSeries)
  353. cond := fmt.Sprintf("%s = ?", seriesOb.Cols().PrimaryId)
  354. pars := make([]interface{}, 0)
  355. pars = append(pars, factorEdbSeriesId)
  356. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, seriesOb.TableName(), cond)
  357. _, e = tx.Raw(sql, pars).Exec()
  358. if e != nil {
  359. err = fmt.Errorf("remove FactorEdbSeries err: %v", e)
  360. return
  361. }
  362. // 清除原指标关联
  363. mappingOb := new(FactorEdbSeriesMapping)
  364. cond1 := fmt.Sprintf("%s = ?", mappingOb.Cols().FactorEdbSeriesId)
  365. pars1 := make([]interface{}, 0)
  366. pars1 = append(pars1, factorEdbSeriesId)
  367. sql = fmt.Sprintf(`DELETE FROM %s WHERE %s`, mappingOb.TableName(), cond1)
  368. _, e = tx.Raw(sql, pars1).Exec()
  369. if e != nil {
  370. err = fmt.Errorf("remove mapping err: %v", e)
  371. return
  372. }
  373. dataOb := new(FactorEdbSeriesCalculateDataQjjs)
  374. //删除原指标数据
  375. cond2 := fmt.Sprintf("%s = ?", dataOb.Cols().FactorEdbSeriesId)
  376. pars2 := make([]interface{}, 0)
  377. pars2 = append(pars2, factorEdbSeriesId)
  378. sql = fmt.Sprintf(`DELETE FROM %s WHERE %s`, dataOb.TableName(), cond2)
  379. _, e = tx.Raw(sql, pars2).Exec()
  380. if e != nil {
  381. err = fmt.Errorf("remove mapping err: %v", e)
  382. return
  383. }
  384. return
  385. }
  386. // CalculateCorrelationMatrixPars 计算相关性矩阵参数
  387. type CalculateCorrelationMatrixPars struct {
  388. BaseEdbInfoId int `description:"标的指标ID"`
  389. SeriesIds []int `description:"系列IDs"`
  390. Correlation CorrelationConfig `description:"相关性配置"`
  391. }