base_from_hisugar.go 17 KB

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