base_from_sci_hq_index.go 14 KB

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