base_from_rzd_index.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // Package data_manage
  2. // @Author gmy 2024/8/7 9:38:00
  3. package data_manage
  4. import (
  5. "eta/eta_api/utils"
  6. "fmt"
  7. "github.com/beego/beego/v2/client/orm"
  8. "github.com/rdlucklib/rdluck_tools/paging"
  9. "time"
  10. )
  11. type BaseFromRzdIndex struct {
  12. BaseFromRzdIndexId int `orm:"column(base_from_rzd_index_id);pk"`
  13. CreateTime time.Time `orm:"column(create_time)"`
  14. ModifyTime time.Time `orm:"column(modify_time)"`
  15. BaseFromRzdClassifyId int `orm:"column(base_from_rzd_classify_id)"`
  16. IndexCode string `orm:"column(index_code)"`
  17. IndexName string `orm:"column(index_name)"`
  18. Frequency string `orm:"column(frequency)"`
  19. Unit string `orm:"column(unit)"`
  20. StartDate time.Time `orm:"column(start_date)"`
  21. EndDate time.Time `orm:"column(end_date)"`
  22. LatestValue float64 `orm:"column(latest_value)" description:"数据值"`
  23. }
  24. type BaseFromRzdIndexAndData struct {
  25. BaseFromRzdIndexId int `orm:"column(base_from_rzd_index_id);pk"`
  26. CreateTime string `orm:"column(create_time)"`
  27. ModifyTime string `orm:"column(modify_time)"`
  28. BaseFromRzdClassifyId int `orm:"column(base_from_rzd_classify_id)"`
  29. IndexCode string `orm:"column(index_code)"`
  30. IndexName string `orm:"column(index_name)"`
  31. Frequency string `orm:"column(frequency)"`
  32. Unit string `orm:"column(unit)"`
  33. ModifyTimeMax string `json:"modify_time_max" description:"最后修改时间"`
  34. Value float64 `orm:"column(value)" description:"数据值"`
  35. }
  36. func (m *BaseFromRzdIndex) ToIndexAndData() (item *BaseFromRzdIndexAndData) {
  37. return &BaseFromRzdIndexAndData{
  38. BaseFromRzdIndexId: m.BaseFromRzdIndexId,
  39. CreateTime: m.CreateTime.Format(utils.FormatDateTime),
  40. ModifyTime: m.ModifyTime.Format(utils.FormatDateTime),
  41. BaseFromRzdClassifyId: m.BaseFromRzdClassifyId,
  42. IndexCode: m.IndexCode,
  43. IndexName: m.IndexName,
  44. Frequency: m.Frequency,
  45. Unit: m.Unit,
  46. //ModifyTimeMax: "",
  47. //Value: 0,
  48. }
  49. }
  50. func (m *BaseFromRzdIndex) ToIndexList() (item *BaseFromRzdIndexList) {
  51. return &BaseFromRzdIndexList{
  52. BaseFromRzdIndexId: m.BaseFromRzdIndexId,
  53. CreateTime: m.CreateTime.Format(utils.FormatDateTime),
  54. ModifyTime: m.ModifyTime.Format(utils.FormatDateTime),
  55. BaseFromRzdClassifyId: m.BaseFromRzdClassifyId,
  56. IndexCode: m.IndexCode,
  57. IndexName: m.IndexName,
  58. Frequency: m.Frequency,
  59. Unit: m.Unit,
  60. //DataList: nil,
  61. //Paging: nil,
  62. //EdbInfoId: 0,
  63. }
  64. }
  65. type BaseFromRzdIndexPage struct {
  66. List []*BaseFromRzdIndexAndData `description:"指标列表"`
  67. Paging *paging.PagingItem `description:"分页数据"`
  68. }
  69. type BaseFromRzdIndexList struct {
  70. BaseFromRzdIndexId int `orm:"column(base_from_rzd_index_id);pk"`
  71. CreateTime string `orm:"column(create_time)"`
  72. ModifyTime string `orm:"column(modify_time)"`
  73. BaseFromRzdClassifyId int `orm:"column(base_from_rzd_classify_id)"`
  74. IndexCode string `orm:"column(index_code)"`
  75. IndexName string `orm:"column(index_name)"`
  76. Frequency string `orm:"column(frequency)"`
  77. Unit string `orm:"column(unit)"`
  78. DataList []*BaseFromRzdData
  79. Paging *paging.PagingItem `description:"分页数据"`
  80. EdbInfoId int `description:"指标库主键id"`
  81. }
  82. // RzdNameCheckResult 校验名称是否存在
  83. type RzdNameCheckResult struct {
  84. IndexCode string `from:"EdbCode" description:"edb编码"`
  85. IndexName string `from:"EdbName" description:"edb名称"`
  86. Exist bool
  87. }
  88. // RzdIndexCheckData 校验编码是否存在
  89. type RzdIndexCheckData struct {
  90. IndexCode string `orm:"column(index_code)" description:"指标编码"`
  91. IndexName string `orm:"column(index_name)" description:"指标名称"`
  92. Frequency string `orm:"column(frequency)" description:"频度"`
  93. Unit string `orm:"column(unit)" description:"单位"`
  94. EdbInfoId int `json:"edb_info_id" description:"指标库主键id"`
  95. UniqueCode string `json:"unique_code" description:"指标库唯一编码"`
  96. ClassifyId int `json:"classify_id" description:"分类id"`
  97. }
  98. // BaseFromRzdIndexBatchAddCheckReq 校验编码是否存在请求参数
  99. type BaseFromRzdIndexBatchAddCheckReq struct {
  100. IndexCodes []string `form:"IndexCodes" description:"指标编码列表"`
  101. ClassifyIdList []int `description:"分类id"`
  102. FrequencyList []string `description:"频度"`
  103. SearchParams string `description:"搜索参数 指标编码/指标名称"`
  104. IsCheckAll bool `description:"是否全选"`
  105. }
  106. func init() {
  107. orm.RegisterModel(new(BaseFromRzdIndex))
  108. }
  109. // GetRzdIndexByClassifyIds 根据分类id获取指标信息
  110. func GetRzdIndexByClassifyIds(classifyIds []int) (items []*BaseFromRzdIndex, err error) {
  111. num := len(classifyIds)
  112. if num <= 0 {
  113. return
  114. }
  115. o := orm.NewOrmUsingDB("data")
  116. sql := fmt.Sprintf(`SELECT * FROM base_from_rzd_index WHERE base_from_rzd_classify_id IN (`+utils.GetOrmInReplace(num)) + `)`
  117. _, err = o.Raw(sql, classifyIds).QueryRows(&items)
  118. if err != nil {
  119. return nil, err
  120. }
  121. return items, nil
  122. }
  123. func GetRzdIndex(condition string, pars interface{}) (items []*BaseFromRzdIndex, err error) {
  124. o := orm.NewOrmUsingDB("data")
  125. sql := ` SELECT * FROM base_from_rzd_index WHERE 1=1 `
  126. if condition != "" {
  127. sql += condition
  128. }
  129. sql += ` ORDER BY base_from_rzd_index_id asc`
  130. _, err = o.Raw(sql, pars).QueryRows(&items)
  131. return
  132. }
  133. func GetRzdIndexNotExistEdbInfoCount(condition string, pars interface{}) (count int, err error) {
  134. o := orm.NewOrmUsingDB("data")
  135. sql := ` SELECT count(1) FROM base_from_rzd_index WHERE index_code not in (select edb_code from edb_info) `
  136. if condition != "" {
  137. sql += condition
  138. }
  139. err = o.Raw(sql, pars).QueryRow(&count)
  140. return
  141. }
  142. func GetRzdIndexNotExistEdbInfoPage(condition string, pars interface{}) (items []*BaseFromRzdIndexAndData, err error) {
  143. o := orm.NewOrmUsingDB("data")
  144. sql := ` SELECT * FROM base_from_rzd_index WHERE index_code not in (select edb_code from edb_info) `
  145. if condition != "" {
  146. sql += condition
  147. }
  148. _, err = o.Raw(sql, pars).QueryRows(&items)
  149. return
  150. }
  151. // GetRzdIndexFrequency 获取指标频度
  152. func GetRzdIndexFrequency(classifyIdList []int) (items []*string, err error) {
  153. sql := `SELECT DISTINCT frequency
  154. FROM base_from_rzd_index
  155. WHERE frequency is not null`
  156. // 如果 classifyId > 0,则添加该条件
  157. if len(classifyIdList) > 0 {
  158. sql += ` AND base_from_rzd_classify_id in (` + utils.GetOrmInReplace(len(classifyIdList)) + `)`
  159. }
  160. sql += ` ORDER BY FIELD(frequency, '日度', '周度', '月度', '季度', '半年度', '年度')`
  161. o := orm.NewOrmUsingDB("data")
  162. if len(classifyIdList) > 0 {
  163. _, err = o.Raw(sql, classifyIdList).QueryRows(&items)
  164. } else {
  165. _, err = o.Raw(sql).QueryRows(&items)
  166. }
  167. return items, err
  168. }
  169. // GetRzdIndexByCodeAndClassify 根据指标编码和分类查询 indexCode非必传
  170. func GetRzdIndexByCodeAndClassify(indexCode string, classifyIdList []int, frequency *string) (items []*BaseFromRzdIndex, err error) {
  171. o := orm.NewOrmUsingDB("data")
  172. // SQL 查询语句
  173. sql := `SELECT a.index_code, a.index_name, a.frequency, a.unit, MAX(b.modify_time) AS modify_time
  174. FROM base_from_rzd_index AS a
  175. INNER JOIN base_from_rzd_data AS b ON a.base_from_rzd_index_id = b.base_from_rzd_index_id
  176. WHERE 1=1`
  177. var params []interface{}
  178. if len(classifyIdList) > 0 {
  179. sql += ` AND a.base_from_rzd_classify_id in (` + utils.GetOrmInReplace(len(classifyIdList)) + `)`
  180. for _, id := range classifyIdList {
  181. params = append(params, id)
  182. }
  183. }
  184. // 如果 indexCode 不为空,增加过滤条件
  185. if indexCode != "" {
  186. sql += ` AND a.index_code = ?`
  187. params = append(params, indexCode)
  188. }
  189. if frequency != nil {
  190. sql += ` AND a.frequency = ?`
  191. params = append(params, *frequency)
  192. }
  193. sql += ` GROUP BY a.index_code, a.index_name, a.frequency, a.unit`
  194. _, err = o.Raw(sql, params...).QueryRows(&items)
  195. if err != nil {
  196. return nil, err
  197. }
  198. return
  199. }
  200. // GetRzdIndexInfoCount 分页查询指标信息行数
  201. func GetRzdIndexInfoCount(condition string, pars []interface{}) (count int, err error) {
  202. o := orm.NewOrmUsingDB("data")
  203. sql := ` SELECT count(1) FROM base_from_rzd_index WHERE index_code not in (select edb_code from edb_info) `
  204. if condition != "" {
  205. sql += condition
  206. }
  207. err = o.Raw(sql, pars).QueryRow(&count)
  208. return
  209. }
  210. // GetRzdIndexInfoPage 分页查询指标信息
  211. func GetRzdIndexInfoPage(condition string, pars []interface{}) (items []*BaseFromRzdIndexAndData, err error) {
  212. o := orm.NewOrmUsingDB("data")
  213. sql := ` SELECT * FROM base_from_rzd_index WHERE index_code not in (select edb_code from edb_info) `
  214. if condition != "" {
  215. sql += condition
  216. }
  217. _, err = o.Raw(sql, pars).QueryRows(&items)
  218. return
  219. }