chart_info.go 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568
  1. package data
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_api/models/company"
  6. "eta/eta_api/models/data_manage"
  7. "eta/eta_api/models/system"
  8. "eta/eta_api/services/alarm_msg"
  9. "eta/eta_api/utils"
  10. "fmt"
  11. "github.com/shopspring/decimal"
  12. "math"
  13. "sort"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. func ChartInfoRefreshV1(chartInfoId int) (err error) {
  19. var errmsg string
  20. defer func() {
  21. if err != nil {
  22. go alarm_msg.SendAlarmMsg("ChartInfoRefresh:"+errmsg, 3)
  23. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "ChartInfoRefresh:"+errmsg, utils.EmailSendToUsers)
  24. fmt.Println("ChartInfoRefresh Err:" + errmsg)
  25. }
  26. }()
  27. baseEdbInfoArr, calculateInfoArr, err := data_manage.GetChartInfoRefreshData(chartInfoId)
  28. if err != nil {
  29. errmsg = "获取需要刷新的指标失败:Err:" + err.Error()
  30. return
  31. }
  32. newBaseEdbInfoArr := make([]*data_manage.EdbInfo, 0)
  33. baseMap := make(map[int]*data_manage.EdbInfo)
  34. for _, bv := range baseEdbInfoArr {
  35. // 如果不是普通指标,那么过滤
  36. if bv.EdbInfoType != 0 {
  37. continue
  38. }
  39. if _, ok := baseMap[bv.EdbInfoId]; !ok {
  40. newBaseEdbInfoArr = append(newBaseEdbInfoArr, bv)
  41. }
  42. baseMap[bv.EdbInfoId] = bv
  43. }
  44. fmt.Println("calculateInfoArr:", len(calculateInfoArr))
  45. newCalculateInfoArr := make([]*data_manage.EdbInfo, 0)
  46. calculateMap := make(map[int]*data_manage.EdbInfo)
  47. var calculateArr []int
  48. for _, bv := range calculateInfoArr {
  49. if _, ok := calculateMap[bv.EdbInfoId]; !ok {
  50. newCalculateInfoArr = append(newCalculateInfoArr, bv)
  51. calculateArr = append(calculateArr, bv.EdbInfoId)
  52. }
  53. calculateMap[bv.EdbInfoId] = bv
  54. }
  55. sort.Ints(calculateArr)
  56. // 刷新相关基础指标
  57. for _, bv := range newBaseEdbInfoArr {
  58. err, errmsg = refreshBaseEdbInfo(bv)
  59. if err != nil {
  60. return
  61. }
  62. }
  63. // 刷新相关计算指标
  64. for _, v := range calculateArr {
  65. edbInfo := calculateMap[v]
  66. if edbInfo == nil {
  67. return err
  68. }
  69. err, errmsg = refreshCalculateEdbInfo(edbInfo)
  70. if err != nil {
  71. return
  72. }
  73. }
  74. return err
  75. }
  76. // BatchChartInfoRefresh 图表批量刷新
  77. func BatchChartInfoRefresh(chartInfoList []*data_manage.ChartInfo) (err error) {
  78. if len(chartInfoList) <= 0 {
  79. return
  80. }
  81. chartInfoIdSlice := make([]string, 0)
  82. for _, chartInfo := range chartInfoList {
  83. chartInfoIdSlice = append(chartInfoIdSlice, strconv.Itoa(chartInfo.ChartInfoId))
  84. }
  85. var errmsg string
  86. defer func() {
  87. if err != nil {
  88. go alarm_msg.SendAlarmMsg("ChartInfoRefresh:"+errmsg, 3)
  89. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "ChartInfoRefresh:"+errmsg, utils.EmailSendToUsers)
  90. fmt.Println("ChartInfoRefresh Err:" + errmsg)
  91. }
  92. }()
  93. baseEdbInfoArr, calculateInfoArr, err := data_manage.GetChartInfoRefreshDataByChartInfoIdSlice(chartInfoIdSlice)
  94. if err != nil {
  95. errmsg = "获取需要刷新的指标失败:Err:" + err.Error()
  96. return
  97. }
  98. //基础指标去重
  99. newBaseEdbInfoArr := make([]*data_manage.EdbInfo, 0)
  100. baseMap := make(map[int]*data_manage.EdbInfo)
  101. for _, bv := range baseEdbInfoArr {
  102. fmt.Println(bv.UniqueCode)
  103. if _, ok := baseMap[bv.EdbInfoId]; !ok {
  104. newBaseEdbInfoArr = append(newBaseEdbInfoArr, bv)
  105. }
  106. baseMap[bv.EdbInfoId] = bv
  107. }
  108. fmt.Println("calculateInfoArr:", len(calculateInfoArr))
  109. //计算指标去重
  110. newCalculateInfoArr := make([]*data_manage.EdbInfo, 0)
  111. calculateMap := make(map[int]*data_manage.EdbInfo)
  112. var calculateArr []int
  113. for _, bv := range calculateInfoArr {
  114. if _, ok := calculateMap[bv.EdbInfoId]; !ok {
  115. newCalculateInfoArr = append(newCalculateInfoArr, bv)
  116. calculateArr = append(calculateArr, bv.EdbInfoId)
  117. }
  118. calculateMap[bv.EdbInfoId] = bv
  119. }
  120. sort.Ints(calculateArr)
  121. // 刷新相关基础指标
  122. for _, bv := range newBaseEdbInfoArr {
  123. err, errmsg = refreshBaseEdbInfo(bv)
  124. if err != nil {
  125. return
  126. }
  127. }
  128. // 刷新相关计算指标
  129. for _, v := range calculateArr {
  130. edbInfo := calculateMap[v]
  131. if edbInfo == nil {
  132. return err
  133. }
  134. err, errmsg = refreshCalculateEdbInfo(edbInfo)
  135. if err != nil {
  136. return
  137. }
  138. }
  139. return err
  140. }
  141. // refreshBaseEdbInfo 刷新基础指标
  142. func refreshBaseEdbInfo(bv *data_manage.EdbInfo) (err error, errMsg string) {
  143. source := bv.Source
  144. edbInfoId := bv.EdbInfoId
  145. edbCode := bv.EdbCode
  146. //startDate := bv.StartDate
  147. frequency := bv.Frequency
  148. if bv.StartDate == "0000-00-00" {
  149. return
  150. }
  151. sTime, err := time.Parse(utils.FormatDate, bv.EndDate)
  152. if err != nil {
  153. return
  154. }
  155. var limitDay int
  156. startDate := ""
  157. switch frequency {
  158. case "日度":
  159. limitDay = utils.DATA_START_REFRESH_LIMIT
  160. case "周度":
  161. limitDay = utils.DATA_START_REFRESH_LIMIT * 7
  162. case "月度":
  163. limitDay = utils.DATA_START_REFRESH_LIMIT * 30
  164. case "季度":
  165. limitDay = utils.DATA_START_REFRESH_LIMIT * 90
  166. case "年度":
  167. limitDay = utils.DATA_START_REFRESH_LIMIT * 365
  168. default:
  169. limitDay = utils.DATA_START_REFRESH_LIMIT
  170. }
  171. startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDate)
  172. fmt.Println("source:", source)
  173. respItem, err := RefreshEdbData(edbInfoId, source, edbCode, startDate)
  174. if err != nil {
  175. errMsg = errors.New("RefreshEdbData Err:" + err.Error()).Error()
  176. return
  177. }
  178. if respItem.Ret != 200 {
  179. errMsg = errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
  180. return
  181. }
  182. //if source == utils.DATA_SOURCE_THS {
  183. // //endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
  184. // //err = RefreshEdbDataByThs(edbInfoId, edbCode, startDate, endDate)
  185. // //if err != nil {
  186. // // errmsg = "RefreshEdbDataByThs Err:" + err.Error()
  187. // //}
  188. // respItem, err :=RefreshThsEdbData(edbInfoId,edbCode,startDate)
  189. // if err != nil {
  190. // errmsg=errors.New("RefreshThsEdbData Err:"+err.Error()).Error()
  191. // return err
  192. // }
  193. // if respItem.Ret != 200 {
  194. // errmsg=errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
  195. // return err
  196. // }
  197. //} else if source == utils.DATA_SOURCE_WIND {
  198. // //endDate := time.Now().Format(utils.FormatDate)
  199. // //err = RefreshEdbDataByWind(edbInfoId, edbCode, startDate, endDate)
  200. // //if err != nil {
  201. // // errmsg = "RefreshEdbDataByWind Err:" + err.Error()
  202. // //}
  203. // respItem, err :=RefreshWindEdbData(edbInfoId,edbCode,startDate)
  204. // if err != nil {
  205. // errmsg=errors.New("RefreshWindEdbData Err:"+err.Error()).Error()
  206. // return err
  207. // }
  208. // if respItem.Ret != 200 {
  209. // errmsg=errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
  210. // return err
  211. // }
  212. //} else if source == utils.DATA_SOURCE_PB {
  213. // //startDate := sTime.Format(utils.FormatDateUnSpace)
  214. // //endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDateUnSpace)
  215. // //err = RefreshEdbDataByPb(edbInfoId, edbCode, startDate, endDate)
  216. // //if err != nil {
  217. // // errmsg = "RefreshEdbDataByPb Err:" + err.Error()
  218. // //}
  219. // startDate = sTime.AddDate(0, 0, -limitDay).Format(utils.FormatDateUnSpace)
  220. // respItem, err :=RefreshPbEdbData(edbInfoId,edbCode,startDate)
  221. // if err != nil {
  222. // errmsg=errors.New("RefreshPbEdbData Err:"+err.Error()).Error()
  223. // return err
  224. // }
  225. // if respItem.Ret != 200 {
  226. // errmsg=errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
  227. // return err
  228. // }
  229. //} else if source == utils.DATA_SOURCE_MANUAL {
  230. // //fmt.Println("RefreshAllEdbDataByManual:" + edbCode)
  231. // //err = data_manage.RefreshAllEdbDataByManual(edbInfoId, bv.Source, edbCode)
  232. // //if err != nil {
  233. // // errmsg = "RefreshEdbDataByManual Err:" + err.Error()
  234. // //}
  235. // respItem, err :=RefreshManualEdbData(edbInfoId,edbCode,startDate)
  236. // if err != nil {
  237. // errmsg=errors.New("RefreshManualEdbData Err:"+err.Error()).Error()
  238. // return err
  239. // }
  240. // if respItem.Ret != 200 {
  241. // errmsg=errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
  242. // return err
  243. // }
  244. //} else if source == utils.DATA_SOURCE_LZ {
  245. // //endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
  246. // //err = data_manage.RefreshEdbDataByLz(edbInfoId, edbCode, startDate, endDate)
  247. // //if err != nil {
  248. // // errmsg = "RefreshEdbDataByLz Err:" + err.Error()
  249. // //}
  250. // respItem, err :=RefreshLzEdbData(edbInfoId,edbCode,startDate)
  251. // if err != nil {
  252. // errmsg=errors.New("RefreshLzEdbData Err:"+err.Error()).Error()
  253. // return err
  254. // }
  255. // if respItem.Ret != 200 {
  256. // errmsg=errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
  257. // return err
  258. // }
  259. //} else if source == utils.DATA_SOURCE_YS {
  260. // //endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
  261. // //err = data_manage.RefreshAllEdbDataByYs(edbInfoId, source, edbCode, startDate, endDate)
  262. // //if err != nil {
  263. // // errmsg = "RefreshEdbDataByYs Err:" + err.Error()
  264. // //}
  265. // respItem, err :=RefreshSmmEdbData(edbInfoId,edbCode,startDate)
  266. // if err != nil {
  267. // errmsg=errors.New("RefreshSmmEdbData Err:"+err.Error()).Error()
  268. // return err
  269. // }
  270. // if respItem.Ret != 200 {
  271. // errmsg=errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
  272. // return err
  273. // }
  274. //} else if source == utils.DATA_SOURCE_GL {
  275. // //endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
  276. // //err = data_manage.RefreshEdbDataByGl(edbInfoId, edbCode, startDate, endDate)
  277. // //if err != nil {
  278. // // errmsg = "RefreshEdbDataByGl Err:" + err.Error()
  279. // //}
  280. // respItem, err :=RefreshMysteelEdbData(edbInfoId,edbCode,startDate)
  281. // if err != nil {
  282. // errmsg=errors.New("RefreshMysteelEdbData Err:"+err.Error()).Error()
  283. // return err
  284. // }
  285. // if respItem.Ret != 200 {
  286. // errmsg=errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
  287. // return err
  288. // }
  289. //} else if source == utils.DATA_SOURCE_LT {
  290. // //endDate := time.Now().Format(utils.FormatDate)
  291. // //err = data_manage.RefreshAllEdbDataByLt(edbInfoId, source, edbCode, startDate, endDate)
  292. // //if err != nil {
  293. // // errmsg = "RefreshAllEdbDataByLT Err:" + err.Error()
  294. // //}
  295. // respItem, err :=RefreshLtEdbData(edbInfoId,edbCode,startDate)
  296. // if err != nil {
  297. // errmsg=errors.New("RefreshLtEdbData Err:"+err.Error()).Error()
  298. // return err
  299. // }
  300. // if respItem.Ret != 200 {
  301. // errmsg=errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
  302. // return err
  303. // }
  304. //} else if source == utils.DATA_SOURCE_ZZ {
  305. // //endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
  306. // //err = data_manage.RefreshEdbDataByZz(edbInfoId, edbCode, startDate, endDate)
  307. // //if err != nil {
  308. // // errmsg = "RefreshAllEdbDataByZz Err:" + err.Error()
  309. // //}
  310. // respItem, err :=RefreshZzEdbData(edbInfoId,edbCode,startDate)
  311. // if err != nil {
  312. // errmsg=errors.New("RefreshZzEdbData Err:"+err.Error()).Error()
  313. // return err
  314. // }
  315. // if respItem.Ret != 200 {
  316. // errmsg=errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
  317. // return err
  318. // }
  319. //} else if source == utils.DATA_SOURCE_DL {
  320. // //endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
  321. // //err = data_manage.RefreshEdbDataByDl(edbInfoId, edbCode, startDate, endDate)
  322. // //if err != nil {
  323. // // errmsg = "RefreshAllEdbDataByDl Err:" + err.Error()
  324. // //}
  325. // respItem, err :=RefreshDlEdbData(edbInfoId,edbCode,startDate)
  326. // if err != nil {
  327. // errmsg=errors.New("RefreshDlEdbData Err:"+err.Error()).Error()
  328. // return err
  329. // }
  330. // if respItem.Ret != 200 {
  331. // errmsg=errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
  332. // return err
  333. // }
  334. //} else if source == utils.DATA_SOURCE_SH {
  335. // //endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
  336. // //err = data_manage.RefreshEdbDataBySh(edbInfoId, edbCode, startDate, endDate)
  337. // //if err != nil {
  338. // // errmsg = "RefreshAllEdbDataBySh Err:" + err.Error()
  339. // //}
  340. // respItem, err :=RefreshShEdbData(edbInfoId,edbCode,startDate)
  341. // if err != nil {
  342. // errmsg=errors.New("RefreshShEdbData Err:"+err.Error()).Error()
  343. // return err
  344. // }
  345. // if respItem.Ret != 200 {
  346. // errmsg=errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
  347. // return err
  348. // }
  349. //} else if source == utils.DATA_SOURCE_CFFEX {
  350. // //endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
  351. // //err = data_manage.RefreshEdbDataByCffex(edbInfoId, edbCode, startDate, endDate)
  352. // //if err != nil {
  353. // // errmsg = "RefreshAllEdbDataByCffex Err:" + err.Error()
  354. // //}
  355. // respItem, err :=RefreshCffexEdbData(edbInfoId,edbCode,startDate)
  356. // if err != nil {
  357. // errmsg=errors.New("RefreshCffexEdbData Err:"+err.Error()).Error()
  358. // return err
  359. // }
  360. // if respItem.Ret != 200 {
  361. // errmsg=errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
  362. // return err
  363. // }
  364. //} else if source == utils.DATA_SOURCE_SHFE {
  365. // //endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
  366. // //err = data_manage.RefreshEdbDataByShfe(edbInfoId, edbCode, startDate, endDate)
  367. // //if err != nil {
  368. // // errmsg = "RefreshAllEdbDataByShfe Err:" + err.Error()
  369. // //}
  370. // respItem, err :=RefreshShfeEdbData(edbInfoId,edbCode,startDate)
  371. // if err != nil {
  372. // errmsg=errors.New("RefreshShfeEdbData Err:"+err.Error()).Error()
  373. // return err
  374. // }
  375. // if respItem.Ret != 200 {
  376. // errmsg=errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
  377. // return err
  378. // }
  379. //} else if source == utils.DATA_SOURCE_GIE {
  380. // //endDate := time.Now().AddDate(utils.DATA_END_DATE_LIMIT, 0, 0).Format(utils.FormatDate)
  381. // //err = data_manage.RefreshEdbDataByGie(edbInfoId, edbCode, startDate, endDate)
  382. // //if err != nil {
  383. // // errmsg = "RefreshEdbDataByGie Err:" + err.Error()
  384. // //}
  385. // respItem, err :=RefreshGieEdbData(edbInfoId,edbCode,startDate)
  386. // if err != nil {
  387. // errmsg=errors.New("RefreshGieEdbData Err:"+err.Error()).Error()
  388. // return err
  389. // }
  390. // if respItem.Ret != 200 {
  391. // errmsg=errors.New(respItem.ErrMsg + ";EdbCode:" + edbCode).Error()
  392. // return err
  393. // }
  394. //}
  395. maxAndMinItem, err := data_manage.GetEdbInfoMaxAndMinInfo(source, edbCode)
  396. if err != nil {
  397. return
  398. }
  399. if maxAndMinItem != nil {
  400. err = data_manage.ModifyEdbInfoMaxAndMinInfo(edbInfoId, maxAndMinItem)
  401. if err != nil {
  402. return
  403. }
  404. }
  405. return
  406. }
  407. // refreshCalculateEdbInfo 刷新计算指标
  408. func refreshCalculateEdbInfo(edbInfo *data_manage.EdbInfo) (err error, errMsg string) {
  409. startDate := edbInfo.StartDate
  410. source := edbInfo.Source
  411. if source == utils.DATA_SOURCE_CALCULATE {
  412. startDate = edbInfo.StartDate
  413. sTime, tmpErr := time.Parse(utils.FormatDate, edbInfo.EndDate)
  414. if tmpErr != nil {
  415. err = tmpErr
  416. return
  417. }
  418. startDate = sTime.Format(utils.FormatDate)
  419. }
  420. result, err := RefreshEdbCalculateData(edbInfo.EdbInfoId, edbInfo.EdbCode, startDate)
  421. if err != nil {
  422. fmt.Println(edbInfo.EdbInfoId, "RefreshEdbCalculateData err", time.Now())
  423. errMsg = "RefreshEdbCalculateData Err:" + err.Error()
  424. return
  425. }
  426. if result.Ret != 200 {
  427. fmt.Println(edbInfo.EdbInfoId, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  428. errMsg = fmt.Sprint(edbInfo.EdbInfoId, "RefreshEdbCalculateData err;msg:", result.Msg, ";errMsg:", result.ErrMsg)
  429. err = errors.New("刷新失败")
  430. return
  431. }
  432. return
  433. }
  434. type ChartInfoReq struct {
  435. ChartInfoId int `description:"图表id,新增时传0"`
  436. }
  437. // DeleteChartInfoDataRedis 清除图表缓存
  438. func DeleteChartInfoDataRedis(bodyByte []byte) (err error) {
  439. var req ChartInfoReq
  440. err = json.Unmarshal(bodyByte, &req)
  441. if err != nil {
  442. return
  443. }
  444. if req.ChartInfoId > 0 {
  445. err = utils.Rc.Delete(GetChartInfoDataKey(req.ChartInfoId))
  446. }
  447. return
  448. }
  449. // DeleteChartClassifyRedis 清除图表分类缓存
  450. func DeleteChartClassifyRedis(bodyByte []byte) (err error) {
  451. err = utils.Rc.Delete(utils.CACHE_CHART_CLASSIFY)
  452. return
  453. }
  454. // GetChartInfoDataKey 获取图表缓存的key
  455. func GetChartInfoDataKey(chartInfoId int) string {
  456. key := fmt.Sprint(utils.CACHE_CHART_INFO_DATA, chartInfoId)
  457. return key
  458. }
  459. // CheckOpChartPermission 判断图表操作权限
  460. func CheckOpChartPermission(sysUser *system.Admin, createUserId int) (ok bool) {
  461. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN {
  462. ok = true
  463. }
  464. // 如果图表创建人与当前操作人相同的话,那么就是允许操作
  465. if ok == false && createUserId == sysUser.AdminId {
  466. ok = true
  467. }
  468. // 如果图表权限id 是 1 ,那么允许编辑
  469. if ok == false && sysUser.ChartPermission == 1 {
  470. ok = true
  471. }
  472. return
  473. }
  474. // GetChartEdbData 获取图表的指标数据
  475. 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) {
  476. edbList = make([]*data_manage.ChartEdbInfoMapping, 0)
  477. xEdbIdValue = make([]int, 0)
  478. yDataList = make([]data_manage.YData, 0)
  479. var extraConfig interface{}
  480. switch chartType {
  481. case 7: // 柱形图
  482. var barConfig data_manage.BarChartInfoReq
  483. if extraConfigStr == `` {
  484. errMsg = "柱方图未配置"
  485. err = errors.New(errMsg)
  486. return
  487. }
  488. err = json.Unmarshal([]byte(extraConfigStr), &barConfig)
  489. if err != nil {
  490. errMsg = "柱方图配置异常"
  491. err = errors.New(errMsg)
  492. return
  493. }
  494. extraConfig = barConfig
  495. case 10: // 截面散点图
  496. var tmpExtraConfig data_manage.SectionScatterReq
  497. if extraConfigStr == `` {
  498. errMsg = "截面散点图未配置"
  499. err = errors.New(errMsg)
  500. return
  501. }
  502. err = json.Unmarshal([]byte(extraConfigStr), &tmpExtraConfig)
  503. if err != nil {
  504. errMsg = "截面散点配置异常"
  505. err = errors.New(errMsg)
  506. return
  507. }
  508. extraConfig = tmpExtraConfig
  509. default:
  510. xEdbIdValue = make([]int, 0)
  511. yDataList = make([]data_manage.YData, 0)
  512. }
  513. // 指标对应的所有数据
  514. edbDataListMap, edbList, err := getEdbDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList, seasonExtraConfig)
  515. if err != nil {
  516. return
  517. }
  518. // 特殊图形数据处理
  519. switch chartType {
  520. case 7: // 柱形图
  521. barChartConf := extraConfig.(data_manage.BarChartInfoReq)
  522. xEdbIdValue, yDataList, err = BarChartData(mappingList, edbDataListMap, barChartConf.DateList, barChartConf.Sort)
  523. for k := range yDataList {
  524. yDataList[k].Unit = barChartConf.Unit
  525. yDataList[k].UnitEn = barChartConf.UnitEn
  526. }
  527. for _, v := range edbList {
  528. // 指标别名
  529. if barChartConf.EdbInfoIdList != nil && len(barChartConf.EdbInfoIdList) > 0 {
  530. for _, reqEdb := range barChartConf.EdbInfoIdList {
  531. if v.EdbInfoId == reqEdb.EdbInfoId {
  532. v.EdbAliasName = reqEdb.Name
  533. }
  534. }
  535. }
  536. }
  537. case 10: // 截面散点图
  538. sectionScatterConf := extraConfig.(data_manage.SectionScatterReq)
  539. xEdbIdValue, dataResp, err = GetSectionScatterChartData(chartInfoId, mappingList, edbDataListMap, sectionScatterConf)
  540. var tmpExtraConfig data_manage.SectionScatterReq
  541. if extraConfigStr == `` {
  542. errMsg = "截面散点图未配置"
  543. err = errors.New(errMsg)
  544. return
  545. }
  546. err = json.Unmarshal([]byte(extraConfigStr), &tmpExtraConfig)
  547. if err != nil {
  548. errMsg = "截面散点配置异常"
  549. err = errors.New(errMsg)
  550. return
  551. }
  552. // 这个数据没有必要返回给前端
  553. for _, v := range edbList {
  554. v.DataList = nil
  555. }
  556. }
  557. return
  558. }
  559. // GetEdbDataMapList 获取指标最后的基础数据
  560. 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) {
  561. edbDataListMap, edbList, err = getEdbDataMapList(chartInfoId, chartType, calendar, startDate, endDate, mappingList, seasonExtra)
  562. return
  563. }
  564. // getEdbDataMapList 获取指标最后的基础数据
  565. 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) {
  566. // 指标对应的所有数据
  567. edbDataListMap = make(map[int][]*data_manage.EdbDataList)
  568. for _, v := range mappingList {
  569. //fmt.Println("v:", v.EdbInfoId)
  570. item := new(data_manage.ChartEdbInfoMapping)
  571. item.EdbInfoId = v.EdbInfoId
  572. item.SourceName = v.SourceName
  573. item.Source = v.Source
  574. item.EdbCode = v.EdbCode
  575. item.EdbName = v.EdbName
  576. item.EdbNameEn = v.EdbNameEn
  577. item.Frequency = v.Frequency
  578. item.EdbType = v.EdbType
  579. item.FrequencyEn = GetFrequencyEn(v.Frequency)
  580. if v.Unit != `无` {
  581. item.Unit = v.Unit
  582. }
  583. item.UnitEn = v.UnitEn
  584. item.StartDate = v.StartDate
  585. item.EndDate = v.EndDate
  586. item.ModifyTime = v.ModifyTime
  587. item.EdbInfoCategoryType = v.EdbInfoCategoryType
  588. item.PredictChartColor = v.PredictChartColor
  589. item.ClassifyId = v.ClassifyId
  590. if chartInfoId <= 0 {
  591. item.IsAxis = 1
  592. item.LeadValue = 0
  593. item.LeadUnit = ""
  594. item.ChartEdbMappingId = 0
  595. item.ChartInfoId = 0
  596. item.IsOrder = false
  597. item.EdbInfoType = 1
  598. item.ChartStyle = ""
  599. item.ChartColor = ""
  600. item.ChartWidth = 0
  601. item.MaxData = v.MaxValue
  602. item.MinData = v.MinValue
  603. } else {
  604. item.IsAxis = v.IsAxis
  605. item.EdbInfoType = v.EdbInfoType
  606. item.LeadValue = v.LeadValue
  607. item.LeadUnit = v.LeadUnit
  608. item.LeadUnitEn = GetLeadUnitEn(v.LeadUnit)
  609. item.ChartEdbMappingId = v.ChartEdbMappingId
  610. item.ChartInfoId = v.ChartInfoId
  611. item.ChartStyle = v.ChartStyle
  612. item.ChartColor = v.ChartColor
  613. item.ChartWidth = v.ChartWidth
  614. item.IsOrder = v.IsOrder
  615. item.MaxData = v.MaxData
  616. item.MinData = v.MinData
  617. }
  618. item.LatestValue = v.LatestValue
  619. item.LatestDate = v.LatestDate
  620. item.UniqueCode = v.UniqueCode
  621. item.MoveLatestDate = v.LatestDate
  622. var startDateReal string
  623. var diffSeconds int64
  624. if chartType == 2 { //季节性图
  625. startDateReal = startDate
  626. } else {
  627. if v.EdbInfoType == 0 && v.LeadUnit != "" && v.LeadValue > 0 { //领先指标
  628. var startTimeRealTemp time.Time
  629. startDateParse, _ := time.Parse(utils.FormatDate, startDate)
  630. switch v.LeadUnit {
  631. case "天":
  632. startTimeRealTemp = startDateParse.AddDate(0, 0, -v.LeadValue)
  633. case "月":
  634. startTimeRealTemp = startDateParse.AddDate(0, -v.LeadValue, 0)
  635. case "季":
  636. startTimeRealTemp = startDateParse.AddDate(0, -3*v.LeadValue, 0)
  637. case "周":
  638. startTimeRealTemp = startDateParse.AddDate(0, 0, -7*v.LeadValue)
  639. case "年":
  640. startTimeRealTemp = startDateParse.AddDate(-v.LeadValue, 0, 0)
  641. }
  642. if startTimeRealTemp.Before(startDateParse) {
  643. startDateReal = startTimeRealTemp.Format(utils.FormatDate)
  644. diffSeconds = (int64(startTimeRealTemp.UnixNano()) - int64(startDateParse.UnixNano())) / 1e6
  645. } else {
  646. startDateReal = startDate
  647. diffSeconds = 0
  648. }
  649. // 预测指标的开始日期也要偏移
  650. {
  651. day, tmpErr := utils.GetDaysBetween2Date(utils.FormatDate, startDate, startDateReal)
  652. if tmpErr != nil {
  653. err = tmpErr
  654. return
  655. }
  656. moveLatestDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, item.MoveLatestDate, time.Local)
  657. if tmpErr != nil {
  658. err = tmpErr
  659. return
  660. }
  661. item.MoveLatestDate = moveLatestDateTime.AddDate(0, 0, day).Format(utils.FormatDate)
  662. }
  663. } else {
  664. startDateReal = startDate
  665. }
  666. }
  667. //fmt.Println("line 1011 chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  668. calendarPreYear := 0
  669. if calendar == "农历" {
  670. newStartDateReal, err := time.Parse(utils.FormatDate, startDateReal)
  671. if err != nil {
  672. fmt.Println("time.Parse:" + err.Error())
  673. }
  674. calendarPreYear = newStartDateReal.Year() - 1
  675. newStartDateReal = newStartDateReal.AddDate(-1, 0, 0)
  676. startDateReal = newStartDateReal.Format(utils.FormatDate)
  677. }
  678. dataList := make([]*data_manage.EdbDataList, 0)
  679. //fmt.Println("chart:", v.Source, v.EdbInfoId, startDateReal, endDate)
  680. fmt.Println("calendarPreYear:", calendarPreYear)
  681. //var newEdbInfo *data_manage.EdbInfo
  682. switch v.EdbInfoCategoryType {
  683. case 0:
  684. dataList, err = data_manage.GetEdbDataList(v.Source, v.EdbInfoId, startDateReal, endDate)
  685. case 1:
  686. _, dataList, _, _, err, _ = GetPredictDataListByPredictEdbInfoId(v.EdbInfoId, startDateReal, endDate, true)
  687. default:
  688. err = errors.New(fmt.Sprint("获取失败,指标类型异常", v.EdbInfoCategoryType))
  689. }
  690. if err != nil {
  691. return
  692. }
  693. edbDataListMap[v.EdbInfoId] = dataList
  694. if diffSeconds != 0 && v.EdbInfoType == 0 {
  695. dataListLen := len(dataList)
  696. for i := 0; i < dataListLen; i++ {
  697. dataList[i].DataTimestamp = dataList[i].DataTimestamp - diffSeconds
  698. }
  699. }
  700. if chartType == 2 {
  701. latestDate, tmpErr := time.Parse(utils.FormatDate, v.LatestDate)
  702. if tmpErr != nil {
  703. //item.DataList = dataList
  704. item.IsNullData = true
  705. edbList = append(edbList, item)
  706. continue
  707. err = errors.New(fmt.Sprint("获取最后实际数据的日期失败,Err:" + tmpErr.Error() + ";LatestDate:" + v.LatestDate))
  708. return
  709. }
  710. if calendar == "农历" {
  711. if len(dataList) <= 0 {
  712. result := new(data_manage.EdbDataResult)
  713. item.DataList = result
  714. } else {
  715. result, tmpErr := data_manage.AddCalculateQuarterV4(dataList)
  716. if tmpErr != nil {
  717. err = errors.New("获取农历数据失败,Err:" + tmpErr.Error())
  718. return
  719. }
  720. quarterDataList, tErr := GetSeasonEdbInfoDataListByXDateNong(result, latestDate, seasonExtraConfig, calendarPreYear)
  721. if tErr != nil {
  722. err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
  723. return
  724. }
  725. item.DataList = quarterDataList
  726. }
  727. } else {
  728. quarterDataList, tErr := GetSeasonEdbInfoDataListByXDate(dataList, latestDate, seasonExtraConfig)
  729. if tErr != nil {
  730. err = errors.New("获取季节性图表数据失败,Err:" + tErr.Error())
  731. return
  732. }
  733. item.DataList = quarterDataList
  734. }
  735. } else if chartType == 7 { //柱方图
  736. //item.DataList = dataList
  737. } else {
  738. item.DataList = dataList
  739. }
  740. edbList = append(edbList, item)
  741. }
  742. return
  743. }
  744. // GetSeasonEdbInfoDataListByXDate 季节性图的指标数据根据横轴展示
  745. func GetSeasonEdbInfoDataListByXDate(dataList []*data_manage.EdbDataList, latestDate time.Time, seasonExtraConfig string) (quarterDataListSort data_manage.QuarterDataList, err error) {
  746. xStartDate := "01-01"
  747. xEndDate := "12-31"
  748. jumpYear := 0
  749. legends := make([]data_manage.SeasonChartLegend, 0)
  750. var seasonExtra data_manage.SeasonExtraItem
  751. if seasonExtraConfig != "" {
  752. err = json.Unmarshal([]byte(seasonExtraConfig), &seasonExtra)
  753. if err != nil {
  754. return
  755. }
  756. }
  757. if seasonExtra.XStartDate != "" {
  758. xStartDate = seasonExtra.XStartDate
  759. xEndDate = seasonExtra.XEndDate
  760. jumpYear = seasonExtra.JumpYear
  761. legends = seasonExtra.ChartLegend
  762. }
  763. length := len(dataList)
  764. if length == 0 {
  765. return
  766. }
  767. legendMap := make(map[string]string, 0)
  768. if len(legends) > 0 {
  769. for _, v := range legends {
  770. legendMap[v.Name] = v.Value
  771. }
  772. }
  773. latestDateStr := latestDate.Format(utils.FormatDate)
  774. //判断横轴的两个时间之间是不是跨年了,如果跨年了,则横轴截止年份比起始年份+1,如果不跨年,截止年份等于起始年份
  775. //根据数据确定最早的年份,和最近年份
  776. //根据横轴的日期,汇总所有的年份
  777. startDate := dataList[0].DataTime
  778. startDateT, tmpErr := time.Parse(utils.FormatDate, startDate)
  779. if tmpErr != nil {
  780. err = tmpErr
  781. return
  782. }
  783. startYear := startDateT.Year()
  784. //获取数据的最新日期
  785. lastDate := dataList[length-1].DataTime
  786. lastDateT, tmpErr := time.Parse(utils.FormatDate, lastDate)
  787. if tmpErr != nil {
  788. err = tmpErr
  789. return
  790. }
  791. endYear := lastDateT.Year()
  792. nowYear := time.Now().Year()
  793. dataMap := make(map[string]data_manage.QuarterXDateItem, 0)
  794. quarterDataList := make([]*data_manage.QuarterData, 0)
  795. quarterMap := make(map[string][]*data_manage.EdbDataList, 0)
  796. //整理出日期
  797. idx := 1
  798. chartLegendMap := make(map[string]int, 0)
  799. for currentStartYear := startYear; currentStartYear <= endYear; currentStartYear++ {
  800. startStr := fmt.Sprintf("%d-%s", currentStartYear, xStartDate)
  801. currentEndYear := currentStartYear
  802. if jumpYear == 1 {
  803. currentEndYear = currentStartYear + 1
  804. }
  805. endStr := fmt.Sprintf("%d-%s", currentEndYear, xEndDate)
  806. name := fmt.Sprintf("%s_%s", startStr, endStr)
  807. showName := fmt.Sprintf("%d_%d", currentStartYear, currentEndYear)
  808. startT, tEr := time.Parse(utils.FormatDate, startStr)
  809. if tEr != nil {
  810. err = tEr
  811. return
  812. }
  813. endT, tEr := time.Parse(utils.FormatDate, endStr)
  814. if tEr != nil {
  815. err = tEr
  816. return
  817. }
  818. if lastDateT.Before(startT) {
  819. //如果最新的日期在起始日之前,则跳出循环
  820. break
  821. }
  822. if endT.Year() > nowYear {
  823. //如果最新的日期比真实年份要大,则数据全部按照最大的年份补齐
  824. nowYear = endT.Year()
  825. }
  826. item := data_manage.QuarterXDateItem{
  827. StartDate: startT,
  828. EndDate: endT,
  829. ShowName: showName,
  830. }
  831. dataMap[name] = item
  832. chartLegendMap[name] = idx
  833. idx++
  834. if lastDateT.Before(endT) {
  835. //如果最新的日期在起始日之前,则跳出循环
  836. break
  837. }
  838. }
  839. lenYear := len(dataMap)
  840. for k, v := range dataMap {
  841. if i, ok := chartLegendMap[k]; ok {
  842. v.ChartLegend = strconv.Itoa(endYear - lenYear + i)
  843. }
  844. dataMap[k] = v
  845. }
  846. for _, v := range dataList {
  847. dataTimeT, _ := time.Parse(utils.FormatDate, v.DataTime)
  848. year := dataTimeT.Year()
  849. newItemDate := dataTimeT.AddDate(nowYear-year, 0, 0)
  850. for k, dateItem := range dataMap {
  851. tmpVal := data_manage.EdbDataList{
  852. EdbDataId: v.EdbDataId,
  853. EdbInfoId: v.EdbInfoId,
  854. DataTime: v.DataTime,
  855. DataTimestamp: v.DataTimestamp,
  856. Value: v.Value,
  857. }
  858. if (dateItem.StartDate.Before(dataTimeT) && dateItem.EndDate.After(dataTimeT)) || dateItem.StartDate == dataTimeT || dateItem.EndDate == dataTimeT {
  859. if jumpYear == 1 {
  860. //计算前一年最大的日期, 只补齐数据到去年
  861. beforeYearMaxDate := fmt.Sprintf("%d-12-31", dateItem.StartDate.Year())
  862. beforeYearMaxDateT, _ := time.Parse(utils.FormatDate, beforeYearMaxDate)
  863. if dataTimeT.Before(beforeYearMaxDateT) || dataTimeT == beforeYearMaxDateT {
  864. newItemDate = dataTimeT.AddDate(nowYear-year-1, 0, 0)
  865. } else {
  866. newItemDate = dataTimeT.AddDate(nowYear-year, 0, 0)
  867. }
  868. } else {
  869. newItemDate = dataTimeT.AddDate(nowYear-year, 0, 0)
  870. }
  871. timestamp := newItemDate.UnixNano() / 1e6
  872. tmpVal.DataTimestamp = timestamp
  873. tmpV := &tmpVal
  874. if findVal, ok := quarterMap[k]; !ok {
  875. findVal = append(findVal, tmpV)
  876. quarterMap[k] = findVal
  877. } else {
  878. findVal = append(findVal, tmpV)
  879. quarterMap[k] = findVal
  880. }
  881. if v.DataTime == latestDateStr {
  882. dateItem.CuttingDataTimestamp = timestamp
  883. dataMap[k] = dateItem
  884. }
  885. //break
  886. }
  887. }
  888. }
  889. for k, v := range dataMap {
  890. itemList := quarterMap[k]
  891. quarterItem := new(data_manage.QuarterData)
  892. quarterItem.Years = v.ShowName
  893. quarterItem.ChartLegend = v.ChartLegend
  894. if le, ok := legendMap[v.ShowName]; ok {
  895. if le != strconv.Itoa(v.StartDate.Year()) && le != strconv.Itoa(v.EndDate.Year()) {
  896. quarterItem.ChartLegend = le
  897. }
  898. }
  899. quarterItem.DataList = itemList
  900. quarterItem.CuttingDataTimestamp = v.CuttingDataTimestamp
  901. //如果等于最后的实际日期,那么将切割时间戳记录
  902. if quarterItem.CuttingDataTimestamp == 0 {
  903. //如果大于最后的实际日期,那么第一个点就是切割的时间戳
  904. if latestDate.Before(v.StartDate) && len(itemList) > 0 {
  905. quarterItem.CuttingDataTimestamp = itemList[0].DataTimestamp - 100
  906. }
  907. }
  908. quarterDataList = append(quarterDataList, quarterItem)
  909. }
  910. if len(quarterDataList) > 0 {
  911. quarterDataListSort = quarterDataList
  912. sort.Sort(quarterDataListSort)
  913. }
  914. return
  915. }
  916. // GetSeasonEdbInfoDataListByXDateNong 季节性图的指标数据根据横轴选择农历时展示
  917. func GetSeasonEdbInfoDataListByXDateNong(result *data_manage.EdbDataResult, latestDate time.Time, seasonExtraConfig string, calendarPreYear int) (quarterDataListSort data_manage.QuarterDataList, err error) {
  918. xStartDate := "01-01"
  919. xEndDate := "12-31"
  920. jumpYear := 0
  921. legends := make([]data_manage.SeasonChartLegend, 0)
  922. var seasonExtra data_manage.SeasonExtraItem
  923. if seasonExtraConfig != "" {
  924. err = json.Unmarshal([]byte(seasonExtraConfig), &seasonExtra)
  925. if err != nil {
  926. return
  927. }
  928. }
  929. if seasonExtra.XStartDate != "" {
  930. xStartDate = seasonExtra.XStartDate
  931. xEndDate = seasonExtra.XEndDate
  932. jumpYear = seasonExtra.JumpYear
  933. legends = seasonExtra.ChartLegend
  934. }
  935. length := len(result.List)
  936. if length == 0 {
  937. return
  938. }
  939. legendMap := make(map[string]string, 0)
  940. if len(legends) > 0 {
  941. for _, v := range legends {
  942. legendMap[v.Name] = v.Value
  943. }
  944. }
  945. latestDateYear := latestDate.Year()
  946. //判断横轴的两个时间之间是不是跨年了,如果跨年了,则横轴截止年份比起始年份+1,如果不跨年,截止年份等于起始年份
  947. //根据数据确定最早的年份,和最近年份
  948. //根据横轴的日期,汇总所有的年份
  949. startYear := result.List[0].Year
  950. /*if jumpYear == 1 {
  951. if startYear != calendarPreYear {
  952. startYear = startYear - 1
  953. }
  954. }*/
  955. itemLength := len(result.List[length-1].Items)
  956. //获取数据的最新日期
  957. lastDate := result.List[length-1].Items[itemLength-1].DataTime
  958. lastDateT, tmpErr := time.Parse(utils.FormatDate, lastDate)
  959. if tmpErr != nil {
  960. err = tmpErr
  961. return
  962. }
  963. endYear := lastDateT.Year()
  964. nowYear := time.Now().Year()
  965. dataMap := make(map[string]data_manage.QuarterXDateItem, 0)
  966. quarterDataList := make([]*data_manage.QuarterData, 0)
  967. resultData := make([]*data_manage.QuarterData, 0)
  968. quarterMap := make(map[string][]*data_manage.EdbDataList, 0)
  969. //整理出日期
  970. idx := 1
  971. chartLegendMap := make(map[string]int, 0)
  972. for currentStartYear := startYear; currentStartYear <= endYear; currentStartYear++ {
  973. startStr := fmt.Sprintf("%d-%s", currentStartYear, xStartDate)
  974. currentEndYear := currentStartYear
  975. if jumpYear == 1 {
  976. currentEndYear = currentStartYear + 1
  977. }
  978. endStr := fmt.Sprintf("%d-%s", currentEndYear, xEndDate)
  979. showName := fmt.Sprintf("%d_%d", currentStartYear, currentEndYear)
  980. startT, tEr := time.Parse(utils.FormatDate, startStr)
  981. if tEr != nil {
  982. err = tEr
  983. return
  984. }
  985. endT, tEr := time.Parse(utils.FormatDate, endStr)
  986. if tEr != nil {
  987. err = tEr
  988. return
  989. }
  990. if lastDateT.Before(startT) {
  991. //如果最新的日期在起始日之前,则跳出循环
  992. break
  993. }
  994. if endT.Year() > nowYear {
  995. //如果最新的日期比真实年份要大,则数据全部按照最大的年份补齐
  996. nowYear = endT.Year()
  997. }
  998. item := data_manage.QuarterXDateItem{
  999. StartDate: startT,
  1000. EndDate: endT,
  1001. ShowName: showName,
  1002. }
  1003. dataMap[showName] = item
  1004. chartLegendMap[showName] = idx
  1005. idx++
  1006. if lastDateT.Before(endT) {
  1007. //如果最新的日期在起始日之前,则跳出循环
  1008. break
  1009. }
  1010. }
  1011. lenYear := len(dataMap)
  1012. for k, v := range dataMap {
  1013. if i, ok := chartLegendMap[k]; ok {
  1014. v.ChartLegend = strconv.Itoa(endYear - lenYear + i)
  1015. }
  1016. dataMap[k] = v
  1017. }
  1018. yearDataListMap := make(map[int]*data_manage.EdbDataItems, 0)
  1019. for _, lv := range result.List {
  1020. yearDataListMap[lv.Year] = lv
  1021. }
  1022. //判断哪些点应该落在同一条时间线上
  1023. maxY := lastDateT.Year()
  1024. endTmp := fmt.Sprintf("%d-%s", maxY, xEndDate)
  1025. endTmpT, _ := time.Parse(utils.FormatDate, endTmp)
  1026. minY := maxY
  1027. if jumpYear == 1 {
  1028. minY = maxY - 1
  1029. }
  1030. startTmp := fmt.Sprintf("%d-%s", minY, xStartDate)
  1031. startTmpT, _ := time.Parse(utils.FormatDate, startTmp)
  1032. for name, dateItem := range dataMap {
  1033. lv, ok1 := yearDataListMap[dateItem.EndDate.Year()]
  1034. if dateItem.EndDate.Year() > lastDateT.Year() {
  1035. lv, ok1 = yearDataListMap[dateItem.StartDate.Year()]
  1036. }
  1037. if !ok1 {
  1038. continue
  1039. }
  1040. for _, item := range lv.Items {
  1041. tmpVal := data_manage.EdbDataList{
  1042. EdbDataId: item.EdbDataId,
  1043. EdbInfoId: item.EdbInfoId,
  1044. DataTime: item.DataTime,
  1045. DataTimestamp: item.DataTimestamp,
  1046. Value: item.Value,
  1047. }
  1048. dataTimeT, _ := time.Parse(utils.FormatDate, item.DataTime)
  1049. year := dataTimeT.Year()
  1050. newItemDate := dataTimeT.AddDate(nowYear-year, 0, 0)
  1051. if dateItem.EndDate.Year() > maxY {
  1052. if (dateItem.StartDate.Before(dataTimeT) && dateItem.EndDate.After(dataTimeT)) || dateItem.StartDate == dataTimeT || dateItem.EndDate == dataTimeT {
  1053. if jumpYear == 1 {
  1054. //计算前一年最大的日期, 只补齐数据到去年
  1055. beforeYearMaxDate := fmt.Sprintf("%d-12-31", dateItem.StartDate.Year())
  1056. beforeYearMaxDateT, _ := time.Parse(utils.FormatDate, beforeYearMaxDate)
  1057. if dataTimeT.Before(beforeYearMaxDateT) || dataTimeT == beforeYearMaxDateT {
  1058. newItemDate = dataTimeT.AddDate(nowYear-year-1, 0, 0)
  1059. } else {
  1060. newItemDate = dataTimeT.AddDate(nowYear-year, 0, 0)
  1061. }
  1062. } else {
  1063. newItemDate = dataTimeT.AddDate(nowYear-year, 0, 0)
  1064. }
  1065. timestamp := newItemDate.UnixNano() / 1e6
  1066. tmpVal.DataTimestamp = timestamp
  1067. tmpV := &tmpVal
  1068. if findVal, ok := quarterMap[name]; !ok {
  1069. findVal = append(findVal, tmpV)
  1070. quarterMap[name] = findVal
  1071. } else {
  1072. findVal = append(findVal, tmpV)
  1073. quarterMap[name] = findVal
  1074. }
  1075. if lv.Year >= latestDateYear {
  1076. // 切割的日期时间字符串
  1077. cuttingDataTimeStr := latestDate.AddDate(0, 0, lv.BetweenDay).Format(utils.FormatDate)
  1078. if item.DataTime == cuttingDataTimeStr {
  1079. dateItem.CuttingDataTimestamp = timestamp
  1080. dataMap[name] = dateItem
  1081. }
  1082. }
  1083. }
  1084. } else {
  1085. if (startTmpT.Before(dataTimeT) && endTmpT.After(dataTimeT)) || startTmpT == dataTimeT || endTmpT == dataTimeT {
  1086. if jumpYear == 1 {
  1087. //计算前一年最大的日期, 只补齐数据到去年
  1088. beforeYearMaxDate := fmt.Sprintf("%d-12-31", startTmpT.Year())
  1089. beforeYearMaxDateT, _ := time.Parse(utils.FormatDate, beforeYearMaxDate)
  1090. if dataTimeT.Before(beforeYearMaxDateT) || dataTimeT == beforeYearMaxDateT {
  1091. newItemDate = dataTimeT.AddDate(nowYear-year-1, 0, 0)
  1092. } else {
  1093. newItemDate = dataTimeT.AddDate(nowYear-year, 0, 0)
  1094. }
  1095. } else {
  1096. newItemDate = dataTimeT.AddDate(nowYear-year, 0, 0)
  1097. }
  1098. timestamp := newItemDate.UnixNano() / 1e6
  1099. tmpVal.DataTimestamp = timestamp
  1100. tmpV := &tmpVal
  1101. if findVal, ok := quarterMap[name]; !ok {
  1102. findVal = append(findVal, tmpV)
  1103. quarterMap[name] = findVal
  1104. } else {
  1105. findVal = append(findVal, tmpV)
  1106. quarterMap[name] = findVal
  1107. }
  1108. if lv.Year >= latestDateYear {
  1109. // 切割的日期时间字符串
  1110. cuttingDataTimeStr := latestDate.AddDate(0, 0, lv.BetweenDay).Format(utils.FormatDate)
  1111. if item.DataTime == cuttingDataTimeStr {
  1112. dateItem.CuttingDataTimestamp = timestamp
  1113. dataMap[name] = dateItem
  1114. }
  1115. }
  1116. }
  1117. }
  1118. }
  1119. }
  1120. for k, v := range dataMap {
  1121. itemList := quarterMap[k]
  1122. quarterItem := new(data_manage.QuarterData)
  1123. quarterItem.Years = v.ShowName
  1124. quarterItem.ChartLegend = v.ChartLegend
  1125. if le, ok := legendMap[v.ShowName]; ok {
  1126. if le != strconv.Itoa(v.StartDate.Year()) && le != strconv.Itoa(v.EndDate.Year()) {
  1127. quarterItem.ChartLegend = le
  1128. }
  1129. }
  1130. quarterItem.DataList = itemList
  1131. quarterItem.CuttingDataTimestamp = v.CuttingDataTimestamp
  1132. quarterDataList = append(quarterDataList, quarterItem)
  1133. }
  1134. if result.List[0].Year != calendarPreYear {
  1135. itemList := make([]*data_manage.EdbDataList, 0)
  1136. items := new(data_manage.QuarterData)
  1137. //items.Year = calendarPreYear
  1138. items.DataList = itemList
  1139. newResult := make([]*data_manage.QuarterData, 0)
  1140. newResult = append(newResult, items)
  1141. newResult = append(newResult, quarterDataList...)
  1142. resultData = newResult
  1143. } else {
  1144. resultData = quarterDataList
  1145. }
  1146. if len(quarterDataList) > 0 {
  1147. quarterDataListSort = resultData
  1148. sort.Sort(quarterDataListSort)
  1149. }
  1150. return
  1151. }
  1152. // BarChartData 柱方图的数据处理
  1153. 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) {
  1154. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  1155. edbDataMap := make(map[int]map[string]float64)
  1156. for edbInfoId, edbDataList := range edbDataListMap {
  1157. edbDateData := make(map[string]float64)
  1158. for _, edbData := range edbDataList {
  1159. edbDateData[edbData.DataTime] = edbData.Value
  1160. }
  1161. edbDataMap[edbInfoId] = edbDateData
  1162. }
  1163. // edbIdList 指标展示顺序;x轴的指标顺序
  1164. edbIdList = make([]int, 0)
  1165. //Sort int `description:"排序类型,0:默认,1:升序,2:降序"`
  1166. dateData := make(map[int]float64)
  1167. if barChartInfoSort.Sort == 0 {
  1168. for _, v := range mappingList {
  1169. edbIdList = append(edbIdList, v.EdbInfoId)
  1170. }
  1171. } else {
  1172. lenBarChartInfoDateList := len(barChartInfoDateList)
  1173. if barChartInfoSort.DateIndex >= lenBarChartInfoDateList {
  1174. err = errors.New("排序日期异常")
  1175. return
  1176. }
  1177. notDataEdbIdList := make([]int, 0) //没有数据的指标id
  1178. // 日期配置
  1179. barChartInfoDate := barChartInfoDateList[barChartInfoSort.DateIndex]
  1180. for edbInfoId, dataList := range edbDataListMap {
  1181. if len(dataList) <= 0 {
  1182. // 没有数据的指标id
  1183. notDataEdbIdList = append(notDataEdbIdList, edbInfoId)
  1184. continue
  1185. }
  1186. findDate := barChartInfoDate.Date
  1187. switch barChartInfoDate.Type {
  1188. case 1: //最新值
  1189. findDate = dataList[len(dataList)-1].DataTime
  1190. case 2: //近期几天
  1191. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  1192. if tmpErr != nil {
  1193. err = tmpErr
  1194. return
  1195. }
  1196. findDateTime = findDateTime.AddDate(0, 0, -barChartInfoDate.Value)
  1197. lenData := len(dataList) - 1
  1198. for i := lenData; i >= 0; i-- {
  1199. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  1200. if tmpErr != nil {
  1201. err = tmpErr
  1202. return
  1203. }
  1204. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  1205. findDate = dataList[i].DataTime
  1206. break
  1207. }
  1208. }
  1209. case 3: // 固定日期
  1210. //最早的日期
  1211. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  1212. if tmpErr != nil {
  1213. err = tmpErr
  1214. return
  1215. }
  1216. //寻找固定日期的数据
  1217. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
  1218. if tmpErr != nil {
  1219. err = tmpErr
  1220. return
  1221. }
  1222. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  1223. tmpDate := tmpDateTime.Format(utils.FormatDate)
  1224. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  1225. findDate = tmpDate
  1226. break
  1227. }
  1228. }
  1229. default:
  1230. err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
  1231. return
  1232. }
  1233. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  1234. dateData[edbInfoId] = tmpValue
  1235. } else {
  1236. // 没有数据的指标id
  1237. notDataEdbIdList = append(notDataEdbIdList, edbInfoId)
  1238. }
  1239. }
  1240. //Sort int `description:"排序类型,0:默认,1:升序,2:降序"`
  1241. // 排序
  1242. dateDataSort := utils.NewMapSorter(dateData)
  1243. sort.Sort(dateDataSort)
  1244. if barChartInfoSort.Sort == 1 {
  1245. // 先将没有数据的指标id放在最前面
  1246. if len(notDataEdbIdList) > 0 {
  1247. edbIdList = append(edbIdList, notDataEdbIdList...)
  1248. }
  1249. for _, v := range dateDataSort {
  1250. edbIdList = append(edbIdList, v.Key)
  1251. }
  1252. } else {
  1253. for i := len(dateDataSort) - 1; i >= 0; i-- {
  1254. edbIdList = append(edbIdList, dateDataSort[i].Key)
  1255. }
  1256. // 再将没有数据的指标id放在最后面
  1257. if len(notDataEdbIdList) > 0 {
  1258. edbIdList = append(edbIdList, notDataEdbIdList...)
  1259. }
  1260. }
  1261. }
  1262. yDataList = make([]data_manage.YData, 0) //y轴的数据列表
  1263. for _, barChartInfoDate := range barChartInfoDateList {
  1264. var maxDate time.Time
  1265. findDataList := make([]float64, 0) // 当前日期的数据值
  1266. for _, edbInfoId := range edbIdList {
  1267. findDate := barChartInfoDate.Date //需要的日期值
  1268. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  1269. if len(dataList) <= 0 {
  1270. // 没有数据的指标id
  1271. findDataList = append(findDataList, 0)
  1272. continue
  1273. }
  1274. switch barChartInfoDate.Type {
  1275. case 1: //最新值
  1276. dataList := edbDataListMap[edbInfoId]
  1277. findDate = dataList[len(dataList)-1].DataTime
  1278. case 2: //近期几天
  1279. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  1280. if tmpErr != nil {
  1281. err = tmpErr
  1282. return
  1283. }
  1284. findDateTime = findDateTime.AddDate(0, 0, -barChartInfoDate.Value)
  1285. lenData := len(dataList) - 1
  1286. for i := lenData; i >= 0; i-- {
  1287. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  1288. if tmpErr != nil {
  1289. err = tmpErr
  1290. return
  1291. }
  1292. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  1293. findDate = dataList[i].DataTime
  1294. break
  1295. }
  1296. }
  1297. case 3: // 固定日期
  1298. //最早的日期
  1299. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  1300. if tmpErr != nil {
  1301. err = tmpErr
  1302. return
  1303. }
  1304. //寻找固定日期的数据
  1305. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, barChartInfoDate.Date, time.Local)
  1306. if tmpErr != nil {
  1307. err = tmpErr
  1308. return
  1309. }
  1310. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  1311. tmpDate := tmpDateTime.Format(utils.FormatDate)
  1312. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  1313. findDate = tmpDate
  1314. break
  1315. }
  1316. }
  1317. default:
  1318. err = errors.New(fmt.Sprint("日期类型异常,Type:", barChartInfoDate.Type))
  1319. return
  1320. }
  1321. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  1322. if maxDate.IsZero() {
  1323. maxDate = findDateTime
  1324. } else {
  1325. if findDateTime.After(maxDate) {
  1326. maxDate = findDateTime
  1327. }
  1328. }
  1329. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  1330. tmpValue, _ = decimal.NewFromFloat(tmpValue).Round(4).Float64()
  1331. findDataList = append(findDataList, tmpValue)
  1332. } else {
  1333. findDataList = append(findDataList, 0)
  1334. }
  1335. }
  1336. yDate := "0000-00-00"
  1337. if !maxDate.IsZero() {
  1338. yDate = maxDate.Format(utils.FormatDate)
  1339. }
  1340. yDataList = append(yDataList, data_manage.YData{
  1341. Date: yDate,
  1342. Value: findDataList,
  1343. Color: barChartInfoDate.Color,
  1344. Name: barChartInfoDate.Name,
  1345. })
  1346. }
  1347. return
  1348. }
  1349. // ChartInfoRefreshV2 图表刷新
  1350. // @author Roc
  1351. // @datetime 2022-09-16 10:15:38
  1352. // @description 将原来自己写的一套获取所有关联指标,然后刷新指标逻辑 改成 只获取使用的指标id,然后遍历去调用“指标刷新服务”
  1353. func ChartInfoRefreshV2(chartInfoId int) (err error, isAsync bool) {
  1354. var errmsg string
  1355. defer func() {
  1356. if err != nil {
  1357. go alarm_msg.SendAlarmMsg("ChartInfoRefresh:"+errmsg, 3)
  1358. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "ChartInfoRefresh:"+errmsg, utils.EmailSendToUsers)
  1359. fmt.Println("ChartInfoRefresh Err:" + errmsg)
  1360. }
  1361. }()
  1362. edbMappingList, err := data_manage.GetChartEdbMappingList(chartInfoId)
  1363. if err != nil {
  1364. errmsg = "获取需要刷新的指标失败:Err:" + err.Error()
  1365. return
  1366. }
  1367. edbIdList := make([]int, 0)
  1368. for _, v := range edbMappingList {
  1369. edbIdList = append(edbIdList, v.EdbInfoId)
  1370. }
  1371. // 批量刷新
  1372. err, isAsync = EdbInfoRefreshAllFromBaseV3(edbIdList, false, false)
  1373. if err != nil {
  1374. return
  1375. }
  1376. return
  1377. }
  1378. // BatchChartInfoRefreshV2 图表批量刷新
  1379. // @author Roc
  1380. // @datetime 2022-09-16 10:15:38
  1381. // @description 将原来自己写的一套获取所有关联指标,然后刷新指标逻辑 改成 只获取使用的指标id,然后遍历去调用“指标刷新服务”
  1382. func BatchChartInfoRefreshV2(chartInfoList []*data_manage.ChartInfo) (err error, isAsync bool) {
  1383. if len(chartInfoList) <= 0 {
  1384. return
  1385. }
  1386. chartInfoIdSlice := make([]int, 0)
  1387. for _, chartInfo := range chartInfoList {
  1388. chartInfoIdSlice = append(chartInfoIdSlice, chartInfo.ChartInfoId)
  1389. }
  1390. var errmsg string
  1391. defer func() {
  1392. if err != nil {
  1393. go alarm_msg.SendAlarmMsg("ChartInfoRefresh:"+errmsg, 3)
  1394. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "ChartInfoRefresh:"+errmsg, utils.EmailSendToUsers)
  1395. fmt.Println("ChartInfoRefresh Err:" + errmsg)
  1396. }
  1397. }()
  1398. edbIdList := make([]int, 0)
  1399. edbInfoIdMap := make(map[int]int)
  1400. edbMappingList, err := data_manage.GetChartEdbMappingListByIdList(chartInfoIdSlice)
  1401. for _, v := range edbMappingList {
  1402. if _, ok := edbInfoIdMap[v.EdbInfoId]; !ok {
  1403. edbIdList = append(edbIdList, v.EdbInfoId)
  1404. edbInfoIdMap[v.EdbInfoId] = v.EdbInfoId
  1405. }
  1406. }
  1407. // 批量刷新
  1408. err, isAsync = EdbInfoRefreshAllFromBaseV3(edbIdList, false, false)
  1409. if err != nil {
  1410. return
  1411. }
  1412. return
  1413. }
  1414. func CheckIsEnChart(chartNameEn string, edbList []*data_manage.ChartEdbInfoMapping, source, chartType int) bool {
  1415. // 相关性图表不判断指标
  1416. if utils.InArrayByInt([]int{utils.CHART_SOURCE_CORRELATION, utils.CHART_SOURCE_ROLLING_CORRELATION, utils.CHART_SOURCE_LINE_EQUATION}, source) && chartNameEn != "" {
  1417. return true
  1418. }
  1419. if chartNameEn != "" && len(edbList) <= 0 {
  1420. return true
  1421. }
  1422. if chartNameEn == "" {
  1423. return false
  1424. }
  1425. // 截面散点图的话,肯定是有英文配置的
  1426. if chartType == utils.CHART_TYPE_SECTION_SCATTER {
  1427. return true
  1428. }
  1429. for _, v := range edbList {
  1430. if v.EdbNameEn == "" {
  1431. return false
  1432. }
  1433. if v.Unit != "无" && v.Unit != "" && v.UnitEn == "" {
  1434. return false
  1435. }
  1436. }
  1437. return true
  1438. }
  1439. func CheckIsEnEdb(edbNameEn, unit, unitEn string) bool {
  1440. if edbNameEn == "" {
  1441. return false
  1442. }
  1443. if unit != "无" && unit != "" && unitEn == "" {
  1444. return false
  1445. }
  1446. return true
  1447. }
  1448. // CheckIsDisableChart 根据指标Id判断是否是禁用图表
  1449. // return int 0:启用,1:禁用
  1450. func CheckIsDisableChart(edbInfoIdArr []int) (isDisable int) {
  1451. crmConfig, err := company.GetConfigDetailByCode("chart_disabled_edb")
  1452. if err != nil {
  1453. return
  1454. }
  1455. //没有配置,立马返回
  1456. if crmConfig.ConfigValue == `` {
  1457. return
  1458. }
  1459. confEdbInfoIdList := strings.Split(crmConfig.ConfigValue, ",")
  1460. for _, edbInfoId := range edbInfoIdArr {
  1461. for _, confEdbInfoId := range confEdbInfoIdList {
  1462. if strconv.Itoa(edbInfoId) == confEdbInfoId {
  1463. isDisable = 1
  1464. return
  1465. }
  1466. }
  1467. }
  1468. return
  1469. }
  1470. // CheckChartExtraConfig 校验图表额外配置的信息,并且获取相关联的指标id
  1471. func CheckChartExtraConfig(chartType int, extraConfigStr string) (edbIdList []int, err error, errMsg string) {
  1472. switch chartType {
  1473. case 10: //截面散点
  1474. var extraConfig data_manage.SectionScatterReq
  1475. err = json.Unmarshal([]byte(extraConfigStr), &extraConfig)
  1476. if err != nil {
  1477. return
  1478. }
  1479. // 判断是否有配置日期序列
  1480. if len(extraConfig.SeriesList) <= 0 {
  1481. errMsg = `请配置序列`
  1482. err = errors.New(errMsg)
  1483. return
  1484. }
  1485. // 判断是否有填写指标
  1486. if len(extraConfig.SeriesList[0].EdbInfoList) <= 0 {
  1487. errMsg = `请选择指标`
  1488. err = errors.New(errMsg)
  1489. return
  1490. }
  1491. // 遍历指标列表获取指标id
  1492. edbIdMap := make(map[int]int)
  1493. for _, v := range extraConfig.SeriesList[0].EdbInfoList {
  1494. // X 轴的指标id
  1495. if _, ok := edbIdMap[v.XEdbInfoId]; !ok {
  1496. edbIdMap[v.XEdbInfoId] = v.XEdbInfoId
  1497. edbIdList = append(edbIdList, v.XEdbInfoId)
  1498. }
  1499. // Y 轴的指标id
  1500. if _, ok := edbIdMap[v.YEdbInfoId]; !ok {
  1501. edbIdMap[v.YEdbInfoId] = v.YEdbInfoId
  1502. edbIdList = append(edbIdList, v.YEdbInfoId)
  1503. }
  1504. }
  1505. }
  1506. return
  1507. }
  1508. // GetSectionScatterChartData 截面散点图的数据处理
  1509. 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) {
  1510. // 指标数据数组(10086:{"2022-12-02":100.01,"2022-12-01":102.3})
  1511. edbDataMap := make(map[int]map[string]float64)
  1512. for edbInfoId, edbDataList := range edbDataListMap {
  1513. edbDateData := make(map[string]float64)
  1514. for _, edbData := range edbDataList {
  1515. edbDateData[edbData.DataTime] = edbData.Value
  1516. }
  1517. edbDataMap[edbInfoId] = edbDateData
  1518. }
  1519. // edbIdList 指标展示顺序;x轴的指标顺序
  1520. edbIdList = make([]int, 0)
  1521. edbMappingMap := make(map[int]*data_manage.ChartEdbInfoMapping)
  1522. for _, v := range mappingList {
  1523. edbIdList = append(edbIdList, v.EdbInfoId)
  1524. edbMappingMap[v.EdbInfoId] = v
  1525. }
  1526. //SectionScatterSeriesInfoResp
  1527. dataListResp := make([]data_manage.SectionScatterSeriesItemResp, 0) //y轴的数据列表
  1528. for _, seriesItem := range extraConfig.SeriesList {
  1529. var maxDate time.Time
  1530. // 系列中的指标数据
  1531. tmpSeriesEdbInfoList := make([]data_manage.SectionScatterEdbItemResp, 0)
  1532. var minXVal, maxXVal, minYVal, maxYVal float64
  1533. for _, edbConf := range seriesItem.EdbInfoList {
  1534. tmpItem := data_manage.SectionScatterEdbItemResp{
  1535. IsShow: edbConf.IsShow,
  1536. Name: edbConf.Name,
  1537. NameEn: edbConf.NameEn,
  1538. } //单个坐标点的数据
  1539. //X轴的数据
  1540. {
  1541. edbInfoId := edbConf.XEdbInfoId //X轴的指标
  1542. edbMappingInfo, ok := edbMappingMap[edbInfoId]
  1543. if !ok {
  1544. continue
  1545. }
  1546. findDate := edbConf.XDate //需要的日期值
  1547. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  1548. if len(dataList) <= 0 {
  1549. // 没有数据的指标id
  1550. //findDataList = append(findDataList, 0)
  1551. continue
  1552. }
  1553. tmpItem.XEdbInfoId = edbInfoId
  1554. tmpItem.XName = edbMappingInfo.EdbName
  1555. tmpItem.XNameEn = edbMappingInfo.EdbNameEn
  1556. switch edbConf.XDateType {
  1557. case 1: //最新值
  1558. findDate = dataList[len(dataList)-1].DataTime
  1559. case 2: //近期几天
  1560. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  1561. if tmpErr != nil {
  1562. err = tmpErr
  1563. return
  1564. }
  1565. findDateTime = findDateTime.AddDate(0, 0, -edbConf.XDateValue)
  1566. lenData := len(dataList) - 1
  1567. for i := lenData; i >= 0; i-- {
  1568. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  1569. if tmpErr != nil {
  1570. err = tmpErr
  1571. return
  1572. }
  1573. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  1574. findDate = dataList[i].DataTime
  1575. break
  1576. }
  1577. }
  1578. case 3: // 固定日期
  1579. //最早的日期
  1580. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  1581. if tmpErr != nil {
  1582. err = tmpErr
  1583. return
  1584. }
  1585. //寻找固定日期的数据
  1586. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, edbConf.XDate, time.Local)
  1587. if tmpErr != nil {
  1588. err = tmpErr
  1589. return
  1590. }
  1591. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  1592. tmpDate := tmpDateTime.Format(utils.FormatDate)
  1593. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  1594. findDate = tmpDate
  1595. break
  1596. }
  1597. }
  1598. default:
  1599. err = errors.New(fmt.Sprint("日期类型异常,Type:", edbConf.XDate))
  1600. return
  1601. }
  1602. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  1603. if maxDate.IsZero() {
  1604. maxDate = findDateTime
  1605. } else {
  1606. if findDateTime.After(maxDate) {
  1607. maxDate = findDateTime
  1608. }
  1609. }
  1610. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  1611. tmpItem.XDate = findDate
  1612. tmpItem.XValue = tmpValue
  1613. } else {
  1614. continue
  1615. }
  1616. }
  1617. //Y轴的数据
  1618. {
  1619. edbInfoId := edbConf.YEdbInfoId //Y轴的指标
  1620. edbMappingInfo, ok := edbMappingMap[edbInfoId]
  1621. if !ok {
  1622. continue
  1623. }
  1624. findDate := edbConf.YDate //需要的日期值
  1625. dataList := edbDataListMap[edbInfoId] //指标的所有数据值
  1626. if len(dataList) <= 0 {
  1627. // 没有数据的指标id
  1628. //findDataList = append(findDataList, 0)
  1629. continue
  1630. }
  1631. tmpItem.YEdbInfoId = edbInfoId
  1632. tmpItem.YName = edbMappingInfo.EdbName
  1633. tmpItem.YNameEn = edbMappingInfo.EdbNameEn
  1634. switch edbConf.YDateType {
  1635. case 1: //最新值
  1636. findDate = dataList[len(dataList)-1].DataTime
  1637. case 2: //近期几天
  1638. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[len(dataList)-1].DataTime, time.Local)
  1639. if tmpErr != nil {
  1640. err = tmpErr
  1641. return
  1642. }
  1643. findDateTime = findDateTime.AddDate(0, 0, -edbConf.YDateValue)
  1644. lenData := len(dataList) - 1
  1645. for i := lenData; i >= 0; i-- {
  1646. currDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[i].DataTime, time.Local)
  1647. if tmpErr != nil {
  1648. err = tmpErr
  1649. return
  1650. }
  1651. if currDateTime.Equal(findDateTime) || currDateTime.Before(findDateTime) {
  1652. findDate = dataList[i].DataTime
  1653. break
  1654. }
  1655. }
  1656. case 3: // 固定日期
  1657. //最早的日期
  1658. minDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, dataList[0].DataTime, time.Local)
  1659. if tmpErr != nil {
  1660. err = tmpErr
  1661. return
  1662. }
  1663. //寻找固定日期的数据
  1664. findDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, edbConf.YDate, time.Local)
  1665. if tmpErr != nil {
  1666. err = tmpErr
  1667. return
  1668. }
  1669. for tmpDateTime := findDateTime; tmpDateTime.After(minDateTime) || tmpDateTime.Equal(minDateTime); tmpDateTime = tmpDateTime.AddDate(0, 0, -1) {
  1670. tmpDate := tmpDateTime.Format(utils.FormatDate)
  1671. if _, ok := edbDataMap[edbInfoId][tmpDate]; ok { //如果能找到数据,那么就返回
  1672. findDate = tmpDate
  1673. break
  1674. }
  1675. }
  1676. default:
  1677. err = errors.New(fmt.Sprint("日期类型异常,Type:", edbConf.YDate))
  1678. return
  1679. }
  1680. findDateTime, _ := time.ParseInLocation(utils.FormatDate, findDate, time.Local)
  1681. if maxDate.IsZero() {
  1682. maxDate = findDateTime
  1683. } else {
  1684. if findDateTime.After(maxDate) {
  1685. maxDate = findDateTime
  1686. }
  1687. }
  1688. if tmpValue, ok := edbDataMap[edbInfoId][findDate]; ok {
  1689. tmpItem.YDate = findDate
  1690. tmpItem.YValue = tmpValue
  1691. } else {
  1692. continue
  1693. }
  1694. }
  1695. // 获取当前系列的X轴的最大最小值
  1696. {
  1697. if tmpItem.XValue < minXVal {
  1698. minXVal = tmpItem.XValue
  1699. }
  1700. if tmpItem.XValue > maxXVal {
  1701. maxXVal = tmpItem.XValue
  1702. }
  1703. if tmpItem.YValue < minYVal {
  1704. minYVal = tmpItem.YValue
  1705. }
  1706. if tmpItem.YValue > maxYVal {
  1707. maxYVal = tmpItem.YValue
  1708. }
  1709. }
  1710. tmpSeriesEdbInfoList = append(tmpSeriesEdbInfoList, tmpItem)
  1711. }
  1712. trendLimitData := make([]data_manage.CoordinatePoint, 0) //趋势线的前后坐标点
  1713. var trendLine, rSquare string
  1714. // 生成线性方程式
  1715. var a, b float64
  1716. {
  1717. coordinateData := make([]utils.Coordinate, 0)
  1718. for _, tmpSeriesEdbInfo := range tmpSeriesEdbInfoList {
  1719. tmpCoordinate1 := utils.Coordinate{
  1720. X: tmpSeriesEdbInfo.XValue,
  1721. Y: tmpSeriesEdbInfo.YValue,
  1722. }
  1723. coordinateData = append(coordinateData, tmpCoordinate1)
  1724. }
  1725. // 只有存在两个坐标点的时候,才能去计算线性方程和R平方
  1726. if len(coordinateData) >= 2 {
  1727. a, b = utils.GetLinearResult(coordinateData)
  1728. if !math.IsNaN(a) && !math.IsNaN(b) {
  1729. if b > 0 {
  1730. trendLine = fmt.Sprintf("y=%sx+%s", utils.SubFloatToString(a, 4), utils.SubFloatToString(b, 4))
  1731. } else {
  1732. trendLine = fmt.Sprintf("y=%sx%s", utils.SubFloatToString(a, 4), utils.SubFloatToString(b, 4))
  1733. }
  1734. minYVal, _ = decimal.NewFromFloat(a).Mul(decimal.NewFromFloat(minXVal)).Add(decimal.NewFromFloat(b)).Round(4).Float64()
  1735. maxYVal, _ = decimal.NewFromFloat(a).Mul(decimal.NewFromFloat(maxXVal)).Add(decimal.NewFromFloat(b)).Round(4).Float64()
  1736. }
  1737. // 计算R平方
  1738. rSquare = fmt.Sprint(utils.CalculationDecisive(coordinateData))
  1739. }
  1740. trendLimitData = append(trendLimitData, data_manage.CoordinatePoint{
  1741. X: minXVal,
  1742. Y: minYVal,
  1743. }, data_manage.CoordinatePoint{
  1744. X: maxXVal,
  1745. Y: maxYVal,
  1746. })
  1747. }
  1748. dataListResp = append(dataListResp, data_manage.SectionScatterSeriesItemResp{
  1749. Name: seriesItem.Name,
  1750. NameEn: seriesItem.NameEn,
  1751. IsNameDefault: seriesItem.IsNameDefault,
  1752. Color: seriesItem.Color,
  1753. EdbInfoList: tmpSeriesEdbInfoList,
  1754. ShowTrendLine: seriesItem.ShowTrendLine,
  1755. ShowFitEquation: seriesItem.ShowFitEquation,
  1756. ShowRSquare: seriesItem.ShowRSquare,
  1757. TrendLine: trendLine,
  1758. RSquare: rSquare,
  1759. TrendLimitData: trendLimitData,
  1760. })
  1761. }
  1762. // 截面散点图点击详情时自动更新系列名
  1763. if len(extraConfig.SeriesList) > 0 {
  1764. // 默认名字的时候才自动更新
  1765. if extraConfig.SeriesList[0].IsNameDefault {
  1766. firstXEdbInfoId := extraConfig.SeriesList[0].EdbInfoList[0].XEdbInfoId
  1767. needUpdate := false
  1768. if v, ok := edbMappingMap[firstXEdbInfoId]; ok {
  1769. extraConfig.SeriesList[0].Name = v.LatestDate
  1770. extraConfig.SeriesList[0].NameEn = v.LatestDate
  1771. dataListResp[0].Name = v.LatestDate
  1772. dataListResp[0].NameEn = v.LatestDate
  1773. needUpdate = true
  1774. }
  1775. extraConfigByte, e := json.Marshal(extraConfig)
  1776. if e != nil {
  1777. errMsg := "截面散点系列更新异常"
  1778. err = errors.New(errMsg)
  1779. return
  1780. }
  1781. extraConfigStr := string(extraConfigByte)
  1782. if needUpdate {
  1783. err = data_manage.EditChartInfoExtraConfig(chartInfoId, extraConfigStr)
  1784. if err != nil {
  1785. errMsg := "截面散点系列更新异常"
  1786. err = errors.New(errMsg)
  1787. return
  1788. }
  1789. }
  1790. }
  1791. }
  1792. chartDataResp = data_manage.SectionScatterInfoResp{
  1793. XName: extraConfig.XName,
  1794. XNameEn: extraConfig.XNameEn,
  1795. XUnitName: extraConfig.XUnitName,
  1796. XUnitNameEn: extraConfig.XUnitNameEn,
  1797. YName: extraConfig.YName,
  1798. YNameEn: extraConfig.YNameEn,
  1799. YUnitName: extraConfig.YUnitName,
  1800. YUnitNameEn: extraConfig.YUnitNameEn,
  1801. XMinValue: extraConfig.XMinValue,
  1802. XMaxValue: extraConfig.XMaxValue,
  1803. YMinValue: extraConfig.YMinValue,
  1804. YMaxValue: extraConfig.YMaxValue,
  1805. DataList: dataListResp,
  1806. }
  1807. return
  1808. }
  1809. // AddChartInfo 添加图表
  1810. func AddChartInfo(req data_manage.AddChartInfoReq, sysUserId int, sysUserRealName string) (chartInfo *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  1811. isSendEmail = true // 默认错误的时候要发送邮件
  1812. req.ChartName = strings.Trim(req.ChartName, " ")
  1813. if req.ChartName == "" {
  1814. errMsg = "请填写图表名称!"
  1815. err = errors.New(errMsg)
  1816. isSendEmail = false
  1817. return
  1818. }
  1819. if req.ChartClassifyId <= 0 {
  1820. errMsg = "分类参数错误!"
  1821. err = errors.New(errMsg)
  1822. isSendEmail = false
  1823. return
  1824. }
  1825. chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
  1826. if err != nil {
  1827. if err.Error() == utils.ErrNoRow() {
  1828. errMsg = "分类不存在"
  1829. err = errors.New(errMsg)
  1830. return
  1831. }
  1832. errMsg = "获取分类信息失败"
  1833. return
  1834. }
  1835. if chartClassify == nil {
  1836. errMsg = "分类不存在"
  1837. err = errors.New(errMsg)
  1838. return
  1839. }
  1840. chartType := req.ChartType
  1841. extraConfig := req.ExtraConfig
  1842. // 季节性图表额外配置信息
  1843. var seasonExtraConfig string
  1844. // 关联指标
  1845. var edbInfoIdArr []int
  1846. chartEdbInfoList := req.ChartEdbInfoList
  1847. if len(chartEdbInfoList) > 0 {
  1848. for _, v := range chartEdbInfoList {
  1849. edbInfoId := v.EdbInfoId
  1850. edbInfo, tmpErr := data_manage.GetEdbInfoById(edbInfoId)
  1851. if tmpErr != nil {
  1852. if tmpErr.Error() == utils.ErrNoRow() {
  1853. errMsg = "指标不存在!"
  1854. err = errors.New("指标不存在,edbInfoId:" + strconv.Itoa(edbInfoId))
  1855. return
  1856. } else {
  1857. errMsg = "获取指标信息失败!"
  1858. err = errors.New("获取图表的指标信息失败,Err:" + tmpErr.Error())
  1859. return
  1860. }
  1861. }
  1862. if edbInfo == nil {
  1863. errMsg = "指标已被删除,请重新选择!"
  1864. err = errors.New("指标不存在,edbInfoId:" + strconv.Itoa(edbInfoId))
  1865. return
  1866. } else {
  1867. if edbInfo.EdbInfoId <= 0 {
  1868. errMsg = "指标已被删除,请重新选择!"
  1869. err = errors.New("指标不存在,edbInfoId:" + strconv.Itoa(edbInfoId))
  1870. return
  1871. }
  1872. }
  1873. edbInfoIdArr = append(edbInfoIdArr, edbInfoId)
  1874. edbInfo.EdbNameSource = edbInfo.EdbName
  1875. //edbList = append(edbList, edbInfo)
  1876. //新增季节性图表数据计算
  1877. if chartType == 2 {
  1878. existCount, tmpErr := data_manage.GetEdbDataQuarterCount(edbInfoId)
  1879. if tmpErr != nil {
  1880. errMsg = "保存失败"
  1881. err = errors.New("判断季节性图表,指标数据是否存在失败,Err:" + tmpErr.Error())
  1882. return
  1883. }
  1884. if existCount <= 0 {
  1885. go data_manage.AddCalculateQuarter(edbInfoId, edbInfo.Source, edbInfo.EdbCode)
  1886. }
  1887. // 处理季节性图表横轴配置
  1888. {
  1889. if req.SeasonExtraConfig.XEndDate != "" {
  1890. if req.SeasonExtraConfig.XStartDate > req.SeasonExtraConfig.XEndDate && req.SeasonExtraConfig.JumpYear != 1 {
  1891. errMsg = "季节性图表配置信息异常:横坐标日期配置错误"
  1892. err = errors.New("季节性图表配置信息异常: 横坐标日期配置错误")
  1893. return
  1894. }
  1895. seasonExtra, tErr := json.Marshal(req.SeasonExtraConfig)
  1896. if tErr != nil {
  1897. errMsg = "季节性图表配置信息异常"
  1898. err = errors.New("季节性图表配置信息异常,Err:" + tErr.Error())
  1899. return
  1900. }
  1901. seasonExtraConfig = string(seasonExtra)
  1902. }
  1903. }
  1904. }
  1905. }
  1906. } else {
  1907. if extraConfig == `` {
  1908. errMsg = "请选择指标!"
  1909. err = errors.New(errMsg)
  1910. return
  1911. }
  1912. tmpEdbInfoIdArr, tmpErr, tmpErrMsg := CheckChartExtraConfig(chartType, extraConfig)
  1913. if err != nil {
  1914. errMsg = tmpErrMsg
  1915. err = errors.New("添加失败:" + tmpErr.Error())
  1916. return
  1917. }
  1918. edbInfoIdArr = tmpEdbInfoIdArr
  1919. lenEdbInfoIdArr := len(edbInfoIdArr)
  1920. if lenEdbInfoIdArr > 0 {
  1921. tmpEdbList, tmpErr := data_manage.GetEdbInfoByIdList(edbInfoIdArr)
  1922. if tmpErr != nil {
  1923. errMsg = "指标异常!"
  1924. err = errors.New("指标异常!Err:" + tmpErr.Error())
  1925. return
  1926. }
  1927. if len(tmpEdbList) != lenEdbInfoIdArr {
  1928. errMsg = "指标异常!"
  1929. err = errors.New(fmt.Sprint("查找出来的指标数量与选择的指标数量不符!查找出来的指标数量:", len(tmpEdbList), ";用户选择的指标数量:", lenEdbInfoIdArr))
  1930. return
  1931. }
  1932. for _, v := range tmpEdbList {
  1933. chartEdbInfoList = append(chartEdbInfoList, &data_manage.ChartSaveItem{
  1934. EdbInfoId: v.EdbInfoId,
  1935. MaxData: 0,
  1936. MinData: 0,
  1937. IsOrder: false,
  1938. IsAxis: 1,
  1939. EdbInfoType: 1,
  1940. LeadValue: 0,
  1941. LeadUnit: "",
  1942. ChartStyle: "",
  1943. ChartColor: "",
  1944. PredictChartColor: "",
  1945. ChartWidth: 0,
  1946. Source: utils.CHART_SOURCE_DEFAULT,
  1947. })
  1948. }
  1949. }
  1950. }
  1951. if len(edbInfoIdArr) <= 0 {
  1952. errMsg = "请选择指标!"
  1953. err = errors.New(errMsg)
  1954. return
  1955. }
  1956. // 图表额外配置
  1957. extraConfig, err, errMsg = HandleExtraConfig(chartType, extraConfig)
  1958. if err != nil {
  1959. if errMsg == `` {
  1960. errMsg = "指标异常!"
  1961. }
  1962. err = errors.New("指标异常!Err:" + err.Error())
  1963. return
  1964. }
  1965. sort.Ints(edbInfoIdArr)
  1966. var edbInfoIdArrStr []string
  1967. for _, v := range edbInfoIdArr {
  1968. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
  1969. }
  1970. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  1971. //count, err := data_manage.ChartInfoExist("", edbInfoIdStr)
  1972. //if err != nil && err.Error() != utils.ErrNoRow() {
  1973. // br.Msg = "保存失败"
  1974. // br.ErrMsg = "判断图表名称是否存在失败,Err:" + err.Error()
  1975. // return
  1976. //}
  1977. //if count > 0 {
  1978. // br.Msg = "所选指标已存在相同指标"
  1979. // return
  1980. //}
  1981. //判断图表是否存在
  1982. var condition string
  1983. var pars []interface{}
  1984. // 图表名称在不同图分类下不允许重复 需求调整时间:2022年11月07日09:47:07
  1985. //condition += " AND chart_classify_id=? "
  1986. //pars = append(pars, req.ChartClassifyId)
  1987. condition += " AND chart_name=? AND source = ? "
  1988. pars = append(pars, req.ChartName, utils.CHART_SOURCE_DEFAULT)
  1989. count, err := data_manage.GetChartInfoCountByCondition(condition, pars)
  1990. if err != nil {
  1991. errMsg = "判断图表名称是否存在失败"
  1992. err = errors.New("判断图表名称是否存在失败,Err:" + err.Error())
  1993. return
  1994. }
  1995. if count > 0 {
  1996. errMsg = "图表已存在,请重新填写"
  1997. err = errors.New("判断图表名称是否存在失败")
  1998. isSendEmail = false
  1999. return
  2000. }
  2001. barChartConf := `` //柱方图的配置信息
  2002. if chartType == 7 {
  2003. barChartConfReq := req.BarChartInfo
  2004. if len(barChartConfReq.EdbInfoIdList) <= 0 {
  2005. errMsg = "请选择指标"
  2006. err = errors.New(errMsg)
  2007. isSendEmail = false
  2008. return
  2009. }
  2010. if len(barChartConfReq.DateList) <= 0 {
  2011. errMsg = "请选择日期"
  2012. err = errors.New(errMsg)
  2013. isSendEmail = false
  2014. return
  2015. }
  2016. barChartConfByte, tmpErr := json.Marshal(barChartConfReq)
  2017. if tmpErr != nil {
  2018. errMsg = "柱方图信息异常"
  2019. err = errors.New("柱方图信息异常,Err:" + tmpErr.Error())
  2020. return
  2021. }
  2022. barChartConf = string(barChartConfByte)
  2023. }
  2024. disableVal := CheckIsDisableChart(edbInfoIdArr)
  2025. chartInfo = new(data_manage.ChartInfo)
  2026. chartInfo.ChartName = req.ChartName
  2027. chartInfo.EdbInfoIds = edbInfoIdStr
  2028. chartInfo.ChartClassifyId = req.ChartClassifyId
  2029. chartInfo.SysUserId = sysUserId
  2030. chartInfo.SysUserRealName = sysUserRealName
  2031. chartInfo.CreateTime = time.Now()
  2032. chartInfo.ModifyTime = time.Now()
  2033. chartInfo.IsSetName = 0
  2034. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  2035. chartInfo.UniqueCode = utils.MD5(utils.CHART_PREFIX + "_" + timestamp)
  2036. // todo 判断是否需要重新计算用户的start_date
  2037. if req.DateType > 0 {
  2038. chartInfo.DateType = req.DateType
  2039. } else {
  2040. chartInfo.DateType = 3
  2041. }
  2042. if chartType == 0 {
  2043. chartType = 1
  2044. }
  2045. chartInfo.ChartType = chartType
  2046. calendar := req.Calendar
  2047. if calendar == "" {
  2048. calendar = "公历"
  2049. }
  2050. chartInfo.Calendar = calendar
  2051. chartInfo.StartDate = req.StartDate
  2052. chartInfo.EndDate = req.EndDate
  2053. chartInfo.SeasonStartDate = req.StartDate
  2054. chartInfo.SeasonEndDate = req.EndDate
  2055. chartInfo.LeftMin = req.LeftMin
  2056. chartInfo.LeftMax = req.LeftMax
  2057. chartInfo.RightMin = req.RightMin
  2058. chartInfo.RightMax = req.RightMax
  2059. chartInfo.Disabled = disableVal
  2060. chartInfo.BarConfig = barChartConf
  2061. chartInfo.ExtraConfig = extraConfig
  2062. chartInfo.SeasonExtraConfig = seasonExtraConfig
  2063. chartInfo.StartYear = req.StartYear
  2064. chartInfo.Source = utils.CHART_SOURCE_DEFAULT
  2065. newId, err := data_manage.AddChartInfo(chartInfo)
  2066. if err != nil {
  2067. errMsg = `保存失败`
  2068. err = errors.New("保存失败,Err:" + err.Error())
  2069. return
  2070. }
  2071. chartInfo.ChartInfoId = int(newId)
  2072. mapList := make([]*data_manage.ChartEdbMapping, 0)
  2073. for _, v := range chartEdbInfoList {
  2074. mapItem := new(data_manage.ChartEdbMapping)
  2075. mapItem.ChartInfoId = int(newId)
  2076. mapItem.EdbInfoId = v.EdbInfoId
  2077. mapItem.CreateTime = time.Now()
  2078. mapItem.ModifyTime = time.Now()
  2079. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  2080. mapItem.UniqueCode = utils.MD5(fmt.Sprint(utils.CHART_PREFIX, "_", v.EdbInfoId, "_", timestamp))
  2081. mapItem.MaxData = v.MaxData
  2082. mapItem.MinData = v.MinData
  2083. mapItem.IsOrder = v.IsOrder
  2084. mapItem.IsAxis = v.IsAxis
  2085. mapItem.EdbInfoType = v.EdbInfoType
  2086. mapItem.LeadValue = v.LeadValue
  2087. mapItem.LeadUnit = v.LeadUnit
  2088. mapItem.ChartStyle = v.ChartStyle
  2089. mapItem.ChartColor = v.ChartColor
  2090. mapItem.PredictChartColor = v.PredictChartColor
  2091. mapItem.ChartWidth = v.ChartWidth
  2092. mapItem.Source = utils.CHART_SOURCE_DEFAULT
  2093. mapList = append(mapList, mapItem)
  2094. }
  2095. err = data_manage.AddChartEdbMapping(mapList)
  2096. if err != nil {
  2097. errMsg = `保存失败`
  2098. err = errors.New("保存失败,Err:" + err.Error())
  2099. return
  2100. }
  2101. //添加es数据
  2102. go EsAddOrEditChartInfo(chartInfo.ChartInfoId)
  2103. return
  2104. }
  2105. // EditChartInfo 编辑图表
  2106. func EditChartInfo(req data_manage.EditChartInfoReq, sysUser *system.Admin) (chartItem *data_manage.ChartInfo, err error, errMsg string, isSendEmail bool) {
  2107. isSendEmail = true // 默认错误的时候要发送邮件
  2108. req.ChartName = strings.Trim(req.ChartName, " ")
  2109. if req.ChartName == "" {
  2110. errMsg = "请填写图表名称!"
  2111. err = errors.New(errMsg)
  2112. isSendEmail = false
  2113. return
  2114. }
  2115. if req.ChartClassifyId <= 0 {
  2116. errMsg = "分类参数错误!"
  2117. err = errors.New(errMsg)
  2118. isSendEmail = false
  2119. return
  2120. }
  2121. chartClassify, err := data_manage.GetChartClassifyById(req.ChartClassifyId)
  2122. if err != nil {
  2123. if err.Error() == utils.ErrNoRow() {
  2124. errMsg = "分类不存在"
  2125. err = errors.New(errMsg)
  2126. return
  2127. }
  2128. errMsg = "获取分类信息失败"
  2129. return
  2130. }
  2131. if chartClassify == nil {
  2132. errMsg = "分类不存在"
  2133. err = errors.New(errMsg)
  2134. return
  2135. }
  2136. chartItem, err = data_manage.GetChartInfoById(req.ChartInfoId)
  2137. if err != nil {
  2138. if err.Error() == utils.ErrNoRow() {
  2139. errMsg = "图表已被删除,请刷新页面"
  2140. err = errors.New(errMsg)
  2141. isSendEmail = false
  2142. return
  2143. }
  2144. errMsg = "获取图表信息失败"
  2145. err = errors.New("获取图表信息失败,Err:" + err.Error())
  2146. return
  2147. }
  2148. // 季节性图表额外配置信息
  2149. var seasonExtraConfig string
  2150. // 图表关联指标id
  2151. chartEdbInfoList := make([]*data_manage.ChartSaveItem, 0)
  2152. // 关联指标
  2153. var edbInfoIdArr []int
  2154. // 非散点截面图的直接通过前端获取,散点截面图需要额外从配置中获取
  2155. if chartItem.ChartType != 10 {
  2156. //edbList := make([]*data_manage.EdbInfo, 0)
  2157. for _, v := range req.ChartEdbInfoList {
  2158. edbInfoId := v.EdbInfoId
  2159. edbInfo, tmpErr := data_manage.GetEdbInfoById(edbInfoId)
  2160. if tmpErr != nil {
  2161. if tmpErr.Error() == utils.ErrNoRow() {
  2162. errMsg = "指标不存在!"
  2163. err = errors.New("指标不存在,edbInfoId:" + strconv.Itoa(edbInfoId))
  2164. return
  2165. } else {
  2166. errMsg = "获取指标信息失败!"
  2167. err = errors.New("获取图表的指标信息失败,Err:" + tmpErr.Error())
  2168. return
  2169. }
  2170. }
  2171. if edbInfo == nil {
  2172. errMsg = "指标已被删除,请重新选择!"
  2173. err = errors.New("指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
  2174. return
  2175. } else {
  2176. if edbInfo.EdbInfoId <= 0 {
  2177. errMsg = "指标已被删除,请重新选择!"
  2178. err = errors.New("指标不存在,ChartInfoId:" + strconv.Itoa(edbInfoId))
  2179. return
  2180. }
  2181. }
  2182. edbInfoIdArr = append(edbInfoIdArr, edbInfoId)
  2183. edbInfo.EdbNameSource = edbInfo.EdbName
  2184. //edbList = append(edbList, edbInfo)
  2185. //新增季节性图表数据计算
  2186. if req.ChartType == 2 {
  2187. existCount, tmpErr := data_manage.GetEdbDataQuarterCount(edbInfoId)
  2188. if tmpErr != nil {
  2189. errMsg = "保存失败"
  2190. err = errors.New("判断季节性图表,指标数据是否存在失败,Err:" + tmpErr.Error())
  2191. return
  2192. }
  2193. if existCount <= 0 {
  2194. go data_manage.AddCalculateQuarter(edbInfoId, edbInfo.Source, edbInfo.EdbCode)
  2195. }
  2196. // 处理季节性图表横轴配置
  2197. {
  2198. if req.SeasonExtraConfig.XEndDate != "" {
  2199. if req.SeasonExtraConfig.XStartDate > req.SeasonExtraConfig.XEndDate && req.SeasonExtraConfig.JumpYear != 1 {
  2200. errMsg = "季节性图表配置信息异常:横坐标日期配置错误"
  2201. err = errors.New("季节性图表配置信息异常: 横坐标日期配置错误")
  2202. return
  2203. }
  2204. seasonExtra, tErr := json.Marshal(req.SeasonExtraConfig)
  2205. if tErr != nil {
  2206. errMsg = "季节性图表配置信息异常"
  2207. err = errors.New("季节性图表配置信息异常,Err:" + tErr.Error())
  2208. return
  2209. }
  2210. seasonExtraConfig = string(seasonExtra)
  2211. }
  2212. }
  2213. }
  2214. }
  2215. chartEdbInfoList = req.ChartEdbInfoList
  2216. } else {
  2217. if req.ExtraConfig == `` {
  2218. errMsg = "请选择指标!"
  2219. err = errors.New(errMsg)
  2220. isSendEmail = false
  2221. return
  2222. }
  2223. // 校验配置的指标列表
  2224. tmpEdbInfoIdArr, tmpErr, tmpErrMsg := CheckChartExtraConfig(req.ChartType, req.ExtraConfig)
  2225. if tmpErr != nil {
  2226. errMsg = tmpErrMsg
  2227. err = errors.New("添加失败:" + tmpErr.Error())
  2228. return
  2229. }
  2230. edbInfoIdArr = tmpEdbInfoIdArr
  2231. lenEdbInfoIdArr := len(edbInfoIdArr)
  2232. if lenEdbInfoIdArr > 0 {
  2233. tmpEdbList, tmpErr := data_manage.GetEdbInfoByIdList(edbInfoIdArr)
  2234. if tmpErr != nil {
  2235. errMsg = "指标异常!"
  2236. err = errors.New("指标异常!Err:" + tmpErr.Error())
  2237. return
  2238. }
  2239. if len(tmpEdbList) != lenEdbInfoIdArr {
  2240. errMsg = "指标异常!"
  2241. err = errors.New(fmt.Sprint("查找出来的指标数量与选择的指标数量不符!查找出来的指标数量:", len(tmpEdbList), ";用户选择的指标数量:", lenEdbInfoIdArr))
  2242. return
  2243. }
  2244. for _, v := range tmpEdbList {
  2245. chartEdbInfoList = append(chartEdbInfoList, &data_manage.ChartSaveItem{
  2246. EdbInfoId: v.EdbInfoId,
  2247. MaxData: 0,
  2248. MinData: 0,
  2249. IsOrder: false,
  2250. IsAxis: 1,
  2251. EdbInfoType: 1,
  2252. LeadValue: 0,
  2253. LeadUnit: "",
  2254. ChartStyle: "",
  2255. ChartColor: "",
  2256. PredictChartColor: "",
  2257. ChartWidth: 0,
  2258. Source: utils.CHART_SOURCE_DEFAULT,
  2259. })
  2260. }
  2261. }
  2262. }
  2263. if len(edbInfoIdArr) <= 0 {
  2264. errMsg = "请选择指标!"
  2265. err = errors.New(errMsg)
  2266. isSendEmail = false
  2267. return
  2268. }
  2269. // 图表额外配置
  2270. extraConfig, err, errMsg := HandleExtraConfig(req.ChartType, req.ExtraConfig)
  2271. if err != nil {
  2272. if errMsg == `` {
  2273. errMsg = "指标异常!"
  2274. }
  2275. err = errors.New("指标异常!Err:" + err.Error())
  2276. return
  2277. }
  2278. req.ExtraConfig = extraConfig
  2279. //图表操作权限
  2280. ok := CheckOpChartPermission(sysUser, chartItem.SysUserId)
  2281. if !ok {
  2282. errMsg = "没有该图表的操作权限"
  2283. err = errors.New(errMsg)
  2284. isSendEmail = false
  2285. return
  2286. }
  2287. var calendar string
  2288. var dateType int
  2289. dateType = req.DateType
  2290. if req.ChartType != chartItem.ChartType {
  2291. if chartItem.ChartType != 2 && chartItem.Calendar == "" { //非季节性图
  2292. calendar = "公历"
  2293. }
  2294. if chartItem.ChartType == 2 && chartItem.DateType <= 0 {
  2295. dateType = 3
  2296. }
  2297. }
  2298. sort.Ints(edbInfoIdArr)
  2299. var edbInfoIdArrStr []string
  2300. for _, v := range edbInfoIdArr {
  2301. edbInfoIdArrStr = append(edbInfoIdArrStr, strconv.Itoa(v))
  2302. }
  2303. edbInfoIdStr := strings.Join(edbInfoIdArrStr, ",")
  2304. //mappingCondition := ` AND chart_info_id<> ` + strconv.Itoa(req.ChartInfoId)
  2305. //count, err := data_manage.ChartInfoExist(mappingCondition, edbInfoIdStr)
  2306. //if err != nil && err.Error() != utils.ErrNoRow() {
  2307. // br.Msg = "保存失败"
  2308. // br.ErrMsg = "判断图表名称是否存在失败,Err:" + err.Error()
  2309. // return
  2310. //}
  2311. //if count > 0 {
  2312. // br.Msg = "所选指标已存在相同指标"
  2313. // return
  2314. //}
  2315. //判断图表是否存在
  2316. var condition string
  2317. var pars []interface{}
  2318. condition += " AND chart_info_id<>? "
  2319. pars = append(pars, req.ChartInfoId)
  2320. // 图表名称在不同图分类下不允许重复 需求调整时间:2022年11月07日09:47:07
  2321. //condition += " AND chart_classify_id=? "
  2322. //pars = append(pars, req.ChartClassifyId)
  2323. condition += " AND chart_name=? AND source = ? "
  2324. pars = append(pars, req.ChartName, utils.CHART_SOURCE_DEFAULT)
  2325. count, err := data_manage.GetChartInfoCountByCondition(condition, pars)
  2326. if err != nil {
  2327. errMsg = "判断图表名称是否存在失败"
  2328. err = errors.New("判断图表名称是否存在失败,Err:" + err.Error())
  2329. return
  2330. }
  2331. if count > 0 {
  2332. errMsg = "图表已存在,请重新填写"
  2333. err = errors.New(errMsg)
  2334. isSendEmail = false
  2335. return
  2336. }
  2337. barChartConf := `` //柱方图的配置信息
  2338. if req.ChartType == 7 {
  2339. barChartConfReq := req.BarChartInfo
  2340. if len(barChartConfReq.EdbInfoIdList) <= 0 {
  2341. errMsg = "请选择指标"
  2342. err = errors.New(errMsg)
  2343. isSendEmail = false
  2344. return
  2345. }
  2346. if len(barChartConfReq.DateList) <= 0 {
  2347. errMsg = "请选择日期"
  2348. err = errors.New(errMsg)
  2349. isSendEmail = false
  2350. return
  2351. }
  2352. barChartConfByte, tmpErr := json.Marshal(barChartConfReq)
  2353. if tmpErr != nil {
  2354. errMsg = "柱方图信息异常"
  2355. err = errors.New("柱方图信息异常,Err:" + tmpErr.Error())
  2356. return
  2357. }
  2358. barChartConf = string(barChartConfByte)
  2359. }
  2360. // 图表启用与否
  2361. disableVal := CheckIsDisableChart(edbInfoIdArr)
  2362. err = data_manage.EditChartInfoAndMapping(&req, edbInfoIdStr, calendar, dateType, disableVal, barChartConf, chartEdbInfoList, seasonExtraConfig)
  2363. if err != nil {
  2364. errMsg = "保存失败"
  2365. err = errors.New("保存失败,Err:" + err.Error())
  2366. return
  2367. }
  2368. resp := new(data_manage.AddChartInfoResp)
  2369. resp.ChartInfoId = chartItem.ChartInfoId
  2370. resp.UniqueCode = chartItem.UniqueCode
  2371. resp.ChartType = req.ChartType
  2372. //添加es数据
  2373. go EsAddOrEditChartInfo(chartItem.ChartInfoId)
  2374. //修改my eta es数据
  2375. go EsAddOrEditMyChartInfoByChartInfoId(chartItem.ChartInfoId)
  2376. return
  2377. }