dict.go 16 KB

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