edb_info.go 12 KB

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