base_from_fenwei.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. package data_manage
  2. import (
  3. "eta/eta_api/global"
  4. "eta/eta_api/utils"
  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 `gorm:"-"`
  30. Paging *paging.PagingItem `description:"分页数据" gorm:"-"`
  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. sql := ` SELECT * FROM base_from_fenwei_index WHERE 1=1 `
  85. if condition != "" {
  86. sql += condition
  87. }
  88. sql += ` ORDER BY sort ASC, fenwei_index_id asc`
  89. err = global.DbMap[utils.DbNameIndex].Raw(sql, pars...).Find(&items).Error
  90. return
  91. }
  92. func GetFenweiIndexDataCount(indexCode string) (count int, err error) {
  93. sql := ` SELECT COUNT(1) AS count FROM base_from_fenwei_data WHERE index_code=? `
  94. err = global.DbMap[utils.DbNameIndex].Raw(sql, indexCode).Scan(&count).Error
  95. return
  96. }
  97. type FenweiIndexDataCountGroup struct {
  98. IndexCode string
  99. Count int
  100. }
  101. func GetFenweiIndexDataCountGroup(indexCodes []string) (items []*FenweiIndexDataCountGroup, err error) {
  102. if len(indexCodes) <= 0 {
  103. return
  104. }
  105. 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`
  106. err = global.DbMap[utils.DbNameIndex].Raw(sql, indexCodes).Find(&items).Error
  107. return
  108. }
  109. func GetFenweiIndexData(indexCode string, startSize, pageSize int) (items []*BaseFromFenweiData, err error) {
  110. sql := ` SELECT * FROM base_from_fenwei_data WHERE index_code=? ORDER BY data_time DESC LIMIT ?,? `
  111. err = global.DbMap[utils.DbNameIndex].Raw(sql, indexCode, startSize, pageSize).Find(&items).Error
  112. return
  113. }
  114. func GetFenweiIndexDataByCodes(indexCode []string) (items []*BaseFromFenweiData, err error) {
  115. if len(indexCode) <= 0 {
  116. return
  117. }
  118. sql := ` SELECT * FROM base_from_fenwei_data WHERE index_code in (` + utils.GetOrmInReplace(len(indexCode)) + `) ORDER BY data_time DESC `
  119. err = global.DbMap[utils.DbNameIndex].Raw(sql, indexCode).Find(&items).Error
  120. return
  121. }
  122. type BaseFromFenweiData struct {
  123. FenweiDataId int `orm:"column(fenwei_data_id);pk"`
  124. FenweiIndexId int
  125. IndexCode string
  126. DataTime string
  127. Value string
  128. CreateTime string
  129. ModifyTime string
  130. DataTimestamp int64
  131. }
  132. type BaseFromFenweiIndexSearchItem struct {
  133. FenweiIndexId int `orm:"column(fenwei_index_id);pk"`
  134. ClassifyId int
  135. IndexCode string
  136. IndexName string
  137. }
  138. // GetFenweiItemList 模糊查询汾渭数据库指标列表
  139. func GetFenweiItemList(condition string) (items []*BaseFromFenweiIndexSearchItem, err error) {
  140. sql := "SELECT * FROM base_from_fenwei_index WHERE 1=1"
  141. if condition != "" {
  142. sql += condition
  143. }
  144. err = global.DbMap[utils.DbNameIndex].Raw(sql).Find(&items).Error
  145. return
  146. }
  147. func GetFenweiIndexDataByCode(indexCode string) (list []*BaseFromFenweiData, err error) {
  148. sql := `SELECT * FROM base_from_fenwei_data WHERE index_code=? `
  149. err = global.DbMap[utils.DbNameIndex].Raw(sql, indexCode).Find(&list).Error
  150. return
  151. }
  152. func GetBaseFromFenweiIndexByIndexCode(indexCode string) (list *BaseFromFenweiIndex, err error) {
  153. sql := ` SELECT * FROM base_from_fenwei_index WHERE index_code=? `
  154. err = global.DbMap[utils.DbNameIndex].Raw(sql, indexCode).First(&list).Error
  155. return
  156. }
  157. // GetFenWeiIndexFrequency 获取指标频度
  158. func GetFenWeiIndexFrequency(classifyId int) (items []*string, err error) {
  159. sql := `SELECT DISTINCT frequency
  160. FROM base_from_fenwei_index
  161. WHERE frequency is not null`
  162. // 如果 classifyId > 0,则添加该条件
  163. if classifyId > 0 {
  164. sql += ` AND classify_id = ?`
  165. }
  166. sql += ` ORDER BY FIELD(frequency, '日度', '周度', '月度', '季度', '半年度', '年度')`
  167. if classifyId > 0 {
  168. err = global.DbMap[utils.DbNameIndex].Raw(sql, classifyId).Find(&items).Error
  169. } else {
  170. err = global.DbMap[utils.DbNameIndex].Raw(sql).Find(&items).Error
  171. }
  172. return items, err
  173. }
  174. // GetFenWeiIndexByCodeAndClassify 根据指标编码和分类查询 indexCode非必传
  175. func GetFenWeiIndexByCodeAndClassify(indexCode string, classifyId int, frequency *string) (items []*BaseFromFenweiIndex, err error) {
  176. // SQL 查询语句
  177. sql := `SELECT a.index_code, a.index_name, a.frequency, a.unit, MAX(b.modify_time) AS modify_time
  178. FROM base_from_fenwei_index AS a
  179. INNER JOIN base_from_fenwei_data AS b ON a.fenwei_index_id = b.fenwei_index_id
  180. WHERE 1=1`
  181. var params []interface{}
  182. if classifyId != 0 {
  183. sql += ` AND a.classify_id = ?`
  184. params = append(params, classifyId)
  185. }
  186. // 如果 indexCode 不为空,增加过滤条件
  187. if indexCode != "" {
  188. sql += ` AND a.index_code = ?`
  189. params = append(params, indexCode)
  190. }
  191. if frequency != nil {
  192. sql += ` AND a.frequency = ?`
  193. params = append(params, *frequency)
  194. }
  195. sql += ` GROUP BY a.index_code, a.index_name, a.frequency, a.unit`
  196. err = global.DbMap[utils.DbNameIndex].Raw(sql, params...).Find(&items).Error
  197. if err != nil {
  198. return nil, err
  199. }
  200. return
  201. }
  202. // GetBaseFromFenWeiDataByIndexCode 根据指标编码查询
  203. func GetBaseFromFenWeiDataByIndexCode(indexCode string) (items []*BaseFromFenweiData, err error) {
  204. sql := `SELECT * FROM base_from_fenwei_data WHERE index_code=? ORDER BY data_time desc`
  205. err = global.DbMap[utils.DbNameIndex].Raw(sql, indexCode).Find(&items).Error
  206. return
  207. }
  208. // GetFenWeiDataListByIndexCodes 根据指标编码查询
  209. func GetFenWeiDataListByIndexCodes(IndexCodes string) (items []string, err error) {
  210. sql := ` SELECT data_time FROM base_from_fenwei_data WHERE index_code IN(` + IndexCodes + `) GROUP BY data_time DESC `
  211. err = global.DbMap[utils.DbNameIndex].Raw(sql).Find(&items).Error
  212. return
  213. }
  214. // GetFenWeiIndexInfoPage 分页查询指标信息
  215. func GetFenWeiIndexInfoPage(condition string, pars []interface{}) (items []*BaseFromFenweiIndex, err error) {
  216. sql := ` SELECT * FROM base_from_fenwei_index WHERE 1=1 `
  217. if condition != "" {
  218. sql += condition
  219. }
  220. sql += ` ORDER BY fenwei_index_id asc`
  221. err = global.DbMap[utils.DbNameIndex].Raw(sql, pars...).Find(&items).Error
  222. return
  223. }
  224. // GetFenWeiIndexInfoCount 查询指标信息总数
  225. func GetFenWeiIndexInfoCount(condition string, pars []interface{}) (count int, err error) {
  226. sql := ` SELECT COUNT(1) AS count FROM base_from_fenwei_index WHERE 1=1 `
  227. if condition != "" {
  228. sql += condition
  229. }
  230. err = global.DbMap[utils.DbNameIndex].Raw(sql, pars...).Scan(&count).Error
  231. return
  232. }
  233. // GetFenWeiDataLastModifyTime 获取指标数据最新更新时间
  234. func GetFenWeiDataLastModifyTime(indexCode string) (lastModifyTime string, err error) {
  235. sql := ` SELECT MAX(modify_time) AS last_modify_time FROM base_from_fenwei_data WHERE index_code=? `
  236. err = global.DbMap[utils.DbNameIndex].Raw(sql, indexCode).Scan(&lastModifyTime).Error
  237. return
  238. }
  239. // GetFenWeiDataLastModifyTimeList 查询指标数据最新更新时间
  240. func GetFenWeiDataLastModifyTimeList(indexCodes []string) (items []*BaseFromFenweiData, err error) {
  241. 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 `
  242. err = global.DbMap[utils.DbNameIndex].Raw(sql, indexCodes).Find(&items).Error
  243. return
  244. }