base_from_hisugar.go 17 KB

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