base_from_calculate.go 58 KB

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