base_from_oilchem.go 13 KB

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