edb_info_calculate.go 68 KB

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