base_from_sci_hq_index.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. package data_manage
  2. import (
  3. "eta/eta_api/global"
  4. "eta/eta_api/utils"
  5. "gorm.io/gorm"
  6. "time"
  7. )
  8. type BaseFromSciHqIndex struct {
  9. BaseFromSciHqIndexId int `orm:"pk" gorm:"primaryKey"`
  10. ClassifyId int `description:"指标分类id"`
  11. IndexCode string `description:"指标编码"`
  12. IndexName string `description:"指标名称"`
  13. Unit string `description:"单位"`
  14. Frequency string `description:"频度"`
  15. StartDate string `description:"开始日期"`
  16. EndDate string `description:"结束日期"`
  17. Sort int `description:"排序"`
  18. LatestValue float64 `description:"最新值"`
  19. LatestDate time.Time `description:"最后更新时间"`
  20. TerminalCode string
  21. FilePath string `json:"-"`
  22. CreateTime time.Time
  23. ModifyTime time.Time
  24. }
  25. type BaseFromSciHqIndexView struct {
  26. BaseFromSciHqIndexId int `orm:"pk" gorm:"primaryKey"`
  27. EdbInfoId int `description:"指标库id"`
  28. ClassifyId int `description:"指标分类id"`
  29. IndexCode string `description:"指标编码"`
  30. IndexName string `description:"指标名称"`
  31. UniqueCode string `description:"唯一code"`
  32. Frequency string `description:"频度"`
  33. Unit string `description:"单位"`
  34. StartDate string `description:"开始日期"`
  35. EndDate string `description:"结束日期"`
  36. Sort int `description:"排序"`
  37. LatestValue float64 `description:"最新值"`
  38. LatestDate string `description:"最后更新时间"`
  39. EdbExist int `description:"edb是否存在"`
  40. ModifyTime string
  41. }
  42. func (b *BaseFromSciHqIndexView) AfterFind(db *gorm.DB) (err error) {
  43. if utils.NeedDateOrTimeFormat(utils.DbDriverName) {
  44. if b.LatestDate != "" {
  45. b.LatestDate = utils.GormDateStrToDateStr(b.LatestDate)
  46. }
  47. if b.StartDate != "" {
  48. b.StartDate = utils.GormDateStrToDateStr(b.StartDate)
  49. }
  50. if b.EndDate != "" {
  51. b.EndDate = utils.GormDateStrToDateStr(b.EndDate)
  52. }
  53. if b.ModifyTime != "" {
  54. b.ModifyTime = utils.GormDateStrToDateTimeStr(b.ModifyTime)
  55. }
  56. }
  57. return
  58. }
  59. func (b *BaseFromSciHqIndex) Update(cols []string) (err error) {
  60. o := global.DbMap[utils.DbNameIndex]
  61. err = o.Model(b).Select(cols).Updates(b).Error
  62. return
  63. }
  64. // GetSciHqIndexByCondition 根据条件获取卓创红期指标列表
  65. func GetSciHqIndexByCondition(condition string, pars []interface{}) (items []*BaseFromSciHqIndex, err error) {
  66. o := global.DbMap[utils.DbNameIndex]
  67. sql := ` SELECT * FROM base_from_sci_hq_index WHERE 1=1 `
  68. if condition != "" {
  69. sql += condition
  70. }
  71. sql += ` ORDER BY sort ASC, base_from_sci_hq_index_id ASC`
  72. err = o.Raw(sql, pars...).Find(&items).Error
  73. return
  74. }
  75. // GetSciHqIndexByCondition 根据条件获取卓创红期指标列表
  76. func GetSciHqIndexByConditionAndFrequency(condition, frequency string, pars []interface{}) (items []*BaseFromSciHqIndex, err error) {
  77. o := global.DbMap[utils.DbNameIndex]
  78. sql := ` SELECT * FROM base_from_sci_hq_index WHERE 1=1 `
  79. if condition != "" {
  80. sql += condition
  81. }
  82. sql += ` AND frequency=?`
  83. sql += ` ORDER BY sort ASC, base_from_sci_hq_index_id ASC`
  84. err = o.Raw(sql, pars, frequency).Find(&items).Error
  85. return
  86. }
  87. func GetSciHqIndexCountByCondition(condition string, pars []interface{}) (count int, err error) {
  88. o := global.DbMap[utils.DbNameIndex]
  89. sql := ` SELECT COUNT(*) AS count FROM base_from_sci_hq_index WHERE 1=1 `
  90. if condition != "" {
  91. sql += condition
  92. }
  93. sql += ` ORDER BY sort ASC, base_from_sci_hq_index_id ASC`
  94. err = o.Raw(sql, pars...).Scan(&count).Error
  95. return
  96. }
  97. // GetSciHqIndexAndEdbInfoByCondition 根据条件获取卓创红期index和指标库的信息
  98. func GetSciHqIndexAndEdbInfoByCondition(condition string, pars []interface{}) (items []*BaseFromSciHqIndexView, err error) {
  99. wherePars := make([]interface{}, 0)
  100. wherePars = append(wherePars, utils.DATA_SOURCE_SCI_HQ)
  101. wherePars = append(wherePars, pars...)
  102. o := global.DbMap[utils.DbNameIndex]
  103. sql := ` SELECT b.*, e.edb_info_id FROM base_from_sci_hq_index AS b LEFT JOIN edb_info AS e ON b.index_code=e.edb_code AND e.source=? WHERE 1=1 `
  104. if condition != "" {
  105. sql += condition
  106. }
  107. sql += ` ORDER BY sort ASC `
  108. err = o.Raw(sql, wherePars...).Find(&items).Error
  109. return
  110. }
  111. // GetSciHqIndexByIndexCode 根据指标编码获取指标信息
  112. func GetSciHqIndexByIndexCode(indexCode string) (item *BaseFromSciHqIndex, err error) {
  113. o := global.DbMap[utils.DbNameIndex]
  114. sql := ` SELECT * FROM base_from_sci_hq_index WHERE index_code=? `
  115. err = o.Raw(sql, indexCode).First(&item).Error
  116. return
  117. }
  118. // GetSciHqIndexByIndexId 根据指标id获取指标信息
  119. func GetSciHqIndexByIndexId(indexId int) (item *BaseFromSciHqIndex, err error) {
  120. o := global.DbMap[utils.DbNameIndex]
  121. sql := ` SELECT * FROM base_from_sci_hq_index WHERE base_from_sci_hq_index_id=? `
  122. err = o.Raw(sql, indexId).First(&item).Error
  123. return
  124. }
  125. // GetSciHqIndexListByIndexIds 根据指标id获取指标信息
  126. func GetSciHqIndexListByIndexIds(indexIds []int) (items []*BaseFromSciHqIndex, err error) {
  127. if len(indexIds) == 0 {
  128. return
  129. }
  130. o := global.DbMap[utils.DbNameIndex]
  131. sql := ` SELECT * FROM base_from_sci_hq_index WHERE base_from_sci_hq_index_id IN (` + utils.GetOrmInReplace(len(indexIds)) + `) `
  132. err = o.Raw(sql, indexIds).Find(&items).Error
  133. return
  134. }
  135. // GetSciHqIndexCountByClassifyIds 获取分类下指标的个数
  136. func GetSciHqIndexCountByClassifyIds(classifyIds []int) (count int, err error) {
  137. o := global.DbMap[utils.DbNameIndex]
  138. num := len(classifyIds)
  139. if num <= 0 {
  140. return
  141. }
  142. sql := `SELECT COUNT(1) AS count FROM base_from_sci_hq_index WHERE classify_id IN (` + utils.GetOrmInReplace(num) + `) `
  143. err = o.Raw(sql, classifyIds).Scan(&count).Error
  144. return
  145. }
  146. // GetSciHqIndexByClassifyId 根据分类id获取卓创红期指标列表
  147. func GetSciHqIndexByClassifyId(classifyIds []int, startSize, pageSize int) (items []*BaseFromSciHqIndexView, err error) {
  148. o := global.DbMap[utils.DbNameIndex]
  149. sql := ` SELECT b.*, e.edb_info_id,
  150. CASE WHEN e.edb_info_id IS NULL THEN 0 ELSE 1 END AS edb_exist
  151. FROM base_from_sci_hq_index AS b
  152. LEFT JOIN edb_info AS e ON b.index_code=e.edb_code AND e.source=88
  153. WHERE b.classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `) ORDER BY b.sort ASC LIMIT ?,? `
  154. err = o.Raw(sql, classifyIds, startSize, pageSize).Find(&items).Error
  155. return
  156. }
  157. // GetSciHqIndexCountByClassifyId 根据分类id获取卓创红期指标数量
  158. func GetSciHqIndexCountByClassifyId(classifyIds []int) (count int, err error) {
  159. if len(classifyIds) == 0 {
  160. return
  161. }
  162. o := global.DbMap[utils.DbNameIndex]
  163. sql := ` SELECT COUNT(*) AS count FROM base_from_sci_hq_index WHERE classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `) `
  164. err = o.Raw(sql, classifyIds).Scan(&count).Error
  165. return
  166. }
  167. // GetSciHqIndexCount 获取卓创红期指标数量
  168. func GetSciHqIndexCount() (count int, err error) {
  169. o := global.DbMap[utils.DbNameIndex]
  170. sql := ` SELECT COUNT(*) AS count FROM base_from_sci_hq_index `
  171. err = o.Raw(sql).Scan(&count).Error
  172. return
  173. }
  174. func GetSciHqIndexByPage(startSize, pageSize int) (items []*BaseFromSciHqIndexView, err error) {
  175. o := global.DbMap[utils.DbNameIndex]
  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_sci_hq_index AS b
  179. LEFT JOIN edb_info AS e ON b.index_code=e.edb_code AND e.source=88
  180. ORDER BY b.modify_time DESC LIMIT ?,?`
  181. err = o.Raw(sql, startSize, pageSize).Find(&items).Error
  182. return
  183. }
  184. // GetSciHqIndexBaseInfoByClassifyId 根据分类id获取卓创红期指标列表
  185. func GetSciHqIndexBaseInfoByClassifyId(classifyId int) (items []*BaseFromSciHqIndexView, err error) {
  186. o := global.DbMap[utils.DbNameIndex]
  187. sql := ` SELECT base_from_sci_hq_index_id, classify_id, index_code, index_name, CONCAT(classify_id, '_', base_from_sci_hq_index_id) AS unique_code FROM base_from_sci_hq_index WHERE classify_id = ? ORDER BY sort ASC `
  188. err = o.Raw(sql, classifyId).Find(&items).Error
  189. return
  190. }
  191. // GetSciHqIndexBaseInfoByClassifyId 根据分类id获取卓创红期指标列表
  192. func GetSciHqIndexBaseInfoByCondition(condition string, pars []interface{}) (items []*BaseFromSciHqIndex, err error) {
  193. o := global.DbMap[utils.DbNameIndex]
  194. sql := ` SELECT base_from_sci_hq_index_id, index_code, index_name FROM base_from_sci_hq_index WHERE 1=1 `
  195. if condition != "" {
  196. sql += condition
  197. }
  198. sql += ` ORDER BY sort ASC `
  199. err = o.Raw(sql, pars...).Find(&items).Error
  200. return
  201. }
  202. func GetSciHqDataMaxCount(condition string, pars []interface{}) (count int, err error) {
  203. o := global.DbMap[utils.DbNameIndex]
  204. sql := `SELECT COALESCE(MAX(t.num), 0) AS count FROM ( SELECT COUNT(1) AS num FROM base_from_sci_hq_index AS a INNER JOIN base_from_sci_hq_data AS b ON a.index_code=b.index_code WHERE 1=1 `
  205. if condition != "" {
  206. sql += condition
  207. }
  208. sql += ` GROUP BY a.base_from_sci_hq_index_id) AS t `
  209. err = o.Raw(sql, pars...).Scan(&count).Error
  210. return
  211. }
  212. // GetSciHqIndexMaxSortByClassifyId 根据分类id获取指标最大排序
  213. func GetSciHqIndexMaxSortByClassifyId(classifyId int) (sort int, err error) {
  214. o := global.DbMap[utils.DbNameIndex]
  215. sql := `SELECT COALESCE(MAX(sort), 0) AS sort FROM base_from_sci_index WHERE classify_id=? `
  216. err = o.Raw(sql, classifyId).Scan(&sort).Error
  217. return
  218. }
  219. func GetSciHqFrequency(classifyId int) (items []*string, err error) {
  220. sql := `SELECT DISTINCT frequency FROM base_from_sci_hq_index WHERE classify_id=? ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年','年度') `
  221. o := global.DbMap[utils.DbNameIndex]
  222. err = o.Raw(sql, classifyId).Find(&items).Error
  223. return
  224. }
  225. func GetSciHqFrequencyByCondition(condition string, pars []interface{}) (items []*string, err error) {
  226. sql := `SELECT DISTINCT frequency FROM base_from_sci_hq_index WHERE 1=1 `
  227. if condition != "" {
  228. sql += condition
  229. }
  230. sql += ` ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年','年度') `
  231. o := global.DbMap[utils.DbNameIndex]
  232. err = o.Raw(sql, pars...).Find(&items).Error
  233. return
  234. }
  235. func GetSciHqFrequencyByCode(code string) (items []*string, err error) {
  236. sql := `SELECT DISTINCT frequency FROM base_from_sci_hq_index WHERE index_code=? ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年','年度') `
  237. o := global.DbMap[utils.DbNameIndex]
  238. err = o.Raw(sql, code).Find(&items).Error
  239. return
  240. }
  241. // GetSciHqClassifyMaxSortByClassifyIds 通过分类id获取对应分类的最大sort
  242. func GetSciHqClassifyMaxSortByClassifyIds(classifyIds []int) (items []*BaseFromSciHqClassifyMaxSort, err error) {
  243. if len(classifyIds) == 0 {
  244. return
  245. }
  246. o := global.DbMap[utils.DbNameIndex]
  247. sql := `SELECT bc.classify_id, COALESCE(MAX(bi.sort), 0) AS max_sort FROM base_from_sci_hq_classify AS bc
  248. LEFT JOIN base_from_sci_hq_index AS bi
  249. ON bc.classify_id=bi.classify_id
  250. WHERE bc.classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `)
  251. GROUP BY bc.classify_id
  252. `
  253. // sql = ` SELECT classify_id, MAX(sort) AS max_sort FROM base_from_sci_hq_index WHERE classify_id IN (` + utils.GetOrmInReplace(len(classifyIds)) + `) GROUP BY classify_id `
  254. err = o.Raw(sql, classifyIds).Find(&items).Error
  255. return
  256. }
  257. func BatchModifySciHqIndexClassify(items []*BaseFromSciHqIndex) (err error) {
  258. o := global.DbMap[utils.DbNameIndex]
  259. sql := `UPDATE base_from_sci_hq_index SET classify_id=?, sort=? WHERE base_from_sci_hq_index_id=? `
  260. for _, v := range items {
  261. err = o.Exec(sql, v.ClassifyId, v.Sort, v.BaseFromSciHqIndexId).Error
  262. if err != nil {
  263. return
  264. }
  265. }
  266. return
  267. }
  268. // MoveDownSciHqIndexBySort 往下移动
  269. func MoveDownSciHqIndexBySort(classifyId, prevSort, currentSort int) (err error) {
  270. o := global.DbMap[utils.DbNameIndex]
  271. sql := `update base_from_sci_hq_index set sort = sort - 1 where classify_id=? and sort <= ? and sort> ? `
  272. err = o.Exec(sql, classifyId, prevSort, currentSort).Error
  273. return
  274. }
  275. // MoveUpSciHqIndexBySort 往上移动
  276. func MoveUpSciHqIndexBySort(classifyId, nextSort, currentSort int) (err error) {
  277. o := global.DbMap[utils.DbNameIndex]
  278. sql := `update base_from_sci_hq_index set sort = sort + 1 where classify_id=? and sort >= ? and sort< ?`
  279. err = o.Exec(sql, classifyId, nextSort, currentSort).Error
  280. return
  281. }
  282. func DeleteSciHqIndexById(indexId int) (err error) {
  283. o := global.DbMap[utils.DbNameIndex]
  284. tx := o.Begin()
  285. if tx.Error != nil {
  286. return tx.Error
  287. }
  288. defer func() {
  289. if err != nil {
  290. _ = tx.Rollback()
  291. } else {
  292. _ = tx.Commit()
  293. }
  294. }()
  295. sql := `DELETE FROM base_from_sci_hq_index WHERE base_from_sci_hq_index_id=? `
  296. err = tx.Exec(sql, indexId).Error
  297. if err != nil {
  298. return
  299. }
  300. sql = `DELETE FROM base_from_sci_hq_data WHERE base_from_sci_hq_index_id=? `
  301. err = tx.Exec(sql, indexId).Error
  302. if err != nil {
  303. return
  304. }
  305. return
  306. }
  307. func DeleteSciHqIndexByIds(indexIds []int) (err error) {
  308. if len(indexIds) == 0 {
  309. return
  310. }
  311. o := global.DbMap[utils.DbNameIndex]
  312. tx := o.Begin()
  313. if tx.Error != nil {
  314. return tx.Error
  315. }
  316. defer func() {
  317. if err != nil {
  318. _ = tx.Rollback()
  319. } else {
  320. _ = tx.Commit()
  321. }
  322. }()
  323. sql := `DELETE FROM base_from_sci_hq_index WHERE base_from_sci_hq_index_id IN (` + utils.GetOrmInReplace(len(indexIds)) + `) `
  324. err = tx.Exec(sql, indexIds).Error
  325. if err != nil {
  326. return
  327. }
  328. sql = `DELETE FROM base_from_sci_hq_data WHERE base_from_sci_hq_index_id IN (` + utils.GetOrmInReplace(len(indexIds)) + `) `
  329. err = tx.Exec(sql, indexIds).Error
  330. if err != nil {
  331. return
  332. }
  333. return
  334. }
  335. // MoveSciHqIndex 移动指标分类
  336. func MoveSciHqIndex(indexId, classifyId int) (err error) {
  337. o := global.DbMap[utils.DbNameIndex]
  338. sql := ` UPDATE base_from_sci_hq_index
  339. SET
  340. classify_id = ?, modify_time=NOW()
  341. WHERE base_from_sci_hq_index_id = ?`
  342. err = o.Exec(sql, classifyId, indexId).Error
  343. return
  344. }
  345. // GetSciHqIndexMinSortByClassifyId 获取最小不等于0的排序
  346. func GetSciHqIndexMinSortByClassifyId(classifyId int) (sort int, err error) {
  347. o := global.DbMap[utils.DbNameIndex]
  348. sql := `SELECT min(sort) FROM base_from_sci_hq_index WHERE classify_id=? and sort <> 0 `
  349. err = o.Raw(sql, classifyId).Scan(&sort).Error
  350. return
  351. }