edb_info_calculate.go 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/controllers"
  5. "eta_gn/eta_api/models"
  6. "eta_gn/eta_api/models/data_manage"
  7. "eta_gn/eta_api/services/data"
  8. "eta_gn/eta_api/services/data/data_manage_permission"
  9. "eta_gn/eta_api/utils"
  10. "fmt"
  11. "net/url"
  12. "strconv"
  13. "strings"
  14. "time"
  15. "github.com/rdlucklib/rdluck_tools/paging"
  16. )
  17. type EdbInfoCalculateController struct {
  18. controllers.BaseAuthController
  19. }
  20. // @router /edb_info/calculate/save [post]
  21. func (this *ChartInfoController) CalculateSave() {
  22. br := new(models.BaseResponse).Init()
  23. defer func() {
  24. this.Data["json"] = br
  25. this.ServeJSON()
  26. }()
  27. sysUser := this.SysUser
  28. if sysUser == nil {
  29. br.Msg = "请登录"
  30. br.ErrMsg = "请登录,SysUser Is Empty"
  31. br.Ret = 408
  32. return
  33. }
  34. var req data_manage.EdbInfoCalculateSaveReq
  35. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  36. if err != nil {
  37. br.Msg = "参数解析异常!"
  38. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  39. return
  40. }
  41. req.EdbName = strings.Trim(req.EdbName, " ")
  42. if req.EdbName == "" {
  43. br.Msg = "指标名称不能为空"
  44. return
  45. }
  46. if req.Frequency == "" {
  47. br.Msg = "频率不能为空"
  48. return
  49. }
  50. if req.Unit == "" {
  51. br.Msg = "单位不能为空"
  52. return
  53. }
  54. if req.ClassifyId <= 0 {
  55. br.Msg = "请选择分类"
  56. return
  57. }
  58. if len(req.EdbInfoIdArr) <= 0 {
  59. br.Msg = "请选择指标"
  60. return
  61. }
  62. if req.CalculateFormula == "" {
  63. br.Msg = "请填写指标"
  64. return
  65. }
  66. calculateFormula := req.CalculateFormula
  67. calculateFormula = strings.Replace(calculateFormula, "(", "(", -1)
  68. calculateFormula = strings.Replace(calculateFormula, ")", ")", -1)
  69. calculateFormula = strings.Replace(calculateFormula, ",", ",", -1)
  70. calculateFormula = strings.Replace(calculateFormula, "。", ".", -1)
  71. calculateFormula = strings.Replace(calculateFormula, "%", "*0.01", -1)
  72. req.CalculateFormula = calculateFormula
  73. edbInfoMap := make(map[int]string)
  74. {
  75. upperCalculateFormulaStr := strings.ToUpper(req.CalculateFormula)
  76. newEdbInfoIdArr := make([]data_manage.EdbInfoFromTag, 0)
  77. for _, tmpEdbInfo := range req.EdbInfoIdArr {
  78. _, ok := edbInfoMap[tmpEdbInfo.EdbInfoId]
  79. if ok {
  80. br.Msg = "选择指标失败,请勿选择重复指标!"
  81. return
  82. }
  83. edbInfoMap[tmpEdbInfo.EdbInfoId] = tmpEdbInfo.FromTag
  84. upperFromTag := strings.ToUpper(tmpEdbInfo.FromTag)
  85. if strings.Contains(upperCalculateFormulaStr, upperFromTag) {
  86. newEdbInfoIdArr = append(newEdbInfoIdArr, tmpEdbInfo)
  87. }
  88. }
  89. req.EdbInfoIdArr = newEdbInfoIdArr
  90. }
  91. var condition string
  92. var pars []interface{}
  93. condition += " AND edb_name=? "
  94. pars = append(pars, req.EdbName)
  95. count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
  96. if err != nil {
  97. br.Msg = "判断指标名称是否存在失败"
  98. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  99. return
  100. }
  101. if count > 0 {
  102. br.Msg = "指标名称已存在,请重新填写"
  103. br.ErrMsg = "指标名称已存在,请重新填写"
  104. br.IsSendEmail = false
  105. return
  106. }
  107. var formulaStr string
  108. var edbInfoIdBytes []string
  109. for _, v := range req.EdbInfoIdArr {
  110. formulaStr += v.FromTag + ","
  111. edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
  112. }
  113. formulaSlice, err := data.CheckFormulaJson(req.CalculateFormula)
  114. if err != nil {
  115. br.Msg = "公式格式错误,请重新填写"
  116. return
  117. }
  118. for _, formula := range formulaSlice {
  119. formulaMap := data.CheckFormula(formula)
  120. for _, v := range formulaMap {
  121. if !strings.Contains(formulaStr, v) {
  122. br.Msg = "公式错误,请重新填写"
  123. return
  124. }
  125. }
  126. }
  127. req2 := &data_manage.EdbInfoCalculateBatchSaveReqByEdbLib{
  128. AdminId: sysUser.AdminId,
  129. AdminName: sysUser.RealName,
  130. EdbName: req.EdbName,
  131. Frequency: req.Frequency,
  132. Unit: req.Unit,
  133. ClassifyId: req.ClassifyId,
  134. CalculateFormula: req.CalculateFormula,
  135. EdbInfoIdArr: req.EdbInfoIdArr,
  136. EmptyType: req.EmptyType,
  137. MaxEmptyType: req.MaxEmptyType,
  138. Extra: req.Extra,
  139. }
  140. reqJson, err := json.Marshal(req2)
  141. if err != nil {
  142. br.Msg = "参数解析异常!"
  143. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  144. return
  145. }
  146. respItem, err := data.AddEdbCalculateData(string(reqJson), this.Lang)
  147. if err != nil {
  148. br.Msg = "新增失败"
  149. br.ErrMsg = "新增失败,Err:" + err.Error()
  150. return
  151. }
  152. if respItem.Ret != 200 {
  153. br.Msg = respItem.Msg
  154. br.ErrMsg = respItem.ErrMsg
  155. return
  156. }
  157. resp := respItem.Data
  158. data.AddOrEditEdbInfoToEs(resp.EdbInfoId)
  159. br.Ret = 200
  160. br.Success = true
  161. br.Msg = "保存成功"
  162. br.Data = resp
  163. br.IsAddLog = true
  164. }
  165. // @router /edb_info/calculate/detail [get]
  166. func (this *ChartInfoController) CalculateDetail() {
  167. br := new(models.BaseResponse).Init()
  168. defer func() {
  169. this.Data["json"] = br
  170. this.ServeJSON()
  171. }()
  172. sysUser := this.SysUser
  173. if sysUser == nil {
  174. br.Msg = "请登录"
  175. br.ErrMsg = "请登录,SysUser Is Empty"
  176. br.Ret = 408
  177. return
  178. }
  179. edbInfoId, _ := this.GetInt("EdbInfoId")
  180. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  181. if err != nil {
  182. br.Msg = "获取失败"
  183. br.ErrMsg = "获取失败,Err:" + err.Error()
  184. return
  185. }
  186. calculateList, err := data_manage.GetEdbInfoCalculateDetail(edbInfoId, edbInfo.Source)
  187. if err != nil {
  188. br.Msg = "获取失败"
  189. br.ErrMsg = "获取失败,Err:" + err.Error()
  190. return
  191. }
  192. for _, v := range calculateList {
  193. v.ConvertToResp()
  194. }
  195. fullEdb := new(data_manage.EdbInfoFullClassify)
  196. classifyList, err, errMsg := data.GetFullClassifyByClassifyId(edbInfo.ClassifyId)
  197. if err != nil {
  198. br.Msg = err.Error()
  199. br.ErrMsg = errMsg
  200. return
  201. }
  202. edbInfoResp, err := edbInfo.ConvertToResp()
  203. if err != nil {
  204. br.Msg = err.Error()
  205. br.ErrMsg = errMsg
  206. return
  207. }
  208. fullEdb.EdbInfoResp = edbInfoResp
  209. fullEdb.ClassifyList = classifyList
  210. var currClassifyItem *data_manage.EdbClassifyIdItems
  211. for _, v := range classifyList {
  212. if v.ClassifyId == edbInfo.ClassifyId {
  213. currClassifyItem = v
  214. }
  215. }
  216. if currClassifyItem != nil {
  217. haveOperaAuth, err := data_manage_permission.CheckEdbPermission(edbInfo.IsJoinPermission, currClassifyItem.IsJoinPermission, sysUser.AdminId, edbInfo.EdbInfoId, edbInfo.ClassifyId)
  218. if err != nil {
  219. br.Msg = err.Error()
  220. br.ErrMsg = "校验指标权限失败,err:" + err.Error()
  221. return
  222. }
  223. fullEdb.HaveOperaAuth = haveOperaAuth
  224. }
  225. if fullEdb.HaveOperaAuth && edbInfo.Source == utils.DATA_SOURCE_CALCULATE_NHCC {
  226. var aEdbInfo, bEdbInfo *data_manage.EdbInfoCalculateDetail
  227. for _, v := range calculateList {
  228. if v.FromTag == "A" {
  229. aEdbInfo = v
  230. continue
  231. }
  232. if v.FromTag == "B" {
  233. bEdbInfo = v
  234. continue
  235. }
  236. }
  237. if aEdbInfo != nil && bEdbInfo != nil {
  238. edbInfoFromTagList := []data_manage.EdbInfoFromTag{
  239. {
  240. EdbInfoId: aEdbInfo.FromEdbInfoId,
  241. FromTag: aEdbInfo.FromTag,
  242. MoveValue: aEdbInfo.MoveValue,
  243. }, {
  244. EdbInfoId: bEdbInfo.FromEdbInfoId,
  245. FromTag: bEdbInfo.FromTag,
  246. MoveValue: bEdbInfo.MoveValue,
  247. },
  248. }
  249. req2 := &data_manage.EdbInfoCalculateBatchSaveReqByEdbLib{
  250. AdminId: sysUser.AdminId,
  251. AdminName: sysUser.RealName,
  252. Formula: edbInfo.CalculateFormula,
  253. EdbInfoIdArr: edbInfoFromTagList,
  254. }
  255. val, err, _ := data.CallCalculateComputeCorrelation(req2, this.Lang)
  256. if err == nil {
  257. fullEdb.CorrelationStr = val
  258. }
  259. }
  260. }
  261. resp := new(data_manage.CalculateDetailResp)
  262. resp.EdbInfoDetail = fullEdb
  263. resp.CalculateList = calculateList
  264. br.Ret = 200
  265. br.Success = true
  266. br.Msg = "保存成功"
  267. br.Data = resp
  268. }
  269. // @router /edb_info/calculate/edit [post]
  270. func (this *ChartInfoController) CalculateEdit() {
  271. br := new(models.BaseResponse).Init()
  272. defer func() {
  273. this.Data["json"] = br
  274. this.ServeJSON()
  275. }()
  276. sysUser := this.SysUser
  277. if sysUser == nil {
  278. br.Msg = "请登录"
  279. br.ErrMsg = "请登录,SysUser Is Empty"
  280. br.Ret = 408
  281. return
  282. }
  283. var req data_manage.EdbInfoCalculateEditReq
  284. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  285. if err != nil {
  286. br.Msg = "参数解析异常!"
  287. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  288. return
  289. }
  290. if req.EdbInfoId <= 0 {
  291. br.Msg = "参数错误"
  292. br.ErrMsg = "指标ID:" + strconv.Itoa(req.EdbInfoId)
  293. return
  294. }
  295. req.EdbName = strings.Trim(req.EdbName, " ")
  296. if req.EdbName == "" {
  297. br.Msg = "指标名称不能为空"
  298. return
  299. }
  300. if req.Frequency == "" {
  301. br.Msg = "频率不能为空"
  302. return
  303. }
  304. if req.Unit == "" {
  305. br.Msg = "单位不能为空"
  306. return
  307. }
  308. if req.ClassifyId <= 0 {
  309. br.Msg = "请选择分类"
  310. return
  311. }
  312. if len(req.EdbInfoIdArr) <= 0 {
  313. br.Msg = "请选择指标"
  314. return
  315. }
  316. if req.CalculateFormula == "" {
  317. br.Msg = "请填写指标"
  318. return
  319. }
  320. calculateFormula := req.CalculateFormula
  321. calculateFormula = strings.Replace(calculateFormula, "(", "(", -1)
  322. calculateFormula = strings.Replace(calculateFormula, ")", ")", -1)
  323. calculateFormula = strings.Replace(calculateFormula, ",", ",", -1)
  324. calculateFormula = strings.Replace(calculateFormula, "。", ".", -1)
  325. calculateFormula = strings.Replace(calculateFormula, "%", "*0.01", -1)
  326. req.CalculateFormula = calculateFormula
  327. fmt.Println("calculateFormula:" + calculateFormula)
  328. edbInfoMap := make(map[int]string)
  329. {
  330. upperCalculateFormulaStr := strings.ToUpper(req.CalculateFormula)
  331. newEdbInfoIdArr := make([]data_manage.EdbInfoFromTag, 0)
  332. for _, tmpEdbInfo := range req.EdbInfoIdArr {
  333. _, ok := edbInfoMap[tmpEdbInfo.EdbInfoId]
  334. if ok {
  335. br.Msg = "选择指标失败,请勿选择重复指标!"
  336. return
  337. }
  338. edbInfoMap[tmpEdbInfo.EdbInfoId] = tmpEdbInfo.FromTag
  339. upperFromTag := strings.ToUpper(tmpEdbInfo.FromTag)
  340. if strings.Contains(upperCalculateFormulaStr, upperFromTag) {
  341. newEdbInfoIdArr = append(newEdbInfoIdArr, tmpEdbInfo)
  342. }
  343. }
  344. req.EdbInfoIdArr = newEdbInfoIdArr
  345. }
  346. var condition string
  347. var pars []interface{}
  348. condition += " AND edb_name=? "
  349. pars = append(pars, req.EdbName)
  350. condition += " AND edb_info_id<>? "
  351. pars = append(pars, req.EdbInfoId)
  352. count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
  353. if err != nil {
  354. br.Msg = "判断指标名称是否存在失败"
  355. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  356. return
  357. }
  358. if count > 0 {
  359. br.Msg = "指标名称已存在,请重新填写"
  360. br.ErrMsg = "指标名称已存在,请重新填写"
  361. br.IsSendEmail = false
  362. return
  363. }
  364. edbInfoDetail, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  365. if err != nil {
  366. br.Msg = "修改失败"
  367. br.Msg = "获取指标信息失败,GetEdbInfoById Err:" + err.Error()
  368. return
  369. }
  370. if edbInfoDetail == nil {
  371. br.Msg = "修改失败"
  372. br.Msg = "指标信息不存在,EdbInfoId:" + strconv.Itoa(req.EdbInfoId)
  373. return
  374. }
  375. req2 := &data_manage.EdbInfoCalculateBatchSaveReqByEdbLib{
  376. AdminId: sysUser.AdminId,
  377. AdminName: sysUser.RealName,
  378. EdbInfoId: req.EdbInfoId,
  379. EdbName: req.EdbName,
  380. Frequency: req.Frequency,
  381. Unit: req.Unit,
  382. ClassifyId: req.ClassifyId,
  383. CalculateFormula: req.CalculateFormula,
  384. EdbInfoIdArr: req.EdbInfoIdArr,
  385. EmptyType: req.EmptyType,
  386. MaxEmptyType: req.MaxEmptyType,
  387. Extra: req.Extra,
  388. }
  389. reqJson, err := json.Marshal(req2)
  390. if err != nil {
  391. br.Msg = "参数解析异常!"
  392. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  393. return
  394. }
  395. respItem, err := data.EditEdbCalculateData(string(reqJson), this.Lang)
  396. if err != nil {
  397. br.Msg = "编辑失败"
  398. br.ErrMsg = "编辑失败,Err:" + err.Error()
  399. return
  400. }
  401. if respItem.Ret != 200 {
  402. br.Msg = respItem.Msg
  403. br.ErrMsg = respItem.ErrMsg
  404. return
  405. }
  406. resp := respItem.Data
  407. data.AddOrEditEdbInfoToEs(req.EdbInfoId)
  408. go data.ModifyPredictEdbBaseInfoBySourceEdb(edbInfoDetail, req.Frequency, req.Unit)
  409. br.Ret = 200
  410. br.Success = true
  411. br.Msg = "保存成功"
  412. br.IsAddLog = true
  413. br.Data = resp
  414. }
  415. // @router /edb_info/calculate/batch/save [post]
  416. func (this *ChartInfoController) CalculateBatchSave() {
  417. br := new(models.BaseResponse).Init()
  418. defer func() {
  419. if br.ErrMsg == "" {
  420. br.IsSendEmail = false
  421. }
  422. this.Data["json"] = br
  423. this.ServeJSON()
  424. }()
  425. sysUser := this.SysUser
  426. if sysUser == nil {
  427. br.Msg = "请登录"
  428. br.ErrMsg = "请登录,SysUser Is Empty"
  429. br.Ret = 408
  430. return
  431. }
  432. var req data_manage.EdbInfoCalculateBatchSaveReq
  433. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  434. if err != nil {
  435. br.Msg = "参数解析异常!"
  436. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  437. return
  438. }
  439. req.EdbName = strings.Trim(req.EdbName, " ")
  440. if req.EdbName == "" {
  441. br.Msg = "指标名称不能为空"
  442. return
  443. }
  444. if req.Frequency == "" {
  445. br.Msg = "频率不能为空"
  446. return
  447. }
  448. if req.Unit == "" {
  449. br.Msg = "单位不能为空"
  450. return
  451. }
  452. if req.ClassifyId <= 0 {
  453. br.Msg = "请选择分类"
  454. return
  455. }
  456. fromEdbInfoId := req.FromEdbInfoId
  457. var formulaInt int
  458. switch req.Source {
  459. case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS, utils.DATA_SOURCE_CALCULATE_HBZ, utils.DATA_SOURCE_CALCULATE_HCZ, utils.DATA_SOURCE_CALCULATE_TIME_SHIFT, utils.DATA_SOURCE_CALCULATE_CJJX:
  460. if req.Formula == "" {
  461. br.Msg = "请填写N值"
  462. return
  463. }
  464. formulaInt, _ = strconv.Atoi(req.Formula)
  465. if formulaInt <= 0 {
  466. br.Msg = "N值输入错误,请重新输入"
  467. return
  468. }
  469. case utils.DATA_SOURCE_CALCULATE_ZJPJ:
  470. _, err = time.ParseInLocation(utils.FormatDate, req.Formula, time.Local)
  471. if err != nil {
  472. br.Msg = "拼接日期有误,请重新输入"
  473. return
  474. }
  475. case utils.DATA_SOURCE_CALCULATE_NHCC: //拟合残差指标
  476. if len(req.EdbInfoIdArr) != 2 {
  477. br.Msg = "选择的指标异常,请重新选择"
  478. return
  479. }
  480. fromEdbInfoId = req.EdbInfoIdArr[0].EdbInfoId
  481. timeList := strings.Split(req.Formula, ",")
  482. if len(timeList) != 2 {
  483. br.Msg = "选择时间有误,请重新输入"
  484. return
  485. }
  486. startDate, err := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
  487. if err != nil {
  488. br.Msg = "开始日期有误,请重新输入"
  489. return
  490. }
  491. endDate, err := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
  492. if err != nil {
  493. br.Msg = "结束日期有误,请重新输入"
  494. return
  495. }
  496. if utils.GetTimeSubDay(startDate, endDate) < 2 {
  497. br.Msg = "日期间隔不得少于两天"
  498. return
  499. }
  500. case utils.DATA_SOURCE_CALCULATE_ZSXY: // 指数修匀
  501. if req.Formula == "" {
  502. br.Msg = "请填写alpha值"
  503. return
  504. }
  505. alpha, e := strconv.ParseFloat(req.Formula, 64)
  506. if e != nil {
  507. br.Msg = "alpha值输入错误, 请重新输入"
  508. return
  509. }
  510. if alpha <= 0 || alpha >= 1 {
  511. br.Msg = "alpha值输入错误, 请重新输入"
  512. return
  513. }
  514. }
  515. notNeedFromEdbSourceList := []int{utils.DATA_SOURCE_CALCULATE_KSZS} // 不需要传入来源指标id的 指标类型
  516. if fromEdbInfoId <= 0 && !utils.InArrayByInt(notNeedFromEdbSourceList, req.Source) {
  517. br.Msg = "请选择指标"
  518. return
  519. }
  520. redisKey := fmt.Sprint("edb_info:calculate:batch:save:", req.Source, ":", req.EdbName)
  521. isExist := utils.Rc.IsExist(redisKey)
  522. if isExist {
  523. br.Msg = "指标正在处理,请勿重复提交"
  524. return
  525. } else {
  526. utils.Rc.SetNX(redisKey, 1, time.Second*300)
  527. defer func() {
  528. utils.Rc.Delete(redisKey)
  529. }()
  530. }
  531. req2 := &data_manage.EdbInfoCalculateBatchSaveReqByEdbLib{
  532. AdminId: sysUser.AdminId,
  533. AdminName: sysUser.RealName,
  534. EdbInfoId: req.EdbInfoId,
  535. EdbName: req.EdbName,
  536. Frequency: req.Frequency,
  537. Unit: req.Unit,
  538. ClassifyId: req.ClassifyId,
  539. Formula: req.Formula, //N数值移动平均计算、环比值、环差值
  540. FromEdbInfoId: req.FromEdbInfoId,
  541. CalculateFormula: req.CalculateFormula,
  542. Source: req.Source,
  543. MoveType: req.MoveType,
  544. MoveFrequency: req.MoveFrequency,
  545. EdbInfoIdArr: req.EdbInfoIdArr,
  546. Calendar: req.Calendar,
  547. Extra: req.Extra,
  548. EmptyType: req.EmptyType,
  549. }
  550. reqJson, err := json.Marshal(req2)
  551. if err != nil {
  552. br.Msg = "参数解析异常!"
  553. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  554. return
  555. }
  556. respItem, err := data.BatchSaveEdbCalculateData(string(reqJson), this.Lang)
  557. if err != nil {
  558. br.Msg = "新增失败"
  559. br.ErrMsg = "新增失败,Err:" + err.Error()
  560. return
  561. }
  562. if respItem.Ret != 200 {
  563. br.Msg = respItem.Msg
  564. br.ErrMsg = respItem.ErrMsg
  565. return
  566. }
  567. resp := respItem.Data
  568. data.AddOrEditEdbInfoToEs(resp.EdbInfoId)
  569. br.Ret = 200
  570. br.Success = true
  571. br.Msg = "保存成功"
  572. br.Data = resp
  573. br.IsAddLog = true
  574. }
  575. // @router /edb_info/calculate/batch/edit [post]
  576. func (this *ChartInfoController) CalculateBatchEdit() {
  577. br := new(models.BaseResponse).Init()
  578. defer func() {
  579. if br.ErrMsg == "" {
  580. br.IsSendEmail = false
  581. }
  582. this.Data["json"] = br
  583. this.ServeJSON()
  584. }()
  585. sysUser := this.SysUser
  586. if sysUser == nil {
  587. br.Msg = "请登录"
  588. br.ErrMsg = "请登录,SysUser Is Empty"
  589. br.Ret = 408
  590. return
  591. }
  592. var req data_manage.EdbInfoCalculateBatchEditReq
  593. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  594. if err != nil {
  595. br.Msg = "参数解析异常!"
  596. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  597. return
  598. }
  599. if req.EdbName == "" {
  600. br.Msg = "指标名称不能为空"
  601. return
  602. }
  603. if req.Frequency == "" {
  604. br.Msg = "频率不能为空"
  605. return
  606. }
  607. if req.Unit == "" {
  608. br.Msg = "单位不能为空"
  609. return
  610. }
  611. if req.ClassifyId <= 0 {
  612. br.Msg = "请选择分类"
  613. return
  614. }
  615. if req.EdbInfoId <= 0 {
  616. br.Msg = "请选择指标"
  617. return
  618. }
  619. var condition string
  620. var pars []interface{}
  621. condition += " AND edb_name=? "
  622. pars = append(pars, req.EdbName)
  623. condition += " AND edb_info_id<>? "
  624. pars = append(pars, req.EdbInfoId)
  625. count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
  626. if err != nil {
  627. br.Msg = "判断指标名称是否存在失败"
  628. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  629. return
  630. }
  631. if count > 0 {
  632. br.Msg = "指标名称已存在,请重新填写"
  633. br.ErrMsg = "指标名称已存在,请重新填写"
  634. br.IsSendEmail = false
  635. return
  636. }
  637. edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  638. if err != nil {
  639. if utils.IsErrNoRow(err) {
  640. br.Msg = "指标已被删除,请刷新页面"
  641. br.ErrMsg = "指标已被删除,请刷新页面:Err:" + err.Error()
  642. return
  643. }
  644. br.Msg = "获取指标信息失败"
  645. br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
  646. return
  647. }
  648. if req.Source <= 0 {
  649. req.Source = edbInfo.Source
  650. }
  651. var formulaInt int
  652. switch req.Source {
  653. case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS, utils.DATA_SOURCE_CALCULATE_HBZ, utils.DATA_SOURCE_CALCULATE_HCZ, utils.DATA_SOURCE_CALCULATE_TIME_SHIFT, utils.DATA_SOURCE_CALCULATE_CJJX:
  654. if req.Formula == "" {
  655. br.Msg = "请填写N值"
  656. return
  657. }
  658. formulaInt, _ = strconv.Atoi(req.Formula)
  659. if formulaInt <= 0 {
  660. br.Msg = "N值输入错误,请重新输入"
  661. return
  662. }
  663. case utils.DATA_SOURCE_CALCULATE_ZJPJ:
  664. _, err = time.ParseInLocation(utils.FormatDate, req.Formula, time.Local)
  665. if err != nil {
  666. br.Msg = "拼接日期有误,请重新输入"
  667. return
  668. }
  669. case utils.DATA_SOURCE_CALCULATE_NHCC: //拟合残差指标
  670. if len(req.EdbInfoIdArr) != 2 {
  671. br.Msg = "选择的指标异常,请重新选择"
  672. return
  673. }
  674. timeList := strings.Split(req.Formula, ",")
  675. if len(timeList) != 2 {
  676. br.Msg = "选择时间有误,请重新输入"
  677. return
  678. }
  679. startDate, err := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
  680. if err != nil {
  681. br.Msg = "开始日期有误,请重新输入"
  682. return
  683. }
  684. endDate, err := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
  685. if err != nil {
  686. br.Msg = "结束日期有误,请重新输入"
  687. return
  688. }
  689. if utils.GetTimeSubDay(startDate, endDate) < 2 {
  690. br.Msg = "日期间隔不得少于两天"
  691. return
  692. }
  693. case utils.DATA_SOURCE_CALCULATE_ZSXY: // 指数修匀
  694. if req.Formula == "" {
  695. br.Msg = "请填写alpha值"
  696. return
  697. }
  698. alpha, e := strconv.ParseFloat(req.Formula, 64)
  699. if e != nil {
  700. br.Msg = "alpha值输入错误, 请重新输入"
  701. return
  702. }
  703. if alpha <= 0 || alpha >= 1 {
  704. br.Msg = "alpha值输入错误, 请重新输入"
  705. return
  706. }
  707. }
  708. req2 := &data_manage.EdbInfoCalculateBatchEditReqByEdbLib{
  709. EdbInfoId: req.EdbInfoId,
  710. EdbName: req.EdbName,
  711. Frequency: req.Frequency,
  712. Unit: req.Unit,
  713. ClassifyId: req.ClassifyId,
  714. AdminId: this.SysUser.AdminId,
  715. AdminName: this.SysUser.RealName,
  716. Formula: req.Formula, //N数值移动平均计算、环比值、环差值
  717. FromEdbInfoId: req.FromEdbInfoId,
  718. Source: req.Source,
  719. MoveType: req.MoveType,
  720. MoveFrequency: req.MoveFrequency,
  721. EdbInfoIdArr: req.EdbInfoIdArr,
  722. Calendar: req.Calendar,
  723. Extra: req.Extra,
  724. EmptyType: req.EmptyType,
  725. }
  726. reqJson, err := json.Marshal(req2)
  727. if err != nil {
  728. br.Msg = "参数解析异常!"
  729. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  730. return
  731. }
  732. respItem, err := data.BatchEditEdbCalculateData(string(reqJson), this.Lang)
  733. if err != nil {
  734. br.Msg = "编辑失败"
  735. br.ErrMsg = "编辑失败,Err:" + err.Error()
  736. return
  737. }
  738. if respItem.Ret != 200 {
  739. br.Msg = respItem.Msg
  740. br.ErrMsg = respItem.ErrMsg
  741. return
  742. }
  743. resp := respItem.Data
  744. data.AddOrEditEdbInfoToEs(resp.EdbInfoId)
  745. go data.ModifyPredictEdbBaseInfoBySourceEdb(edbInfo, req.Frequency, req.Unit)
  746. br.Ret = 200
  747. br.Success = true
  748. br.Msg = "保存成功"
  749. br.Data = resp
  750. br.IsAddLog = true
  751. }
  752. // @router /edb_info/calculate/batch/reset [post]
  753. func (this *ChartInfoController) CalculateBatchReset() {
  754. br := new(models.BaseResponse).Init()
  755. defer func() {
  756. this.Data["json"] = br
  757. this.ServeJSON()
  758. }()
  759. sysUser := this.SysUser
  760. if sysUser == nil {
  761. br.Msg = "请登录"
  762. br.ErrMsg = "请登录,SysUser Is Empty"
  763. br.Ret = 408
  764. return
  765. }
  766. var req data_manage.EdbInfoCalculateBatchSaveReq
  767. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  768. if err != nil {
  769. br.Msg = "参数解析异常!"
  770. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  771. return
  772. }
  773. if req.EdbInfoId <= 0 {
  774. br.Msg = "参数错误"
  775. br.ErrMsg = "参数错误:EdbInfoId:" + strconv.Itoa(req.EdbInfoId)
  776. return
  777. }
  778. edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  779. if err != nil {
  780. if utils.IsErrNoRow(err) {
  781. br.Msg = "指标已被删除,请刷新页面"
  782. br.ErrMsg = "指标已删除,请刷新页面:Err:" + err.Error()
  783. return
  784. }
  785. br.Msg = "重新计算失败"
  786. br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
  787. return
  788. }
  789. uniqueCode := edbInfo.UniqueCode
  790. sourName := edbInfo.SourceName
  791. edbInfoId := edbInfo.EdbInfoId
  792. err, isAsync := data.EdbInfoRefreshAllFromBaseV2(edbInfoId, true, false)
  793. if err != nil {
  794. fmt.Println(edbInfoId, "RefreshEdbCalculateData err", time.Now())
  795. br.Msg = "重新计算失败"
  796. br.ErrMsg = "生成" + sourName + "失败,RefreshEdbCalculateData Err:" + err.Error()
  797. return
  798. }
  799. resp := new(data_manage.AddEdbInfoResp)
  800. resp.EdbInfoId = edbInfoId
  801. resp.UniqueCode = uniqueCode
  802. br.Ret = 200
  803. br.Success = true
  804. br.Msg = "重新计算成功"
  805. if isAsync {
  806. br.Msg = "该指标关联指标较多,请10分钟后刷新页面查看最新数据"
  807. }
  808. br.Data = resp
  809. br.IsAddLog = true
  810. }
  811. // @router /edb_info/python/exec [post]
  812. func (this *ChartInfoController) ExecPythonCode() {
  813. br := new(models.BaseResponse).Init()
  814. defer func() {
  815. this.Data["json"] = br
  816. this.ServeJSON()
  817. }()
  818. sysUser := this.SysUser
  819. if sysUser == nil {
  820. br.Msg = "请登录"
  821. br.ErrMsg = "请登录,SysUser Is Empty"
  822. br.Ret = 408
  823. return
  824. }
  825. var req data_manage.ExecPythonEdbReq
  826. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  827. if err != nil {
  828. br.Msg = "参数解析异常!"
  829. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  830. return
  831. }
  832. if req.PythonCode == "" {
  833. br.Msg = "python代码不能为空"
  834. return
  835. }
  836. resp, err := data.ExecPythonCode(req.PythonCode)
  837. if err != nil {
  838. br.Msg = "python代码执行失败,请检查代码"
  839. br.ErrMsg = "python代码执行失败,Err:" + err.Error()
  840. return
  841. }
  842. if resp.Ret != 200 {
  843. br.Msg = "python代码执行失败,请检查代码:" + resp.Msg
  844. br.ErrMsg = "python代码执行失败,Err:" + resp.ErrMsg
  845. return
  846. }
  847. br.Ret = 200
  848. br.Success = true
  849. br.Msg = "运行成功"
  850. br.Data = resp.Data
  851. }
  852. // @router /edb_info/python/add [post]
  853. func (this *ChartInfoController) AddPythonEdb() {
  854. br := new(models.BaseResponse).Init()
  855. defer func() {
  856. this.Data["json"] = br
  857. this.ServeJSON()
  858. }()
  859. sysUser := this.SysUser
  860. if sysUser == nil {
  861. br.Msg = "请登录"
  862. br.ErrMsg = "请登录,SysUser Is Empty"
  863. br.Ret = 408
  864. return
  865. }
  866. var req data_manage.PostAddPythonEdbReq
  867. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  868. if err != nil {
  869. br.Msg = "参数解析异常!"
  870. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  871. return
  872. }
  873. req.AdminId = sysUser.AdminId
  874. req.AdminName = sysUser.RealName
  875. reqJson, err := json.Marshal(req)
  876. if err != nil {
  877. br.Msg = "参数解析异常!"
  878. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  879. return
  880. }
  881. {
  882. var condition string
  883. var pars []interface{}
  884. condition += " AND edb_name=? "
  885. pars = append(pars, req.EdbName)
  886. condition += " AND edb_info_id<>? "
  887. pars = append(pars, req.EdbInfoId)
  888. count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
  889. if err != nil {
  890. br.Msg = "判断指标名称是否存在失败"
  891. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  892. return
  893. }
  894. if count > 0 {
  895. br.Msg = "指标名称已存在,请重新填写"
  896. br.ErrMsg = "指标名称已存在,请重新填写"
  897. br.IsSendEmail = false
  898. return
  899. }
  900. }
  901. respItem, err := data.AddPythonEdbData(string(reqJson), this.Lang)
  902. if err != nil {
  903. br.Msg = "添加失败"
  904. br.ErrMsg = "获取失败,Err:" + err.Error()
  905. return
  906. }
  907. if respItem.Ret != 200 {
  908. br.Msg = "添加失败"
  909. br.ErrMsg = respItem.ErrMsg + ";EdbName:" + req.EdbName
  910. return
  911. }
  912. respData := respItem.Data.(map[string]interface{})
  913. tmpEdbInfoId, ok := respData["EdbInfoId"]
  914. if !ok {
  915. br.Msg = `数据转换异常`
  916. br.ErrMsg = fmt.Sprint("respData.EdbInfoId 数据转换异常;respData:", respData)
  917. return
  918. }
  919. edbInfoId := int64(tmpEdbInfoId.(float64))
  920. uniqueCode, ok := respData["UniqueCode"]
  921. if !ok {
  922. br.Msg = `数据转换异常`
  923. br.ErrMsg = fmt.Sprint("respData.UniqueCode 数据转换异常;respData:", respData)
  924. return
  925. }
  926. data.AddOrEditEdbInfoToEs(int(edbInfoId))
  927. resp := data_manage.AddEdbInfoResp{
  928. EdbInfoId: int(edbInfoId),
  929. UniqueCode: uniqueCode.(string),
  930. }
  931. br.Ret = 200
  932. br.Success = true
  933. br.Msg = "保存成功"
  934. br.Data = resp
  935. br.IsAddLog = true
  936. }
  937. // @router /edb_info/python/edit [post]
  938. func (this *ChartInfoController) EditPythonEdb() {
  939. br := new(models.BaseResponse).Init()
  940. defer func() {
  941. this.Data["json"] = br
  942. this.ServeJSON()
  943. }()
  944. sysUser := this.SysUser
  945. if sysUser == nil {
  946. br.Msg = "请登录"
  947. br.ErrMsg = "请登录,SysUser Is Empty"
  948. br.Ret = 408
  949. return
  950. }
  951. var req data_manage.PostAddPythonEdbReq
  952. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  953. if err != nil {
  954. br.Msg = "参数解析异常!"
  955. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  956. return
  957. }
  958. req.AdminId = sysUser.AdminId
  959. req.AdminName = sysUser.RealName
  960. reqJson, err := json.Marshal(req)
  961. if err != nil {
  962. br.Msg = "参数解析异常!"
  963. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  964. return
  965. }
  966. {
  967. var condition string
  968. var pars []interface{}
  969. condition += " AND edb_name=? "
  970. pars = append(pars, req.EdbName)
  971. condition += " AND edb_info_id<>? "
  972. pars = append(pars, req.EdbInfoId)
  973. count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
  974. if err != nil {
  975. br.Msg = "判断指标名称是否存在失败"
  976. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  977. return
  978. }
  979. if count > 0 {
  980. br.Msg = "指标名称已存在,请重新填写"
  981. br.ErrMsg = "指标名称已存在,请重新填写"
  982. br.IsSendEmail = false
  983. return
  984. }
  985. }
  986. edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  987. if err != nil {
  988. if utils.IsErrNoRow(err) {
  989. br.Msg = "指标已被删除,请刷新页面"
  990. br.ErrMsg = "指标已被删除,请刷新页面:Err:" + err.Error()
  991. return
  992. }
  993. br.Msg = "获取指标信息失败"
  994. br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
  995. return
  996. }
  997. respItem, err := data.EditPythonEdbData(string(reqJson), this.Lang)
  998. if err != nil {
  999. br.Msg = "保存失败"
  1000. br.ErrMsg = "保存失败,Err:" + err.Error()
  1001. return
  1002. }
  1003. if respItem.Ret != 200 {
  1004. br.Msg = "保存失败"
  1005. br.ErrMsg = respItem.ErrMsg + ";EdbName:" + req.EdbName
  1006. return
  1007. }
  1008. data.AddOrEditEdbInfoToEs(req.EdbInfoId)
  1009. resp := new(data_manage.AddEdbInfoResp)
  1010. resp.EdbInfoId = req.EdbInfoId
  1011. resp.UniqueCode = edbInfo.UniqueCode
  1012. br.Ret = 200
  1013. br.Success = true
  1014. br.Data = resp
  1015. br.Msg = "保存成功"
  1016. br.IsAddLog = true
  1017. }
  1018. // @router /edb_info/python/detail [get]
  1019. func (this *ChartInfoController) GetPythonEdbCode() {
  1020. br := new(models.BaseResponse).Init()
  1021. defer func() {
  1022. this.Data["json"] = br
  1023. this.ServeJSON()
  1024. }()
  1025. sysUser := this.SysUser
  1026. if sysUser == nil {
  1027. br.Msg = "请登录"
  1028. br.ErrMsg = "请登录,SysUser Is Empty"
  1029. br.Ret = 408
  1030. return
  1031. }
  1032. edbInfoId, _ := this.GetInt("EdbInfoId")
  1033. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  1034. if err != nil {
  1035. br.Msg = "获取失败"
  1036. br.ErrMsg = "获取失败,Err:" + err.Error()
  1037. return
  1038. }
  1039. if edbInfo.Source != utils.DATA_SOURCE_PYTHON {
  1040. br.Msg = "该指标不是Python运算指标"
  1041. br.ErrMsg = "该指标不是Python运算指标"
  1042. return
  1043. }
  1044. pythonCode, err := data_manage.GetEdbPythonCodeById(edbInfoId)
  1045. if err != nil {
  1046. br.Msg = "获取失败"
  1047. br.ErrMsg = "获取失败,Err:" + err.Error()
  1048. return
  1049. }
  1050. resp := data_manage.PythonEdbDetailResp{
  1051. EdbInfoDetail: edbInfo,
  1052. PythonCode: pythonCode.PythonCode,
  1053. }
  1054. br.Ret = 200
  1055. br.Success = true
  1056. br.Msg = "保存成功"
  1057. br.Data = resp
  1058. }
  1059. // @router /edb_data/table/detail [get]
  1060. func (this *EdbInfoController) QueryEdbDataTable() {
  1061. br := new(models.BaseResponse).Init()
  1062. defer func() {
  1063. this.Data["json"] = br
  1064. this.ServeJSON()
  1065. }()
  1066. edbInfoId, _ := this.GetInt("EdbInfoId")
  1067. if edbInfoId <= 0 {
  1068. br.Msg = "请选择指标"
  1069. br.ErrMsg = "请选择指标"
  1070. return
  1071. }
  1072. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  1073. if err != nil {
  1074. br.Msg = "获取指标信息失败"
  1075. br.ErrMsg = "获取指标信息失败,err:" + err.Error()
  1076. return
  1077. }
  1078. columnList := []map[string]string{
  1079. {
  1080. "edb_code": "指标code",
  1081. },
  1082. {
  1083. "data_time": "指标日期",
  1084. },
  1085. {
  1086. "value": "指标对应的值",
  1087. },
  1088. }
  1089. tableName := data_manage.GetEdbDataTableName(edbInfo.Source, edbInfo.SubSource)
  1090. var templateStr string
  1091. if edbInfo.Source == utils.DATA_SOURCE_BUSINESS && utils.UseMongo {
  1092. templateStr = fmt.Sprintf("# 查询条件\nquery = {\"edb_code\": \"%s\"}\n# 排序\nsort = [(\"data_time\", -1)] # -1 表示降序排列,对应 SQL 的 DESC\nprojection = {\"data_time\": 1, \"value\": 1, \"_id\": 0} # 只选择data_time和value字段,忽略_id字段\n# 使用 find() 方法获取数据,然后使用 aggregate() 转换为列表\nraw_data = list(collection.find(query, projection).sort(sort))\n# 将结果转换为 DataFrame\nraw = pd.DataFrame(raw_data)\n# 转换data_time字段为本地时区时间\nraw['data_time'] = raw['data_time'].apply(lambda x: x.replace(tzinfo=utc_tz)).dt.tz_convert(local_tz).dt.strftime('%s')", edbInfo.EdbCode, "%Y-%m-%d")
  1093. } else {
  1094. templateStr = fmt.Sprintf("sql1 = f\"\"\"SELECT data_time,`value` FROM %s WHERE edb_code = '%s' ORDER BY data_time DESC;\"\"\"\nraw = pandas_fetch_all(sql1, db)", tableName, edbInfo.EdbCode)
  1095. }
  1096. info := data_manage.TableInfoResp{
  1097. ColumnList: columnList,
  1098. TableName: tableName,
  1099. TemplateStr: url.PathEscape(templateStr),
  1100. }
  1101. br.Ret = 200
  1102. br.Success = true
  1103. br.Msg = "获取成功"
  1104. br.Data = info
  1105. }
  1106. // @router /edb_info/adjust/save [post]
  1107. func (this *ChartInfoController) SaveAdjustEdbInfo() {
  1108. br := new(models.BaseResponse).Init()
  1109. defer func() {
  1110. this.Data["json"] = br
  1111. this.ServeJSON()
  1112. }()
  1113. sysUser := this.SysUser
  1114. if sysUser == nil {
  1115. br.Msg = "请登录"
  1116. br.ErrMsg = "请登录,SysUser Is Empty"
  1117. br.Ret = 408
  1118. return
  1119. }
  1120. var req data_manage.SaveAdjustEdbInfoReq
  1121. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1122. if err != nil {
  1123. br.Msg = "参数解析异常!"
  1124. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1125. return
  1126. }
  1127. req.AdminId = sysUser.AdminId
  1128. req.AdminName = sysUser.RealName
  1129. reqJson, err := json.Marshal(req)
  1130. if err != nil {
  1131. br.Msg = "参数解析异常!"
  1132. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1133. return
  1134. }
  1135. {
  1136. var condition string
  1137. var pars []interface{}
  1138. condition += " AND edb_name=? "
  1139. pars = append(pars, req.EdbName)
  1140. condition += " AND edb_info_id<>? "
  1141. pars = append(pars, req.EdbInfoId)
  1142. count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
  1143. if err != nil {
  1144. br.Msg = "判断指标名称是否存在失败"
  1145. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  1146. return
  1147. }
  1148. if count > 0 {
  1149. br.Msg = "指标名称已存在,请重新填写"
  1150. br.ErrMsg = "指标名称已存在,请重新填写"
  1151. br.IsSendEmail = false
  1152. return
  1153. }
  1154. }
  1155. respItem, err := data.SaveAdjustEdbInfo(string(reqJson), this.Lang)
  1156. if err != nil {
  1157. br.Msg = "添加失败"
  1158. br.ErrMsg = "获取失败,Err:" + err.Error()
  1159. return
  1160. }
  1161. if respItem.Ret != 200 {
  1162. br.Msg = "添加失败"
  1163. br.ErrMsg = respItem.ErrMsg + ";EdbName:" + req.EdbName
  1164. return
  1165. }
  1166. respData := respItem.Data.(map[string]interface{})
  1167. tmpEdbInfoId, ok := respData["EdbInfoId"]
  1168. if !ok {
  1169. br.Msg = `数据转换异常`
  1170. br.ErrMsg = fmt.Sprint("respData.EdbInfoId 数据转换异常;respData:", respData)
  1171. return
  1172. }
  1173. edbInfoId := int64(tmpEdbInfoId.(float64))
  1174. uniqueCode, ok := respData["UniqueCode"]
  1175. if !ok {
  1176. br.Msg = `数据转换异常`
  1177. br.ErrMsg = fmt.Sprint("respData.UniqueCode 数据转换异常;respData:", respData)
  1178. return
  1179. }
  1180. data.AddOrEditEdbInfoToEs(int(edbInfoId))
  1181. resp := data_manage.AddEdbInfoResp{
  1182. EdbInfoId: int(edbInfoId),
  1183. UniqueCode: uniqueCode.(string),
  1184. }
  1185. br.Ret = 200
  1186. br.Success = true
  1187. br.Msg = "保存成功"
  1188. br.Data = resp
  1189. br.IsAddLog = true
  1190. }
  1191. // @router /edb_info/calculate/batch/save/batch [post]
  1192. func (this *ChartInfoController) BatchCalculateBatchSave() {
  1193. br := new(models.BaseResponse).Init()
  1194. defer func() {
  1195. if br.ErrMsg == "" {
  1196. br.IsSendEmail = false
  1197. }
  1198. this.Data["json"] = br
  1199. this.ServeJSON()
  1200. }()
  1201. sysUser := this.SysUser
  1202. if sysUser == nil {
  1203. br.Msg = "请登录"
  1204. br.ErrMsg = "请登录,SysUser Is Empty"
  1205. br.Ret = 408
  1206. return
  1207. }
  1208. var req data_manage.BatchEdbInfoCalculateBatchSaveReq
  1209. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1210. if err != nil {
  1211. br.Msg = "参数解析异常!"
  1212. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1213. return
  1214. }
  1215. resp := data_manage.BatchEdbInfoCalculateBatchSaveResp{
  1216. Fail: make([]data_manage.BatchEdbInfoCalculateBatchSaveFailResp, 0),
  1217. Success: make([]data_manage.BatchEdbInfoCalculateBatchSaveSuccessResp, 0),
  1218. }
  1219. var formulaInt int
  1220. switch req.Source {
  1221. case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS, utils.DATA_SOURCE_CALCULATE_HBZ, utils.DATA_SOURCE_CALCULATE_HCZ, utils.DATA_SOURCE_CALCULATE_TIME_SHIFT, utils.DATA_SOURCE_CALCULATE_CJJX:
  1222. if req.Formula == "" {
  1223. br.Msg = "请输入N值"
  1224. return
  1225. }
  1226. formulaInt, _ = strconv.Atoi(req.Formula)
  1227. if formulaInt <= 0 {
  1228. br.Msg = "N值输入错误,请重新输入"
  1229. return
  1230. }
  1231. case utils.DATA_SOURCE_CALCULATE_ZJPJ:
  1232. _, err = time.ParseInLocation(utils.FormatDate, req.Formula, time.Local)
  1233. if err != nil {
  1234. br.Msg = "拼接日期有误,请重新输入"
  1235. return
  1236. }
  1237. case utils.DATA_SOURCE_CALCULATE_NHCC: //拟合残差指标
  1238. if len(req.EdbInfoIdArr) != 2 {
  1239. br.Msg = "选择的指标异常,请重新选择"
  1240. return
  1241. }
  1242. fromEdbInfoId := req.EdbInfoIdArr[0].EdbInfoId
  1243. if fromEdbInfoId <= 0 {
  1244. br.Msg = "请选择指标"
  1245. return
  1246. }
  1247. timeList := strings.Split(req.Formula, ",")
  1248. if len(timeList) != 2 {
  1249. br.Msg = "选择的指标异常,请重新选择"
  1250. return
  1251. }
  1252. startDate, err := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
  1253. if err != nil {
  1254. br.Msg = "开始日期有误,请重新输入"
  1255. return
  1256. }
  1257. endDate, err := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
  1258. if err != nil {
  1259. br.Msg = "结束日期有误,请重新输入"
  1260. return
  1261. }
  1262. if utils.GetTimeSubDay(startDate, endDate) < 2 {
  1263. br.Msg = "日期间隔不得少于两天"
  1264. return
  1265. }
  1266. case utils.DATA_SOURCE_CALCULATE_ZSXY: // 指数修匀
  1267. if req.Formula == "" {
  1268. br.Msg = "请填写alpha值"
  1269. return
  1270. }
  1271. alpha, e := strconv.ParseFloat(req.Formula, 64)
  1272. if e != nil {
  1273. br.Msg = "alpha值输入错误, 请重新输入"
  1274. return
  1275. }
  1276. if alpha <= 0 || alpha >= 1 {
  1277. br.Msg = "alpha值输入错误, 请重新输入"
  1278. return
  1279. }
  1280. }
  1281. if len(req.EdbList) > 100 {
  1282. br.Msg = "最多只能选择100个指标"
  1283. return
  1284. }
  1285. reqEdbList := make([]*data_manage.CalculateEdbInfoItem, 0)
  1286. for _, v := range req.EdbList {
  1287. v.EdbName = strings.Trim(v.EdbName, " ")
  1288. if v.EdbName == "" {
  1289. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1290. CalculateId: v.CalculateId,
  1291. Msg: "指标名称不能为空",
  1292. })
  1293. continue
  1294. }
  1295. if v.Frequency == "" {
  1296. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1297. CalculateId: v.CalculateId,
  1298. Msg: "频率不能为空",
  1299. })
  1300. continue
  1301. }
  1302. if v.Unit == "" {
  1303. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1304. CalculateId: v.CalculateId,
  1305. Msg: "单位不能为空",
  1306. })
  1307. continue
  1308. }
  1309. if v.ClassifyId <= 0 {
  1310. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1311. CalculateId: v.CalculateId,
  1312. Msg: "请选择分类",
  1313. })
  1314. continue
  1315. }
  1316. redisKey := fmt.Sprint("edb_info:calculate:batch:save:", req.Source, ":", v.EdbName)
  1317. isExist := utils.Rc.IsExist(redisKey)
  1318. if isExist {
  1319. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1320. CalculateId: v.CalculateId,
  1321. Msg: "指标正在处理,请勿重复提交",
  1322. })
  1323. continue
  1324. } else {
  1325. }
  1326. reqEdbList = append(reqEdbList, v)
  1327. }
  1328. if len(reqEdbList) <= 0 {
  1329. br.Msg = "新增失败!"
  1330. if len(resp.Fail) > 0 {
  1331. br.ErrMsg = resp.Fail[0].Msg
  1332. } else {
  1333. br.Msg = "请选择指标"
  1334. }
  1335. return
  1336. }
  1337. req.AdminId = sysUser.AdminId
  1338. req.AdminName = sysUser.RealName
  1339. req.EdbList = reqEdbList
  1340. reqJson, err := json.Marshal(req)
  1341. if err != nil {
  1342. br.Msg = "参数解析异常!"
  1343. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1344. return
  1345. }
  1346. respItem, err := data.BatchSaveEdbCalculateMultiData(string(reqJson), this.Lang)
  1347. if err != nil {
  1348. br.Msg = "新增失败!"
  1349. br.ErrMsg = "新增失败,Err:" + err.Error()
  1350. return
  1351. }
  1352. if respItem.Ret != 200 {
  1353. br.Msg = respItem.Msg
  1354. br.ErrMsg = respItem.ErrMsg
  1355. return
  1356. }
  1357. br.Ret = 200
  1358. br.Success = true
  1359. br.Msg = "批量保存成功"
  1360. br.Data = respItem.Data
  1361. br.IsAddLog = true
  1362. }
  1363. // @router /edb_info/calculate/batch/edit/batch [post]
  1364. func (this *ChartInfoController) BatchCalculateBatchEdit() {
  1365. br := new(models.BaseResponse).Init()
  1366. defer func() {
  1367. if br.ErrMsg == "" {
  1368. br.IsSendEmail = false
  1369. }
  1370. this.Data["json"] = br
  1371. this.ServeJSON()
  1372. }()
  1373. sysUser := this.SysUser
  1374. if sysUser == nil {
  1375. br.Msg = "请登录"
  1376. br.ErrMsg = "请登录,SysUser Is Empty"
  1377. br.Ret = 408
  1378. return
  1379. }
  1380. var req data_manage.BatchEdbInfoCalculateBatchSaveReq
  1381. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1382. if err != nil {
  1383. br.Msg = "参数解析异常!"
  1384. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1385. return
  1386. }
  1387. resp := data_manage.BatchEdbInfoCalculateBatchSaveResp{
  1388. Fail: make([]data_manage.BatchEdbInfoCalculateBatchSaveFailResp, 0),
  1389. Success: make([]data_manage.BatchEdbInfoCalculateBatchSaveSuccessResp, 0),
  1390. }
  1391. var formulaInt int
  1392. switch req.Source {
  1393. case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS, utils.DATA_SOURCE_CALCULATE_HBZ, utils.DATA_SOURCE_CALCULATE_HCZ, utils.DATA_SOURCE_CALCULATE_TIME_SHIFT, utils.DATA_SOURCE_CALCULATE_CJJX:
  1394. if req.Formula == "" {
  1395. br.Msg = "请输入N值"
  1396. return
  1397. }
  1398. formulaInt, _ = strconv.Atoi(req.Formula)
  1399. if formulaInt <= 0 {
  1400. br.Msg = "N值输入错误,请重新输入"
  1401. return
  1402. }
  1403. case utils.DATA_SOURCE_CALCULATE_ZJPJ:
  1404. _, err = time.ParseInLocation(utils.FormatDate, req.Formula, time.Local)
  1405. if err != nil {
  1406. br.Msg = "拼接日期有误,请重新输入"
  1407. return
  1408. }
  1409. case utils.DATA_SOURCE_CALCULATE_NHCC: //拟合残差指标
  1410. if len(req.EdbInfoIdArr) != 2 {
  1411. br.Msg = "选择的指标异常,请重新选择"
  1412. return
  1413. }
  1414. fromEdbInfoId := req.EdbInfoIdArr[0].EdbInfoId
  1415. if fromEdbInfoId <= 0 {
  1416. br.Msg = "请选择指标"
  1417. return
  1418. }
  1419. timeList := strings.Split(req.Formula, ",")
  1420. if len(timeList) != 2 {
  1421. br.Msg = "选择的指标异常,请重新选择"
  1422. return
  1423. }
  1424. startDate, err := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
  1425. if err != nil {
  1426. br.Msg = "开始日期有误,请重新输入"
  1427. return
  1428. }
  1429. endDate, err := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
  1430. if err != nil {
  1431. br.Msg = "结束日期有误,请重新输入"
  1432. return
  1433. }
  1434. if utils.GetTimeSubDay(startDate, endDate) < 2 {
  1435. br.Msg = "日期间隔不得少于两天"
  1436. return
  1437. }
  1438. case utils.DATA_SOURCE_CALCULATE_ZSXY: // 指数修匀
  1439. if req.Formula == "" {
  1440. br.Msg = "请填写alpha值"
  1441. return
  1442. }
  1443. alpha, e := strconv.ParseFloat(req.Formula, 64)
  1444. if e != nil {
  1445. br.Msg = "alpha值输入错误, 请重新输入"
  1446. return
  1447. }
  1448. if alpha <= 0 || alpha >= 1 {
  1449. br.Msg = "alpha值输入错误, 请重新输入"
  1450. return
  1451. }
  1452. }
  1453. if len(req.EdbList) > 50 {
  1454. br.Msg = "最多只能选择50个指标"
  1455. return
  1456. }
  1457. reqEdbList := make([]*data_manage.CalculateEdbInfoItem, 0)
  1458. for _, v := range req.EdbList {
  1459. if v.EdbInfoId <= 0 {
  1460. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1461. CalculateId: v.CalculateId,
  1462. Msg: "请选择要编辑的指标",
  1463. })
  1464. continue
  1465. }
  1466. v.EdbName = strings.Trim(v.EdbName, " ")
  1467. if v.EdbName == "" {
  1468. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1469. CalculateId: v.CalculateId,
  1470. Msg: "指标名称不能为空",
  1471. })
  1472. continue
  1473. }
  1474. if v.Frequency == "" {
  1475. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1476. CalculateId: v.CalculateId,
  1477. Msg: "频率不能为空",
  1478. })
  1479. continue
  1480. }
  1481. if v.Unit == "" {
  1482. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1483. CalculateId: v.CalculateId,
  1484. Msg: "单位不能为空",
  1485. })
  1486. continue
  1487. }
  1488. if v.ClassifyId <= 0 {
  1489. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1490. CalculateId: v.CalculateId,
  1491. Msg: "请选择分类",
  1492. })
  1493. continue
  1494. }
  1495. reqEdbList = append(reqEdbList, v)
  1496. }
  1497. if len(reqEdbList) <= 0 {
  1498. br.Msg = "编辑失败!"
  1499. if len(resp.Fail) > 0 {
  1500. br.ErrMsg = resp.Fail[0].Msg
  1501. } else {
  1502. br.Msg = "请选择指标"
  1503. }
  1504. return
  1505. }
  1506. req.AdminId = sysUser.AdminId
  1507. req.AdminName = sysUser.RealName
  1508. req.EdbList = reqEdbList
  1509. reqJson, err := json.Marshal(req)
  1510. if err != nil {
  1511. br.Msg = "参数解析异常!"
  1512. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1513. return
  1514. }
  1515. respItem, err := data.BatchEditEdbCalculateMultiData(string(reqJson), this.Lang)
  1516. if err != nil {
  1517. br.Msg = "编辑失败!"
  1518. br.ErrMsg = "编辑失败,Err:" + err.Error()
  1519. return
  1520. }
  1521. if respItem.Ret != 200 {
  1522. br.Msg = respItem.Msg
  1523. br.ErrMsg = respItem.ErrMsg
  1524. return
  1525. }
  1526. br.Ret = 200
  1527. br.Success = true
  1528. br.Msg = "批量编辑成功"
  1529. br.Data = respItem.Data
  1530. br.IsAddLog = true
  1531. }
  1532. // @router /edb_info/calculate/mapping [get]
  1533. func (this *ChartInfoController) CalculateMapping() {
  1534. br := new(models.BaseResponse).Init()
  1535. defer func() {
  1536. this.Data["json"] = br
  1537. this.ServeJSON()
  1538. }()
  1539. sysUser := this.SysUser
  1540. if sysUser == nil {
  1541. br.Msg = "请登录"
  1542. br.ErrMsg = "请登录,SysUser Is Empty"
  1543. br.Ret = 408
  1544. return
  1545. }
  1546. edbInfoId, _ := this.GetInt("EdbInfoId")
  1547. if edbInfoId <= 0 {
  1548. br.Msg = "参数有误"
  1549. return
  1550. }
  1551. item, e := data_manage.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1552. if e != nil {
  1553. br.Msg = "获取失败"
  1554. br.ErrMsg = "获取失败,Err:" + e.Error()
  1555. return
  1556. }
  1557. if this.Lang == utils.EnLangVersion {
  1558. fromEdbInfo, e := data_manage.GetEdbInfoById(item.FromEdbInfoId)
  1559. if e != nil {
  1560. br.Msg = "获取失败"
  1561. br.ErrMsg = "获取失败,Err:" + e.Error()
  1562. return
  1563. }
  1564. item.FromEdbName = fromEdbInfo.EdbNameEn
  1565. }
  1566. br.Ret = 200
  1567. br.Success = true
  1568. br.Msg = "保存成功"
  1569. br.Data = item
  1570. }
  1571. // @router /edb_info/calculate/compute_correlation [post]
  1572. func (this *ChartInfoController) CalculateComputeCorrelation() {
  1573. br := new(models.BaseResponse).Init()
  1574. defer func() {
  1575. this.Data["json"] = br
  1576. this.ServeJSON()
  1577. }()
  1578. sysUser := this.SysUser
  1579. if sysUser == nil {
  1580. br.Msg = "请登录"
  1581. br.ErrMsg = "请登录,SysUser Is Empty"
  1582. br.Ret = 408
  1583. return
  1584. }
  1585. var req data_manage.EdbInfoCalculateBatchSaveReq
  1586. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1587. if err != nil {
  1588. br.Msg = "参数解析异常!"
  1589. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1590. return
  1591. }
  1592. if len(req.EdbInfoIdArr) != 2 {
  1593. br.Msg = "选择的指标异常,请重新选择"
  1594. return
  1595. }
  1596. fromEdbInfoId := req.EdbInfoIdArr[0].EdbInfoId
  1597. timeList := strings.Split(req.Formula, ",")
  1598. if len(timeList) != 2 {
  1599. br.Msg = "选择时间有误,请重新输入"
  1600. return
  1601. }
  1602. startDate, err := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
  1603. if err != nil {
  1604. br.Msg = "开始日期有误,请重新输入"
  1605. return
  1606. }
  1607. endDate, err := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
  1608. if err != nil {
  1609. br.Msg = "结束日期有误,请重新输入"
  1610. return
  1611. }
  1612. if utils.GetTimeSubDay(startDate, endDate) < 2 {
  1613. br.Msg = "日期间隔不得少于两天"
  1614. return
  1615. }
  1616. if fromEdbInfoId <= 0 {
  1617. br.Msg = "请选择指标"
  1618. return
  1619. }
  1620. req2 := &data_manage.EdbInfoCalculateBatchSaveReqByEdbLib{
  1621. AdminId: sysUser.AdminId,
  1622. AdminName: sysUser.RealName,
  1623. EdbInfoId: req.EdbInfoId,
  1624. EdbName: req.EdbName,
  1625. Frequency: req.Frequency,
  1626. Unit: req.Unit,
  1627. ClassifyId: req.ClassifyId,
  1628. Formula: req.Formula, //N数值移动平均计算、环比值、环差值
  1629. FromEdbInfoId: req.FromEdbInfoId,
  1630. CalculateFormula: req.CalculateFormula,
  1631. Source: req.Source,
  1632. MoveType: req.MoveType,
  1633. MoveFrequency: req.MoveFrequency,
  1634. EdbInfoIdArr: req.EdbInfoIdArr,
  1635. Calendar: req.Calendar,
  1636. }
  1637. val, err, errMsg := data.CallCalculateComputeCorrelation(req2, this.Lang)
  1638. if err != nil {
  1639. br.Msg = "计算失败"
  1640. if errMsg != `` {
  1641. br.Msg = errMsg
  1642. }
  1643. br.ErrMsg = err.Error()
  1644. return
  1645. }
  1646. br.Data = val
  1647. br.Ret = 200
  1648. br.Success = true
  1649. br.Msg = "计算成功"
  1650. br.IsAddLog = true
  1651. }
  1652. // @router /edb_info/calculate/multi/choice [get]
  1653. func (this *ChartInfoController) CalculateMultiChoice() {
  1654. br := new(models.BaseResponse).Init()
  1655. defer func() {
  1656. this.Data["json"] = br
  1657. this.ServeJSON()
  1658. }()
  1659. frequency := this.GetString("Frequency")
  1660. keyword := this.GetString("Keyword")
  1661. sysUserIds := this.GetString("SysUserIds")
  1662. classifyIds := this.GetString("ClassifyIds")
  1663. edbInfoIds := this.GetString("EdbInfoIds")
  1664. selectAll, _ := this.GetBool("SelectAll")
  1665. notFrequency := this.GetString("NotFrequency")
  1666. edbInfoType, _ := this.GetInt("EdbInfoType", 0)
  1667. var edbIdArr []int
  1668. var filterEdbIds []int
  1669. if edbInfoIds != "" {
  1670. arr := strings.Split(edbInfoIds, ",")
  1671. for _, v := range arr {
  1672. t, e := strconv.Atoi(v)
  1673. if e != nil {
  1674. br.Msg = "参数有误"
  1675. br.ErrMsg = fmt.Sprintf("指标ID有误, %s", v)
  1676. return
  1677. }
  1678. filterEdbIds = append(filterEdbIds, t)
  1679. }
  1680. }
  1681. if selectAll {
  1682. condition := ` AND edb_info_type = ? `
  1683. pars := make([]interface{}, 0)
  1684. pars = append(pars, edbInfoType)
  1685. if classifyIds != "" {
  1686. classifyIdsArr := strings.Split(classifyIds, ",")
  1687. condition += ` AND classify_id IN (` + utils.GetOrmInReplace(len(classifyIdsArr)) + `) `
  1688. pars = append(pars, classifyIdsArr)
  1689. }
  1690. if frequency != "" {
  1691. frequencyArr := strings.Split(frequency, ",")
  1692. condition += ` AND frequency IN (` + utils.GetOrmInReplace(len(frequencyArr)) + `) `
  1693. pars = append(pars, frequencyArr)
  1694. }
  1695. if notFrequency != "" {
  1696. notFrequencyArr := strings.Split(notFrequency, ",")
  1697. condition += ` AND frequency NOT IN (` + utils.GetOrmInReplace(len(notFrequencyArr)) + `) `
  1698. pars = append(pars, notFrequencyArr)
  1699. }
  1700. if sysUserIds != "" {
  1701. sysUserIdSlice := strings.Split(sysUserIds, ",")
  1702. condition += ` AND sys_user_id IN (` + utils.GetOrmInReplace(len(sysUserIdSlice)) + `)`
  1703. pars = append(pars, sysUserIdSlice)
  1704. }
  1705. if keyword != "" {
  1706. keyWordArr := strings.Split(keyword, " ")
  1707. if len(keyWordArr) > 0 {
  1708. for _, v := range keyWordArr {
  1709. condition += ` AND CONCAT(edb_name,edb_code) LIKE ?`
  1710. pars = append(pars, utils.GetLikeKeyword(v))
  1711. }
  1712. }
  1713. }
  1714. edbList, e := data_manage.GetEdbInfoListByCond(condition, pars)
  1715. if e != nil {
  1716. br.Msg = "获取指标列表失败"
  1717. br.ErrMsg = "获取指标列表失败,Err:" + e.Error()
  1718. return
  1719. }
  1720. filterLen := len(filterEdbIds)
  1721. for _, v := range edbList {
  1722. if filterLen > 0 && utils.InArrayByInt(filterEdbIds, v.EdbInfoId) {
  1723. continue
  1724. }
  1725. edbIdArr = append(edbIdArr, v.EdbInfoId)
  1726. }
  1727. } else {
  1728. edbIdStrArr := strings.Split(edbInfoIds, ",")
  1729. for _, v := range edbIdStrArr {
  1730. id, e := strconv.Atoi(v)
  1731. if e != nil {
  1732. br.Msg = "获取指标列表失败"
  1733. br.ErrMsg = "获取指标列表失败,Err:" + e.Error()
  1734. return
  1735. }
  1736. edbIdArr = append(edbIdArr, id)
  1737. }
  1738. }
  1739. if len(edbIdArr) > 100 {
  1740. br.Msg = "最多只能选择100个指标"
  1741. return
  1742. }
  1743. if len(edbIdArr) <= 0 {
  1744. br.Msg = "无符合指标或指标代码错误"
  1745. return
  1746. }
  1747. list, err := data_manage.GetEdbInfoByIdList(edbIdArr)
  1748. if err != nil && !utils.IsErrNoRow(err) {
  1749. br.Msg = "获取指标列表失败"
  1750. br.ErrMsg = "获取指标列表失败,Err:" + err.Error()
  1751. return
  1752. }
  1753. searchItemList := make([]data_manage.EdbInfoBase, 0)
  1754. resp := new(data_manage.CalculateMultiChoiceResp)
  1755. if len(list) > 0 {
  1756. classifyMap := make(map[int]*data_manage.EdbClassify)
  1757. {
  1758. classifyIdList := make([]int, 0)
  1759. for _, info := range list {
  1760. classifyIdList = append(classifyIdList, info.ClassifyId)
  1761. }
  1762. classifyList, err := data_manage.GetEdbClassifyByIdList(classifyIdList)
  1763. if err != nil {
  1764. if err != nil {
  1765. br.Msg = "获取失败"
  1766. br.ErrMsg = "获取分类列表失败,Err:" + err.Error()
  1767. return
  1768. }
  1769. }
  1770. for _, v := range classifyList {
  1771. classifyMap[v.ClassifyId] = v
  1772. }
  1773. }
  1774. permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserEdbAndClassifyPermissionList(this.SysUser.AdminId, 0, 0)
  1775. if err != nil {
  1776. br.Msg = "获取失败"
  1777. br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  1778. return
  1779. }
  1780. for _, info := range list {
  1781. var haveOperaAuth bool
  1782. if currClassify, ok := classifyMap[info.ClassifyId]; ok {
  1783. haveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(info.IsJoinPermission, currClassify.IsJoinPermission, info.EdbInfoId, info.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  1784. }
  1785. searchItem := data_manage.EdbInfoBase{
  1786. Frequency: info.Frequency,
  1787. Unit: info.Unit,
  1788. UnitEn: info.UnitEn,
  1789. EdbName: info.EdbName,
  1790. EdbNameEn: info.EdbNameEn,
  1791. EdbInfoId: info.EdbInfoId,
  1792. ClassifyId: info.ClassifyId,
  1793. HaveOperaAuth: haveOperaAuth,
  1794. EdbInfoType: info.EdbInfoType,
  1795. }
  1796. searchItemList = append(searchItemList, searchItem)
  1797. }
  1798. }
  1799. resp.SearchItem = searchItemList
  1800. br.Ret = 200
  1801. br.Success = true
  1802. br.Msg = "获取成功"
  1803. br.Data = resp
  1804. }
  1805. // @router /edb_info/calculate/multi/search [get]
  1806. func (this *ChartInfoController) CalculateMultiSearch() {
  1807. br := new(models.BaseResponse).Init()
  1808. defer func() {
  1809. this.Data["json"] = br
  1810. this.ServeJSON()
  1811. }()
  1812. frequency := this.GetString("Frequency")
  1813. keyword := this.GetString("Keyword")
  1814. sysUserIds := this.GetString("SysUserIds")
  1815. classifyIds := this.GetString("ClassifyIds")
  1816. pageSize, _ := this.GetInt("PageSize")
  1817. currentIndex, _ := this.GetInt("CurrentIndex")
  1818. notFrequency := this.GetString("NotFrequency")
  1819. edbInfoType, _ := this.GetInt("EdbInfoType", 0)
  1820. var startSize int
  1821. if pageSize <= 0 {
  1822. pageSize = utils.PageSize50
  1823. }
  1824. if currentIndex <= 0 {
  1825. currentIndex = 1
  1826. }
  1827. startSize = utils.StartIndex(currentIndex, pageSize)
  1828. var edbIdArr []int
  1829. condition := ` AND edb_info_type = ? `
  1830. pars := make([]interface{}, 0)
  1831. pars = append(pars, edbInfoType)
  1832. if classifyIds != "" {
  1833. classifyIdsArr := strings.Split(classifyIds, ",")
  1834. condition += ` AND classify_id IN (` + utils.GetOrmInReplace(len(classifyIdsArr)) + `) `
  1835. pars = append(pars, classifyIdsArr)
  1836. }
  1837. if frequency != "" {
  1838. frequencyArr := strings.Split(frequency, ",")
  1839. condition += ` AND frequency IN (` + utils.GetOrmInReplace(len(frequencyArr)) + `) `
  1840. pars = append(pars, frequencyArr)
  1841. }
  1842. if notFrequency != "" {
  1843. notFrequencyArr := strings.Split(notFrequency, ",")
  1844. condition += ` AND frequency NOT IN (` + utils.GetOrmInReplace(len(notFrequencyArr)) + `) `
  1845. pars = append(pars, notFrequencyArr)
  1846. }
  1847. if sysUserIds != "" {
  1848. sysUserIdSlice := strings.Split(sysUserIds, ",")
  1849. condition += ` AND sys_user_id IN (` + utils.GetOrmInReplace(len(sysUserIdSlice)) + `)`
  1850. pars = append(pars, sysUserIdSlice)
  1851. }
  1852. if keyword != "" {
  1853. keyWordArr := strings.Split(keyword, " ")
  1854. if len(keyWordArr) > 0 {
  1855. for _, v := range keyWordArr {
  1856. condition += ` AND CONCAT(edb_name,edb_code) LIKE ?`
  1857. pars = append(pars, utils.GetLikeKeyword(v))
  1858. }
  1859. }
  1860. }
  1861. total, e := data_manage.GetEdbInfoByConditionCount(condition, pars)
  1862. if e != nil {
  1863. br.Msg = "获取指标列表总数失败"
  1864. br.ErrMsg = "获取指标列表总数失败,Err:" + e.Error()
  1865. return
  1866. }
  1867. searchItemList := make([]data_manage.CalculateMultiEdbSearchItem, 0)
  1868. page := paging.GetPaging(currentIndex, pageSize, total)
  1869. resp := new(data_manage.CalculateMultiEdbSearchResp)
  1870. edbList, e := data_manage.GetEdbInfoListByCondition(condition, pars, startSize, pageSize, "")
  1871. if e != nil {
  1872. br.Msg = "获取指标列表失败"
  1873. br.ErrMsg = "获取指标列表失败,Err:" + e.Error()
  1874. return
  1875. }
  1876. if len(edbList) > 0 {
  1877. classifyIdList := make([]int, 0)
  1878. for _, v := range edbList {
  1879. edbIdArr = append(edbIdArr, v.EdbInfoId)
  1880. classifyIdList = append(classifyIdList, v.ClassifyId)
  1881. }
  1882. classifyMap := make(map[int]*data_manage.EdbClassify)
  1883. {
  1884. classifyList, err := data_manage.GetEdbClassifyByIdList(classifyIdList)
  1885. if err != nil {
  1886. if err != nil {
  1887. br.Msg = "获取失败"
  1888. br.ErrMsg = "获取分类列表失败,Err:" + err.Error()
  1889. return
  1890. }
  1891. }
  1892. for _, v := range classifyList {
  1893. classifyMap[v.ClassifyId] = v
  1894. }
  1895. }
  1896. permissionEdbIdList, permissionClassifyIdList, err := data_manage_permission.GetUserEdbAndClassifyPermissionList(this.SysUser.AdminId, 0, 0)
  1897. if err != nil {
  1898. br.Msg = "获取失败"
  1899. br.ErrMsg = "获取所有有权限的指标和分类失败,Err:" + err.Error()
  1900. return
  1901. }
  1902. list, err := data_manage.GetEdbInfoByIdList(edbIdArr)
  1903. if err != nil && !utils.IsErrNoRow(err) {
  1904. br.Msg = "获取指标列表失败"
  1905. br.ErrMsg = "获取指标列表失败,Err:" + err.Error()
  1906. return
  1907. }
  1908. for _, info := range list {
  1909. var haveOperaAuth bool
  1910. if currClassify, ok := classifyMap[info.ClassifyId]; ok {
  1911. haveOperaAuth = data_manage_permission.CheckEdbPermissionByPermissionIdList(info.IsJoinPermission, currClassify.IsJoinPermission, info.EdbInfoId, info.ClassifyId, permissionEdbIdList, permissionClassifyIdList)
  1912. }
  1913. searchItem := data_manage.CalculateMultiEdbSearchItem{
  1914. Frequency: info.Frequency,
  1915. Unit: info.Unit,
  1916. UnitEn: info.UnitEn,
  1917. EdbName: info.EdbName,
  1918. EdbNameEn: info.EdbNameEn,
  1919. EdbInfoId: info.EdbInfoId,
  1920. ClassifyId: info.ClassifyId,
  1921. SysUserRealName: info.SysUserRealName,
  1922. SysUserId: info.SysUserId,
  1923. EndDate: utils.TimeToFormatDate(info.EndDate),
  1924. EndValue: info.EndValue,
  1925. HaveOperaAuth: haveOperaAuth,
  1926. EdbInfoType: info.EdbInfoType,
  1927. }
  1928. searchItemList = append(searchItemList, searchItem)
  1929. }
  1930. }
  1931. resp.SearchItem = searchItemList
  1932. resp.Paging = page
  1933. br.Ret = 200
  1934. br.Success = true
  1935. br.Msg = "获取成功"
  1936. br.Data = resp
  1937. }