base_from_oilchem.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. package data_manage
  2. import (
  3. "eta/eta_api/global"
  4. "eta/eta_api/utils"
  5. "gorm.io/gorm"
  6. "github.com/rdlucklib/rdluck_tools/paging"
  7. )
  8. type BaseFromOilchemClassify struct {
  9. BaseFromOilchemClassifyId int // 分类ID
  10. ClassifyName string // 分类名称
  11. ParentID int // 上级ID
  12. Level int // 层级
  13. Sort int // 排序
  14. CreateTime string // 创建时间
  15. ModifyTime string // 修改时间
  16. }
  17. type BaseFromOilchemIndex struct {
  18. BaseFromOilchemIndexId int // 主键ID
  19. IndexCode string // 指标编码
  20. IndexName string // 指标名称
  21. ClassifyId uint // 分类ID
  22. Unit string // 单位
  23. Frequency string // 频度
  24. Describe string // 指标描述
  25. Sort int // 排序
  26. CreateTime string // 创建时间
  27. ModifyTime string // 修改时间
  28. }
  29. func (baseFromOilchemIndex *BaseFromOilchemIndex) AfterFind(db *gorm.DB) (err error) {
  30. if utils.NeedDateOrTimeFormat(utils.DbDriverName) {
  31. if baseFromOilchemIndex.ModifyTime != "" {
  32. baseFromOilchemIndex.ModifyTime = utils.GormDateStrToDateTimeStr(baseFromOilchemIndex.ModifyTime)
  33. }
  34. if baseFromOilchemIndex.CreateTime != "" {
  35. baseFromOilchemIndex.CreateTime = utils.GormDateStrToDateTimeStr(baseFromOilchemIndex.CreateTime)
  36. }
  37. }
  38. return
  39. }
  40. type BaseFromOilchemData struct {
  41. BaseFromOilchemDataId int // 数据表ID
  42. BaseFromOilchemIndexId int // 指标ID
  43. IndexCode string // 指标编码
  44. DataTime string
  45. Value string
  46. CreateTime string
  47. ModifyTime string
  48. }
  49. func (baseFromOilchemData *BaseFromOilchemData) AfterFind(db *gorm.DB) (err error) {
  50. if utils.NeedDateOrTimeFormat(utils.DbDriverName) {
  51. if baseFromOilchemData.ModifyTime != "" {
  52. baseFromOilchemData.ModifyTime = utils.GormDateStrToDateTimeStr(baseFromOilchemData.ModifyTime)
  53. }
  54. if baseFromOilchemData.CreateTime != "" {
  55. baseFromOilchemData.CreateTime = utils.GormDateStrToDateTimeStr(baseFromOilchemData.CreateTime)
  56. }
  57. if baseFromOilchemData.DataTime != "" {
  58. baseFromOilchemData.DataTime = utils.GormDateStrToDateStr(baseFromOilchemData.DataTime)
  59. }
  60. }
  61. return
  62. }
  63. func GetOilchemClassifyList() (list []*BaseFromOilchemClassify, err error) {
  64. o := global.DbMap[utils.DbNameIndex]
  65. sql := "SELECT * FROM base_from_oilchem_classify ORDER BY sort ASC"
  66. err = o.Raw(sql).Find(&list).Error
  67. return
  68. }
  69. type BaseFromOilchemIndexList struct {
  70. BaseFromOilchemIndexId int // 主键ID
  71. IndexCode string // 指标编码
  72. IndexName string // 指标名称
  73. ClassifyId int // 分类ID
  74. Unit string // 单位
  75. Frequency string // 频度
  76. Describe string // 指标描述
  77. Sort int // 排序
  78. CreateTime string // 创建时间
  79. ModifyTime string // 修改时间
  80. EdbExist int `description:"edb是否存在"`
  81. DataList []*BaseFromOilchemData
  82. Paging *paging.PagingItem `description:"分页数据"`
  83. }
  84. type BaseFromOilchemIndexListResp struct {
  85. List []*BaseFromOilchemIndexView
  86. Paging *paging.PagingItem `description:"分页数据"`
  87. }
  88. func GetOilchemIndexById(indexId int) (item *BaseFromOilchemIndex, err error) {
  89. o := global.DbMap[utils.DbNameIndex]
  90. sql := ` SELECT * FROM base_from_oilchem_index WHERE 1=1 base_from_oilchem_index_id = ? `
  91. sql += `ORDER BY base_from_oilchem_index_id ASC `
  92. err = o.Raw(sql, indexId).First(&item).Error
  93. return
  94. }
  95. func GetOilchemIndexByCode(indexCode string) (item *BaseFromOilchemIndexView, err error) {
  96. o := global.DbMap[utils.DbNameIndex]
  97. sql := ` SELECT a.*,CASE WHEN e.edb_info_id IS NULL THEN 0 ELSE 1 END AS edb_exist
  98. FROM base_from_oilchem_index as a
  99. LEFT JOIN edb_info AS e ON a.index_code=e.edb_code AND e.source=89
  100. WHERE 1=1 and a.index_code = ? `
  101. sql += `ORDER BY a.base_from_oilchem_index_id ASC `
  102. err = o.Raw(sql, indexCode).First(&item).Error
  103. return
  104. }
  105. func GetOilchemIndexList(condition string, pars interface{}, startSize, pageSize int) (items []*BaseFromOilchemIndex, err error) {
  106. o := global.DbMap[utils.DbNameIndex]
  107. sql := ` SELECT * FROM base_from_oilchem_index WHERE 1=1 `
  108. if condition != "" {
  109. sql += condition
  110. }
  111. sql += `group BY index_code ASC order by create_time DESC LIMIT ?,? `
  112. err = o.Raw(sql, pars, startSize, pageSize).Find(&items).Error
  113. return
  114. }
  115. func GetOilchemIndexListCount(condition string, pars interface{}) (count int, err error) {
  116. o := global.DbMap[utils.DbNameIndex]
  117. sql := ` SELECT COUNT(1) AS count FROM base_from_oilchem_index WHERE 1=1 `
  118. if condition != "" {
  119. sql += condition
  120. }
  121. err = o.Raw(sql, pars).Scan(&count).Error
  122. return
  123. }
  124. func GetOilchemDataListCount(condition string, pars interface{}) (count int, err error) {
  125. o := global.DbMap[utils.DbNameIndex]
  126. sql := ` SELECT COUNT(1) AS count FROM base_from_oilchem_data WHERE 1=1 `
  127. if condition != "" {
  128. sql += condition
  129. }
  130. err = o.Raw(sql, pars).Scan(&count).Error
  131. return
  132. }
  133. func GetOilchemIndexData(condition string, pars interface{}, startSize, pageSize int) (items []*BaseFromOilchemData, err error) {
  134. o := global.DbMap[utils.DbNameIndex]
  135. sql := ` SELECT * FROM base_from_oilchem_data WHERE 1=1 `
  136. if condition != "" {
  137. sql += condition
  138. }
  139. sql += ` order by data_time DESC LIMIT ?,? `
  140. err = o.Raw(sql, pars, startSize, pageSize).Find(&items).Error
  141. return
  142. }
  143. // GetOilchemItemList 模糊查询隆众资讯数据库指标列表
  144. func GetOilchemItemList(keyword string) (items []*BaseFromOilchemIndex, err error) {
  145. o := global.DbMap[utils.DbNameIndex]
  146. sql := "SELECT * FROM base_from_oilchem_index WHERE CONCAT(index_name,index_code) LIKE ? "
  147. err = o.Raw(sql, utils.GetLikeKeyword(keyword)).Find(&items).Error
  148. return
  149. }
  150. // OilchemDataBatchListReq 隆众资讯指标批量列表
  151. type OilchemDataBatchListReq struct {
  152. ClassifyId int `description:"分类id"`
  153. KeyWord string `description:"关键字"`
  154. SelectedId []int `description:"已选指标id, 为true时表示反选"`
  155. IsSelectAll bool `description:"是否查询全部, 默认false, true:全选, false:查询已选"`
  156. }
  157. // GetOilchemIndexByCondition 根据条件获取隆众资讯指标列表
  158. func GetOilchemIndexByCondition(condition string, pars []interface{}) (items []*BaseFromOilchemIndex, err error) {
  159. o := global.DbMap[utils.DbNameIndex]
  160. sql := ` SELECT * FROM base_from_oilchem_index WHERE 1=1 `
  161. if condition != "" {
  162. sql += condition
  163. }
  164. sql += ` ORDER BY sort ASC, base_from_oilchem_index_id ASC`
  165. err = o.Raw(sql, pars...).Find(&items).Error
  166. return
  167. }
  168. // OilchemDataBatchAddCheckReq 隆众资讯指标批量添加校验
  169. type OilchemDataBatchAddCheckReq struct {
  170. IndexCodes []string `description:"指标编码"`
  171. }
  172. // GetOilchemIndexAndEdbInfoByCondition 根据条件获取隆众资讯index和指标库的信息
  173. func GetOilchemIndexAndEdbInfoByCondition(condition string, pars []interface{}) (items []*BaseFromOilchemIndexView, err error) {
  174. wherePars := make([]interface{}, 0)
  175. wherePars = append(wherePars, utils.DATA_SOURCE_OILCHEM)
  176. wherePars = append(wherePars, pars...)
  177. o := global.DbMap[utils.DbNameIndex]
  178. sql := ` SELECT b.*, e.edb_info_id, e.unique_code, e.classify_id AS edb_classify_id FROM base_from_oilchem_index AS b LEFT JOIN edb_info AS e ON b.index_code=e.edb_code AND e.source=? WHERE 1=1 `
  179. if condition != "" {
  180. sql += condition
  181. }
  182. sql += ` ORDER BY sort ASC `
  183. err = o.Raw(sql, wherePars...).Find(&items).Error
  184. return
  185. }
  186. type BaseFromOilchemIndexView struct {
  187. BaseFromOilchemIndexId int `orm:"pk" gorm:"primaryKey"`
  188. EdbInfoId int `description:"指标库id"`
  189. ClassifyId int `description:"指标分类id"`
  190. IndexCode string `description:"指标编码"`
  191. IndexName string `description:"指标名称"`
  192. UniqueCode string `description:"唯一code"`
  193. Frequency string `description:"频度"`
  194. Unit string `description:"单位"`
  195. StartDate string `description:"开始日期"`
  196. EndDate string `description:"结束日期"`
  197. Sort int `description:"排序"`
  198. EdbExist int `description:"edb是否存在"`
  199. EdbClassifyId int `description:"edb分类id"`
  200. ModifyTime string
  201. DataTime string `description:"数据时间"`
  202. Value string `description:"值"`
  203. }
  204. func (baseFromOilchemIndexView *BaseFromOilchemIndexView) AfterFind(db *gorm.DB) (err error) {
  205. if utils.NeedDateOrTimeFormat(utils.DbDriverName) {
  206. if baseFromOilchemIndexView.DataTime != "" {
  207. baseFromOilchemIndexView.DataTime = utils.GormDateStrToDateStr(baseFromOilchemIndexView.DataTime)
  208. }
  209. if baseFromOilchemIndexView.StartDate != "" {
  210. baseFromOilchemIndexView.StartDate = utils.GormDateStrToDateStr(baseFromOilchemIndexView.StartDate)
  211. }
  212. if baseFromOilchemIndexView.EndDate != "" {
  213. baseFromOilchemIndexView.EndDate = utils.GormDateStrToDateStr(baseFromOilchemIndexView.EndDate)
  214. }
  215. if baseFromOilchemIndexView.ModifyTime != "" {
  216. baseFromOilchemIndexView.ModifyTime = utils.GormDateStrToDateTimeStr(baseFromOilchemIndexView.ModifyTime)
  217. }
  218. }
  219. return
  220. }
  221. // ExportOilchemExcelReq 导出隆众资讯excel指标
  222. type ExportOilchemExcelReq struct {
  223. KeyWord string `description:"关键字, 指标编码或指标ID"`
  224. IndexCode []string `description:"指标编码,全选时,表示反选"`
  225. IsSelectedAll bool `description:"是否全选:true:全选|false: 无"`
  226. ClassifyId int `description:"指标id"`
  227. }
  228. // GetOilchemIndexByConditionAndFrequency 根据条件获取隆众资讯指标列表
  229. func GetOilchemIndexByConditionAndFrequency(condition, frequency string, pars []interface{}) (items []*BaseFromOilchemIndex, err error) {
  230. o := global.DbMap[utils.DbNameIndex]
  231. sql := ` SELECT * FROM base_from_oilchem_index WHERE 1=1 `
  232. if condition != "" {
  233. sql += condition
  234. }
  235. sql += ` AND frequency=?`
  236. sql += ` ORDER BY sort ASC, base_from_oilchem_index_id ASC`
  237. pars = append(pars, frequency)
  238. err = o.Raw(sql, pars...).Find(&items).Error
  239. return
  240. }
  241. func GetOilchemDataMaxCount(classifyId int) (count int, err error) {
  242. o := global.DbMap[utils.DbNameIndex]
  243. sql := `SELECT COALESCE(MAX(t.num), 0) AS count FROM (
  244. SELECT COUNT(1) AS num FROM base_from_oilchem_index AS a
  245. INNER JOIN base_from_oilchem_data AS b ON a.index_code=b.index_code
  246. WHERE a.classify_id=?
  247. GROUP BY a.base_from_oilchem_index_id
  248. )AS t `
  249. err = o.Raw(sql, classifyId).Scan(&count).Error
  250. return
  251. }
  252. func GetOilchemIndexDataByCode(indexCode string) (items []*BaseFromOilchemData, err error) {
  253. o := global.DbMap[utils.DbNameIndex]
  254. sql := ` SELECT * FROM base_from_oilchem_data WHERE index_code=? ORDER BY data_time DESC `
  255. err = o.Raw(sql, indexCode).Find(&items).Error
  256. return
  257. }
  258. func GetOilchemFrequencyByCondition(condition string, pars []interface{}) (items []*string, err error) {
  259. sql := `SELECT DISTINCT frequency FROM base_from_oilchem_index WHERE 1=1 `
  260. if condition != "" {
  261. sql += condition
  262. }
  263. sql += ` ORDER BY FIELD(frequency,'日度','周度','月度','季度','半年','年度') `
  264. o := global.DbMap[utils.DbNameIndex]
  265. err = o.Raw(sql, pars...).Find(&items).Error
  266. return
  267. }
  268. // GetOilchemIndexViewList 根据分类id获取隆众资讯指标列表
  269. func GetOilchemIndexViewList(condition string, pars []interface{}, startSize, pageSize int) (items []*BaseFromOilchemIndexView, err error) {
  270. o := global.DbMap[utils.DbNameIndex]
  271. sql := ` SELECT b.*, e.edb_info_id,
  272. CASE WHEN e.edb_info_id IS NULL THEN 0 ELSE 1 END AS edb_exist
  273. FROM base_from_oilchem_index AS b
  274. LEFT JOIN edb_info AS e ON b.index_code=e.edb_code AND e.source=89
  275. WHERE 1=1 `
  276. if condition != "" {
  277. sql += condition
  278. }
  279. sql += ` ORDER BY b.modify_time ASC LIMIT ?,? `
  280. pars = append(pars, startSize, pageSize)
  281. err = o.Raw(sql, pars...).Find(&items).Error
  282. return
  283. }
  284. // GetOilchemIndexViewListCount 根据分类id获取隆众资讯指标列表
  285. func GetOilchemIndexViewListCount(condition string, pars []interface{}) (count int, err error) {
  286. o := global.DbMap[utils.DbNameIndex]
  287. sql := ` SELECT COUNT(1) AS count
  288. FROM base_from_oilchem_index AS b
  289. LEFT JOIN edb_info AS e ON b.index_code=e.edb_code AND e.source=89
  290. WHERE 1=1 `
  291. if condition != "" {
  292. sql += condition
  293. }
  294. sql += ` ORDER BY b.modify_time ASC `
  295. err = o.Raw(sql, pars...).Scan(&count).Error
  296. return
  297. }
  298. // GetOilchemDataViewList 根据指标id获取隆众资讯指标列表
  299. func GetOilchemDataViewList(indexIds []int) (items []*BaseFromOilchemData, err error) {
  300. o := global.DbMap[utils.DbNameIndex]
  301. sql := ` SELECT * FROM base_from_oilchem_data WHERE base_from_oilchem_index_id IN (` + utils.GetOrmInReplace(len(indexIds)) + `)ORDER BY data_time desc `
  302. err = o.Raw(sql, indexIds).Find(&items).Error
  303. return
  304. }
  305. // GetOilchemDataDataTimeByIndexId 根据指标id获取指标数据的日期列表
  306. func GetOilchemDataDataTimeByIndexId(indexIdList []int) (items []string, err error) {
  307. if len(indexIdList) == 0 {
  308. return
  309. }
  310. o := global.DbMap[utils.DbNameIndex]
  311. sql := ` SELECT DISTINCT data_time FROM base_from_oilchem_data WHERE base_from_oilchem_index_id IN (` + utils.GetOrmInReplace(len(indexIdList)) + `) ORDER BY data_time DESC`
  312. err = o.Raw(sql, indexIdList).Find(&items).Error
  313. return
  314. }