base_from_hisugar.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. package data_manage
  2. import (
  3. "eta/eta_api/global"
  4. "eta/eta_api/utils"
  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 := global.DbMap[utils.DbNameIndex]
  40. sql := "SELECT * FROM base_from_hisugar_classify ORDER BY sort ASC"
  41. err = o.Raw(sql).Find(&list).Error
  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 := global.DbMap[utils.DbNameIndex]
  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).First(&item).Error
  69. return
  70. }
  71. func GetHisugarIndexByCode(indexCode string) (item *BaseFromHisugarIndexView, err error) {
  72. o := global.DbMap[utils.DbNameIndex]
  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).First(&item).Error
  79. return
  80. }
  81. func GetHisugarIndexList(condition string, pars interface{}, startSize, pageSize int) (items []*BaseFromHisugarIndex, err error) {
  82. o := global.DbMap[utils.DbNameIndex]
  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).Find(&items).Error
  89. return
  90. }
  91. func GetHisugarIndexListCount(condition string, pars interface{}) (count int, err error) {
  92. o := global.DbMap[utils.DbNameIndex]
  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).Scan(&count).Error
  98. return
  99. }
  100. func GetHisugarDataListCount(condition string, pars interface{}) (count int, err error) {
  101. o := global.DbMap[utils.DbNameIndex]
  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).Scan(&count).Error
  107. return
  108. }
  109. func GetHisugarIndexData(condition string, pars interface{}, startSize, pageSize int) (items []*BaseFromHisugarData, err error) {
  110. o := global.DbMap[utils.DbNameIndex]
  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).Find(&items).Error
  117. return
  118. }
  119. // GetHisugarItemList 模糊查询泛糖科技数据库指标列表
  120. func GetHisugarItemList(keyword string) (items []*BaseFromHisugarIndex, err error) {
  121. o := global.DbMap[utils.DbNameIndex]
  122. sql := "SELECT * FROM base_from_hisugar_index WHERE CONCAT(index_name,index_code) LIKE ? "
  123. err = o.Raw(sql, utils.GetLikeKeyword(keyword)).Find(&items).Error
  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 := global.DbMap[utils.DbNameIndex]
  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...).Find(&items).Error
  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. wherePars := make([]interface{}, 0)
  151. wherePars = append(wherePars, utils.DATA_SOURCE_HISUGAR)
  152. wherePars = append(wherePars, pars...)
  153. o := global.DbMap[utils.DbNameIndex]
  154. 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 `
  155. if condition != "" {
  156. sql += condition
  157. }
  158. sql += ` ORDER BY sort ASC `
  159. err = o.Raw(sql, wherePars...).Find(&items).Error
  160. return
  161. }
  162. type BaseFromHisugarIndexView struct {
  163. BaseFromHisugarIndexId int `orm:"pk" gorm:"primaryKey"`
  164. EdbInfoId int `description:"指标库id"`
  165. ClassifyId int `description:"指标分类id"`
  166. IndexCode string `description:"指标编码"`
  167. IndexName string `description:"指标名称"`
  168. UniqueCode string `description:"唯一code"`
  169. Frequency string `description:"频度"`
  170. Unit string `description:"单位"`
  171. StartDate string `description:"开始日期"`
  172. EndDate string `description:"结束日期"`
  173. Sort int `description:"排序"`
  174. EdbExist int `description:"edb是否存在"`
  175. EdbClassifyId int `description:"edb分类id"`
  176. ModifyTime string
  177. DataTime string `description:"数据时间"`
  178. Value string `description:"值"`
  179. }
  180. // ExportHisugarExcelReq 导出泛糖科技excel指标
  181. type ExportHisugarExcelReq struct {
  182. KeyWord string `description:"关键字, 指标编码或指标ID"`
  183. IndexCode []string `description:"指标编码,全选时,表示反选"`
  184. IsSelectedAll bool `description:"是否全选:true:全选|false: 无"`
  185. ClassifyId int `description:"指标id"`
  186. }
  187. // GetHisugarIndexByConditionAndFrequency 根据条件获取泛糖科技指标列表
  188. func GetHisugarIndexByConditionAndFrequency(condition, frequency string, pars []interface{}) (items []*BaseFromHisugarIndex, err error) {
  189. o := global.DbMap[utils.DbNameIndex]
  190. sql := ` SELECT * FROM base_from_hisugar_index WHERE 1=1 `
  191. if condition != "" {
  192. sql += condition
  193. }
  194. sql += ` AND frequency=?`
  195. sql += ` ORDER BY sort ASC, base_from_hisugar_index_id ASC`
  196. err = o.Raw(sql, pars, frequency).Find(&items).Error
  197. return
  198. }
  199. func GetHisugarDataMaxCount(classifyId int) (count int, err error) {
  200. o := global.DbMap[utils.DbNameIndex]
  201. sql := `SELECT COALESCE(MAX(t.num), 0) AS count FROM (
  202. SELECT COUNT(1) AS num FROM base_from_hisugar_index AS a
  203. INNER JOIN base_from_hisugar_data AS b ON a.index_code=b.index_code
  204. WHERE a.classify_id=?
  205. GROUP BY a.base_from_hisugar_index_id
  206. )AS t `
  207. err = o.Raw(sql, classifyId).Scan(&count).Error
  208. return
  209. }
  210. func GetHisugarIndexDataByCode(indexCode string) (items []*BaseFromHisugarData, err error) {
  211. o := global.DbMap[utils.DbNameIndex]
  212. sql := ` SELECT * FROM base_from_hisugar_data WHERE index_code=? ORDER BY data_time DESC `
  213. err = o.Raw(sql, indexCode).Find(&items).Error
  214. return
  215. }
  216. func GetHisugarFrequencyByCondition(condition string, pars []interface{}) (items []*string, err error) {
  217. sql := `SELECT DISTINCT frequency FROM base_from_hisugar_index WHERE 1=1 `
  218. if condition != "" {
  219. sql += condition
  220. }
  221. sql += ` ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年','年度') `
  222. o := global.DbMap[utils.DbNameIndex]
  223. err = o.Raw(sql, pars...).Find(&items).Error
  224. return
  225. }
  226. // GetHisugarIndexViewList 根据分类id获取泛糖科技指标列表
  227. func GetHisugarIndexViewList(condition string, pars []interface{}) (items []*BaseFromHisugarIndexView, err error) {
  228. o := global.DbMap[utils.DbNameIndex]
  229. sql := ` SELECT b.*, e.edb_info_id,
  230. CASE WHEN e.edb_info_id IS NULL THEN 0 ELSE 1 END AS edb_exist
  231. FROM base_from_hisugar_index AS b
  232. LEFT JOIN edb_info AS e ON b.index_code=e.edb_code AND e.source=93
  233. WHERE 1=1 `
  234. if condition != "" {
  235. sql += condition
  236. }
  237. sql += ` ORDER BY b.modify_time ASC `
  238. err = o.Raw(sql, pars...).Find(&items).Error
  239. return
  240. }
  241. // GetHisugarIndexViewListCount 根据分类id获取泛糖科技指标列表
  242. func GetHisugarIndexViewListCount(condition string, pars []interface{}) (count int, err error) {
  243. o := global.DbMap[utils.DbNameIndex]
  244. sql := ` SELECT COUNT(1) AS count
  245. FROM base_from_hisugar_index AS b
  246. LEFT JOIN edb_info AS e ON b.index_code=e.edb_code AND e.source=93
  247. WHERE 1=1 `
  248. if condition != "" {
  249. sql += condition
  250. }
  251. sql += ` ORDER BY b.modify_time ASC `
  252. err = o.Raw(sql, pars...).Scan(&count).Error
  253. return
  254. }
  255. // GetHisugarDataViewList 根据指标id获取泛糖科技指标列表
  256. func GetHisugarDataViewList(indexIds []int) (items []*BaseFromHisugarData, err error) {
  257. o := global.DbMap[utils.DbNameIndex]
  258. sql := ` SELECT * FROM base_from_hisugar_data WHERE base_from_hisugar_index_id IN (` + utils.GetOrmInReplace(len(indexIds)) + `) ORDER BY data_time desc `
  259. err = o.Raw(sql, indexIds).Find(&items).Error
  260. return
  261. }
  262. // GetHisugarDataDataTimeByIndexId 根据指标id获取指标数据的日期列表
  263. func GetHisugarDataDataTimeByIndexId(indexIdList []int) (items []string, err error) {
  264. if len(indexIdList) == 0 {
  265. return
  266. }
  267. o := global.DbMap[utils.DbNameIndex]
  268. 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`
  269. err = o.Raw(sql, indexIdList).Find(&items).Error
  270. return
  271. }
  272. type BaseFromHisugarClassifyItem struct {
  273. BaseFromHisugarClassifyId int `orm:"column(base_from_hisugar_classify_id);pk" gorm:"primaryKey"`
  274. ClassifyName string `description:"分类名称"`
  275. ParentId int `description:"父级id"`
  276. Level int `description:"层级"`
  277. Sort int `description:"排序字段"`
  278. UniqueCode string `description:"唯一code"`
  279. ModifyTime time.Time `description:"修改时间"`
  280. CreateTime time.Time `description:"创建时间"`
  281. ClassifyNameEn string `description:"英文分类名称"`
  282. Children []*BaseFromHisugarClassifyItem `description:"子分类" gorm:"-"`
  283. }
  284. // 获取所有分类
  285. func GetHisugarClassifyAll() (items []*BaseFromHisugarClassifyItem, err error) {
  286. o := global.DbMap[utils.DbNameIndex]
  287. sql := ` SELECT * FROM base_from_hisugar_classify ORDER BY sort ASC, base_from_hisugar_classify_id ASC`
  288. err = o.Raw(sql).Find(&items).Error
  289. return
  290. }
  291. func GetHisugarIndex(condition string, pars interface{}) (items []*BaseFromHisugarIndexList, err error) {
  292. o := global.DbMap[utils.DbNameIndex]
  293. sql := ` SELECT * FROM base_from_hisugar_index WHERE 1=1 `
  294. if condition != "" {
  295. sql += condition
  296. }
  297. sql += ` ORDER BY sort ASC, base_from_hisugar_index_id asc`
  298. err = o.Raw(sql, pars).Find(&items).Error
  299. return
  300. }
  301. type HisugarIndexDataCountGroup struct {
  302. IndexCode string
  303. Count int
  304. }
  305. func GetHisugarIndexDataCountGroup(indexCodes []string) (items []*HisugarIndexDataCountGroup, err error) {
  306. if len(indexCodes) <= 0 {
  307. return
  308. }
  309. o := global.DbMap[utils.DbNameIndex]
  310. 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`
  311. err = o.Raw(sql, indexCodes).Find(&items).Error
  312. return
  313. }
  314. func GetHisugarIndexDataV2(indexCode string, startSize, pageSize int) (items []*BaseFromHisugarData, err error) {
  315. o := global.DbMap[utils.DbNameIndex]
  316. sql := ` SELECT * FROM base_from_hisugar_data WHERE index_code=? ORDER BY data_time DESC LIMIT ?,? `
  317. err = o.Raw(sql, indexCode, startSize, pageSize).Find(&items).Error
  318. return
  319. }
  320. // GetHisugarIndexInfoCount 查询指标信息总数
  321. func GetHisugarIndexInfoCount(condition string, pars []interface{}) (count int, err error) {
  322. o := global.DbMap[utils.DbNameIndex]
  323. sql := ` SELECT COUNT(1) AS count FROM base_from_hisugar_index WHERE 1=1 `
  324. if condition != "" {
  325. sql += condition
  326. }
  327. err = o.Raw(sql, pars...).Scan(&count).Error
  328. return
  329. }
  330. type BaseFromHisugarIndexPage struct {
  331. List []*BaseFromHisugarIndex `description:"指标列表"`
  332. Paging *paging.PagingItem `description:"分页数据"`
  333. }
  334. // GetHisugarIndexInfoPage 分页查询指标信息
  335. func GetHisugarIndexInfoPage(condition string, pars []interface{}, size int, pageSize int) (items []*BaseFromHisugarIndex, err error) {
  336. o := global.DbMap[utils.DbNameIndex]
  337. sql := ` SELECT * FROM base_from_hisugar_index WHERE 1=1 `
  338. if condition != "" {
  339. sql += condition
  340. }
  341. sql += ` ORDER BY base_from_hisugar_index_id asc LIMIT ?,?`
  342. err = o.Raw(sql, pars, size, pageSize).Find(&items).Error
  343. return
  344. }
  345. // 获取所有分类
  346. func GetHisugarClassifyById(classifyId int) (ClassifyIds string, err error) {
  347. o := global.DbMap[utils.DbNameIndex]
  348. 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=?`
  349. err = o.Raw(sql, classifyId, classifyId).Scan(&ClassifyIds).Error
  350. return
  351. }