base_from_rzd_index.go 8.1 KB

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