base_from_yongyi.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 `gorm:"column:yongyi_index_id;primaryKey"`
  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. if utils.NeedDateOrTimeFormat(utils.DbDriverName) {
  135. for i := range items {
  136. items[i] = utils.GormDateStrToDateStr(items[i])
  137. }
  138. }
  139. return
  140. }
  141. type BaseFromYongyiData struct {
  142. YongyiDataId int `gorm:"column:yongyi_data_id;primaryKey"`
  143. YongyiIndexId int
  144. IndexCode string
  145. DataTime string
  146. Value string
  147. CreateTime string
  148. ModifyTime string
  149. DataTimestamp int64 `gorm:"column:data_timestamp"`
  150. }
  151. func (m *BaseFromYongyiData) AfterFind(tx *gorm.DB) (err error) {
  152. if utils.NeedDateOrTimeFormat(utils.DbDriverName) {
  153. if m.CreateTime != "" {
  154. m.CreateTime = utils.GormDateStrToDateTimeStr(m.CreateTime)
  155. }
  156. if m.ModifyTime != "" {
  157. m.ModifyTime = utils.GormDateStrToDateTimeStr(m.ModifyTime)
  158. }
  159. if m.DataTime != "" {
  160. m.DataTime = utils.GormDateStrToDateStr(m.DataTime)
  161. }
  162. }
  163. return
  164. }
  165. type BaseFromYongyiIndexSearchItem struct {
  166. YongyiIndexId int `gorm:"column:yongyi_index_id;primaryKey"`
  167. ClassifyId int
  168. ParentClassifyId int
  169. IndexCode string
  170. IndexName string
  171. }
  172. // GetYongyiItemList 模糊查询Yongyi数据库指标列表
  173. func GetYongyiItemList(condition string) (items []*BaseFromYongyiIndexSearchItem, err error) {
  174. o := global.DbMap[utils.DbNameIndex]
  175. sql := "SELECT * FROM base_from_yongyi_index WHERE 1=1"
  176. if condition != "" {
  177. sql += condition
  178. }
  179. err = o.Raw(sql).Find(&items).Error
  180. return
  181. }
  182. func GetYongyiIndexDataByCode(indexCode string) (list []*BaseFromYongyiData, err error) {
  183. o := global.DbMap[utils.DbNameIndex]
  184. sql := `SELECT * FROM base_from_yongyi_data WHERE index_code=? `
  185. err = o.Raw(sql, indexCode).Find(&list).Error
  186. return
  187. }
  188. func GetBaseFromYongyiIndexByIndexCode(indexCode string) (list *BaseFromYongyiIndex, err error) {
  189. o := global.DbMap[utils.DbNameIndex]
  190. sql := ` SELECT * FROM base_from_yongyi_index WHERE index_code=? `
  191. err = o.Raw(sql, indexCode).First(&list).Error
  192. return
  193. }
  194. type BaseFromYongyiIndexType struct {
  195. Type2 string `gorm:"column:type_2"`
  196. Type3 string `gorm:"column:type_3"`
  197. }
  198. // Update 更新Yongyi指标基础信息
  199. func (item *BaseFromYongyiIndex) Update(cols []string) (err error) {
  200. o := global.DbMap[utils.DbNameIndex]
  201. err = o.Select(cols).Updates(item).Error
  202. return
  203. }
  204. // EditYongyiIndexInfoResp 新增指标的返回
  205. type EditYongyiIndexInfoResp struct {
  206. YongyiIndexId int `description:"指标ID"`
  207. IndexCode string `description:"指标code"`
  208. }