base_from_clarksons_index.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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
  49. Paging *paging.PagingItem `description:"分页数据"`
  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. err = global.DbMap[utils.DbNameIndex].Raw(sql, utils.DATA_SOURCE_SCI_HQ, pars).Find(&items).Error
  99. return
  100. }
  101. // GetClarksonsIndexByIndexCode 根据指标编码获取指标信息
  102. func GetClarksonsIndexByIndexCode(indexCode string) (item *BaseFromClarksonsIndex, err error) {
  103. sql := ` SELECT * FROM base_from_clarksons_index WHERE index_code=? `
  104. err = global.DbMap[utils.DbNameIndex].Raw(sql, indexCode).First(&item).Error
  105. return
  106. }
  107. // GetClarksonsIndexByIndexId 根据指标id获取指标信息
  108. func GetClarksonsIndexByIndexId(indexId int) (item *BaseFromClarksonsIndex, err error) {
  109. sql := ` SELECT * FROM base_from_clarksons_index WHERE base_from_clarksons_index_id=? `
  110. err = global.DbMap[utils.DbNameIndex].Raw(sql, indexId).First(&item).Error
  111. return
  112. }
  113. // GetClarksonsIndexListByIndexIds 根据指标id获取指标信息
  114. func GetClarksonsIndexListByIndexIds(indexIds []int) (items []*BaseFromClarksonsIndex, err error) {
  115. if len(indexIds) == 0 {
  116. return
  117. }
  118. sql := ` SELECT * FROM base_from_clarksons_index WHERE base_from_clarksons_index_id IN (` + utils.GetOrmInReplace(len(indexIds)) + `) `
  119. err = global.DbMap[utils.DbNameIndex].Raw(sql, indexIds).Find(&items).Error
  120. return
  121. }
  122. // GetClarksonsIndexCountByClassifyIds 获取分类下指标的个数
  123. func GetClarksonsIndexCountByClassifyIds(classifyIds []int) (count int, err error) {
  124. num := len(classifyIds)
  125. if num <= 0 {
  126. return
  127. }
  128. sqlStr := `SELECT COUNT(1) AS count FROM base_from_clarksons_index WHERE classify_id IN (` + utils.GetOrmInReplace(num) + `) `
  129. var totalNull sql.NullInt64
  130. err = global.DbMap[utils.DbNameIndex].Raw(sqlStr, classifyIds).Scan(&totalNull).Error
  131. if !totalNull.Valid {
  132. count = 0
  133. } else {
  134. count = int(totalNull.Int64)
  135. }
  136. return
  137. }
  138. // GetClarksonsIndexByClassifyId 根据分类id获取克拉克森指标列表
  139. func GetClarksonsIndexByClassifyId(classifyIds []int, startSize, pageSize int) (items []*BaseFromClarksonsIndexView, err error) {
  140. sql := ` SELECT b.*, e.edb_info_id,
  141. CASE WHEN e.edb_info_id IS NULL THEN 0 ELSE 1 END AS edb_exist
  142. FROM base_from_clarksons_index AS b
  143. LEFT JOIN edb_info AS e ON b.index_code=e.edb_code AND e.source=101
  144. WHERE b.classify_id IN ? ORDER BY b.sort ASC LIMIT ?,? `
  145. //sql := ` SELECT b.*, e.edb_info_id,
  146. //CASE WHEN e.edb_info_id IS NULL THEN 0 ELSE 1 END AS edb_exist
  147. //FROM base_from_clarksons_index AS b
  148. //LEFT JOIN edb_info AS e ON b.index_code=e.edb_code AND e.source=101
  149. //WHERE b.classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `) ORDER BY b.sort ASC LIMIT ?,? `
  150. err = global.DbMap[utils.DbNameIndex].Raw(sql, classifyIds, startSize, pageSize).Find(&items).Error
  151. return
  152. }
  153. // GetClarksonsIndexCountByClassifyId 根据分类id获取克拉克森指标数量
  154. func GetClarksonsIndexCountByClassifyId(classifyIds []int) (count int, err error) {
  155. if len(classifyIds) == 0 {
  156. return
  157. }
  158. sqlStr := ` SELECT COUNT(*) AS count FROM base_from_clarksons_index WHERE classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `) `
  159. var totalNull sql.NullInt64
  160. err = global.DbMap[utils.DbNameIndex].Raw(sqlStr, classifyIds).Scan(&totalNull).Error
  161. if !totalNull.Valid {
  162. count = 0
  163. } else {
  164. count = int(totalNull.Int64)
  165. }
  166. return
  167. }
  168. // GetClarksonsIndexCount 获取克拉克森指标数量
  169. func GetClarksonsIndexCount() (count int, err error) {
  170. sqlStr := ` SELECT COUNT(*) AS count FROM base_from_clarksons_index `
  171. var totalNull sql.NullInt64
  172. err = global.DbMap[utils.DbNameIndex].Raw(sqlStr).Scan(&totalNull).Error
  173. if !totalNull.Valid {
  174. count = 0
  175. } else {
  176. count = int(totalNull.Int64)
  177. }
  178. return
  179. }
  180. func GetClarksonsIndexByPage(startSize, pageSize int) (items []*BaseFromClarksonsIndexView, err error) {
  181. sql := ` SELECT b.*, e.edb_info_id,
  182. CASE WHEN e.edb_info_id IS NULL THEN 0 ELSE 1 END AS edb_exist
  183. FROM base_from_clarksons_index AS b
  184. LEFT JOIN edb_info AS e ON b.index_code=e.edb_code AND e.source=101
  185. ORDER BY b.modify_time DESC LIMIT ?,?`
  186. err = global.DbMap[utils.DbNameIndex].Raw(sql, startSize, pageSize).Find(&items).Error
  187. return
  188. }
  189. // GetClarksonsIndexBaseInfoByClassifyId 根据分类id获取克拉克森指标列表
  190. func GetClarksonsIndexBaseInfoByClassifyId(classifyId int) (items []*BaseFromClarksonsIndexView, err error) {
  191. 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 `
  192. err = global.DbMap[utils.DbNameIndex].Raw(sql, classifyId).Find(&items).Error
  193. return
  194. }
  195. // GetClarksonsIndexBaseInfoByClassifyId 根据分类id获取克拉克森指标列表
  196. func GetClarksonsIndexBaseInfoByCondition(condition string, pars []interface{}) (items []*BaseFromClarksonsIndex, err error) {
  197. sql := ` SELECT base_from_clarksons_index_id, index_code, index_name FROM base_from_clarksons_index WHERE 1=1 `
  198. if condition != "" {
  199. sql += condition
  200. }
  201. sql += ` ORDER BY sort ASC `
  202. err = global.DbMap[utils.DbNameIndex].Raw(sql, pars...).Find(&items).Error
  203. return
  204. }
  205. func GetClarksonsDataMaxCount(condition string, pars []interface{}) (count int, err error) {
  206. 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 `
  207. if condition != "" {
  208. sqlStr += condition
  209. }
  210. sqlStr += ` GROUP BY a.base_from_clarksons_index_id) AS t `
  211. var totalNull sql.NullInt64
  212. err = global.DbMap[utils.DbNameIndex].Raw(sqlStr, pars).Scan(&totalNull).Error
  213. if !totalNull.Valid {
  214. count = 0
  215. } else {
  216. count = int(totalNull.Int64)
  217. }
  218. return
  219. }
  220. // GetClarksonsIndexMaxSortByClassifyId 根据分类id获取指标最大排序
  221. func GetClarksonsIndexMaxSortByClassifyId(classifyId int) (sort int, err error) {
  222. sqlStr := `SELECT MAX(sort) FROM base_from_clarksons_index WHERE classify_id=? `
  223. var totalNull sql.NullInt64
  224. err = global.DbMap[utils.DbNameIndex].Raw(sqlStr, classifyId).Scan(&totalNull).Error
  225. if !totalNull.Valid {
  226. sort = 0
  227. } else {
  228. sort = int(totalNull.Int64)
  229. }
  230. return
  231. }
  232. func GetClarksonsFrequency(classifyId int) (items []*string, err error) {
  233. sql := `SELECT DISTINCT frequency FROM base_from_clarksons_index WHERE classify_id=? ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年','年度') `
  234. err = global.DbMap[utils.DbNameIndex].Raw(sql, classifyId).Find(&items).Error
  235. return
  236. }
  237. func GetClarksonsFrequencyByCondition(condition string, pars []interface{}) (items []*string, err error) {
  238. sql := `SELECT DISTINCT frequency FROM base_from_clarksons_index WHERE 1=1 `
  239. if condition != "" {
  240. sql += condition
  241. }
  242. sql += ` ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年','年度') `
  243. err = global.DbMap[utils.DbNameIndex].Raw(sql, pars...).Find(&items).Error
  244. return
  245. }
  246. func GetClarksonsFrequencyByCode(code string) (items []*string, err error) {
  247. sql := `SELECT DISTINCT frequency FROM base_from_clarksons_index WHERE index_code=? ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年','年度') `
  248. err = global.DbMap[utils.DbNameIndex].Raw(sql, code).Find(&items).Error
  249. return
  250. }
  251. // GetClarksonsClassifyMaxSortByClassifyIds 通过分类id获取对应分类的最大sort
  252. func GetClarksonsClassifyMaxSortByClassifyIds(classifyIds []int) (items []*BaseFromClarksonsClassifyMaxSort, err error) {
  253. if len(classifyIds) == 0 {
  254. return
  255. }
  256. sql := `SELECT bc.base_from_clarksons_classify_id, COALESCE(MAX(bi.sort), 0) AS max_sort FROM base_from_clarksons_classify AS bc
  257. LEFT JOIN base_from_clarksons_index AS bi
  258. ON bc.base_from_clarksons_classify_id=bi.classify_id
  259. WHERE bc.base_from_clarksons_classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `)
  260. GROUP BY bc.base_from_clarksons_classify_id
  261. `
  262. // 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 `
  263. err = global.DbMap[utils.DbNameIndex].Raw(sql, classifyIds).Find(&items).Error
  264. return
  265. }
  266. func BatchModifyClarksonsIndexClassify(items []*BaseFromClarksonsIndex) (err error) {
  267. sql := `UPDATE base_from_clarksons_index SET classify_id=?, sort=? WHERE base_from_clarksons_index_id=? `
  268. for _, v := range items {
  269. err = global.DbMap[utils.DbNameIndex].Exec(sql, v.ClassifyId, v.Sort, v.BaseFromClarksonsIndexId).Error
  270. if err != nil {
  271. return
  272. }
  273. }
  274. return
  275. }
  276. // MoveDownClarksonsIndexBySort 往下移动
  277. func MoveDownClarksonsIndexBySort(classifyId, prevSort, currentSort int) (err error) {
  278. sql := `update base_from_clarksons_index set sort = sort - 1 where classify_id=? and sort <= ? and sort> ? `
  279. err = global.DbMap[utils.DbNameIndex].Exec(sql, classifyId, prevSort, currentSort).Error
  280. return
  281. }
  282. // MoveUpClarksonsIndexBySort 往上移动
  283. func MoveUpClarksonsIndexBySort(classifyId, nextSort, currentSort int) (err error) {
  284. sql := `update base_from_clarksons_index set sort = sort + 1 where classify_id=? and sort >= ? and sort< ?`
  285. err = global.DbMap[utils.DbNameIndex].Exec(sql, classifyId, nextSort, currentSort).Error
  286. return
  287. }
  288. func DeleteClarksonsIndexById(indexId int) (err error) {
  289. tx := global.DbMap[utils.DbNameIndex].Begin()
  290. defer func() {
  291. if err != nil {
  292. _ = tx.Rollback()
  293. } else {
  294. _ = tx.Commit()
  295. }
  296. }()
  297. sql := `DELETE FROM base_from_clarksons_index WHERE base_from_clarksons_index_id=? `
  298. err = tx.Exec(sql, indexId).Error
  299. if err != nil {
  300. return
  301. }
  302. sql = `DELETE FROM base_from_clarksons_data WHERE base_from_clarksons_index_id=? `
  303. err = tx.Exec(sql, indexId).Error
  304. if err != nil {
  305. return
  306. }
  307. return
  308. }
  309. func DeleteClarksonsIndexByIds(indexIds []int) (err error) {
  310. if len(indexIds) == 0 {
  311. return
  312. }
  313. tx := global.DbMap[utils.DbNameIndex].Begin()
  314. defer func() {
  315. if err != nil {
  316. _ = tx.Rollback()
  317. } else {
  318. _ = tx.Commit()
  319. }
  320. }()
  321. sql := `DELETE FROM base_from_clarksons_index WHERE base_from_clarksons_index_id IN (` + utils.GetOrmInReplace(len(indexIds)) + `) `
  322. err = tx.Exec(sql, indexIds).Error
  323. if err != nil {
  324. return
  325. }
  326. sql = `DELETE FROM base_from_clarksons_data WHERE base_from_clarksons_index_id IN (` + utils.GetOrmInReplace(len(indexIds)) + `) `
  327. err = tx.Exec(sql, indexIds).Error
  328. if err != nil {
  329. return
  330. }
  331. return
  332. }
  333. // MoveClarksonsIndex 移动指标分类
  334. func MoveClarksonsIndex(indexId, classifyId int) (err error) {
  335. sql := ` UPDATE base_from_clarksons_index
  336. SET
  337. classify_id = ?, modify_time=NOW()
  338. WHERE base_from_clarksons_index_id = ?`
  339. err = global.DbMap[utils.DbNameIndex].Raw(sql, classifyId, indexId).Error
  340. return
  341. }
  342. // GetClarksonsIndexMinSortByClassifyId 获取最小不等于0的排序
  343. func GetClarksonsIndexMinSortByClassifyId(classifyId int) (sort int, err error) {
  344. sqlStr := `SELECT min(sort) FROM base_from_clarksons_index WHERE classify_id=? and sort <> 0 `
  345. var totalNull sql.NullInt64
  346. err = global.DbMap[utils.DbNameIndex].Raw(sqlStr, classifyId).Scan(&totalNull).Error
  347. if !totalNull.Valid {
  348. sort = 0
  349. } else {
  350. sort = int(totalNull.Int64)
  351. }
  352. return
  353. }
  354. // GetClarksonsIndexInfoCount 分页查询指标信息行数
  355. func GetClarksonsIndexInfoCount(condition string, pars []interface{}) (count int, err error) {
  356. sqlStr := ` SELECT count(1) FROM base_from_clarksons_index WHERE index_code not in (select edb_code from edb_info) `
  357. if condition != "" {
  358. sqlStr += condition
  359. }
  360. var totalNull sql.NullInt64
  361. err = global.DbMap[utils.DbNameIndex].Raw(sqlStr, pars).Scan(&totalNull).Error
  362. if !totalNull.Valid {
  363. count = 0
  364. } else {
  365. count = int(totalNull.Int64)
  366. }
  367. return
  368. }
  369. // GetClarksonsIndexInfoPage 分页查询指标信息
  370. func GetClarksonsIndexInfoPage(condition string, pars []interface{}) (items []*BaseFromRzdIndexAndData, err error) {
  371. sql := ` SELECT * FROM base_from_clarksons_index WHERE index_code not in (select edb_code from edb_info) `
  372. if condition != "" {
  373. sql += condition
  374. }
  375. err = global.DbMap[utils.DbNameIndex].Raw(sql, pars).Find(&items).Error
  376. return
  377. }
  378. func GetClarksonsIndex(condition string, pars interface{}) (items []*BaseFromClarksonsIndexView, err error) {
  379. sql := ` SELECT * FROM base_from_clarksons_index WHERE 1=1 `
  380. if condition != "" {
  381. sql += condition
  382. }
  383. sql += `ORDER BY base_from_clarksons_index_id ASC `
  384. err = global.DbMap[utils.DbNameIndex].Raw(sql, pars).Find(&items).Error
  385. return
  386. }
  387. func GetClarksonsIndexLatestDate(indexCode string) (ModifyTime string, err error) {
  388. sql := ` SELECT modify_time FROM base_from_clarksons_data WHERE index_code=? ORDER BY modify_time DESC limit 1 `
  389. err = global.DbMap[utils.DbNameIndex].Raw(sql, indexCode).Scan(&ModifyTime).Error
  390. return
  391. }