base_from_kpler_index.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. }
  91. type KplerSingleData struct {
  92. Value string `orm:"column(value)" description:"日期"`
  93. DataTime string `orm:"column(data_time)" description:"值"`
  94. }
  95. func GetKplerIndexByClassifyId(classifyId int) (items []*BaseFromKplerIndex, err error) {
  96. o := global.DbMap[utils.DbNameIndex]
  97. 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 `
  98. err = o.Raw(sql, classifyId).Find(&items).Error
  99. return
  100. }
  101. func GetKplerIndex(condition string, pars []interface{}) (items []*BaseFromKplerIndexList, err error) {
  102. o := global.DbMap[utils.DbNameIndex]
  103. sql := ` SELECT * FROM base_from_kpler_index WHERE 1=1 `
  104. if condition != "" {
  105. sql += condition
  106. }
  107. sql += ` ORDER BY sort ASC, base_from_kpler_index_id asc`
  108. err = o.Raw(sql, pars...).Find(&items).Error
  109. return
  110. }
  111. func GetKplerIndexPage(condition string, pars []interface{}, startSize, pageSize int) (items []*BaseFromKplerIndexList, err error) {
  112. o := global.DbMap[utils.DbNameIndex]
  113. sql := ` SELECT * FROM base_from_kpler_index WHERE 1=1 `
  114. if condition != "" {
  115. sql += condition
  116. }
  117. sql += ` ORDER BY sort ASC, base_from_kpler_index_id asc LIMIT ?,?`
  118. pars = append(pars, startSize, pageSize)
  119. err = o.Raw(sql, pars...).Find(&items).Error
  120. return
  121. }
  122. func GetKplerIndexPageCount(condition string, pars []interface{}) (count int, err error) {
  123. o := global.DbMap[utils.DbNameIndex]
  124. sql := ` SELECT COUNT(1) AS count FROM base_from_kpler_index WHERE 1=1 `
  125. if condition != "" {
  126. sql += condition
  127. }
  128. err = o.Raw(sql, pars...).Scan(&count).Error
  129. return
  130. }
  131. func GetKplerIndexDataCount(indexCode string) (count int, err error) {
  132. o := global.DbMap[utils.DbNameIndex]
  133. sql := ` SELECT COUNT(1) AS count FROM base_from_kpler_data WHERE index_code=? `
  134. err = o.Raw(sql, indexCode).Scan(&count).Error
  135. return
  136. }
  137. func GetKplerIndexDataByDataTime(indexCodes []string, startDate, endDate string) (items []*BaseFromKplerData, err error) {
  138. o := global.DbMap[utils.DbNameIndex]
  139. 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 `
  140. err = o.Raw(sql, indexCodes, startDate, endDate).Find(&items).Error
  141. return
  142. }
  143. func GetKplerIndexDataTimePageByCodes(indexCodes []string, startSize, pageSize int) (dataTimes []string, err error) {
  144. o := global.DbMap[utils.DbNameIndex]
  145. 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 ?,? `
  146. err = o.Raw(sql, indexCodes, startSize, pageSize).Scan(&dataTimes).Error
  147. for i := range dataTimes {
  148. dataTimes[i] = utils.GormDateStrToDateStr(dataTimes[i])
  149. }
  150. return
  151. }
  152. func GetKplerIndexDataTimePageCount(indexCodes []string) (count int, err error) {
  153. o := global.DbMap[utils.DbNameIndex]
  154. sql := ` SELECT COUNT(DISTINCT data_time) AS count FROM base_from_kpler_data WHERE index_code in (` + utils.GetOrmInReplace(len(indexCodes)) + `) `
  155. err = o.Raw(sql, indexCodes).Scan(&count).Error
  156. return
  157. }
  158. func GetKplerIndexDataByCodes(indexCode []string) (items []*BaseFromKplerData, err error) {
  159. if len(indexCode) == 0 {
  160. return
  161. }
  162. o := global.DbMap[utils.DbNameIndex]
  163. sql := ` SELECT * FROM base_from_kpler_data WHERE index_code in (` + utils.GetOrmInReplace(len(indexCode)) + `) ORDER BY data_time DESC `
  164. err = o.Raw(sql, indexCode).Find(&items).Error
  165. return
  166. }
  167. // GetKplerByConditionAndFrequency 根据条件获取Kpler指标列表
  168. func GetKplerByConditionAndFrequency(condition, frequency string, pars []interface{}) (items []*BaseFromKplerIndex, err error) {
  169. o := global.DbMap[utils.DbNameIndex]
  170. sql := ` SELECT * FROM base_from_kpler_index WHERE 1=1 `
  171. if condition != "" {
  172. sql += condition
  173. }
  174. sql += ` AND frequency=?`
  175. sql += ` ORDER BY sort ASC, base_from_kpler_index_id ASC`
  176. err = o.Raw(sql, pars, frequency).Find(&items).Error
  177. return
  178. }
  179. func GetKplerFrequencyByCondition(condition string, pars []interface{}) (items []string, err error) {
  180. sql := `SELECT DISTINCT frequency FROM base_from_kpler_index WHERE 1=1 `
  181. if condition != "" {
  182. sql += condition
  183. }
  184. sql += ` ORDER BY FIELD(frequency,'日度','周度','旬度','月度','季度','半年度','年度') `
  185. o := global.DbMap[utils.DbNameIndex]
  186. err = o.Raw(sql, pars...).Find(&items).Error
  187. return
  188. }
  189. // GetKplerDataDataTimeByIndexId 根据指标id获取指标数据的日期列表
  190. func GetKplerDataDataTimeByIndexId(indexIdList []int) (items []string, err error) {
  191. if len(indexIdList) == 0 {
  192. return
  193. }
  194. o := global.DbMap[utils.DbNameIndex]
  195. 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`
  196. err = o.Raw(sql, indexIdList).Find(&items).Error
  197. if err != nil {
  198. return
  199. }
  200. for i := range items {
  201. items[i] = utils.GormDateStrToDateStr(items[i])
  202. }
  203. return
  204. }
  205. type BaseFromKplerData struct {
  206. BaseFromKplerDataId int `orm:"column(base_from_kpler_data_id);pk" gorm:"primaryKey"`
  207. BaseFromKplerIndexId int
  208. IndexCode string
  209. DataTime string
  210. Value float64
  211. CreateTime string
  212. ModifyTime string
  213. DataTimestamp int64
  214. }
  215. func (obj *BaseFromKplerData) AfterFind(tx *gorm.DB) (err error) {
  216. obj.CreateTime = utils.GormDateStrToDateTimeStr(obj.CreateTime)
  217. obj.ModifyTime = utils.GormDateStrToDateTimeStr(obj.ModifyTime)
  218. obj.DataTime = utils.GormDateStrToDateStr(obj.DataTime)
  219. return
  220. }
  221. type BaseFromKplerIndexSearchItem struct {
  222. BaseFromKplerIndexId int `orm:"column(base_from_kpler_index_id);pk" gorm:"primaryKey"`
  223. ClassifyId int
  224. ParentClassifyId int
  225. IndexCode string
  226. IndexName string
  227. }
  228. // BatchCheckKplerEdbReq 指标数据结构体
  229. type BatchCheckKplerEdbReq struct {
  230. Frequencies string `description:"频度;枚举值:日度、周度、月度、季度、半年度、年度"`
  231. Keyword string `description:"关键字"`
  232. ClassifyIds string `description:"所选品种id列表"`
  233. ListAll bool `form:"ListAll" json:"ListAll" description:"列表全选"`
  234. TradeCodeList string `form:"TradeCodeList" json:"TradeCodeList" description:"全选为false时, 该数组为选中; 全选为true时, 该数组为不选的指标"`
  235. }
  236. // GetKplerItemList 模糊查询Kpler数据库指标列表
  237. func GetKplerItemList(condition string) (items []*BaseFromKplerIndexSearchItem, err error) {
  238. o := global.DbMap[utils.DbNameIndex]
  239. sql := "SELECT * FROM base_from_kpler_index WHERE 1=1"
  240. if condition != "" {
  241. sql += condition
  242. }
  243. err = o.Raw(sql).Find(&items).Error
  244. return
  245. }
  246. func GetKplerIndexDataByCode(indexCode string) (list []*BaseFromKplerData, err error) {
  247. o := global.DbMap[utils.DbNameIndex]
  248. sql := `SELECT * FROM base_from_kpler_data WHERE index_code=? `
  249. err = o.Raw(sql, indexCode).Find(&list).Error
  250. return
  251. }
  252. func GetBaseFromKplerIndexByIndexCode(indexCode string) (list *BaseFromKplerIndex, err error) {
  253. o := global.DbMap[utils.DbNameIndex]
  254. sql := ` SELECT * FROM base_from_kpler_index WHERE index_code=? `
  255. err = o.Raw(sql, indexCode).First(&list).Error
  256. return
  257. }
  258. type BaseFromKplerIndexType struct {
  259. Type2 string `gorm:"column:type_2"`
  260. Type3 string `gorm:"column:type_3"`
  261. }
  262. // Update 更新Kpler指标基础信息
  263. func (item *BaseFromKplerIndex) Update(cols []string) (err error) {
  264. o := global.DbMap[utils.DbNameIndex]
  265. err = o.Select(cols).Updates(item).Error
  266. return
  267. }
  268. // EditKplerIndexInfoResp 新增指标的返回
  269. type EditKplerIndexInfoResp struct {
  270. BaseFromKplerIndexId int `description:"指标ID"`
  271. IndexCode string `description:"指标code"`
  272. }
  273. type KplerIndexSource2EdbReq struct {
  274. EdbCode string
  275. EdbName string
  276. Frequency string
  277. Unit string
  278. ClassifyId int
  279. AdminId int
  280. AdminRealName string
  281. }
  282. func GetKplerFrequencyByClassifyId(classifyId int) (items []*GlFrequency, err error) {
  283. o := global.DbMap[utils.DbNameIndex]
  284. sql := ` SELECT frequency FROM base_from_kpler_index WHERE classify_id = ? `
  285. sql += ` GROUP BY frequency ORDER BY frequency ASC `
  286. err = o.Raw(sql, classifyId).Find(&items).Error
  287. return
  288. }
  289. // GetKplerZoneById 根据区域ID获取区域信息
  290. func GetKplerZoneById(zoneId int) (item *BaseFromKplerZone, err error) {
  291. o := global.DbMap[utils.DbNameIndex]
  292. sql := `SELECT * FROM base_from_kpler_zone WHERE base_from_kpler_zone_id=? `
  293. err = o.Raw(sql, zoneId).First(&item).Error
  294. return
  295. }
  296. // GetKplerZoneByIds 根据区域ID列表获取区域信息
  297. func GetKplerZoneByIds(zoneIds []int) (items []*BaseFromKplerZone, err error) {
  298. if len(zoneIds) == 0 {
  299. return
  300. }
  301. o := global.DbMap[utils.DbNameIndex]
  302. sql := `SELECT * FROM base_from_kpler_zone WHERE base_from_kpler_zone_id IN (` + utils.GetOrmInReplace(len(zoneIds)) + `) `
  303. err = o.Raw(sql, zoneIds).Find(&items).Error
  304. return
  305. }
  306. // BaseFromKplerZone Kpler区域表
  307. type BaseFromKplerZone struct {
  308. BaseFromKplerZoneId int `orm:"column(base_from_kpler_zone_id);pk" gorm:"primaryKey"`
  309. ZoneName string `description:"区域名称"`
  310. ZoneType string `description:"区域类型"`
  311. ParentId int `description:"区域父ID"`
  312. CreateTime time.Time `description:"创建时间"`
  313. ModifyTime time.Time `description:"修改时间"`
  314. }
  315. func GetBaseFromKplerZoneByKeyword(keyword string) (items []*BaseFromKplerZone, err error) {
  316. o := global.DbMap[utils.DbNameIndex]
  317. sql := `SELECT * FROM base_from_kpler_zone WHERE zone_name LIKE ? `
  318. err = o.Raw(sql, "%"+keyword+"%").Find(&items).Error
  319. return
  320. }
  321. // GetBaseFromKplerZoneByKeywordWithRelevance searches for zones with similarity-based sorting
  322. // It prioritizes exact matches, then prefix matches, then contains matches
  323. func GetBaseFromKplerZoneByKeywordWithRelevance(keyword string) (items []*BaseFromKplerZone, err error) {
  324. o := global.DbMap[utils.DbNameIndex]
  325. sql := `
  326. SELECT * FROM base_from_kpler_zone
  327. WHERE zone_name LIKE ?
  328. ORDER BY
  329. CASE
  330. WHEN zone_name = ? THEN 1
  331. WHEN zone_name LIKE ? THEN 2
  332. ELSE 3
  333. END,
  334. zone_name ASC
  335. `
  336. err = o.Raw(sql, "%"+keyword+"%", keyword, keyword+"%").Find(&items).Error
  337. return
  338. }
  339. type KplerSearchReq struct {
  340. ProductIds []int `description:"产品ID"`
  341. FromZoneIds []int `description:"来源区域ID,对应Location" `
  342. ToZoneIds []int `description:"流向区域ID"`
  343. Split string `description:"拆分类型"`
  344. FlowDirection string `description:"流向,对应periodicity:export/import"`
  345. Granularity string `description:"粒度: daily/weekly/monthly/yearly"`
  346. Unit string `description:"单位"`
  347. }
  348. // 获取区域
  349. func (y *BaseFromKplerZone) GetByZoneIds(zoneIds []int) (items []*BaseFromKplerZone, err error) {
  350. sql := ` SELECT * FROM base_from_kpler_zone WHERE base_from_kpler_zone_id in (?) `
  351. err = global.DbMap[utils.DbNameIndex].Raw(sql, zoneIds).Find(&items).Error
  352. return
  353. }
  354. // 获取分类
  355. func (y *BaseFromKplerClassify) GetByProductIds(productIds []int) (items []*BaseFromKplerClassify, err error) {
  356. sql := ` SELECT * FROM base_from_kpler_classify WHERE product_id in (?) `
  357. err = global.DbMap[utils.DbNameIndex].Raw(sql, productIds).Find(&items).Error
  358. return
  359. }
  360. func GetKplerIndexByIndexCode(indexCode string) (item *BaseFromKplerIndex, err error) {
  361. o := global.DbMap[utils.DbNameIndex]
  362. sql := ` SELECT * FROM base_from_kpler_index WHERE index_code=? `
  363. err = o.Raw(sql, indexCode).First(&item).Error
  364. return
  365. }
  366. type KplerSearchEdbLibResp struct {
  367. Ret int
  368. Msg string
  369. ErrMsg string
  370. ErrCode string
  371. Data []*KplerIndexItem
  372. Success bool `description:"true 执行成功,false 执行失败"`
  373. }
  374. type KplerIndexItem struct {
  375. IndexCode string
  376. IndexName string
  377. Unit string
  378. Frequency string
  379. ClassifyName string
  380. IndexData []KplerIndexDataItem
  381. }
  382. type KplerIndexDataItem struct {
  383. DataTime string
  384. Value string
  385. }