edb_info.go 11 KB

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