dict.go 15 KB

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