base_from_calculate.go 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. package services
  2. import (
  3. "eta/eta_index_lib/logic"
  4. "eta/eta_index_lib/models"
  5. "eta/eta_index_lib/utils"
  6. "fmt"
  7. "strconv"
  8. "strings"
  9. "time"
  10. )
  11. // EdbCalculateBatchSave 批量指标运算
  12. func EdbCalculateBatchSave(req models.EdbInfoCalculateBatchSaveReq, lang string) (edbInfo *models.EdbInfo, uniqueCode string, err error, errMsg string) {
  13. // 校验参数
  14. req.EdbName = strings.Trim(req.EdbName, " ")
  15. if req.EdbName == "" {
  16. err = fmt.Errorf("指标名称不能为空")
  17. return
  18. }
  19. if req.Frequency == "" {
  20. err = fmt.Errorf("频率不能为空")
  21. return
  22. }
  23. if req.Unit == "" {
  24. err = fmt.Errorf("单位不能为空")
  25. return
  26. }
  27. if req.ClassifyId <= 0 {
  28. err = fmt.Errorf("请选择分类")
  29. return
  30. }
  31. // 基础指标id
  32. fromEdbInfoId := req.FromEdbInfoId
  33. var formulaInt int
  34. var nhccDate models.NhccDate // 拟合残差的日期
  35. switch req.Source {
  36. 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:
  37. if req.Formula == "" {
  38. err = fmt.Errorf("请填写N值")
  39. return
  40. }
  41. formulaInt, _ = strconv.Atoi(req.Formula)
  42. if formulaInt <= 0 {
  43. err = fmt.Errorf(" N值输入错误,请重新输入")
  44. return
  45. }
  46. case utils.DATA_SOURCE_CALCULATE_ZJPJ:
  47. //直接拼接指标
  48. //校验时间格式
  49. _, err = time.ParseInLocation(utils.FormatDate, req.Formula, time.Local)
  50. if err != nil {
  51. err = fmt.Errorf("拼接日期有误,请重新输入")
  52. return
  53. }
  54. case utils.DATA_SOURCE_CALCULATE_NHCC: //拟合残差指标
  55. //指标校验
  56. if len(req.EdbInfoIdArr) != 2 {
  57. err = fmt.Errorf("选择的指标异常,请重新选择")
  58. return
  59. }
  60. fromEdbInfoId = req.EdbInfoIdArr[0].EdbInfoId
  61. //校验时间格式
  62. //数据格式:2022-11-01,2022-11-10
  63. timeList := strings.Split(req.Formula, ",")
  64. if len(timeList) != 2 {
  65. err = fmt.Errorf("选择时间有误,请重新输入")
  66. return
  67. }
  68. startDate, e := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
  69. if e != nil {
  70. err = fmt.Errorf("开始日期有误,请重新输入")
  71. return
  72. }
  73. endDate, e := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
  74. if e != nil {
  75. err = fmt.Errorf("结束日期有误,请重新输入")
  76. return
  77. }
  78. if utils.GetTimeSubDay(startDate, endDate) < 2 {
  79. err = fmt.Errorf("日期间隔不得少于两天")
  80. return
  81. }
  82. nhccDate.StartDate = startDate
  83. nhccDate.EndDate = endDate
  84. case utils.DATA_SOURCE_CALCULATE_JP:
  85. if req.Formula != "期末值" && req.Formula != "平均值" {
  86. err = fmt.Errorf("数据取值类型错误:" + req.Formula)
  87. return
  88. }
  89. case utils.DATA_SOURCE_CALCULATE_ZSXY: // 指数修匀
  90. if req.Formula == "" {
  91. err = fmt.Errorf("请填写alpha值")
  92. return
  93. }
  94. a, e := strconv.ParseFloat(req.Formula, 64)
  95. if e != nil {
  96. err = fmt.Errorf("alpha值输入错误, 请重新输入")
  97. return
  98. }
  99. if a <= 0 || a >= 1 {
  100. err = fmt.Errorf("alpha值输入错误, 请重新输入")
  101. return
  102. }
  103. }
  104. notNeedFromEdbSourceList := []int{utils.DATA_SOURCE_CALCULATE_KSZS, utils.DATA_SOURCE_CALCULATE_CORRELATION, utils.DATA_SOURCE_CALCULATE_ZDYFX, utils.DATA_SOURCE_CALCULATE_SUM, utils.DATA_SOURCE_CALCULATE_AVG} // 不需要传入来源指标id的 指标类型
  105. if fromEdbInfoId <= 0 && !utils.InArrayByInt(notNeedFromEdbSourceList, req.Source) {
  106. err = fmt.Errorf("请选择指标")
  107. return
  108. }
  109. //加入缓存机制,避免创建同一个名称的指标 start
  110. redisKey := fmt.Sprint("edb_lib:edb_info:calculate:batch:save:", req.Source, ":", req.EdbName)
  111. isExist := utils.Rc.IsExist(redisKey)
  112. if isExist {
  113. err = fmt.Errorf("指标正在处理,请勿重复提交")
  114. return
  115. } else {
  116. //设置3分钟缓存
  117. utils.Rc.SetNX(redisKey, 1, time.Second*300)
  118. defer func() {
  119. _ = utils.Rc.Delete(redisKey)
  120. }()
  121. }
  122. //加入缓存机制,避免创建同一个名称的指标 end
  123. var condition string
  124. var pars []interface{}
  125. condition += " AND edb_name=? "
  126. pars = append(pars, req.EdbName)
  127. count, err := models.GetEdbInfoCountByCondition(condition, pars)
  128. if err != nil {
  129. err = fmt.Errorf("判断指标名称是否存在失败")
  130. errMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  131. return
  132. }
  133. if count > 0 {
  134. err = fmt.Errorf("指标名称已存在,请重新填写")
  135. errMsg = "指标名称已存在,请重新填写"
  136. return
  137. }
  138. // 来源指标
  139. var fromEdbInfo *models.EdbInfo
  140. if fromEdbInfoId > 0 {
  141. fromEdbInfo, err = models.GetEdbInfoById(fromEdbInfoId)
  142. if err != nil {
  143. errMsg = "获取指标信息失败:Err:" + err.Error()
  144. err = fmt.Errorf("获取指标信息失败")
  145. return
  146. }
  147. }
  148. //生成指标编码
  149. randStr := utils.GetRandDigit(4)
  150. edbCode := `C` + time.Now().Format("060102") + randStr
  151. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  152. uniqueCode = utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  153. sysUserId := req.AdminId
  154. sysUserName := req.AdminName
  155. var sourName string
  156. var edbInfoId int
  157. var baseEdbInfoModel models.BaseEdbInfoInterface
  158. addParam := models.AddCalculateBatchParams{
  159. Req: &req,
  160. FromEdbInfo: fromEdbInfo,
  161. EdbCode: edbCode,
  162. UniqueCode: uniqueCode,
  163. SysUserId: sysUserId,
  164. SysUserRealName: sysUserName,
  165. }
  166. switch req.Source {
  167. case utils.DATA_SOURCE_CALCULATE:
  168. sourName = "计算指标"
  169. req2 := models.EdbInfoCalculateSaveReq{
  170. AdminId: sysUserId,
  171. AdminName: sysUserName,
  172. EdbName: req.EdbName,
  173. Frequency: req.Frequency,
  174. Unit: req.Unit,
  175. ClassifyId: req.ClassifyId,
  176. CalculateFormula: req.CalculateFormula,
  177. //EdbInfoIdArr: req.EdbInfoIdArr,
  178. EmptyType: req.EmptyType,
  179. MaxEmptyType: req.MaxEmptyType,
  180. Extra: req.Extra,
  181. }
  182. // 放入A指标
  183. t := models.EdbInfoFromTag{
  184. EdbInfoId: req.FromEdbInfoId,
  185. FromTag: "A",
  186. }
  187. req2.EdbInfoIdArr = append(req2.EdbInfoIdArr, t)
  188. // 放入B指标
  189. if len(req.EdbInfoIdArr) > 0 {
  190. t = models.EdbInfoFromTag{
  191. EdbInfoId: req.EdbInfoIdArr[0].EdbInfoId,
  192. FromTag: "B",
  193. }
  194. req2.EdbInfoIdArr = append(req2.EdbInfoIdArr, t)
  195. }
  196. edbInfo, _, err, errMsg = EdbCalculateAdd(req2, lang)
  197. case utils.DATA_SOURCE_CALCULATE_LJZZY:
  198. sourName = "累计值转月值"
  199. if fromEdbInfo.Frequency != "月度" {
  200. err = fmt.Errorf("请选择月度指标")
  201. return
  202. }
  203. edbInfo, err = models.AddCalculateLjzzy(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
  204. case utils.DATA_SOURCE_CALCULATE_TBZ:
  205. if req.Frequency != fromEdbInfo.Frequency {
  206. err = fmt.Errorf("当前频度和原指标频度不一致")
  207. return
  208. }
  209. if req.Unit != "无" {
  210. err = fmt.Errorf("单位只允许为无,禁止选择其他单位")
  211. return
  212. }
  213. sourName = "同比值"
  214. edbInfo, err = models.AddCalculateTbz(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
  215. case utils.DATA_SOURCE_CALCULATE_TCZ:
  216. if req.Frequency != fromEdbInfo.Frequency {
  217. err = fmt.Errorf("当前频度和原指标频度不一致")
  218. return
  219. }
  220. if req.Unit != fromEdbInfo.Unit {
  221. err = fmt.Errorf("当前单位和原指标单位不一致")
  222. return
  223. }
  224. sourName = "同差值"
  225. edbInfo, err = models.AddCalculateTcz(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
  226. case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS:
  227. sourName = "N数值移动平均计算"
  228. edbInfo, err = models.AddCalculateNszydpjjs(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName, formulaInt)
  229. case utils.DATA_SOURCE_CALCULATE_HBZ:
  230. var condition string
  231. var pars []interface{}
  232. condition += " AND edb_info_id =? "
  233. pars = append(pars, fromEdbInfoId)
  234. condition += " AND value <=0 "
  235. checkCount, tmpErr := models.GetEdbDataCount(condition, pars, fromEdbInfo.Source, fromEdbInfo.SubSource)
  236. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  237. err = fmt.Errorf("判断环比值是否可计算失败")
  238. errMsg = "判断环比值是否可计算失败,Err:" + tmpErr.Error()
  239. return
  240. }
  241. if checkCount > 0 {
  242. err = fmt.Errorf("原始数据中存在0或负数,该指标不能进行环比运算")
  243. errMsg = "原始数据中出现0和负值时,提示该指标不能进行环比运算"
  244. return
  245. }
  246. sourName = "环比值"
  247. edbInfo, err = models.AddCalculateHbz(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName, formulaInt)
  248. case utils.DATA_SOURCE_CALCULATE_HCZ:
  249. sourName = "环差值"
  250. edbInfo, err = models.AddCalculateHcz(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName, formulaInt)
  251. case utils.DATA_SOURCE_CALCULATE_BP:
  252. sourName = utils.DATA_SOURCE_NAME_CALCULATE_BP
  253. edbInfo, err = models.AddCalculateBp(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
  254. case utils.DATA_SOURCE_CALCULATE_TIME_SHIFT:
  255. sourName = "时间移位"
  256. edbInfo, err = models.AddCalculateTimeShift(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
  257. case utils.DATA_SOURCE_CALCULATE_ZJPJ:
  258. sourName = "直接拼接"
  259. if len(req.EdbInfoIdArr) != 1 {
  260. err = fmt.Errorf("请传入拼接日期之后的指标")
  261. errMsg = "请传入拼接日期之后的指标"
  262. return
  263. }
  264. secondEdbInfoReq := req.EdbInfoIdArr[0]
  265. secondEdbInfo, tmpErr := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  266. if tmpErr != nil {
  267. err = fmt.Errorf("获取拼接日期之后的指标信息失败")
  268. errMsg = "获取拼接日期之后的指标信息失败:Err:" + tmpErr.Error()
  269. return
  270. }
  271. if fromEdbInfo.EdbInfoId == secondEdbInfo.EdbInfoId {
  272. err = fmt.Errorf("两个指标不允许为同一个")
  273. errMsg = "两个指标不允许为同一个"
  274. return
  275. }
  276. edbInfo, err = models.AddCalculateZjpj(&req, fromEdbInfo, secondEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
  277. case utils.DATA_SOURCE_CALCULATE_LJZTBPJ:
  278. sourName = "累计值同比拼接"
  279. if fromEdbInfo.Frequency != "月度" {
  280. err = fmt.Errorf("待拼接指标只能筛选月度指标")
  281. errMsg = "待拼接指标只能筛选月度指标"
  282. return
  283. }
  284. if len(req.EdbInfoIdArr) != 1 {
  285. err = fmt.Errorf("请传入同比值指标")
  286. errMsg = "请传入同比值指标"
  287. return
  288. }
  289. secondEdbInfoReq := req.EdbInfoIdArr[0]
  290. tbzEdbInfo, tmpErr := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  291. if tmpErr != nil {
  292. err = fmt.Errorf("获取同比值指标信息失败")
  293. errMsg = "获取同比值指标信息失败:Err:" + tmpErr.Error()
  294. return
  295. }
  296. if tbzEdbInfo.Source != utils.DATA_SOURCE_CALCULATE_TBZ {
  297. err = fmt.Errorf("指标必须是传入同比值指标类型")
  298. errMsg = "指标必须是传入同比值指标类型"
  299. return
  300. }
  301. if tbzEdbInfo.Frequency != "月度" {
  302. err = fmt.Errorf("同比值指标只能筛选月度指标")
  303. errMsg = "同比值指标只能筛选月度指标"
  304. return
  305. }
  306. if fromEdbInfo.EdbInfoId == tbzEdbInfo.EdbInfoId {
  307. err = fmt.Errorf("两个指标不允许为同一个")
  308. errMsg = "两个指标不允许为同一个"
  309. return
  310. }
  311. edbInfo, err = models.AddCalculateLjztbpj(&req, fromEdbInfo, tbzEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
  312. case utils.DATA_SOURCE_CALCULATE_CJJX:
  313. sourName = "超季节性"
  314. edbInfo, err = models.AddCalculateCjjx(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName, formulaInt)
  315. case utils.DATA_SOURCE_CALCULATE_NHCC:
  316. sourName = "拟合残差"
  317. secondEdbInfoReq := req.EdbInfoIdArr[1]
  318. secondEdbInfo, tmpErr := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  319. if tmpErr != nil {
  320. err = fmt.Errorf("获取因变量的指标信息失败")
  321. errMsg = "获取因变量的指标信息失败:Err:" + tmpErr.Error()
  322. return
  323. }
  324. if fromEdbInfo.EdbInfoId == secondEdbInfo.EdbInfoId {
  325. err = fmt.Errorf("两个指标不允许为同一个")
  326. errMsg = "两个指标不允许为同一个"
  327. return
  328. }
  329. edbInfo, err, errMsg = models.AddCalculateNhcc(&req, fromEdbInfo, secondEdbInfo, edbCode, uniqueCode, nhccDate, sysUserId, sysUserName)
  330. case utils.DATA_SOURCE_CALCULATE_JP:
  331. if !models.CheckFrequency(fromEdbInfo.Frequency, req.Frequency) {
  332. err = fmt.Errorf("频度异常,不允许低频降频到高频")
  333. return
  334. }
  335. sourName = utils.DATA_SOURCE_NAME_CALCULATE_JP
  336. edbInfo, err = models.AddCalculateJp(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
  337. case utils.DATA_SOURCE_CALCULATE_NH:
  338. sourName = utils.DATA_SOURCE_NAME_CALCULATE_NH
  339. edbInfo, err = models.AddCalculateNh(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
  340. case utils.DATA_SOURCE_CALCULATE_KSZS:
  341. //关联的指标信息
  342. if len(req.EdbInfoIdArr) < 2 {
  343. err = fmt.Errorf("指标数量不能小于2个,请重新选择")
  344. errMsg = "指标数量不能小于2个,请重新选择"
  345. //br.IsSendEmail = false
  346. return
  347. }
  348. sourName = utils.DATA_SOURCE_NAME_CALCULATE_KSZS
  349. edbInfo, err = models.AddCalculateKszs(&req, edbCode, uniqueCode, sysUserId, sysUserName)
  350. case utils.DATA_SOURCE_CALCULATE_CORRELATION:
  351. //关联的指标信息
  352. if len(req.EdbInfoIdArr) < 2 {
  353. err = fmt.Errorf("指标数量不能小于2个,请重新选择")
  354. errMsg = "指标数量不能小于2个,请重新选择"
  355. //br.IsSendEmail = false
  356. return
  357. }
  358. sourName = utils.DATA_SOURCE_NAME_CALCULATE_CORRELATION
  359. edbInfo, err, errMsg = models.AddCalculateCorrelation(&req, edbCode, uniqueCode, sysUserId, sysUserName)
  360. case utils.DATA_SOURCE_CALCULATE_RJZ:
  361. if req.Frequency == "日度" {
  362. err = fmt.Errorf("日度指标无需进行日均值计算")
  363. return
  364. }
  365. if req.Frequency != fromEdbInfo.Frequency {
  366. err = fmt.Errorf("当前频度和原指标频度不一致")
  367. return
  368. }
  369. if req.Unit != fromEdbInfo.Unit {
  370. err = fmt.Errorf("单位只允许为和原指标频度保持一致,禁止选择其他单位")
  371. return
  372. }
  373. sourName = "日均值"
  374. edbInfo, err = models.AddCalculateRjz(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
  375. default:
  376. // 获取通用的数据源处理服务
  377. baseEdbInfoModel = models.GetBaseEdbInfoModel(req.Source)
  378. // 没有找到的话,那么就直接返回报错吧
  379. if baseEdbInfoModel == nil {
  380. err = fmt.Errorf("无效计算方式")
  381. errMsg = "无效计算方式,source:" + strconv.Itoa(req.Source)
  382. return
  383. }
  384. sourName = baseEdbInfoModel.GetSourceName()
  385. edbInfo, err, errMsg = baseEdbInfoModel.Add(addParam)
  386. }
  387. if err != nil {
  388. errMsg = "生成" + sourName + "失败 Err:" + err.Error() + errMsg
  389. err = fmt.Errorf("生成" + sourName + "失败")
  390. return
  391. }
  392. if edbInfo == nil {
  393. err = fmt.Errorf("生成" + sourName + "失败")
  394. errMsg = "生成" + sourName + "失败,指标ID错误:" + strconv.Itoa(edbInfoId)
  395. return
  396. }
  397. edbInfoId = edbInfo.EdbInfoId
  398. //处理同名指标
  399. {
  400. edbNameList, tErr := models.GetEdbInfoByName(req.EdbName)
  401. if tErr != nil {
  402. err = fmt.Errorf("保存失败")
  403. errMsg = "获取指标信息失败,Err:" + tErr.Error()
  404. return
  405. }
  406. if len(edbNameList) >= 2 {
  407. for _, v := range edbNameList {
  408. edbName := v.EdbName + "(" + v.SourceName + ")"
  409. err = models.ModifyEdbInfoNameSource(edbName, v.EdbInfoId)
  410. if err != nil {
  411. errMsg = "修改指标名称失败,Err:" + err.Error()
  412. err = fmt.Errorf("保存失败")
  413. return
  414. }
  415. }
  416. }
  417. }
  418. // 更新指标最大最小值
  419. err, errMsg = models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  420. if err != nil {
  421. return
  422. }
  423. // 更新ES
  424. go logic.UpdateEs(edbInfo.EdbInfoId)
  425. return
  426. }
  427. // EdbCalculateBatchEdit 批量指标编辑
  428. func EdbCalculateBatchEdit(req models.EdbInfoCalculateBatchEditReq) (edbInfo *models.EdbInfo, uniqueCode string, err error, errMsg string) {
  429. // 校验参数
  430. req.EdbName = strings.Trim(req.EdbName, " ")
  431. if req.EdbName == "" {
  432. err = fmt.Errorf("指标名称不能为空")
  433. return
  434. }
  435. if req.Frequency == "" {
  436. err = fmt.Errorf("频率不能为空")
  437. return
  438. }
  439. if req.Unit == "" {
  440. err = fmt.Errorf("单位不能为空")
  441. return
  442. }
  443. if req.ClassifyId <= 0 {
  444. err = fmt.Errorf("请选择分类")
  445. return
  446. }
  447. if req.EdbInfoId <= 0 {
  448. err = fmt.Errorf("请选择指标")
  449. return
  450. }
  451. //加入缓存机制,避免创建同一个名称的指标 start
  452. redisKey := fmt.Sprint("edb_lib:edb_info:calculate:batch:save:", req.Source, ":", req.EdbName)
  453. isExist := utils.Rc.IsExist(redisKey)
  454. if isExist {
  455. err = fmt.Errorf("指标正在处理,请勿重复提交")
  456. return
  457. } else {
  458. //设置3分钟缓存
  459. utils.Rc.SetNX(redisKey, 1, time.Second*300)
  460. defer func() {
  461. _ = utils.Rc.Delete(redisKey)
  462. }()
  463. }
  464. //加入缓存机制,避免创建同一个名称的指标 end
  465. var condition string
  466. var pars []interface{}
  467. condition += " AND edb_name=? AND edb_info_id<>? "
  468. pars = append(pars, req.EdbName, req.EdbInfoId)
  469. count, err := models.GetEdbInfoCountByCondition(condition, pars)
  470. if err != nil {
  471. err = fmt.Errorf("判断指标名称是否存在失败")
  472. errMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  473. return
  474. }
  475. if count > 0 {
  476. err = fmt.Errorf("指标名称已存在,请重新填写")
  477. errMsg = "指标名称已存在,请重新填写"
  478. return
  479. }
  480. edbInfo, err = models.GetEdbInfoById(req.EdbInfoId)
  481. if err != nil {
  482. if err.Error() == utils.ErrNoRow() {
  483. errMsg = "指标已被删除,请刷新页面:Err:" + err.Error()
  484. err = fmt.Errorf("指标已被删除,请刷新页面")
  485. return
  486. }
  487. errMsg = "获取指标信息失败:Err:" + err.Error()
  488. err = fmt.Errorf("获取指标信息失败")
  489. return
  490. }
  491. // 基础指标id
  492. fromEdbInfoId := req.FromEdbInfoId
  493. if req.Source <= 0 {
  494. req.Source = edbInfo.Source
  495. }
  496. var formulaInt int
  497. var nhccDate models.NhccDate // 拟合残差的日期
  498. // 初期的参数校验
  499. switch req.Source {
  500. 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:
  501. if req.Formula == "" {
  502. err = fmt.Errorf("请填写N值")
  503. return
  504. }
  505. formulaInt, _ = strconv.Atoi(req.Formula)
  506. if formulaInt <= 0 {
  507. err = fmt.Errorf("N值输入错误,请重新输入")
  508. return
  509. }
  510. case utils.DATA_SOURCE_CALCULATE_ZJPJ:
  511. //直接拼接指标
  512. //校验时间格式
  513. _, err = time.ParseInLocation(utils.FormatDate, req.Formula, time.Local)
  514. if err != nil {
  515. err = fmt.Errorf("拼接日期有误,请重新输入")
  516. return
  517. }
  518. case utils.DATA_SOURCE_CALCULATE_NHCC: //拟合残差指标
  519. //指标校验
  520. if len(req.EdbInfoIdArr) != 2 {
  521. err = fmt.Errorf("选择的指标异常,请重新选择")
  522. return
  523. }
  524. fromEdbInfoId = req.EdbInfoIdArr[0].EdbInfoId
  525. //校验时间格式
  526. //数据格式:2022-11-01,2022-11-10
  527. timeList := strings.Split(req.Formula, ",")
  528. if len(timeList) != 2 {
  529. err = fmt.Errorf("选择时间有误,请重新输入")
  530. return
  531. }
  532. startDate, e := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
  533. if e != nil {
  534. err = fmt.Errorf("开始日期有误,请重新输入")
  535. return
  536. }
  537. endDate, e := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
  538. if e != nil {
  539. err = fmt.Errorf("结束日期有误,请重新输入")
  540. return
  541. }
  542. if utils.GetTimeSubDay(startDate, endDate) < 2 {
  543. err = fmt.Errorf("日期间隔不得少于两天")
  544. return
  545. }
  546. nhccDate.StartDate = startDate
  547. nhccDate.EndDate = endDate
  548. case utils.DATA_SOURCE_CALCULATE_JP:
  549. if req.Formula != "期末值" && req.Formula != "平均值" {
  550. err = fmt.Errorf("数据取值类型错误:" + req.Formula)
  551. return
  552. }
  553. case utils.DATA_SOURCE_CALCULATE_ZSXY: // 指数修匀
  554. if req.Formula == "" {
  555. err = fmt.Errorf("请填写alpha值")
  556. return
  557. }
  558. a, e := strconv.ParseFloat(req.Formula, 64)
  559. if e != nil {
  560. err = fmt.Errorf("alpha值输入错误, 请重新输入")
  561. return
  562. }
  563. if a <= 0 || a >= 1 {
  564. err = fmt.Errorf("alpha值输入错误, 请重新输入")
  565. return
  566. }
  567. }
  568. // 来源指标
  569. var fromEdbInfo *models.EdbInfo
  570. // 基础指标id
  571. if fromEdbInfoId > 0 {
  572. fromEdbInfo, err = models.GetEdbInfoById(fromEdbInfoId)
  573. if err != nil {
  574. errMsg = "获取指标信息失败:Err:" + err.Error()
  575. err = fmt.Errorf("获取指标信息失败")
  576. return
  577. }
  578. }
  579. var sourName string
  580. var edbInfoId int
  581. var baseEdbInfoModel models.BaseEdbInfoInterface
  582. editParams := models.EditCalculateBatchParams{
  583. Req: &req,
  584. EdbInfo: edbInfo,
  585. FromEdbInfo: fromEdbInfo,
  586. }
  587. switch req.Source {
  588. case utils.DATA_SOURCE_CALCULATE_LJZZY:
  589. sourName = "累计值转月值"
  590. if fromEdbInfo.Frequency != "月度" {
  591. err = fmt.Errorf("请选择月度指标")
  592. return
  593. }
  594. err = models.EditCalculateLjzzy(edbInfo, &req, fromEdbInfo)
  595. case utils.DATA_SOURCE_CALCULATE_TBZ:
  596. sourName = "同比值"
  597. err = models.EditCalculateTbz(edbInfo, &req, fromEdbInfo)
  598. case utils.DATA_SOURCE_CALCULATE_TCZ:
  599. fmt.Println("start edit", time.Now())
  600. sourName = "同差值"
  601. err = models.EditCalculateTcz(edbInfo, &req, fromEdbInfo)
  602. fmt.Println("end edit", time.Now())
  603. case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS:
  604. sourName = "N数值移动平均计算"
  605. err = models.EditCalculateNszydpjjs(edbInfo, &req, fromEdbInfo, formulaInt, edbInfo.CalculateFormula)
  606. case utils.DATA_SOURCE_CALCULATE_HBZ:
  607. var condition string
  608. var pars []interface{}
  609. condition += " AND edb_info_id =? "
  610. pars = append(pars, req.FromEdbInfoId)
  611. condition += " AND value <=0 "
  612. checkCount, tmpErr := models.GetEdbDataCount(condition, pars, fromEdbInfo.Source, fromEdbInfo.SubSource)
  613. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  614. errMsg = "判断环比值是否可计算失败,Err:" + tmpErr.Error()
  615. err = fmt.Errorf("判断环比值是否可计算失败")
  616. return
  617. }
  618. if checkCount > 0 {
  619. err = fmt.Errorf("原始数据中存在0或负数,该指标不能进行环比运算")
  620. errMsg = "原始数据中出现0和负值时,提示该指标不能进行环比运算"
  621. return
  622. }
  623. sourName = "环比值"
  624. err = models.EditCalculateHbz(edbInfo, &req, fromEdbInfo, formulaInt, edbInfo.CalculateFormula)
  625. case utils.DATA_SOURCE_CALCULATE_HCZ:
  626. sourName = "环差值"
  627. err = models.EditCalculateHcz(edbInfo, &req, fromEdbInfo, formulaInt, edbInfo.CalculateFormula)
  628. case utils.DATA_SOURCE_CALCULATE_BP:
  629. sourName = utils.DATA_SOURCE_NAME_CALCULATE_BP
  630. err = models.EditCalculateBp(edbInfo, &req, fromEdbInfo)
  631. case utils.DATA_SOURCE_CALCULATE_TIME_SHIFT:
  632. sourName = "时间移位"
  633. err = models.EditCalculateTimeShift(edbInfo, &req, fromEdbInfo)
  634. case utils.DATA_SOURCE_CALCULATE_ZJPJ:
  635. sourName = "直接拼接"
  636. if len(req.EdbInfoIdArr) != 1 {
  637. err = fmt.Errorf("请传入拼接日期之后的指标")
  638. errMsg = "请传入拼接日期之后的指标"
  639. return
  640. }
  641. secondEdbInfoReq := req.EdbInfoIdArr[0]
  642. secondEdbInfo, tmpErr := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  643. if tmpErr != nil {
  644. errMsg = "获取拼接日期之后的指标信息失败:Err:" + tmpErr.Error()
  645. err = fmt.Errorf("获取拼接日期之后的指标信息失败")
  646. return
  647. }
  648. if fromEdbInfo.EdbInfoId == secondEdbInfo.EdbInfoId {
  649. err = fmt.Errorf("两个指标不允许为同一个")
  650. errMsg = "两个指标不允许为同一个"
  651. return
  652. }
  653. err = models.EditCalculateZjpj(&req, edbInfo, fromEdbInfo, secondEdbInfo)
  654. case utils.DATA_SOURCE_CALCULATE_LJZTBPJ: //累计值同比拼接
  655. sourName = "累计值同比拼接"
  656. if fromEdbInfo.Frequency != "月度" {
  657. errMsg = "待拼接指标只能筛选月度指标"
  658. err = fmt.Errorf("待拼接指标只能筛选月度指标")
  659. return
  660. }
  661. if len(req.EdbInfoIdArr) != 1 {
  662. errMsg = "请传入同比值指标"
  663. err = fmt.Errorf("请传入同比值指标")
  664. return
  665. }
  666. secondEdbInfoReq := req.EdbInfoIdArr[0]
  667. tbzEdbInfo, tmpErr := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  668. if tmpErr != nil {
  669. errMsg = "获取同比值指标信息失败:Err:" + tmpErr.Error()
  670. err = fmt.Errorf("获取同比值指标信息失败")
  671. return
  672. }
  673. if tbzEdbInfo.Source != utils.DATA_SOURCE_CALCULATE_TBZ {
  674. err = fmt.Errorf("指标必须是传入同比值指标类型")
  675. errMsg = "指标必须是传入同比值指标类型"
  676. return
  677. }
  678. if tbzEdbInfo.Frequency != "月度" {
  679. err = fmt.Errorf("同比值指标只能筛选月度指标")
  680. errMsg = "同比值指标只能筛选月度指标"
  681. return
  682. }
  683. if fromEdbInfo.EdbInfoId == tbzEdbInfo.EdbInfoId {
  684. err = fmt.Errorf("两个指标不允许为同一个")
  685. errMsg = "两个指标不允许为同一个"
  686. return
  687. }
  688. err = models.EditCalculateLjztbpj(&req, edbInfo, fromEdbInfo, tbzEdbInfo)
  689. case utils.DATA_SOURCE_CALCULATE_CJJX:
  690. sourName = "超季节性"
  691. err = models.EditCalculateCjjx(&req, edbInfo, fromEdbInfo, formulaInt)
  692. case utils.DATA_SOURCE_CALCULATE_NHCC:
  693. sourName = "拟合残差"
  694. secondEdbInfoReq := req.EdbInfoIdArr[1]
  695. secondEdbInfo, tmpErr := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  696. if tmpErr != nil {
  697. errMsg = "获取因变量的指标信息失败:Err:" + tmpErr.Error()
  698. err = fmt.Errorf("获取因变量的指标信息失败")
  699. return
  700. }
  701. if fromEdbInfo.EdbInfoId == secondEdbInfo.EdbInfoId {
  702. err = fmt.Errorf("两个指标不允许为同一个")
  703. errMsg = "两个指标不允许为同一个"
  704. return
  705. }
  706. err, errMsg = models.EditCalculateNhcc(&req, edbInfo, fromEdbInfo, secondEdbInfo, nhccDate)
  707. case utils.DATA_SOURCE_CALCULATE_JP:
  708. if !models.CheckFrequency(fromEdbInfo.Frequency, req.Frequency) {
  709. err = fmt.Errorf("频度异常,不允许低频降频到高频")
  710. return
  711. }
  712. sourName = utils.DATA_SOURCE_NAME_CALCULATE_JP
  713. err = models.EditCalculateJp(edbInfo, &req, fromEdbInfo)
  714. case utils.DATA_SOURCE_CALCULATE_NH:
  715. sourName = utils.DATA_SOURCE_NAME_CALCULATE_NH
  716. err = models.EditCalculateNh(edbInfo, &req, fromEdbInfo)
  717. case utils.DATA_SOURCE_CALCULATE_KSZS:
  718. //关联的指标信息
  719. if len(req.EdbInfoIdArr) < 2 {
  720. err = fmt.Errorf("指标数量不能小于2个,请重新选择")
  721. errMsg = "指标数量不能小于2个,请重新选择"
  722. //br.IsSendEmail = false
  723. return
  724. }
  725. sourName = utils.DATA_SOURCE_NAME_CALCULATE_KSZS
  726. err = models.EditCalculateKszs(edbInfo, &req)
  727. case utils.DATA_SOURCE_CALCULATE_CORRELATION:
  728. //关联的指标信息
  729. if len(req.EdbInfoIdArr) < 2 {
  730. err = fmt.Errorf("指标数量不能小于2个,请重新选择")
  731. errMsg = "指标数量不能小于2个,请重新选择"
  732. //br.IsSendEmail = false
  733. return
  734. }
  735. sourName = utils.DATA_SOURCE_NAME_CALCULATE_CORRELATION
  736. err, errMsg = models.EditCalculateCorrelation(edbInfo, &req)
  737. case utils.DATA_SOURCE_CALCULATE_RJZ:
  738. if req.Frequency == "日度" {
  739. err = fmt.Errorf("日度指标无需进行日均值计算")
  740. return
  741. }
  742. if req.Frequency != fromEdbInfo.Frequency {
  743. err = fmt.Errorf("当前频度和原指标频度不一致")
  744. return
  745. }
  746. if req.Unit != fromEdbInfo.Unit {
  747. err = fmt.Errorf("单位只允许为和原指标频度保持一致,禁止选择其他单位")
  748. return
  749. }
  750. sourName = "日均值"
  751. err = models.EditCalculateRjz(edbInfo, &req, fromEdbInfo)
  752. default:
  753. // 获取通用的数据源处理服务
  754. baseEdbInfoModel = models.GetBaseEdbInfoModel(req.Source)
  755. // 没有找到的话,那么就直接返回报错吧
  756. if baseEdbInfoModel == nil {
  757. err = fmt.Errorf("无效计算方式")
  758. errMsg = "无效计算方式,source:" + strconv.Itoa(req.Source)
  759. return
  760. }
  761. sourName = baseEdbInfoModel.GetSourceName()
  762. err, errMsg = baseEdbInfoModel.Edit(editParams)
  763. }
  764. if err != nil {
  765. errMsg = "生成" + sourName + "失败 Err:" + err.Error() + errMsg
  766. err = fmt.Errorf("生成" + sourName + "失败")
  767. return
  768. }
  769. if edbInfo == nil {
  770. err = fmt.Errorf("生成" + sourName + "失败")
  771. errMsg = "生成" + sourName + "失败,指标ID错误:" + strconv.Itoa(edbInfoId)
  772. return
  773. }
  774. edbInfoId = edbInfo.EdbInfoId
  775. uniqueCode = edbInfo.UniqueCode
  776. //处理同名指标
  777. {
  778. edbNameList, tErr := models.GetEdbInfoByName(req.EdbName)
  779. if tErr != nil {
  780. err = fmt.Errorf("保存失败")
  781. errMsg = "获取指标信息失败,Err:" + tErr.Error()
  782. return
  783. }
  784. if len(edbNameList) >= 2 {
  785. for _, v := range edbNameList {
  786. edbName := v.EdbName + "(" + v.SourceName + ")"
  787. err = models.ModifyEdbInfoNameSource(edbName, v.EdbInfoId)
  788. if err != nil {
  789. errMsg = "修改指标名称失败,Err:" + err.Error()
  790. err = fmt.Errorf("保存失败")
  791. return
  792. }
  793. }
  794. }
  795. }
  796. // 更新指标最大最小值
  797. err, errMsg = models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  798. if err != nil {
  799. return
  800. }
  801. // 更新ES
  802. go logic.UpdateEs(edbInfo.EdbInfoId)
  803. return
  804. }
  805. // EdbCalculateAdd 新增指标运算
  806. func EdbCalculateAdd(req models.EdbInfoCalculateSaveReq, lang string) (edbInfo *models.EdbInfo, uniqueCode string, err error, errMsg string) {
  807. calculateFormula := req.CalculateFormula
  808. calculateFormula = strings.Replace(calculateFormula, "(", "(", -1)
  809. calculateFormula = strings.Replace(calculateFormula, ")", ")", -1)
  810. calculateFormula = strings.Replace(calculateFormula, ",", ",", -1)
  811. calculateFormula = strings.Replace(calculateFormula, "。", ".", -1)
  812. calculateFormula = strings.Replace(calculateFormula, "%", "*0.01", -1)
  813. req.CalculateFormula = calculateFormula
  814. //判断是否重复指标
  815. edbInfoMap := make(map[int]string)
  816. //移除研究员选择指标中的未使用的指标
  817. {
  818. //转大写的计算公式
  819. upperCalculateFormulaStr := strings.ToUpper(req.CalculateFormula)
  820. //用到的指标
  821. newEdbInfoIdArr := make([]models.EdbInfoFromTag, 0)
  822. for _, tmpEdbInfo := range req.EdbInfoIdArr {
  823. _, ok := edbInfoMap[tmpEdbInfo.EdbInfoId]
  824. if ok {
  825. err = fmt.Errorf("选择指标失败,请勿选择重复指标!")
  826. return
  827. }
  828. edbInfoMap[tmpEdbInfo.EdbInfoId] = tmpEdbInfo.FromTag
  829. upperFromTag := strings.ToUpper(tmpEdbInfo.FromTag)
  830. if strings.Contains(upperCalculateFormulaStr, upperFromTag) {
  831. newEdbInfoIdArr = append(newEdbInfoIdArr, tmpEdbInfo)
  832. }
  833. }
  834. req.EdbInfoIdArr = newEdbInfoIdArr
  835. }
  836. // 校验指标名称是否存在
  837. existEdbName, err := logic.CheckExistByEdbNameAndEdbInfoId(0, 0, req.EdbName, lang)
  838. if err != nil {
  839. errMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  840. err = fmt.Errorf("判断指标名称是否存在失败")
  841. return
  842. }
  843. if existEdbName {
  844. errMsg = "指标名称已存在,请重新填写"
  845. err = fmt.Errorf(errMsg)
  846. //br.IsSendEmail = false
  847. return
  848. }
  849. //检验公式
  850. var formulaStr string
  851. var edbInfoIdBytes []string
  852. for _, v := range req.EdbInfoIdArr {
  853. formulaStr += v.FromTag + ","
  854. edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
  855. }
  856. formulaSlice, err := utils.CheckFormulaJson(req.CalculateFormula)
  857. if err != nil {
  858. err = fmt.Errorf("公式格式错误,请重新填写")
  859. return
  860. }
  861. for _, formula := range formulaSlice {
  862. formulaMap, e := utils.CheckFormula(formula)
  863. if e != nil {
  864. err = fmt.Errorf("公式错误,请重新填写")
  865. return
  866. }
  867. for _, v := range formulaMap {
  868. if !strings.Contains(formulaStr, v) {
  869. err = fmt.Errorf("公式错误,请重新填写")
  870. return
  871. }
  872. }
  873. }
  874. //关联的指标信息
  875. edbInfoList := make([]*models.EdbInfo, 0)
  876. //关联的指标数据表
  877. calculateMappingList := make([]*models.EdbInfoCalculateMapping, 0)
  878. for k, v := range req.EdbInfoIdArr {
  879. fromEdbInfo, e := models.GetEdbInfoById(v.EdbInfoId)
  880. if e != nil {
  881. if e.Error() == utils.ErrNoRow() {
  882. err = fmt.Errorf("生成计算指标失败")
  883. errMsg = "指标 " + strconv.Itoa(v.EdbInfoId) + " 不存在"
  884. return
  885. }
  886. err = fmt.Errorf("生成计算指标失败")
  887. errMsg = "获取指标失败:Err:" + e.Error()
  888. return
  889. }
  890. edbInfoList = append(edbInfoList, fromEdbInfo)
  891. //关联关系表
  892. {
  893. calculateMappingItem := &models.EdbInfoCalculateMapping{
  894. EdbInfoCalculateMappingId: 0,
  895. EdbInfoId: 0,
  896. Source: utils.DATA_SOURCE_CALCULATE,
  897. SourceName: "指标运算",
  898. EdbCode: "",
  899. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  900. FromEdbCode: fromEdbInfo.EdbCode,
  901. FromEdbName: fromEdbInfo.EdbName,
  902. FromSource: fromEdbInfo.Source,
  903. FromSourceName: fromEdbInfo.SourceName,
  904. FromTag: v.FromTag,
  905. Sort: k + 1,
  906. CreateTime: time.Now(),
  907. ModifyTime: time.Now(),
  908. }
  909. //calculateMappingItem.EdbCode = edbCode
  910. //calculateMappingItem.EdbInfoId = int(edbInfoId)
  911. calculateMappingList = append(calculateMappingList, calculateMappingItem)
  912. }
  913. }
  914. for _, v := range formulaSlice {
  915. formulaMap, e := utils.CheckFormula(v)
  916. if e != nil {
  917. err = fmt.Errorf("公式错误,请重新填写")
  918. return
  919. }
  920. //预先计算,判断公式是否正常
  921. ok, _ := models.CheckFormula2(edbInfoList, formulaMap, v, edbInfoIdBytes)
  922. if !ok {
  923. err = fmt.Errorf("生成计算指标失败,请使用正确的计算公式")
  924. return
  925. }
  926. }
  927. randStr := utils.GetRandDigit(4)
  928. edbCode := `C` + time.Now().Format("060102") + randStr
  929. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  930. uniqueCode = utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  931. // 新增计算指标
  932. edbInfo, err = models.AddCalculateInfo(req, calculateMappingList, edbInfoList, edbCode, uniqueCode, edbInfoIdBytes)
  933. if err != nil {
  934. errMsg = "生成计算指标失败,AddEdbInfo Err:" + err.Error()
  935. err = fmt.Errorf("生成计算指标失败")
  936. return
  937. }
  938. return
  939. }