base_from_clarksons_index.go 17 KB

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