base_from_hisugar.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. EdbInfoId int `description:"指标库主键id"`
  59. }
  60. type BaseFromHisugarIndexListResp struct {
  61. List []*BaseFromHisugarIndexView
  62. Paging *paging.PagingItem `description:"分页数据"`
  63. }
  64. func GetHisugarIndexById(indexId int) (item *BaseFromHisugarIndex, err error) {
  65. o := orm.NewOrmUsingDB("data")
  66. sql := ` SELECT * FROM base_from_hisugar_index WHERE 1=1 base_from_hisugar_index_id = ? `
  67. sql += `ORDER BY base_from_hisugar_index_id ASC `
  68. err = o.Raw(sql, indexId).QueryRow(&item)
  69. return
  70. }
  71. func GetHisugarIndexByCode(indexCode string) (item *BaseFromHisugarIndexView, err error) {
  72. o := orm.NewOrmUsingDB("data")
  73. sql := ` SELECT a.*,CASE WHEN e.edb_info_id IS NULL THEN 0 ELSE 1 END AS edb_exist,e.edb_info_id
  74. FROM base_from_hisugar_index as a
  75. LEFT JOIN edb_info AS e ON a.index_code=e.edb_code AND e.source=93
  76. WHERE 1=1 and a.index_code = ? `
  77. sql += `ORDER BY a.base_from_hisugar_index_id ASC `
  78. err = o.Raw(sql, indexCode).QueryRow(&item)
  79. return
  80. }
  81. func GetHisugarIndexList(condition string, pars interface{}, startSize, pageSize int) (items []*BaseFromHisugarIndex, err error) {
  82. o := orm.NewOrmUsingDB("data")
  83. sql := ` SELECT * FROM base_from_hisugar_index WHERE 1=1 `
  84. if condition != "" {
  85. sql += condition
  86. }
  87. sql += `group BY index_code ASC order by create_time DESC LIMIT ?,? `
  88. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  89. return
  90. }
  91. func GetHisugarIndexListCount(condition string, pars interface{}) (count int, err error) {
  92. o := orm.NewOrmUsingDB("data")
  93. sql := ` SELECT COUNT(1) AS count FROM base_from_hisugar_index WHERE 1=1 `
  94. if condition != "" {
  95. sql += condition
  96. }
  97. err = o.Raw(sql, pars).QueryRow(&count)
  98. return
  99. }
  100. func GetHisugarDataListCount(condition string, pars interface{}) (count int, err error) {
  101. o := orm.NewOrmUsingDB("data")
  102. sql := ` SELECT COUNT(1) AS count FROM base_from_hisugar_data WHERE 1=1 `
  103. if condition != "" {
  104. sql += condition
  105. }
  106. err = o.Raw(sql, pars).QueryRow(&count)
  107. return
  108. }
  109. func GetHisugarIndexData(condition string, pars interface{}, startSize, pageSize int) (items []*BaseFromHisugarData, err error) {
  110. o := orm.NewOrmUsingDB("data")
  111. sql := ` SELECT * FROM base_from_hisugar_data WHERE 1=1 `
  112. if condition != "" {
  113. sql += condition
  114. }
  115. sql += ` order by data_time DESC LIMIT ?,? `
  116. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  117. return
  118. }
  119. // GetHisugarItemList 模糊查询泛糖科技数据库指标列表
  120. func GetHisugarItemList(keyword string) (items []*BaseFromHisugarIndex, err error) {
  121. o := orm.NewOrmUsingDB("data")
  122. sql := "SELECT * FROM base_from_hisugar_index WHERE CONCAT(index_name,index_code) LIKE ? "
  123. _, err = o.Raw(sql, utils.GetLikeKeyword(keyword)).QueryRows(&items)
  124. return
  125. }
  126. // HisugarDataBatchListReq 泛糖科技指标批量列表
  127. type HisugarDataBatchListReq struct {
  128. ClassifyId int `description:"分类id"`
  129. KeyWord string `description:"关键字"`
  130. SelectedId []int `description:"已选指标id, 为true时表示反选"`
  131. IsSelectAll bool `description:"是否查询全部, 默认false, true:全选, false:查询已选"`
  132. }
  133. // GetHisugarIndexByCondition 根据条件获取泛糖科技指标列表
  134. func GetHisugarIndexByCondition(condition string, pars []interface{}) (items []*BaseFromHisugarIndex, err error) {
  135. o := orm.NewOrmUsingDB("data")
  136. sql := ` SELECT * FROM base_from_hisugar_index WHERE 1=1 `
  137. if condition != "" {
  138. sql += condition
  139. }
  140. sql += ` ORDER BY sort ASC, base_from_hisugar_index_id ASC`
  141. _, err = o.Raw(sql, pars).QueryRows(&items)
  142. return
  143. }
  144. // HisugarDataBatchAddCheckReq 泛糖科技指标批量添加校验
  145. type HisugarDataBatchAddCheckReq struct {
  146. IndexCodes []string `description:"指标编码"`
  147. }
  148. // GetHisugarIndexAndEdbInfoByCondition 根据条件获取泛糖科技index和指标库的信息
  149. func GetHisugarIndexAndEdbInfoByCondition(condition string, pars []interface{}) (items []*BaseFromHisugarIndexView, err error) {
  150. o := orm.NewOrmUsingDB("data")
  151. 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 `
  152. if condition != "" {
  153. sql += condition
  154. }
  155. sql += ` ORDER BY sort ASC `
  156. _, err = o.Raw(sql, utils.DATA_SOURCE_HISUGAR, pars).QueryRows(&items)
  157. return
  158. }
  159. type BaseFromHisugarIndexView struct {
  160. BaseFromHisugarIndexId int `orm:"pk"`
  161. EdbInfoId int `description:"指标库id"`
  162. ClassifyId int `description:"指标分类id"`
  163. IndexCode string `description:"指标编码"`
  164. IndexName string `description:"指标名称"`
  165. UniqueCode string `description:"唯一code"`
  166. Frequency string `description:"频度"`
  167. Unit string `description:"单位"`
  168. StartDate string `description:"开始日期"`
  169. EndDate string `description:"结束日期"`
  170. Sort int `description:"排序"`
  171. EdbExist int `description:"edb是否存在"`
  172. EdbClassifyId int `description:"edb分类id"`
  173. ModifyTime string
  174. DataTime string `description:"数据时间"`
  175. Value string `description:"值"`
  176. }
  177. // ExportHisugarExcelReq 导出泛糖科技excel指标
  178. type ExportHisugarExcelReq struct {
  179. KeyWord string `description:"关键字, 指标编码或指标ID"`
  180. IndexCode []string `description:"指标编码,全选时,表示反选"`
  181. IsSelectedAll bool `description:"是否全选:true:全选|false: 无"`
  182. ClassifyId int `description:"指标id"`
  183. }
  184. // GetHisugarIndexByConditionAndFrequency 根据条件获取泛糖科技指标列表
  185. func GetHisugarIndexByConditionAndFrequency(condition, frequency string, pars []interface{}) (items []*BaseFromHisugarIndex, err error) {
  186. o := orm.NewOrmUsingDB("data")
  187. sql := ` SELECT * FROM base_from_hisugar_index WHERE 1=1 `
  188. if condition != "" {
  189. sql += condition
  190. }
  191. sql += ` AND frequency=?`
  192. sql += ` ORDER BY sort ASC, base_from_hisugar_index_id ASC`
  193. _, err = o.Raw(sql, pars, frequency).QueryRows(&items)
  194. return
  195. }
  196. func GetHisugarDataMaxCount(classifyId int) (count int, err error) {
  197. o := orm.NewOrmUsingDB("data")
  198. sql := `SELECT MAX(t.num) AS count FROM (
  199. SELECT COUNT(1) AS num FROM base_from_hisugar_index AS a
  200. INNER JOIN base_from_hisugar_data AS b ON a.index_code=b.index_code
  201. WHERE a.classify_id=?
  202. GROUP BY a.base_from_hisugar_index_id
  203. )AS t `
  204. err = o.Raw(sql, classifyId).QueryRow(&count)
  205. return
  206. }
  207. func GetHisugarIndexDataByCode(indexCode string) (items []*BaseFromHisugarData, err error) {
  208. o := orm.NewOrmUsingDB("data")
  209. sql := ` SELECT * FROM base_from_hisugar_data WHERE index_code=? ORDER BY data_time DESC `
  210. _, err = o.Raw(sql, indexCode).QueryRows(&items)
  211. return
  212. }
  213. func GetHisugarFrequencyByCondition(condition string, pars []interface{}) (items []*string, err error) {
  214. sql := `SELECT DISTINCT frequency FROM base_from_hisugar_index WHERE 1=1 `
  215. if condition != "" {
  216. sql += condition
  217. }
  218. sql += ` ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年','年度') `
  219. o := orm.NewOrmUsingDB("data")
  220. _, err = o.Raw(sql, pars...).QueryRows(&items)
  221. return
  222. }
  223. // GetHisugarIndexViewList 根据分类id获取泛糖科技指标列表
  224. func GetHisugarIndexViewList(condition string, pars []interface{}) (items []*BaseFromHisugarIndexView, err error) {
  225. o := orm.NewOrmUsingDB("data")
  226. sql := ` SELECT b.*, e.edb_info_id,
  227. CASE WHEN e.edb_info_id IS NULL THEN 0 ELSE 1 END AS edb_exist
  228. FROM base_from_hisugar_index AS b
  229. LEFT JOIN edb_info AS e ON b.index_code=e.edb_code AND e.source=93
  230. WHERE 1=1 `
  231. if condition != "" {
  232. sql += condition
  233. }
  234. sql += ` ORDER BY b.modify_time ASC `
  235. _, err = o.Raw(sql, pars).QueryRows(&items)
  236. return
  237. }
  238. // GetHisugarIndexViewListCount 根据分类id获取泛糖科技指标列表
  239. func GetHisugarIndexViewListCount(condition string, pars []interface{}) (count int, err error) {
  240. o := orm.NewOrmUsingDB("data")
  241. sql := ` SELECT COUNT(1) AS count
  242. FROM base_from_hisugar_index AS b
  243. LEFT JOIN edb_info AS e ON b.index_code=e.edb_code AND e.source=93
  244. WHERE 1=1 `
  245. if condition != "" {
  246. sql += condition
  247. }
  248. sql += ` ORDER BY b.modify_time ASC `
  249. err = o.Raw(sql, pars).QueryRow(&count)
  250. return
  251. }
  252. // GetHisugarDataViewList 根据指标id获取泛糖科技指标列表
  253. func GetHisugarDataViewList(indexIds []int) (items []*BaseFromHisugarData, err error) {
  254. o := orm.NewOrmUsingDB("data")
  255. sql := ` SELECT * FROM base_from_hisugar_data WHERE base_from_hisugar_index_id IN (` + utils.GetOrmInReplace(len(indexIds)) + `) ORDER BY data_time desc `
  256. _, err = o.Raw(sql, indexIds).QueryRows(&items)
  257. return
  258. }
  259. // GetHisugarDataDataTimeByIndexId 根据指标id获取指标数据的日期列表
  260. func GetHisugarDataDataTimeByIndexId(indexIdList []int) (items []string, err error) {
  261. if len(indexIdList) == 0 {
  262. return
  263. }
  264. o := orm.NewOrmUsingDB("data")
  265. 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`
  266. _, err = o.Raw(sql, indexIdList).QueryRows(&items)
  267. return
  268. }
  269. type BaseFromHisugarClassifyItem struct {
  270. BaseFromHisugarClassifyId int `orm:"column(base_from_hisugar_classify_id);pk"`
  271. ClassifyName string `description:"分类名称"`
  272. ParentId int `description:"父级id"`
  273. Level int `description:"层级"`
  274. Sort int `description:"排序字段"`
  275. UniqueCode string `description:"唯一code"`
  276. ModifyTime time.Time `description:"修改时间"`
  277. CreateTime time.Time `description:"创建时间"`
  278. ClassifyNameEn string `description:"英文分类名称"`
  279. Children []*BaseFromHisugarClassifyItem `description:"子分类"`
  280. }
  281. // 获取所有分类
  282. func GetHisugarClassifyAll() (items []*BaseFromHisugarClassifyItem, err error) {
  283. o := orm.NewOrmUsingDB("data")
  284. sql := ` SELECT * FROM base_from_hisugar_classify ORDER BY sort ASC, base_from_hisugar_classify_id ASC`
  285. _, err = o.Raw(sql).QueryRows(&items)
  286. return
  287. }
  288. func GetHisugarIndex(condition string, pars interface{}) (items []*BaseFromHisugarIndexList, err error) {
  289. o := orm.NewOrmUsingDB("data")
  290. sql := ` SELECT * FROM base_from_hisugar_index WHERE 1=1 `
  291. if condition != "" {
  292. sql += condition
  293. }
  294. sql += ` ORDER BY sort ASC, base_from_hisugar_index_id asc`
  295. _, err = o.Raw(sql, pars).QueryRows(&items)
  296. return
  297. }
  298. type HisugarIndexDataCountGroup struct {
  299. IndexCode string
  300. Count int
  301. }
  302. func GetHisugarIndexDataCountGroup(indexCodes []string) (items []*HisugarIndexDataCountGroup, err error) {
  303. if len(indexCodes) <= 0 {
  304. return
  305. }
  306. o := orm.NewOrmUsingDB("data")
  307. sql := ` SELECT COUNT(1) AS count, index_code FROM base_from_hisugar_data WHERE index_code IN (` + utils.GetOrmInReplace(len(indexCodes)) + `) GROUP BY index_code`
  308. _, err = o.Raw(sql, indexCodes).QueryRows(&items)
  309. return
  310. }
  311. func GetHisugarIndexDataV2(indexCode string, startSize, pageSize int) (items []*BaseFromHisugarData, err error) {
  312. o := orm.NewOrmUsingDB("data")
  313. sql := ` SELECT * FROM base_from_hisugar_data WHERE index_code=? ORDER BY data_time DESC LIMIT ?,? `
  314. _, err = o.Raw(sql, indexCode, startSize, pageSize).QueryRows(&items)
  315. return
  316. }
  317. // GetHisugarIndexInfoCount 查询指标信息总数
  318. func GetHisugarIndexInfoCount(condition string, pars []interface{}) (count int, err error) {
  319. o := orm.NewOrmUsingDB("data")
  320. sql := ` SELECT COUNT(1) AS count FROM base_from_hisugar_index WHERE 1=1 `
  321. if condition != "" {
  322. sql += condition
  323. }
  324. err = o.Raw(sql, pars).QueryRow(&count)
  325. return
  326. }
  327. type BaseFromHisugarIndexPage struct {
  328. List []*BaseFromHisugarIndex `description:"指标列表"`
  329. Paging *paging.PagingItem `description:"分页数据"`
  330. }
  331. // GetHisugarIndexInfoPage 分页查询指标信息
  332. func GetHisugarIndexInfoPage(condition string, pars []interface{}, size int, pageSize int) (items []*BaseFromHisugarIndex, err error) {
  333. o := orm.NewOrmUsingDB("data")
  334. sql := ` SELECT * FROM base_from_hisugar_index WHERE 1=1 `
  335. if condition != "" {
  336. sql += condition
  337. }
  338. sql += ` ORDER BY base_from_hisugar_index_id asc LIMIT ?,?`
  339. _, err = o.Raw(sql, pars, size, pageSize).QueryRows(&items)
  340. return
  341. }
  342. // 获取所有分类
  343. func GetHisugarClassifyById(classifyId int) (ClassifyIds string, err error) {
  344. o := orm.NewOrmUsingDB("data")
  345. sql := ` SELECT GROUP_CONCAT(base_from_hisugar_classify_id) AS classify_ids FROM base_from_hisugar_classify WHERE base_from_hisugar_classify_id=? OR parent_id=?`
  346. err = o.Raw(sql,classifyId,classifyId).QueryRow(&ClassifyIds)
  347. return
  348. }