smm_data.go 11 KB

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