base_from_kpler_index.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. package data_manage
  2. import (
  3. "eta/eta_api/global"
  4. "eta/eta_api/utils"
  5. "time"
  6. "gorm.io/gorm"
  7. "github.com/rdlucklib/rdluck_tools/paging"
  8. )
  9. type BaseFromKplerIndex struct {
  10. BaseFromKplerIndexId int `orm:"column(base_from_kpler_index_id);pk" gorm:"primaryKey"`
  11. ClassifyId int
  12. IndexCode string
  13. IndexName string
  14. Frequency string
  15. Unit string
  16. Sort int
  17. StartDate string `description:"开始日期"`
  18. EndDate string `description:"结束日期"`
  19. EndValue float64
  20. CreateTime time.Time
  21. ModifyTime time.Time
  22. BaseFileName string `description:"文件目录"`
  23. TerminalCode string `description:"所属终端编码"`
  24. ApiQueryUrl string `description:"API查询URL"`
  25. ProductNames string `description:"产品名称"`
  26. FromZoneId int `description:"区域ID"`
  27. FromZoneName string `description:"区域名称"`
  28. ToZoneId int `description:"区域ID"`
  29. ToZoneName string `description:"区域名称"`
  30. FlowDirection string `description:"流向"`
  31. Granularity string `description:"粒度"`
  32. Split string `description:"拆分类型"`
  33. }
  34. type BaseFromKplerIndexList struct {
  35. BaseFromKplerIndexId int `orm:"column(base_from_kpler_index_id);pk" gorm:"primaryKey"`
  36. ClassifyId int
  37. Interface string
  38. EdbInfoId int
  39. EdbUniqueCode string `description:"指标库唯一编码"`
  40. EdbClassifyId int `description:"指标库分类ID"`
  41. StartDate string
  42. EndDate string
  43. EndValue float64
  44. IndexCode string
  45. IndexName string
  46. Frequency string
  47. Unit string
  48. Sort int
  49. CreateTime string
  50. ModifyTime string
  51. BaseFileName string `description:"文件目录"`
  52. TerminalCode string `description:"所属终端编码"`
  53. ApiQueryUrl string `description:"API查询URL"`
  54. ProductNames string `description:"产品名称"`
  55. FromZoneId int `description:"区域ID"`
  56. FromZoneName string `description:"区域名称"`
  57. ToZoneId int `description:"区域ID"`
  58. ToZoneName string `description:"区域名称"`
  59. FlowDirection string `description:"流向"`
  60. Granularity string `description:"粒度"`
  61. Split string `description:"拆分类型"`
  62. EdbExist int `description:"指标库是否已添加:0-否;1-是"`
  63. DataList []*BaseFromKplerData `gorm:"-"`
  64. Paging *paging.PagingItem `description:"分页数据" gorm:"-"`
  65. }
  66. func (obj *BaseFromKplerIndexList) AfterFind(db *gorm.DB) (err error) {
  67. obj.CreateTime = utils.GormDateStrToDateTimeStr(obj.CreateTime)
  68. obj.ModifyTime = utils.GormDateStrToDateTimeStr(obj.ModifyTime)
  69. obj.StartDate = utils.GormDateStrToDateStr(obj.StartDate)
  70. obj.EndDate = utils.GormDateStrToDateStr(obj.EndDate)
  71. return
  72. }
  73. type BaseFromKplerIndexSearchList struct {
  74. List []*BaseFromKplerIndexList
  75. Paging *paging.PagingItem `description:"分页数据"`
  76. }
  77. type KplerSingleDataResp struct {
  78. BaseFromKplerIndexId int
  79. ClassifyId int
  80. EdbInfoId int
  81. IndexCode string
  82. IndexName string
  83. Frequency string
  84. Unit string
  85. StartTime string
  86. CreateTime string
  87. ModifyTime string
  88. EdbExist int `description:"指标库是否已添加:0-否;1-是"`
  89. Data []*KplerSingleData
  90. Pageing *paging.PagingItem `description:"分页数据"`
  91. }
  92. type KplerSingleData struct {
  93. Value string `orm:"column(value)" description:"日期"`
  94. DataTime string `orm:"column(data_time)" description:"值"`
  95. }
  96. func GetKplerIndexByClassifyId(classifyId int) (items []*BaseFromKplerIndex, err error) {
  97. o := global.DbMap[utils.DbNameIndex]
  98. sql := ` SELECT base_from_kpler_index_id, classify_id, index_code, index_name FROM base_from_kpler_index WHERE classify_id=? ORDER BY sort ASC, base_from_kpler_index_id ASC `
  99. err = o.Raw(sql, classifyId).Find(&items).Error
  100. return
  101. }
  102. func GetKplerIndex(condition string, pars []interface{}) (items []*BaseFromKplerIndexList, err error) {
  103. o := global.DbMap[utils.DbNameIndex]
  104. sql := ` SELECT * FROM base_from_kpler_index WHERE 1=1 `
  105. if condition != "" {
  106. sql += condition
  107. }
  108. sql += ` ORDER BY sort ASC, base_from_kpler_index_id asc`
  109. err = o.Raw(sql, pars...).Find(&items).Error
  110. return
  111. }
  112. func GetKplerIndexPage(condition string, pars []interface{}, startSize, pageSize int) (items []*BaseFromKplerIndexList, err error) {
  113. o := global.DbMap[utils.DbNameIndex]
  114. sql := ` SELECT * FROM base_from_kpler_index WHERE 1=1 `
  115. if condition != "" {
  116. sql += condition
  117. }
  118. sql += ` ORDER BY sort ASC, base_from_kpler_index_id asc LIMIT ?,?`
  119. pars = append(pars, startSize, pageSize)
  120. err = o.Raw(sql, pars...).Find(&items).Error
  121. return
  122. }
  123. func GetKplerIndexPageCount(condition string, pars []interface{}) (count int, err error) {
  124. o := global.DbMap[utils.DbNameIndex]
  125. sql := ` SELECT COUNT(1) AS count FROM base_from_kpler_index WHERE 1=1 `
  126. if condition != "" {
  127. sql += condition
  128. }
  129. err = o.Raw(sql, pars...).Scan(&count).Error
  130. return
  131. }
  132. func GetKplerIndexDataCount(indexCode string) (count int, err error) {
  133. o := global.DbMap[utils.DbNameIndex]
  134. sql := ` SELECT COUNT(1) AS count FROM base_from_kpler_data WHERE index_code=? `
  135. err = o.Raw(sql, indexCode).Scan(&count).Error
  136. return
  137. }
  138. func GetKplerIndexDataByDataTime(indexCodes []string, startDate, endDate string) (items []*BaseFromKplerData, err error) {
  139. o := global.DbMap[utils.DbNameIndex]
  140. sql := ` SELECT * FROM base_from_kpler_data WHERE index_code in (` + utils.GetOrmInReplace(len(indexCodes)) + `) and data_time >=? and data_time <? ORDER BY data_time DESC `
  141. err = o.Raw(sql, indexCodes, startDate, endDate).Find(&items).Error
  142. return
  143. }
  144. func GetKplerIndexDataTimePageByCodes(indexCodes []string, startSize, pageSize int) (dataTimes []string, err error) {
  145. o := global.DbMap[utils.DbNameIndex]
  146. sql := ` SELECT data_time FROM base_from_kpler_data WHERE index_code in (` + utils.GetOrmInReplace(len(indexCodes)) + `) GROUP BY data_time ORDER BY data_time DESC LIMIT ?,? `
  147. err = o.Raw(sql, indexCodes, startSize, pageSize).Scan(&dataTimes).Error
  148. for i := range dataTimes {
  149. dataTimes[i] = utils.GormDateStrToDateStr(dataTimes[i])
  150. }
  151. return
  152. }
  153. func GetKplerIndexDataTimePageCount(indexCodes []string) (count int, err error) {
  154. o := global.DbMap[utils.DbNameIndex]
  155. sql := ` SELECT COUNT(DISTINCT data_time) AS count FROM base_from_kpler_data WHERE index_code in (` + utils.GetOrmInReplace(len(indexCodes)) + `) `
  156. err = o.Raw(sql, indexCodes).Scan(&count).Error
  157. return
  158. }
  159. func GetKplerIndexDataByCodes(indexCode []string) (items []*BaseFromKplerData, err error) {
  160. if len(indexCode) == 0 {
  161. return
  162. }
  163. o := global.DbMap[utils.DbNameIndex]
  164. sql := ` SELECT * FROM base_from_kpler_data WHERE index_code in (` + utils.GetOrmInReplace(len(indexCode)) + `) ORDER BY data_time DESC `
  165. err = o.Raw(sql, indexCode).Find(&items).Error
  166. return
  167. }
  168. // GetKplerByConditionAndFrequency 根据条件获取Kpler指标列表
  169. func GetKplerByConditionAndFrequency(condition, frequency string, pars []interface{}) (items []*BaseFromKplerIndex, err error) {
  170. o := global.DbMap[utils.DbNameIndex]
  171. sql := ` SELECT * FROM base_from_kpler_index WHERE 1=1 `
  172. if condition != "" {
  173. sql += condition
  174. }
  175. sql += ` AND frequency=?`
  176. sql += ` ORDER BY sort ASC, base_from_kpler_index_id ASC`
  177. err = o.Raw(sql, pars, frequency).Find(&items).Error
  178. return
  179. }
  180. func GetKplerFrequencyByCondition(condition string, pars []interface{}) (items []string, err error) {
  181. sql := `SELECT DISTINCT frequency FROM base_from_kpler_index WHERE 1=1 `
  182. if condition != "" {
  183. sql += condition
  184. }
  185. sql += ` ORDER BY FIELD(frequency,'日度','周度','旬度','月度','季度','半年度','年度') `
  186. o := global.DbMap[utils.DbNameIndex]
  187. err = o.Raw(sql, pars...).Find(&items).Error
  188. return
  189. }
  190. // GetKplerDataDataTimeByIndexId 根据指标id获取指标数据的日期列表
  191. func GetKplerDataDataTimeByIndexId(indexIdList []int) (items []string, err error) {
  192. if len(indexIdList) == 0 {
  193. return
  194. }
  195. o := global.DbMap[utils.DbNameIndex]
  196. sql := ` SELECT DISTINCT data_time FROM base_from_kpler_data WHERE base_from_kpler_index_id IN (` + utils.GetOrmInReplace(len(indexIdList)) + `) ORDER BY data_time DESC`
  197. err = o.Raw(sql, indexIdList).Find(&items).Error
  198. if err != nil {
  199. return
  200. }
  201. for i := range items {
  202. items[i] = utils.GormDateStrToDateStr(items[i])
  203. }
  204. return
  205. }
  206. type BaseFromKplerData struct {
  207. BaseFromKplerDataId int `orm:"column(base_from_kpler_data_id);pk" gorm:"primaryKey"`
  208. BaseFromKplerIndexId int
  209. IndexCode string
  210. DataTime string
  211. Value float64
  212. CreateTime string
  213. ModifyTime string
  214. DataTimestamp int64
  215. }
  216. func (obj *BaseFromKplerData) AfterFind(tx *gorm.DB) (err error) {
  217. obj.CreateTime = utils.GormDateStrToDateTimeStr(obj.CreateTime)
  218. obj.ModifyTime = utils.GormDateStrToDateTimeStr(obj.ModifyTime)
  219. obj.DataTime = utils.GormDateStrToDateStr(obj.DataTime)
  220. return
  221. }
  222. type BaseFromKplerIndexSearchItem struct {
  223. BaseFromKplerIndexId int `orm:"column(base_from_kpler_index_id);pk" gorm:"primaryKey"`
  224. ClassifyId int
  225. ParentClassifyId int
  226. IndexCode string
  227. IndexName string
  228. }
  229. // BatchCheckKplerEdbReq 指标数据结构体
  230. type BatchCheckKplerEdbReq struct {
  231. Frequencies string `description:"频度;枚举值:日度、周度、月度、季度、半年度、年度"`
  232. Keyword string `description:"关键字"`
  233. ClassifyIds string `description:"所选品种id列表"`
  234. ListAll bool `form:"ListAll" json:"ListAll" description:"列表全选"`
  235. TradeCodeList string `form:"TradeCodeList" json:"TradeCodeList" description:"全选为false时, 该数组为选中; 全选为true时, 该数组为不选的指标"`
  236. }
  237. // GetKplerItemList 模糊查询Kpler数据库指标列表
  238. func GetKplerItemList(condition string) (items []*BaseFromKplerIndexSearchItem, err error) {
  239. o := global.DbMap[utils.DbNameIndex]
  240. sql := "SELECT * FROM base_from_kpler_index WHERE 1=1"
  241. if condition != "" {
  242. sql += condition
  243. }
  244. err = o.Raw(sql).Find(&items).Error
  245. return
  246. }
  247. func GetKplerIndexDataByCode(indexCode string) (list []*BaseFromKplerData, err error) {
  248. o := global.DbMap[utils.DbNameIndex]
  249. sql := `SELECT * FROM base_from_kpler_data WHERE index_code=? `
  250. err = o.Raw(sql, indexCode).Find(&list).Error
  251. return
  252. }
  253. func GetKplerIndexDataByCodeWithPage(indexCode string, startSize, pageSize int) (list []*BaseFromKplerData, err error) {
  254. o := global.DbMap[utils.DbNameIndex]
  255. sql := `SELECT * FROM base_from_kpler_data WHERE index_code=? ORDER BY data_time DESC LIMIT ?,?`
  256. err = o.Raw(sql, indexCode, startSize, pageSize).Find(&list).Error
  257. return
  258. }
  259. func GetKplerIndexDataByCodeCount(indexCode string) (count int, err error) {
  260. o := global.DbMap[utils.DbNameIndex]
  261. sql := `SELECT COUNT(1) FROM base_from_kpler_data WHERE index_code=?`
  262. err = o.Raw(sql, indexCode).Scan(&count).Error
  263. return
  264. }
  265. func GetBaseFromKplerIndexByIndexCode(indexCode string) (list *BaseFromKplerIndex, err error) {
  266. o := global.DbMap[utils.DbNameIndex]
  267. sql := ` SELECT * FROM base_from_kpler_index WHERE index_code=? `
  268. err = o.Raw(sql, indexCode).First(&list).Error
  269. return
  270. }
  271. type BaseFromKplerIndexType struct {
  272. Type2 string `gorm:"column:type_2"`
  273. Type3 string `gorm:"column:type_3"`
  274. }
  275. // Update 更新Kpler指标基础信息
  276. func (item *BaseFromKplerIndex) Update(cols []string) (err error) {
  277. o := global.DbMap[utils.DbNameIndex]
  278. err = o.Select(cols).Updates(item).Error
  279. return
  280. }
  281. // EditKplerIndexInfoResp 新增指标的返回
  282. type EditKplerIndexInfoResp struct {
  283. BaseFromKplerIndexId int `description:"指标ID"`
  284. IndexCode string `description:"指标code"`
  285. }
  286. type KplerIndexSource2EdbReq struct {
  287. EdbCode string
  288. EdbName string
  289. Frequency string
  290. Unit string
  291. ClassifyId int
  292. AdminId int
  293. AdminRealName string
  294. }
  295. func GetKplerFrequencyByClassifyId(classifyId int) (items []*GlFrequency, err error) {
  296. o := global.DbMap[utils.DbNameIndex]
  297. sql := ` SELECT frequency FROM base_from_kpler_index WHERE classify_id = ? `
  298. sql += ` GROUP BY frequency ORDER BY frequency ASC `
  299. err = o.Raw(sql, classifyId).Find(&items).Error
  300. return
  301. }
  302. // GetKplerZoneById 根据区域ID获取区域信息
  303. func GetKplerZoneById(zoneId int) (item *BaseFromKplerZone, err error) {
  304. o := global.DbMap[utils.DbNameIndex]
  305. sql := `SELECT * FROM base_from_kpler_zone WHERE base_from_kpler_zone_id=? `
  306. err = o.Raw(sql, zoneId).First(&item).Error
  307. return
  308. }
  309. // GetKplerZoneByIds 根据区域ID列表获取区域信息
  310. func GetKplerZoneByIds(zoneIds []int) (items []*BaseFromKplerZone, err error) {
  311. if len(zoneIds) == 0 {
  312. return
  313. }
  314. o := global.DbMap[utils.DbNameIndex]
  315. sql := `SELECT * FROM base_from_kpler_zone WHERE base_from_kpler_zone_id IN (` + utils.GetOrmInReplace(len(zoneIds)) + `) `
  316. err = o.Raw(sql, zoneIds).Find(&items).Error
  317. return
  318. }
  319. // BaseFromKplerZone Kpler区域表
  320. type BaseFromKplerZone struct {
  321. BaseFromKplerZoneId int `orm:"column(base_from_kpler_zone_id);pk" gorm:"primaryKey"`
  322. ZoneName string `description:"区域名称"`
  323. ZoneType string `description:"区域类型"`
  324. ParentId int `description:"区域父ID"`
  325. CreateTime time.Time `description:"创建时间"`
  326. ModifyTime time.Time `description:"修改时间"`
  327. }
  328. func GetBaseFromKplerZoneByKeyword(keyword string) (items []*BaseFromKplerZone, err error) {
  329. o := global.DbMap[utils.DbNameIndex]
  330. sql := `SELECT * FROM base_from_kpler_zone WHERE zone_name LIKE ? `
  331. err = o.Raw(sql, "%"+keyword+"%").Find(&items).Error
  332. return
  333. }
  334. // GetBaseFromKplerZoneByKeywordWithRelevance searches for zones with similarity-based sorting
  335. // It prioritizes exact matches, then prefix matches, then contains matches
  336. func GetBaseFromKplerZoneByKeywordWithRelevance(keyword string) (items []*BaseFromKplerZone, err error) {
  337. o := global.DbMap[utils.DbNameIndex]
  338. sql := `
  339. SELECT * FROM base_from_kpler_zone
  340. WHERE zone_name LIKE ?
  341. ORDER BY
  342. CASE
  343. WHEN zone_name = ? THEN 1
  344. WHEN zone_name LIKE ? THEN 2
  345. ELSE 3
  346. END,
  347. zone_name ASC
  348. `
  349. err = o.Raw(sql, "%"+keyword+"%", keyword, keyword+"%").Find(&items).Error
  350. return
  351. }
  352. type KplerSearchReq struct {
  353. ProductIds []int `description:"产品ID"`
  354. FromZoneIds []int `description:"来源区域ID,对应Location" `
  355. ToZoneIds []int `description:"流向区域ID"`
  356. Split string `description:"拆分类型"`
  357. FlowDirection string `description:"流向,对应periodicity:export/import"`
  358. Granularity string `description:"粒度: daily/weekly/monthly/yearly"`
  359. Unit string `description:"单位"`
  360. }
  361. // 获取区域
  362. func (y *BaseFromKplerZone) GetByZoneIds(zoneIds []int) (items []*BaseFromKplerZone, err error) {
  363. sql := ` SELECT * FROM base_from_kpler_zone WHERE base_from_kpler_zone_id in (?) `
  364. err = global.DbMap[utils.DbNameIndex].Raw(sql, zoneIds).Find(&items).Error
  365. return
  366. }
  367. // 获取分类
  368. func (y *BaseFromKplerClassify) GetByProductIds(productIds []int) (items []*BaseFromKplerClassify, err error) {
  369. sql := ` SELECT * FROM base_from_kpler_classify WHERE product_id in (?) `
  370. err = global.DbMap[utils.DbNameIndex].Raw(sql, productIds).Find(&items).Error
  371. return
  372. }
  373. func GetKplerIndexByIndexCode(indexCode string) (item *BaseFromKplerIndex, err error) {
  374. o := global.DbMap[utils.DbNameIndex]
  375. sql := ` SELECT * FROM base_from_kpler_index WHERE index_code=? `
  376. err = o.Raw(sql, indexCode).First(&item).Error
  377. return
  378. }
  379. type KplerSearchEdbLibResp struct {
  380. Ret int
  381. Msg string
  382. ErrMsg string
  383. ErrCode string
  384. Data []*KplerIndexItem
  385. Success bool `description:"true 执行成功,false 执行失败"`
  386. }
  387. type KplerIndexItem struct {
  388. IndexCode string
  389. IndexName string
  390. Unit string
  391. Frequency string
  392. ClassifyName string
  393. IndexData []KplerIndexDataItem
  394. }
  395. type KplerIndexDataItem struct {
  396. DataTime string
  397. Value string
  398. }