base_from_fenwei.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. package data_manage
  2. import (
  3. "eta/eta_api/utils"
  4. "github.com/beego/beego/v2/client/orm"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "time"
  7. )
  8. type BaseFromFenweiIndex struct {
  9. FenweiIndexId int `orm:"column(fenwei_index_id);pk"`
  10. ClassifyId int
  11. IndexCode string
  12. IndexName string
  13. Frequency string
  14. Unit string
  15. Sort int
  16. CreateTime time.Time
  17. ModifyTime time.Time
  18. }
  19. type BaseFromFenweiIndexList struct {
  20. FenweiIndexId int `orm:"column(fenwei_index_id);pk"`
  21. ClassifyId int
  22. IndexCode string
  23. IndexName string
  24. Frequency string
  25. Unit string
  26. Sort int
  27. CreateTime string
  28. ModifyTime string
  29. DataList []*BaseFromFenweiData
  30. Paging *paging.PagingItem `description:"分页数据"`
  31. EdbInfoId int `description:"指标库主键id"`
  32. }
  33. type FenweiSingleDataResp struct {
  34. FenweiIndexId int
  35. ClassifyId int
  36. IndexCode string
  37. IndexName string
  38. Frequency string
  39. Unit string
  40. CreateTime string
  41. ModifyTime string
  42. Data []*FenweiSingleData
  43. EdbInfoId int `description:"指标库主键id"`
  44. }
  45. type FenweiSingleData struct {
  46. Value string `orm:"column(value)" description:"日期"`
  47. DataTime string `orm:"column(data_time)" description:"值"`
  48. }
  49. // BaseFromFenWeiIndexBatchAddCheckReq 校验编码是否存在请求参数
  50. type BaseFromFenWeiIndexBatchAddCheckReq struct {
  51. IndexCodes []string `form:"IndexCodes" description:"指标编码列表"`
  52. }
  53. // IndexCheckData 校验编码是否存在
  54. type FenWeiIndexCheckData struct {
  55. IndexCode string `orm:"column(index_code)" description:"指标编码"`
  56. IndexName string `orm:"column(index_name)" description:"指标名称"`
  57. Frequency string `orm:"column(frequency)" description:"频度"`
  58. Unit string `orm:"column(unit)" description:"单位"`
  59. EdbInfoId int `json:"edb_info_id" description:"指标库主键id"`
  60. UniqueCode string `json:"unique_code" description:"指标库唯一编码"`
  61. ClassifyId int `json:"classify_id" description:"分类id"`
  62. }
  63. // NameCheckResult 校验名称是否存在
  64. type FenWeiNameCheckResult struct {
  65. IndexCode string `from:"EdbCode" description:"edb编码"`
  66. IndexName string `from:"EdbName" description:"edb名称"`
  67. Exist bool
  68. }
  69. // FenWeiIndexAddReq 指标添加vo
  70. type FenWeiIndexAddReq struct {
  71. EdbCode string `description:"指标编码"`
  72. EdbName string `description:"指标名称"`
  73. Frequency string `description:"频度"`
  74. Unit string `description:"单位"`
  75. ClassifyId int `description:"分类ID"`
  76. AdminId int `description:"管理员ID"`
  77. AdminRealName string `description:"管理员名称"`
  78. }
  79. type BaseFromFenWeiIndexPage struct {
  80. List []*BaseFromFenweiIndex `description:"指标列表"`
  81. Paging *paging.PagingItem `description:"分页数据"`
  82. }
  83. func GetFenweiIndex(condition string, pars interface{}) (items []*BaseFromFenweiIndexList, err error) {
  84. o := orm.NewOrmUsingDB("data")
  85. sql := ` SELECT * FROM base_from_fenwei_index WHERE 1=1 `
  86. if condition != "" {
  87. sql += condition
  88. }
  89. sql += ` ORDER BY sort ASC, fenwei_index_id asc`
  90. _, err = o.Raw(sql, pars).QueryRows(&items)
  91. return
  92. }
  93. func GetFenweiIndexDataCount(indexCode string) (count int, err error) {
  94. o := orm.NewOrmUsingDB("data")
  95. sql := ` SELECT COUNT(1) AS count FROM base_from_fenwei_data WHERE index_code=? `
  96. err = o.Raw(sql, indexCode).QueryRow(&count)
  97. return
  98. }
  99. type FenweiIndexDataCountGroup struct {
  100. IndexCode string
  101. Count int
  102. }
  103. func GetFenweiIndexDataCountGroup(indexCodes []string) (items []*FenweiIndexDataCountGroup, err error) {
  104. if len(indexCodes) <= 0 {
  105. return
  106. }
  107. o := orm.NewOrmUsingDB("data")
  108. sql := ` SELECT COUNT(1) AS count, index_code FROM base_from_fenwei_data WHERE index_code IN (` + utils.GetOrmInReplace(len(indexCodes)) + `) GROUP BY index_code`
  109. _, err = o.Raw(sql, indexCodes).QueryRows(&items)
  110. return
  111. }
  112. func GetFenweiIndexData(indexCode string, startSize, pageSize int) (items []*BaseFromFenweiData, err error) {
  113. o := orm.NewOrmUsingDB("data")
  114. sql := ` SELECT * FROM base_from_fenwei_data WHERE index_code=? ORDER BY data_time DESC LIMIT ?,? `
  115. _, err = o.Raw(sql, indexCode, startSize, pageSize).QueryRows(&items)
  116. return
  117. }
  118. func GetFenweiIndexDataByCodes(indexCode []string) (items []*BaseFromFenweiData, err error) {
  119. if len(indexCode) <= 0 {
  120. return
  121. }
  122. o := orm.NewOrmUsingDB("data")
  123. sql := ` SELECT * FROM base_from_fenwei_data WHERE index_code in (` + utils.GetOrmInReplace(len(indexCode)) + `) ORDER BY data_time DESC `
  124. _, err = o.Raw(sql, indexCode).QueryRows(&items)
  125. return
  126. }
  127. type BaseFromFenweiData struct {
  128. FenweiDataId int `orm:"column(fenwei_data_id);pk"`
  129. FenweiIndexId int
  130. IndexCode string
  131. DataTime string
  132. Value string
  133. CreateTime string
  134. ModifyTime string
  135. DataTimestamp int64
  136. }
  137. type BaseFromFenweiIndexSearchItem struct {
  138. FenweiIndexId int `orm:"column(fenwei_index_id);pk"`
  139. ClassifyId int
  140. IndexCode string
  141. IndexName string
  142. }
  143. // GetFenweiItemList 模糊查询汾渭数据库指标列表
  144. func GetFenweiItemList(condition string) (items []*BaseFromFenweiIndexSearchItem, err error) {
  145. o := orm.NewOrmUsingDB("data")
  146. sql := "SELECT * FROM base_from_fenwei_index WHERE 1=1"
  147. if condition != "" {
  148. sql += condition
  149. }
  150. _, err = o.Raw(sql).QueryRows(&items)
  151. return
  152. }
  153. func GetFenweiIndexDataByCode(indexCode string) (list []*BaseFromFenweiData, err error) {
  154. o := orm.NewOrmUsingDB("data")
  155. sql := `SELECT * FROM base_from_fenwei_data WHERE index_code=? `
  156. _, err = o.Raw(sql, indexCode).QueryRows(&list)
  157. return
  158. }
  159. func GetBaseFromFenweiIndexByIndexCode(indexCode string) (list *BaseFromFenweiIndex, err error) {
  160. o := orm.NewOrmUsingDB("data")
  161. sql := ` SELECT * FROM base_from_fenwei_index WHERE index_code=? `
  162. err = o.Raw(sql, indexCode).QueryRow(&list)
  163. return
  164. }
  165. // GetFenWeiIndexFrequency 获取指标频度
  166. func GetFenWeiIndexFrequency(classifyId int) (items []*string, err error) {
  167. sql := `SELECT DISTINCT frequency
  168. FROM base_from_fenwei_index
  169. WHERE frequency is not null`
  170. // 如果 classifyId > 0,则添加该条件
  171. if classifyId > 0 {
  172. sql += ` AND classify_id = ?`
  173. }
  174. sql += ` ORDER BY FIELD(frequency, '日度', '周度', '月度', '季度', '半年度', '年度')`
  175. o := orm.NewOrmUsingDB("data")
  176. if classifyId > 0 {
  177. _, err = o.Raw(sql, classifyId).QueryRows(&items)
  178. } else {
  179. _, err = o.Raw(sql).QueryRows(&items)
  180. }
  181. return items, err
  182. }
  183. // GetFenWeiIndexByCodeAndClassify 根据指标编码和分类查询 indexCode非必传
  184. func GetFenWeiIndexByCodeAndClassify(indexCode string, classifyId int, frequency *string) (items []*BaseFromFenweiIndex, err error) {
  185. o := orm.NewOrmUsingDB("data")
  186. // SQL 查询语句
  187. sql := `SELECT a.index_code, a.index_name, a.frequency, a.unit, MAX(b.modify_time) AS modify_time
  188. FROM base_from_fenwei_index AS a
  189. INNER JOIN base_from_fenwei_data AS b ON a.fenwei_index_id = b.fenwei_index_id
  190. WHERE 1=1`
  191. var params []interface{}
  192. if classifyId != 0 {
  193. sql += ` AND a.classify_id = ?`
  194. params = append(params, classifyId)
  195. }
  196. // 如果 indexCode 不为空,增加过滤条件
  197. if indexCode != "" {
  198. sql += ` AND a.index_code = ?`
  199. params = append(params, indexCode)
  200. }
  201. if frequency != nil {
  202. sql += ` AND a.frequency = ?`
  203. params = append(params, *frequency)
  204. }
  205. sql += ` GROUP BY a.index_code, a.index_name, a.frequency, a.unit`
  206. _, err = o.Raw(sql, params...).QueryRows(&items)
  207. if err != nil {
  208. return nil, err
  209. }
  210. return
  211. }
  212. // GetBaseFromFenWeiDataByIndexCode 根据指标编码查询
  213. func GetBaseFromFenWeiDataByIndexCode(indexCode string) (items []*BaseFromFenweiData, err error) {
  214. sql := `SELECT * FROM base_from_fenwei_data WHERE index_code=? ORDER BY data_time desc`
  215. o := orm.NewOrmUsingDB("data")
  216. _, err = o.Raw(sql, indexCode).QueryRows(&items)
  217. return
  218. }
  219. // GetFenWeiDataListByIndexCodes 根据指标编码查询
  220. func GetFenWeiDataListByIndexCodes(IndexCodes string) (items []string, err error) {
  221. sql := ` SELECT data_time FROM base_from_fenwei_data WHERE index_code IN(` + IndexCodes + `) GROUP BY data_time DESC `
  222. o := orm.NewOrmUsingDB("data")
  223. _, err = o.Raw(sql).QueryRows(&items)
  224. return
  225. }
  226. // GetFenWeiIndexInfoPage 分页查询指标信息
  227. func GetFenWeiIndexInfoPage(condition string, pars []interface{}) (items []*BaseFromFenweiIndex, err error) {
  228. o := orm.NewOrmUsingDB("data")
  229. sql := ` SELECT * FROM base_from_fenwei_index WHERE 1=1 `
  230. if condition != "" {
  231. sql += condition
  232. }
  233. sql += ` ORDER BY fenwei_index_id asc`
  234. _, err = o.Raw(sql, pars).QueryRows(&items)
  235. return
  236. }
  237. // GetFenWeiIndexInfoCount 查询指标信息总数
  238. func GetFenWeiIndexInfoCount(condition string, pars []interface{}) (count int, err error) {
  239. o := orm.NewOrmUsingDB("data")
  240. sql := ` SELECT COUNT(1) AS count FROM base_from_fenwei_index WHERE 1=1 `
  241. if condition != "" {
  242. sql += condition
  243. }
  244. err = o.Raw(sql, pars).QueryRow(&count)
  245. return
  246. }
  247. // GetFenWeiDataLastModifyTimeList 查询指标数据最新更新时间
  248. func GetFenWeiDataLastModifyTimeList(indexCodes []string) (items []*BaseFromFenweiData, err error) {
  249. sql := ` SELECT MAX(modify_time) AS modify_time, index_code FROM base_from_fenwei_data WHERE index_code IN(` + utils.GetOrmInReplace(len(indexCodes)) + `) GROUP BY index_code `
  250. o := orm.NewOrmUsingDB("data")
  251. _, err = o.Raw(sql, indexCodes).QueryRows(&items)
  252. return
  253. }