base_from_calculate.go 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta_gn/eta_index_lib/logic"
  6. "eta_gn/eta_index_lib/models"
  7. "eta_gn/eta_index_lib/services"
  8. "eta_gn/eta_index_lib/utils"
  9. "fmt"
  10. "strconv"
  11. "strings"
  12. "sync"
  13. "time"
  14. )
  15. // CalculateController 计算指标
  16. type CalculateController struct {
  17. BaseAuthController
  18. }
  19. // Add
  20. // @Title 新增计算指标接口
  21. // @Description 新增计算指标接口
  22. // @Success 200 {object} models.EdbInfoCalculateSaveReq
  23. // @router /add [post]
  24. func (this *CalculateController) Add() {
  25. br := new(models.BaseResponse).Init()
  26. defer func() {
  27. this.Data["json"] = br
  28. this.ServeJSON()
  29. }()
  30. var req models.EdbInfoCalculateSaveReq
  31. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  32. if err != nil {
  33. br.Msg = "参数解析异常!"
  34. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  35. return
  36. }
  37. req.EdbName = strings.Trim(req.EdbName, " ")
  38. if req.EdbName == "" {
  39. br.Msg = "指标名称不能为空"
  40. return
  41. }
  42. if req.Frequency == "" {
  43. br.Msg = "频率不能为空"
  44. return
  45. }
  46. if req.Unit == "" {
  47. br.Msg = "单位不能为空"
  48. return
  49. }
  50. if req.ClassifyId <= 0 {
  51. br.Msg = "请选择分类"
  52. return
  53. }
  54. if len(req.EdbInfoIdArr) <= 0 {
  55. br.Msg = "请选择指标"
  56. return
  57. }
  58. if req.CalculateFormula == "" {
  59. br.Msg = "请填写指标"
  60. return
  61. }
  62. //加入缓存机制,避免创建同一个名称的指标 start
  63. redisKey := fmt.Sprint("edb_lib:edb_info:calculate:add:", utils.DATA_SOURCE_CALCULATE, ":", req.EdbName)
  64. isExist := utils.Rc.IsExist(redisKey)
  65. if isExist {
  66. br.Msg = "指标正在处理,请勿重复提交"
  67. return
  68. } else {
  69. //设置3分钟缓存
  70. utils.Rc.SetNX(redisKey, 1, time.Second*300)
  71. defer func() {
  72. _ = utils.Rc.Delete(redisKey)
  73. }()
  74. }
  75. // 新增
  76. edbInfo, uniqueCode, err, errMsg := services.EdbCalculateAdd(req, this.Lang)
  77. if err != nil {
  78. br.Msg = err.Error()
  79. br.ErrMsg = errMsg
  80. return
  81. }
  82. //处理同名指标
  83. {
  84. edbNameList, e := models.GetEdbInfoByName(req.EdbName)
  85. if e != nil {
  86. err = fmt.Errorf("保存失败")
  87. errMsg = "获取指标信息失败,Err:" + e.Error()
  88. return
  89. }
  90. if len(edbNameList) >= 2 {
  91. for _, v := range edbNameList {
  92. edbName := v.EdbName + "(" + v.SourceName + ")"
  93. err = models.ModifyEdbInfoNameSource(edbName, v.EdbInfoId)
  94. if err != nil {
  95. errMsg = "修改指标名称失败,Err:" + err.Error()
  96. err = fmt.Errorf("保存失败")
  97. return
  98. }
  99. }
  100. }
  101. }
  102. // 更新指标最大最小值
  103. err, errMsg = models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  104. if err != nil {
  105. return
  106. }
  107. // 判断是否需要禁用
  108. go services.DisableEdbInfoNoUpdate(edbInfo)
  109. resp := models.AddEdbInfoResp{
  110. EdbInfoId: edbInfo.EdbInfoId,
  111. UniqueCode: uniqueCode,
  112. }
  113. br.Ret = 200
  114. br.Success = true
  115. br.Msg = "保存成功"
  116. br.Data = resp
  117. br.IsAddLog = true
  118. }
  119. // Edit
  120. // @Title 编辑计算指标接口
  121. // @Description 编辑计算指标接口
  122. // @Success 200 {object} models.EdbInfoCalculateSaveReq
  123. // @router /edit [post]
  124. func (this *CalculateController) Edit() {
  125. br := new(models.BaseResponse).Init()
  126. defer func() {
  127. this.Data["json"] = br
  128. this.ServeJSON()
  129. }()
  130. var req models.EdbInfoCalculateSaveReq
  131. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  132. if err != nil {
  133. br.Msg = "参数解析异常!"
  134. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  135. return
  136. }
  137. if req.EdbInfoId <= 0 {
  138. br.Msg = "参数错误"
  139. br.ErrMsg = "指标ID:" + strconv.Itoa(req.EdbInfoId)
  140. return
  141. }
  142. req.EdbName = strings.Trim(req.EdbName, " ")
  143. if req.EdbName == "" {
  144. br.Msg = "指标名称不能为空"
  145. return
  146. }
  147. if req.Frequency == "" {
  148. br.Msg = "频率不能为空"
  149. return
  150. }
  151. if req.Unit == "" {
  152. br.Msg = "单位不能为空"
  153. return
  154. }
  155. if req.ClassifyId <= 0 {
  156. br.Msg = "请选择分类"
  157. return
  158. }
  159. if len(req.EdbInfoIdArr) <= 0 {
  160. br.Msg = "请选择指标"
  161. return
  162. }
  163. if req.CalculateFormula == "" {
  164. br.Msg = "请填写指标公式"
  165. return
  166. }
  167. //todo 校验空值类型,max、min空值类型
  168. //todo req.EdbInfoIdArr根据tag排序,排成A,B,AB,这种格式
  169. calculateFormula := req.CalculateFormula
  170. calculateFormula = strings.Replace(calculateFormula, "(", "(", -1)
  171. calculateFormula = strings.Replace(calculateFormula, ")", ")", -1)
  172. calculateFormula = strings.Replace(calculateFormula, ",", ",", -1)
  173. calculateFormula = strings.Replace(calculateFormula, "。", ".", -1)
  174. calculateFormula = strings.Replace(calculateFormula, "%", "*0.01", -1)
  175. req.CalculateFormula = calculateFormula
  176. fmt.Println("calculateFormula:" + calculateFormula)
  177. //判断是否重复指标
  178. edbInfoMap := make(map[int]string)
  179. //移除研究员选择指标中的未使用的指标
  180. {
  181. //转大写的计算公式
  182. upperCalculateFormulaStr := strings.ToUpper(req.CalculateFormula)
  183. //用到的指标
  184. newEdbInfoIdArr := make([]models.EdbInfoFromTag, 0)
  185. for _, tmpEdbInfo := range req.EdbInfoIdArr {
  186. _, ok := edbInfoMap[tmpEdbInfo.EdbInfoId]
  187. if ok {
  188. br.Msg = "选择指标失败,请勿选择重复指标!"
  189. return
  190. }
  191. edbInfoMap[tmpEdbInfo.EdbInfoId] = tmpEdbInfo.FromTag
  192. upperFromTag := strings.ToUpper(tmpEdbInfo.FromTag)
  193. if strings.Contains(upperCalculateFormulaStr, upperFromTag) {
  194. newEdbInfoIdArr = append(newEdbInfoIdArr, tmpEdbInfo)
  195. }
  196. }
  197. req.EdbInfoIdArr = newEdbInfoIdArr
  198. }
  199. // 根据指标名称和指标ID校验库中是否还存在其他同名指标
  200. existEdbName, err := logic.CheckExistByEdbNameAndEdbInfoId(utils.EDB_INFO_TYPE, req.EdbInfoId, req.EdbName, this.Lang)
  201. if err != nil {
  202. br.Msg = "判断指标名称是否存在失败"
  203. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  204. return
  205. }
  206. if existEdbName {
  207. br.Msg = "指标名称已存在,请重新填写"
  208. br.ErrMsg = "指标名称已存在,请重新填写"
  209. br.IsSendEmail = false
  210. return
  211. }
  212. //判断公式,指标是否有改动
  213. edbInfoDetail, err := models.GetEdbInfoById(req.EdbInfoId)
  214. if err != nil {
  215. br.Msg = "修改失败"
  216. br.Msg = "获取指标信息失败,GetEdbInfoById Err:" + err.Error()
  217. return
  218. }
  219. if edbInfoDetail == nil {
  220. br.Msg = "修改失败"
  221. br.Msg = "指标信息不存在,EdbInfoId:" + strconv.Itoa(req.EdbInfoId)
  222. return
  223. }
  224. // 记录指标原始的基本信息
  225. oldEdbName := edbInfoDetail.EdbName
  226. oldFrequency := edbInfoDetail.Frequency
  227. oldUnit := edbInfoDetail.Unit
  228. var needCalculate bool
  229. if edbInfoDetail.CalculateFormula != req.CalculateFormula || edbInfoDetail.EmptyType != req.EmptyType || edbInfoDetail.MaxEmptyType != req.MaxEmptyType || edbInfoDetail.Extra != req.Extra {
  230. needCalculate = true
  231. }
  232. //检验公式
  233. var formulaStr string
  234. var edbInfoIdBytes []string
  235. var edbInfoIdArr []string
  236. for _, v := range req.EdbInfoIdArr {
  237. formulaStr += v.FromTag + ","
  238. edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
  239. edbInfoIdArr = append(edbInfoIdArr, strconv.Itoa(v.EdbInfoId))
  240. if v.EdbInfoId == req.EdbInfoId {
  241. br.Msg = "计算指标中包含此指标,不能修改"
  242. return
  243. }
  244. }
  245. if needCalculate == false {
  246. edbInfoIdStr := strings.Join(edbInfoIdArr, ",")
  247. fromEdbInfoId, err := models.GetCalculateEdbInfo(req.EdbInfoId)
  248. if err != nil {
  249. br.Msg = "修改失败"
  250. br.Msg = "获取数据失败,GetCalculateEdbInfo:" + err.Error()
  251. return
  252. }
  253. if edbInfoIdStr != fromEdbInfoId {
  254. needCalculate = true
  255. }
  256. }
  257. formulaSlice, err := utils.CheckFormulaJson(req.CalculateFormula)
  258. if err != nil {
  259. br.Msg = "公式格式错误,请重新填写"
  260. return
  261. }
  262. for _, formula := range formulaSlice {
  263. formulaMap, e := utils.CheckFormula(formula)
  264. if e != nil {
  265. br.Msg = "公式错误,请重新填写"
  266. return
  267. }
  268. for _, v := range formulaMap {
  269. if !strings.Contains(formulaStr, v) {
  270. br.Msg = "公式错误,请重新填写"
  271. return
  272. }
  273. }
  274. }
  275. // 额外赋值
  276. switch this.Lang {
  277. case utils.EnLangVersion:
  278. req.EdbNameEn = req.EdbName
  279. req.UnitEn = req.Unit
  280. req.EdbName = edbInfoDetail.EdbName
  281. req.Unit = edbInfoDetail.Unit
  282. default:
  283. req.EdbNameEn = edbInfoDetail.EdbNameEn
  284. req.UnitEn = edbInfoDetail.UnitEn
  285. }
  286. //重新计算
  287. err, errMsg := models.EditCalculateInfo(edbInfoDetail, req, formulaSlice, edbInfoIdBytes, needCalculate)
  288. if err != nil {
  289. br.Msg = "生成计算指标失败"
  290. if errMsg != `` {
  291. br.Msg = errMsg
  292. }
  293. br.Msg = "生成计算指标失败,AddEdbInfo Err:" + err.Error()
  294. return
  295. }
  296. // 更新指标最大最小值
  297. err, errMsg = models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfoDetail)
  298. if err != nil {
  299. br.Msg = errMsg
  300. br.ErrMsg = err.Error()
  301. return
  302. }
  303. // 记录基础信息变更日志
  304. oldEdbInfo := new(models.EdbInfo)
  305. oldEdbInfo.EdbInfoId = edbInfoDetail.EdbInfoId
  306. oldEdbInfo.EdbName = oldEdbName
  307. oldEdbInfo.Frequency = oldFrequency
  308. oldEdbInfo.Unit = oldUnit
  309. newEdbInfoEditRecord := new(models.EdbInfoEditRecord)
  310. newEdbInfoEditRecord.EdbName = req.EdbName
  311. newEdbInfoEditRecord.Frequency = req.Frequency
  312. newEdbInfoEditRecord.Unit = req.Unit
  313. newEdbInfoEditRecord.OperateUserId = req.AdminId
  314. newEdbInfoEditRecord.OperateUserRealName = req.AdminName
  315. err = services.AddEditEdbInfoRcord(oldEdbInfo, newEdbInfoEditRecord)
  316. if err != nil {
  317. br.Msg = "记录基础信息变更日志失败"
  318. br.ErrMsg = err.Error()
  319. return
  320. }
  321. resp := models.AddEdbInfoResp{
  322. EdbInfoId: edbInfoDetail.EdbInfoId,
  323. UniqueCode: edbInfoDetail.UniqueCode,
  324. }
  325. // 重置计算指标中的引用关系
  326. go services.ResetEdbRelation(edbInfoDetail.EdbInfoId)
  327. br.Ret = 200
  328. br.Success = true
  329. br.Msg = "保存成功"
  330. br.Data = resp
  331. br.IsAddLog = true
  332. }
  333. // BatchSave
  334. // @Title 累计值转月-同比值-同差等计算新增
  335. // @Description 累计值转月-同比值-同差等计算新增接口
  336. // @Param request body models.EdbInfoCalculateBatchSaveReq true "type json string"
  337. // @Success Ret=200 返回指标id
  338. // @router /batch/save [post]
  339. func (this *CalculateController) BatchSave() {
  340. br := new(models.BaseResponse).Init()
  341. defer func() {
  342. if br.ErrMsg == "" {
  343. br.IsSendEmail = false
  344. }
  345. this.Data["json"] = br
  346. this.ServeJSON()
  347. }()
  348. var req models.EdbInfoCalculateBatchSaveReq
  349. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  350. if err != nil {
  351. br.Msg = "参数解析异常!"
  352. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  353. return
  354. }
  355. req.EdbName = strings.Trim(req.EdbName, " ")
  356. if req.EdbName == "" {
  357. br.Msg = "指标名称不能为空"
  358. return
  359. }
  360. if req.Frequency == "" {
  361. br.Msg = "频率不能为空"
  362. return
  363. }
  364. if req.Unit == "" {
  365. br.Msg = "单位不能为空"
  366. return
  367. }
  368. if req.ClassifyId <= 0 {
  369. br.Msg = "请选择分类"
  370. return
  371. }
  372. // 基础指标id
  373. fromEdbInfoId := req.FromEdbInfoId
  374. var formulaInt int
  375. var nhccDate models.NhccDate // 拟合残差的日期
  376. switch req.Source {
  377. 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:
  378. if req.Formula == "" {
  379. br.Msg = "请填写N值"
  380. return
  381. }
  382. formulaInt, _ = strconv.Atoi(req.Formula)
  383. if formulaInt <= 0 {
  384. br.Msg = "N值输入错误,请重新输入"
  385. return
  386. }
  387. case utils.DATA_SOURCE_CALCULATE_ZJPJ:
  388. //直接拼接指标
  389. //校验时间格式
  390. _, err = time.ParseInLocation(utils.FormatDate, req.Formula, time.Local)
  391. if err != nil {
  392. br.Msg = "拼接日期有误,请重新输入"
  393. return
  394. }
  395. case utils.DATA_SOURCE_CALCULATE_NHCC: //拟合残差指标
  396. //指标校验
  397. if len(req.EdbInfoIdArr) != 2 {
  398. br.Msg = "选择的指标异常,请重新选择"
  399. return
  400. }
  401. fromEdbInfoId = req.EdbInfoIdArr[0].EdbInfoId
  402. //校验时间格式
  403. //数据格式:2022-11-01,2022-11-10
  404. timeList := strings.Split(req.Formula, ",")
  405. if len(timeList) != 2 {
  406. br.Msg = "选择时间有误,请重新输入"
  407. return
  408. }
  409. startDate, err := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
  410. if err != nil {
  411. br.Msg = "开始日期有误,请重新输入"
  412. return
  413. }
  414. endDate, err := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
  415. if err != nil {
  416. br.Msg = "结束日期有误,请重新输入"
  417. return
  418. }
  419. if utils.GetTimeSubDay(startDate, endDate) < 2 {
  420. br.Msg = "日期间隔不得少于两天"
  421. return
  422. }
  423. nhccDate.StartDate = startDate
  424. nhccDate.EndDate = endDate
  425. case utils.DATA_SOURCE_CALCULATE_JP:
  426. if req.Formula != "期末值" && req.Formula != "平均值" {
  427. br.Msg = "数据取值类型错误:" + req.Formula
  428. return
  429. }
  430. case utils.DATA_SOURCE_CALCULATE_ZSXY: // 指数修匀
  431. if req.Formula == "" {
  432. br.Msg = "请填写alpha值"
  433. return
  434. }
  435. a, e := strconv.ParseFloat(req.Formula, 64)
  436. if e != nil {
  437. br.Msg = "alpha值输入错误, 请重新输入"
  438. return
  439. }
  440. if a <= 0 || a >= 1 {
  441. br.Msg = "alpha值输入错误, 请重新输入"
  442. return
  443. }
  444. }
  445. notNeedFromEdbSourceList := []int{utils.DATA_SOURCE_CALCULATE_KSZS, utils.DATA_SOURCE_CALCULATE_CORRELATION, utils.DATA_SOURCE_CALCULATE_ZDYFX} // 不需要传入来源指标id的 指标类型
  446. if fromEdbInfoId <= 0 && !utils.InArrayByInt(notNeedFromEdbSourceList, req.Source) {
  447. br.Msg = "请选择指标"
  448. return
  449. }
  450. //加入缓存机制,避免创建同一个名称的指标 start
  451. redisKey := fmt.Sprint("edb_lib:edb_info:calculate:batch:save:", req.Source, ":", req.EdbName)
  452. isExist := utils.Rc.IsExist(redisKey)
  453. if isExist {
  454. br.Msg = "指标正在处理,请勿重复提交"
  455. return
  456. } else {
  457. //设置3分钟缓存
  458. utils.Rc.SetNX(redisKey, 1, time.Second*300)
  459. defer func() {
  460. _ = utils.Rc.Delete(redisKey)
  461. }()
  462. }
  463. //加入缓存机制,避免创建同一个名称的指标 end
  464. // 根据指标名称和指标ID校验库中是否还存在其他同名指标
  465. existEdbName, err := logic.CheckExistByEdbNameAndEdbInfoId(utils.EDB_INFO_TYPE, req.EdbInfoId, req.EdbName, this.Lang)
  466. if err != nil {
  467. br.Msg = "判断指标名称是否存在失败"
  468. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  469. return
  470. }
  471. if existEdbName {
  472. br.Msg = "指标名称已存在,请重新填写"
  473. br.ErrMsg = "指标名称已存在,请重新填写"
  474. br.IsSendEmail = false
  475. return
  476. }
  477. // 来源指标
  478. var fromEdbInfo *models.EdbInfo
  479. if fromEdbInfoId > 0 {
  480. fromEdbInfo, err = models.GetEdbInfoById(fromEdbInfoId)
  481. if err != nil {
  482. br.Msg = "获取指标信息失败"
  483. br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
  484. return
  485. }
  486. }
  487. //生成指标编码
  488. edbCode, err := utils.GenerateEdbCode(1, "")
  489. if err != nil {
  490. br.Msg = "指标生成失败"
  491. br.ErrMsg = "指标编码生成失败,Err:" + err.Error()
  492. return
  493. }
  494. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  495. uniqueCode := utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  496. sysUserId := req.AdminId
  497. sysUserName := req.AdminName
  498. var sourName string
  499. var edbInfoId int
  500. var edbInfo *models.EdbInfo
  501. var errMsg string
  502. var baseEdbInfoModel models.BaseEdbInfoInterface
  503. addParam := models.AddCalculateBatchParams{
  504. Req: &req,
  505. FromEdbInfo: fromEdbInfo,
  506. EdbCode: edbCode,
  507. UniqueCode: uniqueCode,
  508. SysUserId: sysUserId,
  509. SysUserRealName: sysUserName,
  510. }
  511. switch req.Source {
  512. case utils.DATA_SOURCE_CALCULATE_LJZZY:
  513. sourName = "累计值转月值"
  514. if fromEdbInfo.Frequency != "月度" {
  515. br.Msg = "请选择月度指标"
  516. return
  517. }
  518. edbInfo, err = models.AddCalculateLjzzy(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
  519. case utils.DATA_SOURCE_CALCULATE_TBZ:
  520. if req.Frequency != fromEdbInfo.Frequency {
  521. br.Msg = "当前频度和原指标频度不一致"
  522. return
  523. }
  524. if req.Unit != "无" {
  525. br.Msg = "单位只允许为无,禁止选择其他单位"
  526. return
  527. }
  528. sourName = "同比值"
  529. edbInfo, err = models.AddCalculateTbz(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
  530. case utils.DATA_SOURCE_CALCULATE_TCZ:
  531. if req.Frequency != fromEdbInfo.Frequency {
  532. br.Msg = "当前频度和原指标频度不一致"
  533. return
  534. }
  535. if req.Unit != fromEdbInfo.Unit {
  536. br.Msg = "当前单位和原指标单位不一致"
  537. return
  538. }
  539. sourName = "同差值"
  540. edbInfo, err = models.AddCalculateTcz(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
  541. case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS:
  542. sourName = "N数值移动平均计算"
  543. edbInfo, err = models.AddCalculateNszydpjjs(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName, formulaInt)
  544. case utils.DATA_SOURCE_CALCULATE_HBZ:
  545. checkCount, tmpErr := models.GetLteZeroEdbDataCount(fromEdbInfo.Source, fromEdbInfo.SubSource, fromEdbInfoId)
  546. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  547. br.Msg = "判断环比值是否可计算失败"
  548. br.ErrMsg = "判断环比值是否可计算失败,Err:" + tmpErr.Error()
  549. return
  550. }
  551. if checkCount > 0 {
  552. br.Msg = "原始数据中存在0或负数,该指标不能进行环比运算"
  553. br.ErrMsg = "原始数据中出现0和负值时,提示该指标不能进行环比运算"
  554. return
  555. }
  556. sourName = "环比值"
  557. edbInfo, err = models.AddCalculateHbz(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName, formulaInt)
  558. case utils.DATA_SOURCE_CALCULATE_HCZ:
  559. sourName = "环差值"
  560. edbInfo, err = models.AddCalculateHcz(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName, formulaInt)
  561. case utils.DATA_SOURCE_CALCULATE_BP:
  562. sourName = utils.DATA_SOURCE_NAME_CALCULATE_BP
  563. edbInfo, err = models.AddCalculateBp(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
  564. case utils.DATA_SOURCE_CALCULATE_TIME_SHIFT:
  565. sourName = "时间移位"
  566. edbInfo, err = models.AddCalculateTimeShift(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
  567. case utils.DATA_SOURCE_CALCULATE_ZJPJ:
  568. sourName = "直接拼接"
  569. if len(req.EdbInfoIdArr) != 1 {
  570. br.Msg = "请传入拼接日期之后的指标"
  571. br.ErrMsg = "请传入拼接日期之后的指标"
  572. return
  573. }
  574. secondEdbInfoReq := req.EdbInfoIdArr[0]
  575. secondEdbInfo, tmpErr := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  576. if tmpErr != nil {
  577. br.Msg = "获取拼接日期之后的指标信息失败"
  578. br.ErrMsg = "获取拼接日期之后的指标信息失败:Err:" + tmpErr.Error()
  579. return
  580. }
  581. if fromEdbInfo.EdbInfoId == secondEdbInfo.EdbInfoId {
  582. br.Msg = "两个指标不允许为同一个"
  583. br.ErrMsg = "两个指标不允许为同一个"
  584. return
  585. }
  586. edbInfo, err = models.AddCalculateZjpj(&req, fromEdbInfo, secondEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
  587. case utils.DATA_SOURCE_CALCULATE_LJZTBPJ:
  588. sourName = "累计值同比拼接"
  589. if fromEdbInfo.Frequency != "月度" {
  590. br.Msg = "待拼接指标只能筛选月度指标"
  591. br.ErrMsg = "待拼接指标只能筛选月度指标"
  592. return
  593. }
  594. if len(req.EdbInfoIdArr) != 1 {
  595. br.Msg = "请传入同比值指标"
  596. br.ErrMsg = "请传入同比值指标"
  597. return
  598. }
  599. secondEdbInfoReq := req.EdbInfoIdArr[0]
  600. tbzEdbInfo, tmpErr := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  601. if tmpErr != nil {
  602. br.Msg = "获取同比值指标信息失败"
  603. br.ErrMsg = "获取同比值指标信息失败:Err:" + tmpErr.Error()
  604. return
  605. }
  606. if tbzEdbInfo.Source != utils.DATA_SOURCE_CALCULATE_TBZ {
  607. br.Msg = "指标必须是传入同比值指标类型"
  608. br.ErrMsg = "指标必须是传入同比值指标类型"
  609. return
  610. }
  611. if tbzEdbInfo.Frequency != "月度" {
  612. br.Msg = "同比值指标只能筛选月度指标"
  613. br.ErrMsg = "同比值指标只能筛选月度指标"
  614. return
  615. }
  616. if fromEdbInfo.EdbInfoId == tbzEdbInfo.EdbInfoId {
  617. br.Msg = "两个指标不允许为同一个"
  618. br.ErrMsg = "两个指标不允许为同一个"
  619. return
  620. }
  621. edbInfo, err = models.AddCalculateLjztbpj(&req, fromEdbInfo, tbzEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
  622. case utils.DATA_SOURCE_CALCULATE_CJJX:
  623. sourName = "超季节性"
  624. edbInfo, err = models.AddCalculateCjjx(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName, formulaInt)
  625. case utils.DATA_SOURCE_CALCULATE_NHCC:
  626. sourName = "拟合残差"
  627. secondEdbInfoReq := req.EdbInfoIdArr[1]
  628. secondEdbInfo, tmpErr := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  629. if tmpErr != nil {
  630. br.Msg = "获取因变量的指标信息失败"
  631. br.ErrMsg = "获取因变量的指标信息失败:Err:" + tmpErr.Error()
  632. return
  633. }
  634. if fromEdbInfo.EdbInfoId == secondEdbInfo.EdbInfoId {
  635. br.Msg = "两个指标不允许为同一个"
  636. br.ErrMsg = "两个指标不允许为同一个"
  637. return
  638. }
  639. edbInfo, err, errMsg = models.AddCalculateNhcc(&req, fromEdbInfo, secondEdbInfo, edbCode, uniqueCode, nhccDate, sysUserId, sysUserName)
  640. case utils.DATA_SOURCE_CALCULATE_JP:
  641. if !models.CheckFrequency(fromEdbInfo.Frequency, req.Frequency) {
  642. br.Msg = "频度异常,不允许低频降频到高频"
  643. return
  644. }
  645. sourName = utils.DATA_SOURCE_NAME_CALCULATE_JP
  646. edbInfo, err = models.AddCalculateJp(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
  647. case utils.DATA_SOURCE_CALCULATE_NH:
  648. sourName = utils.DATA_SOURCE_NAME_CALCULATE_NH
  649. edbInfo, err = models.AddCalculateNh(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
  650. case utils.DATA_SOURCE_CALCULATE_KSZS:
  651. //关联的指标信息
  652. if len(req.EdbInfoIdArr) < 2 {
  653. br.Msg = "指标数量不能小于2个,请重新选择"
  654. br.ErrMsg = "指标数量不能小于2个,请重新选择"
  655. br.IsSendEmail = false
  656. return
  657. }
  658. sourName = utils.DATA_SOURCE_NAME_CALCULATE_KSZS
  659. edbInfo, err = models.AddCalculateKszs(&req, edbCode, uniqueCode, sysUserId, sysUserName)
  660. case utils.DATA_SOURCE_CALCULATE_CORRELATION:
  661. //关联的指标信息
  662. if len(req.EdbInfoIdArr) < 2 {
  663. br.Msg = "指标数量不能小于2个,请重新选择"
  664. br.ErrMsg = "指标数量不能小于2个,请重新选择"
  665. br.IsSendEmail = false
  666. return
  667. }
  668. sourName = utils.DATA_SOURCE_NAME_CALCULATE_CORRELATION
  669. edbInfo, err, errMsg = models.AddCalculateCorrelation(&req, edbCode, uniqueCode, sysUserId, sysUserName)
  670. case utils.DATA_SOURCE_CALCULATE_RJZ:
  671. if req.Frequency == "日度" {
  672. br.Msg = "日度指标无需进行日均值计算"
  673. return
  674. }
  675. if req.Frequency != fromEdbInfo.Frequency {
  676. br.Msg = "当前频度和原指标频度不一致"
  677. return
  678. }
  679. if req.Unit != fromEdbInfo.Unit {
  680. br.Msg = "单位只允许为和原指标频度保持一致,禁止选择其他单位"
  681. return
  682. }
  683. sourName = "日均值"
  684. edbInfo, err = models.AddCalculateRjz(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
  685. default:
  686. // 获取通用的数据源处理服务
  687. baseEdbInfoModel = models.GetBaseEdbInfoModel(req.Source)
  688. // 没有找到的话,那么就直接返回报错吧
  689. if baseEdbInfoModel == nil {
  690. br.Msg = "无效计算方式"
  691. br.ErrMsg = "无效计算方式,source:" + strconv.Itoa(req.Source)
  692. return
  693. }
  694. sourName = baseEdbInfoModel.GetSourceName()
  695. edbInfo, err, errMsg = baseEdbInfoModel.Add(addParam)
  696. }
  697. if err != nil {
  698. br.Msg = "生成" + sourName + "失败"
  699. if errMsg != `` {
  700. br.Msg = errMsg
  701. }
  702. br.ErrMsg = "生成" + sourName + "失败 Err:" + err.Error()
  703. return
  704. }
  705. if edbInfo == nil {
  706. br.Msg = "生成" + sourName + "失败"
  707. br.ErrMsg = "生成" + sourName + "失败,指标ID错误:" + strconv.Itoa(edbInfoId)
  708. return
  709. }
  710. edbInfoId = edbInfo.EdbInfoId
  711. // 更新ES
  712. go logic.UpdateEs(edbInfoId)
  713. //处理同名指标
  714. {
  715. edbNameList, err := models.GetEdbInfoByName(req.EdbName)
  716. if err != nil {
  717. br.Msg = "保存失败"
  718. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  719. return
  720. }
  721. if len(edbNameList) >= 2 {
  722. for _, v := range edbNameList {
  723. edbName := v.EdbName + "(" + v.SourceName + ")"
  724. err = models.ModifyEdbInfoNameSource(edbName, v.EdbInfoId)
  725. if err != nil {
  726. br.Msg = "保存失败"
  727. br.ErrMsg = "修改指标名称失败,Err:" + err.Error()
  728. return
  729. }
  730. }
  731. }
  732. }
  733. // 更新指标最大最小值
  734. err, errMsg = models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  735. if err != nil {
  736. br.Msg = errMsg
  737. br.ErrMsg = err.Error()
  738. return
  739. }
  740. newEdbInfo := new(models.EdbInfoEditRecord)
  741. newEdbInfo.EdbName = req.EdbName
  742. newEdbInfo.Frequency = req.Frequency
  743. newEdbInfo.Unit = req.Unit
  744. newEdbInfo.OperateUserId = req.AdminId
  745. newEdbInfo.OperateUserRealName = req.AdminName
  746. err = services.AddEditEdbInfoRcord(edbInfo, newEdbInfo)
  747. if err != nil {
  748. br.Msg = "保存失败"
  749. br.ErrMsg = "保存失败,Err:" + err.Error()
  750. return
  751. }
  752. // 判断是否需要禁用
  753. go services.DisableEdbInfoNoUpdate(edbInfo)
  754. resp := models.AddEdbInfoResp{
  755. EdbInfoId: edbInfo.EdbInfoId,
  756. UniqueCode: edbInfo.UniqueCode,
  757. }
  758. br.Ret = 200
  759. br.Success = true
  760. br.Msg = "保存成功"
  761. br.Data = resp
  762. br.IsAddLog = true
  763. }
  764. // BatchEdit
  765. // @Title 累计值转月-同比值-同差等计算新增
  766. // @Description 累计值转月-同比值-同差等计算新增接口
  767. // @Param request body models.EdbInfoCalculateBatchSaveReq true "type json string"
  768. // @Success Ret=200 返回指标id
  769. // @router /batch/edit [post]
  770. func (this *CalculateController) BatchEdit() {
  771. br := new(models.BaseResponse).Init()
  772. defer func() {
  773. this.Data["json"] = br
  774. this.ServeJSON()
  775. }()
  776. var req models.EdbInfoCalculateBatchEditReq
  777. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  778. if err != nil {
  779. br.Msg = "参数解析异常!"
  780. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  781. return
  782. }
  783. if req.EdbName == "" {
  784. br.Msg = "指标名称不能为空"
  785. return
  786. }
  787. if req.Frequency == "" {
  788. br.Msg = "频率不能为空"
  789. return
  790. }
  791. if req.Unit == "" {
  792. br.Msg = "单位不能为空"
  793. return
  794. }
  795. if req.ClassifyId <= 0 {
  796. br.Msg = "请选择分类"
  797. return
  798. }
  799. if req.EdbInfoId <= 0 {
  800. br.Msg = "请选择指标"
  801. return
  802. }
  803. // 根据指标名称和指标ID校验库中是否还存在其他同名指标
  804. existEdbName, err := logic.CheckExistByEdbNameAndEdbInfoId(utils.EDB_INFO_TYPE, req.EdbInfoId, req.EdbName, this.Lang)
  805. if err != nil {
  806. br.Msg = "判断指标名称是否存在失败"
  807. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  808. return
  809. }
  810. if existEdbName {
  811. br.Msg = "指标名称已存在,请重新填写"
  812. br.ErrMsg = "指标名称已存在,请重新填写"
  813. br.IsSendEmail = false
  814. return
  815. }
  816. edbInfo, err := models.GetEdbInfoById(req.EdbInfoId)
  817. if err != nil {
  818. if err.Error() == utils.ErrNoRow() {
  819. br.Msg = "指标已被删除,请刷新页面"
  820. br.ErrMsg = "指标已被删除,请刷新页面:Err:" + err.Error()
  821. return
  822. }
  823. br.Msg = "获取指标信息失败"
  824. br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
  825. return
  826. }
  827. // 记录原始指标信息
  828. oldEdbName := edbInfo.EdbName
  829. oldFrequency := edbInfo.Frequency
  830. oldUnit := edbInfo.Unit
  831. // 基础指标id
  832. fromEdbInfoId := req.FromEdbInfoId
  833. if req.Source <= 0 {
  834. req.Source = edbInfo.Source
  835. }
  836. var formulaInt int
  837. var nhccDate models.NhccDate // 拟合残差的日期
  838. // 初期的参数校验
  839. switch req.Source {
  840. 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:
  841. if req.Formula == "" {
  842. br.Msg = "请填写N值"
  843. return
  844. }
  845. formulaInt, _ = strconv.Atoi(req.Formula)
  846. if formulaInt <= 0 {
  847. br.Msg = "N值输入错误,请重新输入"
  848. return
  849. }
  850. case utils.DATA_SOURCE_CALCULATE_ZJPJ:
  851. //直接拼接指标
  852. //校验时间格式
  853. _, err = time.ParseInLocation(utils.FormatDate, req.Formula, time.Local)
  854. if err != nil {
  855. br.Msg = "拼接日期有误,请重新输入"
  856. return
  857. }
  858. case utils.DATA_SOURCE_CALCULATE_NHCC: //拟合残差指标
  859. //指标校验
  860. if len(req.EdbInfoIdArr) != 2 {
  861. br.Msg = "选择的指标异常,请重新选择"
  862. return
  863. }
  864. fromEdbInfoId = req.EdbInfoIdArr[0].EdbInfoId
  865. //校验时间格式
  866. //数据格式:2022-11-01,2022-11-10
  867. timeList := strings.Split(req.Formula, ",")
  868. if len(timeList) != 2 {
  869. br.Msg = "选择时间有误,请重新输入"
  870. return
  871. }
  872. startDate, err := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
  873. if err != nil {
  874. br.Msg = "开始日期有误,请重新输入"
  875. return
  876. }
  877. endDate, err := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
  878. if err != nil {
  879. br.Msg = "结束日期有误,请重新输入"
  880. return
  881. }
  882. if utils.GetTimeSubDay(startDate, endDate) < 2 {
  883. br.Msg = "日期间隔不得少于两天"
  884. return
  885. }
  886. nhccDate.StartDate = startDate
  887. nhccDate.EndDate = endDate
  888. case utils.DATA_SOURCE_CALCULATE_JP:
  889. if req.Formula != "期末值" && req.Formula != "平均值" {
  890. br.Msg = "数据取值类型错误:" + req.Formula
  891. return
  892. }
  893. case utils.DATA_SOURCE_CALCULATE_ZSXY: // 指数修匀
  894. if req.Formula == "" {
  895. br.Msg = "请填写alpha值"
  896. return
  897. }
  898. a, e := strconv.ParseFloat(req.Formula, 64)
  899. if e != nil {
  900. br.Msg = "alpha值输入错误, 请重新输入"
  901. return
  902. }
  903. if a <= 0 || a >= 1 {
  904. br.Msg = "alpha值输入错误, 请重新输入"
  905. return
  906. }
  907. }
  908. var fromEdbInfo *models.EdbInfo
  909. if fromEdbInfoId > 0 {
  910. // 获取基础指标信息
  911. fromEdbInfo, err = models.GetEdbInfoById(fromEdbInfoId)
  912. if err != nil {
  913. if err.Error() == utils.ErrNoRow() {
  914. br.Msg = "指标已被删除,请刷新页面"
  915. br.ErrMsg = "指标已被删除,请刷新页面:Err:" + err.Error()
  916. return
  917. }
  918. br.Msg = "获取指标信息失败"
  919. br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
  920. return
  921. }
  922. }
  923. //dataUpdateTime := time.Now().Format(utils.FormatDateTime)
  924. // 额外赋值
  925. switch this.Lang {
  926. case utils.EnLangVersion:
  927. req.EdbNameEn = req.EdbName
  928. req.UnitEn = req.Unit
  929. req.EdbName = edbInfo.EdbName
  930. req.Unit = edbInfo.Unit
  931. default:
  932. req.EdbNameEn = edbInfo.EdbNameEn
  933. req.UnitEn = edbInfo.UnitEn
  934. }
  935. var sourName, errMsg string
  936. var edbInfoId int
  937. var baseEdbInfoModel models.BaseEdbInfoInterface
  938. editParams := models.EditCalculateBatchParams{
  939. Req: &req,
  940. EdbInfo: edbInfo,
  941. FromEdbInfo: fromEdbInfo,
  942. }
  943. switch req.Source {
  944. case utils.DATA_SOURCE_CALCULATE_LJZZY:
  945. sourName = "累计值转月值"
  946. if fromEdbInfo.Frequency != "月度" {
  947. br.Msg = "请选择月度指标"
  948. return
  949. }
  950. err = models.EditCalculateLjzzy(edbInfo, &req, fromEdbInfo)
  951. case utils.DATA_SOURCE_CALCULATE_TBZ:
  952. sourName = "同比值"
  953. err = models.EditCalculateTbz(edbInfo, &req, fromEdbInfo)
  954. case utils.DATA_SOURCE_CALCULATE_TCZ:
  955. fmt.Println("start edit", time.Now())
  956. sourName = "同差值"
  957. err = models.EditCalculateTcz(edbInfo, &req, fromEdbInfo)
  958. fmt.Println("end edit", time.Now())
  959. case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS:
  960. sourName = "N数值移动平均计算"
  961. err = models.EditCalculateNszydpjjs(edbInfo, &req, fromEdbInfo, formulaInt, edbInfo.CalculateFormula)
  962. case utils.DATA_SOURCE_CALCULATE_HBZ:
  963. checkCount, tmpErr := models.GetLteZeroEdbDataCount(fromEdbInfo.Source, fromEdbInfo.SubSource, req.FromEdbInfoId)
  964. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  965. br.Msg = "判断环比值是否可计算失败"
  966. br.ErrMsg = "判断环比值是否可计算失败,Err:" + tmpErr.Error()
  967. return
  968. }
  969. if checkCount > 0 {
  970. br.Msg = "原始数据中存在0或负数,该指标不能进行环比运算"
  971. br.ErrMsg = "原始数据中出现0和负值时,提示该指标不能进行环比运算"
  972. return
  973. }
  974. sourName = "环比值"
  975. err = models.EditCalculateHbz(edbInfo, &req, fromEdbInfo, formulaInt, edbInfo.CalculateFormula)
  976. case utils.DATA_SOURCE_CALCULATE_HCZ:
  977. sourName = "环差值"
  978. err = models.EditCalculateHcz(edbInfo, &req, fromEdbInfo, formulaInt, edbInfo.CalculateFormula)
  979. case utils.DATA_SOURCE_CALCULATE_BP:
  980. sourName = utils.DATA_SOURCE_NAME_CALCULATE_BP
  981. err = models.EditCalculateBp(edbInfo, &req, fromEdbInfo)
  982. case utils.DATA_SOURCE_CALCULATE_TIME_SHIFT:
  983. sourName = "时间移位"
  984. err = models.EditCalculateTimeShift(edbInfo, &req, fromEdbInfo)
  985. case utils.DATA_SOURCE_CALCULATE_ZJPJ:
  986. sourName = "直接拼接"
  987. if len(req.EdbInfoIdArr) != 1 {
  988. br.Msg = "请传入拼接日期之后的指标"
  989. br.ErrMsg = "请传入拼接日期之后的指标"
  990. return
  991. }
  992. secondEdbInfoReq := req.EdbInfoIdArr[0]
  993. secondEdbInfo, tmpErr := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  994. if tmpErr != nil {
  995. br.Msg = "获取拼接日期之后的指标信息失败"
  996. br.ErrMsg = "获取拼接日期之后的指标信息失败:Err:" + tmpErr.Error()
  997. return
  998. }
  999. if fromEdbInfo.EdbInfoId == secondEdbInfo.EdbInfoId {
  1000. br.Msg = "两个指标不允许为同一个"
  1001. br.ErrMsg = "两个指标不允许为同一个"
  1002. return
  1003. }
  1004. err = models.EditCalculateZjpj(&req, edbInfo, fromEdbInfo, secondEdbInfo)
  1005. case utils.DATA_SOURCE_CALCULATE_LJZTBPJ: //累计值同比拼接
  1006. sourName = "累计值同比拼接"
  1007. if fromEdbInfo.Frequency != "月度" {
  1008. br.Msg = "待拼接指标只能筛选月度指标"
  1009. br.ErrMsg = "待拼接指标只能筛选月度指标"
  1010. return
  1011. }
  1012. if len(req.EdbInfoIdArr) != 1 {
  1013. br.Msg = "请传入同比值指标"
  1014. br.ErrMsg = "请传入同比值指标"
  1015. return
  1016. }
  1017. secondEdbInfoReq := req.EdbInfoIdArr[0]
  1018. tbzEdbInfo, tmpErr := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  1019. if tmpErr != nil {
  1020. br.Msg = "获取同比值指标信息失败"
  1021. br.ErrMsg = "获取同比值指标信息失败:Err:" + tmpErr.Error()
  1022. return
  1023. }
  1024. if tbzEdbInfo.Source != utils.DATA_SOURCE_CALCULATE_TBZ {
  1025. br.Msg = "指标必须是传入同比值指标类型"
  1026. br.ErrMsg = "指标必须是传入同比值指标类型"
  1027. return
  1028. }
  1029. if tbzEdbInfo.Frequency != "月度" {
  1030. br.Msg = "同比值指标只能筛选月度指标"
  1031. br.ErrMsg = "同比值指标只能筛选月度指标"
  1032. return
  1033. }
  1034. if fromEdbInfo.EdbInfoId == tbzEdbInfo.EdbInfoId {
  1035. br.Msg = "两个指标不允许为同一个"
  1036. br.ErrMsg = "两个指标不允许为同一个"
  1037. return
  1038. }
  1039. err = models.EditCalculateLjztbpj(&req, edbInfo, fromEdbInfo, tbzEdbInfo)
  1040. case utils.DATA_SOURCE_CALCULATE_CJJX:
  1041. sourName = "超季节性"
  1042. err = models.EditCalculateCjjx(&req, edbInfo, fromEdbInfo, formulaInt)
  1043. case utils.DATA_SOURCE_CALCULATE_NHCC:
  1044. sourName = "拟合残差"
  1045. secondEdbInfoReq := req.EdbInfoIdArr[1]
  1046. secondEdbInfo, tmpErr := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  1047. if tmpErr != nil {
  1048. br.Msg = "获取因变量的指标信息失败"
  1049. br.ErrMsg = "获取因变量的指标信息失败:Err:" + tmpErr.Error()
  1050. return
  1051. }
  1052. if fromEdbInfo.EdbInfoId == secondEdbInfo.EdbInfoId {
  1053. br.Msg = "两个指标不允许为同一个"
  1054. br.ErrMsg = "两个指标不允许为同一个"
  1055. return
  1056. }
  1057. err, errMsg = models.EditCalculateNhcc(&req, edbInfo, fromEdbInfo, secondEdbInfo, nhccDate)
  1058. case utils.DATA_SOURCE_CALCULATE_JP:
  1059. if !models.CheckFrequency(fromEdbInfo.Frequency, req.Frequency) {
  1060. br.Msg = "频度异常,不允许低频降频到高频"
  1061. return
  1062. }
  1063. sourName = utils.DATA_SOURCE_NAME_CALCULATE_JP
  1064. err = models.EditCalculateJp(edbInfo, &req, fromEdbInfo)
  1065. case utils.DATA_SOURCE_CALCULATE_NH:
  1066. sourName = utils.DATA_SOURCE_NAME_CALCULATE_NH
  1067. err = models.EditCalculateNh(edbInfo, &req, fromEdbInfo)
  1068. case utils.DATA_SOURCE_CALCULATE_KSZS:
  1069. //关联的指标信息
  1070. if len(req.EdbInfoIdArr) < 2 {
  1071. br.Msg = "指标数量不能小于2个,请重新选择"
  1072. br.ErrMsg = "指标数量不能小于2个,请重新选择"
  1073. br.IsSendEmail = false
  1074. return
  1075. }
  1076. sourName = utils.DATA_SOURCE_NAME_CALCULATE_KSZS
  1077. err = models.EditCalculateKszs(edbInfo, &req)
  1078. case utils.DATA_SOURCE_CALCULATE_CORRELATION:
  1079. //关联的指标信息
  1080. if len(req.EdbInfoIdArr) < 2 {
  1081. br.Msg = "指标数量不能小于2个,请重新选择"
  1082. br.ErrMsg = "指标数量不能小于2个,请重新选择"
  1083. br.IsSendEmail = false
  1084. return
  1085. }
  1086. sourName = utils.DATA_SOURCE_NAME_CALCULATE_CORRELATION
  1087. err, errMsg = models.EditCalculateCorrelation(edbInfo, &req)
  1088. case utils.DATA_SOURCE_CALCULATE_RJZ:
  1089. if req.Frequency == "日度" {
  1090. br.Msg = "日度指标无需进行日均值计算"
  1091. return
  1092. }
  1093. if req.Frequency != fromEdbInfo.Frequency {
  1094. br.Msg = "当前频度和原指标频度不一致"
  1095. return
  1096. }
  1097. if req.Unit != fromEdbInfo.Unit {
  1098. br.Msg = "单位只允许为和原指标频度保持一致,禁止选择其他单位"
  1099. return
  1100. }
  1101. sourName = "日均值"
  1102. err = models.EditCalculateRjz(edbInfo, &req, fromEdbInfo)
  1103. default:
  1104. // 获取通用的数据源处理服务
  1105. baseEdbInfoModel = models.GetBaseEdbInfoModel(req.Source)
  1106. // 没有找到的话,那么就直接返回报错吧
  1107. if baseEdbInfoModel == nil {
  1108. br.Msg = "无效计算方式"
  1109. br.ErrMsg = "无效计算方式,source:" + strconv.Itoa(req.Source)
  1110. return
  1111. }
  1112. sourName = baseEdbInfoModel.GetSourceName()
  1113. err, errMsg = baseEdbInfoModel.Edit(editParams)
  1114. }
  1115. if err != nil {
  1116. br.Msg = "生成" + sourName + "失败"
  1117. if errMsg != `` {
  1118. br.Msg = errMsg
  1119. }
  1120. br.Msg = "生成" + sourName + "失败 Err:" + err.Error()
  1121. return
  1122. }
  1123. if edbInfo == nil {
  1124. br.Msg = "生成" + sourName + "失败"
  1125. br.ErrMsg = "生成" + sourName + "失败,指标ID错误:" + strconv.Itoa(edbInfoId)
  1126. return
  1127. }
  1128. edbInfoId = edbInfo.EdbInfoId
  1129. //处理同名指标
  1130. {
  1131. edbNameList, err := models.GetEdbInfoByName(req.EdbName)
  1132. if err != nil {
  1133. br.Msg = "保存失败"
  1134. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  1135. return
  1136. }
  1137. if len(edbNameList) >= 2 {
  1138. for _, v := range edbNameList {
  1139. edbName := v.EdbName + "(" + v.SourceName + ")"
  1140. err = models.ModifyEdbInfoNameSource(edbName, v.EdbInfoId)
  1141. if err != nil {
  1142. br.Msg = "保存失败"
  1143. br.ErrMsg = "修改指标名称失败,Err:" + err.Error()
  1144. return
  1145. }
  1146. }
  1147. }
  1148. }
  1149. // 更新指标最大最小值
  1150. //err, errMsg = models.UnifiedModifyEdbInfoMaxAndMinInfoTmp(edbInfo, dataUpdateTime)
  1151. err, errMsg = models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  1152. if err != nil {
  1153. br.Msg = errMsg
  1154. br.ErrMsg = err.Error()
  1155. return
  1156. }
  1157. // 记录指标的操作记录
  1158. oldEdbInfo := new(models.EdbInfo)
  1159. oldEdbInfo.EdbInfoId = edbInfo.EdbInfoId
  1160. oldEdbInfo.EdbName = oldEdbName
  1161. oldEdbInfo.Frequency = oldFrequency
  1162. oldEdbInfo.Unit = oldUnit
  1163. newEdbInfoRecord := new(models.EdbInfoEditRecord)
  1164. newEdbInfoRecord.EdbName = req.EdbName
  1165. newEdbInfoRecord.Frequency = req.Frequency
  1166. newEdbInfoRecord.Unit = req.Unit
  1167. newEdbInfoRecord.OperateUserId = req.AdminId
  1168. newEdbInfoRecord.OperateUserRealName = req.AdminName
  1169. err = services.AddEditEdbInfoRcord(oldEdbInfo, newEdbInfoRecord)
  1170. if err != nil {
  1171. br.Msg = "保存失败"
  1172. br.ErrMsg = "记录指标操作记录失败,Err:" + err.Error()
  1173. }
  1174. // 重置计算指标中的引用关系
  1175. go services.ResetEdbRelation(edbInfoId)
  1176. // 更新ES
  1177. go logic.UpdateEs(edbInfo.EdbInfoId)
  1178. resp := models.AddEdbInfoResp{
  1179. EdbInfoId: edbInfo.EdbInfoId,
  1180. UniqueCode: edbInfo.UniqueCode,
  1181. }
  1182. br.Ret = 200
  1183. br.Success = true
  1184. br.Msg = "保存成功"
  1185. br.Data = resp
  1186. br.IsAddLog = true
  1187. }
  1188. // Refresh
  1189. // @Title 刷新计算指标接口
  1190. // @Description 刷新计算指标接口
  1191. // @Success 200 {object} models.RefreshEdbInfoReq
  1192. // @router /refresh [post]
  1193. func (this *CalculateController) Refresh() {
  1194. br := new(models.BaseResponse).Init()
  1195. var cacheKey string
  1196. defer func() {
  1197. this.Data["json"] = br
  1198. this.ServeJSON()
  1199. }()
  1200. var req models.RefreshEdbInfoReq
  1201. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1202. if err != nil {
  1203. br.Msg = "参数解析异常!"
  1204. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1205. return
  1206. }
  1207. if req.EdbCode == "" {
  1208. br.Msg = "请输入指标编码!"
  1209. br.ErrMsg = "请输入指标编码,指标编码为空"
  1210. return
  1211. }
  1212. if req.EdbInfoId <= 0 {
  1213. br.Msg = "请输入指标ID!"
  1214. br.ErrMsg = "请输入指标ID"
  1215. return
  1216. }
  1217. edbInfo, err := models.GetEdbInfoById(req.EdbInfoId)
  1218. if err != nil {
  1219. br.Msg = "指标不存在!"
  1220. br.ErrMsg = "指标不存在"
  1221. return
  1222. }
  1223. cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(edbInfo.Source) + "_" + req.EdbCode
  1224. if utils.Rc.IsExist(cacheKey) {
  1225. br.Ret = 501
  1226. br.Success = true
  1227. br.Msg = "系统处理中,请稍后重试"
  1228. return
  1229. }
  1230. // 刷新指标
  1231. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  1232. defer func() {
  1233. _ = utils.Rc.Delete(cacheKey)
  1234. }()
  1235. startDate := req.StartDate
  1236. var errMsg string
  1237. endDate := time.Now().Format(utils.FormatDate)
  1238. edbInfoId := edbInfo.EdbInfoId
  1239. source := edbInfo.Source
  1240. subSource := edbInfo.SubSource
  1241. var baseEdbInfoModel models.BaseEdbInfoInterface
  1242. refreshParams := models.RefreshParams{
  1243. EdbInfo: edbInfo,
  1244. StartDate: "", // 更新所有
  1245. EndDate: "", // 更新所有数据
  1246. }
  1247. switch source {
  1248. case utils.DATA_SOURCE_CALCULATE:
  1249. //startDate = edbInfo.StartDate
  1250. //sTime, err := time.ParseInLocation(utils.FormatDate, edbInfo.EndDate,time.Local)
  1251. //if err != nil {
  1252. // return
  1253. //}
  1254. //startDate = sTime.Format(utils.FormatDate)
  1255. startDate = ""
  1256. var edbInfoIdBytes []string
  1257. calculateMap, err := models.GetEdbInfoCalculateDetailList(edbInfo.EdbInfoId)
  1258. if err != nil {
  1259. errMsg = "GetEdbInfoCalculateDetail Err:" + err.Error()
  1260. break
  1261. }
  1262. var formulaStr string
  1263. edbInfoList := make([]*models.EdbInfo, 0)
  1264. edbInfoTag := make(map[string]int)
  1265. for _, v := range calculateMap {
  1266. edbInfoTag[v.FromTag] = v.FromEdbInfoId
  1267. formulaStr += v.FromTag + ","
  1268. edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
  1269. edbInfo, _ := models.GetEdbInfoById(v.FromEdbInfoId)
  1270. edbInfoList = append(edbInfoList, edbInfo)
  1271. }
  1272. err = models.RefreshAllCalculate(edbInfoList, edbInfoTag, edbInfo.EdbInfoId, source, subSource, edbInfo.EdbCode, edbInfo.CalculateFormula, startDate, endDate, edbInfoIdBytes, edbInfo.EmptyType, edbInfo.MaxEmptyType, edbInfo.Extra)
  1273. if err != nil && err.Error() != utils.ErrNoRow() {
  1274. errMsg = "RefreshCalculate Err:" + err.Error()
  1275. break
  1276. }
  1277. case utils.DATA_SOURCE_CALCULATE_LJZZY: //刷新累计值转月值
  1278. calculateLjzzy, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1279. if err != nil {
  1280. errMsg = "GetEdbInfoCalculateLjzzyDetail Err:" + err.Error()
  1281. break
  1282. }
  1283. fromEdbInfo, err := models.GetEdbInfoById(calculateLjzzy.FromEdbInfoId)
  1284. if err != nil {
  1285. errMsg = "GetEdbInfoById Err:" + err.Error()
  1286. break
  1287. }
  1288. //startDate = edbInfo.StartDate
  1289. //endDate = time.Now().Format(utils.FormatDate)
  1290. endDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
  1291. startDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
  1292. err = models.RefreshAllCalculateLjzzy(edbInfoId, source, subSource, fromEdbInfo, calculateLjzzy.EdbCode, startDate, endDate)
  1293. if err != nil && err.Error() != utils.ErrNoRow() {
  1294. errMsg = "RefreshAllCalculateLjzzy Err:" + err.Error()
  1295. break
  1296. }
  1297. case utils.DATA_SOURCE_CALCULATE_TBZ: //刷新同比值
  1298. calculateTbz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1299. if err != nil {
  1300. errMsg = "GetEdbInfoCalculateTbzDetail Err:" + err.Error()
  1301. break
  1302. }
  1303. fromEdbInfo, err := models.GetEdbInfoById(calculateTbz.FromEdbInfoId)
  1304. if err != nil {
  1305. errMsg = "GetEdbInfoById Err:" + err.Error()
  1306. break
  1307. }
  1308. //startDate = edbInfo.StartDate
  1309. endDate = time.Now().Format(utils.FormatDate)
  1310. err = models.RefreshAllCalculateTbz(edbInfoId, source, subSource, fromEdbInfo, calculateTbz.EdbCode, startDate, endDate)
  1311. if err != nil && err.Error() != utils.ErrNoRow() {
  1312. errMsg = "RefreshAllCalculateTbz Err:" + err.Error()
  1313. break
  1314. }
  1315. case utils.DATA_SOURCE_CALCULATE_TCZ: //同差值
  1316. calculateTcz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1317. if err != nil {
  1318. errMsg = "GetEdbInfoCalculateTczDetail Err:" + err.Error()
  1319. break
  1320. }
  1321. fromEdbInfo, err := models.GetEdbInfoById(calculateTcz.FromEdbInfoId)
  1322. if err != nil {
  1323. errMsg = "GetEdbInfoById Err:" + err.Error()
  1324. break
  1325. }
  1326. //startDate = edbInfo.StartDate
  1327. endDate = time.Now().Format(utils.FormatDate)
  1328. err = models.RefreshAllCalculateTcz(edbInfoId, source, subSource, fromEdbInfo, calculateTcz.EdbCode, startDate, endDate)
  1329. if err != nil && err.Error() != utils.ErrNoRow() {
  1330. errMsg = "RefreshCalculateTcz Err:" + err.Error()
  1331. break
  1332. }
  1333. case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS: //N数值移动平均计算
  1334. calculateNszydpjjs, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1335. if err != nil {
  1336. errMsg = "GetEdbInfoCalculateNszydpjjsDetail Err:" + err.Error()
  1337. break
  1338. }
  1339. fromEdbInfo, err := models.GetEdbInfoById(calculateNszydpjjs.FromEdbInfoId)
  1340. if err != nil {
  1341. errMsg = "GetEdbInfoById Err:" + err.Error()
  1342. break
  1343. }
  1344. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  1345. //startDate = edbInfo.StartDate
  1346. startDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
  1347. err = models.RefreshAllCalculateNszydpjjs(edbInfoId, edbInfo.Source, edbInfo.SubSource, formulaInt, fromEdbInfo, calculateNszydpjjs.EdbCode, startDate)
  1348. if err != nil && err.Error() != utils.ErrNoRow() {
  1349. errMsg = "RefreshCalculateNszydpjjs Err:" + err.Error()
  1350. break
  1351. }
  1352. case utils.DATA_SOURCE_CALCULATE_HBZ: //刷新环比值
  1353. calculateTbz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1354. if err != nil {
  1355. errMsg = "GetEdbInfoCalculateHbzDetail Err:" + err.Error()
  1356. break
  1357. }
  1358. fromEdbInfo, err := models.GetEdbInfoById(calculateTbz.FromEdbInfoId)
  1359. if err != nil {
  1360. errMsg = "GetEdbInfoById Err:" + err.Error()
  1361. break
  1362. }
  1363. startDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
  1364. endDate = time.Now().Format(utils.FormatDate)
  1365. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  1366. err = models.RefreshAllCalculateHbz(edbInfoId, source, subSource, fromEdbInfo, calculateTbz.EdbCode, startDate, endDate, formulaInt)
  1367. if err != nil && err.Error() != utils.ErrNoRow() {
  1368. errMsg = "RefreshAllCalculateHbz Err:" + err.Error()
  1369. break
  1370. }
  1371. case utils.DATA_SOURCE_CALCULATE_HCZ: //刷新环差值
  1372. calculateTbz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1373. if err != nil {
  1374. errMsg = "GetEdbInfoCalculateHczDetail Err:" + err.Error()
  1375. break
  1376. }
  1377. fromEdbInfo, err := models.GetEdbInfoById(calculateTbz.FromEdbInfoId)
  1378. if err != nil {
  1379. errMsg = "GetEdbInfoById Err:" + err.Error()
  1380. break
  1381. }
  1382. startDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
  1383. endDate = time.Now().Format(utils.FormatDate)
  1384. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  1385. err = models.RefreshAllCalculateHcz(edbInfoId, source, subSource, fromEdbInfo, calculateTbz.EdbCode, startDate, endDate, formulaInt)
  1386. if err != nil && err.Error() != utils.ErrNoRow() {
  1387. errMsg = "RefreshAllCalculateHcz Err:" + err.Error()
  1388. break
  1389. }
  1390. case utils.DATA_SOURCE_CALCULATE_BP: //刷新升频
  1391. calculateTbz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1392. if err != nil {
  1393. errMsg = "GetEdbInfoCalculateTbzDetail Err:" + err.Error()
  1394. break
  1395. }
  1396. fromEdbInfo, err := models.GetEdbInfoById(calculateTbz.FromEdbInfoId)
  1397. if err != nil {
  1398. errMsg = "GetEdbInfoById Err:" + err.Error()
  1399. break
  1400. }
  1401. //startDate = edbInfo.StartDate
  1402. endDate = time.Now().Format(utils.FormatDate)
  1403. err = models.RefreshAllCalculateBp(edbInfoId, source, subSource, fromEdbInfo, calculateTbz.EdbCode, startDate, endDate, edbInfo.EmptyType)
  1404. if err != nil && err.Error() != utils.ErrNoRow() {
  1405. errMsg = "RefreshAllCalculateBp Err:" + err.Error()
  1406. break
  1407. }
  1408. case utils.DATA_SOURCE_CALCULATE_TIME_SHIFT:
  1409. calculate, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1410. if err != nil {
  1411. errMsg = "GetEdbInfoCalculateTbzDetail Err:" + err.Error()
  1412. break
  1413. }
  1414. fromEdbInfo, err := models.GetEdbInfoById(calculate.FromEdbInfoId)
  1415. if err != nil {
  1416. errMsg = "GetEdbInfoById Err:" + err.Error()
  1417. break
  1418. }
  1419. //startDate = edbInfo.StartDate
  1420. startDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
  1421. endDate = time.Now().Format(utils.FormatDate)
  1422. formulaInt, _ := strconv.Atoi(calculate.CalculateFormula)
  1423. err = models.RefreshAllCalculateTimeShift(edbInfoId, source, subSource, formulaInt, calculate.MoveType, fromEdbInfo, calculate.EdbCode, startDate, endDate, calculate.MoveFrequency)
  1424. if err != nil && err.Error() != utils.ErrNoRow() {
  1425. errMsg = "RefreshAllCalculateTimeShift Err:" + err.Error()
  1426. }
  1427. case utils.DATA_SOURCE_CALCULATE_ZJPJ: //刷新直接拼接
  1428. err = models.RefreshAllCalculateZjpj(edbInfo)
  1429. if err != nil && err.Error() != utils.ErrNoRow() {
  1430. errMsg = "RefreshAllCalculateZjpj Err:" + err.Error()
  1431. break
  1432. }
  1433. case utils.DATA_SOURCE_CALCULATE_LJZTBPJ: //刷新累计值同比拼接
  1434. err = models.RefreshAllCalculateLjztbpj(edbInfo)
  1435. if err != nil && err.Error() != utils.ErrNoRow() {
  1436. errMsg = "RefreshAllCalculateLjztbpj Err:" + err.Error()
  1437. break
  1438. }
  1439. case utils.DATA_SOURCE_PYTHON: //python代码运算
  1440. edbPythonCode, err := models.GetEdbPythonCodeById(edbInfo.EdbInfoId)
  1441. if err != nil {
  1442. errMsg = "获取python代码失败 Err:" + err.Error()
  1443. break
  1444. }
  1445. edbData, err, errMsg := services.ExecPythonCode(edbInfo.EdbCode, edbPythonCode.PythonCode)
  1446. if err != nil {
  1447. br.Msg = "获取数据失败"
  1448. br.ErrMsg = "python代码获取数据失败,err:" + err.Error()
  1449. if errMsg != "" {
  1450. br.Msg = errMsg
  1451. }
  1452. return
  1453. }
  1454. err = models.RefreshAllPythonEdb(edbInfo, edbData)
  1455. if err != nil && err.Error() != utils.ErrNoRow() {
  1456. errMsg = "RefreshAllPythonEdb Err:" + err.Error()
  1457. break
  1458. }
  1459. case utils.DATA_SOURCE_CALCULATE_CJJX: //超季节性
  1460. calculateCjjx, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1461. if err != nil {
  1462. errMsg = "GetEdbInfoCalculateNszydpjjsDetail Err:" + err.Error()
  1463. break
  1464. }
  1465. fromEdbInfo, err := models.GetEdbInfoById(calculateCjjx.FromEdbInfoId)
  1466. if err != nil {
  1467. errMsg = "GetEdbInfoById Err:" + err.Error()
  1468. break
  1469. }
  1470. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  1471. startDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
  1472. err = models.RefreshAllCalculateCjjx(edbInfoId, edbInfo.Source, edbInfo.SubSource, fromEdbInfo, calculateCjjx.EdbCode, startDate, "", edbInfo.Calendar, formulaInt)
  1473. if err != nil && err.Error() != utils.ErrNoRow() {
  1474. errMsg = "RefreshAllCalculateCjjx Err:" + err.Error()
  1475. break
  1476. }
  1477. case utils.DATA_SOURCE_CALCULATE_NHCC: //nhcc
  1478. err = models.RefreshAllCalculateNhcc(edbInfo)
  1479. if err != nil && err.Error() != utils.ErrNoRow() {
  1480. errMsg = "RefreshAllCalculateNhcc Err:" + err.Error()
  1481. break
  1482. }
  1483. case utils.DATA_SOURCE_CALCULATE_ADJUST:
  1484. calculateMapping, err := models.GetEdbInfoCalculateMappingDetail(edbInfo.EdbInfoId)
  1485. if err != nil {
  1486. br.Msg = "获取来源指标异常!"
  1487. br.ErrMsg = "获取来源指标异常,Err:" + err.Error()
  1488. return
  1489. }
  1490. fromEdbInfo, err := models.GetEdbInfoById(calculateMapping.FromEdbInfoId)
  1491. if err != nil {
  1492. br.Msg = "来源指标不存在!"
  1493. br.ErrMsg = "来源指标不存在"
  1494. return
  1495. }
  1496. err = models.RefreshAllAdjustEdb(edbInfo, fromEdbInfo)
  1497. if err != nil && err.Error() != utils.ErrNoRow() {
  1498. errMsg = "RefreshAllAdjustEdb Err:" + err.Error()
  1499. break
  1500. }
  1501. case utils.DATA_SOURCE_CALCULATE_JP: //刷新降频
  1502. calculateMapping, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1503. if err != nil {
  1504. errMsg = "GetEdbInfoCalculateTbzDetail Err:" + err.Error()
  1505. break
  1506. }
  1507. fromEdbInfo, err := models.GetEdbInfoById(calculateMapping.FromEdbInfoId)
  1508. if err != nil {
  1509. errMsg = "GetEdbInfoById Err:" + err.Error()
  1510. break
  1511. }
  1512. //startDate = edbInfo.StartDate
  1513. endDate = time.Now().Format(utils.FormatDate)
  1514. err = models.RefreshAllCalculateJp(edbInfoId, source, subSource, fromEdbInfo, edbInfo.EdbCode, edbInfo.Frequency, edbInfo.CalculateFormula)
  1515. if err != nil && err.Error() != utils.ErrNoRow() {
  1516. errMsg = "RefreshAllCalculateJp Err:" + err.Error()
  1517. break
  1518. }
  1519. case utils.DATA_SOURCE_CALCULATE_NH: //刷新年化
  1520. calculateMapping, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1521. if err != nil {
  1522. errMsg = "GetEdbInfoCalculateTbzDetail Err:" + err.Error()
  1523. break
  1524. }
  1525. fromEdbInfo, err := models.GetEdbInfoById(calculateMapping.FromEdbInfoId)
  1526. if err != nil {
  1527. errMsg = "GetEdbInfoById Err:" + err.Error()
  1528. break
  1529. }
  1530. //startDate = edbInfo.StartDate
  1531. endDate = time.Now().Format(utils.FormatDate)
  1532. err = models.RefreshAllCalculateNh(edbInfoId, source, subSource, fromEdbInfo, edbInfo.EdbCode)
  1533. if err != nil && err.Error() != utils.ErrNoRow() {
  1534. errMsg = "RefreshAllCalculateNh Err:" + err.Error()
  1535. break
  1536. }
  1537. case utils.DATA_SOURCE_CALCULATE_KSZS: //刷新扩散指数
  1538. //startDate = edbInfo.StartDate
  1539. endDate = time.Now().Format(utils.FormatDate)
  1540. err = models.RefreshAllCalculateKszs(edbInfo)
  1541. if err != nil && err.Error() != utils.ErrNoRow() {
  1542. errMsg = "RefreshAllCalculateKszs Err:" + err.Error()
  1543. break
  1544. }
  1545. case utils.DATA_SOURCE_STOCK_PLANT: //存量装置
  1546. //startDate = edbInfo.StartDate
  1547. endDate = time.Now().Format(utils.FormatDate)
  1548. req.StartDate = `` // 开始日期也给过滤吧,万一研究员补充了之前的数据呢,还是过滤掉吧
  1549. err = models.RefreshEdbDataFromStockPlant(req.EdbInfoId, req.EdbCode, req.StartDate)
  1550. if err != nil && err.Error() != utils.ErrNoRow() {
  1551. errMsg = "RefreshEdbDataFromStockPlant Err:" + err.Error()
  1552. break
  1553. }
  1554. case utils.DATA_SOURCE_CALCULATE_CORRELATION:
  1555. err, errMsg = models.RefreshAllCalculateCorrelation(edbInfo)
  1556. case utils.DATA_SOURCE_CALCULATE_RJZ: //刷新日均值
  1557. calculateRjz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1558. if err != nil {
  1559. errMsg = "GetEdbInfoCalculateMappingDetail Err:" + err.Error()
  1560. break
  1561. }
  1562. fromEdbInfo, err := models.GetEdbInfoById(calculateRjz.FromEdbInfoId)
  1563. if err != nil {
  1564. errMsg = "GetEdbInfoById Err:" + err.Error()
  1565. break
  1566. }
  1567. //startDate = edbInfo.StartDate
  1568. endDate = time.Now().Format(utils.FormatDate)
  1569. err = models.RefreshAllCalculateRjz(edbInfoId, source, fromEdbInfo, calculateRjz.EdbCode, startDate, endDate)
  1570. if err != nil && err.Error() != utils.ErrNoRow() {
  1571. errMsg = "RefreshAllCalculateRjz Err:" + err.Error()
  1572. break
  1573. }
  1574. default:
  1575. // 获取通用的数据源处理服务
  1576. baseEdbInfoModel = models.GetBaseEdbInfoModel(source)
  1577. // 没有找到的话,那么就直接返回报错吧
  1578. if baseEdbInfoModel == nil {
  1579. br.Msg = "来源异常,请联系相关开发!"
  1580. br.ErrMsg = "来源异常,请联系相关开发"
  1581. return
  1582. }
  1583. err, errMsg = baseEdbInfoModel.Refresh(refreshParams)
  1584. }
  1585. if errMsg != `` {
  1586. br.Msg = "刷新指标失败!"
  1587. br.ErrMsg = "刷新指标失败,err:" + errMsg
  1588. return
  1589. }
  1590. // 更新指标最大最小值
  1591. err, errMsg = models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  1592. if err != nil {
  1593. br.Msg = errMsg
  1594. br.ErrMsg = err.Error()
  1595. return
  1596. }
  1597. // 更新ES
  1598. go logic.UpdateEs(edbInfoId)
  1599. br.Ret = 200
  1600. br.Success = true
  1601. br.Msg = "获取成功"
  1602. }
  1603. // SaveAdjust
  1604. // @Title 保存数据调整指标接口
  1605. // @Description 保存数据调整指标接口
  1606. // @Success 200 {object} models.SaveAdjustEdbReq
  1607. // @router /adjust/save [post]
  1608. func (this *CalculateController) SaveAdjust() {
  1609. br := new(models.BaseResponse).Init()
  1610. defer func() {
  1611. this.Data["json"] = br
  1612. this.ServeJSON()
  1613. }()
  1614. var req models.SaveAdjustEdbReq
  1615. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1616. if err != nil {
  1617. br.Msg = "参数解析异常!"
  1618. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1619. return
  1620. }
  1621. req.EdbName = strings.Trim(req.EdbName, " ")
  1622. if req.EdbName == "" {
  1623. br.Msg = "指标名称不能为空"
  1624. return
  1625. }
  1626. if req.Frequency == "" {
  1627. br.Msg = "频率不能为空"
  1628. return
  1629. }
  1630. if req.Unit == "" {
  1631. br.Msg = "单位不能为空"
  1632. return
  1633. }
  1634. if req.ClassifyId <= 0 {
  1635. br.Msg = "请选择分类"
  1636. return
  1637. }
  1638. if len(req.DataList) == 0 {
  1639. br.Msg = "请填写数据"
  1640. return
  1641. }
  1642. //加入缓存机制,避免创建同一个名称的指标 start
  1643. redisKey := fmt.Sprint("edb_info:adjust:add:", utils.DATA_SOURCE_CALCULATE_ADJUST, ":", req.EdbName)
  1644. isExist := utils.Rc.IsExist(redisKey)
  1645. if isExist {
  1646. br.Msg = "指标正在处理,请勿重复提交"
  1647. return
  1648. } else {
  1649. //设置3分钟缓存
  1650. utils.Rc.SetNX(redisKey, 1, time.Second*300)
  1651. defer func() {
  1652. _ = utils.Rc.Delete(redisKey)
  1653. }()
  1654. }
  1655. //获取指标数据
  1656. var condition string
  1657. var pars []interface{}
  1658. condition += " AND edb_name=? "
  1659. pars = append(pars, req.EdbName)
  1660. if req.EdbInfoId > 0 {
  1661. condition += " AND edb_info_id != ? "
  1662. pars = append(pars, req.EdbInfoId)
  1663. }
  1664. count, err := models.GetEdbInfoCountByCondition(condition, pars)
  1665. if err != nil {
  1666. br.Msg = "判断指标名称是否存在失败"
  1667. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  1668. return
  1669. }
  1670. if count > 0 {
  1671. br.Msg = "指标名称已存在,请重新填写"
  1672. br.ErrMsg = "指标名称已存在,请重新填写"
  1673. return
  1674. }
  1675. edbInfo, err, errMsg := models.SaveAdjustEdb(req)
  1676. if err != nil {
  1677. br.Msg = errMsg
  1678. br.Msg = "添加指标失败,Err:" + err.Error()
  1679. return
  1680. }
  1681. //处理同名指标
  1682. {
  1683. edbNameList, err := models.GetEdbInfoByName(req.EdbName)
  1684. if err != nil {
  1685. br.Msg = "保存失败"
  1686. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  1687. return
  1688. }
  1689. if len(edbNameList) >= 2 {
  1690. for _, v := range edbNameList {
  1691. edbName := v.EdbName + "(" + v.SourceName + ")"
  1692. err = models.ModifyEdbInfoNameSource(edbName, v.EdbInfoId)
  1693. if err != nil {
  1694. br.Msg = "保存失败"
  1695. br.ErrMsg = "修改指标名称失败,Err:" + err.Error()
  1696. return
  1697. }
  1698. }
  1699. }
  1700. }
  1701. // 更新指标最大最小值
  1702. err, errMsg = models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  1703. if err != nil {
  1704. br.Msg = errMsg
  1705. br.ErrMsg = err.Error()
  1706. return
  1707. }
  1708. resp := models.AddEdbInfoResp{
  1709. EdbInfoId: edbInfo.EdbInfoId,
  1710. UniqueCode: edbInfo.UniqueCode,
  1711. }
  1712. // 判断是否需要禁用
  1713. go services.DisableEdbInfoNoUpdate(edbInfo)
  1714. // 重置计算指标中的引用关系
  1715. go services.ResetEdbRelation(edbInfo.EdbInfoId)
  1716. br.Ret = 200
  1717. br.Success = true
  1718. br.Msg = "保存成功"
  1719. br.Data = resp
  1720. br.IsAddLog = true
  1721. }
  1722. // CalculateComputeCorrelation
  1723. // @Title 拟合残差计算相关性
  1724. // @Description 拟合残差计算相关性接口
  1725. // @Param request body models.EdbInfoCalculateBatchSaveReq true "type json string"
  1726. // @Success Ret=200 返回指标id
  1727. // @router /compute_correlation [post]
  1728. func (this *CalculateController) CalculateComputeCorrelation() {
  1729. br := new(models.BaseResponse).Init()
  1730. defer func() {
  1731. this.Data["json"] = br
  1732. this.ServeJSON()
  1733. }()
  1734. var req models.EdbInfoCalculateBatchSaveReq
  1735. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1736. if err != nil {
  1737. br.Msg = "参数解析异常!"
  1738. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1739. return
  1740. }
  1741. if len(req.EdbInfoIdArr) != 2 {
  1742. br.Msg = "选择的指标异常,请重新选择"
  1743. return
  1744. }
  1745. //校验时间格式
  1746. //数据格式:2022-11-01,2022-11-10
  1747. timeList := strings.Split(req.Formula, ",")
  1748. if len(timeList) != 2 {
  1749. br.Msg = "选择时间有误,请重新输入"
  1750. return
  1751. }
  1752. startDate, err := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
  1753. if err != nil {
  1754. br.Msg = "开始日期有误,请重新输入"
  1755. return
  1756. }
  1757. endDate, err := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
  1758. if err != nil {
  1759. br.Msg = "结束日期有误,请重新输入"
  1760. return
  1761. }
  1762. if utils.GetTimeSubDay(startDate, endDate) < 2 {
  1763. br.Msg = "日期间隔不得少于两天"
  1764. return
  1765. }
  1766. var nhccDate models.NhccDate // 拟合残差的日期
  1767. nhccDate.StartDate = startDate
  1768. nhccDate.EndDate = endDate
  1769. // 基础指标id
  1770. fromEdbInfoId := req.EdbInfoIdArr[0].EdbInfoId
  1771. if fromEdbInfoId <= 0 {
  1772. br.Msg = "请选择指标"
  1773. return
  1774. }
  1775. // 来源指标(自变量指标)
  1776. fromEdbInfo, err := models.GetEdbInfoById(fromEdbInfoId)
  1777. if err != nil {
  1778. br.Msg = "获取指标信息失败"
  1779. br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
  1780. return
  1781. }
  1782. // 因变量指标
  1783. secondEdbInfoReq := req.EdbInfoIdArr[1]
  1784. secondEdbInfo, tmpErr := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  1785. if tmpErr != nil {
  1786. br.Msg = "获取因变量的指标信息失败"
  1787. br.ErrMsg = "获取因变量的指标信息失败:Err:" + tmpErr.Error()
  1788. return
  1789. }
  1790. if fromEdbInfo.EdbInfoId == secondEdbInfo.EdbInfoId {
  1791. br.Msg = "两个指标不允许为同一个"
  1792. br.ErrMsg = "两个指标不允许为同一个"
  1793. return
  1794. }
  1795. aVal, bVal, rVal, err, errMsg := models.CalculateComputeCorrelation(&req, fromEdbInfo, secondEdbInfo, nhccDate)
  1796. if err != nil {
  1797. br.Msg = "计算失败"
  1798. if errMsg != `` {
  1799. br.Msg = errMsg
  1800. }
  1801. br.ErrMsg = "计算失败,ERR:" + err.Error()
  1802. return
  1803. }
  1804. connStr := ``
  1805. if bVal >= 0 {
  1806. connStr = `+`
  1807. }
  1808. valStr := fmt.Sprintf("y = %sx %s %s R²=%f", utils.SubFloatToString(aVal, 4), connStr, utils.SubFloatToString(bVal, 4), rVal)
  1809. br.Ret = 200
  1810. br.Success = true
  1811. br.Msg = "计算成功"
  1812. br.Data = valStr
  1813. br.IsAddLog = true
  1814. }
  1815. // ResetCustomAnalysisData
  1816. // @Title 自定义表格数据重置
  1817. // @Description 自定义表格数据重置
  1818. // @Param request body models.ResetCustomAnalysisData true "type json string"
  1819. // @Success Ret=200 返回指标id
  1820. // @router /custom_analysis/reset [post]
  1821. func (this *CalculateController) ResetCustomAnalysisData() {
  1822. br := new(models.BaseResponse).Init()
  1823. defer func() {
  1824. this.Data["json"] = br
  1825. this.ServeJSON()
  1826. }()
  1827. var req models.ResetCustomAnalysisData
  1828. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1829. if err != nil {
  1830. br.Msg = "参数解析异常!"
  1831. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1832. br.IsSendEmail = false
  1833. return
  1834. }
  1835. if req.EdbInfoId <= 0 {
  1836. br.Msg = "请选择指标"
  1837. br.ErrMsg = br.Msg
  1838. br.IsSendEmail = false
  1839. return
  1840. }
  1841. if len(req.DateList) <= 0 {
  1842. br.Msg = "请传入日期序列"
  1843. br.ErrMsg = br.Msg
  1844. br.IsSendEmail = false
  1845. return
  1846. }
  1847. if len(req.DataList) <= 0 {
  1848. br.Msg = "请传入数据序列"
  1849. br.ErrMsg = br.Msg
  1850. br.IsSendEmail = false
  1851. return
  1852. }
  1853. edbInfo, err := models.GetEdbInfoById(req.EdbInfoId)
  1854. if err != nil {
  1855. if err.Error() == utils.ErrNoRow() {
  1856. br.Msg = "指标已被删除,请刷新页面"
  1857. br.ErrMsg = "指标已被删除,请刷新页面:Err:" + err.Error()
  1858. return
  1859. }
  1860. br.Msg = "获取指标信息失败"
  1861. br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
  1862. return
  1863. }
  1864. baseEdbInfoModel := models.CustomAnalysis{}
  1865. err, errMsg := baseEdbInfoModel.ResetData(edbInfo, req.DateList, req.DataList)
  1866. if err != nil {
  1867. br.Msg = "更新数据失败"
  1868. if errMsg != `` {
  1869. br.Msg = errMsg
  1870. }
  1871. br.ErrMsg = "更新数据失败 Err:" + err.Error()
  1872. return
  1873. }
  1874. if edbInfo == nil {
  1875. br.Msg = "更新数据失败"
  1876. br.ErrMsg = "更新数据失败,指标ID错误:"
  1877. return
  1878. }
  1879. // 更新指标最大最小值
  1880. err, errMsg = models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  1881. if err != nil {
  1882. br.Msg = errMsg
  1883. br.ErrMsg = err.Error()
  1884. return
  1885. }
  1886. resp := models.AddEdbInfoResp{
  1887. EdbInfoId: edbInfo.EdbInfoId,
  1888. UniqueCode: edbInfo.UniqueCode,
  1889. }
  1890. br.Ret = 200
  1891. br.Success = true
  1892. br.Msg = "处理成功"
  1893. br.Data = resp
  1894. br.IsAddLog = true
  1895. }
  1896. // Calculate
  1897. // @Title 基础数据计算
  1898. // @Description 拟合残差计算相关性接口
  1899. // @Param request body models.EdbInfoCalculateBatchSaveReq true "type json string"
  1900. // @Success Ret=200 返回指标id
  1901. // @router /base [post]
  1902. func (this *CalculateController) Calculate() {
  1903. br := new(models.BaseResponse).Init()
  1904. defer func() {
  1905. this.Data["json"] = br
  1906. this.ServeJSON()
  1907. }()
  1908. var req models.BaseCalculateBySearchData
  1909. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1910. if err != nil {
  1911. br.Msg = "参数解析异常!"
  1912. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1913. return
  1914. }
  1915. dateDataMap := make(map[time.Time]float64)
  1916. var errMsg string
  1917. dataList, err := models.EdbInfoSearchDataToData(req.DataList)
  1918. if err != nil {
  1919. br.Msg = "计算失败,数据异常!"
  1920. br.ErrMsg = "计算失败,数据异常,Err:" + err.Error()
  1921. return
  1922. }
  1923. baseCalculate := models.BaseCalculate{
  1924. DataList: dataList,
  1925. Frequency: req.Frequency,
  1926. Formula: req.Formula,
  1927. Calendar: req.Calendar,
  1928. MoveType: req.MoveType,
  1929. MoveFrequency: req.MoveFrequency,
  1930. FromFrequency: req.FromFrequency,
  1931. Source: req.Source,
  1932. }
  1933. //1:累计值转月;2:累计值转季;3:同比值;4:同差值;5:N数值移动平均数计算;6:环比值;7:环差值;8:升频;9:降频;10:时间移位;11:超季节性;12:年化;13:累计值;14:累计值年初至今;15:指数修匀;16:日均值
  1934. switch baseCalculate.Source {
  1935. case 1:
  1936. dateDataMap, err, errMsg = baseCalculate.Ljzzy()
  1937. case 2:
  1938. dateDataMap, err, errMsg = baseCalculate.Ljzzj()
  1939. case 3:
  1940. dateDataMap, err, errMsg = baseCalculate.Tbz()
  1941. case 4:
  1942. dateDataMap, err, errMsg = baseCalculate.Tcz()
  1943. case 5:
  1944. dateDataMap, err, errMsg = baseCalculate.Nszydpjjs()
  1945. case 6:
  1946. dateDataMap, err, errMsg = baseCalculate.Hbz()
  1947. case 7:
  1948. dateDataMap, err, errMsg = baseCalculate.Hcz()
  1949. case 8:
  1950. dateDataMap, err, errMsg = baseCalculate.UpFrequency()
  1951. case 9:
  1952. dateDataMap, err, errMsg = baseCalculate.DownFrequency()
  1953. case 10:
  1954. dateDataMap, err, errMsg = baseCalculate.TimeShift()
  1955. case 11:
  1956. dateDataMap, err, errMsg = baseCalculate.Cjjx()
  1957. case 12:
  1958. dateDataMap, err, errMsg = baseCalculate.Annualized()
  1959. case 13:
  1960. dateDataMap, err, errMsg = baseCalculate.Ljz()
  1961. case 14:
  1962. dateDataMap, err, errMsg = baseCalculate.LjzNczj()
  1963. case 15:
  1964. dateDataMap, err, errMsg = baseCalculate.ExponentialSmoothing()
  1965. case 16:
  1966. dateDataMap, err, errMsg = baseCalculate.Rjz()
  1967. default:
  1968. errMsg = "错误的计算类型"
  1969. err = errors.New(errMsg + ":" + strconv.Itoa(baseCalculate.Source))
  1970. }
  1971. if err != nil {
  1972. br.Msg = "计算失败"
  1973. if errMsg != `` {
  1974. br.Msg = errMsg
  1975. }
  1976. br.ErrMsg = err.Error()
  1977. return
  1978. }
  1979. dateStrDataMap, dateList := models.GetDateDataAndDateList(dateDataMap)
  1980. br.Ret = 200
  1981. br.Success = true
  1982. br.Msg = "计算成功"
  1983. br.Data = models.BaseCalculateResp{
  1984. DataMap: dateStrDataMap,
  1985. DateList: dateList,
  1986. }
  1987. br.IsAddLog = true
  1988. }
  1989. // BatchSaveMulti
  1990. // @Title 批量计算 累计值转月-同比值-同差等计算新增
  1991. // @Description 批量计算 累计值转月-同比值-同差等计算新增接口
  1992. // @Param request body models.EdbInfoCalculateBatchSaveReq true "type json string"
  1993. // @Success Ret=200 返回指标id
  1994. // @router /batch/save/multi [post]
  1995. func (this *CalculateController) BatchSaveMulti() {
  1996. br := new(models.BaseResponse).Init()
  1997. defer func() {
  1998. if br.ErrMsg == "" {
  1999. br.IsSendEmail = false
  2000. }
  2001. this.Data["json"] = br
  2002. this.ServeJSON()
  2003. }()
  2004. var req models.CalculateBatchSaveReq
  2005. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  2006. if err != nil {
  2007. br.Msg = "参数解析异常!"
  2008. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2009. return
  2010. }
  2011. resp := models.BatchEdbInfoCalculateBatchSaveResp{
  2012. Fail: make([]models.BatchEdbInfoCalculateBatchSaveFailResp, 0),
  2013. Success: make([]models.BatchEdbInfoCalculateBatchSaveSuccessResp, 0),
  2014. }
  2015. redisKeyList := make([]string, 0) //需要清理的缓存key列表
  2016. defer func() {
  2017. for _, redisKey := range redisKeyList {
  2018. utils.Rc.Delete(redisKey)
  2019. }
  2020. }()
  2021. var wg sync.WaitGroup
  2022. // 校验参数
  2023. for _, item := range req.EdbList {
  2024. calculateId := item.CalculateId
  2025. //加入缓存机制,避免创建同一个名称的指标 start
  2026. redisKey := fmt.Sprint("edb_info:calculate:batch:save:", req.AdminId, ":", req.Source, ":", calculateId)
  2027. isExist := utils.Rc.IsExist(redisKey)
  2028. if isExist {
  2029. resp.Fail = append(resp.Fail, models.BatchEdbInfoCalculateBatchSaveFailResp{
  2030. CalculateId: calculateId,
  2031. Msg: "指标正在处理,请勿重复提交",
  2032. })
  2033. continue
  2034. } else {
  2035. //设置3分钟缓存
  2036. utils.Rc.SetNX(redisKey, 1, time.Second*300)
  2037. redisKeyList = append(redisKeyList, redisKey)
  2038. }
  2039. wg.Add(1)
  2040. reqItem := models.EdbInfoCalculateBatchSaveReq{
  2041. AdminId: req.AdminId,
  2042. AdminName: req.AdminName,
  2043. EdbInfoId: item.EdbInfoId,
  2044. EdbName: item.EdbName,
  2045. Frequency: item.Frequency,
  2046. Unit: item.Unit,
  2047. ClassifyId: item.ClassifyId,
  2048. Formula: req.Formula, //N数值移动平均计算、环比值、环差值
  2049. FromEdbInfoId: item.FromEdbInfoId,
  2050. CalculateFormula: req.CalculateFormula,
  2051. Source: req.Source,
  2052. MoveType: req.MoveType,
  2053. MoveFrequency: req.MoveFrequency,
  2054. EdbInfoIdArr: req.EdbInfoIdArr,
  2055. Calendar: req.Calendar,
  2056. EmptyType: req.EmptyType,
  2057. MaxEmptyType: req.MaxEmptyType,
  2058. Extra: req.Extra,
  2059. }
  2060. var edbInfo *models.EdbInfo
  2061. uniqueCode := ""
  2062. errMsg := ""
  2063. go func() {
  2064. defer func() {
  2065. // todo 批量报错处理
  2066. if err != nil {
  2067. // 整理报错的指标
  2068. utils.FileLog.Info(fmt.Sprintf("批量指标计算,保存失败 Err:%v, ErrMsg:%s", err, errMsg))
  2069. resp.Fail = append(resp.Fail, models.BatchEdbInfoCalculateBatchSaveFailResp{
  2070. CalculateId: calculateId,
  2071. Msg: err.Error(),
  2072. ErrMsg: errMsg,
  2073. })
  2074. } else {
  2075. // 整理成功的指标
  2076. //todo 整理执行成功指标信息
  2077. resp.Success = append(resp.Success, models.BatchEdbInfoCalculateBatchSaveSuccessResp{
  2078. ClassifyId: reqItem.ClassifyId,
  2079. CalculateId: calculateId,
  2080. EdbInfoId: edbInfo.EdbInfoId,
  2081. UniqueCode: uniqueCode,
  2082. })
  2083. }
  2084. wg.Done()
  2085. }()
  2086. edbInfo, uniqueCode, err, errMsg = services.EdbCalculateBatchSave(reqItem, this.Lang)
  2087. return
  2088. }()
  2089. }
  2090. wg.Wait()
  2091. // 更新ES
  2092. if resp.Success != nil && len(resp.Success) > 0 {
  2093. for _, item := range resp.Success {
  2094. go logic.UpdateEs(item.EdbInfoId)
  2095. }
  2096. }
  2097. br.Ret = 200
  2098. br.Success = true
  2099. br.Msg = "保存成功"
  2100. br.Data = resp
  2101. br.IsAddLog = true
  2102. }
  2103. // BatchEditMulti
  2104. // @Title 批量计算 累计值转月-同比值-同差等计算编辑
  2105. // @Description 批量计算 累计值转月-同比值-同差等计算编辑接口
  2106. // @Param request body models.EdbInfoCalculateBatchSaveReq true "type json string"
  2107. // @Success Ret=200 返回指标id
  2108. // @router /batch/edit/multi [post]
  2109. func (this *CalculateController) BatchEditMulti() {
  2110. br := new(models.BaseResponse).Init()
  2111. defer func() {
  2112. if br.ErrMsg == "" {
  2113. br.IsSendEmail = false
  2114. }
  2115. this.Data["json"] = br
  2116. this.ServeJSON()
  2117. }()
  2118. var req models.CalculateBatchSaveReq
  2119. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  2120. if err != nil {
  2121. br.Msg = "参数解析异常!"
  2122. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2123. return
  2124. }
  2125. resp := models.BatchEdbInfoCalculateBatchSaveResp{
  2126. Fail: make([]models.BatchEdbInfoCalculateBatchSaveFailResp, 0),
  2127. Success: make([]models.BatchEdbInfoCalculateBatchSaveSuccessResp, 0),
  2128. }
  2129. redisKeyList := make([]string, 0) //需要清理的缓存key列表
  2130. defer func() {
  2131. for _, redisKey := range redisKeyList {
  2132. utils.Rc.Delete(redisKey)
  2133. }
  2134. }()
  2135. var wg sync.WaitGroup
  2136. // 校验参数
  2137. for _, item := range req.EdbList {
  2138. calculateId := item.CalculateId
  2139. if item.EdbInfoId <= 0 {
  2140. resp.Fail = append(resp.Fail, models.BatchEdbInfoCalculateBatchSaveFailResp{
  2141. CalculateId: calculateId,
  2142. Msg: "请选择要编辑的指标",
  2143. })
  2144. continue
  2145. }
  2146. //加入缓存机制,避免创建同一个名称的指标 start
  2147. redisKey := fmt.Sprint("edb_info:calculate:batch:edit:", req.AdminId, ":", req.Source, ":", item.EdbInfoId)
  2148. isExist := utils.Rc.IsExist(redisKey)
  2149. if isExist {
  2150. resp.Fail = append(resp.Fail, models.BatchEdbInfoCalculateBatchSaveFailResp{
  2151. CalculateId: calculateId,
  2152. Msg: "指标正在处理,请勿重复提交",
  2153. })
  2154. continue
  2155. } else {
  2156. //设置3分钟缓存
  2157. utils.Rc.SetNX(redisKey, 1, time.Second*300)
  2158. redisKeyList = append(redisKeyList, redisKey)
  2159. }
  2160. wg.Add(1)
  2161. reqItem := models.EdbInfoCalculateBatchEditReq{
  2162. EdbInfoId: item.EdbInfoId,
  2163. EdbName: item.EdbName,
  2164. Frequency: item.Frequency,
  2165. Unit: item.Unit,
  2166. ClassifyId: item.ClassifyId,
  2167. AdminId: req.AdminId,
  2168. AdminName: req.AdminName,
  2169. Formula: req.Formula, //N数值移动平均计算、环比值、环差值
  2170. FromEdbInfoId: item.FromEdbInfoId,
  2171. CalculateFormula: req.CalculateFormula,
  2172. Source: req.Source,
  2173. MoveType: req.MoveType,
  2174. MoveFrequency: req.MoveFrequency,
  2175. EdbInfoIdArr: req.EdbInfoIdArr,
  2176. Calendar: req.Calendar,
  2177. EmptyType: req.EmptyType,
  2178. MaxEmptyType: req.MaxEmptyType,
  2179. Extra: req.Extra,
  2180. }
  2181. var errMsg string
  2182. var edbInfo *models.EdbInfo
  2183. uniqueCode := ""
  2184. go func() {
  2185. defer func() {
  2186. // todo 批量报错处理
  2187. if err != nil {
  2188. // 整理报错的指标
  2189. utils.FileLog.Info(fmt.Sprintf("批量指标计算,保存失败 Err:%v, ErrMsg:%s", err, errMsg))
  2190. resp.Fail = append(resp.Fail, models.BatchEdbInfoCalculateBatchSaveFailResp{
  2191. CalculateId: calculateId,
  2192. Msg: err.Error(),
  2193. ErrMsg: errMsg,
  2194. })
  2195. } else {
  2196. // 整理成功的指标
  2197. //todo 整理执行成功指标信息
  2198. resp.Success = append(resp.Success, models.BatchEdbInfoCalculateBatchSaveSuccessResp{
  2199. ClassifyId: reqItem.ClassifyId,
  2200. CalculateId: calculateId,
  2201. EdbInfoId: edbInfo.EdbInfoId,
  2202. UniqueCode: uniqueCode,
  2203. })
  2204. }
  2205. wg.Done()
  2206. }()
  2207. edbInfo, uniqueCode, err, errMsg = services.EdbCalculateBatchEdit(reqItem)
  2208. return
  2209. }()
  2210. }
  2211. wg.Wait()
  2212. if err != nil {
  2213. br.Msg = "批量指标计算,保存失败"
  2214. br.ErrMsg = "批量指标计算,保存失败,Err:" + err.Error()
  2215. return
  2216. }
  2217. // 更新ES
  2218. if resp.Success != nil && len(resp.Success) > 0 {
  2219. for _, item := range resp.Success {
  2220. go logic.UpdateEs(item.EdbInfoId)
  2221. }
  2222. }
  2223. br.Ret = 200
  2224. br.Success = true
  2225. br.Msg = "保存成功"
  2226. br.Data = resp
  2227. br.IsAddLog = true
  2228. }
  2229. // StepCalculate
  2230. // @Title 多步骤计算
  2231. // @Description 多步骤计算
  2232. // @Param request body models.StepCalculateBySearchData true "type json string"
  2233. // @Success Ret=200 返回指标id
  2234. // @router /base/step_calculate [post]
  2235. func (this *CalculateController) StepCalculate() {
  2236. br := new(models.BaseResponse).Init()
  2237. defer func() {
  2238. if br.ErrMsg == "" {
  2239. br.IsSendEmail = false
  2240. }
  2241. this.Data["json"] = br
  2242. this.ServeJSON()
  2243. }()
  2244. var req models.StepCalculateBySearchData
  2245. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  2246. br.Msg = "参数解析异常"
  2247. br.ErrMsg = fmt.Sprintf("参数解析失败, Err: %v", e)
  2248. return
  2249. }
  2250. //sort.Slice(req.Calculates, func(i, j int) bool {
  2251. // return req.Calculates[i].Sort < req.Calculates[j].Sort
  2252. //})
  2253. //
  2254. //var errMsg string
  2255. //originData, e := models.EdbInfoSearchDataToData(req.DataList)
  2256. //if e != nil {
  2257. // br.Msg = "基础数据异常"
  2258. // br.ErrMsg = fmt.Sprintf("计算失败, 基础数据异常, Err: %v", e)
  2259. // return
  2260. //}
  2261. //
  2262. //calculateData := originData
  2263. //dateDataMap := make(map[time.Time]float64)
  2264. //for _, v := range req.Calculates {
  2265. // baseCalculate := models.BaseCalculate{
  2266. // DataList: calculateData,
  2267. // Frequency: v.Frequency,
  2268. // Formula: v.Formula,
  2269. // Calendar: v.Calendar,
  2270. // MoveType: v.MoveType,
  2271. // MoveFrequency: v.MoveFrequency,
  2272. // FromFrequency: v.FromFrequency,
  2273. // Source: v.Source,
  2274. // }
  2275. //
  2276. // // 计算方式
  2277. // switch baseCalculate.Source {
  2278. // case utils.EdbBaseCalculateLjzzy:
  2279. // dateDataMap, e, errMsg = baseCalculate.Ljzzy()
  2280. // case utils.EdbBaseCalculateLjzzj:
  2281. // dateDataMap, e, errMsg = baseCalculate.Ljzzj()
  2282. // case utils.EdbBaseCalculateTbz:
  2283. // dateDataMap, e, errMsg = baseCalculate.Tbz()
  2284. // case utils.EdbBaseCalculateTcz:
  2285. // dateDataMap, e, errMsg = baseCalculate.Tcz()
  2286. // case utils.EdbBaseCalculateNszydpjjs:
  2287. // dateDataMap, e, errMsg = baseCalculate.Nszydpjjs()
  2288. // case utils.EdbBaseCalculateHbz:
  2289. // dateDataMap, e, errMsg = baseCalculate.Hbz()
  2290. // case utils.EdbBaseCalculateHcz:
  2291. // dateDataMap, e, errMsg = baseCalculate.Hcz()
  2292. // case utils.EdbBaseCalculateUpFrequency:
  2293. // dateDataMap, e, errMsg = baseCalculate.UpFrequency()
  2294. // case utils.EdbBaseCalculateDownFrequency:
  2295. // dateDataMap, e, errMsg = baseCalculate.DownFrequency()
  2296. // case utils.EdbBaseCalculateTimeShift:
  2297. // dateDataMap, e, errMsg = baseCalculate.TimeShift()
  2298. // case utils.EdbBaseCalculateCjjx:
  2299. // dateDataMap, e, errMsg = baseCalculate.Cjjx()
  2300. // case utils.EdbBaseCalculateAnnualized:
  2301. // dateDataMap, e, errMsg = baseCalculate.Annualized()
  2302. // case utils.EdbBaseCalculateLjz:
  2303. // dateDataMap, e, errMsg = baseCalculate.Ljz()
  2304. // case utils.EdbBaseCalculateLjzNczj:
  2305. // dateDataMap, e, errMsg = baseCalculate.LjzNczj()
  2306. // case utils.EdbBaseCalculateExponentialSmoothing:
  2307. // dateDataMap, e, errMsg = baseCalculate.ExponentialSmoothing()
  2308. // case utils.EdbBaseCalculateRjz:
  2309. // dateDataMap, e, errMsg = baseCalculate.Rjz()
  2310. // default:
  2311. // errMsg = "计算方式无效"
  2312. // e = fmt.Errorf("%s:%d", errMsg, baseCalculate.Source)
  2313. // }
  2314. // if e != nil {
  2315. // br.Msg = "计算失败"
  2316. // if errMsg != "" {
  2317. // br.Msg = errMsg
  2318. // }
  2319. // br.ErrMsg = e.Error()
  2320. // return
  2321. // }
  2322. //
  2323. // calculateData = models.TransDateData2EdbData(dateDataMap)
  2324. //}
  2325. resultData, dates, errMsg, e := services.StepCalculate(req.DataList, req.Calculates)
  2326. if e != nil {
  2327. br.Msg = "计算失败"
  2328. if errMsg != "" {
  2329. br.Msg = errMsg
  2330. }
  2331. br.ErrMsg = fmt.Sprintf("分步骤计算失败, Err: %v", e)
  2332. return
  2333. }
  2334. br.Ret = 200
  2335. br.Success = true
  2336. br.Msg = "计算成功"
  2337. br.Data = models.BaseCalculateResp{
  2338. DataMap: resultData,
  2339. DateList: dates,
  2340. }
  2341. br.IsAddLog = true
  2342. }