edb_info.go 13 KB

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