base_from_yongyi.go 7.0 KB

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