base_from_ccf.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. package data_manage
  2. import (
  3. "database/sql"
  4. "eta/eta_api/global"
  5. "eta/eta_api/utils"
  6. "time"
  7. "github.com/rdlucklib/rdluck_tools/paging"
  8. "gorm.io/gorm"
  9. )
  10. type BaseFromCCFIndex struct {
  11. BaseFromCcfIndexId int `orm:"column(base_from_ccf_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 BaseFromCCFIndexList struct {
  22. BaseFromCcfIndexId int `orm:"column(base_from_ccf_index_id);pk"`
  23. EdbInfoId int
  24. ClassifyId int
  25. IndexCode string
  26. IndexName string
  27. Frequency string
  28. Unit string
  29. Sort int
  30. CreateTime string
  31. ModifyTime string
  32. EndDate string
  33. EndValue string
  34. EdbExist int `description:"指标库是否已添加:0-否;1-是"`
  35. DataList []*BaseFromCCFData `gorm:"-"`
  36. Paging *paging.PagingItem `description:"分页数据" gorm:"-"`
  37. }
  38. func (b *BaseFromCCFIndexList) TableName() string {
  39. return "base_from_ccf_index"
  40. }
  41. func (b *BaseFromCCFIndexList) AfterFind(tx *gorm.DB) (err error) {
  42. if utils.NeedDateOrTimeFormat(utils.DbDriverName) {
  43. b.ModifyTime = utils.GormDateStrToDateTimeStr(b.ModifyTime)
  44. b.CreateTime = utils.GormDateStrToDateTimeStr(b.CreateTime)
  45. b.EndDate = utils.GormDateStrToDateStr(b.EndDate)
  46. }
  47. return
  48. }
  49. type CCFSingleDataResp struct {
  50. BaseFromCcfIndexId int
  51. ClassifyId int
  52. IndexCode string
  53. IndexName string
  54. Frequency string
  55. Unit string
  56. CreateTime string
  57. ModifyTime string
  58. Data []*CCFSingleData
  59. }
  60. type CCFSingleData struct {
  61. Value string `orm:"column(value)" description:"日期"`
  62. DataTime string `orm:"column(data_time)" description:"值"`
  63. }
  64. func GetCCFIndex(condition string, pars []interface{}) (items []*BaseFromCCFIndexList, err error) {
  65. sql := ` SELECT * FROM base_from_ccf_index WHERE 1=1 `
  66. if condition != "" {
  67. sql += condition
  68. }
  69. sql += ` ORDER BY sort ASC, base_from_ccf_index_id asc`
  70. err = global.DbMap[utils.DbNameIndex].Raw(sql, pars...).Find(&items).Error
  71. return
  72. }
  73. func GetCCFIndexDataCount(indexCode string) (count int, err error) {
  74. sqlStr := ` SELECT COUNT(1) AS count FROM base_from_ccf_data WHERE index_code=? `
  75. var totalNull sql.NullInt64
  76. err = global.DbMap[utils.DbNameIndex].Raw(sqlStr, indexCode).Scan(&totalNull).Error
  77. if !totalNull.Valid {
  78. count = 0
  79. } else {
  80. count = int(totalNull.Int64)
  81. }
  82. return
  83. }
  84. type CCFIndexDataCountGroup struct {
  85. IndexCode string
  86. Count int
  87. }
  88. func GetCCFIndexDataCountGroup(indexCodes []string) (items []*CCFIndexDataCountGroup, err error) {
  89. if len(indexCodes) <= 0 {
  90. return
  91. }
  92. sql := ` SELECT COUNT(1) AS count, index_code FROM base_from_ccf_data WHERE index_code IN (` + utils.GetOrmInReplace(len(indexCodes)) + `) GROUP BY index_code`
  93. err = global.DbMap[utils.DbNameIndex].Raw(sql, indexCodes).Find(&items).Error
  94. return
  95. }
  96. func GetCCFIndexData(indexCode string, startSize, pageSize int) (items []*BaseFromCCFData, err error) {
  97. sql := ` SELECT * FROM base_from_ccf_data WHERE index_code=? ORDER BY data_time DESC LIMIT ?,? `
  98. err = global.DbMap[utils.DbNameIndex].Raw(sql, indexCode, startSize, pageSize).Find(&items).Error
  99. return
  100. }
  101. func GetCCFIndexDataByCodes(indexCode []string) (items []*BaseFromCCFData, err error) {
  102. if len(indexCode) <= 0 {
  103. return
  104. }
  105. sql := ` SELECT * FROM base_from_ccf_data WHERE index_code in (` + utils.GetOrmInReplace(len(indexCode)) + `) ORDER BY data_time DESC `
  106. err = global.DbMap[utils.DbNameIndex].Raw(sql, indexCode).Find(&items).Error
  107. return
  108. }
  109. type BaseFromCCFData struct {
  110. BaseFromCcfDataId int `orm:"column(base_from_ccf_data_id);pk"`
  111. BaseFromCcfIndexId int
  112. IndexCode string
  113. DataTime string
  114. Value string
  115. CreateTime string
  116. ModifyTime string
  117. DataTimestamp int64
  118. }
  119. func (baseFromCCFData *BaseFromCCFData) AfterFind(tx *gorm.DB) (err error) {
  120. if utils.NeedDateOrTimeFormat(utils.DbDriverName) {
  121. if baseFromCCFData.ModifyTime != "" {
  122. baseFromCCFData.ModifyTime = utils.GormDateStrToDateTimeStr(baseFromCCFData.ModifyTime)
  123. }
  124. if baseFromCCFData.CreateTime != "" {
  125. baseFromCCFData.CreateTime = utils.GormDateStrToDateTimeStr(baseFromCCFData.CreateTime)
  126. }
  127. if baseFromCCFData.DataTime != "" {
  128. baseFromCCFData.DataTime = utils.GormDateStrToDateStr(baseFromCCFData.DataTime)
  129. }
  130. }
  131. return
  132. }
  133. type BaseFromCCFIndexSearchItem struct {
  134. BaseFromCcfIndexId int `orm:"column(base_from_ccf_index_id);pk"`
  135. ClassifyId int
  136. IndexCode string
  137. IndexName string
  138. }
  139. // GetCCFItemList 模糊查询CCF数据库指标列表
  140. func GetCCFItemList(condition string) (items []*BaseFromCCFIndexSearchItem, err error) {
  141. sql := "SELECT * FROM base_from_ccf_index WHERE 1=1"
  142. if condition != "" {
  143. sql += condition
  144. }
  145. err = global.DbMap[utils.DbNameIndex].Raw(sql).Find(&items).Error
  146. return
  147. }
  148. func GetCCFIndexDataByCode(indexCode string) (list []*BaseFromCCFData, err error) {
  149. sql := `SELECT * FROM base_from_ccf_data WHERE index_code=? `
  150. err = global.DbMap[utils.DbNameIndex].Raw(sql, indexCode).Find(&list).Error
  151. return
  152. }
  153. func GetBaseFromCCFIndexByIndexCode(indexCode string) (list *BaseFromCCFIndex, err error) {
  154. sql := ` SELECT * FROM base_from_ccf_index WHERE index_code=? `
  155. err = global.DbMap[utils.DbNameIndex].Raw(sql, indexCode).First(&list).Error
  156. return
  157. }
  158. func GetCCFIndexPage(condition string, pars []interface{}, startSize, pageSize int) (items []*BaseFromCCFIndexList, err error) {
  159. sql := ` SELECT * FROM base_from_ccf_index WHERE 1=1 `
  160. if condition != "" {
  161. sql += condition
  162. }
  163. sql += ` ORDER BY sort ASC, base_from_ccf_index_id asc LIMIT ?,?`
  164. pars = append(pars, startSize, pageSize)
  165. err = global.DbMap[utils.DbNameIndex].Raw(sql, pars...).Find(&items).Error
  166. return
  167. }
  168. func GetCCFIndexPageCount(condition string, pars []interface{}) (count int, err error) {
  169. sql := ` SELECT COUNT(1) AS count FROM base_from_ccf_index WHERE 1=1 `
  170. if condition != "" {
  171. sql += condition
  172. }
  173. err = global.DbMap[utils.DbNameIndex].Raw(sql, pars...).Scan(&count).Error
  174. return
  175. }
  176. type BaseFromCCFIndexSearchList struct {
  177. List []*BaseFromCCFIndexList
  178. Paging *paging.PagingItem `description:"分页数据"`
  179. }
  180. type CCFIndexSource2EdbReq struct {
  181. EdbCode string
  182. EdbName string
  183. Frequency string
  184. Unit string
  185. ClassifyId int
  186. AdminId int
  187. AdminRealName string
  188. }
  189. // BatchCheckCCFEdbReq 指标数据结构体
  190. type BatchCheckCCFEdbReq struct {
  191. //IsJoinEdb int `form:"IsJoinEdb" description:"是否加到指标库,0:未加到指标库"`
  192. Frequencies string `description:"频度;枚举值:日度、周度、月度、季度、半年度、年度"`
  193. Keyword string `description:"关键字"`
  194. ClassifyIds string `description:"所选品种id列表"`
  195. ListAll bool `form:"ListAll" json:"ListAll" description:"列表全选"`
  196. TradeCodeList string `form:"TradeCodeList" json:"TradeCodeList" description:"全选为false时, 该数组为选中; 全选为true时, 该数组为不选的指标"`
  197. }
  198. func GetCCFFrequencyByClassifyId(classifyId int) (items []*GlFrequency, err error) {
  199. sql := ` SELECT frequency FROM base_from_ccf_index WHERE classify_id = ? `
  200. sql += ` GROUP BY frequency ORDER BY frequency ASC `
  201. err = global.DbMap[utils.DbNameIndex].Raw(sql, classifyId).Find(&items).Error
  202. return
  203. }
  204. func GetCCFIndexDataByDataTime(indexCodes []string, startDate, endDate string) (items []*BaseFromCCFData, err error) {
  205. sql := ` SELECT * FROM base_from_ccf_data WHERE index_code in (` + utils.GetOrmInReplace(len(indexCodes)) + `) and data_time >=? and data_time <? ORDER BY data_time DESC `
  206. err = global.DbMap[utils.DbNameIndex].Raw(sql, indexCodes, startDate, endDate).Find(&items).Error
  207. return
  208. }
  209. func GetCCFIndexDataTimePageByCodes(indexCodes []string, startSize, pageSize int) (dataTimes []string, err error) {
  210. sql := ` SELECT data_time FROM base_from_ccf_data WHERE index_code in (` + utils.GetOrmInReplace(len(indexCodes)) + `) GROUP BY data_time ORDER BY data_time DESC LIMIT ?,? `
  211. err = global.DbMap[utils.DbNameIndex].Raw(sql, indexCodes, startSize, pageSize).Find(&dataTimes).Error
  212. if utils.NeedDateOrTimeFormat(utils.DbDriverName) {
  213. for i := range dataTimes {
  214. dataTimes[i] = utils.GormDateStrToDateStr(dataTimes[i])
  215. }
  216. }
  217. return
  218. }
  219. func GetCCFIndexDataTimePageCount(indexCodes []string) (count int, err error) {
  220. //sqlStr := ` SELECT COUNT(DISTINCT data_time) AS count FROM base_from_ccf_data WHERE index_code in (` + utils.GetOrmInReplace(len(indexCodes)) + `)`
  221. sqlStr := ` SELECT COUNT(DISTINCT data_time) AS count FROM base_from_ccf_data WHERE index_code in ?`
  222. var totalNull sql.NullInt64
  223. err = global.DbMap[utils.DbNameIndex].Raw(sqlStr, indexCodes).Scan(&count).Error
  224. if !totalNull.Valid {
  225. count = 0
  226. } else {
  227. count = int(totalNull.Int64)
  228. }
  229. return
  230. }
  231. // GetCCFDataDataTimeByIndexId 根据指标id获取指标数据的日期列表
  232. func GetCCFDataDataTimeByIndexId(indexIdList []int) (items []string, err error) {
  233. if len(indexIdList) == 0 {
  234. return
  235. }
  236. sql := ` SELECT DISTINCT data_time FROM base_from_ccf_data WHERE base_from_ccf_index_id IN (` + utils.GetOrmInReplace(len(indexIdList)) + `) ORDER BY data_time DESC`
  237. err = global.DbMap[utils.DbNameIndex].Raw(sql, indexIdList).Scan(&items).Error
  238. if err != nil {
  239. return
  240. }
  241. if utils.NeedDateOrTimeFormat(utils.DbDriverName) {
  242. for i := range items {
  243. items[i] = utils.GormDateStrToDateStr(items[i])
  244. }
  245. }
  246. return
  247. }
  248. func GetCCFIndexDataByIndexIdAndDataTime(indexId []int, dataTimeList []string) (items []*BaseFromCCFData, err error) {
  249. sql := ` SELECT * FROM base_from_ccf_data WHERE base_from_ccf_index_id in (` + utils.GetOrmInReplace(len(indexId)) + `) and data_time in (` + utils.GetOrmInReplace(len(dataTimeList)) + `) `
  250. err = global.DbMap[utils.DbNameIndex].Raw(sql, indexId, dataTimeList).Find(&items).Error
  251. return
  252. }