base_from_ccf.go 9.5 KB

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