base_from_hisugar.go 16 KB

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