factor_edb_series.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. package models
  2. import (
  3. "encoding/json"
  4. "eta/eta_index_lib/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.NewOrm()
  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.NewOrm()
  62. _, err = o.InsertMulti(len(items), items)
  63. return
  64. }
  65. func (m *FactorEdbSeries) Update(cols []string) (err error) {
  66. o := orm.NewOrm()
  67. _, err = o.Update(m, cols...)
  68. return
  69. }
  70. func (m *FactorEdbSeries) Remove() (err error) {
  71. o := orm.NewOrm()
  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.NewOrm()
  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) GetItemById(id int) (item *FactorEdbSeries, err error) {
  86. o := orm.NewOrm()
  87. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.Cols().PrimaryId)
  88. err = o.Raw(sql, id).QueryRow(&item)
  89. return
  90. }
  91. func (m *FactorEdbSeries) GetItemByCondition(condition string, pars []interface{}, orderRule string) (item *FactorEdbSeries, err error) {
  92. o := orm.NewOrm()
  93. order := ``
  94. if orderRule != "" {
  95. order = ` ORDER BY ` + orderRule
  96. }
  97. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s %s LIMIT 1`, m.TableName(), condition, order)
  98. err = o.Raw(sql, pars).QueryRow(&item)
  99. return
  100. }
  101. func (m *FactorEdbSeries) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  102. o := orm.NewOrm()
  103. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  104. err = o.Raw(sql, pars).QueryRow(&count)
  105. return
  106. }
  107. func (m *FactorEdbSeries) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*FactorEdbSeries, err error) {
  108. o := orm.NewOrm()
  109. fields := strings.Join(fieldArr, ",")
  110. if len(fieldArr) == 0 {
  111. fields = `*`
  112. }
  113. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  114. if orderRule != "" {
  115. order = ` ORDER BY ` + orderRule
  116. }
  117. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  118. _, err = o.Raw(sql, pars).QueryRows(&items)
  119. return
  120. }
  121. func (m *FactorEdbSeries) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*FactorEdbSeries, err error) {
  122. o := orm.NewOrm()
  123. fields := strings.Join(fieldArr, ",")
  124. if len(fieldArr) == 0 {
  125. fields = `*`
  126. }
  127. order := fmt.Sprintf(`ORDER BY %s DESC`, m.Cols().CreateTime)
  128. if orderRule != "" {
  129. order = ` ORDER BY ` + orderRule
  130. }
  131. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  132. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  133. return
  134. }
  135. // FactorEdbSeriesItem 多因子系列信息
  136. type FactorEdbSeriesItem struct {
  137. SeriesId int `description:"多因子系列ID"`
  138. SeriesName string `description:"系列名称"`
  139. EdbInfoType int `description:"关联指标类型:0-普通指标;1-预测指标"`
  140. CalculateStep []FactorEdbSeriesCalculatePars `description:"计算步骤-JSON"`
  141. CreateTime string `description:"创建时间"`
  142. ModifyTime string `description:"修改时间"`
  143. }
  144. func (m *FactorEdbSeries) Format2Item() (item *FactorEdbSeriesItem) {
  145. item = new(FactorEdbSeriesItem)
  146. item.SeriesId = m.FactorEdbSeriesId
  147. item.SeriesName = m.SeriesName
  148. item.EdbInfoType = m.EdbInfoType
  149. if m.CalculateStep != "" {
  150. _ = json.Unmarshal([]byte(m.CalculateStep), &item.CalculateStep)
  151. }
  152. item.CreateTime = utils.TimeTransferString(utils.FormatDateTime, m.CreateTime)
  153. item.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, m.ModifyTime)
  154. return
  155. }
  156. // FactorEdbSeriesCalculatePars 计算参数
  157. type FactorEdbSeriesCalculatePars struct {
  158. Formula interface{} `description:"N值/移动天数/指数修匀alpha值/计算公式等"`
  159. Calendar string `description:"公历/农历"`
  160. Frequency string `description:"需要转换的频度"`
  161. MoveType int `description:"移动方式: 1-领先(默认); 2-滞后"`
  162. MoveFrequency string `description:"移动频度"`
  163. FromFrequency string `description:"来源的频度"`
  164. Source int `description:"计算方式来源(不是指标来源)"`
  165. Sort int `description:"计算顺序"`
  166. }
  167. // CreateSeriesAndMapping 新增系列和指标关联
  168. func (m *FactorEdbSeries) CreateSeriesAndMapping(item *FactorEdbSeries, mappings []*FactorEdbSeriesMapping) (seriesId int, err error) {
  169. if item == nil {
  170. err = fmt.Errorf("series is nil")
  171. return
  172. }
  173. o := orm.NewOrm()
  174. tx, e := o.Begin()
  175. if e != nil {
  176. err = fmt.Errorf("orm begin err: %v", e)
  177. return
  178. }
  179. defer func() {
  180. if err != nil {
  181. _ = tx.Rollback()
  182. return
  183. }
  184. _ = tx.Commit()
  185. }()
  186. id, e := tx.Insert(item)
  187. if e != nil {
  188. err = fmt.Errorf("insert series err: %v", e)
  189. return
  190. }
  191. seriesId = int(id)
  192. item.FactorEdbSeriesId = seriesId
  193. if len(mappings) > 0 {
  194. for _, v := range mappings {
  195. v.FactorEdbSeriesId = seriesId
  196. }
  197. _, e = tx.InsertMulti(200, mappings)
  198. if e != nil {
  199. err = fmt.Errorf("insert multi mapping err: %v", e)
  200. return
  201. }
  202. }
  203. return
  204. }
  205. // EditSeriesAndMapping 编辑系列和指标关联
  206. func (m *FactorEdbSeries) EditSeriesAndMapping(item *FactorEdbSeries, mappings []*FactorEdbSeriesMapping, updateCols []string) (err error) {
  207. if item == nil {
  208. err = fmt.Errorf("series is nil")
  209. return
  210. }
  211. o := orm.NewOrm()
  212. tx, e := o.Begin()
  213. if e != nil {
  214. err = fmt.Errorf("orm begin err: %v", e)
  215. return
  216. }
  217. defer func() {
  218. if err != nil {
  219. _ = tx.Rollback()
  220. return
  221. }
  222. _ = tx.Commit()
  223. }()
  224. _, e = tx.Update(item, updateCols...)
  225. if e != nil {
  226. err = fmt.Errorf("update series err: %v", e)
  227. return
  228. }
  229. // 清除原指标关联
  230. mappingOb := new(FactorEdbSeriesMapping)
  231. cond := fmt.Sprintf("%s = ?", mappingOb.Cols().FactorEdbSeriesId)
  232. pars := make([]interface{}, 0)
  233. pars = append(pars, item.FactorEdbSeriesId)
  234. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s`, mappingOb.TableName(), cond)
  235. _, e = tx.Raw(sql, pars).Exec()
  236. if e != nil {
  237. err = fmt.Errorf("remove mapping err: %v", e)
  238. return
  239. }
  240. if len(mappings) > 0 {
  241. for _, v := range mappings {
  242. v.FactorEdbSeriesId = item.FactorEdbSeriesId
  243. }
  244. _, e = tx.InsertMulti(200, mappings)
  245. if e != nil {
  246. err = fmt.Errorf("insert multi mapping err: %v", e)
  247. return
  248. }
  249. }
  250. return
  251. }
  252. // FactorEdbSeriesStepCalculateResp 批量计算响应
  253. type FactorEdbSeriesStepCalculateResp struct {
  254. SeriesId int `description:"多因子指标系列ID"`
  255. Fail []FactorEdbSeriesStepCalculateResult `description:"计算失败的指标"`
  256. Success []FactorEdbSeriesStepCalculateResult `description:"计算成功的指标"`
  257. }
  258. // FactorEdbSeriesStepCalculateResult 批量计算结果
  259. type FactorEdbSeriesStepCalculateResult struct {
  260. EdbInfoId int `description:"指标ID"`
  261. EdbCode string `description:"指标编码"`
  262. Msg string `description:"提示信息"`
  263. ErrMsg string `description:"错误信息"`
  264. }
  265. // FactorEdbSeriesDetail 因子指标系列-详情
  266. type FactorEdbSeriesDetail struct {
  267. *FactorEdbSeriesItem
  268. EdbMappings []*FactorEdbSeriesMappingItem
  269. }
  270. // FactorEdbSeriesCorrelationMatrixResp 因子指标系列-相关性矩阵响应
  271. type FactorEdbSeriesCorrelationMatrixResp struct {
  272. Fail []FactorEdbSeriesCorrelationMatrixItem `description:"计算失败的指标"`
  273. Success []FactorEdbSeriesCorrelationMatrixItem `description:"计算成功的指标"`
  274. }
  275. // FactorEdbSeriesCorrelationMatrixItem 因子指标系列-相关性矩阵信息
  276. type FactorEdbSeriesCorrelationMatrixItem struct {
  277. SeriesId int `description:"因子指标系列ID"`
  278. EdbInfoId int `description:"指标ID"`
  279. EdbCode string `description:"指标编码"`
  280. EdbName string `description:"指标名称"`
  281. Values []FactorEdbSeriesCorrelationMatrixValues `description:"X轴和Y轴数据"`
  282. Msg string `description:"提示信息"`
  283. ErrMsg string `description:"错误信息"`
  284. Used bool `description:"是否选中"`
  285. SourceName string `description:"指标来源名称"`
  286. }
  287. // FactorEdbSeriesCorrelationMatrixValues 因子指标系列-相关性矩阵XY值
  288. type FactorEdbSeriesCorrelationMatrixValues struct {
  289. XData int `description:"X轴数据"`
  290. YData float64 `description:"Y轴数据"`
  291. }
  292. // FactorEdbSeriesCorrelationMatrixOrder 排序规则[0 1 2 3 -1 -2 -3]
  293. type FactorEdbSeriesCorrelationMatrixOrder []FactorEdbSeriesCorrelationMatrixValues
  294. func (a FactorEdbSeriesCorrelationMatrixOrder) Len() int {
  295. return len(a)
  296. }
  297. func (a FactorEdbSeriesCorrelationMatrixOrder) Swap(i, j int) {
  298. a[i], a[j] = a[j], a[i]
  299. }
  300. func (a FactorEdbSeriesCorrelationMatrixOrder) Less(i, j int) bool {
  301. // 非负数优先
  302. if a[i].XData >= 0 && a[j].XData < 0 {
  303. return true
  304. }
  305. if a[i].XData < 0 && a[j].XData >= 0 {
  306. return false
  307. }
  308. // 非负数升序排序
  309. if a[i].XData >= 0 {
  310. return a[i].XData < a[j].XData
  311. }
  312. // 负数按绝对值的降序排序(即数值的升序)
  313. return a[i].XData > a[j].XData
  314. }
  315. // FactorEdbRecalculateReq 因子指标重新计算
  316. type FactorEdbRecalculateReq struct {
  317. EdbInfoId int `description:"指标ID"`
  318. EdbCode string `description:"指标编码"`
  319. }
  320. // FactorEdbChartRecalculateReq 因子指标关联的图表数据重计算
  321. type FactorEdbChartRecalculateReq struct {
  322. ChartInfoId int `description:"图表ID"`
  323. //EdbInfoId int `description:"指标ID"`
  324. //EdbCode string `description:"指标编码"`
  325. }