base_from_clarksons_index.go 17 KB

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