edb_info_calculate.go 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hz_crm_api/controllers"
  6. "hongze/hz_crm_api/models"
  7. "hongze/hz_crm_api/models/data_manage"
  8. "hongze/hz_crm_api/services/data"
  9. "hongze/hz_crm_api/utils"
  10. "net/url"
  11. "strconv"
  12. "strings"
  13. "time"
  14. )
  15. // 计算指标
  16. type EdbInfoCalculateController struct {
  17. controllers.BaseAuthController
  18. }
  19. // @Title 生成计算指标
  20. // @Description 生成计算指标接口
  21. // @Param request body data_manage.EdbInfoCalculateSaveReq true "type json string"
  22. // @Success Ret=200 返回图表id
  23. // @router /edb_info/calculate/save [post]
  24. func (this *ChartInfoController) CalculateSave() {
  25. br := new(models.BaseResponse).Init()
  26. defer func() {
  27. this.Data["json"] = br
  28. this.ServeJSON()
  29. }()
  30. sysUser := this.SysUser
  31. if sysUser == nil {
  32. br.Msg = "请登录"
  33. br.ErrMsg = "请登录,SysUser Is Empty"
  34. br.Ret = 408
  35. return
  36. }
  37. var req data_manage.EdbInfoCalculateSaveReq
  38. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  39. if err != nil {
  40. br.Msg = "参数解析异常!"
  41. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  42. return
  43. }
  44. req.EdbName = strings.Trim(req.EdbName, " ")
  45. if req.EdbName == "" {
  46. br.Msg = "指标名称不能为空"
  47. return
  48. }
  49. if req.Frequency == "" {
  50. br.Msg = "频率不能为空"
  51. return
  52. }
  53. if req.Unit == "" {
  54. br.Msg = "单位不能为空"
  55. return
  56. }
  57. if req.ClassifyId <= 0 {
  58. br.Msg = "请选择分类"
  59. return
  60. }
  61. if len(req.EdbInfoIdArr) <= 0 {
  62. br.Msg = "请选择指标"
  63. return
  64. }
  65. if req.CalculateFormula == "" {
  66. br.Msg = "请填写指标"
  67. return
  68. }
  69. calculateFormula := req.CalculateFormula
  70. calculateFormula = strings.Replace(calculateFormula, "(", "(", -1)
  71. calculateFormula = strings.Replace(calculateFormula, ")", ")", -1)
  72. calculateFormula = strings.Replace(calculateFormula, ",", ",", -1)
  73. calculateFormula = strings.Replace(calculateFormula, "。", ".", -1)
  74. calculateFormula = strings.Replace(calculateFormula, "%", "*0.01", -1)
  75. req.CalculateFormula = calculateFormula
  76. //判断是否重复指标
  77. edbInfoMap := make(map[int]string)
  78. //移除研究员选择指标中的未使用的指标
  79. {
  80. //转大写的计算公式
  81. upperCalculateFormulaStr := strings.ToUpper(req.CalculateFormula)
  82. //用到的指标
  83. newEdbInfoIdArr := make([]data_manage.EdbInfoFromTag, 0)
  84. for _, tmpEdbInfo := range req.EdbInfoIdArr {
  85. _, ok := edbInfoMap[tmpEdbInfo.EdbInfoId]
  86. if ok {
  87. br.Msg = "选择指标失败,请勿选择重复指标!"
  88. return
  89. }
  90. edbInfoMap[tmpEdbInfo.EdbInfoId] = tmpEdbInfo.FromTag
  91. upperFromTag := strings.ToUpper(tmpEdbInfo.FromTag)
  92. if strings.Contains(upperCalculateFormulaStr, upperFromTag) {
  93. newEdbInfoIdArr = append(newEdbInfoIdArr, tmpEdbInfo)
  94. }
  95. }
  96. req.EdbInfoIdArr = newEdbInfoIdArr
  97. }
  98. var condition string
  99. var pars []interface{}
  100. condition += " AND edb_name=? "
  101. pars = append(pars, req.EdbName)
  102. count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
  103. if err != nil {
  104. br.Msg = "判断指标名称是否存在失败"
  105. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  106. return
  107. }
  108. if count > 0 {
  109. br.Msg = "指标名称已存在,请重新填写"
  110. br.ErrMsg = "指标名称已存在,请重新填写"
  111. br.IsSendEmail = false
  112. return
  113. }
  114. //检验公式
  115. var formulaStr string
  116. var edbInfoIdBytes []string
  117. for _, v := range req.EdbInfoIdArr {
  118. formulaStr += v.FromTag + ","
  119. edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
  120. }
  121. formulaMap := data.CheckFormula(req.CalculateFormula)
  122. for _, v := range formulaMap {
  123. if !strings.Contains(formulaStr, v) {
  124. br.Msg = "公式错误,请重新填写"
  125. return
  126. }
  127. }
  128. // 调用指标库去新增
  129. req2 := &data_manage.EdbInfoCalculateBatchSaveReqByEdbLib{
  130. AdminId: sysUser.AdminId,
  131. AdminName: sysUser.RealName,
  132. //EdbInfoId: req.EdbInfoId,
  133. EdbName: req.EdbName,
  134. Frequency: req.Frequency,
  135. Unit: req.Unit,
  136. ClassifyId: req.ClassifyId,
  137. CalculateFormula: req.CalculateFormula,
  138. EdbInfoIdArr: req.EdbInfoIdArr,
  139. }
  140. reqJson, err := json.Marshal(req2)
  141. if err != nil {
  142. br.Msg = "参数解析异常!"
  143. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  144. return
  145. }
  146. respItem, err := data.AddEdbCalculateData(string(reqJson))
  147. if err != nil {
  148. br.Msg = "新增失败"
  149. br.ErrMsg = "新增失败,Err:" + err.Error()
  150. return
  151. }
  152. if respItem.Ret != 200 {
  153. br.Msg = respItem.Msg
  154. br.ErrMsg = respItem.ErrMsg
  155. return
  156. }
  157. resp := respItem.Data
  158. //添加es
  159. data.AddOrEditEdbInfoToEs(resp.EdbInfoId)
  160. br.Ret = 200
  161. br.Success = true
  162. br.Msg = "保存成功"
  163. br.Data = resp
  164. br.IsAddLog = true
  165. }
  166. // @Title 获取计算指标详情
  167. // @Description 获取计算指标详情接口
  168. // @Param EdbInfoId query int true "指标id"
  169. // @Success 200 {object} data_manage.CalculateDetailResp
  170. // @router /edb_info/calculate/detail [get]
  171. func (this *ChartInfoController) CalculateDetail() {
  172. br := new(models.BaseResponse).Init()
  173. defer func() {
  174. this.Data["json"] = br
  175. this.ServeJSON()
  176. }()
  177. sysUser := this.SysUser
  178. if sysUser == nil {
  179. br.Msg = "请登录"
  180. br.ErrMsg = "请登录,SysUser Is Empty"
  181. br.Ret = 408
  182. return
  183. }
  184. edbInfoId, _ := this.GetInt("EdbInfoId")
  185. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  186. if err != nil {
  187. br.Msg = "获取失败"
  188. br.ErrMsg = "获取失败,Err:" + err.Error()
  189. return
  190. }
  191. calculateList, err := data_manage.GetEdbInfoCalculateDetail(edbInfoId, edbInfo.Source)
  192. if err != nil {
  193. br.Msg = "获取失败"
  194. br.ErrMsg = "获取失败,Err:" + err.Error()
  195. return
  196. }
  197. resp := new(data_manage.CalculateDetailResp)
  198. resp.EdbInfoDetail = edbInfo
  199. resp.CalculateList = calculateList
  200. br.Ret = 200
  201. br.Success = true
  202. br.Msg = "保存成功"
  203. br.Data = resp
  204. }
  205. // @Title 修改计算指标
  206. // @Description 修改计算指标接口
  207. // @Param request body data_manage.EdbInfoCalculateEditReq true "type json string"
  208. // @Success Ret=200 返回图表id
  209. // @router /edb_info/calculate/edit [post]
  210. func (this *ChartInfoController) CalculateEdit() {
  211. br := new(models.BaseResponse).Init()
  212. defer func() {
  213. this.Data["json"] = br
  214. this.ServeJSON()
  215. }()
  216. sysUser := this.SysUser
  217. if sysUser == nil {
  218. br.Msg = "请登录"
  219. br.ErrMsg = "请登录,SysUser Is Empty"
  220. br.Ret = 408
  221. return
  222. }
  223. var req data_manage.EdbInfoCalculateEditReq
  224. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  225. if err != nil {
  226. br.Msg = "参数解析异常!"
  227. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  228. return
  229. }
  230. if req.EdbInfoId <= 0 {
  231. br.Msg = "参数错误"
  232. br.ErrMsg = "指标ID:" + strconv.Itoa(req.EdbInfoId)
  233. return
  234. }
  235. req.EdbName = strings.Trim(req.EdbName, " ")
  236. if req.EdbName == "" {
  237. br.Msg = "指标名称不能为空"
  238. return
  239. }
  240. if req.Frequency == "" {
  241. br.Msg = "频率不能为空"
  242. return
  243. }
  244. if req.Unit == "" {
  245. br.Msg = "单位不能为空"
  246. return
  247. }
  248. if req.ClassifyId <= 0 {
  249. br.Msg = "请选择分类"
  250. return
  251. }
  252. if len(req.EdbInfoIdArr) <= 0 {
  253. br.Msg = "请选择指标"
  254. return
  255. }
  256. if req.CalculateFormula == "" {
  257. br.Msg = "请填写指标"
  258. return
  259. }
  260. calculateFormula := req.CalculateFormula
  261. calculateFormula = strings.Replace(calculateFormula, "(", "(", -1)
  262. calculateFormula = strings.Replace(calculateFormula, ")", ")", -1)
  263. calculateFormula = strings.Replace(calculateFormula, ",", ",", -1)
  264. calculateFormula = strings.Replace(calculateFormula, "。", ".", -1)
  265. calculateFormula = strings.Replace(calculateFormula, "%", "*0.01", -1)
  266. req.CalculateFormula = calculateFormula
  267. fmt.Println("calculateFormula:" + calculateFormula)
  268. //判断是否重复指标
  269. edbInfoMap := make(map[int]string)
  270. //移除研究员选择指标中的未使用的指标
  271. {
  272. //转大写的计算公式
  273. upperCalculateFormulaStr := strings.ToUpper(req.CalculateFormula)
  274. //用到的指标
  275. newEdbInfoIdArr := make([]data_manage.EdbInfoFromTag, 0)
  276. for _, tmpEdbInfo := range req.EdbInfoIdArr {
  277. _, ok := edbInfoMap[tmpEdbInfo.EdbInfoId]
  278. if ok {
  279. br.Msg = "选择指标失败,请勿选择重复指标!"
  280. return
  281. }
  282. edbInfoMap[tmpEdbInfo.EdbInfoId] = tmpEdbInfo.FromTag
  283. upperFromTag := strings.ToUpper(tmpEdbInfo.FromTag)
  284. if strings.Contains(upperCalculateFormulaStr, upperFromTag) {
  285. newEdbInfoIdArr = append(newEdbInfoIdArr, tmpEdbInfo)
  286. }
  287. }
  288. req.EdbInfoIdArr = newEdbInfoIdArr
  289. }
  290. var condition string
  291. var pars []interface{}
  292. condition += " AND edb_name=? "
  293. pars = append(pars, req.EdbName)
  294. condition += " AND edb_info_id<>? "
  295. pars = append(pars, req.EdbInfoId)
  296. count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
  297. if err != nil {
  298. br.Msg = "判断指标名称是否存在失败"
  299. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  300. return
  301. }
  302. if count > 0 {
  303. br.Msg = "指标名称已存在,请重新填写"
  304. br.ErrMsg = "指标名称已存在,请重新填写"
  305. br.IsSendEmail = false
  306. return
  307. }
  308. //判断公式,指标是否有改动
  309. edbInfoDetail, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  310. if err != nil {
  311. br.Msg = "修改失败"
  312. br.Msg = "获取指标信息失败,GetEdbInfoById Err:" + err.Error()
  313. return
  314. }
  315. if edbInfoDetail == nil {
  316. br.Msg = "修改失败"
  317. br.Msg = "指标信息不存在,EdbInfoId:" + strconv.Itoa(req.EdbInfoId)
  318. return
  319. }
  320. // 调用指标库去更新
  321. req2 := &data_manage.EdbInfoCalculateBatchSaveReqByEdbLib{
  322. AdminId: sysUser.AdminId,
  323. AdminName: sysUser.RealName,
  324. EdbInfoId: req.EdbInfoId,
  325. EdbName: req.EdbName,
  326. Frequency: req.Frequency,
  327. Unit: req.Unit,
  328. ClassifyId: req.ClassifyId,
  329. CalculateFormula: req.CalculateFormula,
  330. EdbInfoIdArr: req.EdbInfoIdArr,
  331. }
  332. reqJson, err := json.Marshal(req2)
  333. if err != nil {
  334. br.Msg = "参数解析异常!"
  335. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  336. return
  337. }
  338. respItem, err := data.EditEdbCalculateData(string(reqJson))
  339. if err != nil {
  340. br.Msg = "编辑失败"
  341. br.ErrMsg = "编辑失败,Err:" + err.Error()
  342. return
  343. }
  344. if respItem.Ret != 200 {
  345. br.Msg = respItem.Msg
  346. br.ErrMsg = respItem.ErrMsg
  347. return
  348. }
  349. resp := respItem.Data
  350. //添加es
  351. data.AddOrEditEdbInfoToEs(req.EdbInfoId)
  352. br.Ret = 200
  353. br.Success = true
  354. br.Msg = "保存成功"
  355. br.IsAddLog = true
  356. br.Data = resp
  357. }
  358. // @Title 累计值转月-同比值-同差等计算新增
  359. // @Description 累计值转月-同比值-同差等计算新增接口
  360. // @Param request body data_manage.EdbInfoCalculateBatchSaveReq true "type json string"
  361. // @Success Ret=200 返回指标id
  362. // @router /edb_info/calculate/batch/save [post]
  363. func (this *ChartInfoController) CalculateBatchSave() {
  364. br := new(models.BaseResponse).Init()
  365. defer func() {
  366. this.Data["json"] = br
  367. this.ServeJSON()
  368. }()
  369. sysUser := this.SysUser
  370. if sysUser == nil {
  371. br.Msg = "请登录"
  372. br.ErrMsg = "请登录,SysUser Is Empty"
  373. br.Ret = 408
  374. return
  375. }
  376. var req data_manage.EdbInfoCalculateBatchSaveReq
  377. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  378. if err != nil {
  379. br.Msg = "参数解析异常!"
  380. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  381. return
  382. }
  383. req.EdbName = strings.Trim(req.EdbName, " ")
  384. if req.EdbName == "" {
  385. br.Msg = "指标名称不能为空"
  386. return
  387. }
  388. if req.Frequency == "" {
  389. br.Msg = "频率不能为空"
  390. return
  391. }
  392. if req.Unit == "" {
  393. br.Msg = "单位不能为空"
  394. return
  395. }
  396. if req.ClassifyId <= 0 {
  397. br.Msg = "请选择分类"
  398. return
  399. }
  400. fromEdbInfoId := req.FromEdbInfoId
  401. var formulaInt int
  402. switch req.Source {
  403. 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:
  404. if req.Formula == "" {
  405. br.Msg = "请填写N值"
  406. return
  407. }
  408. formulaInt, _ = strconv.Atoi(req.Formula)
  409. if formulaInt <= 0 {
  410. br.Msg = "N值输入错误,请重新输入"
  411. return
  412. }
  413. case utils.DATA_SOURCE_CALCULATE_ZJPJ:
  414. //直接拼接指标
  415. //校验时间格式
  416. _, err = time.ParseInLocation(utils.FormatDate, req.Formula, time.Local)
  417. if err != nil {
  418. br.Msg = "拼接日期有误,请重新输入"
  419. return
  420. }
  421. case utils.DATA_SOURCE_CALCULATE_NHCC: //拟合残差指标
  422. //指标校验
  423. if len(req.EdbInfoIdArr) != 2 {
  424. br.Msg = "选择的指标异常,请重新选择"
  425. return
  426. }
  427. fromEdbInfoId = req.EdbInfoIdArr[0].EdbInfoId
  428. //校验时间格式
  429. //数据格式:2022-11-01,2022-11-10
  430. timeList := strings.Split(req.Formula, ",")
  431. if len(timeList) != 2 {
  432. br.Msg = "选择时间有误,请重新输入"
  433. return
  434. }
  435. startDate, err := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
  436. if err != nil {
  437. br.Msg = "开始日期有误,请重新输入"
  438. return
  439. }
  440. endDate, err := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
  441. if err != nil {
  442. br.Msg = "结束日期有误,请重新输入"
  443. return
  444. }
  445. if utils.GetTimeSubDay(startDate, endDate) < 2 {
  446. br.Msg = "日期间隔不得少于两天"
  447. return
  448. }
  449. }
  450. notNeedFromEdbSourceList := []int{utils.DATA_SOURCE_CALCULATE_KSZS} // 不需要传入来源指标id的 指标类型
  451. if fromEdbInfoId <= 0 && !utils.InArrayByInt(notNeedFromEdbSourceList, req.Source) {
  452. br.Msg = "请选择指标"
  453. return
  454. }
  455. //加入缓存机制,避免创建同一个名称的指标 start
  456. redisKey := fmt.Sprint("edb_info:calculate:batch:save:", req.Source, ":", req.EdbName)
  457. isExist := utils.Rc.IsExist(redisKey)
  458. if isExist {
  459. br.Msg = "指标正在处理,请勿重复提交"
  460. return
  461. } else {
  462. //设置3分钟缓存
  463. utils.Rc.SetNX(redisKey, 1, time.Second*300)
  464. defer func() {
  465. utils.Rc.Delete(redisKey)
  466. }()
  467. }
  468. //加入缓存机制,避免创建同一个名称的指标 end
  469. req2 := &data_manage.EdbInfoCalculateBatchSaveReqByEdbLib{
  470. AdminId: sysUser.AdminId,
  471. AdminName: sysUser.RealName,
  472. EdbInfoId: req.EdbInfoId,
  473. EdbName: req.EdbName,
  474. Frequency: req.Frequency,
  475. Unit: req.Unit,
  476. ClassifyId: req.ClassifyId,
  477. Formula: req.Formula, //N数值移动平均计算、环比值、环差值
  478. FromEdbInfoId: req.FromEdbInfoId,
  479. CalculateFormula: req.CalculateFormula,
  480. Source: req.Source,
  481. MoveType: req.MoveType,
  482. MoveFrequency: req.MoveFrequency,
  483. //CalculateFormula: edbInfo.CalculateFormula,
  484. EdbInfoIdArr: req.EdbInfoIdArr,
  485. Calendar: req.Calendar,
  486. }
  487. // 调用指标库去更新
  488. reqJson, err := json.Marshal(req2)
  489. if err != nil {
  490. br.Msg = "参数解析异常!"
  491. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  492. return
  493. }
  494. respItem, err := data.BatchSaveEdbCalculateData(string(reqJson))
  495. if err != nil {
  496. br.Msg = "新增失败"
  497. br.ErrMsg = "新增失败,Err:" + err.Error()
  498. return
  499. }
  500. if respItem.Ret != 200 {
  501. br.Msg = respItem.Msg
  502. br.ErrMsg = respItem.ErrMsg
  503. return
  504. }
  505. resp := respItem.Data
  506. //添加es
  507. data.AddOrEditEdbInfoToEs(resp.EdbInfoId)
  508. br.Ret = 200
  509. br.Success = true
  510. br.Msg = "保存成功"
  511. br.Data = resp
  512. br.IsAddLog = true
  513. }
  514. // @Title 累计值转月-同比值-同差等计算编辑
  515. // @Description 累计值转月-同比值-同差等计算编辑接口
  516. // @Param request body data_manage.EdbInfoCalculateBatchEditReq true "type json string"
  517. // @Success Ret=200 返回指标id
  518. // @router /edb_info/calculate/batch/edit [post]
  519. func (this *ChartInfoController) CalculateBatchEdit() {
  520. br := new(models.BaseResponse).Init()
  521. defer func() {
  522. this.Data["json"] = br
  523. this.ServeJSON()
  524. }()
  525. sysUser := this.SysUser
  526. if sysUser == nil {
  527. br.Msg = "请登录"
  528. br.ErrMsg = "请登录,SysUser Is Empty"
  529. br.Ret = 408
  530. return
  531. }
  532. var req data_manage.EdbInfoCalculateBatchEditReq
  533. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  534. if err != nil {
  535. br.Msg = "参数解析异常!"
  536. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  537. return
  538. }
  539. if req.EdbName == "" {
  540. br.Msg = "指标名称不能为空"
  541. return
  542. }
  543. if req.Frequency == "" {
  544. br.Msg = "频率不能为空"
  545. return
  546. }
  547. if req.Unit == "" {
  548. br.Msg = "单位不能为空"
  549. return
  550. }
  551. if req.ClassifyId <= 0 {
  552. br.Msg = "请选择分类"
  553. return
  554. }
  555. if req.EdbInfoId <= 0 {
  556. br.Msg = "请选择指标"
  557. return
  558. }
  559. var condition string
  560. var pars []interface{}
  561. condition += " AND edb_name=? "
  562. pars = append(pars, req.EdbName)
  563. condition += " AND edb_info_id<>? "
  564. pars = append(pars, req.EdbInfoId)
  565. count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
  566. if err != nil {
  567. br.Msg = "判断指标名称是否存在失败"
  568. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  569. return
  570. }
  571. if count > 0 {
  572. br.Msg = "指标名称已存在,请重新填写"
  573. br.ErrMsg = "指标名称已存在,请重新填写"
  574. br.IsSendEmail = false
  575. return
  576. }
  577. edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  578. if err != nil {
  579. if err.Error() == utils.ErrNoRow() {
  580. br.Msg = "指标已被删除,请刷新页面"
  581. br.ErrMsg = "指标已被删除,请刷新页面:Err:" + err.Error()
  582. return
  583. }
  584. br.Msg = "获取指标信息失败"
  585. br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
  586. return
  587. }
  588. if req.Source <= 0 {
  589. req.Source = edbInfo.Source
  590. }
  591. var formulaInt int
  592. switch req.Source {
  593. 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:
  594. if req.Formula == "" {
  595. br.Msg = "请填写N值"
  596. return
  597. }
  598. formulaInt, _ = strconv.Atoi(req.Formula)
  599. if formulaInt <= 0 {
  600. br.Msg = "N值输入错误,请重新输入"
  601. return
  602. }
  603. case utils.DATA_SOURCE_CALCULATE_ZJPJ:
  604. //直接拼接指标
  605. //校验时间格式
  606. _, err = time.ParseInLocation(utils.FormatDate, req.Formula, time.Local)
  607. if err != nil {
  608. br.Msg = "拼接日期有误,请重新输入"
  609. return
  610. }
  611. case utils.DATA_SOURCE_CALCULATE_NHCC: //拟合残差指标
  612. //指标校验
  613. if len(req.EdbInfoIdArr) != 2 {
  614. br.Msg = "选择的指标异常,请重新选择"
  615. return
  616. }
  617. //校验时间格式
  618. //数据格式:2022-11-01,2022-11-10
  619. timeList := strings.Split(req.Formula, ",")
  620. if len(timeList) != 2 {
  621. br.Msg = "选择时间有误,请重新输入"
  622. return
  623. }
  624. startDate, err := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
  625. if err != nil {
  626. br.Msg = "开始日期有误,请重新输入"
  627. return
  628. }
  629. endDate, err := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
  630. if err != nil {
  631. br.Msg = "结束日期有误,请重新输入"
  632. return
  633. }
  634. if utils.GetTimeSubDay(startDate, endDate) < 2 {
  635. br.Msg = "日期间隔不得少于两天"
  636. return
  637. }
  638. }
  639. // 构造请求
  640. req2 := &data_manage.EdbInfoCalculateBatchEditReqByEdbLib{
  641. EdbInfoId: req.EdbInfoId,
  642. EdbName: req.EdbName,
  643. Frequency: req.Frequency,
  644. Unit: req.Unit,
  645. ClassifyId: req.ClassifyId,
  646. Formula: req.Formula, //N数值移动平均计算、环比值、环差值
  647. FromEdbInfoId: req.FromEdbInfoId,
  648. Source: req.Source,
  649. MoveType: req.MoveType,
  650. MoveFrequency: req.MoveFrequency,
  651. EdbInfoIdArr: req.EdbInfoIdArr,
  652. Calendar: req.Calendar,
  653. }
  654. // 调用指标库去更新
  655. reqJson, err := json.Marshal(req2)
  656. if err != nil {
  657. br.Msg = "参数解析异常!"
  658. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  659. return
  660. }
  661. respItem, err := data.BatchEditEdbCalculateData(string(reqJson))
  662. if err != nil {
  663. br.Msg = "编辑失败"
  664. br.ErrMsg = "编辑失败,Err:" + err.Error()
  665. return
  666. }
  667. if respItem.Ret != 200 {
  668. br.Msg = respItem.Msg
  669. br.ErrMsg = respItem.ErrMsg
  670. return
  671. }
  672. resp := respItem.Data
  673. //添加es
  674. data.AddOrEditEdbInfoToEs(resp.EdbInfoId)
  675. br.Ret = 200
  676. br.Success = true
  677. br.Msg = "保存成功"
  678. br.Data = resp
  679. br.IsAddLog = true
  680. }
  681. // @Title 累计值转月-同比值-同差等重新计算
  682. // @Description 累计值转月-同比值-同差等重新计算接口
  683. // @Param request body data_manage.EdbInfoCalculateBatchSaveReq true "type json string"
  684. // @Success Ret=200 返回指标id
  685. // @router /edb_info/calculate/batch/reset [post]
  686. func (this *ChartInfoController) CalculateBatchReset() {
  687. br := new(models.BaseResponse).Init()
  688. defer func() {
  689. this.Data["json"] = br
  690. this.ServeJSON()
  691. }()
  692. sysUser := this.SysUser
  693. if sysUser == nil {
  694. br.Msg = "请登录"
  695. br.ErrMsg = "请登录,SysUser Is Empty"
  696. br.Ret = 408
  697. return
  698. }
  699. var req data_manage.EdbInfoCalculateBatchSaveReq
  700. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  701. if err != nil {
  702. br.Msg = "参数解析异常!"
  703. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  704. return
  705. }
  706. if req.EdbInfoId <= 0 {
  707. br.Msg = "参数错误"
  708. br.ErrMsg = "参数错误:EdbInfoId:" + strconv.Itoa(req.EdbInfoId)
  709. return
  710. }
  711. edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  712. if err != nil {
  713. if err.Error() == utils.ErrNoRow() {
  714. br.Msg = "指标已被删除,请刷新页面"
  715. br.ErrMsg = "指标已删除,请刷新页面:Err:" + err.Error()
  716. return
  717. }
  718. br.Msg = "重新计算失败"
  719. br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
  720. return
  721. }
  722. uniqueCode := edbInfo.UniqueCode
  723. sourName := edbInfo.SourceName
  724. edbInfoId := edbInfo.EdbInfoId
  725. //if source == utils.DATA_SOURCE_CALCULATE {
  726. // //检验公式
  727. // var formulaStr string
  728. // var edbInfoIdBytes []string
  729. // for _, v := range fromEdbInfoList {
  730. // formulaStr += v.FromTag + ","
  731. // edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
  732. // }
  733. // formulaStr = strings.Trim(formulaStr, ",")
  734. // formulaMap := data.CheckFormula(req.CalculateFormula)
  735. // for _, v := range formulaMap {
  736. // if !strings.Contains(formulaStr, v) {
  737. // br.Msg = "公式错误,请重新填写"
  738. // return
  739. // }
  740. // }
  741. //
  742. // edbInfoList := make([]*data_manage.EdbInfo, 0)
  743. //
  744. // for _, v := range fromEdbInfoList {
  745. // edbInfo, err := data_manage.GetEdbInfoById(v.FromEdbInfoId)
  746. // if err != nil {
  747. // if err.Error() == utils.ErrNoRow() {
  748. // br.Msg = "重新计算失败"
  749. // br.Msg = "指标 " + strconv.Itoa(v.FromEdbInfoId) + " 不存在"
  750. // return
  751. // }
  752. // br.Msg = "重新计算失败"
  753. // br.Msg = "获取指标失败:Err:" + err.Error()
  754. // return
  755. // }
  756. // edbInfoList = append(edbInfoList, edbInfo)
  757. // }
  758. // //清除历史数据
  759. // err = data_manage.DeleteCalculateData(edbInfoId)
  760. // if err != nil {
  761. // br.Msg = "重新计算失败"
  762. // br.Msg = "获取指标失败:Err:" + err.Error()
  763. // return
  764. // }
  765. // err = data.Calculate(edbInfoList, int(edbInfoId), edbCode, edbInfo.CalculateFormula, edbInfoIdBytes)
  766. // if err != nil {
  767. // br.Msg = "重新计算失败"
  768. // br.Msg = "生成计算指标失败,Calculate Err:" + err.Error()
  769. // return
  770. // }
  771. //} else if source == utils.DATA_SOURCE_CALCULATE_LJZZY {
  772. // _, err = data_manage.AddCalculateLjzzy(&req, fromEdbInfo, edbCode, uniqueCode, sysUser.AdminId, sysUser.RealName)
  773. //} else if source == utils.DATA_SOURCE_CALCULATE_TBZ {
  774. // sourName = "同比值"
  775. // edbInfoId, err = data_manage.AddCalculateTbz(&req, fromEdbInfo, edbCode, uniqueCode, sysUser.AdminId, sysUser.RealName)
  776. //} else if source == utils.DATA_SOURCE_CALCULATE_TCZ {
  777. // sourName = "同差值"
  778. // edbInfoId, err = data_manage.AddCalculateTcz(&req, fromEdbInfo, edbCode, uniqueCode, sysUser.AdminId, sysUser.RealName)
  779. //} else if source == utils.DATA_SOURCE_CALCULATE_NSZYDPJJS {
  780. // sourName = "N数值移动平均计算"
  781. // formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  782. // edbInfoId, err = data_manage.AddCalculateNszydpjjs(&req, fromEdbInfo, edbCode, uniqueCode, sysUser.AdminId, sysUser.RealName, formulaInt)
  783. //} else if source == utils.DATA_SOURCE_CALCULATE_HBZ {
  784. // sourName = "环比值"
  785. // formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  786. // edbInfoId, err = data_manage.AddCalculateHbz(&req, fromEdbInfo, edbCode, uniqueCode, sysUser.AdminId, sysUser.RealName, formulaInt)
  787. //} else if source == utils.DATA_SOURCE_CALCULATE_HCZ {
  788. // sourName = "环差值"
  789. // formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  790. // edbInfoId, err = data_manage.AddCalculateHcz(&req, fromEdbInfo, edbCode, uniqueCode, sysUser.AdminId, sysUser.RealName, formulaInt)
  791. //} else if source == utils.DATA_SOURCE_CALCULATE_BP {
  792. // sourName = "变频"
  793. // edbInfoId, err = data_manage.AddCalculateBp(&req, fromEdbInfo, edbCode, uniqueCode, sysUser.AdminId, sysUser.RealName)
  794. //} else if source == utils.DATA_SOURCE_CALCULATE_TIME_SHIFT {
  795. // sourName = "时间移位"
  796. // if req.MoveFrequency == "" {
  797. // req.MoveFrequency = edbInfo.MoveFrequency
  798. // }
  799. // if req.Formula == "" {
  800. // req.Formula = edbInfo.CalculateFormula
  801. // }
  802. // edbInfoId, err = data_manage.AddCalculateTimeShift(&req, fromEdbInfo, edbCode, uniqueCode, sysUser.AdminId, sysUser.RealName)
  803. //} else if source == utils.DATA_SOURCE_CALCULATE_ZJPJ {
  804. // sourName = "直接拼接"
  805. // err = data_manage.RefreshAllCalculateZjpj(edbInfo)
  806. //} else if source == utils.DATA_SOURCE_CALCULATE_LJZTBPJ {
  807. // sourName = "累计值同比拼接"
  808. // err = data_manage.RefreshAllCalculateLjztbpj(edbInfo)
  809. //} else {
  810. // br.Msg = "无效计算方式"
  811. // br.ErrMsg = "无效计算方式,source:" + strconv.Itoa(source)
  812. // return
  813. //}
  814. // 重新计算
  815. err = data.EdbInfoRefreshAllFromBaseV2(edbInfoId, true)
  816. if err != nil {
  817. fmt.Println(edbInfoId, "RefreshEdbCalculateData err", time.Now())
  818. br.Msg = "重新计算失败"
  819. br.ErrMsg = "生成" + sourName + "失败,RefreshEdbCalculateData Err:" + err.Error()
  820. return
  821. }
  822. resp := new(data_manage.AddEdbInfoResp)
  823. resp.EdbInfoId = edbInfoId
  824. resp.UniqueCode = uniqueCode
  825. br.Ret = 200
  826. br.Success = true
  827. br.Msg = "重新计算成功"
  828. br.Data = resp
  829. br.IsAddLog = true
  830. }
  831. //累计值转月值
  832. //func init() {
  833. // fmt.Println("start CalculateBatchSave")
  834. // req := new(data_manage.EdbInfoCalculateBatchSaveReq)
  835. // req.EdbInfoId = 100282
  836. //
  837. // randStr := utils.GetRandDigit(4)
  838. // edbCode := `C` + time.Now().Format("060102") + randStr
  839. // timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  840. // uniqueCode := utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  841. // fromEdbInfo, _ := data_manage.GetEdbInfoById(req.EdbInfoId)
  842. // data_manage.AddCalculateLjzzy(req, fromEdbInfo, edbCode, uniqueCode, 1, "rdluck")
  843. // fmt.Println("end CalculateBatchSave")
  844. // return
  845. //}
  846. ////同比值
  847. //func init() {
  848. // fmt.Println("start AddCalculateTbz")
  849. // req := new(data_manage.EdbInfoCalculateBatchSaveReq)
  850. // req.EdbInfoId = 100092
  851. //
  852. // randStr := utils.GetRandDigit(4)
  853. // edbCode := `C` + time.Now().Format("060102") + randStr
  854. // timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  855. // uniqueCode := utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  856. // fromEdbInfo, _ := data_manage.GetEdbInfoById(req.EdbInfoId)
  857. //
  858. // data_manage.AddCalculateTbz(req, fromEdbInfo, edbCode, uniqueCode, 1, "rdluck")
  859. // fmt.Println("end AddCalculateTbz")
  860. // return
  861. //}
  862. //同差值
  863. //func init() {
  864. // fmt.Println("start AddCalculateTbz")
  865. // req := new(data_manage.EdbInfoCalculateBatchSaveReq)
  866. // req.EdbInfoId = 100092
  867. //
  868. // randStr := utils.GetRandDigit(4)
  869. // edbCode := `C` + time.Now().Format("060102") + randStr
  870. // timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  871. // uniqueCode := utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  872. // fromEdbInfo, _ := data_manage.GetEdbInfoById(req.EdbInfoId)
  873. //
  874. // data_manage.AddCalculateTcz(req, fromEdbInfo, edbCode, uniqueCode, 1, "rdluck")
  875. //
  876. // fmt.Println("end AddCalculateTbz")
  877. // return
  878. //}
  879. //N数值移动平均计算
  880. //func init() {
  881. // fmt.Println("start AddCalculateTbz")
  882. // req := new(data_manage.EdbInfoCalculateBatchSaveReq)
  883. // req.FromEdbInfoId = 100081
  884. //
  885. // randStr := utils.GetRandDigit(4)
  886. // edbCode := `C` + time.Now().Format("060102") + randStr
  887. // timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  888. // uniqueCode := utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  889. // fromEdbInfo, _ := data_manage.GetEdbInfoById(req.FromEdbInfoId)
  890. // formulaInt := 3
  891. // fmt.Println("edbCode:", edbCode)
  892. // data_manage.AddCalculateNszydpjjs(req, fromEdbInfo, edbCode, uniqueCode, 1, "rdluck", formulaInt)
  893. //
  894. // fmt.Println("end AddCalculateTbz")
  895. // return
  896. //}
  897. // ExecPythonCode
  898. // @Title 执行python代码
  899. // @Description 执行python代码接口
  900. // @Param request body data_manage.EdbInfoCalculateSaveReq true "type json string"
  901. // @Success Ret=200 返回图表id
  902. // @router /edb_info/python/exec [post]
  903. func (this *ChartInfoController) ExecPythonCode() {
  904. br := new(models.BaseResponse).Init()
  905. defer func() {
  906. this.Data["json"] = br
  907. this.ServeJSON()
  908. }()
  909. sysUser := this.SysUser
  910. if sysUser == nil {
  911. br.Msg = "请登录"
  912. br.ErrMsg = "请登录,SysUser Is Empty"
  913. br.Ret = 408
  914. return
  915. }
  916. var req data_manage.ExecPythonEdbReq
  917. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  918. if err != nil {
  919. br.Msg = "参数解析异常!"
  920. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  921. return
  922. }
  923. if req.PythonCode == "" {
  924. br.Msg = "python代码不能为空"
  925. return
  926. }
  927. resp, err := data.ExecPythonCode(req.PythonCode)
  928. if err != nil {
  929. br.Msg = "python代码执行失败,请检查代码"
  930. br.ErrMsg = "python代码执行失败,Err:" + err.Error()
  931. return
  932. }
  933. if resp.Ret != 200 {
  934. br.Msg = "python代码执行失败,请检查代码"
  935. br.ErrMsg = "python代码执行失败,Err:" + resp.ErrMsg
  936. return
  937. }
  938. br.Ret = 200
  939. br.Success = true
  940. br.Msg = "运行成功"
  941. br.Data = resp.Data
  942. }
  943. // AddPythonEdb
  944. // @Title 添加python指标
  945. // @Description 添加python指标接口
  946. // @Param request body data_manage.AddPythonEdbReq true "type json string"
  947. // @Success 200 {object} data_manage.AddEdbInfoResp
  948. // @router /edb_info/python/add [post]
  949. func (this *ChartInfoController) AddPythonEdb() {
  950. br := new(models.BaseResponse).Init()
  951. defer func() {
  952. this.Data["json"] = br
  953. this.ServeJSON()
  954. }()
  955. sysUser := this.SysUser
  956. if sysUser == nil {
  957. br.Msg = "请登录"
  958. br.ErrMsg = "请登录,SysUser Is Empty"
  959. br.Ret = 408
  960. return
  961. }
  962. var req data_manage.PostAddPythonEdbReq
  963. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  964. if err != nil {
  965. br.Msg = "参数解析异常!"
  966. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  967. return
  968. }
  969. //传入操作人
  970. req.AdminId = sysUser.AdminId
  971. req.AdminName = sysUser.RealName
  972. reqJson, err := json.Marshal(req)
  973. if err != nil {
  974. br.Msg = "参数解析异常!"
  975. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  976. return
  977. }
  978. // 判断指标名称是否重复
  979. {
  980. var condition string
  981. var pars []interface{}
  982. condition += " AND edb_name=? "
  983. pars = append(pars, req.EdbName)
  984. condition += " AND edb_info_id<>? "
  985. pars = append(pars, req.EdbInfoId)
  986. count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
  987. if err != nil {
  988. br.Msg = "判断指标名称是否存在失败"
  989. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  990. return
  991. }
  992. if count > 0 {
  993. br.Msg = "指标名称已存在,请重新填写"
  994. br.ErrMsg = "指标名称已存在,请重新填写"
  995. br.IsSendEmail = false
  996. return
  997. }
  998. }
  999. // 添加python指标
  1000. respItem, err := data.AddPythonEdbData(string(reqJson))
  1001. if err != nil {
  1002. br.Msg = "添加失败"
  1003. br.ErrMsg = "获取失败,Err:" + err.Error()
  1004. return
  1005. }
  1006. if respItem.Ret != 200 {
  1007. br.Msg = "添加失败"
  1008. br.ErrMsg = respItem.ErrMsg + ";EdbName:" + req.EdbName
  1009. return
  1010. }
  1011. respData := respItem.Data.(map[string]interface{})
  1012. tmpEdbInfoId, ok := respData["EdbInfoId"]
  1013. if !ok {
  1014. br.Msg = `数据转换异常`
  1015. br.ErrMsg = fmt.Sprint("respData.EdbInfoId 数据转换异常;respData:", respData)
  1016. return
  1017. }
  1018. //指标id
  1019. edbInfoId := int64(tmpEdbInfoId.(float64))
  1020. uniqueCode, ok := respData["UniqueCode"]
  1021. if !ok {
  1022. br.Msg = `数据转换异常`
  1023. br.ErrMsg = fmt.Sprint("respData.UniqueCode 数据转换异常;respData:", respData)
  1024. return
  1025. }
  1026. //添加es
  1027. data.AddOrEditEdbInfoToEs(int(edbInfoId))
  1028. //
  1029. resp := data_manage.AddEdbInfoResp{
  1030. EdbInfoId: int(edbInfoId),
  1031. UniqueCode: uniqueCode.(string),
  1032. }
  1033. br.Ret = 200
  1034. br.Success = true
  1035. br.Msg = "保存成功"
  1036. br.Data = resp
  1037. br.IsAddLog = true
  1038. }
  1039. // EditPythonEdb
  1040. // @Title 修改python运算指标
  1041. // @Description 修改python运算指标接口
  1042. // @Param request body data_manage.AddPythonEdbReq true "type json string"
  1043. // @Success Ret=200 返回成功
  1044. // @router /edb_info/python/edit [post]
  1045. func (this *ChartInfoController) EditPythonEdb() {
  1046. br := new(models.BaseResponse).Init()
  1047. defer func() {
  1048. this.Data["json"] = br
  1049. this.ServeJSON()
  1050. }()
  1051. sysUser := this.SysUser
  1052. if sysUser == nil {
  1053. br.Msg = "请登录"
  1054. br.ErrMsg = "请登录,SysUser Is Empty"
  1055. br.Ret = 408
  1056. return
  1057. }
  1058. var req data_manage.PostAddPythonEdbReq
  1059. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1060. if err != nil {
  1061. br.Msg = "参数解析异常!"
  1062. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1063. return
  1064. }
  1065. //传入操作人
  1066. req.AdminId = sysUser.AdminId
  1067. req.AdminName = sysUser.RealName
  1068. reqJson, err := json.Marshal(req)
  1069. if err != nil {
  1070. br.Msg = "参数解析异常!"
  1071. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1072. return
  1073. }
  1074. // 判断指标名称是否重复
  1075. {
  1076. var condition string
  1077. var pars []interface{}
  1078. condition += " AND edb_name=? "
  1079. pars = append(pars, req.EdbName)
  1080. condition += " AND edb_info_id<>? "
  1081. pars = append(pars, req.EdbInfoId)
  1082. count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
  1083. if err != nil {
  1084. br.Msg = "判断指标名称是否存在失败"
  1085. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  1086. return
  1087. }
  1088. if count > 0 {
  1089. br.Msg = "指标名称已存在,请重新填写"
  1090. br.ErrMsg = "指标名称已存在,请重新填写"
  1091. br.IsSendEmail = false
  1092. return
  1093. }
  1094. }
  1095. //获取指标信息
  1096. edbInfo, err := data_manage.GetEdbInfoById(req.EdbInfoId)
  1097. if err != nil {
  1098. if err.Error() == utils.ErrNoRow() {
  1099. br.Msg = "指标已被删除,请刷新页面"
  1100. br.ErrMsg = "指标已被删除,请刷新页面:Err:" + err.Error()
  1101. return
  1102. }
  1103. br.Msg = "获取指标信息失败"
  1104. br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
  1105. return
  1106. }
  1107. // 修改python运算指标
  1108. respItem, err := data.EditPythonEdbData(string(reqJson))
  1109. if err != nil {
  1110. br.Msg = "保存失败"
  1111. br.ErrMsg = "保存失败,Err:" + err.Error()
  1112. return
  1113. }
  1114. if respItem.Ret != 200 {
  1115. br.Msg = "保存失败"
  1116. br.ErrMsg = respItem.ErrMsg + ";EdbName:" + req.EdbName
  1117. return
  1118. }
  1119. //添加es
  1120. data.AddOrEditEdbInfoToEs(req.EdbInfoId)
  1121. resp := new(data_manage.AddEdbInfoResp)
  1122. resp.EdbInfoId = req.EdbInfoId
  1123. resp.UniqueCode = edbInfo.UniqueCode
  1124. br.Ret = 200
  1125. br.Success = true
  1126. br.Data = resp
  1127. br.Msg = "保存成功"
  1128. br.IsAddLog = true
  1129. }
  1130. // GetPythonEdbCode
  1131. // @Title 获取python代码
  1132. // @Description 获取python代码接口
  1133. // @Param EdbInfoId query int true "指标id"
  1134. // @Success 200 {object} data_manage.PythonEdbDetailResp
  1135. // @router /edb_info/python/detail [get]
  1136. func (this *ChartInfoController) GetPythonEdbCode() {
  1137. br := new(models.BaseResponse).Init()
  1138. defer func() {
  1139. this.Data["json"] = br
  1140. this.ServeJSON()
  1141. }()
  1142. sysUser := this.SysUser
  1143. if sysUser == nil {
  1144. br.Msg = "请登录"
  1145. br.ErrMsg = "请登录,SysUser Is Empty"
  1146. br.Ret = 408
  1147. return
  1148. }
  1149. edbInfoId, _ := this.GetInt("EdbInfoId")
  1150. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  1151. if err != nil {
  1152. br.Msg = "获取失败"
  1153. br.ErrMsg = "获取失败,Err:" + err.Error()
  1154. return
  1155. }
  1156. if edbInfo.Source != utils.DATA_SOURCE_PYTHON {
  1157. br.Msg = "该指标不是Python运算指标"
  1158. br.ErrMsg = "该指标不是Python运算指标"
  1159. return
  1160. }
  1161. pythonCode, err := data_manage.GetEdbPythonCodeById(edbInfoId)
  1162. if err != nil {
  1163. br.Msg = "获取失败"
  1164. br.ErrMsg = "获取失败,Err:" + err.Error()
  1165. return
  1166. }
  1167. resp := data_manage.PythonEdbDetailResp{
  1168. EdbInfoDetail: edbInfo,
  1169. PythonCode: pythonCode.PythonCode,
  1170. }
  1171. br.Ret = 200
  1172. br.Success = true
  1173. br.Msg = "保存成功"
  1174. br.Data = resp
  1175. }
  1176. // QueryEdbDataTable
  1177. // @Title 指标筛选接口
  1178. // @Description 指标筛选接口
  1179. // @Param EdbInfoId query int false "指标id"
  1180. // @Success 200 {object} data_manage.EdbInfoList
  1181. // @router /edb_data/table/detail [get]
  1182. func (this *EdbInfoController) QueryEdbDataTable() {
  1183. br := new(models.BaseResponse).Init()
  1184. defer func() {
  1185. this.Data["json"] = br
  1186. this.ServeJSON()
  1187. }()
  1188. edbInfoId, _ := this.GetInt("EdbInfoId")
  1189. if edbInfoId <= 0 {
  1190. br.Msg = "请选择指标"
  1191. br.ErrMsg = "请选择指标"
  1192. return
  1193. }
  1194. edbInfo, err := data_manage.GetEdbInfoById(edbInfoId)
  1195. if err != nil {
  1196. br.Msg = "获取指标信息失败"
  1197. br.ErrMsg = "获取指标信息失败,err:" + err.Error()
  1198. return
  1199. }
  1200. tableName := data_manage.GetEdbDataTableName(edbInfo.Source)
  1201. columnList := []map[string]string{
  1202. {
  1203. "edb_code": "指标code",
  1204. },
  1205. {
  1206. "data_time": "指标日期",
  1207. },
  1208. {
  1209. "value": "指标对应的值",
  1210. },
  1211. }
  1212. 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)
  1213. info := data_manage.TableInfoResp{
  1214. ColumnList: columnList,
  1215. TableName: tableName,
  1216. TemplateStr: url.PathEscape(templateStr),
  1217. }
  1218. br.Ret = 200
  1219. br.Success = true
  1220. br.Msg = "获取成功"
  1221. br.Data = info
  1222. }
  1223. // SaveAdjustEdbInfo
  1224. // @Title 保存数据调整接口
  1225. // @Description 保存数据调整接口
  1226. // @Param request body data_manage.AddPythonEdbReq true "type json string"
  1227. // @Success 200 {object} data_manage.AddEdbInfoResp
  1228. // @router /edb_info/adjust/save [post]
  1229. func (this *ChartInfoController) SaveAdjustEdbInfo() {
  1230. br := new(models.BaseResponse).Init()
  1231. defer func() {
  1232. this.Data["json"] = br
  1233. this.ServeJSON()
  1234. }()
  1235. sysUser := this.SysUser
  1236. if sysUser == nil {
  1237. br.Msg = "请登录"
  1238. br.ErrMsg = "请登录,SysUser Is Empty"
  1239. br.Ret = 408
  1240. return
  1241. }
  1242. var req data_manage.SaveAdjustEdbInfoReq
  1243. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1244. if err != nil {
  1245. br.Msg = "参数解析异常!"
  1246. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1247. return
  1248. }
  1249. //传入操作人
  1250. req.AdminId = sysUser.AdminId
  1251. req.AdminName = sysUser.RealName
  1252. reqJson, err := json.Marshal(req)
  1253. if err != nil {
  1254. br.Msg = "参数解析异常!"
  1255. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1256. return
  1257. }
  1258. // 判断指标名称是否重复
  1259. {
  1260. var condition string
  1261. var pars []interface{}
  1262. condition += " AND edb_name=? "
  1263. pars = append(pars, req.EdbName)
  1264. condition += " AND edb_info_id<>? "
  1265. pars = append(pars, req.EdbInfoId)
  1266. count, err := data_manage.GetEdbInfoCountByCondition(condition, pars)
  1267. if err != nil {
  1268. br.Msg = "判断指标名称是否存在失败"
  1269. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  1270. return
  1271. }
  1272. if count > 0 {
  1273. br.Msg = "指标名称已存在,请重新填写"
  1274. br.ErrMsg = "指标名称已存在,请重新填写"
  1275. br.IsSendEmail = false
  1276. return
  1277. }
  1278. }
  1279. // 添加数据调整指标
  1280. respItem, err := data.SaveAdjustEdbInfo(string(reqJson))
  1281. if err != nil {
  1282. br.Msg = "添加失败"
  1283. br.ErrMsg = "获取失败,Err:" + err.Error()
  1284. return
  1285. }
  1286. if respItem.Ret != 200 {
  1287. br.Msg = "添加失败"
  1288. br.ErrMsg = respItem.ErrMsg + ";EdbName:" + req.EdbName
  1289. return
  1290. }
  1291. respData := respItem.Data.(map[string]interface{})
  1292. tmpEdbInfoId, ok := respData["EdbInfoId"]
  1293. if !ok {
  1294. br.Msg = `数据转换异常`
  1295. br.ErrMsg = fmt.Sprint("respData.EdbInfoId 数据转换异常;respData:", respData)
  1296. return
  1297. }
  1298. //指标id
  1299. edbInfoId := int64(tmpEdbInfoId.(float64))
  1300. uniqueCode, ok := respData["UniqueCode"]
  1301. if !ok {
  1302. br.Msg = `数据转换异常`
  1303. br.ErrMsg = fmt.Sprint("respData.UniqueCode 数据转换异常;respData:", respData)
  1304. return
  1305. }
  1306. //添加es
  1307. data.AddOrEditEdbInfoToEs(int(edbInfoId))
  1308. //
  1309. resp := data_manage.AddEdbInfoResp{
  1310. EdbInfoId: int(edbInfoId),
  1311. UniqueCode: uniqueCode.(string),
  1312. }
  1313. br.Ret = 200
  1314. br.Success = true
  1315. br.Msg = "保存成功"
  1316. br.Data = resp
  1317. br.IsAddLog = true
  1318. }
  1319. // BatchCalculateBatchSave
  1320. // @Title 批量添加 累计值转月-同比值-同差等计算新增
  1321. // @Description 批量添加 累计值转月-同比值-同差等计算新增接口
  1322. // @Param request body data_manage.EdbInfoCalculateBatchSaveReq true "type json string"
  1323. // @Success Ret=200 返回指标id
  1324. // @router /edb_info/calculate/batch/save/batch [post]
  1325. func (this *ChartInfoController) BatchCalculateBatchSave() {
  1326. br := new(models.BaseResponse).Init()
  1327. defer func() {
  1328. this.Data["json"] = br
  1329. this.ServeJSON()
  1330. }()
  1331. sysUser := this.SysUser
  1332. if sysUser == nil {
  1333. br.Msg = "请登录"
  1334. br.ErrMsg = "请登录,SysUser Is Empty"
  1335. br.Ret = 408
  1336. return
  1337. }
  1338. var reqList []data_manage.BatchEdbInfoCalculateBatchSaveReq
  1339. err := json.Unmarshal(this.Ctx.Input.RequestBody, &reqList)
  1340. if err != nil {
  1341. br.Msg = "参数解析异常!"
  1342. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1343. return
  1344. }
  1345. resp := data_manage.BatchEdbInfoCalculateBatchSaveResp{
  1346. Fail: make([]data_manage.BatchEdbInfoCalculateBatchSaveFailResp, 0),
  1347. Success: make([]data_manage.BatchEdbInfoCalculateBatchSaveSuccessResp, 0),
  1348. }
  1349. redisKeyList := make([]string, 0) //需要清理的缓存key列表
  1350. defer func() {
  1351. for _, redisKey := range redisKeyList {
  1352. utils.Rc.Delete(redisKey)
  1353. }
  1354. }()
  1355. for _, v := range reqList {
  1356. req := v.CalculateInfo
  1357. req.EdbName = strings.Trim(req.EdbName, " ")
  1358. if req.EdbName == "" {
  1359. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1360. CalculateId: v.CalculateId,
  1361. Msg: "指标名称不能为空",
  1362. })
  1363. continue
  1364. }
  1365. if req.Frequency == "" {
  1366. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1367. CalculateId: v.CalculateId,
  1368. Msg: "频率不能为空",
  1369. })
  1370. continue
  1371. }
  1372. if req.Unit == "" {
  1373. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1374. CalculateId: v.CalculateId,
  1375. Msg: "单位不能为空",
  1376. })
  1377. continue
  1378. }
  1379. if req.ClassifyId <= 0 {
  1380. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1381. CalculateId: v.CalculateId,
  1382. Msg: "请选择分类",
  1383. })
  1384. continue
  1385. }
  1386. fromEdbInfoId := req.FromEdbInfoId
  1387. var formulaInt int
  1388. switch req.Source {
  1389. 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:
  1390. if req.Formula == "" {
  1391. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1392. CalculateId: v.CalculateId,
  1393. Msg: "请选择分类",
  1394. })
  1395. continue
  1396. }
  1397. formulaInt, _ = strconv.Atoi(req.Formula)
  1398. if formulaInt <= 0 {
  1399. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1400. CalculateId: v.CalculateId,
  1401. Msg: "N值输入错误,请重新输入",
  1402. })
  1403. continue
  1404. }
  1405. case utils.DATA_SOURCE_CALCULATE_ZJPJ:
  1406. //直接拼接指标
  1407. //校验时间格式
  1408. _, err = time.ParseInLocation(utils.FormatDate, req.Formula, time.Local)
  1409. if err != nil {
  1410. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1411. CalculateId: v.CalculateId,
  1412. Msg: "拼接日期有误,请重新输入",
  1413. })
  1414. continue
  1415. }
  1416. case utils.DATA_SOURCE_CALCULATE_NHCC: //拟合残差指标
  1417. //指标校验
  1418. if len(req.EdbInfoIdArr) != 2 {
  1419. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1420. CalculateId: v.CalculateId,
  1421. Msg: "选择的指标异常,请重新选择",
  1422. })
  1423. continue
  1424. }
  1425. fromEdbInfoId = req.EdbInfoIdArr[0].EdbInfoId
  1426. //校验时间格式
  1427. //数据格式:2022-11-01,2022-11-10
  1428. timeList := strings.Split(req.Formula, ",")
  1429. if len(timeList) != 2 {
  1430. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1431. CalculateId: v.CalculateId,
  1432. Msg: "选择时间有误,请重新输入",
  1433. })
  1434. continue
  1435. }
  1436. startDate, err := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
  1437. if err != nil {
  1438. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1439. CalculateId: v.CalculateId,
  1440. Msg: "开始日期有误,请重新输入",
  1441. })
  1442. continue
  1443. }
  1444. endDate, err := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
  1445. if err != nil {
  1446. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1447. CalculateId: v.CalculateId,
  1448. Msg: "结束日期有误,请重新输入",
  1449. })
  1450. continue
  1451. }
  1452. if utils.GetTimeSubDay(startDate, endDate) < 2 {
  1453. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1454. CalculateId: v.CalculateId,
  1455. Msg: "日期间隔不得少于两天",
  1456. })
  1457. continue
  1458. }
  1459. }
  1460. if fromEdbInfoId <= 0 {
  1461. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1462. CalculateId: v.CalculateId,
  1463. Msg: "请选择指标",
  1464. })
  1465. continue
  1466. }
  1467. //加入缓存机制,避免创建同一个名称的指标 start
  1468. redisKey := fmt.Sprint("edb_info:calculate:batch:save:", sysUser.AdminId, ":", req.Source, ":", v.CalculateId)
  1469. isExist := utils.Rc.IsExist(redisKey)
  1470. if isExist {
  1471. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1472. CalculateId: v.CalculateId,
  1473. Msg: "指标正在处理,请勿重复提交",
  1474. })
  1475. continue
  1476. } else {
  1477. //设置3分钟缓存
  1478. utils.Rc.SetNX(redisKey, 1, time.Second*300)
  1479. redisKeyList = append(redisKeyList, redisKey)
  1480. }
  1481. //加入缓存机制,避免创建同一个名称的指标 end
  1482. req2 := &data_manage.EdbInfoCalculateBatchSaveReqByEdbLib{
  1483. AdminId: sysUser.AdminId,
  1484. AdminName: sysUser.RealName,
  1485. EdbInfoId: req.EdbInfoId,
  1486. EdbName: req.EdbName,
  1487. Frequency: req.Frequency,
  1488. Unit: req.Unit,
  1489. ClassifyId: req.ClassifyId,
  1490. Formula: req.Formula, //N数值移动平均计算、环比值、环差值
  1491. FromEdbInfoId: req.FromEdbInfoId,
  1492. CalculateFormula: req.CalculateFormula,
  1493. Source: req.Source,
  1494. MoveType: req.MoveType,
  1495. MoveFrequency: req.MoveFrequency,
  1496. //CalculateFormula: edbInfo.CalculateFormula,
  1497. EdbInfoIdArr: req.EdbInfoIdArr,
  1498. Calendar: req.Calendar,
  1499. }
  1500. // 调用指标库去更新
  1501. reqJson, err := json.Marshal(req2)
  1502. if err != nil {
  1503. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1504. CalculateId: v.CalculateId,
  1505. Msg: "参数解析异常!",
  1506. ErrMsg: "参数解析失败,Err:" + err.Error(),
  1507. })
  1508. continue
  1509. }
  1510. respItem, err := data.BatchSaveEdbCalculateData(string(reqJson))
  1511. if err != nil {
  1512. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1513. CalculateId: v.CalculateId,
  1514. Msg: "新增失败!",
  1515. ErrMsg: "新增失败,Err:" + err.Error(),
  1516. })
  1517. continue
  1518. }
  1519. if respItem.Ret != 200 {
  1520. resp.Fail = append(resp.Fail, data_manage.BatchEdbInfoCalculateBatchSaveFailResp{
  1521. CalculateId: v.CalculateId,
  1522. Msg: respItem.Msg,
  1523. ErrMsg: respItem.ErrMsg,
  1524. })
  1525. continue
  1526. }
  1527. result := respItem.Data
  1528. //添加es
  1529. data.AddOrEditEdbInfoToEs(result.EdbInfoId)
  1530. resp.Success = append(resp.Success, data_manage.BatchEdbInfoCalculateBatchSaveSuccessResp{
  1531. ClassifyId: req.ClassifyId,
  1532. CalculateId: v.CalculateId,
  1533. EdbInfoId: result.EdbInfoId,
  1534. UniqueCode: result.UniqueCode,
  1535. })
  1536. }
  1537. br.Ret = 200
  1538. br.Success = true
  1539. br.Msg = "批量保存成功"
  1540. br.Data = resp
  1541. br.IsAddLog = true
  1542. }
  1543. // CalculateMapping
  1544. // @Title 获取计算指标与基础指标关联
  1545. // @Description 获取计算指标与基础指标关联
  1546. // @Param EdbInfoId query int true "指标id"
  1547. // @Success 200 {object} data_manage.CalculateDetailResp
  1548. // @router /edb_info/calculate/mapping [get]
  1549. func (this *ChartInfoController) CalculateMapping() {
  1550. br := new(models.BaseResponse).Init()
  1551. defer func() {
  1552. this.Data["json"] = br
  1553. this.ServeJSON()
  1554. }()
  1555. sysUser := this.SysUser
  1556. if sysUser == nil {
  1557. br.Msg = "请登录"
  1558. br.ErrMsg = "请登录,SysUser Is Empty"
  1559. br.Ret = 408
  1560. return
  1561. }
  1562. edbInfoId, _ := this.GetInt("EdbInfoId")
  1563. if edbInfoId <= 0 {
  1564. br.Msg = "参数有误"
  1565. return
  1566. }
  1567. item, e := data_manage.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1568. if e != nil {
  1569. br.Msg = "获取失败"
  1570. br.ErrMsg = "获取失败,Err:" + e.Error()
  1571. return
  1572. }
  1573. br.Ret = 200
  1574. br.Success = true
  1575. br.Msg = "保存成功"
  1576. br.Data = item
  1577. }