base_from_calculate.go 32 KB

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