edb_info.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. package data_manage
  2. import (
  3. "errors"
  4. "eta/eta_task/global"
  5. "eta/eta_task/utils"
  6. "fmt"
  7. "gorm.io/gorm"
  8. "strconv"
  9. "time"
  10. )
  11. type EdbInfo struct {
  12. EdbInfoId int `gorm:"primaryKey"`
  13. SourceName string `description:"来源名称"`
  14. Source int `description:"来源id"`
  15. TerminalCode string `description:"终端编码"`
  16. SubSource int `description:"子数据来源:0:经济数据库,1:日期序列"`
  17. EdbCode string `description:"指标编码"`
  18. EdbName string `description:"指标名称"`
  19. EdbNameSource string `description:"指标名称来源"`
  20. Frequency string `description:"频率"`
  21. Unit string `description:"单位"`
  22. StartDate string `description:"起始日期"`
  23. EndDate string `description:"终止日期"`
  24. ClassifyId int `description:"分类id"`
  25. SysUserId int
  26. SysUserRealName string
  27. UniqueCode string `description:"指标唯一编码"`
  28. CreateTime time.Time
  29. ModifyTime time.Time
  30. MinValue float64 `description:"指标最小值"`
  31. MaxValue float64 `description:"指标最大值"`
  32. CalculateFormula string `description:"计算公式"`
  33. NoUpdate int8 `description:"是否停止更新,0:继续更新;1:停止更新"`
  34. EdbInfoType int `description:"指标类型,0:普通指标,1:预测指标"`
  35. EdbType int `description:"指标类型:1:基础指标,2:计算指标"`
  36. }
  37. func (m *EdbInfo) AfterFind(db *gorm.DB) (err error) {
  38. m.StartDate = utils.GormDateStrToDateStr(m.StartDate)
  39. m.EndDate = utils.GormDateStrToDateStr(m.EndDate)
  40. return
  41. }
  42. func (m *EdbInfo) ConvertTimeStr() {
  43. m.StartDate = utils.GormDateStrToDateStr(m.StartDate)
  44. m.EndDate = utils.GormDateStrToDateStr(m.EndDate)
  45. return
  46. }
  47. type EdbInfoList struct {
  48. EdbInfoId int `gorm:"primaryKey"`
  49. SourceName string `description:"来源名称"`
  50. Source int `description:"来源id"`
  51. SubSource int `description:"子数据来源:0:经济数据库,1:日期序列"`
  52. SubSourceName string `description:"子数据来源名称"`
  53. EdbCode string `description:"指标编码"`
  54. EdbName string `description:"指标名称"`
  55. Frequency string `description:"频率"`
  56. Unit string `description:"单位"`
  57. StartDate time.Time `description:"起始日期"`
  58. EndDate time.Time `description:"终止日期"`
  59. ClassifyId int `description:"分类id"`
  60. UniqueCode string `description:"指标唯一编码"`
  61. CalculateFormula string `description:"计算公式"`
  62. ModifyTime string `description:"更新时间"`
  63. NoUpdate int8 `description:"是否停止更新,0:继续更新;1:停止更新"`
  64. }
  65. func (m *EdbInfoList) AfterFind(db *gorm.DB) (err error) {
  66. m.ModifyTime = utils.GormDateStrToDateStr(m.ModifyTime)
  67. return
  68. }
  69. type EdbInfoSearchData struct {
  70. EdbDataId int `description:"指标数据Id"`
  71. DataTime string `description:"数据日期"`
  72. Value float64 `description:"数据"`
  73. }
  74. func (m *EdbInfoSearchData) AfterFind(db *gorm.DB) (err error) {
  75. m.DataTime = utils.GormDateStrToDateStr(m.DataTime)
  76. return
  77. }
  78. type EdbInfoSearchDataV1 struct {
  79. DataTime string `description:"数据日期"`
  80. Value string `description:"数据"`
  81. }
  82. func (m *EdbInfoSearchDataV1) AfterFind(db *gorm.DB) (err error) {
  83. m.DataTime = utils.GormDateStrToDateStr(m.DataTime)
  84. return
  85. }
  86. func GetEdbInfoByCondition(condition string, pars []interface{}, order int) (item []*EdbInfoList, err error) {
  87. o := global.DbMap[utils.DbNameIndex]
  88. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  89. if condition != "" {
  90. sql += condition
  91. }
  92. if order == 1 {
  93. sql += ` ORDER BY end_date ASC `
  94. } else {
  95. sql += ` ORDER BY edb_info_id ASC `
  96. }
  97. err = o.Raw(sql, pars...).Find(&item).Error
  98. return
  99. }
  100. //func ModifyEdbDataInfoDate(edbInfoId int, maxDate string) (err error) {
  101. // o := global.DbMap[utils.DbNameIndex]
  102. // sql := ` UPDATE edb_info SET end_date=?,modify_time=NOW() WHERE edb_info_id=? `
  103. // err = o.Exec(sql, maxDate, edbInfoId).Error
  104. // return
  105. //}
  106. type EdbInfoMaxAndMinInfo struct {
  107. MinDate string `description:"最小日期"`
  108. MaxDate string `description:"最大日期"`
  109. MinValue float64 `description:"最小值"`
  110. MaxValue float64 `description:"最大值"`
  111. LatestValue float64 `description:"最新值"`
  112. }
  113. func (m *EdbInfoMaxAndMinInfo) ConvertTimeStr() {
  114. m.MinDate = utils.GormDateStrToDateStr(m.MinDate)
  115. m.MaxDate = utils.GormDateStrToDateStr(m.MaxDate)
  116. return
  117. }
  118. func GetEdbInfoMaxAndMinInfo(source, subSource int, edbCode string) (item *EdbInfoMaxAndMinInfo, err error) {
  119. o := global.DbMap[utils.DbNameIndex]
  120. sql := ``
  121. tableName := GetEdbDataTableName(source, subSource)
  122. if tableName == "" {
  123. err = errors.New("无效的表名称:source:" + strconv.Itoa(source))
  124. return nil, err
  125. }
  126. sql = ` SELECT MIN(data_time) AS min_date,MAX(data_time) AS max_date,MIN(value) AS min_value,MAX(value) AS max_value FROM %s WHERE edb_code=? `
  127. sql = fmt.Sprintf(sql, tableName)
  128. err = o.Raw(sql, edbCode).First(&item).Error
  129. if err != nil {
  130. return
  131. }
  132. if item != nil {
  133. item.ConvertTimeStr()
  134. }
  135. var latest_value float64
  136. sql = ` SELECT value AS latest_value FROM %s WHERE edb_code=? ORDER BY data_time DESC LIMIT 1 `
  137. sql = fmt.Sprintf(sql, tableName)
  138. sql = utils.ReplaceDriverKeywords("", sql)
  139. err = o.Raw(sql, edbCode).Scan(&latest_value).Error
  140. item.LatestValue = latest_value
  141. return
  142. }
  143. func ModifyEdbInfoMaxAndMinInfo(edbInfoId int, item *EdbInfoMaxAndMinInfo) (err error) {
  144. o := global.DbMap[utils.DbNameIndex]
  145. sql := ` UPDATE edb_info SET start_date=?,end_date=?,min_value=?,max_value=?,is_update=2,latest_date=?,latest_value=?,modify_time=NOW() WHERE edb_info_id=? `
  146. err = o.Exec(sql, item.MinDate, item.MaxDate, item.MinValue, item.MaxValue, item.MaxDate, item.LatestValue, edbInfoId).Error
  147. return
  148. }
  149. //order:1升序,其余值为降序
  150. func GetEdbDataListAll(condition string, pars []interface{}, source, subSource, order int) (item []*EdbInfoSearchData, err error) {
  151. o := global.DbMap[utils.DbNameIndex]
  152. sql := ``
  153. tableName := GetEdbDataTableName(source, subSource)
  154. sql = ` SELECT * FROM %s WHERE 1=1 `
  155. sql = fmt.Sprintf(sql, tableName)
  156. if condition != "" {
  157. sql += condition
  158. }
  159. if order == 1 {
  160. sql += ` ORDER BY data_time ASC `
  161. } else {
  162. sql += ` ORDER BY data_time DESC `
  163. }
  164. err = o.Raw(sql, pars...).Find(&item).Error
  165. return
  166. }
  167. func GetEdbDataListAllV1(condition string, pars []interface{}, source, subSource, order int) (item []*EdbInfoSearchDataV1, err error) {
  168. o := global.DbMap[utils.DbNameIndex]
  169. sql := ``
  170. tableName := GetEdbDataTableName(source, subSource)
  171. sql = ` SELECT * FROM %s WHERE 1=1 `
  172. sql = fmt.Sprintf(sql, tableName)
  173. if condition != "" {
  174. sql += condition
  175. }
  176. if order == 1 {
  177. sql += ` ORDER BY data_time ASC `
  178. } else {
  179. sql += ` ORDER BY data_time DESC `
  180. }
  181. err = o.Raw(sql, pars...).Find(&item).Error
  182. return
  183. }
  184. func GetEdbInfoById(edbInfoId int) (item *EdbInfo, err error) {
  185. o := global.DbMap[utils.DbNameIndex]
  186. sql := ` SELECT * FROM edb_info WHERE edb_info_id=? `
  187. err = o.Raw(sql, edbInfoId).First(&item).Error
  188. return
  189. }
  190. func GetQuarterEdbInfo() (item []*EdbInfo, err error) {
  191. o := global.DbMap[utils.DbNameIndex]
  192. sql := ` SELECT c.* FROM chart_info AS a
  193. INNER JOIN chart_edb_mapping AS b ON a.chart_info_id=b.chart_info_id
  194. INNER JOIN edb_info AS c ON b.edb_info_id=c.edb_info_id
  195. WHERE a.chart_type=2
  196. GROUP BY b.edb_info_id
  197. ORDER BY b.edb_info_id ASC `
  198. err = o.Raw(sql).Find(&item).Error
  199. return
  200. }
  201. func ResetEdbInfoIsUpdate() (err error) {
  202. o := global.DbMap[utils.DbNameIndex]
  203. sql := ` UPDATE edb_info SET is_update=1 `
  204. err = o.Exec(sql).Error
  205. return
  206. }
  207. // GetEdbInfoCalculateListByCondition 获取指标关系列表
  208. func GetEdbInfoCalculateListByCondition(condition string, pars []interface{}) (items []*EdbInfoCalculateMapping, err error) {
  209. o := global.DbMap[utils.DbNameIndex]
  210. //calculateTableName := GetEdbInfoCalculateTableName(source)
  211. //if calculateTableName == "" {
  212. // err = errors.New("无效的表名")
  213. // return
  214. //}
  215. sql := ` SELECT * FROM edb_info_calculate_mapping WHERE 1=1 `
  216. //sql = fmt.Sprintf(sql, calculateTableName)
  217. if condition != "" {
  218. sql += condition
  219. }
  220. err = o.Raw(sql, pars...).Find(&items).Error
  221. return
  222. }
  223. func DeleteEdbDataByIdAndSource(edbDataId, source, subSource int) (err error) {
  224. sql := ` DELETE FROM %s WHERE edb_data_id=? `
  225. tableName := GetEdbDataTableName(source, subSource)
  226. sql = fmt.Sprintf(sql, tableName)
  227. o := global.DbMap[utils.DbNameIndex]
  228. err = o.Exec(sql, edbDataId).Error
  229. return
  230. }
  231. type EdbInfoClassify struct {
  232. EdbInfoId int `gorm:"primaryKey"`
  233. SourceName string `description:"来源名称"`
  234. Source int `description:"来源id"`
  235. EdbCode string `description:"指标编码"`
  236. ClassifyId int `description:"分类id"`
  237. SysUserId int
  238. SysUserRealName string
  239. UniqueCode string `description:"指标唯一编码"`
  240. CreateTime time.Time
  241. ModifyTime time.Time
  242. }
  243. // GetAllEdbInfoClassifyListByCondition
  244. // @Description: 获取指标与分类的关系列表
  245. // @author: Roc
  246. // @datetime 2024-02-29 10:55:38
  247. // @param condition string
  248. // @param pars []interface{}
  249. // @return item []*EdbInfoUpdateLog
  250. // @return err error
  251. func GetAllEdbInfoClassifyListByCondition(condition string, pars []interface{}) (item []*EdbInfoClassify, err error) {
  252. o := global.DbMap[utils.DbNameIndex]
  253. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  254. if condition != "" {
  255. sql += condition
  256. }
  257. sql += `ORDER BY edb_info_id ASC `
  258. err = o.Raw(sql, pars...).Find(&item).Error
  259. return
  260. }
  261. // GetEdbInfoItemByCodeAndSource
  262. // @Description: 根据指标编码和来源id获取指标信息
  263. // @author: Roc
  264. // @datetime 2024-03-11 16:26:23
  265. // @param source int
  266. // @param edbCode string
  267. // @return item *EdbInfo
  268. // @return err error
  269. func GetEdbInfoItemByCodeAndSource(source int, edbCode string) (item *EdbInfoItem, err error) {
  270. o := global.DbMap[utils.DbNameIndex]
  271. sql := ` SELECT * FROM edb_info WHERE edb_code=? AND source = ?`
  272. err = o.Raw(sql, source, edbCode).First(&item).Error
  273. if err != nil {
  274. return
  275. }
  276. if item != nil && item.EdbInfoId > 0 {
  277. item.ConvertTimeStr()
  278. }
  279. return
  280. }
  281. // GetEdbInfoMaxModifyTime
  282. // @Description: 根据指标来源和编码获取该指标数据最晚修改时间
  283. // @author: Roc
  284. // @datetime 2024-03-11 17:01:01
  285. // @param source int
  286. // @param edbCode string
  287. // @return modifyTime string
  288. // @return err error
  289. func GetEdbInfoMaxModifyTime(source, subSource int, edbCode string) (modifyTime string, err error) {
  290. o := global.DbMap[utils.DbNameIndex]
  291. tableName := GetEdbDataTableName(source, subSource)
  292. if tableName == "" {
  293. err = errors.New("无效的表名称:source:" + strconv.Itoa(source))
  294. return
  295. }
  296. sql := ` SELECT MAX(modify_time) AS modify_time FROM %s WHERE edb_code=? `
  297. sql = fmt.Sprintf(sql, tableName)
  298. err = o.Raw(sql, edbCode).Scan(&modifyTime).Error
  299. return
  300. }
  301. func GetEdbInfoPageByCondition(condition string, pars []interface{}, startPage, pageSize int) (item []*EdbInfo, err error) {
  302. o := global.DbMap[utils.DbNameIndex]
  303. sql := ` SELECT * FROM edb_info WHERE 1=1 `
  304. if condition != "" {
  305. sql += condition
  306. }
  307. sql += ` LIMIT ?,? `
  308. pars = append(pars, startPage, pageSize)
  309. err = o.Raw(sql, pars...).Find(&item).Error
  310. return
  311. }
  312. func GetEdbInfoCountByCondition(condition string, pars []interface{}) (total int64, err error) {
  313. o := global.DbMap[utils.DbNameIndex]
  314. sql := ` SELECT count(*) FROM edb_info WHERE 1=1 `
  315. if condition != "" {
  316. sql += condition
  317. }
  318. err = o.Raw(sql, pars...).Scan(&total).Error
  319. return
  320. }
  321. func ModifyEdbUpdateStatus(edbIdList []int, indexCodeList []string, calculateEdbInfoIds []int) (err error) {
  322. idNum := len(edbIdList)
  323. if idNum <= 0 {
  324. return
  325. }
  326. o := global.DbMap[utils.DbNameIndex]
  327. tx := o.Begin()
  328. if tx.Error != nil {
  329. return tx.Error
  330. }
  331. defer func() {
  332. if err != nil {
  333. _ = tx.Rollback()
  334. return
  335. }
  336. _ = tx.Commit()
  337. }()
  338. // 更改指标的更新状态
  339. sql := ` UPDATE edb_info SET no_update = 1 WHERE source in (?,?,?,?,?) AND edb_info_id IN (` + utils.GetOrmInReplace(idNum) + `) AND no_update = 0`
  340. err = tx.Exec(sql, utils.DATA_SOURCE_MYSTEEL_CHEMICAL, utils.DATA_SOURCE_WIND, utils.DATA_SOURCE_PB_FINANCE, utils.DATA_SOURCE_PB, utils.DATA_SOURCE_THS, edbIdList).Error
  341. if err != nil {
  342. return
  343. }
  344. // 更改钢联化工指标更新状态
  345. if len(indexCodeList) > 0 {
  346. // 更改数据源的更新状态
  347. sql = ` UPDATE base_from_mysteel_chemical_index SET is_stop = 1 WHERE index_code IN (` + utils.GetOrmInReplace(len(indexCodeList)) + `) and is_stop=0`
  348. err = tx.Exec(sql, indexCodeList).Error
  349. if err != nil {
  350. return
  351. }
  352. }
  353. // 更新相关的计算指标状态
  354. if len(calculateEdbInfoIds) > 0 {
  355. // 批量更新相关联的指标ID
  356. sql = ` UPDATE edb_info SET no_update = 1 WHERE edb_info_id IN (` + utils.GetOrmInReplace(len(calculateEdbInfoIds)) + `) AND no_update = 0`
  357. err = tx.Exec(sql, calculateEdbInfoIds).Error
  358. if err != nil {
  359. return
  360. }
  361. }
  362. return
  363. }
  364. //func StopRefreshCalculateEdbIndex(edbInfoId int) (err error) {
  365. // o := global.DbMap[utils.DbNameIndex]
  366. // // 更改指标的更新状态
  367. // sql := ` UPDATE edb_info SET no_update = 1,set_update_time=? WHERE edb_type =2 AND edb_info_id = ? AND no_update = 0`
  368. // err = o.Exec(sql, time.Now(), edbInfoId).Error
  369. // return
  370. //}
  371. // GetEdbInfoByIdList 根据指标id集合 获取 指标列表
  372. func GetEdbInfoByIdList(edbInfoIdList []int) (items []*EdbInfo, err error) {
  373. num := len(edbInfoIdList)
  374. if num <= 0 {
  375. return
  376. }
  377. o := global.DbMap[utils.DbNameIndex]
  378. sql := ` SELECT * FROM edb_info WHERE edb_info_id in (` + utils.GetOrmInReplace(num) + `) `
  379. err = o.Raw(sql, edbInfoIdList).Find(&items).Error
  380. return
  381. }
  382. func GetEdbInfoBySourceAndTerminalCode(source int, terminalCode string) (item *EdbInfo, err error) {
  383. o := global.DbMap[utils.DbNameIndex]
  384. sql := ` SELECT * FROM edb_info WHERE source = ? and sub_source = 0 and terminal_code = ? and frequency = '日度' and no_update = 0 limit 1 `
  385. err = o.Raw(sql, source, terminalCode).First(&item).Error
  386. return
  387. }