edb_info_calculate.go 69 KB

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