factor_edb_series.go 13 KB

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