base_from_calculate.go 48 KB

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