smm_data.go 11 KB

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