base_from_sci_hq_index.go 13 KB

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