base_from_ccf.go 9.3 KB

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