factor_edb_series.go 12 KB

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