base_from_fenwei.go 10 KB

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