base_from_clarksons_index.go 17 KB

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