123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794 |
- package data_manage
- import (
- "encoding/json"
- "github.com/rdlucklib/rdluck_tools/paging"
- "hongze/hz_crm_api/controllers"
- "hongze/hz_crm_api/models"
- "hongze/hz_crm_api/models/data_manage"
- "hongze/hz_crm_api/models/data_manage/request"
- "hongze/hz_crm_api/models/data_manage/response"
- "hongze/hz_crm_api/services/data"
- "hongze/hz_crm_api/services/elastic"
- "hongze/hz_crm_api/utils"
- "sort"
- "strconv"
- "strings"
- "time"
- )
- // PredictEdbInfoController 预测指标
- type PredictEdbInfoController struct {
- controllers.BaseAuthController
- }
- // EdbChartList
- // @Title 预测指标列表接口
- // @Description 预测指标列表接口
- // @Param PageSize query int true "每页数据条数"
- // @Param CurrentIndex query int true "当前页页码,从1开始"
- // @Param ClassifyId query int true "分类id"
- // @Param Keyword query string true "搜索关键词"
- // @Param AdminId query int false "创建人id"
- // @Param IsOnlyMe query bool false "是否只看我的"
- // @Success 200 {object} response.PredictEdbInfoListResp
- // @router /predict_edb_info/chart/list [get]
- func (this *PredictEdbInfoController) EdbChartList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- classifyId, _ := this.GetInt("ClassifyId")
- pageSize, _ := this.GetInt("PageSize")
- currentIndex, _ := this.GetInt("CurrentIndex")
- keyword := this.GetString("Keyword")
- adminId, _ := this.GetInt("AdminId")
- isOnlyMe, _ := this.GetBool("IsOnlyMe")
- var total int
- page := paging.GetPaging(currentIndex, pageSize, total)
- var startSize int
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize = paging.StartIndex(currentIndex, pageSize)
- var condition string
- var pars []interface{}
- // 预测指标
- condition += ` AND edb_info_type = ? `
- pars = append(pars, 1)
- // 筛选分类
- if classifyId > 0 {
- _, err := data_manage.GetEdbClassifyById(classifyId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取分类信息失败"
- br.ErrMsg = "获取分类信息失败,GetEdbClassifyById,Err:" + err.Error()
- return
- }
- condition += " AND classify_id = ? "
- pars = append(pars, classifyId)
- }
- if keyword != "" {
- condition += ` AND ( excel_name LIKE ? )`
- pars = append(pars, `%`+keyword+`%`)
- }
- if adminId > 0 {
- condition += " AND sys_user_id = ? "
- pars = append(pars, adminId)
- }
- if isOnlyMe {
- condition += " AND sys_user_id = ? "
- pars = append(pars, sysUser.AdminId)
- }
- // 获取当前账号的不可见指标
- obj := data_manage.EdbInfoNoPermissionAdmin{}
- confList, err := obj.GetAllListByAdminId(this.SysUser.AdminId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取失败"
- br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
- return
- }
- noPermissionEdbInfoIdList := make([]int, 0)
- for _, v := range confList {
- noPermissionEdbInfoIdList = append(noPermissionEdbInfoIdList, v.EdbInfoId)
- }
- noPermissionEdbInfoIdNum := len(noPermissionEdbInfoIdList)
- if noPermissionEdbInfoIdNum > 0 {
- condition += ` AND edb_info_id not in (` + utils.GetOrmInReplace(noPermissionEdbInfoIdNum) + `) `
- pars = append(pars, noPermissionEdbInfoIdList)
- }
- //获取指标信息
- dataCount, list, err := data_manage.GetEdbInfoFilterList(condition, pars, startSize, pageSize)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Success = true
- br.Msg = "获取预测指标列表失败"
- br.ErrMsg = "获取预测指标列表失败,Err:" + err.Error()
- return
- }
- if list == nil || len(list) <= 0 || (err != nil && err.Error() == utils.ErrNoRow()) {
- list = make([]*data_manage.EdbInfoList, 0)
- } else {
- page = paging.GetPaging(currentIndex, pageSize, int(dataCount))
- }
- resp := response.PredictEdbInfoListResp{
- Paging: page,
- List: list,
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // List
- // @Title 指标数据列表接口
- // @Description 指标数据列表接口
- // @Param PageSize query int true "每页数据条数"
- // @Param CurrentIndex query int true "当前页页码,从1开始"
- // @Param EdbInfoId query int true "指标id"
- // @Param KeyWord query string false "搜索关键词:指标ID/指标名称"
- // @Success 200 {object} data_manage.EdbInfoListResp
- // @router /predict_edb_info/list [get]
- func (this *PredictEdbInfoController) List() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- edbInfoId, _ := this.GetInt("EdbInfoId")
- keyWord := this.GetString("KeyWord")
- pageSize, _ := this.GetInt("PageSize")
- currentIndex, _ := this.GetInt("CurrentIndex")
- var total int
- page := paging.GetPaging(currentIndex, pageSize, total)
- var startSize int
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize = paging.StartIndex(currentIndex, pageSize)
- var condition string
- var pars []interface{}
- // 指标类型,0:普通指标,1:预测指标
- condition += " AND edb_info_type=? "
- pars = append(pars, 1)
- if edbInfoId > 0 {
- condition += " AND edb_info_id=? "
- pars = append(pars, edbInfoId)
- }
- if keyWord != "" {
- condition += ` AND ( edb_code LIKE '%` + keyWord + `%' OR edb_name LIKE '%` + keyWord + `%' )`
- //pars = append(pars, keyWord)
- //pars = append(pars, keyWord)
- }
- resp := data_manage.EdbInfoListResp{}
- if condition == "" {
- item := new(data_manage.EdbInfoList)
- resp.Paging = page
- resp.Item = item
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- return
- }
- //获取指标信息
- edbInfoItem, err := data_manage.GetEdbInfoByCondition(condition, pars)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Success = true
- br.Msg = "获取指标信息失败"
- br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
- return
- }
- if edbInfoItem == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
- item := new(data_manage.EdbInfoList)
- resp.Paging = page
- resp.Item = item
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- return
- }
- //获取英文频度
- edbInfoItem.FrequencyEn = data.GetFrequencyEn(edbInfoItem.Frequency)
- //查询目录
- resultList, _ := data_manage.GetClassifyAllByClassifyId(edbInfoItem.ClassifyId)
- // 未来的指标预测数据
- predictDataList := make([]*data_manage.EdbData, 0)
- var existCondition string
- var existPars []interface{}
- existCondition += " AND edb_info_id=? "
- existPars = append(existPars, edbInfoId)
- //查询出所有的关联指标
- sourceEdbInfoCalculateMappingList, err := data_manage.GetEdbInfoCalculateListByCondition(existCondition, existPars)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取预测指标关联信息失败,Err:" + err.Error()
- return
- }
- if len(sourceEdbInfoCalculateMappingList) <= 0 {
- br.Msg = "获取失败"
- br.ErrMsg = "查找预测指标关联信息失败"
- return
- }
- if edbInfoItem.EdbType == 1 { //普通的预测指标
- // 查找该预测指标配置
- predictEdbConfList, err := data_manage.GetPredictEdbConfListById(edbInfoId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取失败"
- br.ErrMsg = "获取预测指标配置信息失败,Err:" + err.Error()
- return
- }
- if len(predictEdbConfList) == 0 {
- br.Msg = "找不到该预测指标配置"
- br.ErrMsg = "找不到该预测指标配置"
- return
- }
- predictEdbConf := predictEdbConfList[0]
- //预测数据的配置
- edbInfoItem.RuleType = predictEdbConf.RuleType
- edbInfoItem.FixedValue = predictEdbConf.FixedValue
- // 来源指标
- //sourceEdbInfoId := sourceEdbInfoCalculateMappingList[0].FromEdbInfoId
- sourceEdbInfoId := predictEdbConf.SourceEdbInfoId
- sourceEdbInfoItem, err := data_manage.GetEdbInfoById(sourceEdbInfoId)
- if err != nil {
- br.Msg = "获取来源指标信息失败"
- br.ErrMsg = "获取来源指标信息失败"
- if err.Error() != utils.ErrNoRow() {
- br.ErrMsg = "获取来源指标信息失败,Err:" + err.Error()
- }
- br.Success = true
- return
- }
- //获取指标数据(实际已生成)
- {
- var dataCondition string
- var dataPars []interface{}
- dataCondition += ` AND edb_info_id=? `
- dataPars = append(dataPars, sourceEdbInfoItem.EdbInfoId)
- dataCount, err := data_manage.GetEdbDataCountByCondition(dataCondition, dataPars, sourceEdbInfoItem.Source)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取指标信息失败"
- br.ErrMsg = "获取指标数据总数失败,Err:" + err.Error()
- return
- }
- page = paging.GetPaging(currentIndex, pageSize, dataCount)
- dataList, err := data_manage.GetEdbDataListByCondition(dataCondition, dataPars, sourceEdbInfoItem.Source, pageSize, startSize)
- if err != nil {
- br.Msg = "获取指标信息失败"
- br.ErrMsg = "获取指标数据失败,Err:" + err.Error()
- return
- }
- edbInfoItem.DataList = dataList
- }
- // 第一页才需要 获取预测指标未来的数据
- if currentIndex == 1 {
- allDataList, err := data_manage.GetEdbDataList(sourceEdbInfoItem.Source, sourceEdbInfoItem.EdbInfoId, "", "")
- if err != nil {
- br.Msg = "获取失败"
- br.Msg = "获取失败,Err:" + err.Error()
- return
- }
- predictEdbConfDataList := make([]data_manage.PredictEdbConfAndData, 0)
- for _, v := range predictEdbConfList {
- predictEdbConfDataList = append(predictEdbConfDataList, data_manage.PredictEdbConfAndData{
- ConfigId: v.ConfigId,
- PredictEdbInfoId: v.PredictEdbInfoId,
- SourceEdbInfoId: v.SourceEdbInfoId,
- RuleType: v.RuleType,
- FixedValue: v.FixedValue,
- Value: v.Value,
- EndDate: v.EndDate,
- ModifyTime: v.ModifyTime,
- CreateTime: v.CreateTime,
- DataList: make([]*data_manage.EdbDataList, 0),
- })
- }
- tmpPredictDataList, _, _, err, _ := data.GetChartPredictEdbInfoDataListByConfList(predictEdbConfDataList, sourceEdbInfoItem.LatestDate, sourceEdbInfoItem.LatestDate, edbInfoItem.EndDate, sourceEdbInfoItem.Frequency, allDataList)
- if err != nil {
- br.Msg = "获取预测指标数据失败"
- br.ErrMsg = "获取预测指标数据失败" + err.Error()
- return
- }
- lenTmpPredictDataList := len(tmpPredictDataList)
- if lenTmpPredictDataList > 0 {
- for i := lenTmpPredictDataList - 1; i >= 0; i-- {
- v := tmpPredictDataList[i]
- predictDataList = append(predictDataList, &data_manage.EdbData{
- EdbDataId: v.EdbDataId,
- EdbInfoId: v.EdbInfoId,
- DataTime: v.DataTime,
- Value: v.Value,
- })
- }
- }
- }
- } else {
- //获取指标数据(实际已生成)
- {
- var dataCondition string
- var dataPars []interface{}
- dataCondition += ` AND edb_info_id=? AND data_time <= ? `
- dataPars = append(dataPars, edbInfoItem.EdbInfoId, edbInfoItem.LatestDate)
- dataCount, err := data_manage.GetEdbDataCountByCondition(dataCondition, dataPars, edbInfoItem.Source)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取指标信息失败"
- br.ErrMsg = "获取指标数据总数失败,Err:" + err.Error()
- return
- }
- page = paging.GetPaging(currentIndex, pageSize, dataCount)
- dataList, err := data_manage.GetEdbDataListByCondition(dataCondition, dataPars, edbInfoItem.Source, pageSize, startSize)
- if err != nil {
- br.Msg = "获取指标信息失败"
- br.ErrMsg = "获取指标数据失败,Err:" + err.Error()
- return
- }
- edbInfoItem.DataList = dataList
- }
- // 第一页才需要 获取预测指标未来的数据
- if currentIndex == 1 {
- var dataCondition string
- var dataPars []interface{}
- dataCondition += ` AND edb_info_id=? AND data_time > ? `
- dataPars = append(dataPars, edbInfoItem.EdbInfoId, edbInfoItem.LatestDate)
- predictDataList, err = data_manage.GetAllEdbDataListByCondition(dataCondition, dataPars, edbInfoItem.Source)
- if err != nil {
- br.Msg = "获取指标信息失败"
- br.ErrMsg = "获取指标数据失败,Err:" + err.Error()
- return
- }
- if err != nil {
- br.Msg = "获取预测指标数据失败"
- br.ErrMsg = "获取预测指标数据失败" + err.Error()
- return
- }
- }
- }
- // 如果预测数据没有,那么返回空数组
- if predictDataList == nil {
- predictDataList = make([]*data_manage.EdbData, 0)
- }
- edbInfoItem.PredictDataList = predictDataList
- // 如果实际数据没有,那么返回空数组
- if edbInfoItem.DataList == nil {
- edbInfoItem.DataList = make([]*data_manage.EdbData, 0)
- }
- resp.Paging = page
- resp.Item = edbInfoItem
- resp.ClassifyList = resultList
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // Add
- // @Title 新增指标接口
- // @Description 新增指标接口
- // @Param request body request.AddPredictEdbInfoReq true "type json string"
- // @Success 200 {object} response.AddEdbInfoResp
- // @router /predict_edb_info/add [post]
- func (this *PredictEdbInfoController) Add() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- deleteCache := true
- cacheKey := "CACHE_EDB_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
- defer func() {
- if deleteCache {
- utils.Rc.Delete(cacheKey)
- }
- }()
- if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
- deleteCache = false
- br.Msg = "系统处理中,请稍后重试!"
- br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
- return
- }
- var req request.AddPredictEdbInfoReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- sourceEdbInfoId := req.SourceEdbInfoId
- if sourceEdbInfoId <= 0 {
- br.Msg = "请选择来源指标"
- br.IsSendEmail = false
- return
- }
- if req.EdbName == "" {
- br.Msg = "指标名称不能为空"
- br.IsSendEmail = false
- return
- }
- if req.ClassifyId <= 0 {
- br.Msg = "请选择分类"
- br.IsSendEmail = false
- return
- }
- if len(req.RuleList) <= 0 {
- br.Msg = "请填写预测规则"
- br.IsSendEmail = false
- return
- }
- // 根据截止日期重新排序
- ruleList := make([]request.RuleConfig, 0)
- {
- ruleMap := make(map[string]request.RuleConfig)
- dateIntList := make([]int64, 0)
- for _, v := range req.RuleList {
- confEndDate, err := time.Parse(utils.FormatDate, v.EndDate)
- if err != nil {
- br.Msg = "配置项中时间异常,请重新选择"
- br.ErrMsg = "配置项中时间异常,请重新选择,err:" + err.Error()
- br.IsSendEmail = false
- return
- }
- dateIntList = append(dateIntList, confEndDate.Unix())
- ruleMap[v.EndDate] = v
- }
- sort.Slice(dateIntList, func(i, j int) bool {
- return dateIntList[i] < dateIntList[j]
- })
- for _, dateInt := range dateIntList {
- currDateTime := time.Unix(dateInt, 0)
- item, ok := ruleMap[currDateTime.Format(utils.FormatDate)]
- if !ok {
- br.Msg = "配置项中时间异常,请重新选择"
- br.ErrMsg = "配置项中时间异常,请重新选择"
- br.IsSendEmail = false
- return
- }
- ruleList = append(ruleList, item)
- }
- }
- req.RuleList = ruleList
- //传入操作人
- req.AdminId = sysUser.AdminId
- req.AdminName = sysUser.RealName
- // 添加计算指标
- reqJson, err := json.Marshal(req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- respItem, err := data.SaveBasePredictEdbData(string(reqJson))
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- if respItem.Ret != 200 {
- br.Msg = respItem.Msg
- br.ErrMsg = respItem.ErrMsg
- return
- }
- resp := respItem.Data
- //添加es
- data.AddOrEditEdbInfoToEs(resp.EdbInfoId)
- br.Ret = 200
- br.Success = true
- br.Msg = "保存成功"
- br.Data = resp
- br.IsAddLog = true
- }
- // ChartImageSet
- // @Title 设置指标的图表图片
- // @Description 设置指标的图表图片接口
- // @Param request body data_manage.SetChartInfoImageReq true "type json string"
- // @Success Ret=200 保存成功
- // @router /predict_edb_info/image/set [post]
- func (this *PredictEdbInfoController) ChartImageSet() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req data_manage.SetEdbInfoImageReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.EdbInfoId > 0 && req.ImageUrl != "" {
- edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
- if err != nil {
- br.Msg = "保存失败"
- br.ErrMsg = "保存失败,Err:" + err.Error()
- if err.Error() == utils.ErrNoRow() {
- br.Msg = "找不到该指标"
- br.ErrMsg = "找不到该指标"
- br.IsSendEmail = false
- }
- return
- }
- edbInfo.ChartImage = req.ImageUrl
- err = edbInfo.Update([]string{"ChartImage"})
- if err != nil {
- br.Msg = "保存失败"
- br.ErrMsg = "保存失败,Err:" + err.Error()
- return
- }
- //修改es数据
- go data.AddOrEditEdbInfoToEs(req.EdbInfoId)
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "保存成功"
- }
- // Edit
- // @Title 编辑指标接口
- // @Description 编辑指标接口
- // @Success 200 {object} request.EditEdbInfoReq
- // @router /predict_edb_info/edit [post]
- func (this *PredictEdbInfoController) Edit() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req request.AddPredictEdbInfoReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- edbInfoId := req.EdbInfoId
- if edbInfoId <= 0 {
- br.Msg = "请选择预测指标"
- br.IsSendEmail = false
- return
- }
- if req.EdbName == "" {
- br.Msg = "指标名称不能为空"
- br.IsSendEmail = false
- return
- }
- if req.ClassifyId <= 0 {
- br.Msg = "请选择分类"
- br.IsSendEmail = false
- return
- }
- if len(req.RuleList) <= 0 {
- br.Msg = "请填写预测规则"
- br.IsSendEmail = false
- return
- }
- // 根据截止日期重新排序
- ruleList := make([]request.RuleConfig, 0)
- {
- ruleMap := make(map[string]request.RuleConfig)
- dateIntList := make([]int64, 0)
- for _, v := range req.RuleList {
- confEndDate, err := time.Parse(utils.FormatDate, v.EndDate)
- if err != nil {
- br.Msg = "配置项中时间异常,请重新选择"
- br.ErrMsg = "配置项中时间异常,请重新选择,err:" + err.Error()
- br.IsSendEmail = false
- return
- }
- dateIntList = append(dateIntList, confEndDate.Unix())
- ruleMap[v.EndDate] = v
- }
- sort.Slice(dateIntList, func(i, j int) bool {
- return dateIntList[i] < dateIntList[j]
- })
- for _, dateInt := range dateIntList {
- currDateTime := time.Unix(dateInt, 0)
- item, ok := ruleMap[currDateTime.Format(utils.FormatDate)]
- if !ok {
- br.Msg = "配置项中时间异常,请重新选择"
- br.ErrMsg = "配置项中时间异常,请重新选择"
- br.IsSendEmail = false
- return
- }
- ruleList = append(ruleList, item)
- }
- }
- req.RuleList = ruleList
- edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
- if err != nil {
- br.Msg = "修改失败"
- br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
- return
- }
- // 编辑权限校验
- button := data.GetPredictEdbOpButton(this.SysUser, edbInfo.SysUserId)
- if !button.OpButton {
- br.Msg = "无权限操作"
- br.IsSendEmail = false
- return
- }
- //传入操作人
- req.AdminId = sysUser.AdminId
- req.AdminName = sysUser.RealName
- // 添加计算指标
- reqJson, err := json.Marshal(req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- respItem, err := data.SaveBasePredictEdbData(string(reqJson))
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- if respItem.Ret != 200 {
- br.Msg = respItem.Msg
- br.ErrMsg = respItem.ErrMsg
- return
- }
- resp := respItem.Data
- //修改es
- data.AddOrEditEdbInfoToEs(resp.EdbInfoId)
- // 刷新关联指标
- go data.EdbInfoRefreshAllFromBaseV2(resp.EdbInfoId, true)
- br.Ret = 200
- br.Success = true
- br.Msg = "修改成功"
- br.IsAddLog = true
- }
- // Refresh
- // @Title 指标刷新接口
- // @Description 指标刷新接口
- // @Param EdbInfoId query int true "指标id"
- // @Success Ret=200 刷新成功
- // @router /predict_edb_info/refresh [get]
- func (this *PredictEdbInfoController) Refresh() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- edbInfoId, _ := this.GetInt("EdbInfoId")
- if edbInfoId <= 0 {
- br.Msg = "参数错误"
- br.ErrMsg = "参数错误"
- return
- }
- _, err, errMsg := data.RefreshPredictEdbInfo(edbInfoId, false)
- if errMsg != `` {
- br.Msg = errMsg
- br.ErrMsg = errMsg
- if err != nil {
- br.ErrMsg = err.Error()
- } else {
- br.IsSendEmail = false
- }
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "刷新成功"
- br.IsAddLog = true
- }
- // Move
- // @Title 移动指标接口
- // @Description 移动指标接口
- // @Success 200 {object} data_manage.MoveEdbInfoReq
- // @router /predict_edb_info/move [post]
- func (this *PredictEdbInfoController) Move() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req data_manage.MoveEdbInfoReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- if req.EdbInfoId <= 0 {
- br.Msg = "参数错误"
- br.ErrMsg = "指标id小于等于0"
- return
- }
- if req.ClassifyId <= 0 {
- br.Msg = "请选择分类"
- return
- }
- // 移动预测指标
- err, errMsg := data.MovePredictEdbInfo(req.EdbInfoId, req.ClassifyId, req.PrevEdbInfoId, req.NextEdbInfoId, sysUser, string(this.Ctx.Input.RequestBody), this.Ctx.Input.URI())
- if errMsg != `` {
- br.Msg = errMsg
- br.ErrMsg = errMsg
- if err != nil {
- br.ErrMsg = errMsg + ";Err:" + err.Error()
- } else {
- br.IsSendEmail = false
- }
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "移动成功"
- }
- // Detail
- // @Title 获取指标详情
- // @Description 获取指标详情接口
- // @Param EdbInfoId query int true "指标id"
- // @Success 200 {object} response.PredictEdbInfo
- // @router /predict_edb_info/detail [get]
- func (this *PredictEdbInfoController) Detail() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- edbInfoId, _ := this.GetInt("EdbInfoId")
- if edbInfoId <= 0 {
- br.Msg = "参数错误"
- br.ErrMsg = "参数错误"
- return
- }
- edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
- return
- }
- var ruleType int
- var fixedValue float64
- calculateList := make([]*data_manage.EdbInfoCalculateDetail, 0)
- predictEdbConfList := make([]data_manage.PredictEdbConfDetail, 0)
- calculateList, err = data_manage.GetEdbInfoCalculateDetail(edbInfoId, edbInfo.Source)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- if edbInfo.EdbType == 1 {
- // 获取所有的指标预测规则配置
- tmpPredictEdbConfList, err := data_manage.GetPredictEdbConfListById(edbInfoId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取失败"
- br.ErrMsg = "获取预测指标配置信息失败,Err:" + err.Error()
- return
- }
- for _, v := range tmpPredictEdbConfList {
- var tmpPredictEdbConfCalculateMappingDetail []*data_manage.PredictEdbConfCalculateMappingDetail
- if v.RuleType == 9 || v.RuleType == 14 {
- tmpPredictEdbConfCalculateMappingDetail, err = data_manage.GetPredictEdbConfCalculateMappingDetailListByConfigId(v.PredictEdbInfoId, v.ConfigId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取失败"
- br.ErrMsg = "获取预测指标配置与关联指标信息失败,Err:" + err.Error()
- return
- }
- }
- if len(tmpPredictEdbConfCalculateMappingDetail) <= 0 {
- tmpPredictEdbConfCalculateMappingDetail = make([]*data_manage.PredictEdbConfCalculateMappingDetail, 0)
- }
- tmp := data_manage.PredictEdbConfDetail{
- ConfigId: v.ConfigId,
- PredictEdbInfoId: v.PredictEdbInfoId,
- SourceEdbInfoId: v.SourceEdbInfoId,
- RuleType: v.RuleType,
- FixedValue: v.FixedValue,
- Value: v.Value,
- EndDate: v.EndDate,
- ModifyTime: v.ModifyTime,
- CreateTime: v.CreateTime,
- CalculateList: tmpPredictEdbConfCalculateMappingDetail,
- }
- predictEdbConfList = append(predictEdbConfList, tmp)
- }
- //calculateList, err = data_manage.GetEdbInfoCalculateDetail(edbInfoId, edbInfo.Source)
- //if err != nil {
- // br.Msg = "获取失败"
- // br.ErrMsg = "获取失败,Err:" + err.Error()
- // return
- //}
- if len(predictEdbConfList) == 0 {
- br.Msg = "找不到该预测指标配置"
- br.ErrMsg = "找不到该预测指标配置"
- return
- }
- predictEdbConf := predictEdbConfList[0]
- ruleType = predictEdbConf.RuleType
- fixedValue = predictEdbConf.FixedValue
- }
- resp := response.PredictEdbInfo{
- EdbInfo: *edbInfo,
- RuleType: ruleType,
- FixedValue: fixedValue,
- CalculateList: calculateList,
- RuleList: predictEdbConfList,
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // FilterByEs
- // @Title 指标筛选接口
- // @Description 指标筛选接口
- // @Param KeyWord query string false "搜索关键词:指标ID/指标名称"
- // @Param FilterSource query int false "搜索来源:1:其他搜索,2:累计值转月值搜索,3:变频,4:基础指标,5:同比"
- // @Param Frequency query string false "频度"
- // @Param IsOnlyMe query bool false "是否只看我的"
- // @Param PageSize query int true "每页数据条数"
- // @Param CurrentIndex query int true "当前页页码,从1开始"
- // @Success 200 {object} data_manage.EdbInfoList
- // @router /predict_edb_info/filter_by_es [get]
- func (this *PredictEdbInfoController) FilterByEs() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- pageSize, _ := this.GetInt("PageSize")
- currentIndex, _ := this.GetInt("CurrentIndex")
- var total int64
- var startSize int
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize = paging.StartIndex(currentIndex, pageSize)
- keyWord := this.GetString("Keyword")
- keyWord = strings.TrimSpace(keyWord) //移除字符串首尾空格
- //兼容前端
- if keyWord == `` {
- keyWord = this.GetString("KeyWord")
- keyWord = strings.TrimSpace(keyWord) //移除字符串首尾空格
- }
- filterSource, _ := this.GetInt("FilterSource")
- if filterSource <= 0 {
- filterSource = 1
- }
- //指标来源
- source, _ := this.GetInt("Source")
- frequency := this.GetString("Frequency") //频度
- isOnlyMe, _ := this.GetBool("IsOnlyMe")
- var edbInfoList []*data_manage.EdbInfoList
- var err error
- noPermissionEdbInfoIdList := make([]int, 0) //无权限指标
- // 获取当前账号的不可见指标
- {
- obj := data_manage.EdbInfoNoPermissionAdmin{}
- confList, err := obj.GetAllListByAdminId(this.SysUser.AdminId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取失败"
- br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
- return
- }
- for _, v := range confList {
- noPermissionEdbInfoIdList = append(noPermissionEdbInfoIdList, v.EdbInfoId)
- }
- }
- if keyWord != "" {
- var keyWordArr []string
- keyWordArr = append(keyWordArr, keyWord)
- newKeyWord := strings.Split(keyWord, " ")
- keyWordArr = append(keyWordArr, newKeyWord...)
- if isOnlyMe {
- total, edbInfoList, err = elastic.SearchEdbInfoDataByAdminId(utils.DATA_INDEX_NAME, keyWord, startSize, pageSize, filterSource, source, 1, frequency, this.SysUser.AdminId)
- } else {
- total, edbInfoList, err = elastic.SearchEdbInfoData(utils.DATA_INDEX_NAME, keyWord, startSize, pageSize, filterSource, source, 1, frequency, noPermissionEdbInfoIdList)
- }
- } else {
- var condition string
- var pars []interface{}
- // 预测指标
- condition += ` AND edb_info_type = ? `
- pars = append(pars, 1)
- // 无权限指标id
- lenNoPermissionEdbInfoIdList := len(noPermissionEdbInfoIdList)
- if lenNoPermissionEdbInfoIdList > 0 {
- condition += ` AND edb_info_id not in (` + utils.GetOrmInReplace(lenNoPermissionEdbInfoIdList) + `) `
- pars = append(pars, noPermissionEdbInfoIdList)
- }
- switch filterSource {
- case 2:
- condition += ` AND frequency='月度' `
- case 3:
- condition += ` AND frequency <> '日度' `
- case 4:
- condition += ` AND edb_type = 1 `
- case 5:
- condition += ` AND source = 32 ` //来源(预测同比值)
- case 6:
- condition += ` AND frequency != ? `
- pars = append(pars, "年度")
- }
- //频度
- if frequency != "" {
- condition += ` AND frequency = ? `
- pars = append(pars, frequency)
- }
- if isOnlyMe {
- condition += " AND sys_user_id = ? "
- pars = append(pars, this.SysUser.AdminId)
- }
- total, edbInfoList, err = data_manage.GetEdbInfoFilterList(condition, pars, startSize, pageSize)
- }
- if err != nil {
- edbInfoList = make([]*data_manage.EdbInfoList, 0)
- }
- page := paging.GetPaging(currentIndex, pageSize, int(total))
- edbInfoListLen := len(edbInfoList)
- for i := 0; i < edbInfoListLen; i++ {
- edbInfoList[i].EdbNameAlias = edbInfoList[i].EdbName
- }
- for i := 0; i < edbInfoListLen; i++ {
- for j := 0; j < edbInfoListLen; j++ {
- if (edbInfoList[i].EdbNameAlias == edbInfoList[j].EdbNameAlias) &&
- (edbInfoList[i].EdbInfoId != edbInfoList[j].EdbInfoId) &&
- !(strings.Contains(edbInfoList[i].EdbName, edbInfoList[i].SourceName)) {
- edbInfoList[i].EdbName = edbInfoList[i].EdbName + "(" + edbInfoList[i].SourceName + ")"
- }
- }
- }
- //新增搜索词记录
- {
- searchKeyword := new(data_manage.SearchKeyword)
- searchKeyword.KeyWord = keyWord
- searchKeyword.CreateTime = time.Now()
- go data_manage.AddSearchKeyword(searchKeyword)
- }
- resp := data_manage.EdbInfoFilterDataResp{
- Paging: page,
- List: edbInfoList,
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // RefreshAll
- // @Title 指标全部刷新接口
- // @Description 指标全部刷新接口
- // @Param EdbInfoId query int true "指标id"
- // @Success Ret=200 全部刷新成功
- // @router /predict_edb_info/refresh/all [get]
- func (this *PredictEdbInfoController) RefreshAll() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- edbInfoId, _ := this.GetInt("EdbInfoId")
- if edbInfoId <= 0 {
- br.Msg = "参数错误"
- br.ErrMsg = "参数错误"
- return
- }
- _, err, errMsg := data.RefreshPredictEdbInfo(edbInfoId, true)
- if errMsg != `` {
- br.Msg = errMsg
- br.ErrMsg = errMsg
- if err != nil {
- br.ErrMsg = err.Error()
- } else {
- br.IsSendEmail = false
- }
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "刷新成功"
- }
- // DataList
- // @Title 获取指标数据
- // @Description 获取指标数据
- // @Param EdbInfoId query int true "指标ID"
- // @Param DateType query int true "日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间,6:自定义起始时间至今,7:18年至今,8:19年至今,9:20年至今,10:全部"
- // @Param StartDate query string true "自定义开始日期"
- // @Param EndDate query string true "自定义结束日期"
- // @Param Calendar query string true "公历/农历"
- // @Param SeasonStartDate query string true "季节性图开始日期"
- // @Param SeasonEndDate query string true "季节性图结束日期"
- // @Param ChartType query int true "生成样式:1:曲线图,2:季节性图"
- // @Success 200 {object} data_manage.EdbInfoDataResp
- // @router /predict_edb_info/data [get]
- func (this *PredictEdbInfoController) DataList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- edbInfoId, _ := this.GetInt("EdbInfoId")
- if edbInfoId <= 0 {
- br.Msg = "参数错误"
- return
- }
- dateType, _ := this.GetInt("DateType")
- if dateType <= 0 {
- dateType = 3
- }
- edbInfoType, _ := this.GetInt("EdbInfoType")
- if edbInfoType < 0 {
- edbInfoType = 1
- }
- startDate := this.GetString("StartDate")
- endDate := this.GetString("EndDate")
- seasonStartDate := this.GetString("SeasonStartDate")
- seasonEndDate := this.GetString("SeasonEndDate")
- chartType, _ := this.GetInt("ChartType")
- if chartType <= 0 {
- chartType = 1
- }
- calendar := this.GetString("Calendar")
- if calendar == "" {
- calendar = "公历"
- }
- isTimeBetween := false //是否是时间区间
- if chartType == 2 {
- dateType = 0
- if seasonStartDate != "" {
- startDate = seasonStartDate + "-01-01"
- } else {
- fivePre := time.Now().AddDate(-4, 0, 0).Year()
- startDate = strconv.Itoa(fivePre) + "-01-01"
- }
- if seasonEndDate != "" {
- endDate = seasonEndDate + "-12-31"
- isTimeBetween = true
- } else {
- endDate = "" //time.Now().AddDate(2, 0, 0).Format(utils.FormatDate)
- }
- }
- switch dateType {
- case 1:
- startDate = "2000-01-01"
- endDate = time.Now().Format(utils.FormatDate)
- case 2:
- startDate = "2010-01-01"
- endDate = time.Now().Format(utils.FormatDate)
- case 3:
- startDate = "2015-01-01"
- endDate = time.Now().Format(utils.FormatDate)
- case 4:
- //startDate = strconv.Itoa(time.Now().Year()) + "-01-01"
- startDate = "2021-01-01"
- endDate = time.Now().Format(utils.FormatDate)
- case 5:
- startDate = startDate + "-01"
- endDate = endDate + "-01"
- isTimeBetween = true
- case 6:
- startDate = startDate + "-01"
- endDate = time.Now().Format(utils.FormatDate)
- case 7:
- startDate = "2018-01-01"
- endDate = time.Now().Format(utils.FormatDate)
- case 8:
- startDate = "2019-01-01"
- endDate = time.Now().Format(utils.FormatDate)
- case 9:
- startDate = "2020-01-01"
- endDate = time.Now().Format(utils.FormatDate)
- case 10:
- startDate = ""
- endDate = ""
- case 11:
- startDate = "2022-01-01"
- endDate = time.Now().Format(utils.FormatDate)
- //default:
- //startDate = startDate + "-01"
- //endDate = endDate + "-01"
- }
- // 获取预测指标信息
- edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
- if err != nil {
- br.Msg = "获取预测指标信息失败"
- br.ErrMsg = "获取预测指标信息失败,Err:" + err.Error()
- return
- }
- // 按钮校验
- button := data.GetPredictEdbOpButton(this.SysUser, edbInfo.SysUserId)
- button.AddButton = false
- dataList, _, _, err, _ := data.GetPredictDataListByPredictEdbInfo(edbInfo, startDate, endDate, isTimeBetween)
- //lenDataList := len(dataList)
- //if lenDataList > 0 {
- // tmpValue := dataList[lenDataList-1]
- //
- // // 如果最大值 小于 预测值,那么将预测值作为最大值数据返回
- // if edbInfo.MaxValue < tmpValue.Value {
- // edbInfo.MaxValue = tmpValue.Value
- // }
- //
- // // 如果最小值 大于 预测值,那么将预测值作为最小值数据返回
- // if edbInfo.MinValue > tmpValue.Value {
- // edbInfo.MinValue = tmpValue.Value
- // }
- //}
- // 季节性图、公历、农历处理
- resultDataList, err := data.GetChartDataList(dataList, chartType, calendar, edbInfo.LatestDate, startDate)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- resp := response.PredictEdbInfoDataResp{
- EdbInfo: edbInfo,
- DataList: resultDataList,
- Button: button,
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // ChartDataList
- // @Title 获取指标数据
- // @Description 获取指标数据
- // @Param request body data_manage.AddPredictEdbInfoReq true "type json string"
- // @Success 200 {object} data_manage.EdbInfoDataResp
- // @router /predict_edb_info/chart/data/list [post]
- func (this *PredictEdbInfoController) ChartDataList() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req request.PredictEdbInfoChartDataReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if req.SourceEdbInfoId <= 0 {
- br.Msg = "参数错误"
- return
- }
- dateType := req.DateType
- startDate := req.StartDate
- endDate := req.EndDate
- seasonStartDate := req.SeasonStartDate
- seasonEndDate := req.SeasonEndDate
- calendar := req.Calendar
- if calendar == "" {
- calendar = "公历"
- }
- isTimeBetween := false //是否是时间区间
- if req.ChartType == 2 {
- dateType = 0
- if seasonStartDate != "" {
- startDate = seasonStartDate + "-01-01"
- } else {
- fivePre := time.Now().AddDate(-4, 0, 0).Year()
- startDate = strconv.Itoa(fivePre) + "-01-01"
- }
- if seasonEndDate != "" {
- endDate = seasonEndDate + "-12-31"
- isTimeBetween = true
- } else {
- endDate = "" //time.Now().AddDate(2, 0, 0).Format(utils.FormatDate)
- }
- }
- switch dateType {
- case 1:
- startDate = "2000-01-01"
- endDate = time.Now().Format(utils.FormatDate)
- case 2:
- startDate = "2010-01-01"
- endDate = time.Now().Format(utils.FormatDate)
- case 3:
- startDate = "2015-01-01"
- endDate = time.Now().Format(utils.FormatDate)
- case 4:
- //startDate = strconv.Itoa(time.Now().Year()) + "-01-01"
- startDate = "2021-01-01"
- endDate = time.Now().Format(utils.FormatDate)
- case 5:
- startDate = startDate + "-01"
- endDate = endDate + "-01"
- isTimeBetween = true
- case 6:
- startDate = startDate + "-01"
- endDate = time.Now().Format(utils.FormatDate)
- case 7:
- startDate = "2018-01-01"
- endDate = time.Now().Format(utils.FormatDate)
- case 8:
- startDate = "2019-01-01"
- endDate = time.Now().Format(utils.FormatDate)
- case 9:
- startDate = "2020-01-01"
- endDate = time.Now().Format(utils.FormatDate)
- case 10:
- startDate = ""
- endDate = ""
- case 11:
- startDate = "2022-01-01"
- endDate = time.Now().Format(utils.FormatDate)
- //default:
- // startDate = startDate + "-01"
- // endDate = endDate + "-01"
- }
- // 规则列表
- // 根据截止日期重新排序
- ruleList := make([]request.RuleConfig, 0)
- {
- ruleMap := make(map[string]request.RuleConfig)
- dateIntList := make([]int64, 0)
- for _, v := range req.RuleList {
- confEndDate, err := time.Parse(utils.FormatDate, v.EndDate)
- if err != nil {
- br.Msg = "配置项中时间异常,请重新选择"
- br.ErrMsg = "配置项中时间异常,请重新选择,err:" + err.Error()
- br.IsSendEmail = false
- return
- }
- dateIntList = append(dateIntList, confEndDate.Unix())
- ruleMap[v.EndDate] = v
- }
- sort.Slice(dateIntList, func(i, j int) bool {
- return dateIntList[i] < dateIntList[j]
- })
- for _, dateInt := range dateIntList {
- currDateTime := time.Unix(dateInt, 0)
- item, ok := ruleMap[currDateTime.Format(utils.FormatDate)]
- if !ok {
- br.Msg = "配置项中时间异常,请重新选择"
- br.ErrMsg = "配置项中时间异常,请重新选择"
- br.IsSendEmail = false
- return
- }
- ruleList = append(ruleList, item)
- }
- }
- // 预测指标配置
- predictEdbConfAndDataList := make([]data_manage.PredictEdbConfAndData, 0)
- endDateStr := `` //数据的结束日期
- for _, v := range ruleList {
- endDateStr = v.EndDate //预测指标的结束日期
- confEndDate, err := time.ParseInLocation(utils.FormatDate, v.EndDate, time.Local)
- if err != nil {
- br.Msg = "配置项中时间异常,请重新选择"
- br.ErrMsg = "配置项中时间异常,请重新选择,err:" + err.Error()
- return
- }
- // 没有数据,自己瞎测试
- //switch v.RuleType {
- //case 3: //3:同比
- // v.Value = "0.1"
- //case 4: //4:同差
- // v.Value = "10"
- //case 5: //5:环比
- // v.Value = "0.1"
- //case 6: //6:环差
- // v.Value = "0.1"
- //case 7: //7:N期移动均值
- // v.Value = "5"
- //case 8: //8:N期段线性外推值
- // v.Value = "5"
- //}
- tmpDataList := make([]*data_manage.EdbDataList, 0)
- switch v.RuleType {
- case 9:
- // 获取计算参数
- formula, edbInfoList, edbInfoIdBytes, err, errMsg := data.GetCalculateByRuleByNineParams(v)
- if err != nil {
- br.Msg = "计算失败"
- if errMsg != "" {
- br.Msg = errMsg
- }
- br.Msg = err.Error()
- br.IsSendEmail = false
- return
- }
- // 获取计算数据
- tmpDataList, err = data.CalculateByRuleByNine(formula, edbInfoList, edbInfoIdBytes)
- if err != nil {
- br.Msg = "计算失败"
- br.ErrMsg = err.Error()
- br.IsSendEmail = false
- return
- }
- }
- tmpPredictEdbConfAndData := data_manage.PredictEdbConfAndData{
- ConfigId: 0,
- PredictEdbInfoId: 0,
- SourceEdbInfoId: req.SourceEdbInfoId,
- RuleType: v.RuleType,
- FixedValue: 0,
- Value: v.Value,
- EndDate: confEndDate,
- ModifyTime: time.Now(),
- CreateTime: time.Now(),
- DataList: tmpDataList,
- }
- predictEdbConfAndDataList = append(predictEdbConfAndDataList, tmpPredictEdbConfAndData)
- }
- // 来源指标
- sourceEdbInfoItem, err := data_manage.GetEdbInfoById(req.SourceEdbInfoId)
- if err != nil {
- br.Msg = "获取来源指标信息失败"
- br.ErrMsg = "获取来源指标信息失败"
- if err.Error() != utils.ErrNoRow() {
- br.ErrMsg = "获取来源指标信息失败,Err:" + err.Error()
- }
- br.Success = true
- return
- }
- minValue := sourceEdbInfoItem.MinValue
- maxValue := sourceEdbInfoItem.MaxValue
- allDataList := make([]*data_manage.EdbDataList, 0)
- //获取指标数据(实际已生成)
- dataList, err := data_manage.GetEdbDataList(sourceEdbInfoItem.Source, sourceEdbInfoItem.EdbInfoId, startDate, endDate)
- if err != nil {
- br.Msg = "获取失败"
- br.Msg = "获取失败,Err:" + err.Error()
- return
- }
- // 如果选择了日期,那么需要筛选所有的数据,用于未来指标的生成
- if startDate != `` {
- allDataList, err = data_manage.GetEdbDataList(sourceEdbInfoItem.Source, sourceEdbInfoItem.EdbInfoId, "", "")
- if err != nil {
- br.Msg = "获取失败"
- br.Msg = "获取失败,Err:" + err.Error()
- return
- }
- } else {
- allDataList = dataList
- }
- // 获取预测指标未来的数据
- if len(predictEdbConfAndDataList) > 0 {
- predictDataList := make([]*data_manage.EdbDataList, 0)
- if isTimeBetween { //如果是时间区间,那么
- reqEndDateTime, _ := time.ParseInLocation(utils.FormatDate, endDate, time.Local)
- endDateTime, _ := time.ParseInLocation(utils.FormatDate, req.PredictEndDate, time.Local)
- // 如果选择的时间区间结束日期 晚于 当天,那么预测数据截止到当天
- if reqEndDateTime.Before(endDateTime) {
- endDateStr = endDate
- }
- }
- // 获取预测数据
- var predictMinValue, predictMaxValue float64
- predictDataList, predictMinValue, predictMaxValue, err, errMsg := data.GetChartPredictEdbInfoDataListByConfList(predictEdbConfAndDataList, startDate, sourceEdbInfoItem.LatestDate, endDateStr, sourceEdbInfoItem.Frequency, allDataList)
- if err != nil {
- br.Msg = "获取预测指标数据失败"
- if errMsg != `` {
- br.Msg = errMsg
- }
- br.ErrMsg = "获取预测指标数据失败:" + err.Error()
- return
- }
- dataList = append(dataList, predictDataList...)
- if len(predictDataList) > 0 {
- // 如果最小值 大于 预测值,那么将预测值作为最小值数据返回
- if minValue > predictMinValue {
- minValue = predictMinValue
- }
- // 如果最大值 小于 预测值,那么将预测值作为最大值数据返回
- if maxValue < predictMaxValue {
- maxValue = predictMaxValue
- }
- }
- }
- // 季节性图、公历、农历处理
- resultDataList, err := data.GetChartDataList(dataList, req.ChartType, calendar, sourceEdbInfoItem.LatestDate, startDate)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- resp := response.PredictEdbInfoChartDataResp{
- MinValue: minValue,
- MaxValue: maxValue,
- DataList: resultDataList,
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // PredictRuleCalculateByNine
- // @Title 根据预测规则9来计算数据
- // @Description 获取指标数据
- // @Param request body data_manage.RuleConfig true "type json string"
- // @Success 200 {object} data_manage.EdbInfoDataResp
- // @router /predict_edb_info/rule/calculate_by_nine [post]
- func (this *PredictEdbInfoController) PredictRuleCalculateByNine() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- time.Now().AddDate(0, -1, -1).Format(utils.FormatDate)
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req request.RuleConfig
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- // 获取计算参数
- formula, edbInfoList, edbInfoIdBytes, err, errMsg := data.GetCalculateByRuleByNineParams(req)
- if err != nil {
- br.Msg = "计算失败"
- if errMsg != "" {
- br.Msg = errMsg
- }
- br.Msg = err.Error()
- br.IsSendEmail = false
- return
- }
- // 获取计算数据
- dataList, err := data.CalculateByRuleByNine(formula, edbInfoList, edbInfoIdBytes)
- if err != nil {
- br.Msg = "数据计算失败"
- br.ErrMsg = "数据计算失败:Err:" + err.Error()
- br.IsSendEmail = false
- return
- }
- latestDate := time.Now()
- for _, v := range edbInfoList {
- tmpLatestDate, err := time.ParseInLocation(utils.FormatDate, v.LatestDate, time.Local)
- if err != nil {
- continue
- }
- if tmpLatestDate.Before(latestDate) {
- latestDate = tmpLatestDate
- }
- }
- newDataList := make([]*data_manage.EdbDataList, 0)
- lenData := len(dataList)
- if lenData > 0 {
- for i := lenData - 1; i >= 0; i-- {
- newDataList = append(newDataList, dataList[i])
- }
- }
- resp := response.PredictRuleCalculateByNineResp{
- LatestDate: latestDate.Format(utils.FormatDate),
- DataList: newDataList,
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- // Modify
- // @Title 修改指标信息接口
- // @Description 编辑指标接口
- // @Success 200 {object} request.EditEdbInfoReq
- // @router /predict_edb_info/modify [post]
- func (this *PredictEdbInfoController) Modify() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req request.ModifyPredictEdbInfoReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- edbInfoId := req.EdbInfoId
- if edbInfoId <= 0 {
- br.Msg = "请选择预测指标"
- br.IsSendEmail = false
- return
- }
- edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
- if err != nil {
- br.Msg = "修改失败"
- br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
- return
- }
- // 编辑权限校验
- button := data.GetPredictEdbOpButton(this.SysUser, edbInfo.SysUserId)
- if !button.OpButton {
- br.Msg = "无权限操作"
- br.IsSendEmail = false
- return
- }
- edbInfo.MaxValue = req.MaxValue
- edbInfo.MinValue = req.MinValue
- edbInfo.ModifyTime = time.Now()
- err = edbInfo.Update([]string{"MaxValue", "MinValue", "ModifyTime"})
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- //修改es
- data.AddOrEditEdbInfoToEs(edbInfo.EdbInfoId)
- br.Ret = 200
- br.Success = true
- br.Msg = "修改成功"
- br.IsAddLog = true
- }
- //func init() {
- // r := new(regression.Regression)
- // r.SetObserved("Murders per annum per 1,000,000 inhabitants")
- // r.SetVar(0, "Inhabitants")
- // r.SetVar(1, "Percent with incomes below $5000")
- // r.SetVar(2, "Percent unemployed")
- // r.Train(
- // regression.DataPoint(11.2, []float64{587000, 16.5, 6.2}),
- // regression.DataPoint(13.4, []float64{643000, 20.5, 6.4}),
- // regression.DataPoint(40.7, []float64{635000, 26.3, 9.3}),
- // regression.DataPoint(5.3, []float64{692000, 16.5, 5.3}),
- // regression.DataPoint(24.8, []float64{1248000, 19.2, 7.3}),
- // regression.DataPoint(12.7, []float64{643000, 16.5, 5.9}),
- // regression.DataPoint(20.9, []float64{1964000, 20.2, 6.4}),
- // regression.DataPoint(35.7, []float64{1531000, 21.3, 7.6}),
- // regression.DataPoint(8.7, []float64{713000, 17.2, 4.9}),
- // regression.DataPoint(9.6, []float64{749000, 14.3, 6.4}),
- // regression.DataPoint(14.5, []float64{7895000, 18.1, 6}),
- // regression.DataPoint(26.9, []float64{762000, 23.1, 7.4}),
- // regression.DataPoint(15.7, []float64{2793000, 19.1, 5.8}),
- // regression.DataPoint(36.2, []float64{741000, 24.7, 8.6}),
- // regression.DataPoint(18.1, []float64{625000, 18.6, 6.5}),
- // regression.DataPoint(28.9, []float64{854000, 24.9, 8.3}),
- // regression.DataPoint(14.9, []float64{716000, 17.9, 6.7}),
- // regression.DataPoint(25.8, []float64{921000, 22.4, 8.6}),
- // regression.DataPoint(21.7, []float64{595000, 20.2, 8.4}),
- // regression.DataPoint(25.7, []float64{3353000, 16.9, 6.7}),
- // )
- // r.Run()
- //
- // fmt.Printf("Regression formula:\n%v\n", r.Formula)
- // //fmt.Printf("Regression:\n%s\n", r)
- //}
- //func init() {
- // //data := []float64{4080, 4070, 4070, 4020, 4020, 4010, 3950, 3980, 4000, 3990, 4020}
- // //median, _ := stats.Median(data)
- // //fmt.Println(median) // 3.65
- // //stats.LinearRegression()
- //
- // //data := []float64{ , , , , , , , , }
- //
- // data := []Coordinate{
- // {1, 4080},
- // {2, 4070},
- // {3, 4070},
- // {4, 4020},
- // {5, 4020},
- // {6, 4010},
- // {7, 3950},
- // {8, 3980},
- // {9, 4000},
- // {10, 3990},
- // {11, 4020},
- // }
- //
- // //r, _ := ct(data)
- //
- // ct(data)
- // //fmt.Println(r)
- //
- //}
- //func init() {
- // data.AddOrEditAllEdbInfoToEs()
- //}
|