base_from_sci_hq_index.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 string `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(condition string, pars []interface{}) (count int, err error) {
  182. o := orm.NewOrmUsingDB("data")
  183. sql := `SELECT MAX(t.num) AS count FROM ( SELECT COUNT(1) AS num FROM base_from_sci_hq_index AS a INNER JOIN base_from_sci_hq_data AS b ON a.index_code=b.index_code WHERE 1=1 `
  184. if condition != "" {
  185. sql += condition
  186. }
  187. sql += ` GROUP BY a.base_from_sci_hq_index_id) AS t `
  188. err = o.Raw(sql, pars).QueryRow(&count)
  189. return
  190. }
  191. // GetSciHqIndexMaxSortByClassifyId 根据分类id获取指标最大排序
  192. func GetSciHqIndexMaxSortByClassifyId(classifyId int) (sort int, err error) {
  193. o := orm.NewOrmUsingDB("data")
  194. sql := `SELECT MAX(sort) FROM base_from_sci_index WHERE classify_id=? `
  195. err = o.Raw(sql, classifyId).QueryRow(&sort)
  196. return
  197. }
  198. func GetSciHqFrequency(classifyId int) (items []*string, err error) {
  199. sql := `SELECT DISTINCT frequency FROM base_from_sci_hq_index WHERE classify_id=? ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年','年度') `
  200. o := orm.NewOrmUsingDB("data")
  201. _, err = o.Raw(sql, classifyId).QueryRows(&items)
  202. return
  203. }
  204. func GetSciHqFrequencyByCondition(condition string, pars []interface{}) (items []*string, err error) {
  205. sql := `SELECT DISTINCT frequency FROM base_from_sci_hq_index WHERE 1=1 `
  206. if condition != "" {
  207. sql += condition
  208. }
  209. sql += ` ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年','年度') `
  210. o := orm.NewOrmUsingDB("data")
  211. _, err = o.Raw(sql, pars...).QueryRows(&items)
  212. return
  213. }
  214. func GetSciHqFrequencyByCode(code string) (items []*string, err error) {
  215. sql := `SELECT DISTINCT frequency FROM base_from_sci_hq_index WHERE index_code=? ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年','年度') `
  216. o := orm.NewOrmUsingDB("data")
  217. _, err = o.Raw(sql, code).QueryRows(&items)
  218. return
  219. }
  220. // GetSciHqClassifyMaxSortByClassifyIds 通过分类id获取对应分类的最大sort
  221. func GetSciHqClassifyMaxSortByClassifyIds(classifyIds []int) (items []*BaseFromSciHqClassifyMaxSort, err error) {
  222. if len(classifyIds) == 0 {
  223. return
  224. }
  225. o := orm.NewOrmUsingDB("data")
  226. sql := `SELECT bc.classify_id, COALESCE(MAX(bi.sort), 0) AS max_sort FROM base_from_sci_hq_classify AS bc
  227. LEFT JOIN base_from_sci_hq_index AS bi
  228. ON bc.classify_id=bi.classify_id
  229. WHERE bc.classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `)
  230. GROUP BY bc.classify_id
  231. `
  232. // 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 `
  233. _, err = o.Raw(sql, classifyIds).QueryRows(&items)
  234. return
  235. }
  236. func BatchModifySciHqIndexClassify(items []*BaseFromSciHqIndex) (err error) {
  237. o := orm.NewOrmUsingDB("data")
  238. sql := `UPDATE base_from_sci_hq_index SET classify_id=?, sort=? WHERE base_from_sci_hq_index_id=? `
  239. p, err := o.Raw(sql).Prepare()
  240. if err != nil {
  241. return
  242. }
  243. defer func() {
  244. p.Close()
  245. }()
  246. for _, v := range items {
  247. _, err = p.Exec(v.ClassifyId, v.Sort, v.BaseFromSciHqIndexId)
  248. if err != nil {
  249. return
  250. }
  251. }
  252. return
  253. }
  254. // MoveDownSciHqIndexBySort 往下移动
  255. func MoveDownSciHqIndexBySort(classifyId, prevSort, currentSort int) (err error) {
  256. o := orm.NewOrmUsingDB("data")
  257. sql := `update base_from_sci_hq_index set sort = sort - 1 where classify_id=? and sort <= ? and sort> ? `
  258. _, err = o.Raw(sql, classifyId, prevSort, currentSort).Exec()
  259. return
  260. }
  261. // MoveUpSciHqIndexBySort 往上移动
  262. func MoveUpSciHqIndexBySort(classifyId, nextSort, currentSort int) (err error) {
  263. o := orm.NewOrmUsingDB("data")
  264. sql := `update base_from_sci_hq_index set sort = sort + 1 where classify_id=? and sort >= ? and sort< ?`
  265. _, err = o.Raw(sql, classifyId, nextSort, currentSort).Exec()
  266. return
  267. }
  268. func DeleteSciHqIndexById(indexId int) (err error) {
  269. o := orm.NewOrmUsingDB("data")
  270. to, err := o.Begin()
  271. if err != nil {
  272. return
  273. }
  274. defer func() {
  275. if err != nil {
  276. _ = to.Rollback()
  277. } else {
  278. _ = to.Commit()
  279. }
  280. }()
  281. sql := `DELETE FROM base_from_sci_hq_index WHERE base_from_sci_hq_index_id=? `
  282. _, err = to.Raw(sql, indexId).Exec()
  283. if err != nil {
  284. return
  285. }
  286. sql = `DELETE FROM base_from_sci_hq_data WHERE base_from_sci_hq_index_id=? `
  287. _, err = to.Raw(sql, indexId).Exec()
  288. if err != nil {
  289. return
  290. }
  291. return
  292. }
  293. func DeleteSciHqIndexByIds(indexIds []int) (err error) {
  294. if len(indexIds) == 0 {
  295. return
  296. }
  297. o := orm.NewOrmUsingDB("data")
  298. to, err := o.Begin()
  299. if err != nil {
  300. return
  301. }
  302. defer func() {
  303. if err != nil {
  304. _ = to.Rollback()
  305. } else {
  306. _ = to.Commit()
  307. }
  308. }()
  309. sql := `DELETE FROM base_from_sci_hq_index WHERE base_from_sci_hq_index_id IN (` + utils.GetOrmInReplace(len(indexIds)) + `) `
  310. _, err = o.Raw(sql, indexIds).Exec()
  311. if err != nil {
  312. return
  313. }
  314. sql = `DELETE FROM base_from_sci_hq_data WHERE base_from_sci_hq_index_id IN (` + utils.GetOrmInReplace(len(indexIds)) + `) `
  315. _, err = o.Raw(sql, indexIds).Exec()
  316. if err != nil {
  317. return
  318. }
  319. return
  320. }
  321. // MoveSciHqIndex 移动指标分类
  322. func MoveSciHqIndex(indexId, classifyId int) (err error) {
  323. o := orm.NewOrmUsingDB("data")
  324. sql := ` UPDATE base_from_sci_hq_index
  325. SET
  326. classify_id = ?, modify_time=NOW()
  327. WHERE base_from_sci_hq_index_id = ?`
  328. _, err = o.Raw(sql, classifyId, indexId).Exec()
  329. return
  330. }
  331. // GetSciHqIndexMinSortByClassifyId 获取最小不等于0的排序
  332. func GetSciHqIndexMinSortByClassifyId(classifyId int) (sort int, err error) {
  333. o := orm.NewOrmUsingDB("data")
  334. sql := `SELECT min(sort) FROM base_from_sci_hq_index WHERE classify_id=? and sort <> 0 `
  335. err = o.Raw(sql, classifyId).QueryRow(&sort)
  336. return
  337. }