base_from_fenwei.go 10 KB

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