dict.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. package jiayue
  2. import (
  3. "database/sql"
  4. "eta/eta_bridge/global"
  5. "fmt"
  6. "time"
  7. )
  8. var (
  9. IndexTableName = "DICT_INDEX"
  10. )
  11. func GetDictIndex(condition string, pars []interface{}) (dictIndexList []DictIndex, err error) {
  12. defer func() {
  13. if err != nil {
  14. global.LOG.Info("获取桥接服务指标信息失败 Err:" + err.Error())
  15. }
  16. }()
  17. selectVals := "ID, CODE, NAME, UNIT, FREQUENCY, DESCRIPTION, TABLE_NAME, SOURCE_TYPE, SOURCE_CODE, SOURCE_DESCRIPTION, INDUSTRY, TYPE, COMMODITY, SJB_ID, USER_ID, ROWS_COUNT, DATE_FIRST, DATE_LAST, TIME_LAST_UPDATE, TIME_LAST_REQUEST, PRIORITY, STATUS, SHORT_NAME, UPDATE_DESCRIPTION, FORECAST_FLAG, MANUAL_FLAG, VARIABLE_FLAG, MARKETDATA_FLAG, CREATE_USER, CREATE_TIME, UPDATE_USER, UPDATE_TIME"
  18. sqlStatement := fmt.Sprintf("SELECT %s, 0 AS no FROM %s WHERE %s", selectVals, IndexTableName, condition)
  19. dictIndexList, err = getDictIndex(sqlStatement, pars)
  20. if err != nil {
  21. fmt.Printf("查询指标信息失败 %s", err)
  22. err = fmt.Errorf("查询指标信息失败 %s", err)
  23. return
  24. }
  25. fmt.Printf("查询指标信息成功")
  26. return
  27. }
  28. // GetDictPageIndex 分页获取指标
  29. func GetDictPageIndex(condition string, pars []interface{}, pageIndex, pageSize, sortField, sortRule int) (dictIndexList []DictIndex, err error) {
  30. defer func() {
  31. if err != nil {
  32. fmt.Printf("查询指标信息失败 %s", err)
  33. global.LOG.Info("获取桥接服务指标信息失败 Err:" + err.Error())
  34. }
  35. }()
  36. fields := "ID, CODE, NAME, UNIT, FREQUENCY, DESCRIPTION, TABLE_NAME, SOURCE_TYPE, SOURCE_CODE, SOURCE_DESCRIPTION, INDUSTRY, TYPE, COMMODITY, SJB_ID, USER_ID, ROWS_COUNT, DATE_FIRST, DATE_LAST, TIME_LAST_UPDATE, TIME_LAST_REQUEST, PRIORITY, STATUS, SHORT_NAME, UPDATE_DESCRIPTION, FORECAST_FLAG, MANUAL_FLAG, VARIABLE_FLAG, MARKETDATA_FLAG, CREATE_USER, CREATE_TIME, UPDATE_USER, UPDATE_TIME"
  37. // 排序
  38. order := ``
  39. sortFMap := map[int]string{1: "DateFirst", 2: "DateLast", 3: "TimeLastUpdate"}
  40. sortRMap := map[int]string{1: "ASC", 2: "DESC"}
  41. if sortFMap[sortField] != "" && sortRMap[sortRule] != "" {
  42. order = fmt.Sprintf("ORDER BY %s %s", sortFMap[sortField], sortRMap[sortRule])
  43. }
  44. sqlBase := fmt.Sprintf(`SELECT %s, no FROM (SELECT rownum no, a.* FROM %s a WHERE %s) WHERE no BETWEEN %d AND %d %s`, fields, IndexTableName, condition, (pageIndex-1)*pageSize+1, pageIndex*pageSize, order)
  45. dictIndexList, err = getDictIndex(sqlBase, pars)
  46. if err != nil {
  47. err = fmt.Errorf("查询指标信息失败 %s", err)
  48. return
  49. }
  50. return
  51. }
  52. func GetDictData(tableName string, condition string, pars []interface{}) (dictIndexData []DictData, err error) {
  53. defer func() {
  54. if err != nil {
  55. global.LOG.Info("获取桥接服务指标数据失败 Err:" + err.Error())
  56. }
  57. }()
  58. sqlStatement := fmt.Sprintf("SELECT ID, INDEX_ID, INDEX_VALUE, INDEX_DATE, UPDATE_TIME FROM %s WHERE %s", tableName, condition)
  59. dictIndexData, err = getDictData(sqlStatement, pars)
  60. if err != nil {
  61. fmt.Printf("查询指标数据失败 %s", err)
  62. err = fmt.Errorf("查询指标数据失败 %s", err)
  63. return
  64. }
  65. fmt.Printf("查询指标信息成功")
  66. return
  67. }
  68. type DictIndex struct {
  69. Id int `description:"主键"`
  70. Code string `description:"指标编码"`
  71. Name string `description:"指标名称"`
  72. Unit string `description:"单位"`
  73. Frequency string `description:"更新频率"`
  74. Description string `description:"描述"`
  75. TableName string `description:"指标数据存储库表"`
  76. SourceType string `description:"指标来源,如:wind:万德;manual:手工导入;formula:公式;webisite:网页爬取"`
  77. SourceCode string `description:"来源编码"`
  78. SourceDescription string `description:"来源说明"`
  79. Industry string `description:"品种板块"`
  80. Type string `description:"指标类型"`
  81. Commodity string `description:"商品名称"`
  82. SjbId int `description:"SJB_ID"`
  83. UserId int `description:"所属用户"`
  84. RowsCount int `description:"指标数据数量"`
  85. DateFirst string `description:"指标开始时间"`
  86. DateLast string `description:"指标最新时间"`
  87. TimeLastUpdate time.Time `description:"最新更新时间"`
  88. TimeLastRequest time.Time `description:"下次更新时间"`
  89. Priority int `description:"更新优先级"`
  90. Status int `description:"指标状态"`
  91. ShortName string `description:"指标简称"`
  92. UpdateDescription string `description:"更新说明"`
  93. ForecastFlag int `description:"预测标识"`
  94. ManualFlag int `description:"手动标识"`
  95. VariableFlag int `description:"有效标识"`
  96. MarketDataFlag int `description:"市场价标识"`
  97. CreateUser int `description:"创建用户"`
  98. CreateTime time.Time `description:"创建时间"`
  99. UpdateUser int `description:"更新用户"`
  100. UpdateTime time.Time `description:"更新时间"`
  101. }
  102. type DictIndexSql struct {
  103. Id int `description:"主键" json:"ID"`
  104. Code sql.NullString `description:"指标编码" json:"CODE"`
  105. Name sql.NullString `description:"指标名称" json:"NAME"`
  106. Unit sql.NullString `description:"单位" json:"UNIT"`
  107. Frequency sql.NullString `description:"更新频率" json:"FREQUENCY"`
  108. Description sql.NullString `description:"描述" json:"DESCRIPTION"`
  109. TableName sql.NullString `description:"指标数据存储库表" json:"TABLE_NAME"`
  110. SourceType sql.NullString `description:"指标来源,如:wind:万德;manual:手工导入;formula:公式;webisite:网页爬取" json:"SOURCE_TYPE"`
  111. SourceCode sql.NullString `description:"来源编码" json:"SOURCE_CODE"`
  112. SourceDescription sql.NullString `description:"来源说明" json:"SOURCE_DESCRIPTION"`
  113. Industry sql.NullString `description:"品种板块" json:"INDUSTRY"`
  114. Type sql.NullString `description:"指标类型" json:"TYPE"`
  115. Commodity sql.NullString `description:"商品名称" json:"COMMODITY"`
  116. SjbId sql.NullInt32 `json:"SJB_ID"`
  117. UserId sql.NullInt32 `description:"所属用户" json:"USER_ID"`
  118. RowsCount sql.NullInt32 `description:"指标数据数量" json:"ROWS_COUNT"`
  119. DateFirst sql.NullString `description:"指标开始时间" json:"DATE_FIRST"`
  120. DateLast sql.NullString `description:"指标最新时间" json:"DATE_LAST"`
  121. TimeLastUpdate sql.NullTime `description:"最新更新时间" json:"TIME_LAST_UPDATE"`
  122. TimeLastRequest sql.NullTime `description:"下次更新时间" json:"TIME_LAST_REQUEST"`
  123. Priority sql.NullInt32 `description:"更新优先级" json:"PRIORITY"`
  124. Status sql.NullInt32 `description:"指标状态" json:"STATUS"`
  125. ShortName sql.NullString `description:"指标简称" json:"SHORT_NAME"`
  126. UpdateDescription sql.NullString `description:"更新说明" json:"UPDATE_DESCRIPTION"`
  127. ForecastFlag sql.NullInt32 `description:"预测标识" json:"FORECAST_FLAG"`
  128. ManualFlag sql.NullInt32 `description:"手动标识" json:"MANUAL_FLAG"`
  129. VariableFlag sql.NullInt32 `description:"有效标识" json:"VARIABLE_FLAG"`
  130. MarketDataFlag sql.NullInt32 `description:"市场价标识" json:"MARKETDATA_FLAG"`
  131. CreateUser sql.NullInt32 `description:"创建用户" json:"CREATE_USER"`
  132. CreateTime sql.NullTime `description:"创建时间" json:"CREATE_TIME"`
  133. UpdateUser sql.NullInt32 `description:"更新用户" json:"UPDATE_USER"`
  134. UpdateTime sql.NullTime `description:"更新时间" json:"UPDATE_TIME"`
  135. No sql.NullInt32 `description:"分页查询数" json:"no"`
  136. }
  137. type DictDataSql struct {
  138. Id int `description:"主键" json:"ID"`
  139. IndexId int `json:"INDEX_ID"`
  140. IndexValue sql.NullFloat64 `json:"INDEX_VALUE"`
  141. IndexDate sql.NullString `json:"INDEX_DATE"`
  142. UpdateTime sql.NullString `json:"UPDATE_TIME"`
  143. }
  144. type DictData struct {
  145. Id int `description:"主键" json:"ID"`
  146. IndexId int
  147. IndexValue float64
  148. IndexDate string
  149. UpdateTime string
  150. }
  151. func getDictIndex(sqlStatement string, pars []interface{}) (dictIndexs []DictIndex, err error) {
  152. //查询ZHUJI表中code='pt'的sql:SELECT * FROM ZHUJI where CODE = 'pt'
  153. //sqlStatement := "SELECT * FROM DICT_CATEGORY where CODE = :1"
  154. //sqlStatement := "SELECT * FROM DICT_INDEX WHERE SOURCE_TYPE = :1 AND ROWNUM < 11"
  155. stmt, err := global.OracleJy.Prepare(sqlStatement)
  156. if err != nil {
  157. err = fmt.Errorf("sql 预处理 失败,Err: %s", err)
  158. return
  159. }
  160. rows, err := stmt.Query(pars...) //输入sql中对应参数的值
  161. if err != nil {
  162. err = fmt.Errorf("sql 查询失败,Err: %s", err)
  163. return
  164. }
  165. defer rows.Close() //defer关闭查询连接
  166. //获取列相关信息
  167. //获取列相关信息
  168. strings, _ := rows.Columns()
  169. global.LOG.Info("获取列表相关信息")
  170. for i := 0; i < len(strings); i++ {
  171. fmt.Print(" ", strings[i])
  172. //global.LOG.Info("显示字段:", strings[i])
  173. }
  174. fmt.Print("\n")
  175. global.LOG.Info("\n")
  176. for rows.Next() {
  177. var tmp DictIndexSql
  178. err = rows.Scan(&tmp.Id, &tmp.Code, &tmp.Name, &tmp.Unit, &tmp.Frequency, &tmp.Description, &tmp.TableName, &tmp.SourceType, &tmp.SourceCode, &tmp.SourceDescription, &tmp.Industry, &tmp.Type, &tmp.Commodity, &tmp.SjbId, &tmp.UserId, &tmp.RowsCount, &tmp.DateFirst, &tmp.DateLast, &tmp.TimeLastUpdate, &tmp.TimeLastRequest, &tmp.Priority, &tmp.Status, &tmp.ShortName, &tmp.UpdateDescription, &tmp.ForecastFlag, &tmp.ManualFlag, &tmp.VariableFlag, &tmp.MarketDataFlag, &tmp.CreateUser, &tmp.CreateTime, &tmp.UpdateUser, &tmp.UpdateTime, &tmp.No)
  179. if err != nil {
  180. fmt.Printf("扫描错误:", err)
  181. err = fmt.Errorf("扫描错误:" + err.Error())
  182. return
  183. }
  184. dictTmp := DictIndex{
  185. Id: tmp.Id,
  186. Code: tmp.Code.String,
  187. Name: tmp.Name.String,
  188. Unit: tmp.Unit.String,
  189. Frequency: tmp.Frequency.String,
  190. Description: tmp.Description.String,
  191. TableName: tmp.TableName.String,
  192. SourceType: tmp.SourceType.String,
  193. SourceCode: tmp.SourceCode.String,
  194. SourceDescription: tmp.SourceDescription.String,
  195. Industry: tmp.Industry.String,
  196. Type: tmp.Type.String,
  197. Commodity: tmp.Commodity.String,
  198. SjbId: int(tmp.SjbId.Int32),
  199. UserId: int(tmp.UserId.Int32),
  200. RowsCount: int(tmp.RowsCount.Int32),
  201. DateFirst: tmp.DateFirst.String,
  202. DateLast: tmp.DateLast.String,
  203. TimeLastUpdate: tmp.TimeLastUpdate.Time,
  204. TimeLastRequest: tmp.TimeLastRequest.Time,
  205. Priority: int(tmp.Priority.Int32),
  206. Status: int(tmp.Status.Int32),
  207. ShortName: tmp.ShortName.String,
  208. UpdateDescription: tmp.UpdateDescription.String,
  209. ForecastFlag: int(tmp.ForecastFlag.Int32),
  210. ManualFlag: int(tmp.ManualFlag.Int32),
  211. VariableFlag: int(tmp.VariableFlag.Int32),
  212. MarketDataFlag: int(tmp.MarketDataFlag.Int32),
  213. CreateUser: int(tmp.CreateUser.Int32),
  214. CreateTime: tmp.CreateTime.Time,
  215. UpdateUser: int(tmp.UpdateUser.Int32),
  216. UpdateTime: tmp.UpdateTime.Time,
  217. }
  218. dictIndexs = append(dictIndexs, dictTmp)
  219. }
  220. if err = rows.Err(); err != nil {
  221. // handle the error here
  222. err = fmt.Errorf("解析行数据失败" + err.Error())
  223. return
  224. }
  225. defer stmt.Close()
  226. /*data, err := json.Marshal(dictIndexs)
  227. if err != nil {
  228. fmt.Printf("序列化错误 err = %v\n", err)
  229. global.LOG.Info("序列化错误 err = %v\n", err)
  230. return
  231. }
  232. //输出序列化后的结果 json字符串
  233. fmt.Printf("序列化后 = %v\n", string(data))
  234. global.LOG.Info("序列化后 = %v\n", string(data))*/
  235. return
  236. }
  237. func getDictData(sqlStatement string, pars []interface{}) (dictData []DictData, err error) {
  238. //查询ZHUJI表中code='pt'的sql:SELECT * FROM ZHUJI where CODE = 'pt'
  239. //sqlStatement := "SELECT * FROM DICT_CATEGORY where CODE = :1"
  240. //sqlStatement := "SELECT * FROM DICT_INDEX WHERE SOURCE_TYPE = :1 AND ROWNUM < 11"
  241. stmt, err := global.OracleJy.Prepare(sqlStatement)
  242. if err != nil {
  243. err = fmt.Errorf("sql 预处理 失败,Err: %s", err)
  244. return
  245. }
  246. rows, err := stmt.Query(pars...) //输入sql中对应参数的值
  247. if err != nil {
  248. err = fmt.Errorf("sql 查询失败,Err: %s", err)
  249. return
  250. }
  251. defer rows.Close() //defer关闭查询连接
  252. //获取列相关信息
  253. //获取列相关信息
  254. strings, _ := rows.Columns()
  255. for i := 0; i < len(strings); i++ {
  256. fmt.Print(" ", strings[i])
  257. }
  258. fmt.Print("\n")
  259. for rows.Next() {
  260. var tmp DictDataSql
  261. err = rows.Scan(&tmp.Id, &tmp.IndexId, &tmp.IndexValue, &tmp.IndexDate, &tmp.UpdateTime)
  262. if err != nil {
  263. fmt.Printf("扫描错误:", err)
  264. err = fmt.Errorf("扫描错误" + err.Error())
  265. return
  266. }
  267. dictTmp := DictData{
  268. Id: tmp.Id,
  269. IndexId: tmp.IndexId,
  270. IndexValue: tmp.IndexValue.Float64,
  271. IndexDate: tmp.IndexDate.String,
  272. UpdateTime: tmp.UpdateTime.String,
  273. }
  274. dictData = append(dictData, dictTmp)
  275. }
  276. if err = rows.Err(); err != nil {
  277. // handle the error here
  278. err = fmt.Errorf("解析行数据失败" + err.Error())
  279. return
  280. }
  281. defer stmt.Close()
  282. /*data, err := json.Marshal(dictData)
  283. if err != nil {
  284. fmt.Printf("序列化错误 err = %v\n", err)
  285. }
  286. //输出序列化后的结果 json字符串
  287. fmt.Printf("序列化后 = %v\n", string(data))*/
  288. return
  289. }
  290. // GetDictFrequency 获取指标频度
  291. func GetDictFrequency() (frequencies []string, err error) {
  292. defer func() {
  293. if err != nil {
  294. global.LOG.Info("嘉悦-获取指标频度失败, Err:" + err.Error())
  295. }
  296. }()
  297. sqlBase := fmt.Sprintf(`SELECT DISTINCT FREQUENCY FROM %s`, IndexTableName)
  298. stmt, e := global.OracleJy.Prepare(sqlBase)
  299. if e != nil {
  300. err = fmt.Errorf("预处理sql失败, err: %s", e.Error())
  301. return
  302. }
  303. rows, e := stmt.Query()
  304. if e != nil {
  305. err = fmt.Errorf("查询sql失败, err: %s", e.Error())
  306. return
  307. }
  308. defer func() {
  309. _ = rows.Close() // 关闭查询连接
  310. }()
  311. for rows.Next() {
  312. var frequency string
  313. if e = rows.Scan(&frequency); e != nil {
  314. err = fmt.Errorf("扫描错误, err: %s", e.Error())
  315. return
  316. }
  317. frequencies = append(frequencies, frequency)
  318. }
  319. if e = rows.Err(); e != nil {
  320. err = fmt.Errorf("解析行数据失败, err: %s", e.Error())
  321. return
  322. }
  323. defer func() {
  324. _ = stmt.Close()
  325. }()
  326. return
  327. }
  328. // GetDictIndexCount 获取指标总记录数
  329. func GetDictIndexCount(condition string, pars []interface{}) (total int, err error) {
  330. defer func() {
  331. if err != nil {
  332. global.LOG.Info("嘉悦-获取指标总记录数失败, Err:" + err.Error())
  333. }
  334. }()
  335. sqlBase := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE %s`, IndexTableName, condition)
  336. stmt, e := global.OracleJy.Prepare(sqlBase)
  337. if e != nil {
  338. err = fmt.Errorf("预处理sql失败, err: %s", e.Error())
  339. return
  340. }
  341. rows, e := stmt.Query(pars...)
  342. if e != nil {
  343. err = fmt.Errorf("查询sql失败, err: %s", e.Error())
  344. return
  345. }
  346. defer func() {
  347. _ = rows.Close()
  348. }()
  349. for rows.Next() {
  350. if e = rows.Scan(&total); e != nil {
  351. err = fmt.Errorf("扫描错误, err: %s", e.Error())
  352. return
  353. }
  354. }
  355. if e = rows.Err(); e != nil {
  356. err = fmt.Errorf("解析行数据失败, err: %s", e.Error())
  357. return
  358. }
  359. defer func() {
  360. _ = stmt.Close()
  361. }()
  362. return
  363. }