base_from_hisugar.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. package data_manage
  2. import (
  3. "eta/eta_api/utils"
  4. "github.com/beego/beego/v2/client/orm"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "time"
  7. )
  8. type BaseFromHisugarClassify struct {
  9. BaseFromHisugarClassifyId int // 分类ID
  10. ClassifyName string // 分类名称
  11. ParentID int // 上级ID
  12. Level int // 层级
  13. Sort int // 排序
  14. CreateTime string // 创建时间
  15. ModifyTime string // 修改时间
  16. }
  17. type BaseFromHisugarIndex struct {
  18. BaseFromHisugarIndexId int // 主键ID
  19. IndexCode string // 指标编码
  20. IndexName string // 指标名称
  21. ClassifyId uint // 分类ID
  22. Unit string // 单位
  23. Frequency string // 频度
  24. Describe string // 指标描述
  25. Sort int // 排序
  26. CreateTime string // 创建时间
  27. ModifyTime string // 修改时间
  28. }
  29. type BaseFromHisugarData struct {
  30. BaseFromHisugarDataId int // 数据表ID
  31. BaseFromHisugarIndexId int // 指标ID
  32. IndexCode string // 指标编码
  33. DataTime string
  34. Value string
  35. CreateTime string
  36. ModifyTime string
  37. }
  38. func GetHisugarClassifyList() (list []*BaseFromHisugarClassify, err error) {
  39. o := orm.NewOrmUsingDB("data")
  40. sql := "SELECT * FROM base_from_hisugar_classify ORDER BY sort ASC"
  41. _, err = o.Raw(sql).QueryRows(&list)
  42. return
  43. }
  44. type BaseFromHisugarIndexList struct {
  45. BaseFromHisugarIndexId int // 主键ID
  46. IndexCode string // 指标编码
  47. IndexName string // 指标名称
  48. ClassifyId int // 分类ID
  49. Unit string // 单位
  50. Frequency string // 频度
  51. Describe string // 指标描述
  52. Sort int // 排序
  53. CreateTime string // 创建时间
  54. ModifyTime string // 修改时间
  55. EdbExist int `description:"edb是否存在"`
  56. DataList []*BaseFromHisugarData
  57. Paging *paging.PagingItem `description:"分页数据"`
  58. }
  59. type BaseFromHisugarIndexListResp struct {
  60. List []*BaseFromHisugarIndexView
  61. Paging *paging.PagingItem `description:"分页数据"`
  62. }
  63. func GetHisugarIndexById(indexId int) (item *BaseFromHisugarIndex, err error) {
  64. o := orm.NewOrmUsingDB("data")
  65. sql := ` SELECT * FROM base_from_hisugar_index WHERE 1=1 base_from_hisugar_index_id = ? `
  66. sql += `ORDER BY base_from_hisugar_index_id ASC `
  67. err = o.Raw(sql, indexId).QueryRow(&item)
  68. return
  69. }
  70. func GetHisugarIndexByCode(indexCode string) (item *BaseFromHisugarIndexView, err error) {
  71. o := orm.NewOrmUsingDB("data")
  72. sql := ` SELECT a.*,CASE WHEN e.edb_info_id IS NULL THEN 0 ELSE 1 END AS edb_exist
  73. FROM base_from_hisugar_index as a
  74. LEFT JOIN edb_info AS e ON a.index_code=e.edb_code AND e.source=89
  75. WHERE 1=1 and a.index_code = ? `
  76. sql += `ORDER BY a.base_from_hisugar_index_id ASC `
  77. err = o.Raw(sql, indexCode).QueryRow(&item)
  78. return
  79. }
  80. func GetHisugarIndexList(condition string, pars interface{}, startSize, pageSize int) (items []*BaseFromHisugarIndex, err error) {
  81. o := orm.NewOrmUsingDB("data")
  82. sql := ` SELECT * FROM base_from_hisugar_index WHERE 1=1 `
  83. if condition != "" {
  84. sql += condition
  85. }
  86. sql += `group BY index_code ASC order by create_time DESC LIMIT ?,? `
  87. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  88. return
  89. }
  90. func GetHisugarIndexListCount(condition string, pars interface{}) (count int, err error) {
  91. o := orm.NewOrmUsingDB("data")
  92. sql := ` SELECT COUNT(1) AS count FROM base_from_hisugar_index WHERE 1=1 `
  93. if condition != "" {
  94. sql += condition
  95. }
  96. err = o.Raw(sql, pars).QueryRow(&count)
  97. return
  98. }
  99. func GetHisugarDataListCount(condition string, pars interface{}) (count int, err error) {
  100. o := orm.NewOrmUsingDB("data")
  101. sql := ` SELECT COUNT(1) AS count FROM base_from_hisugar_data WHERE 1=1 `
  102. if condition != "" {
  103. sql += condition
  104. }
  105. err = o.Raw(sql, pars).QueryRow(&count)
  106. return
  107. }
  108. func GetHisugarIndexData(condition string, pars interface{}, startSize, pageSize int) (items []*BaseFromHisugarData, err error) {
  109. o := orm.NewOrmUsingDB("data")
  110. sql := ` SELECT * FROM base_from_hisugar_data WHERE 1=1 `
  111. if condition != "" {
  112. sql += condition
  113. }
  114. sql += ` order by data_time DESC LIMIT ?,? `
  115. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  116. return
  117. }
  118. // GetHisugarItemList 模糊查询泛糖科技数据库指标列表
  119. func GetHisugarItemList(keyword string) (items []*BaseFromHisugarIndex, err error) {
  120. o := orm.NewOrmUsingDB("data")
  121. sql := "SELECT * FROM base_from_hisugar_index WHERE CONCAT(index_name,index_code) LIKE ? "
  122. _, err = o.Raw(sql, utils.GetLikeKeyword(keyword)).QueryRows(&items)
  123. return
  124. }
  125. // HisugarDataBatchListReq 泛糖科技指标批量列表
  126. type HisugarDataBatchListReq struct {
  127. ClassifyId int `description:"分类id"`
  128. KeyWord string `description:"关键字"`
  129. SelectedId []int `description:"已选指标id, 为true时表示反选"`
  130. IsSelectAll bool `description:"是否查询全部, 默认false, true:全选, false:查询已选"`
  131. }
  132. // GetHisugarIndexByCondition 根据条件获取泛糖科技指标列表
  133. func GetHisugarIndexByCondition(condition string, pars []interface{}) (items []*BaseFromHisugarIndex, err error) {
  134. o := orm.NewOrmUsingDB("data")
  135. sql := ` SELECT * FROM base_from_hisugar_index WHERE 1=1 `
  136. if condition != "" {
  137. sql += condition
  138. }
  139. sql += ` ORDER BY sort ASC, base_from_hisugar_index_id ASC`
  140. _, err = o.Raw(sql, pars).QueryRows(&items)
  141. return
  142. }
  143. // HisugarDataBatchAddCheckReq 泛糖科技指标批量添加校验
  144. type HisugarDataBatchAddCheckReq struct {
  145. IndexCodes []string `description:"指标编码"`
  146. }
  147. // GetHisugarIndexAndEdbInfoByCondition 根据条件获取泛糖科技index和指标库的信息
  148. func GetHisugarIndexAndEdbInfoByCondition(condition string, pars []interface{}) (items []*BaseFromHisugarIndexView, err error) {
  149. o := orm.NewOrmUsingDB("data")
  150. sql := ` SELECT b.*, e.edb_info_id, e.unique_code, e.classify_id AS edb_classify_id FROM base_from_hisugar_index AS b LEFT JOIN edb_info AS e ON b.index_code=e.edb_code AND e.source=? WHERE 1=1 `
  151. if condition != "" {
  152. sql += condition
  153. }
  154. sql += ` ORDER BY sort ASC `
  155. _, err = o.Raw(sql, utils.DATA_SOURCE_HISUGAR, pars).QueryRows(&items)
  156. return
  157. }
  158. type BaseFromHisugarIndexView struct {
  159. BaseFromHisugarIndexId int `orm:"pk"`
  160. EdbInfoId int `description:"指标库id"`
  161. ClassifyId int `description:"指标分类id"`
  162. IndexCode string `description:"指标编码"`
  163. IndexName string `description:"指标名称"`
  164. UniqueCode string `description:"唯一code"`
  165. Frequency string `description:"频度"`
  166. Unit string `description:"单位"`
  167. StartDate string `description:"开始日期"`
  168. EndDate string `description:"结束日期"`
  169. Sort int `description:"排序"`
  170. EdbExist int `description:"edb是否存在"`
  171. EdbClassifyId int `description:"edb分类id"`
  172. ModifyTime string
  173. DataTime string `description:"数据时间"`
  174. Value string `description:"值"`
  175. }
  176. // ExportHisugarExcelReq 导出泛糖科技excel指标
  177. type ExportHisugarExcelReq struct {
  178. KeyWord string `description:"关键字, 指标编码或指标ID"`
  179. IndexCode []string `description:"指标编码,全选时,表示反选"`
  180. IsSelectedAll bool `description:"是否全选:true:全选|false: 无"`
  181. ClassifyId int `description:"指标id"`
  182. }
  183. // GetHisugarIndexByConditionAndFrequency 根据条件获取泛糖科技指标列表
  184. func GetHisugarIndexByConditionAndFrequency(condition, frequency string, pars []interface{}) (items []*BaseFromHisugarIndex, err error) {
  185. o := orm.NewOrmUsingDB("data")
  186. sql := ` SELECT * FROM base_from_hisugar_index WHERE 1=1 `
  187. if condition != "" {
  188. sql += condition
  189. }
  190. sql += ` AND frequency=?`
  191. sql += ` ORDER BY sort ASC, base_from_hisugar_index_id ASC`
  192. _, err = o.Raw(sql, pars, frequency).QueryRows(&items)
  193. return
  194. }
  195. func GetHisugarDataMaxCount(classifyId int) (count int, err error) {
  196. o := orm.NewOrmUsingDB("data")
  197. sql := `SELECT MAX(t.num) AS count FROM (
  198. SELECT COUNT(1) AS num FROM base_from_hisugar_index AS a
  199. INNER JOIN base_from_hisugar_data AS b ON a.index_code=b.index_code
  200. WHERE a.classify_id=?
  201. GROUP BY a.base_from_hisugar_index_id
  202. )AS t `
  203. err = o.Raw(sql, classifyId).QueryRow(&count)
  204. return
  205. }
  206. func GetHisugarIndexDataByCode(indexCode string) (items []*BaseFromHisugarData, err error) {
  207. o := orm.NewOrmUsingDB("data")
  208. sql := ` SELECT * FROM base_from_hisugar_data WHERE index_code=? ORDER BY data_time DESC `
  209. _, err = o.Raw(sql, indexCode).QueryRows(&items)
  210. return
  211. }
  212. func GetHisugarFrequencyByCondition(condition string, pars []interface{}) (items []*string, err error) {
  213. sql := `SELECT DISTINCT frequency FROM base_from_hisugar_index WHERE 1=1 `
  214. if condition != "" {
  215. sql += condition
  216. }
  217. sql += ` ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年','年度') `
  218. o := orm.NewOrmUsingDB("data")
  219. _, err = o.Raw(sql, pars...).QueryRows(&items)
  220. return
  221. }
  222. // GetHisugarIndexViewList 根据分类id获取泛糖科技指标列表
  223. func GetHisugarIndexViewList(condition string, pars []interface{}, startSize, pageSize int) (items []*BaseFromHisugarIndexView, err error) {
  224. o := orm.NewOrmUsingDB("data")
  225. sql := ` SELECT b.*, e.edb_info_id,
  226. CASE WHEN e.edb_info_id IS NULL THEN 0 ELSE 1 END AS edb_exist
  227. FROM base_from_hisugar_index AS b
  228. LEFT JOIN edb_info AS e ON b.index_code=e.edb_code AND e.source=89
  229. WHERE 1=1 `
  230. if condition != "" {
  231. sql += condition
  232. }
  233. sql += ` ORDER BY b.modify_time ASC LIMIT ?,? `
  234. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  235. return
  236. }
  237. // GetHisugarIndexViewListCount 根据分类id获取泛糖科技指标列表
  238. func GetHisugarIndexViewListCount(condition string, pars []interface{}) (count int, err error) {
  239. o := orm.NewOrmUsingDB("data")
  240. sql := ` SELECT COUNT(1) AS count
  241. FROM base_from_hisugar_index AS b
  242. LEFT JOIN edb_info AS e ON b.index_code=e.edb_code AND e.source=89
  243. WHERE 1=1 `
  244. if condition != "" {
  245. sql += condition
  246. }
  247. sql += ` ORDER BY b.modify_time ASC `
  248. err = o.Raw(sql, pars).QueryRow(&count)
  249. return
  250. }
  251. // GetHisugarDataViewList 根据指标id获取泛糖科技指标列表
  252. func GetHisugarDataViewList(indexIds []int) (items []*BaseFromHisugarData, err error) {
  253. o := orm.NewOrmUsingDB("data")
  254. sql := ` SELECT * FROM base_from_hisugar_data WHERE base_from_hisugar_index_id IN (` + utils.GetOrmInReplace(len(indexIds)) + `)ORDER BY data_time desc `
  255. _, err = o.Raw(sql, indexIds).QueryRows(&items)
  256. return
  257. }
  258. // GetHisugarDataDataTimeByIndexId 根据指标id获取指标数据的日期列表
  259. func GetHisugarDataDataTimeByIndexId(indexIdList []int) (items []string, err error) {
  260. if len(indexIdList) == 0 {
  261. return
  262. }
  263. o := orm.NewOrmUsingDB("data")
  264. sql := ` SELECT DISTINCT data_time FROM base_from_hisugar_data WHERE base_from_hisugar_index_id IN (` + utils.GetOrmInReplace(len(indexIdList)) + `) ORDER BY data_time DESC`
  265. _, err = o.Raw(sql, indexIdList).QueryRows(&items)
  266. return
  267. }
  268. type BaseFromHisugarClassifyItem struct {
  269. BaseFromHisugarClassifyId int `orm:"column(base_from_hisugar_classify_id);pk"`
  270. ClassifyName string `description:"分类名称"`
  271. ParentId int `description:"父级id"`
  272. Level int `description:"层级"`
  273. Sort int `description:"排序字段"`
  274. UniqueCode string `description:"唯一code"`
  275. ModifyTime time.Time `description:"修改时间"`
  276. CreateTime time.Time `description:"创建时间"`
  277. ClassifyNameEn string `description:"英文分类名称"`
  278. Children []*BaseFromHisugarClassifyItem `description:"子分类"`
  279. }
  280. // 获取所有分类
  281. func GetHisugarClassifyAll() (items []*BaseFromHisugarClassifyItem, err error) {
  282. o := orm.NewOrmUsingDB("data")
  283. sql := ` SELECT * FROM base_from_hisugar_classify ORDER BY sort ASC, base_from_hisugar_classify_id ASC`
  284. _, err = o.Raw(sql).QueryRows(&items)
  285. return
  286. }