base_from_clarksons_index.go 17 KB

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