base_from_clarksons_index.go 16 KB

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