base_from_sci_hq_index.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. package data_manage
  2. import (
  3. "eta/eta_api/utils"
  4. "time"
  5. "github.com/beego/beego/v2/client/orm"
  6. )
  7. type BaseFromSciHqIndex struct {
  8. BaseFromSciHqIndexId int `orm:"pk"`
  9. ClassifyId int `description:"指标分类id"`
  10. IndexCode string `description:"指标编码"`
  11. IndexName string `description:"指标名称"`
  12. Unit string `description:"单位"`
  13. Frequency string `description:"频度"`
  14. StartDate string `description:"开始日期"`
  15. EndDate string `description:"结束日期"`
  16. Sort int `description:"排序"`
  17. LatestValue float64 `description:"最新值"`
  18. LatestDate time.Time `description:"最后更新时间"`
  19. TerminalCode string
  20. FilePath string `json:"-"`
  21. CreateTime time.Time
  22. ModifyTime time.Time
  23. }
  24. type BaseFromSciHqIndexView struct {
  25. BaseFromSciHqIndexId int `orm:"pk"`
  26. EdbInfoId int `description:"指标库id"`
  27. ClassifyId int `description:"指标分类id"`
  28. IndexCode string `description:"指标编码"`
  29. IndexName string `description:"指标名称"`
  30. UniqueCode string `description:"唯一code"`
  31. Frequency string `description:"频度"`
  32. Unit string `description:"单位"`
  33. StartDate string `description:"开始日期"`
  34. EndDate string `description:"结束日期"`
  35. Sort int `description:"排序"`
  36. LatestValue float64 `description:"最新值"`
  37. LatestDate time.Time `description:"最后更新时间"`
  38. EdbExist int `description:"edb是否存在"`
  39. ModifyTime string
  40. }
  41. func (b *BaseFromSciHqIndex) Update(cols []string) (err error) {
  42. o := orm.NewOrmUsingDB("data")
  43. _, err = o.Update(b, cols...)
  44. return
  45. }
  46. // GetSciHqIndexByCondition 根据条件获取卓创红期指标列表
  47. func GetSciHqIndexByCondition(condition string, pars []interface{}) (items []*BaseFromSciHqIndex, err error) {
  48. o := orm.NewOrmUsingDB("data")
  49. sql := ` SELECT * FROM base_from_sci_hq_index WHERE 1=1 `
  50. if condition != "" {
  51. sql += condition
  52. }
  53. sql += ` ORDER BY sort ASC, base_from_sci_hq_index_id ASC`
  54. _, err = o.Raw(sql, pars).QueryRows(&items)
  55. return
  56. }
  57. // GetSciHqIndexByCondition 根据条件获取卓创红期指标列表
  58. func GetSciHqIndexByConditionAndFrequency(condition, frequency string, pars []interface{}) (items []*BaseFromSciHqIndex, err error) {
  59. o := orm.NewOrmUsingDB("data")
  60. sql := ` SELECT * FROM base_from_sci_hq_index WHERE 1=1 `
  61. if condition != "" {
  62. sql += condition
  63. }
  64. sql += ` AND frequency=?`
  65. sql += ` ORDER BY sort ASC, base_from_sci_hq_index_id ASC`
  66. _, err = o.Raw(sql, pars, frequency).QueryRows(&items)
  67. return
  68. }
  69. func GetSciHqIndexCountByCondition(condition string, pars []interface{}) (count int, err error) {
  70. o := orm.NewOrmUsingDB("data")
  71. sql := ` SELECT COUNT(*) AS count FROM base_from_sci_hq_index WHERE 1=1 `
  72. if condition != "" {
  73. sql += condition
  74. }
  75. sql += ` ORDER BY sort ASC, base_from_sci_hq_index_id ASC`
  76. err = o.Raw(sql, pars).QueryRow(&count)
  77. return
  78. }
  79. // GetSciHqIndexAndEdbInfoByCondition 根据条件获取卓创红期index和指标库的信息
  80. func GetSciHqIndexAndEdbInfoByCondition(condition string, pars []interface{}) (items []*BaseFromSciHqIndexView, err error) {
  81. o := orm.NewOrmUsingDB("data")
  82. sql := ` SELECT b.*, e.edb_info_id FROM base_from_sci_hq_index AS b LEFT JOIN edb_info AS e ON b.index_code=e.edb_code AND e.source=? WHERE 1=1 `
  83. if condition != "" {
  84. sql += condition
  85. }
  86. sql += ` ORDER BY sort ASC `
  87. _, err = o.Raw(sql, utils.DATA_SOURCE_SCI_HQ, pars).QueryRows(&items)
  88. return
  89. }
  90. // GetSciHqIndexByIndexCode 根据指标编码获取指标信息
  91. func GetSciHqIndexByIndexCode(indexCode string) (item *BaseFromSciHqIndex, err error) {
  92. o := orm.NewOrmUsingDB("data")
  93. sql := ` SELECT * FROM base_from_sci_hq_index WHERE index_code=? `
  94. err = o.Raw(sql, indexCode).QueryRow(&item)
  95. return
  96. }
  97. // GetSciHqIndexByIndexId 根据指标id获取指标信息
  98. func GetSciHqIndexByIndexId(indexId int) (item *BaseFromSciHqIndex, err error) {
  99. o := orm.NewOrmUsingDB("data")
  100. sql := ` SELECT * FROM base_from_sci_hq_index WHERE base_from_sci_hq_index_id=? `
  101. err = o.Raw(sql, indexId).QueryRow(&item)
  102. return
  103. }
  104. // GetSciHqIndexListByIndexIds 根据指标id获取指标信息
  105. func GetSciHqIndexListByIndexIds(indexIds []int) (items []*BaseFromSciHqIndex, err error) {
  106. if len(indexIds) == 0 {
  107. return
  108. }
  109. o := orm.NewOrmUsingDB("data")
  110. sql := ` SELECT * FROM base_from_sci_hq_index WHERE base_from_sci_hq_index_id IN (` + utils.GetOrmInReplace(len(indexIds)) + `) `
  111. _, err = o.Raw(sql, indexIds).QueryRows(&items)
  112. return
  113. }
  114. // GetSciHqIndexCountByClassifyIds 获取分类下指标的个数
  115. func GetSciHqIndexCountByClassifyIds(classifyIds []int) (count int, err error) {
  116. o := orm.NewOrmUsingDB("data")
  117. num := len(classifyIds)
  118. if num <= 0 {
  119. return
  120. }
  121. sql := `SELECT COUNT(1) AS count FROM base_from_sci_hq_index WHERE classify_id IN (` + utils.GetOrmInReplace(num) + `) `
  122. err = o.Raw(sql, classifyIds).QueryRow(&count)
  123. return
  124. }
  125. // GetSciHqIndexByClassifyId 根据分类id获取卓创红期指标列表
  126. func GetSciHqIndexByClassifyId(classifyIds []int, startSize, pageSize int) (items []*BaseFromSciHqIndexView, err error) {
  127. o := orm.NewOrmUsingDB("data")
  128. sql := ` SELECT b.*, e.edb_info_id,
  129. CASE WHEN e.edb_info_id IS NULL THEN 0 ELSE 1 END AS edb_exist
  130. FROM base_from_sci_hq_index AS b
  131. LEFT JOIN edb_info AS e ON b.index_code=e.edb_code AND e.source=88
  132. WHERE b.classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `) ORDER BY b.sort ASC LIMIT ?,? `
  133. _, err = o.Raw(sql, classifyIds, startSize, pageSize).QueryRows(&items)
  134. return
  135. }
  136. // GetSciHqIndexCountByClassifyId 根据分类id获取卓创红期指标数量
  137. func GetSciHqIndexCountByClassifyId(classifyIds []int) (count int, err error) {
  138. if len(classifyIds) == 0 {
  139. return
  140. }
  141. o := orm.NewOrmUsingDB("data")
  142. sql := ` SELECT COUNT(*) AS count FROM base_from_sci_hq_index WHERE classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `) `
  143. err = o.Raw(sql, classifyIds).QueryRow(&count)
  144. return
  145. }
  146. // GetSciHqIndexCount 获取卓创红期指标数量
  147. func GetSciHqIndexCount() (count int, err error) {
  148. o := orm.NewOrmUsingDB("data")
  149. sql := ` SELECT COUNT(*) AS count FROM base_from_sci_hq_index `
  150. err = o.Raw(sql).QueryRow(&count)
  151. return
  152. }
  153. func GetSciHqIndexByPage(startSize, pageSize int) (items []*BaseFromSciHqIndexView, err error) {
  154. o := orm.NewOrmUsingDB("data")
  155. sql := ` SELECT b.*, e.edb_info_id,
  156. CASE WHEN e.edb_info_id IS NULL THEN 0 ELSE 1 END AS edb_exist
  157. FROM base_from_sci_hq_index AS b
  158. LEFT JOIN edb_info AS e ON b.index_code=e.edb_code AND e.source=88
  159. ORDER BY b.modify_time DESC LIMIT ?,?`
  160. _, err = o.Raw(sql, startSize, pageSize).QueryRows(&items)
  161. return
  162. }
  163. // GetSciHqIndexBaseInfoByClassifyId 根据分类id获取卓创红期指标列表
  164. func GetSciHqIndexBaseInfoByClassifyId(classifyId int) (items []*BaseFromSciHqIndexView, err error) {
  165. o := orm.NewOrmUsingDB("data")
  166. sql := ` SELECT base_from_sci_hq_index_id, classify_id, index_code, index_name, CONCAT(classify_id, '_', base_from_sci_hq_index_id) AS unique_code FROM base_from_sci_hq_index WHERE classify_id = ? ORDER BY sort ASC `
  167. _, err = o.Raw(sql, classifyId).QueryRows(&items)
  168. return
  169. }
  170. // GetSciHqIndexBaseInfoByClassifyId 根据分类id获取卓创红期指标列表
  171. func GetSciHqIndexBaseInfoByCondition(condition string, pars []interface{}) (items []*BaseFromSciHqIndex, err error) {
  172. o := orm.NewOrmUsingDB("data")
  173. sql := ` SELECT base_from_sci_hq_index_id, index_code, index_name FROM base_from_sci_hq_index WHERE 1=1 `
  174. if condition != "" {
  175. sql += condition
  176. }
  177. sql += ` ORDER BY sort ASC `
  178. _, err = o.Raw(sql, pars...).QueryRows(&items)
  179. return
  180. }
  181. func GetSciHqDataMaxCount(classifyId int) (count int, err error) {
  182. o := orm.NewOrmUsingDB("data")
  183. sql := `SELECT MAX(t.num) AS count FROM (
  184. SELECT COUNT(1) AS num FROM base_from_sci_hq_index AS a
  185. INNER JOIN base_from_sci_hq_data AS b ON a.index_code=b.index_code
  186. WHERE a.classify_id=?
  187. GROUP BY a.base_from_sci_hq_index_id
  188. )AS t `
  189. err = o.Raw(sql, classifyId).QueryRow(&count)
  190. return
  191. }
  192. // GetSciHqIndexMaxSortByClassifyId 根据分类id获取指标最大排序
  193. func GetSciHqIndexMaxSortByClassifyId(classifyId int) (sort int, err error) {
  194. o := orm.NewOrmUsingDB("data")
  195. sql := `SELECT MAX(sort) FROM base_from_sci_index WHERE classify_id=? `
  196. err = o.Raw(sql, classifyId).QueryRow(&sort)
  197. return
  198. }
  199. func GetSciHqFrequency(classifyId int) (items []*string, err error) {
  200. sql := `SELECT DISTINCT frequency FROM base_from_sci_hq_index WHERE classify_id=? ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年','年度') `
  201. o := orm.NewOrmUsingDB("data")
  202. _, err = o.Raw(sql, classifyId).QueryRows(&items)
  203. return
  204. }
  205. func GetSciHqFrequencyByCondition(condition string, pars []interface{}) (items []*string, err error) {
  206. sql := `SELECT DISTINCT frequency FROM base_from_sci_hq_index WHERE 1=1 `
  207. if condition != "" {
  208. sql += condition
  209. }
  210. sql += ` ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年','年度') `
  211. o := orm.NewOrmUsingDB("data")
  212. _, err = o.Raw(sql, pars...).QueryRows(&items)
  213. return
  214. }
  215. func GetSciHqFrequencyByCode(code string) (items []*string, err error) {
  216. sql := `SELECT DISTINCT frequency FROM base_from_sci_hq_index WHERE index_code=? ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年','年度') `
  217. o := orm.NewOrmUsingDB("data")
  218. _, err = o.Raw(sql, code).QueryRows(&items)
  219. return
  220. }
  221. // GetSciHqClassifyMaxSortByClassifyIds 通过分类id获取对应分类的最大sort
  222. func GetSciHqClassifyMaxSortByClassifyIds(classifyIds []int) (items []*BaseFromSciHqClassifyMaxSort, err error) {
  223. if len(classifyIds) == 0 {
  224. return
  225. }
  226. o := orm.NewOrmUsingDB("data")
  227. sql := `SELECT bc.classify_id, COALESCE(MAX(bi.sort), 0) AS max_sort FROM base_from_sci_hq_classify AS bc
  228. LEFT JOIN base_from_sci_hq_index AS bi
  229. ON bc.classify_id=bi.classify_id
  230. WHERE bc.classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `)
  231. GROUP BY bc.classify_id
  232. `
  233. // sql = ` SELECT classify_id, MAX(sort) AS max_sort FROM base_from_sci_hq_index WHERE classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `) GROUP BY classify_id `
  234. _, err = o.Raw(sql, classifyIds).QueryRows(&items)
  235. return
  236. }
  237. func BatchModifySciHqIndexClassify(items []*BaseFromSciHqIndex) (err error) {
  238. o := orm.NewOrmUsingDB("data")
  239. sql := `UPDATE base_from_sci_hq_index SET classify_id=?, sort=? WHERE base_from_sci_hq_index_id=? `
  240. p, err := o.Raw(sql).Prepare()
  241. if err != nil {
  242. return
  243. }
  244. defer func() {
  245. p.Close()
  246. }()
  247. for _, v := range items {
  248. _, err = p.Exec(v.ClassifyId, v.Sort, v.BaseFromSciHqIndexId)
  249. if err != nil {
  250. return
  251. }
  252. }
  253. return
  254. }
  255. // MoveDownSciHqIndexBySort 往下移动
  256. func MoveDownSciHqIndexBySort(classifyId, prevSort, currentSort int) (err error) {
  257. o := orm.NewOrmUsingDB("data")
  258. sql := `update base_from_sci_hq_index set sort = sort - 1 where classify_id=? and sort <= ? and sort> ? `
  259. _, err = o.Raw(sql, classifyId, prevSort, currentSort).Exec()
  260. return
  261. }
  262. // MoveUpSciHqIndexBySort 往上移动
  263. func MoveUpSciHqIndexBySort(classifyId, nextSort, currentSort int) (err error) {
  264. o := orm.NewOrmUsingDB("data")
  265. sql := `update base_from_sci_hq_index set sort = sort + 1 where classify_id=? and sort >= ? and sort< ?`
  266. _, err = o.Raw(sql, classifyId, nextSort, currentSort).Exec()
  267. return
  268. }
  269. func DeleteSciHqIndexById(indexId int) (err error) {
  270. o := orm.NewOrmUsingDB("data")
  271. to, err := o.Begin()
  272. if err != nil {
  273. return
  274. }
  275. defer func() {
  276. if err != nil {
  277. _ = to.Rollback()
  278. } else {
  279. _ = to.Commit()
  280. }
  281. }()
  282. sql := `DELETE FROM base_from_sci_hq_index WHERE base_from_sci_hq_index_id=? `
  283. _, err = to.Raw(sql, indexId).Exec()
  284. if err != nil {
  285. return
  286. }
  287. sql = `DELETE FROM base_from_sci_hq_data WHERE base_from_sci_hq_index_id=? `
  288. _, err = to.Raw(sql, indexId).Exec()
  289. if err != nil {
  290. return
  291. }
  292. return
  293. }
  294. func DeleteSciHqIndexByIds(indexIds []int) (err error) {
  295. if len(indexIds) == 0 {
  296. return
  297. }
  298. o := orm.NewOrmUsingDB("data")
  299. to, err := o.Begin()
  300. if err != nil {
  301. return
  302. }
  303. defer func() {
  304. if err != nil {
  305. _ = to.Rollback()
  306. } else {
  307. _ = to.Commit()
  308. }
  309. }()
  310. sql := `DELETE FROM base_from_sci_hq_index WHERE base_from_sci_hq_index_id IN (` + utils.GetOrmInReplace(len(indexIds)) + `) `
  311. _, err = o.Raw(sql, indexIds).Exec()
  312. if err != nil {
  313. return
  314. }
  315. sql = `DELETE FROM base_from_sci_hq_data WHERE base_from_sci_hq_index_id IN (` + utils.GetOrmInReplace(len(indexIds)) + `) `
  316. _, err = o.Raw(sql, indexIds).Exec()
  317. if err != nil {
  318. return
  319. }
  320. return
  321. }
  322. // MoveSciHqIndex 移动指标分类
  323. func MoveSciHqIndex(indexId, classifyId int) (err error) {
  324. o := orm.NewOrmUsingDB("data")
  325. sql := ` UPDATE base_from_sci_hq_index
  326. SET
  327. classify_id = ?, modify_time=NOW()
  328. WHERE base_from_sci_hq_index_id = ?`
  329. _, err = o.Raw(sql, classifyId, indexId).Exec()
  330. return
  331. }
  332. // GetSciHqIndexMinSortByClassifyId 获取最小不等于0的排序
  333. func GetSciHqIndexMinSortByClassifyId(classifyId int) (sort int, err error) {
  334. o := orm.NewOrmUsingDB("data")
  335. sql := `SELECT min(sort) FROM base_from_sci_hq_index WHERE classify_id=? and sort <> 0 `
  336. err = o.Raw(sql, classifyId).QueryRow(&sort)
  337. return
  338. }