base_from_clarksons_index.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. package data_manage
  2. import (
  3. "eta/eta_api/utils"
  4. "github.com/rdlucklib/rdluck_tools/paging"
  5. "time"
  6. "github.com/beego/beego/v2/client/orm"
  7. )
  8. type BaseFromClarksonsIndex struct {
  9. BaseFromClarksonsIndexId int `orm:"pk"`
  10. ClassifyId int `description:"指标分类id"`
  11. IndexCode string `description:"指标编码"`
  12. IndexName string `description:"指标名称"`
  13. Unit string `description:"单位"`
  14. Frequency string `description:"频度"`
  15. StartDate string `description:"开始日期"`
  16. EndDate string `description:"结束日期"`
  17. Sort int `description:"排序"`
  18. CreateTime time.Time
  19. ModifyTime time.Time
  20. }
  21. type BaseFromClarksonsIndexView struct {
  22. BaseFromClarksonsIndexId int `orm:"pk"`
  23. EdbInfoId int `description:"指标库id"`
  24. ClassifyId int `description:"指标分类id"`
  25. IndexCode string `description:"指标编码"`
  26. IndexName string `description:"指标名称"`
  27. UniqueCode string `description:"唯一code"`
  28. Frequency string `description:"频度"`
  29. Unit string `description:"单位"`
  30. StartDate string `description:"开始日期"`
  31. EndDate string `description:"结束日期"`
  32. Sort int `description:"排序"`
  33. LatestDate string `description:"最后更新时间"`
  34. EdbExist int `description:"edb是否存在"`
  35. ModifyTime string
  36. }
  37. type BaseFromClarksonsIndexList struct {
  38. BaseFromClarksonsIndexId int `orm:"pk"`
  39. IndexCode string // 指标编码
  40. IndexName string // 指标名称
  41. ClassifyId int // 分类Id
  42. Unit string // 单位
  43. Frequency string // 频度
  44. Describe string // 指标描述
  45. CreateTime string // 创建时间
  46. ModifyTime string // 修改时间
  47. DataList []*BaseFromClarksonsData
  48. Paging *paging.PagingItem `description:"分页数据"`
  49. }
  50. func (b *BaseFromClarksonsIndex) Update(cols []string) (err error) {
  51. o := orm.NewOrmUsingDB("data")
  52. _, err = o.Update(b, cols...)
  53. return
  54. }
  55. // GetClarksonsIndexByCondition 根据条件获取克拉克森指标列表
  56. func GetClarksonsIndexByCondition(condition string, pars []interface{}) (items []*BaseFromClarksonsIndex, err error) {
  57. o := orm.NewOrmUsingDB("data")
  58. sql := ` SELECT * FROM base_from_clarksons_index WHERE 1=1 `
  59. if condition != "" {
  60. sql += condition
  61. }
  62. sql += ` ORDER BY sort ASC, base_from_clarksons_index_id ASC`
  63. _, err = o.Raw(sql, pars).QueryRows(&items)
  64. return
  65. }
  66. // GetClarksonsIndexByCondition 根据条件获取克拉克森指标列表
  67. func GetClarksonsIndexByConditionAndFrequency(condition, frequency string, pars []interface{}) (items []*BaseFromClarksonsIndex, err error) {
  68. o := orm.NewOrmUsingDB("data")
  69. sql := ` SELECT * FROM base_from_clarksons_index WHERE 1=1 `
  70. if condition != "" {
  71. sql += condition
  72. }
  73. sql += ` AND frequency=?`
  74. sql += ` ORDER BY sort ASC, base_from_clarksons_index_id ASC`
  75. _, err = o.Raw(sql, pars, frequency).QueryRows(&items)
  76. return
  77. }
  78. func GetClarksonsIndexCountByCondition(condition string, pars []interface{}) (count int, err error) {
  79. o := orm.NewOrmUsingDB("data")
  80. sql := ` SELECT COUNT(*) AS count FROM base_from_clarksons_index WHERE 1=1 `
  81. if condition != "" {
  82. sql += condition
  83. }
  84. sql += ` ORDER BY sort ASC, base_from_clarksons_index_id ASC`
  85. err = o.Raw(sql, pars).QueryRow(&count)
  86. return
  87. }
  88. // GetClarksonsIndexAndEdbInfoByCondition 根据条件获取克拉克森index和指标库的信息
  89. func GetClarksonsIndexAndEdbInfoByCondition(condition string, pars []interface{}) (items []*BaseFromClarksonsIndexView, err error) {
  90. o := orm.NewOrmUsingDB("data")
  91. sql := ` SELECT b.*, e.edb_info_id FROM base_from_clarksons_index AS b LEFT JOIN edb_info AS e ON b.index_code=e.edb_code AND e.source=? WHERE 1=1 `
  92. if condition != "" {
  93. sql += condition
  94. }
  95. sql += ` ORDER BY sort ASC `
  96. _, err = o.Raw(sql, utils.DATA_SOURCE_SCI_HQ, pars).QueryRows(&items)
  97. return
  98. }
  99. // GetClarksonsIndexByIndexCode 根据指标编码获取指标信息
  100. func GetClarksonsIndexByIndexCode(indexCode string) (item *BaseFromClarksonsIndex, err error) {
  101. o := orm.NewOrmUsingDB("data")
  102. sql := ` SELECT * FROM base_from_clarksons_index WHERE index_code=? `
  103. err = o.Raw(sql, indexCode).QueryRow(&item)
  104. return
  105. }
  106. // GetClarksonsIndexByIndexId 根据指标id获取指标信息
  107. func GetClarksonsIndexByIndexId(indexId int) (item *BaseFromClarksonsIndex, err error) {
  108. o := orm.NewOrmUsingDB("data")
  109. sql := ` SELECT * FROM base_from_clarksons_index WHERE base_from_clarksons_index_id=? `
  110. err = o.Raw(sql, indexId).QueryRow(&item)
  111. return
  112. }
  113. // GetClarksonsIndexListByIndexIds 根据指标id获取指标信息
  114. func GetClarksonsIndexListByIndexIds(indexIds []int) (items []*BaseFromClarksonsIndex, err error) {
  115. if len(indexIds) == 0 {
  116. return
  117. }
  118. o := orm.NewOrmUsingDB("data")
  119. sql := ` SELECT * FROM base_from_clarksons_index WHERE base_from_clarksons_index_id IN (` + utils.GetOrmInReplace(len(indexIds)) + `) `
  120. _, err = o.Raw(sql, indexIds).QueryRows(&items)
  121. return
  122. }
  123. // GetClarksonsIndexCountByClassifyIds 获取分类下指标的个数
  124. func GetClarksonsIndexCountByClassifyIds(classifyIds []int) (count int, err error) {
  125. o := orm.NewOrmUsingDB("data")
  126. num := len(classifyIds)
  127. if num <= 0 {
  128. return
  129. }
  130. sql := `SELECT COUNT(1) AS count FROM base_from_clarksons_index WHERE classify_id IN (` + utils.GetOrmInReplace(num) + `) `
  131. err = o.Raw(sql, classifyIds).QueryRow(&count)
  132. return
  133. }
  134. // GetClarksonsIndexByClassifyId 根据分类id获取克拉克森指标列表
  135. func GetClarksonsIndexByClassifyId(classifyIds []int, startSize, pageSize int) (items []*BaseFromClarksonsIndexView, err error) {
  136. o := orm.NewOrmUsingDB("data")
  137. sql := ` SELECT b.*, e.edb_info_id,
  138. CASE WHEN e.edb_info_id IS NULL THEN 0 ELSE 1 END AS edb_exist
  139. FROM base_from_clarksons_index AS b
  140. LEFT JOIN edb_info AS e ON b.index_code=e.edb_code AND e.source=101
  141. WHERE b.classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `) ORDER BY b.sort ASC LIMIT ?,? `
  142. _, err = o.Raw(sql, classifyIds, startSize, pageSize).QueryRows(&items)
  143. return
  144. }
  145. // GetClarksonsIndexCountByClassifyId 根据分类id获取克拉克森指标数量
  146. func GetClarksonsIndexCountByClassifyId(classifyIds []int) (count int, err error) {
  147. if len(classifyIds) == 0 {
  148. return
  149. }
  150. o := orm.NewOrmUsingDB("data")
  151. sql := ` SELECT COUNT(*) AS count FROM base_from_clarksons_index WHERE classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `) `
  152. err = o.Raw(sql, classifyIds).QueryRow(&count)
  153. return
  154. }
  155. // GetClarksonsIndexCount 获取克拉克森指标数量
  156. func GetClarksonsIndexCount() (count int, err error) {
  157. o := orm.NewOrmUsingDB("data")
  158. sql := ` SELECT COUNT(*) AS count FROM base_from_clarksons_index `
  159. err = o.Raw(sql).QueryRow(&count)
  160. return
  161. }
  162. func GetClarksonsIndexByPage(startSize, pageSize int) (items []*BaseFromClarksonsIndexView, err error) {
  163. o := orm.NewOrmUsingDB("data")
  164. sql := ` SELECT b.*, e.edb_info_id,
  165. CASE WHEN e.edb_info_id IS NULL THEN 0 ELSE 1 END AS edb_exist
  166. FROM base_from_clarksons_index AS b
  167. LEFT JOIN edb_info AS e ON b.index_code=e.edb_code AND e.source=101
  168. ORDER BY b.modify_time DESC LIMIT ?,?`
  169. _, err = o.Raw(sql, startSize, pageSize).QueryRows(&items)
  170. return
  171. }
  172. // GetClarksonsIndexBaseInfoByClassifyId 根据分类id获取克拉克森指标列表
  173. func GetClarksonsIndexBaseInfoByClassifyId(classifyId int) (items []*BaseFromClarksonsIndexView, err error) {
  174. o := orm.NewOrmUsingDB("data")
  175. sql := ` SELECT base_from_clarksons_index_id, classify_id, index_code, index_name, CONCAT(classify_id, '_', base_from_clarksons_index_id) AS unique_code FROM base_from_clarksons_index WHERE classify_id = ? ORDER BY sort ASC `
  176. _, err = o.Raw(sql, classifyId).QueryRows(&items)
  177. return
  178. }
  179. // GetClarksonsIndexBaseInfoByClassifyId 根据分类id获取克拉克森指标列表
  180. func GetClarksonsIndexBaseInfoByCondition(condition string, pars []interface{}) (items []*BaseFromClarksonsIndex, err error) {
  181. o := orm.NewOrmUsingDB("data")
  182. sql := ` SELECT base_from_clarksons_index_id, index_code, index_name FROM base_from_clarksons_index WHERE 1=1 `
  183. if condition != "" {
  184. sql += condition
  185. }
  186. sql += ` ORDER BY sort ASC `
  187. _, err = o.Raw(sql, pars...).QueryRows(&items)
  188. return
  189. }
  190. func GetClarksonsDataMaxCount(condition string, pars []interface{}) (count int, err error) {
  191. o := orm.NewOrmUsingDB("data")
  192. sql := `SELECT MAX(t.num) AS count FROM ( SELECT COUNT(1) AS num FROM base_from_clarksons_index AS a INNER JOIN base_from_clarksons_data AS b ON a.index_code=b.index_code WHERE 1=1 `
  193. if condition != "" {
  194. sql += condition
  195. }
  196. sql += ` GROUP BY a.base_from_clarksons_index_id) AS t `
  197. err = o.Raw(sql, pars).QueryRow(&count)
  198. return
  199. }
  200. // GetClarksonsIndexMaxSortByClassifyId 根据分类id获取指标最大排序
  201. func GetClarksonsIndexMaxSortByClassifyId(classifyId int) (sort int, err error) {
  202. o := orm.NewOrmUsingDB("data")
  203. sql := `SELECT MAX(sort) FROM base_from_clarksons_index WHERE classify_id=? `
  204. err = o.Raw(sql, classifyId).QueryRow(&sort)
  205. return
  206. }
  207. func GetClarksonsFrequency(classifyId int) (items []*string, err error) {
  208. sql := `SELECT DISTINCT frequency FROM base_from_clarksons_index WHERE classify_id=? ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年','年度') `
  209. o := orm.NewOrmUsingDB("data")
  210. _, err = o.Raw(sql, classifyId).QueryRows(&items)
  211. return
  212. }
  213. func GetClarksonsFrequencyByCondition(condition string, pars []interface{}) (items []*string, err error) {
  214. sql := `SELECT DISTINCT frequency FROM base_from_clarksons_index WHERE 1=1 `
  215. if condition != "" {
  216. sql += condition
  217. }
  218. sql += ` ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年','年度') `
  219. o := orm.NewOrmUsingDB("data")
  220. _, err = o.Raw(sql, pars...).QueryRows(&items)
  221. return
  222. }
  223. func GetClarksonsFrequencyByCode(code string) (items []*string, err error) {
  224. sql := `SELECT DISTINCT frequency FROM base_from_clarksons_index WHERE index_code=? ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年','年度') `
  225. o := orm.NewOrmUsingDB("data")
  226. _, err = o.Raw(sql, code).QueryRows(&items)
  227. return
  228. }
  229. // GetClarksonsClassifyMaxSortByClassifyIds 通过分类id获取对应分类的最大sort
  230. func GetClarksonsClassifyMaxSortByClassifyIds(classifyIds []int) (items []*BaseFromClarksonsClassifyMaxSort, err error) {
  231. if len(classifyIds) == 0 {
  232. return
  233. }
  234. o := orm.NewOrmUsingDB("data")
  235. sql := `SELECT bc.base_from_clarksons_classify_id, COALESCE(MAX(bi.sort), 0) AS max_sort FROM base_from_clarksons_classify AS bc
  236. LEFT JOIN base_from_clarksons_index AS bi
  237. ON bc.base_from_clarksons_classify_id=bi.classify_id
  238. WHERE bc.base_from_clarksons_classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `)
  239. GROUP BY bc.base_from_clarksons_classify_id
  240. `
  241. // sql = ` SELECT classify_id, MAX(sort) AS max_sort FROM base_from_clarksons_index WHERE classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `) GROUP BY classify_id `
  242. _, err = o.Raw(sql, classifyIds).QueryRows(&items)
  243. return
  244. }
  245. func BatchModifyClarksonsIndexClassify(items []*BaseFromClarksonsIndex) (err error) {
  246. o := orm.NewOrmUsingDB("data")
  247. sql := `UPDATE base_from_clarksons_index SET classify_id=?, sort=? WHERE base_from_clarksons_index_id=? `
  248. p, err := o.Raw(sql).Prepare()
  249. if err != nil {
  250. return
  251. }
  252. defer func() {
  253. p.Close()
  254. }()
  255. for _, v := range items {
  256. _, err = p.Exec(v.ClassifyId, v.Sort, v.BaseFromClarksonsIndexId)
  257. if err != nil {
  258. return
  259. }
  260. }
  261. return
  262. }
  263. // MoveDownClarksonsIndexBySort 往下移动
  264. func MoveDownClarksonsIndexBySort(classifyId, prevSort, currentSort int) (err error) {
  265. o := orm.NewOrmUsingDB("data")
  266. sql := `update base_from_clarksons_index set sort = sort - 1 where classify_id=? and sort <= ? and sort> ? `
  267. _, err = o.Raw(sql, classifyId, prevSort, currentSort).Exec()
  268. return
  269. }
  270. // MoveUpClarksonsIndexBySort 往上移动
  271. func MoveUpClarksonsIndexBySort(classifyId, nextSort, currentSort int) (err error) {
  272. o := orm.NewOrmUsingDB("data")
  273. sql := `update base_from_clarksons_index set sort = sort + 1 where classify_id=? and sort >= ? and sort< ?`
  274. _, err = o.Raw(sql, classifyId, nextSort, currentSort).Exec()
  275. return
  276. }
  277. func DeleteClarksonsIndexById(indexId int) (err error) {
  278. o := orm.NewOrmUsingDB("data")
  279. to, err := o.Begin()
  280. if err != nil {
  281. return
  282. }
  283. defer func() {
  284. if err != nil {
  285. _ = to.Rollback()
  286. } else {
  287. _ = to.Commit()
  288. }
  289. }()
  290. sql := `DELETE FROM base_from_clarksons_index WHERE base_from_clarksons_index_id=? `
  291. _, err = to.Raw(sql, indexId).Exec()
  292. if err != nil {
  293. return
  294. }
  295. sql = `DELETE FROM base_from_clarksons_data WHERE base_from_clarksons_index_id=? `
  296. _, err = to.Raw(sql, indexId).Exec()
  297. if err != nil {
  298. return
  299. }
  300. return
  301. }
  302. func DeleteClarksonsIndexByIds(indexIds []int) (err error) {
  303. if len(indexIds) == 0 {
  304. return
  305. }
  306. o := orm.NewOrmUsingDB("data")
  307. to, err := o.Begin()
  308. if err != nil {
  309. return
  310. }
  311. defer func() {
  312. if err != nil {
  313. _ = to.Rollback()
  314. } else {
  315. _ = to.Commit()
  316. }
  317. }()
  318. sql := `DELETE FROM base_from_clarksons_index WHERE base_from_clarksons_index_id IN (` + utils.GetOrmInReplace(len(indexIds)) + `) `
  319. _, err = o.Raw(sql, indexIds).Exec()
  320. if err != nil {
  321. return
  322. }
  323. sql = `DELETE FROM base_from_clarksons_data WHERE base_from_clarksons_index_id IN (` + utils.GetOrmInReplace(len(indexIds)) + `) `
  324. _, err = o.Raw(sql, indexIds).Exec()
  325. if err != nil {
  326. return
  327. }
  328. return
  329. }
  330. // MoveClarksonsIndex 移动指标分类
  331. func MoveClarksonsIndex(indexId, classifyId int) (err error) {
  332. o := orm.NewOrmUsingDB("data")
  333. sql := ` UPDATE base_from_clarksons_index
  334. SET
  335. classify_id = ?, modify_time=NOW()
  336. WHERE base_from_clarksons_index_id = ?`
  337. _, err = o.Raw(sql, classifyId, indexId).Exec()
  338. return
  339. }
  340. // GetClarksonsIndexMinSortByClassifyId 获取最小不等于0的排序
  341. func GetClarksonsIndexMinSortByClassifyId(classifyId int) (sort int, err error) {
  342. o := orm.NewOrmUsingDB("data")
  343. sql := `SELECT min(sort) FROM base_from_clarksons_index WHERE classify_id=? and sort <> 0 `
  344. err = o.Raw(sql, classifyId).QueryRow(&sort)
  345. return
  346. }
  347. // GetClarksonsIndexInfoCount 分页查询指标信息行数
  348. func GetClarksonsIndexInfoCount(condition string, pars []interface{}) (count int, err error) {
  349. o := orm.NewOrmUsingDB("data")
  350. sql := ` SELECT count(1) FROM base_from_clarksons_index WHERE index_code not in (select edb_code from edb_info) `
  351. if condition != "" {
  352. sql += condition
  353. }
  354. err = o.Raw(sql, pars).QueryRow(&count)
  355. return
  356. }
  357. // GetClarksonsIndexInfoPage 分页查询指标信息
  358. func GetClarksonsIndexInfoPage(condition string, pars []interface{}) (items []*BaseFromRzdIndexAndData, err error) {
  359. o := orm.NewOrmUsingDB("data")
  360. sql := ` SELECT * FROM base_from_clarksons_index WHERE index_code not in (select edb_code from edb_info) `
  361. if condition != "" {
  362. sql += condition
  363. }
  364. _, err = o.Raw(sql, pars).QueryRows(&items)
  365. return
  366. }
  367. func GetClarksonsIndex(condition string, pars interface{}) (items []*BaseFromClarksonsIndexView, err error) {
  368. o := orm.NewOrmUsingDB("data")
  369. sql := ` SELECT * FROM base_from_clarksons_index WHERE 1=1 `
  370. if condition != "" {
  371. sql += condition
  372. }
  373. sql += `ORDER BY base_from_clarksons_index_id ASC `
  374. _, err = o.Raw(sql, pars).QueryRows(&items)
  375. return
  376. }
  377. func GetClarksonsIndexLatestDate(indexCode string) (ModifyTime string, err error) {
  378. o := orm.NewOrmUsingDB("data")
  379. sql := ` SELECT modify_time FROM base_from_clarksons_data WHERE index_code=? ORDER BY modify_time DESC limit 1 `
  380. err = o.Raw(sql, indexCode).QueryRow(&ModifyTime)
  381. return
  382. }