factor_edb_series.go 14 KB

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