smm_data.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. package data_manage
  2. import (
  3. "eta/eta_api/global"
  4. "eta/eta_api/utils"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "github.com/shopspring/decimal"
  7. "gorm.io/gorm"
  8. "time"
  9. )
  10. type SmmClassify struct {
  11. TypeName string `orm:"column(type_name)" description:"分类名称"`
  12. TypeCode string `orm:"column(type_code)" description:"分类名称编码"`
  13. }
  14. func GetSmmClassify() (items []*SmmClassify, err error) {
  15. sql := `SELECT CONCAT(type_2,type_3) AS type_name,CONCAT(type_2,'#',type_3) type_code FROM base_from_smm_index GROUP BY CONCAT(type_2,type_3) ORDER BY CONCAT(type_2,type_3) ASC `
  16. o := global.DbMap[utils.DbNameIndex]
  17. err = o.Raw(sql).Find(&items).Error
  18. return
  19. }
  20. type SmmFrequency struct {
  21. Frequency string `description:"频度"`
  22. }
  23. func GetSmmFrequencyByClassifyId(classifyId int) (items []*GlFrequency, err error) {
  24. o := global.DbMap[utils.DbNameIndex]
  25. sql := ` SELECT frequency FROM base_from_smm_index WHERE classify_id = ? `
  26. sql += ` GROUP BY frequency ORDER BY frequency ASC `
  27. err = o.Raw(sql, classifyId).Find(&items).Error
  28. return
  29. }
  30. type SmmIndex struct {
  31. BaseFromSmmIndexId int `gorm:"column:base_from_smm_index_id;primaryKey"`
  32. Interface string
  33. Name string
  34. IndexCode string
  35. IndexName string
  36. Type1 string `gorm:"column:type_1"`
  37. Type2 string `gorm:"column:type_2"`
  38. Type3 string `gorm:"column:type_3"`
  39. Frequency string
  40. Unit string
  41. ApiStartTime string
  42. ApiUpdateTime string
  43. StartTime string
  44. FinishTime string
  45. CreateTime string
  46. ModifyTime string
  47. IsStop int `description:"是否停更:1:停更,0:未停更"`
  48. EndValue float64 `description:"指标的最新值"`
  49. }
  50. func (obj *SmmIndex) AfterFind(tx *gorm.DB) (err error) {
  51. obj.CreateTime = utils.GormDateStrToDateTimeStr(obj.CreateTime)
  52. obj.ModifyTime = utils.GormDateStrToDateTimeStr(obj.ModifyTime)
  53. obj.StartTime = utils.GormDateStrToDateTimeStr(obj.StartTime)
  54. obj.FinishTime = utils.GormDateStrToDateTimeStr(obj.FinishTime)
  55. obj.ApiStartTime = utils.GormDateStrToDateTimeStr(obj.ApiStartTime)
  56. obj.ApiUpdateTime = utils.GormDateStrToDateTimeStr(obj.ApiUpdateTime)
  57. return
  58. }
  59. type SmmIndexItem struct {
  60. BaseFromSmmIndexId int `orm:"column(base_from_smm_index_id);pk" gorm:"primaryKey"`
  61. ClassifyId int
  62. ParentClassifyId int
  63. Interface string
  64. Name string
  65. IndexCode string
  66. IndexName string
  67. Type1 string `gorm:"column:type_1"`
  68. Type2 string `gorm:"column:type_2"`
  69. Type3 string `gorm:"column:type_3"`
  70. Frequency string
  71. Unit string
  72. ApiStartTime string
  73. ApiUpdateTime string
  74. StartTime string
  75. FinishTime string
  76. CreateTime string
  77. ModifyTime string
  78. IsStop int `description:"是否停更:1:停更,0:未停更"`
  79. EndValue float64 `description:"指标的最新值"`
  80. }
  81. func GetSmmIndex(condition string, pars []interface{}) (items []*SmmIndex, err error) {
  82. o := global.DbMap[utils.DbNameIndex]
  83. sql := ` SELECT * FROM base_from_smm_index WHERE 1=1 `
  84. if condition != "" {
  85. sql += condition
  86. }
  87. sql += `ORDER BY sort ASC, base_from_smm_index_id asc`
  88. err = o.Raw(sql, pars...).Find(&items).Error
  89. return
  90. }
  91. // GetSmmIndexById
  92. // @Description: 根据id获取指标信息
  93. // @author: Roc
  94. // @datetime 2024-01-10 14:25:26
  95. // @param basFromSmmIndexId int
  96. // @return item *SmmIndex
  97. // @return err error
  98. func GetSmmIndexById(basFromSmmIndexId int) (item *SmmIndex, err error) {
  99. o := global.DbMap[utils.DbNameIndex]
  100. sql := ` SELECT * FROM base_from_smm_index WHERE base_from_smm_index_id = ? `
  101. err = o.Raw(sql, basFromSmmIndexId).Find(&item).Error
  102. return
  103. }
  104. type SmmExportIndex struct {
  105. TypeName string
  106. IndexCode string
  107. IndexName string
  108. Type1 string `gorm:"column:type_1"`
  109. Type2 string `gorm:"column:type_2"`
  110. Type3 string `gorm:"column:type_3"`
  111. Frequency string
  112. Unit string
  113. ModifyTime string
  114. }
  115. func GetExportSmmIndex(typeCodes []string) (items []*SmmExportIndex, err error) {
  116. if len(typeCodes) == 0 {
  117. return
  118. }
  119. o := global.DbMap[utils.DbNameIndex]
  120. sql := ` SELECT *,CONCAT(type_2, "#", type_3) AS type_name FROM base_from_smm_index WHERE CONCAT(type_2, "#", type_3) IN (` + utils.GetOrmInReplace(len(typeCodes)) + `) ORDER BY frequency ASC,index_code ASC`
  121. err = o.Raw(sql, typeCodes).Find(&items).Error
  122. return
  123. }
  124. func GetSmmFrequency(classifyId int) (items []*string, err error) {
  125. sql := `SELECT DISTINCT frequency FROM base_from_smm_index WHERE classify_id=? ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年','年度') `
  126. o := global.DbMap[utils.DbNameIndex]
  127. err = o.Raw(sql, classifyId).Find(&items).Error
  128. return
  129. }
  130. func GetSmmFrequencyByCode(code string) (items []*string, err error) {
  131. sql := `SELECT DISTINCT frequency FROM base_from_smm_index WHERE index_code=? ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年','年度') `
  132. o := global.DbMap[utils.DbNameIndex]
  133. err = o.Raw(sql, code).Find(&items).Error
  134. return
  135. }
  136. type SmmIndexList struct {
  137. BaseFromSmmIndexId int `gorm:"column:base_from_smm_index_id);primaryKey"`
  138. Interface string
  139. Name string
  140. IndexCode string
  141. IndexName string
  142. Type1 string `gorm:"column:type_1"`
  143. Type2 string `gorm:"column:type_2"`
  144. Type3 string `gorm:"column:type_3"`
  145. Frequency string
  146. Unit string
  147. ApiStartTime string
  148. ApiUpdateTime string
  149. StartTime string
  150. FinishTime string
  151. ModifyTime string
  152. DataList []*SmmIndexData `gorm:"-"`
  153. Paging *paging.PagingItem `description:"分页数据" gorm:"-"`
  154. }
  155. type SmmIndexData struct {
  156. Value decimal.Decimal `gorm:"column:value" description:"日期"`
  157. DataTime string `gorm:"column:data_time" description:"值"`
  158. }
  159. func (m *SmmIndexData) AfterFind(tx *gorm.DB) (err error) {
  160. m.DataTime = utils.GormDateStrToDateStr(m.DataTime)
  161. return
  162. }
  163. func GetSmmIndexData(indexCode string, startSize, pageSize int) (items []*SmmIndexData, err error) {
  164. o := global.DbMap[utils.DbNameIndex]
  165. sql := ` SELECT * FROM base_from_smm_data WHERE index_code=? ORDER BY data_time DESC LIMIT ?,? `
  166. err = o.Raw(sql, indexCode, startSize, pageSize).Find(&items).Error
  167. return
  168. }
  169. func GetSmmIndexDataCount(indexCode string) (count int, err error) {
  170. o := global.DbMap[utils.DbNameIndex]
  171. sql := ` SELECT COUNT(1) AS count FROM base_from_smm_data WHERE index_code=? `
  172. err = o.Raw(sql, indexCode).Scan(&count).Error
  173. return
  174. }
  175. // GetSmmItemList 模糊查询Smm数据库指标列表
  176. func GetSmmItemList(keyword string) (items []*SmmIndexItem, err error) {
  177. o := global.DbMap[utils.DbNameIndex]
  178. sql := "SELECT * FROM base_from_smm_index WHERE CONCAT(index_name,index_code) LIKE ? "
  179. err = o.Raw(sql, utils.GetLikeKeyword(keyword)).Find(&items).Error
  180. return
  181. }
  182. func GetSmmIndexDataByCode(indexCode string) (items []*SmmIndexData, err error) {
  183. o := global.DbMap[utils.DbNameIndex]
  184. sql := ` SELECT * FROM base_from_smm_data WHERE index_code=? ORDER BY data_time DESC `
  185. err = o.Raw(sql, indexCode).Find(&items).Error
  186. return
  187. }
  188. func GetSmmDataMaxCount(classifyId int) (count int, err error) {
  189. o := global.DbMap[utils.DbNameIndex]
  190. sql := `SELECT COALESCE(MAX(t.num), 0) AS count FROM (
  191. SELECT COUNT(1) AS num FROM base_from_smm_index AS a
  192. INNER JOIN base_from_smm_data AS b ON a.index_code=b.index_code
  193. WHERE a.classify_id=?
  194. GROUP BY a.base_from_smm_index_id
  195. )AS t `
  196. err = o.Raw(sql, classifyId).Scan(&count).Error
  197. return
  198. }
  199. type ExportSmmDataMaxCount struct {
  200. TypeName string
  201. Count int
  202. }
  203. func GetExportSmmDataMaxCount(typeCodes []string) (items []*ExportSmmDataMaxCount, err error) {
  204. if len(typeCodes) == 0 {
  205. return
  206. }
  207. o := global.DbMap[utils.DbNameIndex]
  208. sql := ` SELECT
  209. COALESCE(MAX(t.num), 0) AS count,
  210. t.type_name
  211. FROM
  212. (
  213. SELECT
  214. COUNT(1) AS num,
  215. CONCAT(a.type_2, "#", a.type_3) AS type_name
  216. FROM
  217. base_from_smm_index AS a
  218. INNER JOIN base_from_smm_data AS b ON a.index_code = b.index_code
  219. WHERE
  220. CONCAT(a.type_2, "#", a.type_3) IN (` + utils.GetOrmInReplace(len(typeCodes)) + `)
  221. GROUP BY
  222. a.base_from_smm_index_id
  223. ) AS t
  224. GROUP BY
  225. type_name `
  226. err = o.Raw(sql, typeCodes).Find(&items).Error
  227. return
  228. }
  229. type ExportSmmIndexData struct {
  230. Value string `orm:"column(value)" description:"日期"`
  231. DataTime string `orm:"column(data_time)" description:"值"`
  232. IndexCode string `orm:"column(index_code)" description:"指标编码"`
  233. }
  234. func GetExportSmmIndexDataByCodes(indexCodes []string) (items []*ExportSmmIndexData, err error) {
  235. if len(indexCodes) == 0 {
  236. return
  237. }
  238. o := global.DbMap[utils.DbNameIndex]
  239. sql := ` SELECT index_code,data_time,value FROM base_from_smm_data WHERE index_code IN (` + utils.GetOrmInReplace(len(indexCodes)) + `) ORDER BY data_time DESC `
  240. err = o.Raw(sql, indexCodes).Find(&items).Error
  241. return
  242. }
  243. // GetSmmBaseInfoList
  244. // @Description: 获取有色数据列表
  245. // @author: Roc
  246. // @datetime 2024-01-10 14:28:29
  247. // @param condition string
  248. // @param pars []interface{}
  249. // @param orderBy string
  250. // @param startSize int
  251. // @param pageSize int
  252. // @return total int
  253. // @return items []*BaseRefreshEdbInfo
  254. // @return err error
  255. func GetSmmBaseInfoList(condition string, pars []interface{}, orderBy string, startSize, pageSize int) (total int, items []*BaseRefreshEdbInfo, err error) {
  256. o := global.DbMap[utils.DbNameIndex]
  257. // 数量汇总
  258. totalSql := ` SELECT count(1) FROM base_from_smm_index WHERE 1=1 `
  259. if condition != "" {
  260. totalSql += condition
  261. }
  262. err = o.Raw(totalSql, pars...).Scan(&total).Error
  263. if err != nil {
  264. return
  265. }
  266. // 列表数据
  267. sql := ` SELECT base_from_smm_index_id as edb_info_id, classify_id,index_code,index_name,end_date,end_value,frequency,is_stop,terminal_code FROM base_from_smm_index WHERE 1=1 `
  268. if condition != "" {
  269. sql += condition
  270. }
  271. if orderBy != "" {
  272. sql += ` ORDER BY ` + orderBy
  273. } else {
  274. sql += ` ORDER BY base_from_smm_index_id ASC `
  275. }
  276. sql += ` LIMIT ?,? `
  277. pars = append(pars, startSize, pageSize)
  278. err = o.Raw(sql, pars...).Find(&items).Error
  279. return
  280. }
  281. // ModifySmmUpdateStatus
  282. // @Description: 修改有色数据停更状态
  283. // @author: Roc
  284. // @datetime 2024-01-08 16:23:31
  285. // @param edbIdList []int
  286. // @param indexCodeList []string
  287. // @param isStop int
  288. // @return err error
  289. func ModifySmmUpdateStatus(edbIdList []int, indexCodeList []string, isStop int) (err error) {
  290. idNum := len(edbIdList)
  291. if idNum <= 0 {
  292. return
  293. }
  294. o := global.DbMap[utils.DbNameIndex].Begin()
  295. defer func() {
  296. if err != nil {
  297. _ = o.Rollback()
  298. return
  299. }
  300. _ = o.Commit()
  301. }()
  302. // 更改数据源的更新状态
  303. sql := ` UPDATE base_from_smm_index SET is_stop = ? WHERE base_from_smm_index_id IN (` + utils.GetOrmInReplace(idNum) + `) `
  304. err = o.Exec(sql, isStop, edbIdList).Error
  305. if err != nil {
  306. return
  307. }
  308. codeNum := len(indexCodeList)
  309. if codeNum <= 0 {
  310. // 需要通过指标id列表查找code列表
  311. sql := ` SELECT index_code FROM base_from_smm_index WHERE base_from_smm_index_id IN (` + utils.GetOrmInReplace(idNum) + `) `
  312. err = o.Raw(sql, edbIdList).Scan(&indexCodeList).Error
  313. if err != nil {
  314. return
  315. }
  316. }
  317. codeNum = len(indexCodeList)
  318. // 查出来的编码是空的话,那么就直接返回了
  319. if codeNum <= 0 {
  320. return
  321. }
  322. // 更改指标的更新状态
  323. sql = ` UPDATE edb_info SET no_update = ?, set_update_time=? WHERE source = ? AND sub_source= ? AND edb_code IN (` + utils.GetOrmInReplace(codeNum) + `) `
  324. err = o.Exec(sql, isStop, time.Now(), utils.DATA_SOURCE_YS, 0, indexCodeList).Error
  325. if err != nil {
  326. return
  327. }
  328. return
  329. }