index.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. package xiangyu
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_bridge/global"
  6. "fmt"
  7. "io"
  8. "net/http"
  9. "strings"
  10. )
  11. // IndexErrResp
  12. // @Description: 错误信息返回
  13. type IndexErrResp struct {
  14. ErrCode string `json:"errcode" description:"失败编码 1001:缺少参数client_id, 2001:缺少参数access_token, 2006:缺少参数uid, 2002:参数access _token,不正确或过期, 1005:参数client_id非法"`
  15. Msg string `json:"msg" description:"失败信息"`
  16. }
  17. // PushResp
  18. // @Description: push数据返回
  19. type PushResp struct {
  20. IndexErrResp
  21. AccessToken string `json:"access_token"`
  22. RefreshToken string `json:"refresh_token"`
  23. Uid string `json:"uid"`
  24. ExpiresIn int `json:"expires_in"`
  25. }
  26. // PushIndexReq
  27. // @Description: 指标推送请求
  28. type PushIndexReq struct {
  29. In0 In0 `json:"in0"`
  30. Parameters PushIndexParamReq `json:"parameters" description:"业务报文"`
  31. }
  32. // PushIndexParamReq
  33. // @Description: 业务报文
  34. type PushIndexParamReq struct {
  35. SerialID string `json:"serialID" description:"流水号"`
  36. TableCode string `json:"tableCode" description:"数据表编码"`
  37. Total int `json:"total" description:"本次落表数据总数"`
  38. IsEmailWarn int `json:"isEmailWarn" description:"是否发送预警邮件,(1-是 0-否)"`
  39. Data []PushIndexItemReq `json:"data" description:"报文体,指标数据列表"`
  40. }
  41. // PushIndexItemReq
  42. // @Description: 指标数据结构
  43. type PushIndexItemReq struct {
  44. SourceIndexCode string `json:"source_index_code" description:"上游来源指标ID"`
  45. IndexCode string `json:"index_code" description:""`
  46. IndexName string `json:"index_name" description:""`
  47. IndexShortName string `json:"index_short_name" description:""`
  48. FrequenceName string `json:"frequence_name" description:""`
  49. UnitName string `json:"unit_name" description:""`
  50. //CountryName string `json:"country_name" description:""`
  51. //ProvinceName string `json:"province_name" description:""`
  52. //AreaName string `json:"area_name" description:""`
  53. //CityName string `json:"city_name" description:""`
  54. //CountyName string `json:"county_name" description:""`
  55. //RegionName string `json:"region_name" description:""`
  56. //CompanyName string `json:"company_name" description:""`
  57. //BreedName string `json:"breed_name" description:""`
  58. //MaterialName string `json:"material_name" description:""`
  59. //SpecName string `json:"spec_name" description:""`
  60. //MarketName string `json:"market_name" description:""`
  61. //DerivativeType string `json:"derivative_type" description:""`
  62. //ContractName string `json:"contract_name" description:""`
  63. //AuthKindName string `json:"auth_kind_name" description:""`
  64. //CustomSmallClassName string `json:"custom_small_class_name" description:""`
  65. AssetBeginDate string `json:"asset_begin_date" description:""`
  66. AssetEndDate string `json:"asset_end_date" description:""`
  67. CreateUser string `json:"create_user" description:""`
  68. IndexCreateTime string `json:"index_create_time" description:""`
  69. UpdateUser string `json:"update_user" description:""`
  70. DetailUpdateTime string `json:"detail_update_time" description:""`
  71. IndexUpdateTime string `json:"index_update_time" description:""`
  72. //DutyDept string `json:"duty_dept" description:""`
  73. //BusinessDept string `json:"business_dept" description:""`
  74. OrginSource string `json:"orgin_source" description:""`
  75. OrginSysSource string `json:"orgin_sys_source" description:""`
  76. SysSource string `json:"sys_source" description:""`
  77. SourceType string `json:"source_type" description:""`
  78. Status int `json:"status" description:""`
  79. }
  80. type PushIndexDataResp struct {
  81. Out struct {
  82. ReturnCode string `json:"returnCode"`
  83. ReturnMsg string `json:"returnMsg"`
  84. Data int `json:"data"`
  85. } `json:"out"`
  86. }
  87. // PushIndexData
  88. // @Description: 指标信息推送
  89. // @author: Roc
  90. // @datetime 2024-02-28 17:45:03
  91. // @param data PushIndexParamReq
  92. // @return resp *PushDataResp
  93. // @return err error
  94. func PushIndexData(data PushBaseParamReq) (resp *PushIndexDataResp, err error) {
  95. urlPath := `/DAQ/CY/ProxyServices/pushMarketPricePS`
  96. req := PushBaseReq{
  97. In0: In0{
  98. PageTotal: "",
  99. PageNo: "",
  100. DocType: "pushMarketPricePS",
  101. Property: "",
  102. //DocCode: getDocCode(),
  103. Source: global.CONFIG.Xiangyu.SystemCode,
  104. Target: global.CONFIG.Xiangyu.IndexSyncTarget,
  105. },
  106. Parameters: data,
  107. }
  108. postData, err := json.Marshal(req)
  109. if err != nil {
  110. return
  111. }
  112. result, err := HttpPostIndex(urlPath, string(postData))
  113. if err != nil {
  114. return
  115. }
  116. // 解析响应结果
  117. err = json.Unmarshal(result, &resp)
  118. if err != nil {
  119. return
  120. }
  121. if resp.Out.ReturnCode != "S" {
  122. err = errors.New(fmt.Sprintf("响应代码:%s,错误信息:%s", resp.Out.ReturnCode, resp.Out.ReturnMsg))
  123. return
  124. }
  125. return
  126. }
  127. // PushIndexValueReq
  128. // @Description: 指标日期值推送请求
  129. type PushIndexValueReq struct {
  130. In0 In0 `json:"in0"`
  131. Parameters PushIndexValueParamReq `json:"parameters" description:"业务报文"`
  132. }
  133. // PushIndexValueParamReq
  134. // @Description: 业务报文
  135. type PushIndexValueParamReq struct {
  136. SerialID string `json:"serialID" description:"流水号"`
  137. TableCode string `json:"tableCode" description:"数据表编码"`
  138. Total int `json:"total" description:"本次落表数据总数"`
  139. IsEmailWarn int `json:"isEmailWarn" description:"是否发送预警邮件,(1-是 0-否)"`
  140. Data []PushIndexValueItemReq `json:"data" description:"报文体,指标日期值数据列表"`
  141. }
  142. // PushIndexValueItemReq
  143. // @Description: 指标日期值数据结构
  144. type PushIndexValueItemReq struct {
  145. Id string `json:"id"`
  146. IndexCode string `json:"index_code" description:"指标代码"`
  147. Value string `json:"value" description:"数值"`
  148. BusinessDate string `json:"business_date" description:"业务日期(数据日期)"`
  149. CreateTime string `json:"create_time" description:"数据进入ETA的时间"`
  150. UpdateTime string `json:"update_time" description:"eta库中修改数据的时间"`
  151. Status string `json:"status" description:"逻辑删除使用,0-禁用,1-启用"`
  152. }
  153. // PushEdbValue
  154. // @Description: 获取token信息
  155. // @author: Roc
  156. // @datetime 2024-01-23 15:40:56
  157. // @param code string
  158. // @return resp *GetTokenResp
  159. // @return err error
  160. func PushEdbValue(data PushBaseParamReq) (resp *PushIndexDataResp, err error) {
  161. urlPath := `/DAQ/CY/ProxyServices/pushMarketPricePS`
  162. req := PushBaseReq{
  163. In0: In0{
  164. PageTotal: "",
  165. PageNo: "",
  166. DocType: "pushMarketPricePS",
  167. Property: "",
  168. //DocCode: getDocCode(),
  169. Source: global.CONFIG.Xiangyu.SystemCode,
  170. Target: global.CONFIG.Xiangyu.IndexSyncTarget,
  171. },
  172. Parameters: data,
  173. }
  174. postData, err := json.Marshal(req)
  175. if err != nil {
  176. return
  177. }
  178. result, err := HttpPostIndex(urlPath, string(postData))
  179. if err != nil {
  180. return
  181. }
  182. // 解析响应结果
  183. err = json.Unmarshal(result, &resp)
  184. if err != nil {
  185. return
  186. }
  187. if resp.Out.ReturnCode != "S" {
  188. err = errors.New(fmt.Sprintf("响应代码:%s,错误信息:%s", resp.Out.ReturnCode, resp.Out.ReturnMsg))
  189. return
  190. }
  191. return
  192. }
  193. // PushBaseReq
  194. // @Description: 基础请求
  195. type PushBaseReq struct {
  196. In0 In0 `json:"in0"`
  197. Parameters PushBaseParamReq `json:"parameters" description:"业务报文"`
  198. }
  199. // PushBaseParamReq
  200. // @Description: 基础业务报文
  201. type PushBaseParamReq struct {
  202. SerialID string `json:"serialID" description:"流水号"`
  203. TableCode string `json:"tableCode" description:"数据表编码"`
  204. Total int `json:"total" description:"本次落表数据总数"`
  205. IsEmailWarn int `json:"isEmailWarn" description:"是否发送预警邮件,(1-是 0-否)"`
  206. Data interface{} `json:"data" description:"报文体,指标日期值数据列表"`
  207. }
  208. // PushClassifyItemReq
  209. // @Description: 指标分类数据结构
  210. type PushClassifyItemReq struct {
  211. ClassifyId int `json:"classify_id" description:"自增id"`
  212. ClassifyType int `json:"classify_type" description:"分类类型,0:普通指标分类,1:预测指标分类"`
  213. ClassifyName string `json:"classify_name" description:"分类名称"`
  214. ParentId int `json:"parent_id" description:"父级id"`
  215. HasData int `json:"has_data" description:"是否存在指标数据,1:有,2:无"`
  216. CreateTime string `json:"create_time" description:"创建时间"`
  217. UpdateTime string `json:"update_time" description:"修改时间"`
  218. SysUserId int `json:"sys_user_id" description:"创建人id"`
  219. SysUserRealName string `json:"sys_user_real_name" description:"创建人姓名"`
  220. Level int `json:"level" description:"层级"`
  221. UniqueCode string `json:"unique_code" description:"唯一编码"`
  222. SortColumn int `json:"sort_column" description:"排序字段,越小越靠前,默认值:10"`
  223. }
  224. // PushClassify
  225. // @Description: 推送指标分类
  226. // @author: Roc
  227. // @datetime 2024-01-23 15:40:56
  228. // @param code string
  229. // @return resp *GetTokenResp
  230. // @return err error
  231. func PushClassify(data PushBaseParamReq) (resp *PushIndexDataResp, err error) {
  232. urlPath := `/DAQ/CY/ProxyServices/pushMarketPricePS`
  233. req := PushBaseReq{
  234. In0: In0{
  235. PageTotal: "",
  236. PageNo: "",
  237. DocType: "pushMarketPricePS",
  238. Property: "",
  239. //DocCode: getDocCode(),
  240. Source: global.CONFIG.Xiangyu.SystemCode,
  241. Target: global.CONFIG.Xiangyu.IndexSyncTarget,
  242. },
  243. Parameters: data,
  244. }
  245. postData, err := json.Marshal(req)
  246. if err != nil {
  247. return
  248. }
  249. result, err := HttpPostIndex(urlPath, string(postData))
  250. if err != nil {
  251. return
  252. }
  253. // 解析响应结果
  254. err = json.Unmarshal(result, &resp)
  255. if err != nil {
  256. return
  257. }
  258. if resp.Out.ReturnCode != "S" {
  259. err = errors.New(fmt.Sprintf("响应代码:%s,错误信息:%s", resp.Out.ReturnCode, resp.Out.ReturnMsg))
  260. return
  261. }
  262. return
  263. }
  264. // PushBase
  265. // @Description: 基础推送接口
  266. // @author: Roc
  267. // @datetime 2024-01-23 15:40:56
  268. // @param code string
  269. // @return resp *GetTokenResp
  270. // @return err error
  271. func PushBase(data PushBaseParamReq) (resp *PushIndexDataResp, err error) {
  272. urlPath := `/DAQ/CY/ProxyServices/pushMarketPricePS`
  273. req := PushBaseReq{
  274. In0: In0{
  275. PageTotal: "",
  276. PageNo: "",
  277. DocType: "pushMarketPricePS",
  278. Property: "",
  279. //DocCode: getDocCode(),
  280. Source: global.CONFIG.Xiangyu.SystemCode,
  281. Target: global.CONFIG.Xiangyu.IndexSyncTarget,
  282. },
  283. Parameters: data,
  284. }
  285. postData, err := json.Marshal(req)
  286. if err != nil {
  287. return
  288. }
  289. result, err := HttpPostIndex(urlPath, string(postData))
  290. if err != nil {
  291. return
  292. }
  293. // 解析响应结果
  294. err = json.Unmarshal(result, &resp)
  295. if err != nil {
  296. return
  297. }
  298. if resp.Out.ReturnCode != "S" {
  299. err = errors.New(fmt.Sprintf("响应代码:%s,错误信息:%s", resp.Out.ReturnCode, resp.Out.ReturnMsg))
  300. return
  301. }
  302. return
  303. }
  304. // PushEdbClassifyItemReq
  305. // @Description: 指标与目录的关系请求结构
  306. type PushEdbClassifyItemReq struct {
  307. Id string `json:"id" description:"唯一主键"`
  308. ClassifyId int `json:"classify_id" description:"目录分类ID"`
  309. IndexCode string `json:"index_code" description:"指标ID"`
  310. CreateTime string `json:"create_time" description:"创建时间"`
  311. CreateUser string `json:"create_user" description:"创建人"`
  312. UpdateTime string `json:"update_time" description:"修改时间"`
  313. UpdateUser string `json:"update_user" description:"修改人"`
  314. }
  315. // HttpPostIndex
  316. // @Description: post请求
  317. // @author: Roc
  318. // @datetime 2024-02-27 18:45:30
  319. // @param urlPath string
  320. // @param postData string
  321. // @return []byte
  322. // @return error
  323. func HttpPostIndex(urlPath, postData string) ([]byte, error) {
  324. if global.CONFIG.Xiangyu.IndexSyncHost == `` {
  325. return nil, errors.New("数仓同步接口地址为空")
  326. }
  327. // 请求地址
  328. postUrl := global.CONFIG.Xiangyu.IndexSyncHost + urlPath
  329. body := io.NopCloser(strings.NewReader(postData))
  330. client := &http.Client{}
  331. req, err := http.NewRequest("POST", postUrl, body)
  332. if err != nil {
  333. return nil, err
  334. }
  335. req.Header.Set("content-Type", "application/json; charset=utf-8")
  336. req.Header.Set("Accept-Encoding", "application/json; charset=utf-8")
  337. req.Header.Set("Accept", "application/json; charset=utf-8")
  338. // 鉴权
  339. req.SetBasicAuth(global.CONFIG.Xiangyu.IndexSyncAuthUserName, global.CONFIG.Xiangyu.IndexSyncAuthPwd)
  340. // 秘钥
  341. if global.CONFIG.Xiangyu.IndexKey != `` {
  342. req.Header.Set("deipaaskeyauth", global.CONFIG.Xiangyu.IndexKey)
  343. }
  344. resp, err := client.Do(req)
  345. if err != nil {
  346. return nil, err
  347. }
  348. defer resp.Body.Close()
  349. result, err := io.ReadAll(resp.Body)
  350. if err != nil {
  351. return nil, err
  352. }
  353. // 日志记录
  354. global.FILE_LOG.Debug("指标同步服务:地址:" + postUrl + ";\n请求参数:" + postData + ";\n返回参数:" + string(result))
  355. // 解析返回参数,判断是否是json
  356. if !json.Valid(result) {
  357. err = errors.New("返回参数不是json格式")
  358. }
  359. return result, err
  360. }