base_from_hisugar.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 `gorm:"-"`
  58. Paging *paging.PagingItem `description:"分页数据" gorm:"-"`
  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. if err != nil {
  275. return
  276. }
  277. if utils.NeedDateOrTimeFormat(utils.DbDriverName) {
  278. for i, v := range items {
  279. items[i] = utils.GormDateStrToDateStr(v)
  280. }
  281. }
  282. return
  283. }
  284. type BaseFromHisugarClassifyItem struct {
  285. BaseFromHisugarClassifyId int `orm:"column(base_from_hisugar_classify_id);pk" gorm:"primaryKey"`
  286. ClassifyName string `description:"分类名称"`
  287. ParentId int `description:"父级id"`
  288. Level int `description:"层级"`
  289. Sort int `description:"排序字段"`
  290. UniqueCode string `description:"唯一code"`
  291. ModifyTime time.Time `description:"修改时间"`
  292. CreateTime time.Time `description:"创建时间"`
  293. ClassifyNameEn string `description:"英文分类名称"`
  294. Children []*BaseFromHisugarClassifyItem `description:"子分类" gorm:"-"`
  295. }
  296. // 获取所有分类
  297. func GetHisugarClassifyAll() (items []*BaseFromHisugarClassifyItem, err error) {
  298. o := global.DbMap[utils.DbNameIndex]
  299. sql := ` SELECT * FROM base_from_hisugar_classify ORDER BY sort ASC, base_from_hisugar_classify_id ASC`
  300. err = o.Raw(sql).Find(&items).Error
  301. return
  302. }
  303. func GetHisugarIndex(condition string, pars interface{}) (items []*BaseFromHisugarIndexList, err error) {
  304. o := global.DbMap[utils.DbNameIndex]
  305. sql := ` SELECT * FROM base_from_hisugar_index WHERE 1=1 `
  306. if condition != "" {
  307. sql += condition
  308. }
  309. sql += ` ORDER BY sort ASC, base_from_hisugar_index_id asc`
  310. err = o.Raw(sql, pars).Find(&items).Error
  311. return
  312. }
  313. type HisugarIndexDataCountGroup struct {
  314. IndexCode string
  315. Count int
  316. }
  317. func GetHisugarIndexDataCountGroup(indexCodes []string) (items []*HisugarIndexDataCountGroup, err error) {
  318. if len(indexCodes) <= 0 {
  319. return
  320. }
  321. o := global.DbMap[utils.DbNameIndex]
  322. 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`
  323. err = o.Raw(sql, indexCodes).Find(&items).Error
  324. return
  325. }
  326. func GetHisugarIndexDataV2(indexCode string, startSize, pageSize int) (items []*BaseFromHisugarData, err error) {
  327. o := global.DbMap[utils.DbNameIndex]
  328. sql := ` SELECT * FROM base_from_hisugar_data WHERE index_code=? ORDER BY data_time DESC LIMIT ?,? `
  329. err = o.Raw(sql, indexCode, startSize, pageSize).Find(&items).Error
  330. return
  331. }
  332. // GetHisugarIndexInfoCount 查询指标信息总数
  333. func GetHisugarIndexInfoCount(condition string, pars []interface{}) (count int, err error) {
  334. o := global.DbMap[utils.DbNameIndex]
  335. sql := ` SELECT COUNT(1) AS count FROM base_from_hisugar_index WHERE 1=1 `
  336. if condition != "" {
  337. sql += condition
  338. }
  339. err = o.Raw(sql, pars...).Scan(&count).Error
  340. return
  341. }
  342. type BaseFromHisugarIndexPage struct {
  343. List []*BaseFromHisugarIndex `description:"指标列表"`
  344. Paging *paging.PagingItem `description:"分页数据"`
  345. }
  346. // GetHisugarIndexInfoPage 分页查询指标信息
  347. func GetHisugarIndexInfoPage(condition string, pars []interface{}, size int, pageSize int) (items []*BaseFromHisugarIndex, err error) {
  348. o := global.DbMap[utils.DbNameIndex]
  349. sql := ` SELECT * FROM base_from_hisugar_index WHERE 1=1 `
  350. if condition != "" {
  351. sql += condition
  352. }
  353. sql += ` ORDER BY base_from_hisugar_index_id asc LIMIT ?,?`
  354. pars = append(pars, size, pageSize)
  355. //err = o.Raw(sql, pars, size, pageSize).Find(&items).Error
  356. err = o.Raw(sql, pars...).Find(&items).Error
  357. return
  358. }
  359. // 获取所有分类
  360. func GetHisugarClassifyById(classifyId int) (ClassifyIds string, err error) {
  361. o := global.DbMap[utils.DbNameIndex]
  362. 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", ",", ""))
  363. //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=?`
  364. //sql := ` SELECT AS classify_ids FROM base_from_hisugar_classify WHERE base_from_hisugar_classify_id=? OR parent_id=?`
  365. err = o.Raw(sql, classifyId, classifyId).Scan(&ClassifyIds).Error
  366. return
  367. }