base_from_calculate.go 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  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. "sort"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. // EdbCalculateBatchSave 批量指标运算
  13. func EdbCalculateBatchSave(req models.EdbInfoCalculateBatchSaveReq, lang string) (edbInfo *models.EdbInfo, uniqueCode string, err error, errMsg string) {
  14. // 校验参数
  15. req.EdbName = strings.Trim(req.EdbName, " ")
  16. if req.EdbName == "" {
  17. err = fmt.Errorf("指标名称不能为空")
  18. return
  19. }
  20. if req.Frequency == "" {
  21. err = fmt.Errorf("频率不能为空")
  22. return
  23. }
  24. if req.Unit == "" {
  25. err = fmt.Errorf("单位不能为空")
  26. return
  27. }
  28. if req.ClassifyId <= 0 {
  29. err = fmt.Errorf("请选择分类")
  30. return
  31. }
  32. // 基础指标id
  33. fromEdbInfoId := req.FromEdbInfoId
  34. var formulaInt int
  35. var nhccDate models.NhccDate // 拟合残差的日期
  36. switch req.Source {
  37. 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:
  38. if req.Formula == "" {
  39. err = fmt.Errorf("请填写N值")
  40. return
  41. }
  42. formulaInt, _ = strconv.Atoi(req.Formula)
  43. if formulaInt <= 0 {
  44. err = fmt.Errorf(" N值输入错误,请重新输入")
  45. return
  46. }
  47. case utils.DATA_SOURCE_CALCULATE_ZJPJ:
  48. //直接拼接指标
  49. //校验时间格式
  50. _, err = time.ParseInLocation(utils.FormatDate, req.Formula, time.Local)
  51. if err != nil {
  52. err = fmt.Errorf("拼接日期有误,请重新输入")
  53. return
  54. }
  55. case utils.DATA_SOURCE_CALCULATE_NHCC: //拟合残差指标
  56. //指标校验
  57. if len(req.EdbInfoIdArr) != 2 {
  58. err = fmt.Errorf("选择的指标异常,请重新选择")
  59. return
  60. }
  61. fromEdbInfoId = req.EdbInfoIdArr[0].EdbInfoId
  62. //校验时间格式
  63. //数据格式:2022-11-01,2022-11-10
  64. timeList := strings.Split(req.Formula, ",")
  65. if len(timeList) != 2 {
  66. err = fmt.Errorf("选择时间有误,请重新输入")
  67. return
  68. }
  69. startDate, e := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
  70. if e != nil {
  71. err = fmt.Errorf("开始日期有误,请重新输入")
  72. return
  73. }
  74. endDate, e := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
  75. if e != nil {
  76. err = fmt.Errorf("结束日期有误,请重新输入")
  77. return
  78. }
  79. if utils.GetTimeSubDay(startDate, endDate) < 2 {
  80. err = fmt.Errorf("日期间隔不得少于两天")
  81. return
  82. }
  83. nhccDate.StartDate = startDate
  84. nhccDate.EndDate = endDate
  85. case utils.DATA_SOURCE_CALCULATE_JP:
  86. if req.Formula != "期末值" && req.Formula != "平均值" {
  87. err = fmt.Errorf("数据取值类型错误:" + req.Formula)
  88. return
  89. }
  90. case utils.DATA_SOURCE_CALCULATE_ZSXY: // 指数修匀
  91. if req.Formula == "" {
  92. err = fmt.Errorf("请填写alpha值")
  93. return
  94. }
  95. a, e := strconv.ParseFloat(req.Formula, 64)
  96. if e != nil {
  97. err = fmt.Errorf("alpha值输入错误, 请重新输入")
  98. return
  99. }
  100. if a <= 0 || a >= 1 {
  101. err = fmt.Errorf("alpha值输入错误, 请重新输入")
  102. return
  103. }
  104. }
  105. 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的 指标类型
  106. if fromEdbInfoId <= 0 && !utils.InArrayByInt(notNeedFromEdbSourceList, req.Source) {
  107. err = fmt.Errorf("请选择指标")
  108. return
  109. }
  110. //加入缓存机制,避免创建同一个名称的指标 start
  111. redisKey := fmt.Sprint("edb_lib:edb_info:calculate:batch:save:", req.Source, ":", req.EdbName)
  112. isExist := utils.Rc.IsExist(redisKey)
  113. if isExist {
  114. err = fmt.Errorf("指标正在处理,请勿重复提交")
  115. return
  116. } else {
  117. //设置3分钟缓存
  118. utils.Rc.SetNX(redisKey, 1, time.Second*300)
  119. defer func() {
  120. _ = utils.Rc.Delete(redisKey)
  121. }()
  122. }
  123. //加入缓存机制,避免创建同一个名称的指标 end
  124. var condition string
  125. var pars []interface{}
  126. condition += " AND edb_name=? "
  127. pars = append(pars, req.EdbName)
  128. count, err := models.GetEdbInfoCountByCondition(condition, pars)
  129. if err != nil {
  130. err = fmt.Errorf("判断指标名称是否存在失败")
  131. errMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  132. return
  133. }
  134. if count > 0 {
  135. err = fmt.Errorf("指标名称已存在,请重新填写")
  136. errMsg = "指标名称已存在,请重新填写"
  137. return
  138. }
  139. // 来源指标
  140. var fromEdbInfo *models.EdbInfo
  141. if fromEdbInfoId > 0 {
  142. fromEdbInfo, err = models.GetEdbInfoById(fromEdbInfoId)
  143. if err != nil {
  144. errMsg = "获取指标信息失败:Err:" + err.Error()
  145. err = fmt.Errorf("获取指标信息失败")
  146. return
  147. }
  148. }
  149. // 记录旧的指标信息
  150. oldEdbName := fromEdbInfo.EdbName
  151. oldFrequency := fromEdbInfo.Frequency
  152. oldUnit := fromEdbInfo.Unit
  153. //生成指标编码
  154. edbCode, err := utils.GenerateEdbCode(1, "")
  155. if err != nil {
  156. errMsg = "指标编码生成失败:Err:" + err.Error()
  157. err = fmt.Errorf("指标生成失败")
  158. return
  159. }
  160. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  161. uniqueCode = utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  162. sysUserId := req.AdminId
  163. sysUserName := req.AdminName
  164. var sourName string
  165. var edbInfoId int
  166. var baseEdbInfoModel models.BaseEdbInfoInterface
  167. addParam := models.AddCalculateBatchParams{
  168. Req: &req,
  169. FromEdbInfo: fromEdbInfo,
  170. EdbCode: edbCode,
  171. UniqueCode: uniqueCode,
  172. SysUserId: sysUserId,
  173. SysUserRealName: sysUserName,
  174. }
  175. switch req.Source {
  176. case utils.DATA_SOURCE_CALCULATE:
  177. sourName = "计算指标"
  178. req2 := models.EdbInfoCalculateSaveReq{
  179. AdminId: sysUserId,
  180. AdminName: sysUserName,
  181. EdbName: req.EdbName,
  182. Frequency: req.Frequency,
  183. Unit: req.Unit,
  184. ClassifyId: req.ClassifyId,
  185. CalculateFormula: req.CalculateFormula,
  186. //EdbInfoIdArr: req.EdbInfoIdArr,
  187. EmptyType: req.EmptyType,
  188. MaxEmptyType: req.MaxEmptyType,
  189. Extra: req.Extra,
  190. }
  191. // 放入A指标
  192. t := models.EdbInfoFromTag{
  193. EdbInfoId: req.FromEdbInfoId,
  194. FromTag: "A",
  195. }
  196. req2.EdbInfoIdArr = append(req2.EdbInfoIdArr, t)
  197. // 放入B指标
  198. if len(req.EdbInfoIdArr) > 0 {
  199. t = models.EdbInfoFromTag{
  200. EdbInfoId: req.EdbInfoIdArr[0].EdbInfoId,
  201. FromTag: "B",
  202. }
  203. req2.EdbInfoIdArr = append(req2.EdbInfoIdArr, t)
  204. }
  205. edbInfo, _, err, errMsg = EdbCalculateAdd(req2, lang)
  206. case utils.DATA_SOURCE_CALCULATE_LJZZY:
  207. sourName = "累计值转月值"
  208. if fromEdbInfo.Frequency != "月度" {
  209. err = fmt.Errorf("请选择月度指标")
  210. return
  211. }
  212. edbInfo, err = models.AddCalculateLjzzy(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
  213. case utils.DATA_SOURCE_CALCULATE_TBZ:
  214. if req.Frequency != fromEdbInfo.Frequency {
  215. err = fmt.Errorf("当前频度和原指标频度不一致")
  216. return
  217. }
  218. if req.Unit != "无" {
  219. err = fmt.Errorf("单位只允许为无,禁止选择其他单位")
  220. return
  221. }
  222. sourName = "同比值"
  223. edbInfo, err = models.AddCalculateTbz(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
  224. case utils.DATA_SOURCE_CALCULATE_TCZ:
  225. if req.Frequency != fromEdbInfo.Frequency {
  226. err = fmt.Errorf("当前频度和原指标频度不一致")
  227. return
  228. }
  229. if req.Unit != fromEdbInfo.Unit {
  230. err = fmt.Errorf("当前单位和原指标单位不一致")
  231. return
  232. }
  233. sourName = "同差值"
  234. edbInfo, err = models.AddCalculateTcz(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
  235. case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS:
  236. sourName = "N数值移动平均计算"
  237. edbInfo, err = models.AddCalculateNszydpjjs(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName, formulaInt)
  238. case utils.DATA_SOURCE_CALCULATE_HBZ:
  239. checkCount, tmpErr := models.GetLteZeroEdbDataCount(fromEdbInfo.Source, fromEdbInfo.SubSource, fromEdbInfoId)
  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. go DisableEdbInfoNoUpdate(edbInfo)
  448. return
  449. }
  450. // EdbCalculateBatchEdit 批量指标编辑
  451. func EdbCalculateBatchEdit(req models.EdbInfoCalculateBatchEditReq) (edbInfo *models.EdbInfo, uniqueCode string, err error, errMsg string) {
  452. // 校验参数
  453. req.EdbName = strings.Trim(req.EdbName, " ")
  454. if req.EdbName == "" {
  455. err = fmt.Errorf("指标名称不能为空")
  456. return
  457. }
  458. if req.Frequency == "" {
  459. err = fmt.Errorf("频率不能为空")
  460. return
  461. }
  462. if req.Unit == "" {
  463. err = fmt.Errorf("单位不能为空")
  464. return
  465. }
  466. if req.ClassifyId <= 0 {
  467. err = fmt.Errorf("请选择分类")
  468. return
  469. }
  470. if req.EdbInfoId <= 0 {
  471. err = fmt.Errorf("请选择指标")
  472. return
  473. }
  474. //加入缓存机制,避免创建同一个名称的指标 start
  475. redisKey := fmt.Sprint("edb_lib:edb_info:calculate:batch:save:", req.Source, ":", req.EdbName)
  476. isExist := utils.Rc.IsExist(redisKey)
  477. if isExist {
  478. err = fmt.Errorf("指标正在处理,请勿重复提交")
  479. return
  480. } else {
  481. //设置3分钟缓存
  482. utils.Rc.SetNX(redisKey, 1, time.Second*300)
  483. defer func() {
  484. _ = utils.Rc.Delete(redisKey)
  485. }()
  486. }
  487. //加入缓存机制,避免创建同一个名称的指标 end
  488. var condition string
  489. var pars []interface{}
  490. condition += " AND edb_name=? AND edb_info_id<>? "
  491. pars = append(pars, req.EdbName, req.EdbInfoId)
  492. count, err := models.GetEdbInfoCountByCondition(condition, pars)
  493. if err != nil {
  494. err = fmt.Errorf("判断指标名称是否存在失败")
  495. errMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  496. return
  497. }
  498. if count > 0 {
  499. err = fmt.Errorf("指标名称已存在,请重新填写")
  500. errMsg = "指标名称已存在,请重新填写"
  501. return
  502. }
  503. edbInfo, err = models.GetEdbInfoById(req.EdbInfoId)
  504. if err != nil {
  505. if err.Error() == utils.ErrNoRow() {
  506. errMsg = "指标已被删除,请刷新页面:Err:" + err.Error()
  507. err = fmt.Errorf("指标已被删除,请刷新页面")
  508. return
  509. }
  510. errMsg = "获取指标信息失败:Err:" + err.Error()
  511. err = fmt.Errorf("获取指标信息失败")
  512. return
  513. }
  514. // 记录旧的指标基本信息
  515. oldEdbName := edbInfo.EdbName
  516. oldFrequency := edbInfo.Frequency
  517. oldUnit := edbInfo.Unit
  518. // 基础指标id
  519. fromEdbInfoId := req.FromEdbInfoId
  520. if req.Source <= 0 {
  521. req.Source = edbInfo.Source
  522. }
  523. var formulaInt int
  524. var nhccDate models.NhccDate // 拟合残差的日期
  525. // 初期的参数校验
  526. switch req.Source {
  527. 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:
  528. if req.Formula == "" {
  529. err = fmt.Errorf("请填写N值")
  530. return
  531. }
  532. formulaInt, _ = strconv.Atoi(req.Formula)
  533. if formulaInt <= 0 {
  534. err = fmt.Errorf("N值输入错误,请重新输入")
  535. return
  536. }
  537. case utils.DATA_SOURCE_CALCULATE_ZJPJ:
  538. //直接拼接指标
  539. //校验时间格式
  540. _, err = time.ParseInLocation(utils.FormatDate, req.Formula, time.Local)
  541. if err != nil {
  542. err = fmt.Errorf("拼接日期有误,请重新输入")
  543. return
  544. }
  545. case utils.DATA_SOURCE_CALCULATE_NHCC: //拟合残差指标
  546. //指标校验
  547. if len(req.EdbInfoIdArr) != 2 {
  548. err = fmt.Errorf("选择的指标异常,请重新选择")
  549. return
  550. }
  551. fromEdbInfoId = req.EdbInfoIdArr[0].EdbInfoId
  552. //校验时间格式
  553. //数据格式:2022-11-01,2022-11-10
  554. timeList := strings.Split(req.Formula, ",")
  555. if len(timeList) != 2 {
  556. err = fmt.Errorf("选择时间有误,请重新输入")
  557. return
  558. }
  559. startDate, e := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
  560. if e != nil {
  561. err = fmt.Errorf("开始日期有误,请重新输入")
  562. return
  563. }
  564. endDate, e := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
  565. if e != nil {
  566. err = fmt.Errorf("结束日期有误,请重新输入")
  567. return
  568. }
  569. if utils.GetTimeSubDay(startDate, endDate) < 2 {
  570. err = fmt.Errorf("日期间隔不得少于两天")
  571. return
  572. }
  573. nhccDate.StartDate = startDate
  574. nhccDate.EndDate = endDate
  575. case utils.DATA_SOURCE_CALCULATE_JP:
  576. if req.Formula != "期末值" && req.Formula != "平均值" {
  577. err = fmt.Errorf("数据取值类型错误:" + req.Formula)
  578. return
  579. }
  580. case utils.DATA_SOURCE_CALCULATE_ZSXY: // 指数修匀
  581. if req.Formula == "" {
  582. err = fmt.Errorf("请填写alpha值")
  583. return
  584. }
  585. a, e := strconv.ParseFloat(req.Formula, 64)
  586. if e != nil {
  587. err = fmt.Errorf("alpha值输入错误, 请重新输入")
  588. return
  589. }
  590. if a <= 0 || a >= 1 {
  591. err = fmt.Errorf("alpha值输入错误, 请重新输入")
  592. return
  593. }
  594. }
  595. // 来源指标
  596. var fromEdbInfo *models.EdbInfo
  597. // 基础指标id
  598. if fromEdbInfoId > 0 {
  599. fromEdbInfo, err = models.GetEdbInfoById(fromEdbInfoId)
  600. if err != nil {
  601. errMsg = "获取指标信息失败:Err:" + err.Error()
  602. err = fmt.Errorf("获取指标信息失败")
  603. return
  604. }
  605. }
  606. var sourName string
  607. var edbInfoId int
  608. var baseEdbInfoModel models.BaseEdbInfoInterface
  609. editParams := models.EditCalculateBatchParams{
  610. Req: &req,
  611. EdbInfo: edbInfo,
  612. FromEdbInfo: fromEdbInfo,
  613. }
  614. switch req.Source {
  615. case utils.DATA_SOURCE_CALCULATE_LJZZY:
  616. sourName = "累计值转月值"
  617. if fromEdbInfo.Frequency != "月度" {
  618. err = fmt.Errorf("请选择月度指标")
  619. return
  620. }
  621. err = models.EditCalculateLjzzy(edbInfo, &req, fromEdbInfo)
  622. case utils.DATA_SOURCE_CALCULATE_TBZ:
  623. sourName = "同比值"
  624. err = models.EditCalculateTbz(edbInfo, &req, fromEdbInfo)
  625. case utils.DATA_SOURCE_CALCULATE_TCZ:
  626. fmt.Println("start edit", time.Now())
  627. sourName = "同差值"
  628. err = models.EditCalculateTcz(edbInfo, &req, fromEdbInfo)
  629. fmt.Println("end edit", time.Now())
  630. case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS:
  631. sourName = "N数值移动平均计算"
  632. err = models.EditCalculateNszydpjjs(edbInfo, &req, fromEdbInfo, formulaInt, edbInfo.CalculateFormula)
  633. case utils.DATA_SOURCE_CALCULATE_HBZ:
  634. checkCount, tmpErr := models.GetLteZeroEdbDataCount(fromEdbInfo.Source, fromEdbInfo.SubSource, req.FromEdbInfoId)
  635. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  636. errMsg = "判断环比值是否可计算失败,Err:" + tmpErr.Error()
  637. err = fmt.Errorf("判断环比值是否可计算失败")
  638. return
  639. }
  640. if checkCount > 0 {
  641. err = fmt.Errorf("原始数据中存在0或负数,该指标不能进行环比运算")
  642. errMsg = "原始数据中出现0和负值时,提示该指标不能进行环比运算"
  643. return
  644. }
  645. sourName = "环比值"
  646. err = models.EditCalculateHbz(edbInfo, &req, fromEdbInfo, formulaInt, edbInfo.CalculateFormula)
  647. case utils.DATA_SOURCE_CALCULATE_HCZ:
  648. sourName = "环差值"
  649. err = models.EditCalculateHcz(edbInfo, &req, fromEdbInfo, formulaInt, edbInfo.CalculateFormula)
  650. case utils.DATA_SOURCE_CALCULATE_BP:
  651. sourName = utils.DATA_SOURCE_NAME_CALCULATE_BP
  652. err = models.EditCalculateBp(edbInfo, &req, fromEdbInfo)
  653. case utils.DATA_SOURCE_CALCULATE_TIME_SHIFT:
  654. sourName = "时间移位"
  655. err = models.EditCalculateTimeShift(edbInfo, &req, fromEdbInfo)
  656. case utils.DATA_SOURCE_CALCULATE_ZJPJ:
  657. sourName = "直接拼接"
  658. if len(req.EdbInfoIdArr) != 1 {
  659. err = fmt.Errorf("请传入拼接日期之后的指标")
  660. errMsg = "请传入拼接日期之后的指标"
  661. return
  662. }
  663. secondEdbInfoReq := req.EdbInfoIdArr[0]
  664. secondEdbInfo, tmpErr := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  665. if tmpErr != nil {
  666. errMsg = "获取拼接日期之后的指标信息失败:Err:" + tmpErr.Error()
  667. err = fmt.Errorf("获取拼接日期之后的指标信息失败")
  668. return
  669. }
  670. if fromEdbInfo.EdbInfoId == secondEdbInfo.EdbInfoId {
  671. err = fmt.Errorf("两个指标不允许为同一个")
  672. errMsg = "两个指标不允许为同一个"
  673. return
  674. }
  675. err = models.EditCalculateZjpj(&req, edbInfo, fromEdbInfo, secondEdbInfo)
  676. case utils.DATA_SOURCE_CALCULATE_LJZTBPJ: //累计值同比拼接
  677. sourName = "累计值同比拼接"
  678. if fromEdbInfo.Frequency != "月度" {
  679. errMsg = "待拼接指标只能筛选月度指标"
  680. err = fmt.Errorf("待拼接指标只能筛选月度指标")
  681. return
  682. }
  683. if len(req.EdbInfoIdArr) != 1 {
  684. errMsg = "请传入同比值指标"
  685. err = fmt.Errorf("请传入同比值指标")
  686. return
  687. }
  688. secondEdbInfoReq := req.EdbInfoIdArr[0]
  689. tbzEdbInfo, tmpErr := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  690. if tmpErr != nil {
  691. errMsg = "获取同比值指标信息失败:Err:" + tmpErr.Error()
  692. err = fmt.Errorf("获取同比值指标信息失败")
  693. return
  694. }
  695. if tbzEdbInfo.Source != utils.DATA_SOURCE_CALCULATE_TBZ {
  696. err = fmt.Errorf("指标必须是传入同比值指标类型")
  697. errMsg = "指标必须是传入同比值指标类型"
  698. return
  699. }
  700. if tbzEdbInfo.Frequency != "月度" {
  701. err = fmt.Errorf("同比值指标只能筛选月度指标")
  702. errMsg = "同比值指标只能筛选月度指标"
  703. return
  704. }
  705. if fromEdbInfo.EdbInfoId == tbzEdbInfo.EdbInfoId {
  706. err = fmt.Errorf("两个指标不允许为同一个")
  707. errMsg = "两个指标不允许为同一个"
  708. return
  709. }
  710. err = models.EditCalculateLjztbpj(&req, edbInfo, fromEdbInfo, tbzEdbInfo)
  711. case utils.DATA_SOURCE_CALCULATE_CJJX:
  712. sourName = "超季节性"
  713. err = models.EditCalculateCjjx(&req, edbInfo, fromEdbInfo, formulaInt)
  714. case utils.DATA_SOURCE_CALCULATE_NHCC:
  715. sourName = "拟合残差"
  716. secondEdbInfoReq := req.EdbInfoIdArr[1]
  717. secondEdbInfo, tmpErr := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  718. if tmpErr != nil {
  719. errMsg = "获取因变量的指标信息失败:Err:" + tmpErr.Error()
  720. err = fmt.Errorf("获取因变量的指标信息失败")
  721. return
  722. }
  723. if fromEdbInfo.EdbInfoId == secondEdbInfo.EdbInfoId {
  724. err = fmt.Errorf("两个指标不允许为同一个")
  725. errMsg = "两个指标不允许为同一个"
  726. return
  727. }
  728. err, errMsg = models.EditCalculateNhcc(&req, edbInfo, fromEdbInfo, secondEdbInfo, nhccDate)
  729. case utils.DATA_SOURCE_CALCULATE_JP:
  730. if !models.CheckFrequency(fromEdbInfo.Frequency, req.Frequency) {
  731. err = fmt.Errorf("频度异常,不允许低频降频到高频")
  732. return
  733. }
  734. sourName = utils.DATA_SOURCE_NAME_CALCULATE_JP
  735. err = models.EditCalculateJp(edbInfo, &req, fromEdbInfo)
  736. case utils.DATA_SOURCE_CALCULATE_NH:
  737. sourName = utils.DATA_SOURCE_NAME_CALCULATE_NH
  738. err = models.EditCalculateNh(edbInfo, &req, fromEdbInfo)
  739. case utils.DATA_SOURCE_CALCULATE_KSZS:
  740. //关联的指标信息
  741. if len(req.EdbInfoIdArr) < 2 {
  742. err = fmt.Errorf("指标数量不能小于2个,请重新选择")
  743. errMsg = "指标数量不能小于2个,请重新选择"
  744. //br.IsSendEmail = false
  745. return
  746. }
  747. sourName = utils.DATA_SOURCE_NAME_CALCULATE_KSZS
  748. err = models.EditCalculateKszs(edbInfo, &req)
  749. case utils.DATA_SOURCE_CALCULATE_CORRELATION:
  750. //关联的指标信息
  751. if len(req.EdbInfoIdArr) < 2 {
  752. err = fmt.Errorf("指标数量不能小于2个,请重新选择")
  753. errMsg = "指标数量不能小于2个,请重新选择"
  754. //br.IsSendEmail = false
  755. return
  756. }
  757. sourName = utils.DATA_SOURCE_NAME_CALCULATE_CORRELATION
  758. err, errMsg = models.EditCalculateCorrelation(edbInfo, &req)
  759. case utils.DATA_SOURCE_CALCULATE_RJZ:
  760. if req.Frequency == "日度" {
  761. err = fmt.Errorf("日度指标无需进行日均值计算")
  762. return
  763. }
  764. if req.Frequency != fromEdbInfo.Frequency {
  765. err = fmt.Errorf("当前频度和原指标频度不一致")
  766. return
  767. }
  768. if req.Unit != fromEdbInfo.Unit {
  769. err = fmt.Errorf("单位只允许为和原指标频度保持一致,禁止选择其他单位")
  770. return
  771. }
  772. sourName = "日均值"
  773. err = models.EditCalculateRjz(edbInfo, &req, fromEdbInfo)
  774. default:
  775. // 获取通用的数据源处理服务
  776. baseEdbInfoModel = models.GetBaseEdbInfoModel(req.Source)
  777. // 没有找到的话,那么就直接返回报错吧
  778. if baseEdbInfoModel == nil {
  779. err = fmt.Errorf("无效计算方式")
  780. errMsg = "无效计算方式,source:" + strconv.Itoa(req.Source)
  781. return
  782. }
  783. sourName = baseEdbInfoModel.GetSourceName()
  784. err, errMsg = baseEdbInfoModel.Edit(editParams)
  785. }
  786. if err != nil {
  787. errMsg = "生成" + sourName + "失败 Err:" + err.Error() + errMsg
  788. err = fmt.Errorf("生成" + sourName + "失败")
  789. return
  790. }
  791. if edbInfo == nil {
  792. err = fmt.Errorf("生成" + sourName + "失败")
  793. errMsg = "生成" + sourName + "失败,指标ID错误:" + strconv.Itoa(edbInfoId)
  794. return
  795. }
  796. edbInfoId = edbInfo.EdbInfoId
  797. uniqueCode = edbInfo.UniqueCode
  798. //处理同名指标
  799. {
  800. edbNameList, tErr := models.GetEdbInfoByName(req.EdbName)
  801. if tErr != nil {
  802. err = fmt.Errorf("保存失败")
  803. errMsg = "获取指标信息失败,Err:" + tErr.Error()
  804. return
  805. }
  806. if len(edbNameList) >= 2 {
  807. for _, v := range edbNameList {
  808. edbName := v.EdbName + "(" + v.SourceName + ")"
  809. err = models.ModifyEdbInfoNameSource(edbName, v.EdbInfoId)
  810. if err != nil {
  811. errMsg = "修改指标名称失败,Err:" + err.Error()
  812. err = fmt.Errorf("保存失败")
  813. return
  814. }
  815. }
  816. }
  817. }
  818. // 更新指标最大最小值
  819. err, errMsg = models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  820. if err != nil {
  821. return
  822. }
  823. // 记录基础信息操作变更日志
  824. oldEdbInfo := new(models.EdbInfo)
  825. oldEdbInfo.EdbInfoId = edbInfoId
  826. oldEdbInfo.EdbName = oldEdbName
  827. oldEdbInfo.Frequency = oldFrequency
  828. oldEdbInfo.Unit = oldUnit
  829. newEdbInfoEditRecord := new(models.EdbInfoEditRecord)
  830. newEdbInfoEditRecord.EdbName = req.EdbName
  831. newEdbInfoEditRecord.Frequency = req.Frequency
  832. newEdbInfoEditRecord.Unit = req.Unit
  833. newEdbInfoEditRecord.OperateUserId = req.AdminId
  834. newEdbInfoEditRecord.OperateUserRealName = req.AdminName
  835. err = AddEditEdbInfoRcord(oldEdbInfo, newEdbInfoEditRecord)
  836. if err != nil {
  837. errMsg = "记录基础信息操作变更日志失败,Err:" + err.Error()
  838. err = fmt.Errorf("操作记录保存失败")
  839. return
  840. }
  841. // 更新ES
  842. go logic.UpdateEs(edbInfo.EdbInfoId)
  843. // 重置计算指标中的引用关系
  844. go ResetEdbRelation(edbInfoId)
  845. return
  846. }
  847. // EdbCalculateAdd 新增指标运算
  848. func EdbCalculateAdd(req models.EdbInfoCalculateSaveReq, lang string) (edbInfo *models.EdbInfo, uniqueCode string, err error, errMsg string) {
  849. calculateFormula := req.CalculateFormula
  850. calculateFormula = strings.Replace(calculateFormula, "(", "(", -1)
  851. calculateFormula = strings.Replace(calculateFormula, ")", ")", -1)
  852. calculateFormula = strings.Replace(calculateFormula, ",", ",", -1)
  853. calculateFormula = strings.Replace(calculateFormula, "。", ".", -1)
  854. calculateFormula = strings.Replace(calculateFormula, "%", "*0.01", -1)
  855. req.CalculateFormula = calculateFormula
  856. //判断是否重复指标
  857. edbInfoMap := make(map[int]string)
  858. //移除研究员选择指标中的未使用的指标
  859. {
  860. //转大写的计算公式
  861. upperCalculateFormulaStr := strings.ToUpper(req.CalculateFormula)
  862. //用到的指标
  863. newEdbInfoIdArr := make([]models.EdbInfoFromTag, 0)
  864. for _, tmpEdbInfo := range req.EdbInfoIdArr {
  865. _, ok := edbInfoMap[tmpEdbInfo.EdbInfoId]
  866. if ok {
  867. err = fmt.Errorf("选择指标失败,请勿选择重复指标!")
  868. return
  869. }
  870. edbInfoMap[tmpEdbInfo.EdbInfoId] = tmpEdbInfo.FromTag
  871. upperFromTag := strings.ToUpper(tmpEdbInfo.FromTag)
  872. if strings.Contains(upperCalculateFormulaStr, upperFromTag) {
  873. newEdbInfoIdArr = append(newEdbInfoIdArr, tmpEdbInfo)
  874. }
  875. }
  876. req.EdbInfoIdArr = newEdbInfoIdArr
  877. }
  878. // 校验指标名称是否存在
  879. existEdbName, err := logic.CheckExistByEdbNameAndEdbInfoId(0, 0, req.EdbName, lang)
  880. if err != nil {
  881. errMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  882. err = fmt.Errorf("判断指标名称是否存在失败")
  883. return
  884. }
  885. if existEdbName {
  886. errMsg = "指标名称已存在,请重新填写"
  887. err = fmt.Errorf(errMsg)
  888. //br.IsSendEmail = false
  889. return
  890. }
  891. //检验公式
  892. var formulaStr string
  893. var edbInfoIdBytes []string
  894. for _, v := range req.EdbInfoIdArr {
  895. formulaStr += v.FromTag + ","
  896. edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
  897. }
  898. formulaSlice, err := utils.CheckFormulaJson(req.CalculateFormula)
  899. if err != nil {
  900. err = fmt.Errorf("公式格式错误,请重新填写")
  901. return
  902. }
  903. for _, formula := range formulaSlice {
  904. formulaMap, e := utils.CheckFormula(formula)
  905. if e != nil {
  906. err = fmt.Errorf("公式错误,请重新填写")
  907. return
  908. }
  909. for _, v := range formulaMap {
  910. if !strings.Contains(formulaStr, v) {
  911. err = fmt.Errorf("公式错误,请重新填写")
  912. return
  913. }
  914. }
  915. }
  916. //关联的指标信息
  917. edbInfoList := make([]*models.EdbInfo, 0)
  918. //关联的指标数据表
  919. calculateMappingList := make([]*models.EdbInfoCalculateMapping, 0)
  920. for k, v := range req.EdbInfoIdArr {
  921. fromEdbInfo, e := models.GetEdbInfoById(v.EdbInfoId)
  922. if e != nil {
  923. if e.Error() == utils.ErrNoRow() {
  924. err = fmt.Errorf("生成计算指标失败")
  925. errMsg = "指标 " + strconv.Itoa(v.EdbInfoId) + " 不存在"
  926. return
  927. }
  928. err = fmt.Errorf("生成计算指标失败")
  929. errMsg = "获取指标失败:Err:" + e.Error()
  930. return
  931. }
  932. edbInfoList = append(edbInfoList, fromEdbInfo)
  933. //关联关系表
  934. {
  935. calculateMappingItem := &models.EdbInfoCalculateMapping{
  936. EdbInfoCalculateMappingId: 0,
  937. EdbInfoId: 0,
  938. Source: utils.DATA_SOURCE_CALCULATE,
  939. SourceName: "指标运算",
  940. EdbCode: "",
  941. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  942. FromEdbCode: fromEdbInfo.EdbCode,
  943. FromEdbName: fromEdbInfo.EdbName,
  944. FromSource: fromEdbInfo.Source,
  945. FromSourceName: fromEdbInfo.SourceName,
  946. FromTag: v.FromTag,
  947. Sort: k + 1,
  948. CreateTime: time.Now(),
  949. ModifyTime: time.Now(),
  950. }
  951. //calculateMappingItem.EdbCode = edbCode
  952. //calculateMappingItem.EdbInfoId = int(edbInfoId)
  953. calculateMappingList = append(calculateMappingList, calculateMappingItem)
  954. }
  955. }
  956. for _, v := range formulaSlice {
  957. formulaMap, e := utils.CheckFormula(v)
  958. if e != nil {
  959. err = fmt.Errorf("公式错误,请重新填写")
  960. return
  961. }
  962. //预先计算,判断公式是否正常
  963. ok, _ := models.CheckFormula2(edbInfoList, formulaMap, v, edbInfoIdBytes)
  964. if !ok {
  965. err = fmt.Errorf("生成计算指标失败,请使用正确的计算公式")
  966. return
  967. }
  968. }
  969. //生成指标编码
  970. edbCode, err := utils.GenerateEdbCode(1, "")
  971. if err != nil {
  972. errMsg = "生成计算指标失败,AddEdbInfo Err:" + err.Error()
  973. err = fmt.Errorf("指标编码生成失败,Err:" + err.Error())
  974. return
  975. }
  976. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  977. uniqueCode = utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  978. // 新增计算指标
  979. edbInfo, err = models.AddCalculateInfo(req, calculateMappingList, edbInfoList, edbCode, uniqueCode, edbInfoIdBytes)
  980. if err != nil {
  981. errMsg = "生成计算指标失败,AddEdbInfo Err:" + err.Error()
  982. err = fmt.Errorf("生成计算指标失败")
  983. return
  984. }
  985. go DisableEdbInfoNoUpdate(edbInfo)
  986. return
  987. }
  988. // StepCalculate 多公式计算
  989. func StepCalculate(dataList []*models.EdbInfoSearchData, calculates []models.CalculatesReq) (resultData map[string]float64, dates []string, errMsg string, err error) {
  990. sort.Slice(calculates, func(i, j int) bool {
  991. return calculates[i].Sort < calculates[j].Sort
  992. })
  993. originData, e := models.EdbInfoSearchDataToData(dataList)
  994. if e != nil {
  995. err = fmt.Errorf("基础数据转换异常, err: %v", e)
  996. return
  997. }
  998. calculateData := originData
  999. dateDataMap := make(map[time.Time]float64)
  1000. for _, v := range calculates {
  1001. baseCalculate := models.BaseCalculate{
  1002. DataList: calculateData,
  1003. Frequency: v.Frequency,
  1004. Formula: v.Formula,
  1005. Calendar: v.Calendar,
  1006. MoveType: v.MoveType,
  1007. MoveFrequency: v.MoveFrequency,
  1008. FromFrequency: v.FromFrequency,
  1009. Source: v.Source,
  1010. }
  1011. // 计算方式
  1012. switch baseCalculate.Source {
  1013. case utils.EdbBaseCalculateLjzzy:
  1014. dateDataMap, e, errMsg = baseCalculate.Ljzzy()
  1015. case utils.EdbBaseCalculateLjzzj:
  1016. dateDataMap, e, errMsg = baseCalculate.Ljzzj()
  1017. case utils.EdbBaseCalculateTbz:
  1018. dateDataMap, e, errMsg = baseCalculate.Tbz()
  1019. case utils.EdbBaseCalculateTcz:
  1020. dateDataMap, e, errMsg = baseCalculate.Tcz()
  1021. case utils.EdbBaseCalculateNszydpjjs:
  1022. dateDataMap, e, errMsg = baseCalculate.Nszydpjjs()
  1023. case utils.EdbBaseCalculateHbz:
  1024. dateDataMap, e, errMsg = baseCalculate.Hbz()
  1025. case utils.EdbBaseCalculateHcz:
  1026. dateDataMap, e, errMsg = baseCalculate.Hcz()
  1027. case utils.EdbBaseCalculateUpFrequency:
  1028. dateDataMap, e, errMsg = baseCalculate.UpFrequency()
  1029. case utils.EdbBaseCalculateDownFrequency:
  1030. dateDataMap, e, errMsg = baseCalculate.DownFrequency()
  1031. case utils.EdbBaseCalculateTimeShift:
  1032. dateDataMap, e, errMsg = baseCalculate.TimeShift()
  1033. case utils.EdbBaseCalculateCjjx:
  1034. dateDataMap, e, errMsg = baseCalculate.Cjjx()
  1035. case utils.EdbBaseCalculateAnnualized:
  1036. dateDataMap, e, errMsg = baseCalculate.Annualized()
  1037. case utils.EdbBaseCalculateLjz:
  1038. dateDataMap, e, errMsg = baseCalculate.Ljz()
  1039. case utils.EdbBaseCalculateLjzNczj:
  1040. dateDataMap, e, errMsg = baseCalculate.LjzNczj()
  1041. case utils.EdbBaseCalculateExponentialSmoothing:
  1042. dateDataMap, e, errMsg = baseCalculate.ExponentialSmoothing()
  1043. case utils.EdbBaseCalculateRjz:
  1044. dateDataMap, e, errMsg = baseCalculate.Rjz()
  1045. default:
  1046. errMsg = "计算方式无效"
  1047. e = fmt.Errorf("%s:%d", errMsg, baseCalculate.Source)
  1048. }
  1049. if e != nil {
  1050. err = fmt.Errorf("计算失败, err: %v", e)
  1051. return
  1052. }
  1053. calculateData = models.TransDateData2EdbData(dateDataMap)
  1054. }
  1055. resultData, dates = models.GetDateDataAndDateList(dateDataMap)
  1056. //resp.DataMap = resultData
  1057. //resp.DateList = dates
  1058. return
  1059. }