base_from_ccf.go 9.5 KB

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