edb_info.go 12 KB

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