base_from_hisugar.go 18 KB

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