base_from_sci_hq_index.go 13 KB

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