base_from_hisugar.go 18 KB

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