base_from_calculate.go 34 KB

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