123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619 |
- package watch
- import (
- "encoding/json"
- "errors"
- "eta/mysteel_watch/global"
- "eta/mysteel_watch/models/index"
- "eta/mysteel_watch/utils"
- "fmt"
- "io/ioutil"
- "net/http"
- "strings"
- "time"
- )
- type BaseResponse struct {
- Ret int
- Msg string
- ErrMsg string
- ErrCode string
- Data interface{}
- Success bool `description:"true 执行成功,false 执行失败"`
- IsSendEmail bool `json:"-" description:"true 发送邮件,false 不发送邮件"`
- IsAddLog bool `json:"-" description:"true 新增操作日志,false 不新增操作日志" `
- }
- type IndexesResponse struct {
- Ret int
- Msg string
- ErrMsg string
- ErrCode string
- Data []index.BaseFromMysteelChemicalIndex
- Success bool `description:"true 执行成功,false 执行失败"`
- IsSendEmail bool `json:"-" description:"true 发送邮件,false 不发送邮件"`
- IsAddLog bool `json:"-" description:"true 新增操作日志,false 不新增操作日志" `
- }
- type IndexResponse struct {
- Ret int
- Msg string
- ErrMsg string
- ErrCode string
- Data index.BaseFromMysteelChemicalIndex
- Success bool `description:"true 执行成功,false 执行失败"`
- IsSendEmail bool `json:"-" description:"true 发送邮件,false 不发送邮件"`
- IsAddLog bool `json:"-" description:"true 新增操作日志,false 不新增操作日志" `
- }
- // postRefreshEdbData 刷新指标数据
- //func postRefreshEdbData(param map[string]interface{}) (resp *BaseResponse, err error) {
- // urlStr := "mysteel_chemical/refresh"
- //
- // if global.CONFIG.Serve.EdbLibUrl == `` {
- // err = errors.New("刷新未配置")
- // return
- // }
- // postUrl := global.CONFIG.Serve.EdbLibUrl + urlStr
- // postData, err := json.Marshal(param)
- // if err != nil {
- // return
- // }
- // result, err := HttpPost(postUrl, string(postData), "application/json")
- // if err != nil {
- // return
- // }
- // if result != nil {
- // global.LOG.Info(" Refresh Result: " + string(result))
- // err = json.Unmarshal(result, &resp)
- // if err != nil {
- // return
- // }
- // return resp, nil
- // }
- // return nil, err
- //}
- // postHandleMysteelIndex 处理指标和指标数据
- func postHandleMysteelIndex(req *HandleMysteelIndexReq) (resp *BaseResponse, err error) {
- urlStr := "/mysteel_chemical/handle/mysteel/index"
- if global.CONFIG.Serve.EdbLibUrl == `` {
- err = errors.New("刷新未配置")
- return
- }
- postUrl := global.CONFIG.Serve.EdbLibUrl + urlStr
- postData, err := json.Marshal(req)
- if err != nil {
- return
- }
- //fmt.Println("postData:" + string(postData))
- global.LOG.Debug(" postData: " + string(postData))
- result, err := HttpPost(postUrl, string(postData), "application/json")
- if err != nil {
- return
- }
- if result != nil {
- global.LOG.Debug(" postHandleMysteelIndex Result: " + string(result))
- err = json.Unmarshal(result, &resp)
- if err != nil {
- return
- }
- return resp, nil
- }
- return nil, err
- }
- // GetMaxFileIndexReq 获取最大文件数请求
- type GetMaxFileIndexReq struct {
- Frequency string
- EndDate string
- TerminalCode string `description:"终端编码"`
- }
- // GetNoMergeIndexByFrequencyCount 获取未合并的指标总数量
- func GetNoMergeIndexByFrequencyCount(req GetMaxFileIndexReq) (total float64, err error) {
- urlStr := "/mysteel_chemical/getNoMergeIndexByFrequencyCount"
- if global.CONFIG.Serve.EdbLibUrl == `` {
- err = errors.New("刷新未配置")
- return
- }
- postUrl := global.CONFIG.Serve.EdbLibUrl + urlStr
- postData, err := json.Marshal(req)
- if err != nil {
- return
- }
- result, err := HttpPost(postUrl, string(postData), "application/json")
- if err != nil {
- return
- }
- var resp BaseResponse
- global.LOG.Debug(" GetNoMergeIndexByFrequencyCount Result: " + string(result))
- err = json.Unmarshal(result, &resp)
- if err != nil {
- return
- }
- t, ok := resp.Data.(float64)
- if ok {
- total = t
- }
- return
- }
- // GetMaxFileIndex 获取最大的文件编号下标
- func GetMaxFileIndex(req GetMaxFileIndexReq) (baseFromMysteelChemicalIndex index.BaseFromMysteelChemicalIndex, err error) {
- urlStr := "/mysteel_chemical/getMaxFileIndex"
- if global.CONFIG.Serve.EdbLibUrl == `` {
- err = errors.New("刷新未配置")
- return
- }
- postUrl := global.CONFIG.Serve.EdbLibUrl + urlStr
- postData, err := json.Marshal(req)
- if err != nil {
- return
- }
- result, err := HttpPost(postUrl, string(postData), "application/json")
- if err != nil {
- return
- }
- var resp IndexResponse
- global.LOG.Debug(" Refresh Result: " + string(result))
- err = json.Unmarshal(result, &resp)
- if err != nil {
- return
- }
- baseFromMysteelChemicalIndex = resp.Data
- return
- }
- // GetNoMergeIndexByFrequencyPageReq 获取最大文件数请求
- type GetNoMergeIndexByFrequencyPageReq struct {
- Frequency string
- Limit int
- }
- type GetNoMergeIndexByFrequencyPageReqV2 struct {
- Frequency string
- EndDate string
- StartSize int
- PageSize int
- TerminalCode string `description:"终端编码"`
- }
- // GetNoMergeIndexByFrequencyPage 当下需要合并到excel的指标
- func GetNoMergeIndexByFrequencyPage(frequency string, limit int) (item []index.BaseFromMysteelChemicalIndex, err error) {
- urlStr := "/mysteel_chemical/getNoMergeIndexByFrequencyPage"
- if global.CONFIG.Serve.EdbLibUrl == `` {
- err = errors.New("刷新未配置")
- return
- }
- postUrl := global.CONFIG.Serve.EdbLibUrl + urlStr
- postData, err := json.Marshal(GetNoMergeIndexByFrequencyPageReq{
- Frequency: frequency,
- Limit: limit,
- })
- if err != nil {
- return
- }
- result, err := HttpPost(postUrl, string(postData), "application/json")
- if err != nil {
- return
- }
- var resp IndexesResponse
- global.LOG.Debug(" GetNoMergeIndexByFrequencyPage Result: " + string(result))
- err = json.Unmarshal(result, &resp)
- if err != nil {
- return
- }
- item = resp.Data
- return
- }
- // GetIndexCountByMergeFilePathReq 获取该文件下标已经存在多少的指标请求体
- type GetIndexCountByMergeFilePathReq struct {
- MergeFilePath string
- }
- // GetIndexCountByMergeFilePath 获取该文件下标已经存在多少的指标
- func GetIndexCountByMergeFilePath(mergeFilePath string) (total float64, err error) {
- if global.CONFIG.Serve.EdbLibUrl == `` {
- err = errors.New("刷新未配置")
- return
- }
- if mergeFilePath == "" {
- err = fmt.Errorf("文件路径为空")
- return
- }
- baseUrl := "/mysteel_chemical/getIndexCountByMergeFilePath"
- url := fmt.Sprint(global.CONFIG.Serve.EdbLibUrl, baseUrl)
- postData, err := json.Marshal(GetIndexCountByMergeFilePathReq{
- MergeFilePath: mergeFilePath,
- })
- result, err := HttpPost(url, string(postData), "application/json")
- if err != nil {
- return
- }
- var resp BaseResponse
- global.LOG.Debug(" GetIndexCountByMergeFilePath Result: " + string(result))
- err = json.Unmarshal(result, &resp)
- if err != nil {
- return
- }
- t, ok := resp.Data.(float64)
- if ok {
- total = t
- }
- return
- }
- // GetIndexCreate 获取两分钟前新增的指标
- func GetIndexCreate() (items []index.BaseFromMysteelChemicalIndex, err error) {
- if global.CONFIG.Serve.EdbLibUrl == `` {
- err = errors.New("刷新未配置")
- return
- }
- baseUrl := "/mysteel_chemical/getIndexCreate?TerminalCode=" + global.CONFIG.Serve.TerminalCode
- url := fmt.Sprint(global.CONFIG.Serve.EdbLibUrl, baseUrl)
- result, err := HttpPost(url, "", "application/json")
- if err != nil {
- return
- }
- var resp IndexesResponse
- global.LOG.Debug(" GetIndexCreate Result: " + string(result))
- err = json.Unmarshal(result, &resp)
- if err != nil {
- return
- }
- items = resp.Data
- return
- }
- // GetIndexRefreshAllByMergeFile 获取全部刷新所需文件
- func GetIndexRefreshAllByMergeFile() (items []index.BaseFromMysteelChemicalIndex, err error) {
- if global.CONFIG.Serve.EdbLibUrl == `` {
- err = errors.New("刷新未配置")
- return
- }
- baseUrl := "/mysteel_chemical/query/refresh"
- url := fmt.Sprint(global.CONFIG.Serve.EdbLibUrl, baseUrl)
- result, err := HttpPost(url, "", "application/json")
- if err != nil {
- return
- }
- var resp IndexesResponse
- global.LOG.Debug(" GetIndexRefreshAllByMergeFile Result: " + string(result))
- err = json.Unmarshal(result, &resp)
- if err != nil {
- return
- }
- items = resp.Data
- return
- }
- // GetIndexRefreshMethanolByMergeFile 获取需要刷新的甲醇文件
- func GetIndexRefreshMethanolByMergeFile() (items []index.BaseFromMysteelChemicalIndex, err error) {
- if global.CONFIG.Serve.EdbLibUrl == `` {
- err = errors.New("刷新未配置")
- return
- }
- baseUrl := "/mysteel_chemical/getIndexRefreshMethanolByMergeFile"
- url := fmt.Sprint(global.CONFIG.Serve.EdbLibUrl, baseUrl)
- result, err := HttpPost(url, "", "application/json")
- if err != nil {
- return
- }
- var resp IndexesResponse
- global.LOG.Debug(" GetIndexRefreshMethanolByMergeFile Result: " + string(result))
- err = json.Unmarshal(result, &resp)
- if err != nil {
- return
- }
- items = resp.Data
- return
- }
- // GetIndexRefreshMethanolByTimely 获取需要及时刷新的文件
- func GetIndexRefreshMethanolByTimely() (items []index.BaseFromMysteelChemicalIndex, err error) {
- if global.CONFIG.Serve.EdbLibUrl == `` {
- err = errors.New("刷新未配置")
- return
- }
- baseUrl := "/mysteel_chemical/getIndexRefreshMethanolByTimely"
- url := fmt.Sprint(global.CONFIG.Serve.EdbLibUrl, baseUrl)
- result, err := HttpPost(url, "", "application/json")
- if err != nil {
- return
- }
- var resp IndexesResponse
- global.LOG.Debug(" GetIndexRefreshMethanolByTimely Result: " + string(result))
- err = json.Unmarshal(result, &resp)
- if err != nil {
- return
- }
- items = resp.Data
- return
- }
- type GetIndexByFrequencyReq struct {
- Frequency string
- }
- // GetIndexByFrequency 根据频率获取指标
- func GetIndexByFrequency(frequency string) (items []index.BaseFromMysteelChemicalIndex, err error) {
- if global.CONFIG.Serve.EdbLibUrl == `` {
- err = errors.New("刷新未配置")
- return
- }
- if frequency == "" {
- err = fmt.Errorf("频率为空")
- return
- }
- baseUrl := "/mysteel_chemical/getIndexByFrequency"
- url := fmt.Sprint(global.CONFIG.Serve.EdbLibUrl, baseUrl)
- postData, err := json.Marshal(GetIndexByFrequencyReq{
- Frequency: frequency,
- })
- result, err := HttpPost(url, string(postData), "application/json")
- if err != nil {
- return
- }
- var resp IndexesResponse
- global.LOG.Debug(" GetIndexByFrequency Result: " + string(result))
- err = json.Unmarshal(result, &resp)
- if err != nil {
- return
- }
- items = resp.Data
- return
- }
- type GetIndexRefreshWeekReq struct {
- NowWeekZn string
- StartTime string
- EndTime string
- }
- // GetIndexRefreshWeek 根据周获取指标
- func GetIndexRefreshWeek(nowWeekZn, startTime, endTime string) (items []index.BaseFromMysteelChemicalIndex, err error) {
- if global.CONFIG.Serve.EdbLibUrl == `` {
- err = errors.New("刷新未配置")
- return
- }
- if nowWeekZn == "" || startTime == "" || endTime == "" {
- err = fmt.Errorf("参数有误, nowWeekZn: %s, startTime: %s, endTime: %s", nowWeekZn, startTime, endTime)
- return
- }
- baseUrl := "/mysteel_chemical/getIndexRefreshWeek"
- url := fmt.Sprint(global.CONFIG.Serve.EdbLibUrl, baseUrl)
- postData, err := json.Marshal(GetIndexRefreshWeekReq{
- NowWeekZn: nowWeekZn,
- StartTime: startTime,
- EndTime: endTime,
- })
- result, err := HttpPost(url, string(postData), "application/json")
- if err != nil {
- return
- }
- var resp IndexesResponse
- global.LOG.Debug(" GetIndexRefreshWeek Result: " + string(result))
- err = json.Unmarshal(result, &resp)
- if err != nil {
- return
- }
- items = resp.Data
- return
- }
- // UpdateIndexReq 更新指标
- type UpdateIndexReq struct {
- Item *index.BaseFromMysteelChemicalIndex
- UpdateCols []string
- }
- // MultiUpdateIndex 批量更新指标
- func MultiUpdateIndex(req []UpdateIndexReq) (err error) {
- if global.CONFIG.Serve.EdbLibUrl == `` {
- err = errors.New("刷新未配置")
- return
- }
- if len(req) == 0 {
- err = fmt.Errorf("无指标需要更新")
- return
- }
- urlStr := "/mysteel_chemical/multiUpdateIndex"
- postUrl := global.CONFIG.Serve.EdbLibUrl + urlStr
- postData, err := json.Marshal(req)
- if err != nil {
- return
- }
- result, err := HttpPost(postUrl, string(postData), "application/json")
- if err != nil {
- return
- }
- var resp BaseResponse
- global.LOG.Debug(" MultiUpdateIndex Result: " + string(result))
- err = json.Unmarshal(result, &resp)
- if err != nil {
- return
- }
- if resp.Ret != 200 {
- err = fmt.Errorf(resp.ErrMsg)
- }
- return
- }
- // RefreshConfigResponse
- // @Description: 获取刷新配置列表
- type RefreshConfigResponse struct {
- Ret int
- Msg string
- ErrMsg string
- ErrCode string
- Data []EdbRefreshDefaultConfig
- Success bool `description:"true 执行成功,false 执行失败"`
- IsSendEmail bool `json:"-" description:"true 发送邮件,false 不发送邮件"`
- IsAddLog bool `json:"-" description:"true 新增操作日志,false 不新增操作日志" `
- }
- // EdbRefreshDefaultConfig
- // @Description: 指标的默认刷新时间配置
- type EdbRefreshDefaultConfig struct {
- Id int `orm:"column(id);pk"`
- Source int `description:"来源"`
- SubSource int `description:"来源名称"`
- Frequency string `description:"频度"`
- RefreshFrequency string `description:"刷新频率"`
- RefreshFrequencyDay int `description:"具体刷新的日期"`
- RefreshTime string `description:"刷新时间"`
- RefreshAllData int `description:"是否刷新所有数据,0:否,1:刷新所有数据"`
- RefreshDataNum int `description:"刷新单元格数"`
- ModifyTime time.Time `description:"最晚一次的更新时间"`
- CreateTime time.Time `description:"添加时间"`
- }
- // GetEdbRefreshConfigReq
- // @Description: 获取默认配置的请求参数
- type GetEdbRefreshConfigReq struct {
- Source int `description:"指标来源ID"`
- SubSource int `description:"子来源"`
- }
- // GetRefreshConfigList 获取刷新配置列表
- func GetRefreshConfigList() (resp *RefreshConfigResponse, err error) {
- urlStr := "/edb_refresh/config/default/list"
- if global.CONFIG.Serve.EdbLibUrl == `` {
- err = errors.New("刷新未配置")
- return
- }
- postUrl := global.CONFIG.Serve.EdbLibUrl + urlStr
- postData, err := json.Marshal(GetEdbRefreshConfigReq{
- Source: utils.DATA_SOURCE_MYSTEEL_CHEMICAL,
- SubSource: 0,
- })
- if err != nil {
- return
- }
- result, err := HttpPost(postUrl, string(postData), "application/json")
- if err != nil {
- return
- }
- if result != nil {
- global.LOG.Debug(" GetRefreshConfigList Result: " + string(result))
- err = json.Unmarshal(result, &resp)
- if err != nil {
- return
- }
- return resp, nil
- }
- return nil, err
- }
- type RefreshCheckByDayReq struct {
- Source int `description:"来源id"`
- LatestDate string `description:"数据最新日期"`
- FrequencyBatch string `description:"更新频度"`
- }
- type RefreshCheckByDayData struct {
- UpdateNum int `description:"已更新指标数"`
- UnUpdateNum int `description:"未更新指标数"`
- }
- type RefreshCheckByDayResponse struct {
- Ret int
- Msg string
- ErrMsg string
- ErrCode string
- Data RefreshCheckByDayData
- Success bool `description:"true 执行成功,false 执行失败"`
- }
- // RefreshCheckByDay 每日更新检查
- func RefreshCheckByDay(req RefreshCheckByDayReq) (data RefreshCheckByDayData, err error) {
- if global.CONFIG.Serve.EdbLibUrl == `` {
- err = errors.New("刷新未配置")
- return
- }
- urlStr := "/edb_info/refresh_check"
- postUrl := global.CONFIG.Serve.EdbLibUrl + urlStr
- postData, err := json.Marshal(req)
- if err != nil {
- return
- }
- result, err := HttpPost(postUrl, string(postData), "application/json")
- if err != nil {
- return
- }
- var resp RefreshCheckByDayResponse
- global.LOG.Debug(" RefreshCheckByDay Result: " + string(result))
- err = json.Unmarshal(result, &resp)
- if err != nil {
- return
- }
- if resp.Ret != 200 {
- err = fmt.Errorf(resp.ErrMsg)
- }
- data = resp.Data
- return
- }
- func HttpPost(url, postData string, params ...string) ([]byte, error) {
- body := ioutil.NopCloser(strings.NewReader(postData))
- client := &http.Client{}
- req, err := http.NewRequest("POST", url, body)
- if err != nil {
- return nil, err
- }
- contentType := "application/x-www-form-urlencoded;charset=utf-8"
- if len(params) > 0 && params[0] != "" {
- contentType = params[0]
- }
- req.Header.Set("Content-Type", contentType)
- req.Header.Set("authorization", utils.MD5(global.CONFIG.Serve.AppEdbLibNameEn+global.CONFIG.Serve.EdbLibMd5Key))
- resp, err := client.Do(req)
- if resp != nil {
- defer resp.Body.Close()
- b, err := ioutil.ReadAll(resp.Body)
- //fmt.Println("HttpPost:" + string(b))
- return b, err
- }
- return nil, err
- }
|