base_from_fenwei.go 8.1 KB

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