edb_lib.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. package watch
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/mysteel_watch/global"
  6. "eta/mysteel_watch/models/index"
  7. "eta/mysteel_watch/utils"
  8. "fmt"
  9. "io/ioutil"
  10. "net/http"
  11. "strings"
  12. "time"
  13. )
  14. type BaseResponse struct {
  15. Ret int
  16. Msg string
  17. ErrMsg string
  18. ErrCode string
  19. Data interface{}
  20. Success bool `description:"true 执行成功,false 执行失败"`
  21. IsSendEmail bool `json:"-" description:"true 发送邮件,false 不发送邮件"`
  22. IsAddLog bool `json:"-" description:"true 新增操作日志,false 不新增操作日志" `
  23. }
  24. type IndexesResponse struct {
  25. Ret int
  26. Msg string
  27. ErrMsg string
  28. ErrCode string
  29. Data []index.BaseFromMysteelChemicalIndex
  30. Success bool `description:"true 执行成功,false 执行失败"`
  31. IsSendEmail bool `json:"-" description:"true 发送邮件,false 不发送邮件"`
  32. IsAddLog bool `json:"-" description:"true 新增操作日志,false 不新增操作日志" `
  33. }
  34. type IndexResponse struct {
  35. Ret int
  36. Msg string
  37. ErrMsg string
  38. ErrCode string
  39. Data index.BaseFromMysteelChemicalIndex
  40. Success bool `description:"true 执行成功,false 执行失败"`
  41. IsSendEmail bool `json:"-" description:"true 发送邮件,false 不发送邮件"`
  42. IsAddLog bool `json:"-" description:"true 新增操作日志,false 不新增操作日志" `
  43. }
  44. // postRefreshEdbData 刷新指标数据
  45. //func postRefreshEdbData(param map[string]interface{}) (resp *BaseResponse, err error) {
  46. // urlStr := "mysteel_chemical/refresh"
  47. //
  48. // if global.CONFIG.Serve.EdbLibUrl == `` {
  49. // err = errors.New("刷新未配置")
  50. // return
  51. // }
  52. // postUrl := global.CONFIG.Serve.EdbLibUrl + urlStr
  53. // postData, err := json.Marshal(param)
  54. // if err != nil {
  55. // return
  56. // }
  57. // result, err := HttpPost(postUrl, string(postData), "application/json")
  58. // if err != nil {
  59. // return
  60. // }
  61. // if result != nil {
  62. // global.LOG.Info(" Refresh Result: " + string(result))
  63. // err = json.Unmarshal(result, &resp)
  64. // if err != nil {
  65. // return
  66. // }
  67. // return resp, nil
  68. // }
  69. // return nil, err
  70. //}
  71. // postHandleMysteelIndex 处理指标和指标数据
  72. func postHandleMysteelIndex(req *HandleMysteelIndexReq) (resp *BaseResponse, err error) {
  73. urlStr := "/mysteel_chemical/handle/mysteel/index"
  74. if global.CONFIG.Serve.EdbLibUrl == `` {
  75. err = errors.New("刷新未配置")
  76. return
  77. }
  78. postUrl := global.CONFIG.Serve.EdbLibUrl + urlStr
  79. postData, err := json.Marshal(req)
  80. if err != nil {
  81. return
  82. }
  83. //fmt.Println("postData:" + string(postData))
  84. global.LOG.Debug(" postData: " + string(postData))
  85. result, err := HttpPost(postUrl, string(postData), "application/json")
  86. if err != nil {
  87. return
  88. }
  89. if result != nil {
  90. global.LOG.Debug(" postHandleMysteelIndex Result: " + string(result))
  91. err = json.Unmarshal(result, &resp)
  92. if err != nil {
  93. return
  94. }
  95. return resp, nil
  96. }
  97. return nil, err
  98. }
  99. // GetMaxFileIndexReq 获取最大文件数请求
  100. type GetMaxFileIndexReq struct {
  101. Frequency string
  102. EndDate string
  103. TerminalCode string `description:"终端编码"`
  104. }
  105. // GetNoMergeIndexByFrequencyCount 获取未合并的指标总数量
  106. func GetNoMergeIndexByFrequencyCount(req GetMaxFileIndexReq) (total float64, err error) {
  107. urlStr := "/mysteel_chemical/getNoMergeIndexByFrequencyCount"
  108. if global.CONFIG.Serve.EdbLibUrl == `` {
  109. err = errors.New("刷新未配置")
  110. return
  111. }
  112. postUrl := global.CONFIG.Serve.EdbLibUrl + urlStr
  113. postData, err := json.Marshal(req)
  114. if err != nil {
  115. return
  116. }
  117. result, err := HttpPost(postUrl, string(postData), "application/json")
  118. if err != nil {
  119. return
  120. }
  121. var resp BaseResponse
  122. global.LOG.Debug(" GetNoMergeIndexByFrequencyCount Result: " + string(result))
  123. err = json.Unmarshal(result, &resp)
  124. if err != nil {
  125. return
  126. }
  127. t, ok := resp.Data.(float64)
  128. if ok {
  129. total = t
  130. }
  131. return
  132. }
  133. // GetMaxFileIndex 获取最大的文件编号下标
  134. func GetMaxFileIndex(req GetMaxFileIndexReq) (baseFromMysteelChemicalIndex index.BaseFromMysteelChemicalIndex, err error) {
  135. urlStr := "/mysteel_chemical/getMaxFileIndex"
  136. if global.CONFIG.Serve.EdbLibUrl == `` {
  137. err = errors.New("刷新未配置")
  138. return
  139. }
  140. postUrl := global.CONFIG.Serve.EdbLibUrl + urlStr
  141. postData, err := json.Marshal(req)
  142. if err != nil {
  143. return
  144. }
  145. result, err := HttpPost(postUrl, string(postData), "application/json")
  146. if err != nil {
  147. return
  148. }
  149. var resp IndexResponse
  150. global.LOG.Debug(" Refresh Result: " + string(result))
  151. err = json.Unmarshal(result, &resp)
  152. if err != nil {
  153. return
  154. }
  155. baseFromMysteelChemicalIndex = resp.Data
  156. return
  157. }
  158. // GetNoMergeIndexByFrequencyPageReq 获取最大文件数请求
  159. type GetNoMergeIndexByFrequencyPageReq struct {
  160. Frequency string
  161. Limit int
  162. }
  163. type GetNoMergeIndexByFrequencyPageReqV2 struct {
  164. Frequency string
  165. EndDate string
  166. StartSize int
  167. PageSize int
  168. TerminalCode string `description:"终端编码"`
  169. }
  170. // GetNoMergeIndexByFrequencyPage 当下需要合并到excel的指标
  171. func GetNoMergeIndexByFrequencyPage(frequency string, limit int) (item []index.BaseFromMysteelChemicalIndex, err error) {
  172. urlStr := "/mysteel_chemical/getNoMergeIndexByFrequencyPage"
  173. if global.CONFIG.Serve.EdbLibUrl == `` {
  174. err = errors.New("刷新未配置")
  175. return
  176. }
  177. postUrl := global.CONFIG.Serve.EdbLibUrl + urlStr
  178. postData, err := json.Marshal(GetNoMergeIndexByFrequencyPageReq{
  179. Frequency: frequency,
  180. Limit: limit,
  181. })
  182. if err != nil {
  183. return
  184. }
  185. result, err := HttpPost(postUrl, string(postData), "application/json")
  186. if err != nil {
  187. return
  188. }
  189. var resp IndexesResponse
  190. global.LOG.Debug(" GetNoMergeIndexByFrequencyPage Result: " + string(result))
  191. err = json.Unmarshal(result, &resp)
  192. if err != nil {
  193. return
  194. }
  195. item = resp.Data
  196. return
  197. }
  198. // GetIndexCountByMergeFilePathReq 获取该文件下标已经存在多少的指标请求体
  199. type GetIndexCountByMergeFilePathReq struct {
  200. MergeFilePath string
  201. }
  202. // GetIndexCountByMergeFilePath 获取该文件下标已经存在多少的指标
  203. func GetIndexCountByMergeFilePath(mergeFilePath string) (total float64, err error) {
  204. if global.CONFIG.Serve.EdbLibUrl == `` {
  205. err = errors.New("刷新未配置")
  206. return
  207. }
  208. if mergeFilePath == "" {
  209. err = fmt.Errorf("文件路径为空")
  210. return
  211. }
  212. baseUrl := "/mysteel_chemical/getIndexCountByMergeFilePath"
  213. url := fmt.Sprint(global.CONFIG.Serve.EdbLibUrl, baseUrl)
  214. postData, err := json.Marshal(GetIndexCountByMergeFilePathReq{
  215. MergeFilePath: mergeFilePath,
  216. })
  217. result, err := HttpPost(url, string(postData), "application/json")
  218. if err != nil {
  219. return
  220. }
  221. var resp BaseResponse
  222. global.LOG.Debug(" GetIndexCountByMergeFilePath Result: " + string(result))
  223. err = json.Unmarshal(result, &resp)
  224. if err != nil {
  225. return
  226. }
  227. t, ok := resp.Data.(float64)
  228. if ok {
  229. total = t
  230. }
  231. return
  232. }
  233. // GetIndexCreate 获取两分钟前新增的指标
  234. func GetIndexCreate() (items []index.BaseFromMysteelChemicalIndex, err error) {
  235. if global.CONFIG.Serve.EdbLibUrl == `` {
  236. err = errors.New("刷新未配置")
  237. return
  238. }
  239. baseUrl := "/mysteel_chemical/getIndexCreate?TerminalCode=" + global.CONFIG.Serve.TerminalCode
  240. url := fmt.Sprint(global.CONFIG.Serve.EdbLibUrl, baseUrl)
  241. result, err := HttpPost(url, "", "application/json")
  242. if err != nil {
  243. return
  244. }
  245. var resp IndexesResponse
  246. global.LOG.Debug(" GetIndexCreate Result: " + string(result))
  247. err = json.Unmarshal(result, &resp)
  248. if err != nil {
  249. return
  250. }
  251. items = resp.Data
  252. return
  253. }
  254. // GetIndexRefreshAllByMergeFile 获取全部刷新所需文件
  255. func GetIndexRefreshAllByMergeFile() (items []index.BaseFromMysteelChemicalIndex, err error) {
  256. if global.CONFIG.Serve.EdbLibUrl == `` {
  257. err = errors.New("刷新未配置")
  258. return
  259. }
  260. baseUrl := "/mysteel_chemical/query/refresh"
  261. url := fmt.Sprint(global.CONFIG.Serve.EdbLibUrl, baseUrl)
  262. result, err := HttpPost(url, "", "application/json")
  263. if err != nil {
  264. return
  265. }
  266. var resp IndexesResponse
  267. global.LOG.Debug(" GetIndexRefreshAllByMergeFile Result: " + string(result))
  268. err = json.Unmarshal(result, &resp)
  269. if err != nil {
  270. return
  271. }
  272. items = resp.Data
  273. return
  274. }
  275. // GetIndexRefreshMethanolByMergeFile 获取需要刷新的甲醇文件
  276. func GetIndexRefreshMethanolByMergeFile() (items []index.BaseFromMysteelChemicalIndex, err error) {
  277. if global.CONFIG.Serve.EdbLibUrl == `` {
  278. err = errors.New("刷新未配置")
  279. return
  280. }
  281. baseUrl := "/mysteel_chemical/getIndexRefreshMethanolByMergeFile"
  282. url := fmt.Sprint(global.CONFIG.Serve.EdbLibUrl, baseUrl)
  283. result, err := HttpPost(url, "", "application/json")
  284. if err != nil {
  285. return
  286. }
  287. var resp IndexesResponse
  288. global.LOG.Debug(" GetIndexRefreshMethanolByMergeFile Result: " + string(result))
  289. err = json.Unmarshal(result, &resp)
  290. if err != nil {
  291. return
  292. }
  293. items = resp.Data
  294. return
  295. }
  296. // GetIndexRefreshMethanolByTimely 获取需要及时刷新的文件
  297. func GetIndexRefreshMethanolByTimely() (items []index.BaseFromMysteelChemicalIndex, err error) {
  298. if global.CONFIG.Serve.EdbLibUrl == `` {
  299. err = errors.New("刷新未配置")
  300. return
  301. }
  302. baseUrl := "/mysteel_chemical/getIndexRefreshMethanolByTimely"
  303. url := fmt.Sprint(global.CONFIG.Serve.EdbLibUrl, baseUrl)
  304. result, err := HttpPost(url, "", "application/json")
  305. if err != nil {
  306. return
  307. }
  308. var resp IndexesResponse
  309. global.LOG.Debug(" GetIndexRefreshMethanolByTimely Result: " + string(result))
  310. err = json.Unmarshal(result, &resp)
  311. if err != nil {
  312. return
  313. }
  314. items = resp.Data
  315. return
  316. }
  317. type GetIndexByFrequencyReq struct {
  318. Frequency string
  319. }
  320. // GetIndexByFrequency 根据频率获取指标
  321. func GetIndexByFrequency(frequency string) (items []index.BaseFromMysteelChemicalIndex, err error) {
  322. if global.CONFIG.Serve.EdbLibUrl == `` {
  323. err = errors.New("刷新未配置")
  324. return
  325. }
  326. if frequency == "" {
  327. err = fmt.Errorf("频率为空")
  328. return
  329. }
  330. baseUrl := "/mysteel_chemical/getIndexByFrequency"
  331. url := fmt.Sprint(global.CONFIG.Serve.EdbLibUrl, baseUrl)
  332. postData, err := json.Marshal(GetIndexByFrequencyReq{
  333. Frequency: frequency,
  334. })
  335. result, err := HttpPost(url, string(postData), "application/json")
  336. if err != nil {
  337. return
  338. }
  339. var resp IndexesResponse
  340. global.LOG.Debug(" GetIndexByFrequency Result: " + string(result))
  341. err = json.Unmarshal(result, &resp)
  342. if err != nil {
  343. return
  344. }
  345. items = resp.Data
  346. return
  347. }
  348. type GetIndexRefreshWeekReq struct {
  349. NowWeekZn string
  350. StartTime string
  351. EndTime string
  352. }
  353. // GetIndexRefreshWeek 根据周获取指标
  354. func GetIndexRefreshWeek(nowWeekZn, startTime, endTime string) (items []index.BaseFromMysteelChemicalIndex, err error) {
  355. if global.CONFIG.Serve.EdbLibUrl == `` {
  356. err = errors.New("刷新未配置")
  357. return
  358. }
  359. if nowWeekZn == "" || startTime == "" || endTime == "" {
  360. err = fmt.Errorf("参数有误, nowWeekZn: %s, startTime: %s, endTime: %s", nowWeekZn, startTime, endTime)
  361. return
  362. }
  363. baseUrl := "/mysteel_chemical/getIndexRefreshWeek"
  364. url := fmt.Sprint(global.CONFIG.Serve.EdbLibUrl, baseUrl)
  365. postData, err := json.Marshal(GetIndexRefreshWeekReq{
  366. NowWeekZn: nowWeekZn,
  367. StartTime: startTime,
  368. EndTime: endTime,
  369. })
  370. result, err := HttpPost(url, string(postData), "application/json")
  371. if err != nil {
  372. return
  373. }
  374. var resp IndexesResponse
  375. global.LOG.Debug(" GetIndexRefreshWeek Result: " + string(result))
  376. err = json.Unmarshal(result, &resp)
  377. if err != nil {
  378. return
  379. }
  380. items = resp.Data
  381. return
  382. }
  383. // UpdateIndexReq 更新指标
  384. type UpdateIndexReq struct {
  385. Item *index.BaseFromMysteelChemicalIndex
  386. UpdateCols []string
  387. }
  388. // MultiUpdateIndex 批量更新指标
  389. func MultiUpdateIndex(req []UpdateIndexReq) (err error) {
  390. if global.CONFIG.Serve.EdbLibUrl == `` {
  391. err = errors.New("刷新未配置")
  392. return
  393. }
  394. if len(req) == 0 {
  395. err = fmt.Errorf("无指标需要更新")
  396. return
  397. }
  398. urlStr := "/mysteel_chemical/multiUpdateIndex"
  399. postUrl := global.CONFIG.Serve.EdbLibUrl + urlStr
  400. postData, err := json.Marshal(req)
  401. if err != nil {
  402. return
  403. }
  404. result, err := HttpPost(postUrl, string(postData), "application/json")
  405. if err != nil {
  406. return
  407. }
  408. var resp BaseResponse
  409. global.LOG.Debug(" MultiUpdateIndex Result: " + string(result))
  410. err = json.Unmarshal(result, &resp)
  411. if err != nil {
  412. return
  413. }
  414. if resp.Ret != 200 {
  415. err = fmt.Errorf(resp.ErrMsg)
  416. }
  417. return
  418. }
  419. // RefreshConfigResponse
  420. // @Description: 获取刷新配置列表
  421. type RefreshConfigResponse struct {
  422. Ret int
  423. Msg string
  424. ErrMsg string
  425. ErrCode string
  426. Data []EdbRefreshDefaultConfig
  427. Success bool `description:"true 执行成功,false 执行失败"`
  428. IsSendEmail bool `json:"-" description:"true 发送邮件,false 不发送邮件"`
  429. IsAddLog bool `json:"-" description:"true 新增操作日志,false 不新增操作日志" `
  430. }
  431. // EdbRefreshDefaultConfig
  432. // @Description: 指标的默认刷新时间配置
  433. type EdbRefreshDefaultConfig struct {
  434. Id int `orm:"column(id);pk"`
  435. Source int `description:"来源"`
  436. SubSource int `description:"来源名称"`
  437. Frequency string `description:"频度"`
  438. RefreshFrequency string `description:"刷新频率"`
  439. RefreshFrequencyDay int `description:"具体刷新的日期"`
  440. RefreshTime string `description:"刷新时间"`
  441. RefreshAllData int `description:"是否刷新所有数据,0:否,1:刷新所有数据"`
  442. RefreshDataNum int `description:"刷新单元格数"`
  443. ModifyTime time.Time `description:"最晚一次的更新时间"`
  444. CreateTime time.Time `description:"添加时间"`
  445. }
  446. // GetEdbRefreshConfigReq
  447. // @Description: 获取默认配置的请求参数
  448. type GetEdbRefreshConfigReq struct {
  449. Source int `description:"指标来源ID"`
  450. SubSource int `description:"子来源"`
  451. }
  452. // GetRefreshConfigList 获取刷新配置列表
  453. func GetRefreshConfigList() (resp *RefreshConfigResponse, err error) {
  454. urlStr := "/edb_refresh/config/default/list"
  455. if global.CONFIG.Serve.EdbLibUrl == `` {
  456. err = errors.New("刷新未配置")
  457. return
  458. }
  459. postUrl := global.CONFIG.Serve.EdbLibUrl + urlStr
  460. postData, err := json.Marshal(GetEdbRefreshConfigReq{
  461. Source: utils.DATA_SOURCE_MYSTEEL_CHEMICAL,
  462. SubSource: 0,
  463. })
  464. if err != nil {
  465. return
  466. }
  467. result, err := HttpPost(postUrl, string(postData), "application/json")
  468. if err != nil {
  469. return
  470. }
  471. if result != nil {
  472. global.LOG.Debug(" GetRefreshConfigList Result: " + string(result))
  473. err = json.Unmarshal(result, &resp)
  474. if err != nil {
  475. return
  476. }
  477. return resp, nil
  478. }
  479. return nil, err
  480. }
  481. type RefreshCheckByDayReq struct {
  482. Source int `description:"来源id"`
  483. LatestDate string `description:"数据最新日期"`
  484. FrequencyBatch string `description:"更新频度"`
  485. }
  486. type RefreshCheckByDayData struct {
  487. UpdateNum int `description:"已更新指标数"`
  488. UnUpdateNum int `description:"未更新指标数"`
  489. }
  490. type RefreshCheckByDayResponse struct {
  491. Ret int
  492. Msg string
  493. ErrMsg string
  494. ErrCode string
  495. Data RefreshCheckByDayData
  496. Success bool `description:"true 执行成功,false 执行失败"`
  497. }
  498. // RefreshCheckByDay 每日更新检查
  499. func RefreshCheckByDay(req RefreshCheckByDayReq) (data RefreshCheckByDayData, err error) {
  500. if global.CONFIG.Serve.EdbLibUrl == `` {
  501. err = errors.New("刷新未配置")
  502. return
  503. }
  504. urlStr := "/edb_info/refresh_check"
  505. postUrl := global.CONFIG.Serve.EdbLibUrl + urlStr
  506. postData, err := json.Marshal(req)
  507. if err != nil {
  508. return
  509. }
  510. result, err := HttpPost(postUrl, string(postData), "application/json")
  511. if err != nil {
  512. return
  513. }
  514. var resp RefreshCheckByDayResponse
  515. global.LOG.Debug(" RefreshCheckByDay Result: " + string(result))
  516. err = json.Unmarshal(result, &resp)
  517. if err != nil {
  518. return
  519. }
  520. if resp.Ret != 200 {
  521. err = fmt.Errorf(resp.ErrMsg)
  522. }
  523. data = resp.Data
  524. return
  525. }
  526. func HttpPost(url, postData string, params ...string) ([]byte, error) {
  527. body := ioutil.NopCloser(strings.NewReader(postData))
  528. client := &http.Client{}
  529. req, err := http.NewRequest("POST", url, body)
  530. if err != nil {
  531. return nil, err
  532. }
  533. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  534. if len(params) > 0 && params[0] != "" {
  535. contentType = params[0]
  536. }
  537. req.Header.Set("Content-Type", contentType)
  538. req.Header.Set("authorization", utils.MD5(global.CONFIG.Serve.AppEdbLibNameEn+global.CONFIG.Serve.EdbLibMd5Key))
  539. resp, err := client.Do(req)
  540. if resp != nil {
  541. defer resp.Body.Close()
  542. b, err := ioutil.ReadAll(resp.Body)
  543. //fmt.Println("HttpPost:" + string(b))
  544. return b, err
  545. }
  546. return nil, err
  547. }