12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026 |
- package data
- import (
- "encoding/json"
- "errors"
- "eta/eta_api/models"
- "eta/eta_api/models/company"
- "eta/eta_api/models/data_manage"
- "eta/eta_api/models/system"
- "eta/eta_api/services/alarm_msg"
- "eta/eta_api/utils"
- "fmt"
- "github.com/shopspring/decimal"
- "math"
- "sort"
- "strconv"
- "strings"
- "time"
- )
- func ChartInfoRefreshV1(chartInfoId int) (err error) {
- var errmsg string
- defer func() {
- if err != nil {
- go alarm_msg.SendAlarmMsg("ChartInfoRefresh:"+errmsg, 3)
- //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "ChartInfoRefresh:"+errmsg, utils.EmailSendToUsers)
- fmt.Println("ChartInfoRefresh Err:" + errmsg)
- }
- }()
- baseEdbInfoArr, calculateInfoArr, err := data_manage.GetChartInfoRefreshData(chartInfoId)
- if err != nil {
- errmsg = "获取需要刷新的指标失败:Err:" + err.Error()
- return
- }
- newBaseEdbInfoArr := make([]*data_manage.EdbInfo, 0)
- baseMap := make(map[int]*data_manage.EdbInfo)
- for _, bv := range baseEdbInfoArr {
- // 如果不是普通指标,那么过滤
- if bv.EdbInfoType != 0 {
- continue
- }
- if _, ok := baseMap[bv.EdbInfoId]; !ok {
- newBaseEdbInfoArr = append(newBaseEdbInfoArr, bv)
- }
- baseMap[bv.EdbInfoId] = bv
- }
- fmt.Println("calculateInfoArr:", len(calculateInfoArr))
- newCalculateInfoArr := make([]*data_manage.EdbInfo, 0)
- calculateMap := make(map[int]*data_manage.EdbInfo)
- var calculateArr []int
- for _, bv := range calculateInfoArr {
- if _, ok := calculateMap[bv.EdbInfoId]; !ok {
- newCalculateInfoArr = append(newCalculateInfoArr, bv)
- calculateArr = append(calculateArr, bv.EdbInfoId)
- }
- calculateMap[bv.EdbInfoId] = bv
- }
- sort.Ints(calculateArr)
- // 刷新相关基础指标
- for _, bv := range newBaseEdbInfoArr {
- err, errmsg = refreshBaseEdbInfo(bv)
- if err != nil {
- return
- }
- }
- // 刷新相关计算指标
- for _, v := range calculateArr {
- edbInfo := calculateMap[v]
- if edbInfo == nil {
- return err
- }
- err, errmsg = refreshCalculateEdbInfo(edbInfo)
- if err != nil {
- return
- }
- }
- return err
- }
- // BatchChartInfoRefresh 图表批量刷新
- func BatchChartInfoRefresh(chartInfoList []*data_manage.ChartInfo) (err error) {
- if len(chartInfoList) <= 0 {
- return
- }
- chartInfoIdSlice := make([]string, 0)
- for _, chartInfo := range chartInfoList {
- chartInfoIdSlice = append(chartInfoIdSlice, strconv.Itoa(chartInfo.ChartInfoId))
- }
- var errmsg string
- defer func() {
- if err != nil {
- go alarm_msg.SendAlarmMsg("ChartInfoRefresh:"+errmsg, 3)
- //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "ChartInfoRefresh:"+errmsg, utils.EmailSendToUsers)
- fmt.Println("ChartInfoRefresh Err:" + errmsg)
- }
- }()
- baseEdbInfoArr, calculateInfoArr, err := data_manage.GetChartInfoRefreshDataByChartInfoIdSlice(chartInfoIdSlice)
- if err != nil {
- errmsg = "获取需要刷新的指标失败:Err:" + err.Error()
- return
- }
- //基础指标去重
- newBaseEdbInfoArr := make([]*data_manage.EdbInfo, 0)
- baseMap := make(map[int]*data_manage.EdbInfo)
- for _, bv := range baseEdbInfoArr {
- fmt.Println(bv.UniqueCode)
- if _, ok := baseMap[bv.EdbInfoId]; !ok {
- newBaseEdbInfoArr = append(newBaseEdbInfoArr, bv)
- }
- baseMap[bv.EdbInfoId] = bv
- }
- fmt.Println("calculateInfoArr:", len(calculateInfoArr))
- //计算指标去重
- newCalculateInfoArr := make([]*data_manage.EdbInfo, 0)
- calculateMap := make(map[int]*data_manage.EdbInfo)
- var calculateArr []int
- for _, bv := range calculateInfoArr {
- if _, ok := calculateMap[bv.EdbInfoId]; !ok {
- newCalculateInfoArr = append(newCalculateInfoArr, bv)
- calculateArr = append(calculateArr, bv.EdbInfoId)
- }
- calculateMap[bv.EdbInfoId] = bv
- }
- sort.Ints(calculateArr)
- // 刷新相关基础指标
- for _, bv := range newBaseEdbInfoArr {
- err, errmsg = refreshBaseEdbInfo(bv)
- if err != nil {
- return
- }
- }
- // 刷新相关计算指标
- for _, v := range calculateArr {
- edbInfo := calculateMap[v]
- if edbInfo == nil {
- return err
- }
- err, errmsg = refreshCalculateEdbInfo(edbInfo)
- if err != nil {
- return
- }
- }
- return err
- }
- // refreshBaseEdbInfo 刷新基础指标
- func refreshBaseEdbInfo(bv *data_manage.EdbInfo) (err error, errMsg string) {
- source := bv.Source
- subSource := bv.SubSource
- edbInfoId := bv.EdbInfoId
- edbCode := bv.EdbCode
- //startDate := bv.StartDate
- frequency := bv.Frequency
- if bv.StartDate == "0000-00-00" {
- return
- }
- sTime, err := time.Parse(utils.FormatDate, bv.EndDate)
- if err != nil {
- return
- }
- var limitDay int
- startDate := ""
- switch frequency {
- case "日度":
- limitDay = utils.DATA_START_REFRESH_LIMIT
- case "周度":
- limitDay = utils.DATA_START_REFRESH_LIMIT * 7
- case "月度":
- limitDay = utils.DATA_START_REFRESH_LIMIT * 30
- case "季度":
- limitDay = utils.DATA_START_REFRESH_LIMIT * 90
- case "年度":
- limitDay = utils.DATA_START_REFRESH_LIMIT * 365
- default:
- limitDay = utils.DATA_START_REFRESH_LIMIT
- }
- startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
- fmt.Println("source:", source)
- respItem, err := RefreshEdbData(edbInfoId, source, subSource, edbCode, startDate)
- if err != nil {
- errMsg = errors.New("RefreshEdbData Err:" + err.Error()).Error()
- return
- }
- if respItem.Ret != 200 {
- errMsg = errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
- return
- }
- maxAndMinItem, err := data_manage.GetEdbInfoMaxAndMinInfo(source, subSource, edbCode)
- if err != nil {
- return
- }
- if maxAndMinItem != nil {
- err = data_manage.ModifyEdbInfoMaxAndMinInfo(edbInfoId, maxAndMinItem)
- if err != nil {
- return
- }
- }
- return
- }
- // refreshCalculateEdbInfo 刷新计算指标
- func refreshCalculateEdbInfo(edbInfo *data_manage.EdbInfo) (err error, errMsg string) {
- startDate := edbInfo.StartDate
- source := edbInfo.Source
- if source == utils.DATA_SOURCE_CALCULATE {
- startDate = edbInfo.StartDate
- sTime, tmpErr := time.Parse(utils.FormatDate, edbInfo.EndDate)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- startDate = sTime.Format(utils.FormatDate)
- }
- result, err := RefreshEdbCalculateData(edbInfo.EdbInfoId, edbInfo.EdbCode, startDate)
- if err != nil {
- fmt.Println(edbInfo.EdbInfoId, "RefreshEdbCalculateData err", time.Now())
- errMsg = "RefreshEdbCalculateData Err:" + err.Error()
- return
- }
- if result.Ret != 200 {
- fmt.Println(edbInfo.EdbInfoId, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
- errMsg = fmt.Sprint(edbInfo.EdbInfoId, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
- err = errors.New("刷新失败")
- return
- }
- return
- }
- type ChartInfoReq struct {
- ChartInfoId int `description:"图表id,新增时传0"`
- }
- // DeleteChartInfoDataRedis 清除图表缓存
- func DeleteChartInfoDataRedis(bodyByte []byte) (err error) {
- var req ChartInfoReq
- err = json.Unmarshal(bodyByte, &req)
- if err != nil {
- return
- }
- if req.ChartInfoId > 0 {
- err = utils.Rc.Delete(GetChartInfoDataKey(req.ChartInfoId))
- }
- return
- }
- // DeleteChartClassifyRedis 清除图表分类缓存
- func DeleteChartClassifyRedis(bodyByte []byte) (err error) {
- err = utils.Rc.Delete(utils.CACHE_CHART_CLASSIFY)
- return
- }
- // GetChartInfoDataKey 获取图表缓存的key
- func GetChartInfoDataKey(chartInfoId int) string {
- key := fmt.Sprint(utils.CACHE_CHART_INFO_DATA, chartInfoId)
- return key
- }
- // CheckOpChartPermission 判断图表操作权限
- func CheckOpChartPermission(sysUser *system.Admin, createUserId int) (ok bool) {
- if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN {
- ok = true
- }
- // 如果图表创建人与当前操作人相同的话,那么就是允许操作
- if ok == false && createUserId == sysUser.AdminId {
- ok = true
- }
- // 如果图表权限id 是 1 ,那么允许编辑
- if ok == false && sysUser.ChartPermission == 1 {
- ok = true
- }
- return
- }
- // GetChartEdbData 获取图表的指标数据
- func GetChartEdbData(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*data_manage.ChartEdbInfoMapping, extraConfigStr string, seasonExtraConfig string) (edbList []*data_manage.ChartEdbInfoMapping, xEdbIdValue []int, yDataList []data_manage.YData, dataResp interface{}, err error, errMsg string) {
- edbList = make([]*data_manage.ChartEdbInfoMapping, 0)
- xEdbIdValue = make([]int, 0)
- yDataList = make([]data_manage.YData, 0)
- var extraConfig interface{}
- switch chartType {
- case 7: // 柱形图
- var barConfig data_manage.BarChartInfoReq
- if extraConfigStr == `` {
- errMsg = "柱方图未配置"
- err = errors.New(errMsg)
- return
- }
- err = json.Unmarshal([]byte(extraConfigStr), &barConfig)
- if err != nil {
- errMsg = "柱方图配置异常"
- err = errors.New(errMsg)
- return
- }
- extraConfig = barConfig
- case 10: // 截面散点图
- var tmpExtraConfig data_manage.SectionScatterReq
- if extraConfigStr == `` {
- errMsg = "截面散点图未配置"
- err = errors.New(errMsg)
- return
- }
- err = json.Unmarshal([]byte(extraConfigStr), &tmpExtraConfig)
- if err != nil {
- errMsg = "截面散点配置异常"
- err = errors.New(errMsg)
- return
- }
- extraConfig = tmpExtraConfig
- case utils.CHART_TYPE_RADAR:
- var barConfig data_manage.RadarChartInfoReq
- if extraConfigStr == `` {
- errMsg = "雷达图未配置"
- err = errors.New(errMsg)
- return
- }
- err = json.Unmarshal([]byte(extraConfigStr), &barConfig)
- if err != nil {
- errMsg = "雷达图配置异常"
- err = errors.New(errMsg)
- return
- }
- extraConfig = barConfig
- default:
- xEdbIdValue = make([]int, 0)
- yDataList = make([]data_manage.YData, 0)
- }
- // 指标对应的所有数据
- edbDataListMap, edbList, err := getEdbDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList, seasonExtraConfig)
- if err != nil {
- return
- }
- // 特殊图形数据处理
- switch chartType {
- case 7: // 柱形图
- barChartConf := extraConfig.(data_manage.BarChartInfoReq)
- xEdbIdValue, yDataList, err = BarChartData(mappingList, edbDataListMap, barChartConf.DateList, barChartConf.Sort)
- for k := range yDataList {
- yDataList[k].Unit = barChartConf.Unit
- yDataList[k].UnitEn = barChartConf.UnitEn
- }
- for _, v := range edbList {
- // 指标别名
- if barChartConf.EdbInfoIdList != nil && len(barChartConf.EdbInfoIdList) > 0 {
- for _, reqEdb := range barChartConf.EdbInfoIdList {
- if v.EdbInfoId == reqEdb.EdbInfoId {
- v.EdbAliasName = reqEdb.Name
- }
- }
- }
- }
- case 10: // 截面散点图
- sectionScatterConf := extraConfig.(data_manage.SectionScatterReq)
- xEdbIdValue, dataResp, err = GetSectionScatterChartData(chartInfoId, mappingList, edbDataListMap, sectionScatterConf)
- var tmpExtraConfig data_manage.SectionScatterReq
- if extraConfigStr == `` {
- errMsg = "截面散点图未配置"
- err = errors.New(errMsg)
- return
- }
- err = json.Unmarshal([]byte(extraConfigStr), &tmpExtraConfig)
- if err != nil {
- errMsg = "截面散点配置异常"
- err = errors.New(errMsg)
- return
- }
- // 这个数据没有必要返回给前端
- for _, v := range edbList {
- v.DataList = nil
- }
- case utils.CHART_TYPE_RADAR: //雷达图
- radarConf := extraConfig.(data_manage.RadarChartInfoReq)
- xEdbIdValue, dataResp, err = RadarChartData(mappingList, edbDataListMap, radarConf)
- }
- return
- }
- // GetEdbDataMapList 获取指标最后的基础数据
- func GetEdbDataMapList(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*data_manage.ChartEdbInfoMapping, seasonExtra string) (edbDataListMap map[int][]*data_manage.EdbDataList, edbList []*data_manage.ChartEdbInfoMapping, err error) {
- edbDataListMap, edbList, err = getEdbDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList, seasonExtra)
- return
- }
- // getEdbDataMapList 获取指标最后的基础数据
- func getEdbDataMapList(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*data_manage.ChartEdbInfoMapping, seasonExtraConfig string) (edbDataListMap map[int][]*data_manage.EdbDataList, edbList []*data_manage.ChartEdbInfoMapping, err error) {
- // 指标对应的所有数据
- edbDataListMap = make(map[int][]*data_manage.EdbDataList)
- for _, v := range mappingList {
- //fmt.Println("v:", v.EdbInfoId)
- item := new(data_manage.ChartEdbInfoMapping)
- item.EdbInfoId = v.EdbInfoId
- item.SourceName = v.SourceName
- item.Source = v.Source
- item.EdbCode = v.EdbCode
- item.EdbName = v.EdbName
- item.EdbNameEn = v.EdbNameEn
- item.Frequency = v.Frequency
- item.EdbType = v.EdbType
- item.FrequencyEn = GetFrequencyEn(v.Frequency)
- if v.Unit != `无` {
- item.Unit = v.Unit
- }
- item.UnitEn = v.UnitEn
- item.StartDate = v.StartDate
- item.EndDate = v.EndDate
- item.ModifyTime = v.ModifyTime
- item.EdbInfoCategoryType = v.EdbInfoCategoryType
- item.PredictChartColor = v.PredictChartColor
- item.ClassifyId = v.ClassifyId
- if chartInfoId <= 0 {
- item.IsAxis = 1
- item.LeadValue = 0
- item.LeadUnit = ""
- item.ChartEdbMappingId = 0
- item.ChartInfoId = 0
- item.IsOrder = false
- item.EdbInfoType = 1
- item.ChartStyle = ""
- item.ChartColor = ""
- item.ChartWidth = 0
- item.MaxData = v.MaxValue
- item.MinData = v.MinValue
- } else {
- item.IsAxis = v.IsAxis
- item.EdbInfoType = v.EdbInfoType
- item.LeadValue = v.LeadValue
- item.LeadUnit = v.LeadUnit
- item.LeadUnitEn = GetLeadUnitEn(v.LeadUnit)
- item.ChartEdbMappingId = v.ChartEdbMappingId
- item.ChartInfoId = v.ChartInfoId
- item.ChartStyle = v.ChartStyle
- item.ChartColor = v.ChartColor
- item.ChartWidth = v.ChartWidth
- item.IsOrder = v.IsOrder
- item.MaxData = v.MaxData
- item.MinData = v.MinData
- }
- item.LatestValue = v.LatestValue
- item.LatestDate = v.LatestDate
- item.UniqueCode = v.UniqueCode
- item.MoveLatestDate = v.LatestDate
- item.EdbAliasName = v.EdbAliasName
- item.IsConvert = v.IsConvert
- item.ConvertType = v.ConvertType
- item.ConvertValue = v.ConvertValue
- item.ConvertUnit = v.ConvertUnit
- item.ConvertEnUnit = v.ConvertEnUnit
- var startDateReal string
- var diffSeconds int64
- if chartType == 2 { //季节性图
- startDateReal = startDate
- } else {
- if v.EdbInfoType == 0 && v.LeadUnit != "" && v.LeadValue > 0 { //领先指标
- var startTimeRealTemp time.Time
- startDateParse, _ := time.Parse(utils.FormatDate, startDate)
- switch v.LeadUnit {
- case "天":
- startTimeRealTemp = startDateParse.AddDate(0, 0, -v.LeadValue)
- case "月":
- startTimeRealTemp = startDateParse.AddDate(0, -v.LeadValue, 0)
- case "季":
- startTimeRealTemp = startDateParse.AddDate(0, -3*v.LeadValue, 0)
- case "周":
- startTimeRealTemp = startDateParse.AddDate(0, 0, -7*v.LeadValue)
- case "年":
- startTimeRealTemp = startDateParse.AddDate(-v.LeadValue, 0, 0)
- }
- if startTimeRealTemp.Before(startDateParse) {
- startDateReal = startTimeRealTemp.Format(utils.FormatDate)
- diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6
- } else {
- startDateReal = startDate
- diffSeconds = 0
- }
- // 预测指标的开始日期也要偏移
- {
- day, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, startDate, startDateReal)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- moveLatestDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, item.MoveLatestDate, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- item.MoveLatestDate = moveLatestDateTime.AddDate(0, 0, day).Format(utils.FormatDate)
- }
- } else {
- startDateReal = startDate
- }
- }
- //fmt.Println("line 1011 chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
- calendarPreYear := 0
- if calendar == "农历" {
- newStartDateReal, err := time.Parse(utils.FormatDate, startDateReal)
- if err != nil {
- fmt.Println("time.Parse:" + err.Error())
- }
- calendarPreYear = newStartDateReal.Year() - 1
- newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
- startDateReal = newStartDateReal.Format(utils.FormatDate)
- }
- dataList := make([]*data_manage.EdbDataList, 0)
- //fmt.Println("chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
- //fmt.Println("calendarPreYear:", calendarPreYear)
- //var newEdbInfo *data_manage.EdbInfo
- switch v.EdbInfoCategoryType {
- case 0:
- dataList, err = data_manage.GetEdbDataList(v.Source, v.SubSource, v.EdbInfoId, startDateReal, endDate)
- case 1:
- _, dataList, _, _, err, _ = GetPredictDataListByPredictEdbInfoId(v.EdbInfoId, startDateReal, endDate, true)
- default:
- err = errors.New(fmt.Sprint("获取失败,指标类型异常", v.EdbInfoCategoryType))
- }
- if err != nil {
- return
- }
- if v.IsConvert == 1 {
- switch v.ConvertType {
- case 1:
- for i, data := range dataList {
- dataList[i].Value = data.Value * v.ConvertValue
- }
- //item.MaxData = item.MaxData * v.ConvertValue
- //item.MinData = item.MinData * v.ConvertValue
- case 2:
- for i, data := range dataList {
- dataList[i].Value = data.Value / v.ConvertValue
- }
- //item.MaxData = item.MaxData / v.ConvertValue
- //item.MinData = item.MinData / v.ConvertValue
- case 3:
- for i, data := range dataList {
- if data.Value <= 0 {
- err = errors.New("数据中含有负数或0,无法对数运算")
- return
- }
- dataList[i].Value = math.Log(data.Value) / math.Log(v.ConvertValue)
- }
- //item.MaxData = math.Log(item.MaxData) / math.Log(v.ConvertValue)
- //item.MinData = math.Log(item.MinData) / math.Log(v.ConvertValue)
- }
- }
- edbDataListMap[v.EdbInfoId] = dataList
- if diffSeconds != 0 && v.EdbInfoType == 0 {
- dataListLen := len(dataList)
- for i := 0; i < dataListLen; i++ {
- dataList[i].DataTimestamp = dataList[i].DataTimestamp - diffSeconds
- }
- }
- if chartType == 2 {
- latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
- if tmpErr != nil {
- //item.DataList = dataList
- item.IsNullData = true
- edbList = append(edbList, item)
- continue
- err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + v.LatestDate))
- return
- }
- if calendar == "农历" {
- if len(dataList) <= 0 {
- result := new(data_manage.EdbDataResult)
- item.DataList = result
- } else {
- result, tmpErr := data_manage.AddCalculateQuarterV6(dataList)
- if tmpErr != nil {
- err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
- return
- }
- quarterDataList, tErr := GetSeasonEdbInfoDataListByXDateNong(result, latestDate, seasonExtraConfig, calendarPreYear)
- if tErr != nil {
- err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
- return
- }
- item.DataList = quarterDataList
- }
- } else {
- quarterDataList, tErr := GetSeasonEdbInfoDataListByXDate(dataList, latestDate, seasonExtraConfig)
- if tErr != nil {
- err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
- return
- }
- item.DataList = quarterDataList
- }
- } else if chartType == 7 || chartType == utils.CHART_TYPE_RADAR { //柱方图
- //item.DataList = dataList
- } else {
- item.DataList = dataList
- }
- edbList = append(edbList, item)
- }
- return
- }
- // GetSeasonEdbInfoDataListByXDate 季节性图的指标数据根据横轴展示
- func GetSeasonEdbInfoDataListByXDate(dataList []*data_manage.EdbDataList, latestDate time.Time, seasonExtraConfig string) (quarterDataListSort data_manage.QuarterDataList, err error) {
- xStartDate := "01-01"
- xEndDate := "12-31"
- jumpYear := 0
- legends := make([]data_manage.SeasonChartLegend, 0)
- var seasonExtra data_manage.SeasonExtraItem
- if seasonExtraConfig != "" {
- err = json.Unmarshal([]byte(seasonExtraConfig), &seasonExtra)
- if err != nil {
- return
- }
- }
- if seasonExtra.XStartDate != "" {
- xStartDate = seasonExtra.XStartDate
- xEndDate = seasonExtra.XEndDate
- jumpYear = seasonExtra.JumpYear
- legends = seasonExtra.ChartLegend
- }
- length := len(dataList)
- if length == 0 {
- return
- }
- legendMap := make(map[string]string, 0)
- if len(legends) > 0 {
- for _, v := range legends {
- legendMap[v.Name] = v.Value
- }
- }
- latestDateStr := latestDate.Format(utils.FormatDate)
- //判断横轴的两个时间之间是不是跨年了,如果跨年了,则横轴截止年份比起始年份+1,如果不跨年,截止年份等于起始年份
- //根据数据确定最早的年份,和最近年份
- //根据横轴的日期,汇总所有的年份
- startDate := dataList[0].DataTime
- startDateT, tmpErr := time.Parse(utils.FormatDate, startDate)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- startYear := startDateT.Year()
- //获取数据的最新日期
- lastDate := dataList[length-1].DataTime
- lastDateT, tmpErr := time.Parse(utils.FormatDate, lastDate)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- endYear := lastDateT.Year()
- nowYear := time.Now().Year()
- dataMap := make(map[string]data_manage.QuarterXDateItem, 0)
- quarterDataList := make([]*data_manage.QuarterData, 0)
- quarterMap := make(map[string][]*data_manage.EdbDataList, 0)
- //整理出日期
- idx := 1
- chartLegendMap := make(map[string]int, 0)
- for currentStartYear := startYear; currentStartYear <= endYear; currentStartYear++ {
- startStr := fmt.Sprintf("%d-%s", currentStartYear, xStartDate)
- currentEndYear := currentStartYear
- if jumpYear == 1 {
- currentEndYear = currentStartYear + 1
- }
- endStr := fmt.Sprintf("%d-%s", currentEndYear, xEndDate)
- name := fmt.Sprintf("%s_%s", startStr, endStr)
- showName := fmt.Sprintf("%d_%d", currentStartYear, currentEndYear)
- startT, tEr := time.Parse(utils.FormatDate, startStr)
- if tEr != nil {
- err = tEr
- return
- }
- endT, tEr := time.Parse(utils.FormatDate, endStr)
- if tEr != nil {
- err = tEr
- return
- }
- if lastDateT.Before(startT) {
- //如果最新的日期在起始日之前,则跳出循环
- break
- }
- if endT.Year() > nowYear {
- //如果最新的日期比真实年份要大,则数据全部按照最大的年份补齐
- nowYear = endT.Year()
- }
- item := data_manage.QuarterXDateItem{
- StartDate: startT,
- EndDate: endT,
- ShowName: showName,
- }
- dataMap[name] = item
- chartLegendMap[name] = idx
- idx++
- fmt.Println("年份" + showName + "日期" + startStr + " " + endStr)
- if lastDateT.Before(endT) {
- //如果最新的日期在起始日之前,则跳出循环
- break
- }
- }
- lenYear := len(dataMap)
- for k, v := range dataMap {
- if i, ok := chartLegendMap[k]; ok {
- v.ChartLegend = strconv.Itoa(endYear - lenYear + i)
- }
- dataMap[k] = v
- }
- for _, v := range dataList {
- dataTimeT, _ := time.Parse(utils.FormatDate, v.DataTime)
- year := dataTimeT.Year()
- newItemDate := dataTimeT.AddDate(nowYear-year, 0, 0)
- for k, dateItem := range dataMap {
- tmpVal := data_manage.EdbDataList{
- EdbDataId: v.EdbDataId,
- EdbInfoId: v.EdbInfoId,
- DataTime: v.DataTime,
- DataTimestamp: v.DataTimestamp,
- Value: v.Value,
- }
- if (dateItem.StartDate.Before(dataTimeT) && dateItem.EndDate.After(dataTimeT)) || dateItem.StartDate == dataTimeT || dateItem.EndDate == dataTimeT {
- if jumpYear == 1 {
- //计算前一年最大的日期, 只补齐数据到去年
- beforeYearMaxDate := fmt.Sprintf("%d-12-31", dateItem.StartDate.Year())
- beforeYearMaxDateT, _ := time.Parse(utils.FormatDate, beforeYearMaxDate)
- if dataTimeT.Before(beforeYearMaxDateT) || dataTimeT == beforeYearMaxDateT {
- newItemDate = dataTimeT.AddDate(nowYear-year-1, 0, 0)
- } else {
- newItemDate = dataTimeT.AddDate(nowYear-year, 0, 0)
- }
- } else {
- newItemDate = dataTimeT.AddDate(nowYear-year, 0, 0)
- }
- timestamp := newItemDate.UnixNano() / 1e6
- tmpVal.DataTimestamp = timestamp
- tmpV := &tmpVal
- if findVal, ok := quarterMap[k]; !ok {
- findVal = append(findVal, tmpV)
- quarterMap[k] = findVal
- } else {
- findVal = append(findVal, tmpV)
- quarterMap[k] = findVal
- }
- if v.DataTime == latestDateStr {
- dateItem.CuttingDataTimestamp = timestamp
- dataMap[k] = dateItem
- }
- //break
- }
- }
- }
- for k, v := range dataMap {
- itemList := quarterMap[k]
- quarterItem := new(data_manage.QuarterData)
- quarterItem.Years = v.ShowName
- quarterItem.ChartLegend = v.ChartLegend
- if le, ok := legendMap[v.ShowName]; ok {
- if le != strconv.Itoa(v.StartDate.Year()) && le != strconv.Itoa(v.EndDate.Year()) {
- quarterItem.ChartLegend = le
- }
- }
- quarterItem.DataList = itemList
- quarterItem.CuttingDataTimestamp = v.CuttingDataTimestamp
- //如果等于最后的实际日期,那么将切割时间戳记录
- if quarterItem.CuttingDataTimestamp == 0 {
- //如果大于最后的实际日期,那么第一个点就是切割的时间戳
- if latestDate.Before(v.StartDate) && len(itemList) > 0 {
- quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100
- }
- }
- quarterDataList = append(quarterDataList, quarterItem)
- }
- if len(quarterDataList) > 0 {
- quarterDataListSort = quarterDataList
- sort.Sort(quarterDataListSort)
- }
- return
- }
- // GetSeasonEdbInfoDataListByXDateNong 季节性图的指标数据根据横轴选择农历时展示
- func GetSeasonEdbInfoDataListByXDateNong(result *data_manage.EdbDataResult, latestDate time.Time, seasonExtraConfig string, calendarPreYear int) (quarterDataListSort data_manage.QuarterDataList, err error) {
- xStartDate := "01-01"
- xEndDate := "12-31"
- jumpYear := 0
- legends := make([]data_manage.SeasonChartLegend, 0)
- var seasonExtra data_manage.SeasonExtraItem
- if seasonExtraConfig != "" {
- err = json.Unmarshal([]byte(seasonExtraConfig), &seasonExtra)
- if err != nil {
- return
- }
- }
- if seasonExtra.XStartDate != "" {
- xStartDate = seasonExtra.XStartDate
- xEndDate = seasonExtra.XEndDate
- jumpYear = seasonExtra.JumpYear
- legends = seasonExtra.ChartLegend
- }
- length := len(result.List)
- if length == 0 {
- return
- }
- legendMap := make(map[string]string, 0)
- if len(legends) > 0 {
- for _, v := range legends {
- legendMap[v.Name] = v.Value
- }
- }
- latestDateYear := latestDate.Year()
- //判断横轴的两个时间之间是不是跨年了,如果跨年了,则横轴截止年份比起始年份+1,如果不跨年,截止年份等于起始年份
- //根据数据确定最早的年份,和最近年份
- //根据横轴的日期,汇总所有的年份
- startYear := result.List[0].Year
- /*if jumpYear == 1 {
- if startYear != calendarPreYear {
- startYear = startYear - 1
- }
- }*/
- itemLength := len(result.List[length-1].Items)
- //获取数据的最新日期
- lastDate := result.List[length-1].Items[itemLength-1].DataTime
- maxY := result.List[length-1].Year
- lastDateT, tmpErr := time.Parse(utils.FormatDate, lastDate)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- endYear := lastDateT.Year()
- nowYear := time.Now().Year()
- dataMap := make(map[string]data_manage.QuarterXDateItem, 0)
- quarterDataList := make([]*data_manage.QuarterData, 0)
- resultData := make([]*data_manage.QuarterData, 0)
- quarterMap := make(map[string][]*data_manage.EdbDataList, 0)
- //整理出日期
- var startTmpT, endTmpT time.Time
- idx := 1
- chartLegendMap := make(map[string]int, 0)
- for currentStartYear := startYear; currentStartYear <= endYear; currentStartYear++ {
- startStr := fmt.Sprintf("%d-%s", currentStartYear, xStartDate)
- currentEndYear := currentStartYear
- if jumpYear == 1 {
- currentEndYear = currentStartYear + 1
- }
- endStr := fmt.Sprintf("%d-%s", currentEndYear, xEndDate)
- showName := fmt.Sprintf("%d_%d", currentStartYear, currentEndYear)
- startT, tEr := time.Parse(utils.FormatDate, startStr)
- if tEr != nil {
- err = tEr
- return
- }
- endT, tEr := time.Parse(utils.FormatDate, endStr)
- if tEr != nil {
- err = tEr
- return
- }
- if lastDateT.Before(startT) {
- //如果最新的日期在起始日之前,则跳出循环
- break
- }
- if endT.Year() > nowYear {
- //如果最新的日期比真实年份要大,则数据全部按照最大的年份补齐
- nowYear = endT.Year()
- }
- item := data_manage.QuarterXDateItem{
- StartDate: startT,
- EndDate: endT,
- ShowName: showName,
- }
- dataMap[showName] = item
- fmt.Println("年份" + showName + "日期" + startStr + " " + endStr)
- startTmpT = startT
- endTmpT = endT
- chartLegendMap[showName] = idx
- idx++
- if lastDateT.Before(endT) {
- //如果最新的日期在起始日之前,则跳出循环
- break
- }
- }
- lenYear := len(dataMap)
- for k, v := range dataMap {
- if i, ok := chartLegendMap[k]; ok {
- v.ChartLegend = strconv.Itoa(endYear - lenYear + i)
- }
- dataMap[k] = v
- }
- yearDataListMap := make(map[int]*data_manage.EdbDataItems, 0)
- for _, lv := range result.List {
- yearDataListMap[lv.Year] = lv
- }
- //判断哪些点应该落在同一条时间线上
- /*maxY := lastDateT.Year()
- changeFlag := false
- if lastDateT.Month() >= 11 {
- maxY = maxY + 1
- }
- if maxY < nowYear {
- changeFlag = true
- maxY = nowYear
- }*/
- /*endTmp := fmt.Sprintf("%d-%s", maxY, xEndDate)
- endTmpT, _ := time.Parse(utils.FormatDate, endTmp)
- minY := maxY
- if jumpYear == 1 {
- minY = maxY - 1
- }
- startTmp := fmt.Sprintf("%d-%s", minY, xStartDate)
- startTmpT, _ := time.Parse(utils.FormatDate, startTmp)*/
- fmt.Println("横轴截取日" + startTmpT.Format(utils.FormatDate) + " " + endTmpT.Format(utils.FormatDate))
- fmt.Printf("lastDateT.Year() 为%d \n", lastDateT.Year())
- fmt.Printf("maxY 为%d \n", maxY)
- for name, dateItem := range dataMap {
- tY := dateItem.EndDate.Year()
- if lastDateT.Month() >= 11 {
- if maxY > endTmpT.Year() {
- tY = tY + 1
- }
- }
- lv, ok1 := yearDataListMap[tY]
- fmt.Printf("name %s yearDataListMap[%d]\n", name, tY)
- if !ok1 {
- continue
- }
- for _, item := range lv.Items {
- tmpVal := data_manage.EdbDataList{
- EdbDataId: item.EdbDataId,
- EdbInfoId: item.EdbInfoId,
- DataTime: item.DataTime,
- DataTimestamp: item.DataTimestamp,
- Value: item.Value,
- }
- dataTimeT, _ := time.Parse(utils.FormatDate, item.DataTime)
- if (startTmpT.Before(dataTimeT) && endTmpT.After(dataTimeT)) || startTmpT == dataTimeT || endTmpT == dataTimeT {
- tmpV := &tmpVal
- if findVal, ok := quarterMap[name]; !ok {
- findVal = append(findVal, tmpV)
- quarterMap[name] = findVal
- } else {
- findVal = append(findVal, tmpV)
- quarterMap[name] = findVal
- }
- if lv.Year >= latestDateYear {
- // 切割的日期时间字符串
- cuttingDataTimeStr := latestDate.AddDate(0, 0, lv.BetweenDay).Format(utils.FormatDate)
- if item.DataTime == cuttingDataTimeStr {
- dateItem.CuttingDataTimestamp = tmpVal.DataTimestamp
- dataMap[name] = dateItem
- }
- }
- }
- }
- }
- for k, v := range dataMap {
- itemList := quarterMap[k]
- quarterItem := new(data_manage.QuarterData)
- quarterItem.Years = v.ShowName
- quarterItem.ChartLegend = v.ChartLegend
- if le, ok := legendMap[v.ShowName]; ok {
- if le != strconv.Itoa(v.StartDate.Year()) && le != strconv.Itoa(v.EndDate.Year()) {
- quarterItem.ChartLegend = le
- }
- }
- quarterItem.DataList = itemList
- quarterItem.CuttingDataTimestamp = v.CuttingDataTimestamp
- //如果等于最后的实际日期,那么将切割时间戳记录
- if quarterItem.CuttingDataTimestamp == 0 {
- //如果大于最后的实际日期,那么第一个点就是切割的时间戳
- if latestDate.Before(v.StartDate) && len(itemList) > 0 {
- quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100
- }
- }
- quarterDataList = append(quarterDataList, quarterItem)
- }
- if result.List[0].Year != calendarPreYear {
- itemList := make([]*data_manage.EdbDataList, 0)
- items := new(data_manage.QuarterData)
- //items.Year = calendarPreYear
- items.DataList = itemList
- newResult := make([]*data_manage.QuarterData, 0)
- newResult = append(newResult, items)
- newResult = append(newResult, quarterDataList...)
- resultData = newResult
- } else {
- resultData = quarterDataList
- }
- if len(quarterDataList) > 0 {
- quarterDataListSort = resultData
- sort.Sort(quarterDataListSort)
- }
- return
- }
- // BarChartData 柱方图的数据处理
- func BarChartData(mappingList []*data_manage.ChartEdbInfoMapping, edbDataListMap map[int][]*data_manage.EdbDataList, barChartInfoDateList []data_manage.BarChartInfoDateReq, barChartInfoSort data_manage.BarChartInfoSortReq) (edbIdList []int, yDataList []data_manage.YData, err error) {
- // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
- edbDataMap := make(map[int]map[string]float64)
- for edbInfoId, edbDataList := range edbDataListMap {
- edbDateData := make(map[string]float64)
- for _, edbData := range edbDataList {
- edbDateData[edbData.DataTime] = edbData.Value
- }
- edbDataMap[edbInfoId] = edbDateData
- }
- // edbIdList 指标展示顺序;x轴的指标顺序
- edbIdList = make([]int, 0)
- //Sort int `description:"排序类型,0:默认,1:升序,2:降序"`
- dateData := make(map[int]float64)
- if barChartInfoSort.Sort == 0 {
- for _, v := range mappingList {
- edbIdList = append(edbIdList, v.EdbInfoId)
- }
- } else {
- lenBarChartInfoDateList := len(barChartInfoDateList)
- if barChartInfoSort.DateIndex >= lenBarChartInfoDateList {
- err = errors.New("排序日期异常")
- return
- }
- notDataEdbIdList := make([]int, 0) //没有数据的指标id
- // 日期配置
- barChartInfoDate := barChartInfoDateList[barChartInfoSort.DateIndex]
- for edbInfoId, dataList := range edbDataListMap {
- if len(dataList) <= 0 {
- // 没有数据的指标id
- notDataEdbIdList = append(notDataEdbIdList, edbInfoId)
- continue
- }
- findDate := barChartInfoDate.Date
- switch barChartInfoDate.Type {
- case 1: //最新值
- findDate = dataList[len(dataList)-1].DataTime
- case 2: //近期几天
- findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- findDateTime = findDateTime.AddDate(0, 0, -barChartInfoDate.Value)
- lenData := len(dataList) - 1
- for i := lenData; i >= 0; i-- {
- currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
- findDate = dataList[i].DataTime
- break
- }
- }
- case 3: // 固定日期
- //最早的日期
- minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- //寻找固定日期的数据
- findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
- tmpDate := tmpDateTime.Format(utils.FormatDate)
- if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
- findDate = tmpDate
- break
- }
- }
- default:
- err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
- return
- }
- if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
- dateData[edbInfoId] = tmpValue
- } else {
- // 没有数据的指标id
- notDataEdbIdList = append(notDataEdbIdList, edbInfoId)
- }
- }
- //Sort int `description:"排序类型,0:默认,1:升序,2:降序"`
- // 排序
- dateDataSort := utils.NewMapSorter(dateData)
- sort.Sort(dateDataSort)
- if barChartInfoSort.Sort == 1 {
- // 先将没有数据的指标id放在最前面
- if len(notDataEdbIdList) > 0 {
- edbIdList = append(edbIdList, notDataEdbIdList...)
- }
- for _, v := range dateDataSort {
- edbIdList = append(edbIdList, v.Key)
- }
- } else {
- for i := len(dateDataSort) - 1; i >= 0; i-- {
- edbIdList = append(edbIdList, dateDataSort[i].Key)
- }
- // 再将没有数据的指标id放在最后面
- if len(notDataEdbIdList) > 0 {
- edbIdList = append(edbIdList, notDataEdbIdList...)
- }
- }
- }
- yDataList = make([]data_manage.YData, 0) //y轴的数据列表
- for _, barChartInfoDate := range barChartInfoDateList {
- var maxDate time.Time
- findDataList := make([]float64, 0) // 当前日期的数据值
- for _, edbInfoId := range edbIdList {
- findDate := barChartInfoDate.Date //需要的日期值
- dataList := edbDataListMap[edbInfoId] //指标的所有数据值
- if len(dataList) <= 0 {
- // 没有数据的指标id
- findDataList = append(findDataList, 0)
- continue
- }
- switch barChartInfoDate.Type {
- case 1: //最新值
- dataList := edbDataListMap[edbInfoId]
- findDate = dataList[len(dataList)-1].DataTime
- case 2: //近期几天
- findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- findDateTime = findDateTime.AddDate(0, 0, -barChartInfoDate.Value)
- lenData := len(dataList) - 1
- for i := lenData; i >= 0; i-- {
- currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
- findDate = dataList[i].DataTime
- break
- }
- }
- case 3: // 固定日期
- //最早的日期
- minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- //寻找固定日期的数据
- findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
- tmpDate := tmpDateTime.Format(utils.FormatDate)
- if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
- findDate = tmpDate
- break
- }
- }
- default:
- err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
- return
- }
- findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
- if maxDate.IsZero() {
- maxDate = findDateTime
- } else {
- if findDateTime.After(maxDate) {
- maxDate = findDateTime
- }
- }
- if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
- tmpValue, _ = decimal.NewFromFloat(tmpValue).Round(4).Float64()
- findDataList = append(findDataList, tmpValue)
- } else {
- findDataList = append(findDataList, 0)
- }
- }
- yDate := "0000-00-00"
- if !maxDate.IsZero() {
- yDate = maxDate.Format(utils.FormatDate)
- }
- yDataList = append(yDataList, data_manage.YData{
- Date: yDate,
- Value: findDataList,
- Color: barChartInfoDate.Color,
- Name: barChartInfoDate.Name,
- })
- }
- return
- }
- // ChartInfoRefreshV2 图表刷新
- // @author Roc
- // @datetime 2022-09-16 10:15:38
- // @description 将原来自己写的一套获取所有关联指标,然后刷新指标逻辑 改成 只获取使用的指标id,然后遍历去调用“指标刷新服务”
- func ChartInfoRefreshV2(chartInfoId int) (err error, isAsync bool) {
- var errmsg string
- defer func() {
- if err != nil {
- go alarm_msg.SendAlarmMsg("ChartInfoRefresh:"+errmsg, 3)
- //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "ChartInfoRefresh:"+errmsg, utils.EmailSendToUsers)
- fmt.Println("ChartInfoRefresh Err:" + errmsg)
- }
- }()
- edbMappingList, err := data_manage.GetChartEdbMappingList(chartInfoId)
- if err != nil {
- errmsg = "获取需要刷新的指标失败:Err:" + err.Error()
- return
- }
- edbIdList := make([]int, 0)
- for _, v := range edbMappingList {
- edbIdList = append(edbIdList, v.EdbInfoId)
- }
- // 批量刷新
- err, isAsync = EdbInfoRefreshAllFromBaseV3(edbIdList, false, false, false)
- if err != nil {
- return
- }
- return
- }
- // BatchChartInfoRefreshV2 图表批量刷新
- // @author Roc
- // @datetime 2022-09-16 10:15:38
- // @description 将原来自己写的一套获取所有关联指标,然后刷新指标逻辑 改成 只获取使用的指标id,然后遍历去调用“指标刷新服务”
- func BatchChartInfoRefreshV2(chartInfoList []*data_manage.ChartInfo, redisKey string) (err error, isAsync bool) {
- if len(chartInfoList) <= 0 {
- return
- }
- chartInfoIdSlice := make([]int, 0)
- for _, chartInfo := range chartInfoList {
- chartInfoIdSlice = append(chartInfoIdSlice, chartInfo.ChartInfoId)
- }
- var errMsg string
- defer func() {
- if err != nil {
- go alarm_msg.SendAlarmMsg("ChartInfoRefresh:"+errMsg, 3)
- //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "ChartInfoRefresh:"+errmsg, utils.EmailSendToUsers)
- fmt.Println("ChartInfoRefresh Err:" + errMsg)
- }
- }()
- // 是否删除缓存
- isDeleteCache := true
- if redisKey != `` {
- //设置最多10分钟缓存
- utils.Rc.SetNX(redisKey, 1, time.Minute*10)
- defer func() {
- if isDeleteCache {
- utils.Rc.Delete(redisKey)
- }
- }()
- }
- // 获取需要刷新的指标信息
- edbIdList := make([]int, 0)
- edbInfoIdMap := make(map[int]int)
- edbMappingList, err := data_manage.GetChartEdbMappingListByIdList(chartInfoIdSlice)
- for _, v := range edbMappingList {
- if _, ok := edbInfoIdMap[v.EdbInfoId]; !ok {
- edbIdList = append(edbIdList, v.EdbInfoId)
- edbInfoIdMap[v.EdbInfoId] = v.EdbInfoId
- }
- }
- // 获取需要刷新的指标列表
- newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr := getEdbInfoIdList(edbIdList)
- // 需要刷新的指标数量
- totalEdbInfo := len(newBaseEdbInfoArr) + len(calculateArr) + len(predictCalculateArr)
- if totalEdbInfo > 20 { // 关联指标过多的时候,异步刷新
- isAsync = true
- isDeleteCache = false // 不删除缓存
- go refreshChartEdbInfo(chartInfoList, redisKey, newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr)
- } else {
- err = edbInfoRefreshAll(false, newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr)
- //清除图表缓存
- for _, v := range chartInfoList {
- key := utils.HZ_CHART_LIB_DETAIL + v.UniqueCode
- _ = utils.Rc.Delete(key)
- }
- }
- return
- }
- // refreshChartEdbInfo
- // @Description: 刷新图表中的质保列表
- // @author: Roc
- // @datetime 2023-12-01 16:05:39
- // @param chartInfoList []*data_manage.ChartInfo
- // @param redisKey string
- // @param newBaseEdbInfoArr []*data_manage.EdbInfo
- // @param newBasePredictEdbInfoArr []*data_manage.EdbInfo
- // @param newCalculateMap map[int]*data_manage.EdbInfo
- // @param newPredictCalculateMap map[int]*data_manage.EdbInfo
- // @param calculateArr []int
- // @param predictCalculateArr []int
- func refreshChartEdbInfo(chartInfoList []*data_manage.ChartInfo, redisKey string, newBaseEdbInfoArr, newBasePredictEdbInfoArr []*data_manage.EdbInfo, newCalculateMap, newPredictCalculateMap map[int]*data_manage.EdbInfo, calculateArr, predictCalculateArr []int) {
- edbInfoRefreshAll(false, newBaseEdbInfoArr, newBasePredictEdbInfoArr, newCalculateMap, newPredictCalculateMap, calculateArr, predictCalculateArr)
- //清除图表缓存
- for _, v := range chartInfoList {
- key := utils.HZ_CHART_LIB_DETAIL + v.UniqueCode
- _ = utils.Rc.Delete(key)
- }
- // 如果是有插入redisKey,那么刷新结束后就清除缓存
- if redisKey != `` {
- utils.Rc.Delete(redisKey)
- }
- }
- // GetBatchChartRefreshKey
- // @Description: 获取批量刷新key
- // @author: Roc
- // @datetime 2023-11-30 13:30:26
- // @param source string
- // @param reportId int
- // @param reportChapterId int
- // @return string
- func GetBatchChartRefreshKey(source string, reportId, reportChapterId int) string {
- if source == `` {
- return ``
- }
- return fmt.Sprint("batch_chart_refresh:", source, ":", reportId, ":", reportChapterId)
- }
- // CheckBatchChartRefreshResult
- // @Description: 判断报告中的图表是否刷新完成
- // @author: Roc
- // @datetime 2023-11-30 13:30:26
- // @param source string
- // @param reportId int
- // @param reportChapterId int
- // @return string
- func CheckBatchChartRefreshResult(source string, reportId, reportChapterId int) (refreshResult bool) {
- refreshResult = true
- redisKey := GetBatchChartRefreshKey(source, reportId, reportChapterId)
- if redisKey != `` {
- // 如果找到了key,那么就是还在更新中
- isOk := utils.Rc.IsExist(redisKey)
- if isOk {
- refreshResult = false
- }
- }
- return
- }
- func CheckIsEnChart(chartNameEn string, edbList []*data_manage.ChartEdbInfoMapping, source, chartType int) bool {
- // 相关性图表不判断指标
- if utils.InArrayByInt([]int{utils.CHART_SOURCE_CORRELATION, utils.CHART_SOURCE_ROLLING_CORRELATION, utils.CHART_SOURCE_LINE_EQUATION}, source) && chartNameEn != "" {
- return true
- }
- if chartNameEn != "" && len(edbList) <= 0 {
- return true
- }
- if chartNameEn == "" {
- return false
- }
- // 截面散点图的话,肯定是有英文配置的
- if chartType == utils.CHART_TYPE_SECTION_SCATTER {
- return true
- }
- for _, v := range edbList {
- if v.EdbNameEn == "" {
- return false
- }
- if v.Unit != "无" && v.Unit != "" && v.UnitEn == "" {
- return false
- }
- }
- return true
- }
- func CheckIsEnEdb(edbNameEn, unit, unitEn string) bool {
- if edbNameEn == "" {
- return false
- }
- if unit != "无" && unit != "" && unitEn == "" {
- return false
- }
- return true
- }
- // CheckIsDisableChart 根据指标Id判断是否是禁用图表
- // return int 0:启用,1:禁用
- func CheckIsDisableChart(edbInfoIdArr []int) (isDisable int) {
- crmConfig, err := company.GetConfigDetailByCode("chart_disabled_edb")
- if err != nil {
- return
- }
- //没有配置,立马返回
- if crmConfig.ConfigValue == `` {
- return
- }
- confEdbInfoIdList := strings.Split(crmConfig.ConfigValue, ",")
- for _, edbInfoId := range edbInfoIdArr {
- for _, confEdbInfoId := range confEdbInfoIdList {
- if strconv.Itoa(edbInfoId) == confEdbInfoId {
- isDisable = 1
- return
- }
- }
- }
- return
- }
- // CheckChartExtraConfig 校验图表额外配置的信息,并且获取相关联的指标id
- func CheckChartExtraConfig(chartType int, extraConfigStr string) (edbIdList []int, err error, errMsg string) {
- switch chartType {
- case 10: //截面散点
- var extraConfig data_manage.SectionScatterReq
- err = json.Unmarshal([]byte(extraConfigStr), &extraConfig)
- if err != nil {
- return
- }
- // 判断是否有配置日期序列
- if len(extraConfig.SeriesList) <= 0 {
- errMsg = `请配置序列`
- err = errors.New(errMsg)
- return
- }
- // 判断是否有填写指标
- if len(extraConfig.SeriesList[0].EdbInfoList) <= 0 {
- errMsg = `请选择指标`
- err = errors.New(errMsg)
- return
- }
- // 遍历指标列表获取指标id
- edbIdMap := make(map[int]int)
- for _, v := range extraConfig.SeriesList[0].EdbInfoList {
- // X 轴的指标id
- if _, ok := edbIdMap[v.XEdbInfoId]; !ok {
- edbIdMap[v.XEdbInfoId] = v.XEdbInfoId
- edbIdList = append(edbIdList, v.XEdbInfoId)
- }
- // Y 轴的指标id
- if _, ok := edbIdMap[v.YEdbInfoId]; !ok {
- edbIdMap[v.YEdbInfoId] = v.YEdbInfoId
- edbIdList = append(edbIdList, v.YEdbInfoId)
- }
- }
- case utils.CHART_TYPE_RADAR:
- var extraConfig data_manage.RadarChartInfoReq
- if extraConfigStr == `` {
- errMsg = "雷达图未配置"
- err = errors.New(errMsg)
- return
- }
- err = json.Unmarshal([]byte(extraConfigStr), &extraConfig)
- if err != nil {
- errMsg = "雷达图配置异常"
- err = errors.New(errMsg)
- return
- }
- }
- return
- }
- // GetSectionScatterChartData 截面散点图的数据处理
- func GetSectionScatterChartData(chartInfoId int, mappingList []*data_manage.ChartEdbInfoMapping, edbDataListMap map[int][]*data_manage.EdbDataList, extraConfig data_manage.SectionScatterReq) (edbIdList []int, chartDataResp data_manage.SectionScatterInfoResp, err error) {
- // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
- edbDataMap := make(map[int]map[string]float64)
- for edbInfoId, edbDataList := range edbDataListMap {
- edbDateData := make(map[string]float64)
- for _, edbData := range edbDataList {
- edbDateData[edbData.DataTime] = edbData.Value
- }
- edbDataMap[edbInfoId] = edbDateData
- }
- // edbIdList 指标展示顺序;x轴的指标顺序
- edbIdList = make([]int, 0)
- edbMappingMap := make(map[int]*data_manage.ChartEdbInfoMapping)
- for _, v := range mappingList {
- edbIdList = append(edbIdList, v.EdbInfoId)
- edbMappingMap[v.EdbInfoId] = v
- }
- //SectionScatterSeriesInfoResp
- dataListResp := make([]data_manage.SectionScatterSeriesItemResp, 0) //y轴的数据列表
- for _, seriesItem := range extraConfig.SeriesList {
- var maxDate time.Time
- // 系列中的指标数据
- tmpSeriesEdbInfoList := make([]data_manage.SectionScatterEdbItemResp, 0)
- var minXVal, maxXVal, minYVal, maxYVal float64
- for _, edbConf := range seriesItem.EdbInfoList {
- tmpItem := data_manage.SectionScatterEdbItemResp{
- IsShow: edbConf.IsShow,
- Name: edbConf.Name,
- NameEn: edbConf.NameEn,
- } //单个坐标点的数据
- //X轴的数据
- {
- edbInfoId := edbConf.XEdbInfoId //X轴的指标
- edbMappingInfo, ok := edbMappingMap[edbInfoId]
- if !ok {
- continue
- }
- findDate := edbConf.XDate //需要的日期值
- dataList := edbDataListMap[edbInfoId] //指标的所有数据值
- if len(dataList) <= 0 {
- // 没有数据的指标id
- //findDataList = append(findDataList, 0)
- continue
- }
- tmpItem.XEdbInfoId = edbInfoId
- tmpItem.XName = edbMappingInfo.EdbName
- tmpItem.XNameEn = edbMappingInfo.EdbNameEn
- switch edbConf.XDateType {
- case 1: //最新值
- findDate = dataList[len(dataList)-1].DataTime
- case 2: //近期几天
- findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- findDateTime = findDateTime.AddDate(0, 0, -edbConf.XDateValue)
- lenData := len(dataList) - 1
- for i := lenData; i >= 0; i-- {
- currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
- findDate = dataList[i].DataTime
- break
- }
- }
- case 3: // 固定日期
- //最早的日期
- minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- //寻找固定日期的数据
- findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, edbConf.XDate, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
- tmpDate := tmpDateTime.Format(utils.FormatDate)
- if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
- findDate = tmpDate
- break
- }
- }
- default:
- err = errors.New(fmt.Sprint("日期类型异常,Type:", edbConf.XDate))
- return
- }
- findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
- if maxDate.IsZero() {
- maxDate = findDateTime
- } else {
- if findDateTime.After(maxDate) {
- maxDate = findDateTime
- }
- }
- if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
- tmpItem.XDate = findDate
- tmpItem.XValue = tmpValue
- } else {
- continue
- }
- }
- //Y轴的数据
- {
- edbInfoId := edbConf.YEdbInfoId //Y轴的指标
- edbMappingInfo, ok := edbMappingMap[edbInfoId]
- if !ok {
- continue
- }
- findDate := edbConf.YDate //需要的日期值
- dataList := edbDataListMap[edbInfoId] //指标的所有数据值
- if len(dataList) <= 0 {
- // 没有数据的指标id
- //findDataList = append(findDataList, 0)
- continue
- }
- tmpItem.YEdbInfoId = edbInfoId
- tmpItem.YName = edbMappingInfo.EdbName
- tmpItem.YNameEn = edbMappingInfo.EdbNameEn
- switch edbConf.YDateType {
- case 1: //最新值
- findDate = dataList[len(dataList)-1].DataTime
- case 2: //近期几天
- findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- findDateTime = findDateTime.AddDate(0, 0, -edbConf.YDateValue)
- lenData := len(dataList) - 1
- for i := lenData; i >= 0; i-- {
- currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
- findDate = dataList[i].DataTime
- break
- }
- }
- case 3: // 固定日期
- //最早的日期
- minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- //寻找固定日期的数据
- findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, edbConf.YDate, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
- tmpDate := tmpDateTime.Format(utils.FormatDate)
- if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
- findDate = tmpDate
- break
- }
- }
- default:
- err = errors.New(fmt.Sprint("日期类型异常,Type:", edbConf.YDate))
- return
- }
- findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
- if maxDate.IsZero() {
- maxDate = findDateTime
- } else {
- if findDateTime.After(maxDate) {
- maxDate = findDateTime
- }
- }
- if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
- tmpItem.YDate = findDate
- tmpItem.YValue = tmpValue
- } else {
- continue
- }
- }
- // 获取当前系列的X轴的最大最小值
- {
- if tmpItem.XValue < minXVal {
- minXVal = tmpItem.XValue
- }
- if tmpItem.XValue > maxXVal {
- maxXVal = tmpItem.XValue
- }
- if tmpItem.YValue < minYVal {
- minYVal = tmpItem.YValue
- }
- if tmpItem.YValue > maxYVal {
- maxYVal = tmpItem.YValue
- }
- }
- tmpSeriesEdbInfoList = append(tmpSeriesEdbInfoList, tmpItem)
- }
- trendLimitData := make([]data_manage.CoordinatePoint, 0) //趋势线的前后坐标点
- var trendLine, rSquare string
- // 生成线性方程式
- var a, b float64
- {
- coordinateData := make([]utils.Coordinate, 0)
- for _, tmpSeriesEdbInfo := range tmpSeriesEdbInfoList {
- tmpCoordinate1 := utils.Coordinate{
- X: tmpSeriesEdbInfo.XValue,
- Y: tmpSeriesEdbInfo.YValue,
- }
- coordinateData = append(coordinateData, tmpCoordinate1)
- }
- // 只有存在两个坐标点的时候,才能去计算线性方程和R平方
- if len(coordinateData) >= 2 {
- a, b = utils.GetLinearResult(coordinateData)
- if !math.IsNaN(a) && !math.IsNaN(b) {
- if b > 0 {
- trendLine = fmt.Sprintf("y=%sx+%s", utils.SubFloatToString(a, 4), utils.SubFloatToString(b, 4))
- } else {
- trendLine = fmt.Sprintf("y=%sx%s", utils.SubFloatToString(a, 4), utils.SubFloatToString(b, 4))
- }
- minYVal, _ = decimal.NewFromFloat(a).Mul(decimal.NewFromFloat(minXVal)).Add(decimal.NewFromFloat(b)).Round(4).Float64()
- maxYVal, _ = decimal.NewFromFloat(a).Mul(decimal.NewFromFloat(maxXVal)).Add(decimal.NewFromFloat(b)).Round(4).Float64()
- }
- // 计算R平方
- rSquare = fmt.Sprint(utils.CalculationDecisive(coordinateData))
- }
- trendLimitData = append(trendLimitData, data_manage.CoordinatePoint{
- X: minXVal,
- Y: minYVal,
- }, data_manage.CoordinatePoint{
- X: maxXVal,
- Y: maxYVal,
- })
- }
- dataListResp = append(dataListResp, data_manage.SectionScatterSeriesItemResp{
- Name: seriesItem.Name,
- NameEn: seriesItem.NameEn,
- IsNameDefault: seriesItem.IsNameDefault,
- Color: seriesItem.Color,
- EdbInfoList: tmpSeriesEdbInfoList,
- ShowTrendLine: seriesItem.ShowTrendLine,
- ShowFitEquation: seriesItem.ShowFitEquation,
- ShowRSquare: seriesItem.ShowRSquare,
- TrendLine: trendLine,
- RSquare: rSquare,
- TrendLimitData: trendLimitData,
- })
- }
- // 截面散点图点击详情时自动更新系列名
- if len(extraConfig.SeriesList) > 0 {
- // 默认名字的时候才自动更新
- if extraConfig.SeriesList[0].IsNameDefault {
- firstXEdbInfoId := extraConfig.SeriesList[0].EdbInfoList[0].XEdbInfoId
- needUpdate := false
- if v, ok := edbMappingMap[firstXEdbInfoId]; ok {
- extraConfig.SeriesList[0].Name = v.LatestDate
- extraConfig.SeriesList[0].NameEn = v.LatestDate
- dataListResp[0].Name = v.LatestDate
- dataListResp[0].NameEn = v.LatestDate
- needUpdate = true
- }
- extraConfigByte, e := json.Marshal(extraConfig)
- if e != nil {
- errMsg := "截面散点系列更新异常"
- err = errors.New(errMsg)
- return
- }
- extraConfigStr := string(extraConfigByte)
- if needUpdate {
- err = data_manage.EditChartInfoExtraConfig(chartInfoId, extraConfigStr)
- if err != nil {
- errMsg := "截面散点系列更新异常"
- err = errors.New(errMsg)
- return
- }
- }
- }
- }
- chartDataResp = data_manage.SectionScatterInfoResp{
- XName: extraConfig.XName,
- XNameEn: extraConfig.XNameEn,
- XUnitName: extraConfig.XUnitName,
- XUnitNameEn: extraConfig.XUnitNameEn,
- YName: extraConfig.YName,
- YNameEn: extraConfig.YNameEn,
- YUnitName: extraConfig.YUnitName,
- YUnitNameEn: extraConfig.YUnitNameEn,
- XMinValue: extraConfig.XMinValue,
- XMaxValue: extraConfig.XMaxValue,
- YMinValue: extraConfig.YMinValue,
- YMaxValue: extraConfig.YMaxValue,
- DataList: dataListResp,
- }
- return
- }
- // AddChartInfo 添加图表
- func AddChartInfo(req data_manage.AddChartInfoReq, sysUserId int, sysUserRealName string) (chartInfo *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
- isSendEmail = true // 默认错误的时候要发送邮件
- req.ChartName = strings.Trim(req.ChartName, " ")
- if req.ChartName == "" {
- errMsg = "请填写图表名称!"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- if req.ChartClassifyId <= 0 {
- errMsg = "分类参数错误!"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- //if req.ChartThemeId <= 0 {
- // errMsg = "请选择主题"
- // err = errors.New(errMsg)
- // isSendEmail = false
- // return
- //}
- //
- //// 判断主题是否存在
- //_, err = chart_theme.GetChartThemeId(req.ChartThemeId)
- //if err != nil {
- // if err.Error() == utils.ErrNoRow() {
- // errMsg = "主题不存在"
- // err = errors.New(errMsg)
- // return
- // }
- // errMsg = "获取主题信息失败"
- // return
- //}
- chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "分类不存在"
- err = errors.New(errMsg)
- return
- }
- errMsg = "获取分类信息失败"
- return
- }
- if chartClassify == nil {
- errMsg = "分类不存在"
- err = errors.New(errMsg)
- return
- }
- chartType := req.ChartType
- extraConfig := req.ExtraConfig
- var extraConfigEdbInfoIdArr []int
- extraConfigEdbInfoIdArr, err, errMsg = CheckChartExtraConfig(chartType, extraConfig)
- if err != nil {
- err = errors.New("添加失败:" + err.Error())
- return
- }
- // 季节性图表额外配置信息
- var seasonExtraConfig string
- // 关联指标
- var edbInfoIdArr []int
- chartEdbInfoList := req.ChartEdbInfoList
- if len(chartEdbInfoList) > 0 {
- for _, v := range chartEdbInfoList {
- edbInfoId := v.EdbInfoId
- edbInfo, tmpErr := data_manage.GetEdbInfoById(edbInfoId)
- if tmpErr != nil {
- if tmpErr.Error() == utils.ErrNoRow() {
- errMsg = "指标不存在!"
- err = errors.New("指标不存在,edbInfoId:" + strconv.Itoa(edbInfoId))
- return
- } else {
- errMsg = "获取指标信息失败!"
- err = errors.New("获取图表的指标信息失败,Err:" + tmpErr.Error())
- return
- }
- }
- if edbInfo == nil {
- errMsg = "指标已被删除,请重新选择!"
- err = errors.New("指标不存在,edbInfoId:" + strconv.Itoa(edbInfoId))
- return
- } else {
- if edbInfo.EdbInfoId <= 0 {
- errMsg = "指标已被删除,请重新选择!"
- err = errors.New("指标不存在,edbInfoId:" + strconv.Itoa(edbInfoId))
- return
- }
- }
- edbInfoIdArr = append(edbInfoIdArr, edbInfoId)
- edbInfo.EdbNameSource = edbInfo.EdbName
- //edbList = append(edbList, edbInfo)
- //新增季节性图表数据计算
- if chartType == 2 {
- existCount, tmpErr := data_manage.GetEdbDataQuarterCount(edbInfoId)
- if tmpErr != nil {
- errMsg = "保存失败"
- err = errors.New("判断季节性图表,指标数据是否存在失败,Err:" + tmpErr.Error())
- return
- }
- if existCount <= 0 {
- go data_manage.AddCalculateQuarter(edbInfoId, edbInfo.Source, edbInfo.EdbCode)
- }
- // 处理季节性图表横轴配置
- {
- if req.SeasonExtraConfig.XEndDate != "" {
- if req.SeasonExtraConfig.XStartDate > req.SeasonExtraConfig.XEndDate && req.SeasonExtraConfig.JumpYear != 1 {
- errMsg = "季节性图表配置信息异常:横坐标日期配置错误"
- err = errors.New("季节性图表配置信息异常: 横坐标日期配置错误")
- return
- }
- seasonExtra, tErr := json.Marshal(req.SeasonExtraConfig)
- if tErr != nil {
- errMsg = "季节性图表配置信息异常"
- err = errors.New("季节性图表配置信息异常,Err:" + tErr.Error())
- return
- }
- seasonExtraConfig = string(seasonExtra)
- }
- }
- }
- }
- } else {
- if extraConfig == `` {
- errMsg = "请选择指标!"
- err = errors.New(errMsg)
- return
- }
- edbInfoIdArr = extraConfigEdbInfoIdArr
- lenEdbInfoIdArr := len(edbInfoIdArr)
- if lenEdbInfoIdArr > 0 {
- tmpEdbList, tmpErr := data_manage.GetEdbInfoByIdList(edbInfoIdArr)
- if tmpErr != nil {
- errMsg = "指标异常!"
- err = errors.New("指标异常!Err:" + tmpErr.Error())
- return
- }
- if len(tmpEdbList) != lenEdbInfoIdArr {
- errMsg = "指标异常!"
- err = errors.New(fmt.Sprint("查找出来的指标数量与选择的指标数量不符!查找出来的指标数量:", len(tmpEdbList), ";用户选择的指标数量:", lenEdbInfoIdArr))
- return
- }
- for _, v := range tmpEdbList {
- chartEdbInfoList = append(chartEdbInfoList, &data_manage.ChartSaveItem{
- EdbInfoId: v.EdbInfoId,
- MaxData: 0,
- MinData: 0,
- IsOrder: false,
- IsAxis: 1,
- EdbInfoType: 1,
- LeadValue: 0,
- LeadUnit: "",
- ChartStyle: "",
- ChartColor: "",
- PredictChartColor: "",
- ChartWidth: 0,
- Source: utils.CHART_SOURCE_DEFAULT,
- })
- }
- }
- }
- if len(edbInfoIdArr) <= 0 {
- errMsg = "请选择指标!"
- err = errors.New(errMsg)
- return
- }
- // 图表额外配置
- extraConfig, err, errMsg = HandleExtraConfig(chartType, extraConfig)
- if err != nil {
- if errMsg == `` {
- errMsg = "指标异常!"
- }
- err = errors.New("指标异常!Err:" + err.Error())
- return
- }
- sort.Ints(edbInfoIdArr)
- var edbInfoIdArrStr []string
- for _, v := range edbInfoIdArr {
- edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
- }
- edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
- //count, err := data_manage.ChartInfoExist("", edbInfoIdStr)
- //if err != nil && err.Error() != utils.ErrNoRow() {
- // br.Msg = "保存失败"
- // br.ErrMsg = "判断图表名称是否存在失败,Err:" + err.Error()
- // return
- //}
- //if count > 0 {
- // br.Msg = "所选指标已存在相同指标"
- // return
- //}
- //判断图表是否存在
- var condition string
- var pars []interface{}
- // 图表名称在不同图分类下不允许重复 需求调整时间:2022年11月07日09:47:07
- //condition += " AND chart_classify_id=? "
- //pars = append(pars, req.ChartClassifyId)
- condition += " AND chart_name=? AND source = ? "
- pars = append(pars, req.ChartName, utils.CHART_SOURCE_DEFAULT)
- count, err := data_manage.GetChartInfoCountByCondition(condition, pars)
- if err != nil {
- errMsg = "判断图表名称是否存在失败"
- err = errors.New("判断图表名称是否存在失败,Err:" + err.Error())
- return
- }
- if count > 0 {
- errMsg = "图表已存在,请重新填写"
- err = errors.New("判断图表名称是否存在失败")
- isSendEmail = false
- return
- }
- barChartConf := `` //柱方图的配置信息
- if chartType == 7 {
- barChartConfReq := req.BarChartInfo
- if len(barChartConfReq.EdbInfoIdList) <= 0 {
- errMsg = "请选择指标"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- if len(barChartConfReq.DateList) <= 0 {
- errMsg = "请选择日期"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- barChartConfByte, tmpErr := json.Marshal(barChartConfReq)
- if tmpErr != nil {
- errMsg = "柱方图信息异常"
- err = errors.New("柱方图信息异常,Err:" + tmpErr.Error())
- return
- }
- barChartConf = string(barChartConfByte)
- }
- disableVal := CheckIsDisableChart(edbInfoIdArr)
- chartInfo = new(data_manage.ChartInfo)
- chartInfo.ChartName = req.ChartName
- chartInfo.EdbInfoIds = edbInfoIdStr
- chartInfo.ChartClassifyId = req.ChartClassifyId
- chartInfo.SysUserId = sysUserId
- chartInfo.SysUserRealName = sysUserRealName
- chartInfo.CreateTime = time.Now()
- chartInfo.ModifyTime = time.Now()
- chartInfo.IsSetName = 0
- timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
- chartInfo.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
- // todo 判断是否需要重新计算用户的start_date
- if req.DateType > 0 {
- chartInfo.DateType = req.DateType
- } else {
- chartInfo.DateType = 3
- }
- if chartType == 0 {
- chartType = 1
- }
- chartInfo.ChartType = chartType
- calendar := req.Calendar
- if calendar == "" {
- calendar = "公历"
- }
- chartInfo.Calendar = calendar
- chartInfo.StartDate = req.StartDate
- chartInfo.EndDate = req.EndDate
- chartInfo.SeasonStartDate = req.StartDate
- chartInfo.SeasonEndDate = req.EndDate
- chartInfo.LeftMin = req.LeftMin
- chartInfo.LeftMax = req.LeftMax
- chartInfo.RightMin = req.RightMin
- chartInfo.RightMax = req.RightMax
- chartInfo.Disabled = disableVal
- chartInfo.BarConfig = barChartConf
- chartInfo.ExtraConfig = extraConfig
- chartInfo.SeasonExtraConfig = seasonExtraConfig
- chartInfo.StartYear = req.StartYear
- chartInfo.Source = utils.CHART_SOURCE_DEFAULT
- chartInfo.ChartThemeId = req.ChartThemeId
- chartInfo.SourcesFrom = req.SourcesFrom
- chartInfo.Instructions = req.Instructions
- chartInfo.MarkersLines = req.MarkersLines
- chartInfo.MarkersAreas = req.MarkersAreas
- chartInfo.Unit = req.Unit
- chartInfo.UnitEn = req.UnitEn
- newId, err := data_manage.AddChartInfo(chartInfo)
- if err != nil {
- errMsg = `保存失败`
- err = errors.New("保存失败,Err:" + err.Error())
- return
- }
- chartInfo.ChartInfoId = int(newId)
- mapList := make([]*data_manage.ChartEdbMapping, 0)
- for _, v := range chartEdbInfoList {
- mapItem := new(data_manage.ChartEdbMapping)
- mapItem.ChartInfoId = int(newId)
- mapItem.EdbInfoId = v.EdbInfoId
- mapItem.CreateTime = time.Now()
- mapItem.ModifyTime = time.Now()
- timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
- mapItem.UniqueCode = utils.MD5(fmt.Sprint(utils.CHART_PREFIX, "_", v.EdbInfoId, "_", timestamp))
- mapItem.MaxData = v.MaxData
- mapItem.MinData = v.MinData
- mapItem.IsOrder = v.IsOrder
- mapItem.IsAxis = v.IsAxis
- mapItem.EdbInfoType = v.EdbInfoType
- mapItem.LeadValue = v.LeadValue
- mapItem.LeadUnit = v.LeadUnit
- mapItem.ChartStyle = v.ChartStyle
- mapItem.ChartColor = v.ChartColor
- mapItem.PredictChartColor = v.PredictChartColor
- mapItem.ChartWidth = v.ChartWidth
- mapItem.Source = utils.CHART_SOURCE_DEFAULT
- mapItem.EdbAliasName = v.EdbAliasName
- mapItem.IsConvert = v.IsConvert
- mapItem.ConvertType = v.ConvertType
- mapItem.ConvertValue = v.ConvertValue
- mapItem.ConvertUnit = v.ConvertUnit
- mapItem.ConvertEnUnit = v.ConvertEnUnit
- mapList = append(mapList, mapItem)
- }
- err = data_manage.AddChartEdbMapping(mapList)
- if err != nil {
- errMsg = `保存失败`
- err = errors.New("保存失败,Err:" + err.Error())
- return
- }
- //添加es数据
- go EsAddOrEditChartInfo(chartInfo.ChartInfoId)
- return
- }
- // EditChartInfo 编辑图表
- func EditChartInfo(req data_manage.EditChartInfoReq, sysUser *system.Admin) (chartItem *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
- isSendEmail = true // 默认错误的时候要发送邮件
- req.ChartName = strings.Trim(req.ChartName, " ")
- if req.ChartName == "" {
- errMsg = "请填写图表名称!"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- if req.ChartClassifyId <= 0 {
- errMsg = "分类参数错误!"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- //if req.ChartThemeId <= 0 {
- // errMsg = "请选择主题!"
- // err = errors.New(errMsg)
- // isSendEmail = false
- // return
- //}
- //
- //// 判断主题是否存在
- //_, err = chart_theme.GetChartThemeId(req.ChartThemeId)
- //if err != nil {
- // if err.Error() == utils.ErrNoRow() {
- // errMsg = "主题不存在"
- // err = errors.New(errMsg)
- // return
- // }
- // errMsg = "获取主题信息失败"
- // return
- //}
- chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "分类不存在"
- err = errors.New(errMsg)
- return
- }
- errMsg = "获取分类信息失败"
- return
- }
- if chartClassify == nil {
- errMsg = "分类不存在"
- err = errors.New(errMsg)
- return
- }
- chartItem, err = data_manage.GetChartInfoById(req.ChartInfoId)
- if err != nil {
- if err.Error() == utils.ErrNoRow() {
- errMsg = "图表已被删除,请刷新页面"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- errMsg = "获取图表信息失败"
- err = errors.New("获取图表信息失败,Err:" + err.Error())
- return
- }
- var extraConfigEdbInfoIdArr []int
- extraConfigEdbInfoIdArr, err, errMsg = CheckChartExtraConfig(req.ChartType, req.ExtraConfig)
- if err != nil {
- err = errors.New("添加失败:" + err.Error())
- return
- }
- // 季节性图表额外配置信息
- var seasonExtraConfig string
- // 图表关联指标id
- chartEdbInfoList := make([]*data_manage.ChartSaveItem, 0)
- // 关联指标
- var edbInfoIdArr []int
- // 非散点截面图的直接通过前端获取,散点截面图需要额外从配置中获取
- if chartItem.ChartType != 10 {
- //edbList := make([]*data_manage.EdbInfo, 0)
- for _, v := range req.ChartEdbInfoList {
- edbInfoId := v.EdbInfoId
- edbInfo, tmpErr := data_manage.GetEdbInfoById(edbInfoId)
- if tmpErr != nil {
- if tmpErr.Error() == utils.ErrNoRow() {
- errMsg = "指标不存在!"
- err = errors.New("指标不存在,edbInfoId:" + strconv.Itoa(edbInfoId))
- return
- } else {
- errMsg = "获取指标信息失败!"
- err = errors.New("获取图表的指标信息失败,Err:" + tmpErr.Error())
- return
- }
- }
- if edbInfo == nil {
- errMsg = "指标已被删除,请重新选择!"
- err = errors.New("指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
- return
- } else {
- if edbInfo.EdbInfoId <= 0 {
- errMsg = "指标已被删除,请重新选择!"
- err = errors.New("指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
- return
- }
- }
- edbInfoIdArr = append(edbInfoIdArr, edbInfoId)
- edbInfo.EdbNameSource = edbInfo.EdbName
- //edbList = append(edbList, edbInfo)
- //新增季节性图表数据计算
- if req.ChartType == 2 {
- existCount, tmpErr := data_manage.GetEdbDataQuarterCount(edbInfoId)
- if tmpErr != nil {
- errMsg = "保存失败"
- err = errors.New("判断季节性图表,指标数据是否存在失败,Err:" + tmpErr.Error())
- return
- }
- if existCount <= 0 {
- go data_manage.AddCalculateQuarter(edbInfoId, edbInfo.Source, edbInfo.EdbCode)
- }
- // 处理季节性图表横轴配置
- {
- if req.SeasonExtraConfig.XEndDate != "" {
- if req.SeasonExtraConfig.XStartDate > req.SeasonExtraConfig.XEndDate && req.SeasonExtraConfig.JumpYear != 1 {
- errMsg = "季节性图表配置信息异常:横坐标日期配置错误"
- err = errors.New("季节性图表配置信息异常: 横坐标日期配置错误")
- return
- }
- seasonExtra, tErr := json.Marshal(req.SeasonExtraConfig)
- if tErr != nil {
- errMsg = "季节性图表配置信息异常"
- err = errors.New("季节性图表配置信息异常,Err:" + tErr.Error())
- return
- }
- seasonExtraConfig = string(seasonExtra)
- }
- }
- }
- }
- chartEdbInfoList = req.ChartEdbInfoList
- } else {
- if req.ExtraConfig == `` {
- errMsg = "请选择指标!"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- edbInfoIdArr = extraConfigEdbInfoIdArr
- lenEdbInfoIdArr := len(edbInfoIdArr)
- if lenEdbInfoIdArr > 0 {
- tmpEdbList, tmpErr := data_manage.GetEdbInfoByIdList(edbInfoIdArr)
- if tmpErr != nil {
- errMsg = "指标异常!"
- err = errors.New("指标异常!Err:" + tmpErr.Error())
- return
- }
- if len(tmpEdbList) != lenEdbInfoIdArr {
- errMsg = "指标异常!"
- err = errors.New(fmt.Sprint("查找出来的指标数量与选择的指标数量不符!查找出来的指标数量:", len(tmpEdbList), ";用户选择的指标数量:", lenEdbInfoIdArr))
- return
- }
- for _, v := range tmpEdbList {
- chartEdbInfoList = append(chartEdbInfoList, &data_manage.ChartSaveItem{
- EdbInfoId: v.EdbInfoId,
- MaxData: 0,
- MinData: 0,
- IsOrder: false,
- IsAxis: 1,
- EdbInfoType: 1,
- LeadValue: 0,
- LeadUnit: "",
- ChartStyle: "",
- ChartColor: "",
- PredictChartColor: "",
- ChartWidth: 0,
- Source: utils.CHART_SOURCE_DEFAULT,
- })
- }
- }
- }
- if len(edbInfoIdArr) <= 0 {
- errMsg = "请选择指标!"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- // 图表额外配置
- extraConfig, err, errMsg := HandleExtraConfig(req.ChartType, req.ExtraConfig)
- if err != nil {
- if errMsg == `` {
- errMsg = "指标异常!"
- }
- err = errors.New("指标异常!Err:" + err.Error())
- return
- }
- req.ExtraConfig = extraConfig
- //图表操作权限
- ok := CheckOpChartPermission(sysUser, chartItem.SysUserId)
- if !ok {
- errMsg = "没有该图表的操作权限"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- var calendar string
- var dateType int
- dateType = req.DateType
- if req.ChartType != chartItem.ChartType {
- if chartItem.ChartType != 2 && chartItem.Calendar == "" { //非季节性图
- calendar = "公历"
- }
- if chartItem.ChartType == 2 && chartItem.DateType <= 0 {
- dateType = 3
- }
- }
- sort.Ints(edbInfoIdArr)
- var edbInfoIdArrStr []string
- for _, v := range edbInfoIdArr {
- edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
- }
- edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
- //mappingCondition := ` AND chart_info_id<> ` + strconv.Itoa(req.ChartInfoId)
- //count, err := data_manage.ChartInfoExist(mappingCondition, edbInfoIdStr)
- //if err != nil && err.Error() != utils.ErrNoRow() {
- // br.Msg = "保存失败"
- // br.ErrMsg = "判断图表名称是否存在失败,Err:" + err.Error()
- // return
- //}
- //if count > 0 {
- // br.Msg = "所选指标已存在相同指标"
- // return
- //}
- //判断图表是否存在
- var condition string
- var pars []interface{}
- condition += " AND chart_info_id<>? "
- pars = append(pars, req.ChartInfoId)
- // 图表名称在不同图分类下不允许重复 需求调整时间:2022年11月07日09:47:07
- //condition += " AND chart_classify_id=? "
- //pars = append(pars, req.ChartClassifyId)
- condition += " AND chart_name=? AND source = ? "
- pars = append(pars, req.ChartName, utils.CHART_SOURCE_DEFAULT)
- count, err := data_manage.GetChartInfoCountByCondition(condition, pars)
- if err != nil {
- errMsg = "判断图表名称是否存在失败"
- err = errors.New("判断图表名称是否存在失败,Err:" + err.Error())
- return
- }
- if count > 0 {
- errMsg = "图表已存在,请重新填写"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- barChartConf := `` //柱方图的配置信息
- if req.ChartType == 7 {
- barChartConfReq := req.BarChartInfo
- if len(barChartConfReq.EdbInfoIdList) <= 0 {
- errMsg = "请选择指标"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- if len(barChartConfReq.DateList) <= 0 {
- errMsg = "请选择日期"
- err = errors.New(errMsg)
- isSendEmail = false
- return
- }
- barChartConfByte, tmpErr := json.Marshal(barChartConfReq)
- if tmpErr != nil {
- errMsg = "柱方图信息异常"
- err = errors.New("柱方图信息异常,Err:" + tmpErr.Error())
- return
- }
- barChartConf = string(barChartConfByte)
- }
- // 图表启用与否
- disableVal := CheckIsDisableChart(edbInfoIdArr)
- err = data_manage.EditChartInfoAndMapping(&req, edbInfoIdStr, calendar, dateType, disableVal, barChartConf, chartEdbInfoList, seasonExtraConfig)
- if err != nil {
- errMsg = "保存失败"
- err = errors.New("保存失败,Err:" + err.Error())
- return
- }
- resp := new(data_manage.AddChartInfoResp)
- resp.ChartInfoId = chartItem.ChartInfoId
- resp.UniqueCode = chartItem.UniqueCode
- resp.ChartType = req.ChartType
- //添加es数据
- go EsAddOrEditChartInfo(chartItem.ChartInfoId)
- //修改my eta es数据
- go EsAddOrEditMyChartInfoByChartInfoId(chartItem.ChartInfoId)
- return
- }
- // GetEdbSourceByEdbInfoIdList 获取关联指标的来源
- func GetEdbSourceByEdbInfoIdList(chartEdbInfoMappingList []*data_manage.ChartEdbInfoMapping) (sourceNameList, sourceNameEnList []string) {
- sourceNameList = make([]string, 0)
- sourceNameEnList = make([]string, 0)
- sourceMap := make(map[int]string)
- for _, v := range chartEdbInfoMappingList {
- // 指标类型:1:基础指标,2:计算指标
- if v.EdbType == 2 || v.EdbInfoCategoryType == 1 {
- //sourceMap[0] = "弘则研究"
- baseEdbInfoArr, _, _ := data_manage.GetRefreshEdbInfoFromBase(v.EdbInfoId, v.Source)
- for _, baseEdbInfo := range baseEdbInfoArr {
- if baseEdbInfo.EdbInfoType == 0 { //普通指标才参与,预测指标不参与
- sourceMap[baseEdbInfo.Source] = baseEdbInfo.SourceName
- }
- }
- } else {
- sourceMap[v.Source] = v.SourceName
- }
- }
- for source, sourceName := range sourceMap {
- if utils.InArrayByInt([]int{utils.DATA_SOURCE_MANUAL, utils.DATA_SOURCE_MYSTEEL_CHEMICAL}, source) {
- continue
- }
- sourceNameList = append(sourceNameList, sourceName)
- sourceNameEn, ok := utils.DataSourceEnMap[source]
- if !ok {
- sourceNameEn = sourceName
- }
- sourceNameEnList = append(sourceNameEnList, sourceNameEn)
- }
- //sourceNameList = append(sourceNameList, utils.ChartDefaultNameCn)
- //sourceNameEnList = append(sourceNameEnList, utils.ChartDefaultNameEn)
- // 图表来源
- conf, e := models.GetBusinessConf()
- if e != nil {
- return
- }
- if conf[models.BusinessConfCompanyName] != "" {
- sourceNameList = append(sourceNameList, conf[models.BusinessConfCompanyName])
- sourceNameEnList = append(sourceNameEnList, conf[models.BusinessConfCompanyName])
- }
- return
- }
- // RadarChartData 雷达图的数据处理
- func RadarChartData(mappingList []*data_manage.ChartEdbInfoMapping, edbDataListMap map[int][]*data_manage.EdbDataList, extraConfig data_manage.RadarChartInfoReq) (edbIdList []int, chartDataResp data_manage.RadarChartInfoResp, err error) {
- // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
- edbDataMap := make(map[int]map[string]float64)
- for edbInfoId, edbDataList := range edbDataListMap {
- edbDateData := make(map[string]float64)
- for _, edbData := range edbDataList {
- edbDateData[edbData.DataTime] = edbData.Value
- }
- edbDataMap[edbInfoId] = edbDateData
- }
- // edbIdList 指标展示顺序;x轴的指标顺序
- edbIdList = make([]int, 0)
- for _, v := range mappingList {
- edbIdList = append(edbIdList, v.EdbInfoId)
- }
- chartDateList := extraConfig.DateList
- yDataList := make([]data_manage.RadarYData, 0) //y轴的数据列表
- for _, chartDate := range chartDateList {
- var maxDate time.Time
- findDataList := make([]float64, 0) // 当前日期的数据值
- for _, edbInfoId := range edbIdList {
- findDate := chartDate.Date //需要的日期值
- dataList := edbDataListMap[edbInfoId] //指标的所有数据值
- if len(dataList) <= 0 {
- // 没有数据的指标id
- findDataList = append(findDataList, 0)
- continue
- }
- switch chartDate.Type {
- case 1: //最新值
- findDate = dataList[len(dataList)-1].DataTime
- case 2: //近期几天
- findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- findDateTime = findDateTime.AddDate(0, 0, -chartDate.Value)
- lenData := len(dataList) - 1
- for i := lenData; i >= 0; i-- {
- currDateTime, e := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
- if e != nil {
- err = e
- return
- }
- if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
- findDate = dataList[i].DataTime
- break
- }
- }
- case 3: // 固定日期
- //最早的日期
- minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- //寻找固定日期的数据
- findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, chartDate.Date, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
- tmpDate := tmpDateTime.Format(utils.FormatDate)
- if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
- findDate = tmpDate
- break
- }
- }
- default:
- err = errors.New(fmt.Sprint("日期类型异常,Type:", chartDate.Type))
- return
- }
- findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
- if maxDate.IsZero() {
- maxDate = findDateTime
- } else {
- if findDateTime.After(maxDate) {
- maxDate = findDateTime
- }
- }
- if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
- tmpValue, _ = decimal.NewFromFloat(tmpValue).Round(4).Float64()
- findDataList = append(findDataList, tmpValue)
- } else {
- findDataList = append(findDataList, 0)
- }
- }
- yDate := "0000-00-00"
- if !maxDate.IsZero() {
- yDate = maxDate.Format(utils.FormatDate)
- }
- yDataList = append(yDataList, data_manage.RadarYData{
- Date: yDate,
- Value: findDataList,
- Color: chartDate.Color,
- Name: chartDate.Name,
- })
- }
- chartDataResp = data_manage.RadarChartInfoResp{
- YDataList: yDataList,
- XEdbIdValue: edbIdList,
- }
- return
- }
- // GetChartConvertEdbData 获取图表数据转换的指标数据
- func GetChartConvertEdbData(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*data_manage.ChartEdbInfoMapping, extraConfigStr string, seasonExtraConfig string) (edbList []*data_manage.ChartEdbInfoMapping, xEdbIdValue []int, yDataList []data_manage.YData, dataResp interface{}, err error, errMsg string) {
- edbList = make([]*data_manage.ChartEdbInfoMapping, 0)
- xEdbIdValue = make([]int, 0)
- yDataList = make([]data_manage.YData, 0)
- var extraConfig interface{}
- switch chartType {
- case 7: // 柱形图
- var barConfig data_manage.BarChartInfoReq
- if extraConfigStr == `` {
- errMsg = "柱方图未配置"
- err = errors.New(errMsg)
- return
- }
- err = json.Unmarshal([]byte(extraConfigStr), &barConfig)
- if err != nil {
- errMsg = "柱方图配置异常"
- err = errors.New(errMsg)
- return
- }
- extraConfig = barConfig
- case 10: // 截面散点图
- var tmpExtraConfig data_manage.SectionScatterReq
- if extraConfigStr == `` {
- errMsg = "截面散点图未配置"
- err = errors.New(errMsg)
- return
- }
- err = json.Unmarshal([]byte(extraConfigStr), &tmpExtraConfig)
- if err != nil {
- errMsg = "截面散点配置异常"
- err = errors.New(errMsg)
- return
- }
- extraConfig = tmpExtraConfig
- case utils.CHART_TYPE_RADAR:
- var barConfig data_manage.RadarChartInfoReq
- if extraConfigStr == `` {
- errMsg = "雷达图未配置"
- err = errors.New(errMsg)
- return
- }
- err = json.Unmarshal([]byte(extraConfigStr), &barConfig)
- if err != nil {
- errMsg = "雷达图配置异常"
- err = errors.New(errMsg)
- return
- }
- extraConfig = barConfig
- default:
- xEdbIdValue = make([]int, 0)
- yDataList = make([]data_manage.YData, 0)
- }
- // 指标对应的所有数据
- edbDataListMap, edbList, err := getEdbConvertDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList, seasonExtraConfig)
- if err != nil {
- return
- }
- // 特殊图形数据处理
- switch chartType {
- case 7: // 柱形图
- barChartConf := extraConfig.(data_manage.BarChartInfoReq)
- xEdbIdValue, yDataList, err = BarChartData(mappingList, edbDataListMap, barChartConf.DateList, barChartConf.Sort)
- for k := range yDataList {
- yDataList[k].Unit = barChartConf.Unit
- yDataList[k].UnitEn = barChartConf.UnitEn
- }
- for _, v := range edbList {
- // 指标别名
- if barChartConf.EdbInfoIdList != nil && len(barChartConf.EdbInfoIdList) > 0 {
- for _, reqEdb := range barChartConf.EdbInfoIdList {
- if v.EdbInfoId == reqEdb.EdbInfoId {
- v.EdbAliasName = reqEdb.Name
- }
- }
- }
- }
- case 10: // 截面散点图
- sectionScatterConf := extraConfig.(data_manage.SectionScatterReq)
- xEdbIdValue, dataResp, err = GetSectionScatterChartData(chartInfoId, mappingList, edbDataListMap, sectionScatterConf)
- var tmpExtraConfig data_manage.SectionScatterReq
- if extraConfigStr == `` {
- errMsg = "截面散点图未配置"
- err = errors.New(errMsg)
- return
- }
- err = json.Unmarshal([]byte(extraConfigStr), &tmpExtraConfig)
- if err != nil {
- errMsg = "截面散点配置异常"
- err = errors.New(errMsg)
- return
- }
- // 这个数据没有必要返回给前端
- for _, v := range edbList {
- v.DataList = nil
- }
- case utils.CHART_TYPE_RADAR: //雷达图
- radarConf := extraConfig.(data_manage.RadarChartInfoReq)
- xEdbIdValue, dataResp, err = RadarChartData(mappingList, edbDataListMap, radarConf)
- }
- return
- }
- func getEdbConvertDataMapList(chartInfoId, chartType int, calendar, startDate, endDate string, mappingList []*data_manage.ChartEdbInfoMapping, seasonExtraConfig string) (edbDataListMap map[int][]*data_manage.EdbDataList, edbList []*data_manage.ChartEdbInfoMapping, err error) {
- // 指标对应的所有数据
- edbDataListMap = make(map[int][]*data_manage.EdbDataList)
- for _, v := range mappingList {
- //fmt.Println("v:", v.EdbInfoId)
- item := new(data_manage.ChartEdbInfoMapping)
- item.EdbInfoId = v.EdbInfoId
- item.SourceName = v.SourceName
- item.Source = v.Source
- item.EdbCode = v.EdbCode
- item.EdbName = v.EdbName
- item.EdbNameEn = v.EdbNameEn
- item.Frequency = v.Frequency
- item.EdbType = v.EdbType
- item.FrequencyEn = GetFrequencyEn(v.Frequency)
- if v.Unit != `无` {
- item.Unit = v.Unit
- }
- item.UnitEn = v.UnitEn
- item.StartDate = v.StartDate
- item.EndDate = v.EndDate
- item.ModifyTime = v.ModifyTime
- item.EdbInfoCategoryType = v.EdbInfoCategoryType
- item.PredictChartColor = v.PredictChartColor
- item.ClassifyId = v.ClassifyId
- if chartInfoId <= 0 {
- item.IsAxis = 1
- item.LeadValue = 0
- item.LeadUnit = ""
- item.ChartEdbMappingId = 0
- item.ChartInfoId = 0
- item.IsOrder = false
- item.EdbInfoType = 1
- item.ChartStyle = ""
- item.ChartColor = ""
- item.ChartWidth = 0
- item.MaxData = v.MaxValue
- item.MinData = v.MinValue
- } else {
- item.IsAxis = v.IsAxis
- item.EdbInfoType = v.EdbInfoType
- item.LeadValue = v.LeadValue
- item.LeadUnit = v.LeadUnit
- item.LeadUnitEn = GetLeadUnitEn(v.LeadUnit)
- item.ChartEdbMappingId = v.ChartEdbMappingId
- item.ChartInfoId = v.ChartInfoId
- item.ChartStyle = v.ChartStyle
- item.ChartColor = v.ChartColor
- item.ChartWidth = v.ChartWidth
- item.IsOrder = v.IsOrder
- item.MaxData = v.MaxData
- item.MinData = v.MinData
- }
- item.LatestValue = v.LatestValue
- item.LatestDate = v.LatestDate
- item.UniqueCode = v.UniqueCode
- item.MoveLatestDate = v.LatestDate
- item.EdbAliasName = v.EdbAliasName
- item.IsConvert = v.IsConvert
- item.ConvertType = v.ConvertType
- item.ConvertValue = v.ConvertValue
- item.ConvertUnit = v.ConvertUnit
- item.ConvertEnUnit = v.ConvertEnUnit
- var startDateReal string
- var diffSeconds int64
- if chartType == 2 { //季节性图
- startDateReal = startDate
- } else {
- if v.EdbInfoType == 0 && v.LeadUnit != "" && v.LeadValue > 0 { //领先指标
- var startTimeRealTemp time.Time
- startDateParse, _ := time.Parse(utils.FormatDate, startDate)
- switch v.LeadUnit {
- case "天":
- startTimeRealTemp = startDateParse.AddDate(0, 0, -v.LeadValue)
- case "月":
- startTimeRealTemp = startDateParse.AddDate(0, -v.LeadValue, 0)
- case "季":
- startTimeRealTemp = startDateParse.AddDate(0, -3*v.LeadValue, 0)
- case "周":
- startTimeRealTemp = startDateParse.AddDate(0, 0, -7*v.LeadValue)
- case "年":
- startTimeRealTemp = startDateParse.AddDate(-v.LeadValue, 0, 0)
- }
- if startTimeRealTemp.Before(startDateParse) {
- startDateReal = startTimeRealTemp.Format(utils.FormatDate)
- diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6
- } else {
- startDateReal = startDate
- diffSeconds = 0
- }
- // 预测指标的开始日期也要偏移
- {
- day, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, startDate, startDateReal)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- moveLatestDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, item.MoveLatestDate, time.Local)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- item.MoveLatestDate = moveLatestDateTime.AddDate(0, 0, day).Format(utils.FormatDate)
- }
- } else {
- startDateReal = startDate
- }
- }
- //fmt.Println("line 1011 chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
- calendarPreYear := 0
- if calendar == "农历" {
- newStartDateReal, err := time.Parse(utils.FormatDate, startDateReal)
- if err != nil {
- fmt.Println("time.Parse:" + err.Error())
- }
- calendarPreYear = newStartDateReal.Year() - 1
- newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
- startDateReal = newStartDateReal.Format(utils.FormatDate)
- }
- dataList := make([]*data_manage.EdbDataList, 0)
- //fmt.Println("chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
- //fmt.Println("calendarPreYear:", calendarPreYear)
- //var newEdbInfo *data_manage.EdbInfo
- switch v.EdbInfoCategoryType {
- case 0:
- dataList, err = data_manage.GetEdbDataList(v.Source, v.SubSource, v.EdbInfoId, startDateReal, endDate)
- case 1:
- _, dataList, _, _, err, _ = GetPredictDataListByPredictEdbInfoId(v.EdbInfoId, startDateReal, endDate, true)
- default:
- err = errors.New(fmt.Sprint("获取失败,指标类型异常", v.EdbInfoCategoryType))
- }
- if err != nil {
- return
- }
- if v.IsConvert == 1 {
- switch v.ConvertType {
- case 1:
- for i, data := range dataList {
- dataList[i].Value = data.Value * v.ConvertValue
- }
- item.MaxData = item.MaxData * v.ConvertValue
- item.MinData = item.MinData * v.ConvertValue
- case 2:
- for i, data := range dataList {
- dataList[i].Value = data.Value / v.ConvertValue
- }
- item.MaxData = item.MaxData / v.ConvertValue
- item.MinData = item.MinData / v.ConvertValue
- case 3:
- for i, data := range dataList {
- dataList[i].Value = math.Log(data.Value) / math.Log(v.ConvertValue)
- }
- item.MaxData = math.Log(item.MaxData) / math.Log(v.ConvertValue)
- item.MinData = math.Log(item.MinData) / math.Log(v.ConvertValue)
- }
- }
- edbDataListMap[v.EdbInfoId] = dataList
- if diffSeconds != 0 && v.EdbInfoType == 0 {
- dataListLen := len(dataList)
- for i := 0; i < dataListLen; i++ {
- dataList[i].DataTimestamp = dataList[i].DataTimestamp - diffSeconds
- }
- }
- if chartType == 2 {
- latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
- if tmpErr != nil {
- //item.DataList = dataList
- item.IsNullData = true
- edbList = append(edbList, item)
- continue
- err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + v.LatestDate))
- return
- }
- if calendar == "农历" {
- if len(dataList) <= 0 {
- result := new(data_manage.EdbDataResult)
- item.DataList = result
- } else {
- result, tmpErr := data_manage.AddCalculateQuarterV6(dataList)
- if tmpErr != nil {
- err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
- return
- }
- quarterDataList, tErr := GetSeasonEdbInfoDataListByXDateNong(result, latestDate, seasonExtraConfig, calendarPreYear)
- if tErr != nil {
- err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
- return
- }
- item.DataList = quarterDataList
- }
- } else {
- quarterDataList, tErr := GetSeasonEdbInfoDataListByXDate(dataList, latestDate, seasonExtraConfig)
- if tErr != nil {
- err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
- return
- }
- item.DataList = quarterDataList
- }
- } else if chartType == 7 || chartType == utils.CHART_TYPE_RADAR { //柱方图
- //item.DataList = dataList
- } else {
- item.DataList = dataList
- }
- edbList = append(edbList, item)
- }
- return
- }
|