base_from_usda_fas.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package data_manage
  2. import (
  3. "eta/eta_api/global"
  4. "eta/eta_api/utils"
  5. "time"
  6. "github.com/rdlucklib/rdluck_tools/paging"
  7. )
  8. type BaseFromUsdaFasIndex struct {
  9. BaseFromUsdaFasIndexId int `orm:"column(base_from_usda_fas_index_id);pk" gorm:"primaryKey"`
  10. ClassifyId int
  11. IndexCode string
  12. IndexName string
  13. Frequency string
  14. Unit string
  15. Sort int
  16. StartDate string `description:"开始日期"`
  17. EndDate string `description:"结束日期"`
  18. EndValue float64
  19. CreateTime time.Time
  20. ModifyTime time.Time
  21. }
  22. type BaseFromUsdaFasIndexList struct {
  23. BaseFromUsdaFasIndexId int `orm:"column(base_from_usda_fas_index_id);pk" gorm:"primaryKey"`
  24. ClassifyId int
  25. Interface string
  26. EdbInfoId int
  27. EdbUniqueCode string `description:"指标库唯一编码"`
  28. EdbClassifyId int `description:"指标库分类ID"`
  29. StartDate string
  30. EndDate string
  31. EndValue float64
  32. IndexCode string
  33. IndexName string
  34. Frequency string
  35. Unit string
  36. Sort int
  37. CreateTime string
  38. ModifyTime string
  39. EdbExist int `description:"指标库是否已添加:0-否;1-是"`
  40. DataList []*BaseFromUsdaFasData `gorm:"-"`
  41. Paging *paging.PagingItem `description:"分页数据" gorm:"-"`
  42. }
  43. type BaseFromUsdaFasIndexSearchList struct {
  44. List []*BaseFromUsdaFasIndexList
  45. Paging *paging.PagingItem `description:"分页数据"`
  46. }
  47. type UsdaFasSingleDataResp struct {
  48. BaseFromUsdaFasIndexId int
  49. ClassifyId int
  50. EdbInfoId int
  51. IndexCode string
  52. IndexName string
  53. Frequency string
  54. Unit string
  55. StartTime string
  56. CreateTime string
  57. ModifyTime string
  58. EdbExist int `description:"指标库是否已添加:0-否;1-是"`
  59. Data []*UsdaFasSingleData
  60. }
  61. type UsdaFasSingleData struct {
  62. Value string `orm:"column(value)" description:"日期"`
  63. DataTime string `orm:"column(data_time)" description:"值"`
  64. }
  65. func GetUsdaFasIndexByClassifyId(classifyId int) (items []*BaseFromUsdaFasIndex, err error) {
  66. o := global.DbMap[utils.DbNameIndex]
  67. sql := ` SELECT base_from_usda_fas_index_id, classify_id, index_code, index_name FROM base_from_usda_fas_index WHERE classify_id=? ORDER BY sort ASC, base_from_usda_fas_index_id ASC `
  68. err = o.Raw(sql, classifyId).Find(&items).Error
  69. return
  70. }
  71. func GetUsdaFasIndex(condition string, pars []interface{}) (items []*BaseFromUsdaFasIndexList, err error) {
  72. o := global.DbMap[utils.DbNameIndex]
  73. sql := ` SELECT * FROM base_from_usda_fas_index WHERE 1=1 `
  74. if condition != "" {
  75. sql += condition
  76. }
  77. sql += ` ORDER BY sort ASC, base_from_usda_fas_index_id asc`
  78. err = o.Raw(sql, pars...).Find(&items).Error
  79. return
  80. }
  81. func GetUsdaFasIndexPage(condition string, pars []interface{}, startSize, pageSize int) (items []*BaseFromUsdaFasIndexList, err error) {
  82. o := global.DbMap[utils.DbNameIndex]
  83. sql := ` SELECT * FROM base_from_usda_fas_index WHERE 1=1 `
  84. if condition != "" {
  85. sql += condition
  86. }
  87. sql += ` ORDER BY sort ASC, base_from_usda_fas_index_id asc LIMIT ?,?`
  88. pars = append(pars, startSize, pageSize)
  89. err = o.Raw(sql, pars...).Find(&items).Error
  90. return
  91. }
  92. func GetUsdaFasIndexPageCount(condition string, pars []interface{}) (count int, err error) {
  93. o := global.DbMap[utils.DbNameIndex]
  94. sql := ` SELECT COUNT(1) AS count FROM base_from_usda_fas_index WHERE 1=1 `
  95. if condition != "" {
  96. sql += condition
  97. }
  98. err = o.Raw(sql, pars...).Scan(&count).Error
  99. return
  100. }
  101. func GetUsdaFasIndexDataCount(indexCode string) (count int, err error) {
  102. o := global.DbMap[utils.DbNameIndex]
  103. sql := ` SELECT COUNT(1) AS count FROM base_from_usda_fas_data WHERE index_code=? `
  104. err = o.Raw(sql, indexCode).Scan(&count).Error
  105. return
  106. }
  107. func GetUsdaFasIndexDataByDataTime(indexCodes []string, startDate, endDate string) (items []*BaseFromUsdaFasData, err error) {
  108. o := global.DbMap[utils.DbNameIndex]
  109. sql := ` SELECT * FROM base_from_usda_fas_data WHERE index_code in (` + utils.GetOrmInReplace(len(indexCodes)) + `) and data_time >=? and data_time <? ORDER BY data_time DESC `
  110. err = o.Raw(sql, indexCodes, startDate, endDate).Find(&items).Error
  111. return
  112. }
  113. func GetUsdaFasIndexDataTimePageByCodes(indexCodes []string, startSize, pageSize int) (dataTimes []string, err error) {
  114. o := global.DbMap[utils.DbNameIndex]
  115. sql := ` SELECT data_time FROM base_from_usda_fas_data WHERE index_code in (` + utils.GetOrmInReplace(len(indexCodes)) + `) GROUP BY data_time ORDER BY data_time DESC LIMIT ?,? `
  116. err = o.Raw(sql, indexCodes, startSize, pageSize).Scan(&dataTimes).Error
  117. return
  118. }
  119. func GetUsdaFasIndexDataTimePageCount(indexCodes []string) (count int, err error) {
  120. o := global.DbMap[utils.DbNameIndex]
  121. sql := ` SELECT COUNT(DISTINCT data_time) AS count FROM base_from_usda_fas_data WHERE index_code in (` + utils.GetOrmInReplace(len(indexCodes)) + `) `
  122. err = o.Raw(sql, indexCodes).Scan(&count).Error
  123. return
  124. }
  125. func GetUsdaFasIndexDataByCodes(indexCode []string) (items []*BaseFromUsdaFasData, err error) {
  126. if len(indexCode) == 0 {
  127. return
  128. }
  129. o := global.DbMap[utils.DbNameIndex]
  130. sql := ` SELECT * FROM base_from_usda_fas_data WHERE index_code in (` + utils.GetOrmInReplace(len(indexCode)) + `) ORDER BY data_time DESC `
  131. err = o.Raw(sql, indexCode).Find(&items).Error
  132. return
  133. }
  134. // GetUsdaFasByConditionAndFrequency 根据条件获取涌益咨询指标列表
  135. func GetUsdaFasByConditionAndFrequency(condition, frequency string, pars []interface{}) (items []*BaseFromUsdaFasIndex, err error) {
  136. o := global.DbMap[utils.DbNameIndex]
  137. sql := ` SELECT * FROM base_from_usda_fas_index WHERE 1=1 `
  138. if condition != "" {
  139. sql += condition
  140. }
  141. sql += ` AND frequency=?`
  142. sql += ` ORDER BY sort ASC, base_from_usda_fas_index_id ASC`
  143. err = o.Raw(sql, pars, frequency).Find(&items).Error
  144. return
  145. }
  146. func GetUsdaFasFrequencyByCondition(condition string, pars []interface{}) (items []string, err error) {
  147. sql := `SELECT DISTINCT frequency FROM base_from_usda_fas_index WHERE 1=1 `
  148. if condition != "" {
  149. sql += condition
  150. }
  151. sql += ` ORDER BY FIELD(frequency,'日度','周度','旬度','月度','季度','半年度','年度') `
  152. o := global.DbMap[utils.DbNameIndex]
  153. err = o.Raw(sql, pars...).Find(&items).Error
  154. return
  155. }
  156. // GetUsdaFasDataDataTimeByIndexId 根据指标id获取指标数据的日期列表
  157. func GetUsdaFasDataDataTimeByIndexId(indexIdList []int) (items []string, err error) {
  158. if len(indexIdList) == 0 {
  159. return
  160. }
  161. o := global.DbMap[utils.DbNameIndex]
  162. sql := ` SELECT DISTINCT data_time FROM base_from_usda_fas_data WHERE base_from_usda_fas_index_id IN (` + utils.GetOrmInReplace(len(indexIdList)) + `) ORDER BY data_time DESC`
  163. err = o.Raw(sql, indexIdList).Find(&items).Error
  164. return
  165. }
  166. type BaseFromUsdaFasData struct {
  167. BaseFromUsdaFasDataId int `orm:"column(base_from_usda_fas_data_id);pk" gorm:"primaryKey"`
  168. BaseFromUsdaFasIndexId int
  169. IndexCode string
  170. DataTime string
  171. Value string
  172. CreateTime string
  173. ModifyTime string
  174. DataTimestamp int64
  175. }
  176. type BaseFromUsdaFasIndexSearchItem struct {
  177. BaseFromUsdaFasIndexId int `orm:"column(base_from_usda_fas_index_id);pk" gorm:"primaryKey"`
  178. ClassifyId int
  179. ParentClassifyId int
  180. IndexCode string
  181. IndexName string
  182. }
  183. // BatchCheckUsdaFasEdbReq 指标数据结构体
  184. type BatchCheckUsdaFasEdbReq struct {
  185. //IsJoinEdb int `form:"IsJoinEdb" description:"是否加到指标库,0:未加到指标库"`
  186. Frequencies string `description:"频度;枚举值:日度、周度、月度、季度、半年度、年度"`
  187. Keyword string `description:"关键字"`
  188. ClassifyIds string `description:"所选品种id列表"`
  189. ListAll bool `form:"ListAll" json:"ListAll" description:"列表全选"`
  190. TradeCodeList string `form:"TradeCodeList" json:"TradeCodeList" description:"全选为false时, 该数组为选中; 全选为true时, 该数组为不选的指标"`
  191. }
  192. // GetUsdaFasItemList 模糊查询UsdaFas数据库指标列表
  193. func GetUsdaFasItemList(condition string) (items []*BaseFromUsdaFasIndexSearchItem, err error) {
  194. o := global.DbMap[utils.DbNameIndex]
  195. sql := "SELECT * FROM base_from_usda_fas_index WHERE 1=1"
  196. if condition != "" {
  197. sql += condition
  198. }
  199. err = o.Raw(sql).Find(&items).Error
  200. return
  201. }
  202. func GetUsdaFasIndexDataByCode(indexCode string) (list []*BaseFromUsdaFasData, err error) {
  203. o := global.DbMap[utils.DbNameIndex]
  204. sql := `SELECT * FROM base_from_usda_fas_data WHERE index_code=? `
  205. err = o.Raw(sql, indexCode).Find(&list).Error
  206. return
  207. }
  208. func GetBaseFromUsdaFasIndexByIndexCode(indexCode string) (list *BaseFromUsdaFasIndex, err error) {
  209. o := global.DbMap[utils.DbNameIndex]
  210. sql := ` SELECT * FROM base_from_usda_fas_index WHERE index_code=? `
  211. err = o.Raw(sql, indexCode).First(&list).Error
  212. return
  213. }
  214. type BaseFromUsdaFasIndexType struct {
  215. Type2 string `orm:"column(type_2)"`
  216. Type3 string `orm:"column(type_3)"`
  217. }
  218. // Update 更新UsdaFas指标基础信息
  219. func (item *BaseFromUsdaFasIndex) Update(cols []string) (err error) {
  220. o := global.DbMap[utils.DbNameIndex]
  221. err = o.Select(cols).Updates(item).Error
  222. return
  223. }
  224. // EditUsdaFasIndexInfoResp 新增指标的返回
  225. type EditUsdaFasIndexInfoResp struct {
  226. BaseFromUsdaFasIndexId int `description:"指标ID"`
  227. IndexCode string `description:"指标code"`
  228. }
  229. type UsdaFasIndexSource2EdbReq struct {
  230. EdbCode string
  231. EdbName string
  232. Frequency string
  233. Unit string
  234. ClassifyId int
  235. AdminId int
  236. AdminRealName string
  237. }
  238. func GetUsdaFasFrequencyByClassifyId(classifyId int) (items []*GlFrequency, err error) {
  239. o := global.DbMap[utils.DbNameIndex]
  240. sql := ` SELECT frequency FROM base_from_usda_fas_index WHERE classify_id = ? `
  241. sql += ` GROUP BY frequency ORDER BY frequency ASC `
  242. err = o.Raw(sql, classifyId).Find(&items).Error
  243. return
  244. }