edb_info.go 9.2 KB

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