base_from_usda_fas.go 11 KB

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