base_from_yongyi.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package data_manage
  2. import (
  3. "eta/eta_api/global"
  4. "eta/eta_api/utils"
  5. "github.com/shopspring/decimal"
  6. "gorm.io/gorm"
  7. "time"
  8. "github.com/rdlucklib/rdluck_tools/paging"
  9. )
  10. type BaseFromYongyiIndex struct {
  11. YongyiIndexId int `orm:"column(yongyi_index_id);pk"`
  12. ClassifyId int
  13. IndexCode string
  14. IndexName string
  15. Frequency string
  16. Unit string
  17. Sort int
  18. CreateTime time.Time
  19. ModifyTime time.Time
  20. }
  21. type BaseFromYongyiIndexList struct {
  22. YongyiIndexId int `gorm:"column:yongyi_index_id;primaryKey"`
  23. ClassifyId int
  24. Interface string
  25. EdbInfoId int
  26. EdbUniqueCode string `description:"指标库唯一编码"`
  27. EdbClassifyId int `description:"指标库分类ID"`
  28. IndexCode string
  29. IndexName string
  30. Frequency string
  31. Unit string
  32. Sort int
  33. CreateTime string
  34. ModifyTime string
  35. EdbExist int `description:"指标库是否已添加:0-否;1-是"`
  36. DataList []*BaseFromYongyiData `gorm:"-"`
  37. Paging *paging.PagingItem `description:"分页数据" gorm:"-"`
  38. }
  39. func (m *BaseFromYongyiIndexList) AfterFind(tx *gorm.DB) (err error) {
  40. if utils.NeedDateOrTimeFormat(utils.DbDriverName) {
  41. if m.CreateTime != "" {
  42. m.CreateTime = utils.GormDateStrToDateTimeStr(m.CreateTime)
  43. }
  44. if m.ModifyTime != "" {
  45. m.ModifyTime = utils.GormDateStrToDateTimeStr(m.ModifyTime)
  46. }
  47. }
  48. return
  49. }
  50. type YongyiSingleDataResp struct {
  51. YongyiIndexId int
  52. ClassifyId int
  53. EdbInfoId int
  54. IndexCode string
  55. IndexName string
  56. Frequency string
  57. Unit string
  58. StartTime string
  59. CreateTime string
  60. ModifyTime string
  61. EdbExist int `description:"指标库是否已添加:0-否;1-是"`
  62. Data []*YongyiSingleData
  63. }
  64. type YongyiSingleData struct {
  65. Value string `orm:"column(value)" description:"日期"`
  66. DataTime string `orm:"column(data_time)" description:"值"`
  67. }
  68. func GetYongyiIndexByClassifyId(classifyId int) (items []*BaseFromYongyiIndex, err error) {
  69. o := global.DbMap[utils.DbNameIndex]
  70. sql := ` SELECT yongyi_index_id, classify_id, index_code, index_name FROM base_from_yongyi_index WHERE classify_id=? ORDER BY sort ASC, yongyi_index_id ASC `
  71. err = o.Raw(sql, classifyId).Find(&items).Error
  72. return
  73. }
  74. func GetYongyiIndex(condition string, pars []interface{}) (items []*BaseFromYongyiIndexList, err error) {
  75. o := global.DbMap[utils.DbNameIndex]
  76. sql := ` SELECT * FROM base_from_yongyi_index WHERE 1=1 `
  77. if condition != "" {
  78. sql += condition
  79. }
  80. sql += ` ORDER BY sort ASC, yongyi_index_id asc`
  81. err = o.Raw(sql, pars...).Find(&items).Error
  82. return
  83. }
  84. func GetYongyiIndexDataCount(indexCode string) (count int, err error) {
  85. o := global.DbMap[utils.DbNameIndex]
  86. sql := ` SELECT COUNT(1) AS count FROM base_from_yongyi_data WHERE index_code=? `
  87. err = o.Raw(sql, indexCode).Scan(&count).Error
  88. return
  89. }
  90. func GetYongyiIndexData(indexCode string, startSize, pageSize int) (items []*BaseFromYongyiData, err error) {
  91. o := global.DbMap[utils.DbNameIndex]
  92. sql := ` SELECT * FROM base_from_yongyi_data WHERE index_code=? ORDER BY data_time DESC LIMIT ?,? `
  93. err = o.Raw(sql, indexCode, startSize, pageSize).Find(&items).Error
  94. return
  95. }
  96. func GetYongyiIndexDataByCodes(indexCode []string) (items []*BaseFromYongyiData, err error) {
  97. if len(indexCode) == 0 {
  98. return
  99. }
  100. o := global.DbMap[utils.DbNameIndex]
  101. sql := ` SELECT * FROM base_from_yongyi_data WHERE index_code in (` + utils.GetOrmInReplace(len(indexCode)) + `) ORDER BY data_time DESC `
  102. err = o.Raw(sql, indexCode).Find(&items).Error
  103. return
  104. }
  105. // GetYongyiByConditionAndFrequency 根据条件获取涌益咨询指标列表
  106. func GetYongyiByConditionAndFrequency(condition, frequency string, pars []interface{}) (items []*BaseFromYongyiIndex, err error) {
  107. o := global.DbMap[utils.DbNameIndex]
  108. sql := ` SELECT * FROM base_from_yongyi_index WHERE 1=1 `
  109. if condition != "" {
  110. sql += condition
  111. }
  112. sql += ` AND frequency=?`
  113. sql += ` ORDER BY sort ASC, yongyi_index_id ASC`
  114. err = o.Raw(sql, pars, frequency).Find(&items).Error
  115. return
  116. }
  117. func GetYongyiFrequencyByCondition(condition string, pars []interface{}) (items []string, err error) {
  118. sql := `SELECT DISTINCT frequency FROM base_from_yongyi_index WHERE 1=1 `
  119. if condition != "" {
  120. sql += condition
  121. }
  122. sql += ` ORDER BY FIELD(frequency,'日度','周度','旬度','月度','季度','半年度','年度') `
  123. o := global.DbMap[utils.DbNameIndex]
  124. err = o.Raw(sql, pars...).Find(&items).Error
  125. return
  126. }
  127. // GetYongyiDataDataTimeByIndexId 根据指标id获取指标数据的日期列表
  128. func GetYongyiDataDataTimeByIndexId(indexIdList []int) (items []string, err error) {
  129. if len(indexIdList) == 0 {
  130. return
  131. }
  132. o := global.DbMap[utils.DbNameIndex]
  133. sql := ` SELECT DISTINCT data_time FROM base_from_yongyi_data WHERE yongyi_index_id IN (` + utils.GetOrmInReplace(len(indexIdList)) + `) ORDER BY data_time DESC`
  134. err = o.Raw(sql, indexIdList).Find(&items).Error
  135. if utils.NeedDateOrTimeFormat(utils.DbDriverName) {
  136. for i := range items {
  137. items[i] = utils.GormDateStrToDateStr(items[i])
  138. }
  139. }
  140. return
  141. }
  142. type BaseFromYongyiData struct {
  143. YongyiDataId int `gorm:"column:yongyi_data_id;primaryKey"`
  144. YongyiIndexId int
  145. IndexCode string
  146. DataTime string
  147. Value decimal.Decimal
  148. CreateTime string
  149. ModifyTime string
  150. DataTimestamp int64 `gorm:"column:data_timestamp"`
  151. }
  152. func (m *BaseFromYongyiData) AfterFind(tx *gorm.DB) (err error) {
  153. if utils.NeedDateOrTimeFormat(utils.DbDriverName) {
  154. if m.CreateTime != "" {
  155. m.CreateTime = utils.GormDateStrToDateTimeStr(m.CreateTime)
  156. }
  157. if m.ModifyTime != "" {
  158. m.ModifyTime = utils.GormDateStrToDateTimeStr(m.ModifyTime)
  159. }
  160. if m.DataTime != "" {
  161. m.DataTime = utils.GormDateStrToDateStr(m.DataTime)
  162. }
  163. }
  164. return
  165. }
  166. type BaseFromYongyiIndexSearchItem struct {
  167. YongyiIndexId int `gorm:"column:yongyi_index_id;primaryKey"`
  168. ClassifyId int
  169. ParentClassifyId int
  170. IndexCode string
  171. IndexName string
  172. }
  173. // GetYongyiItemList 模糊查询Yongyi数据库指标列表
  174. func GetYongyiItemList(condition string) (items []*BaseFromYongyiIndexSearchItem, err error) {
  175. o := global.DbMap[utils.DbNameIndex]
  176. sql := "SELECT * FROM base_from_yongyi_index WHERE 1=1"
  177. if condition != "" {
  178. sql += condition
  179. }
  180. err = o.Raw(sql).Find(&items).Error
  181. return
  182. }
  183. func GetYongyiIndexDataByCode(indexCode string) (list []*BaseFromYongyiData, err error) {
  184. o := global.DbMap[utils.DbNameIndex]
  185. sql := `SELECT * FROM base_from_yongyi_data WHERE index_code=? `
  186. err = o.Raw(sql, indexCode).Find(&list).Error
  187. return
  188. }
  189. func GetBaseFromYongyiIndexByIndexCode(indexCode string) (list *BaseFromYongyiIndex, err error) {
  190. o := global.DbMap[utils.DbNameIndex]
  191. sql := ` SELECT * FROM base_from_yongyi_index WHERE index_code=? `
  192. err = o.Raw(sql, indexCode).First(&list).Error
  193. return
  194. }
  195. type BaseFromYongyiIndexType struct {
  196. Type2 string `gorm:"column:type_2"`
  197. Type3 string `gorm:"column:type_3"`
  198. }
  199. // Update 更新Yongyi指标基础信息
  200. func (item *BaseFromYongyiIndex) Update(cols []string) (err error) {
  201. o := global.DbMap[utils.DbNameIndex]
  202. err = o.Select(cols).Updates(item).Error
  203. return
  204. }
  205. // EditYongyiIndexInfoResp 新增指标的返回
  206. type EditYongyiIndexInfoResp struct {
  207. YongyiIndexId int `description:"指标ID"`
  208. IndexCode string `description:"指标code"`
  209. }