base_from_calculate.go 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "eta/eta_index_lib/logic"
  6. "eta/eta_index_lib/models"
  7. "eta/eta_index_lib/services"
  8. "eta/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.AddEditEdbInfoRecord(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. case utils.DATA_SOURCE_CALCULATE_PHASE_SHIFT:
  686. sourName = "期数移位"
  687. edbInfo, err = models.AddCalculatePhaseShift(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
  688. default:
  689. // 获取通用的数据源处理服务
  690. baseEdbInfoModel = models.GetBaseEdbInfoModel(req.Source)
  691. // 没有找到的话,那么就直接返回报错吧
  692. if baseEdbInfoModel == nil {
  693. br.Msg = "无效计算方式"
  694. br.ErrMsg = "无效计算方式,source:" + strconv.Itoa(req.Source)
  695. return
  696. }
  697. sourName = baseEdbInfoModel.GetSourceName()
  698. edbInfo, err, errMsg = baseEdbInfoModel.Add(addParam)
  699. }
  700. if err != nil {
  701. br.Msg = "生成" + sourName + "失败"
  702. if errMsg != `` {
  703. br.Msg = errMsg
  704. }
  705. br.ErrMsg = "生成" + sourName + "失败 Err:" + err.Error()
  706. return
  707. }
  708. if edbInfo == nil {
  709. br.Msg = "生成" + sourName + "失败"
  710. br.ErrMsg = "生成" + sourName + "失败,指标ID错误:" + strconv.Itoa(edbInfoId)
  711. return
  712. }
  713. edbInfoId = edbInfo.EdbInfoId
  714. //处理同名指标
  715. {
  716. edbNameList, err := models.GetEdbInfoByName(req.EdbName)
  717. if err != nil {
  718. br.Msg = "保存失败"
  719. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  720. return
  721. }
  722. if len(edbNameList) >= 2 {
  723. for _, v := range edbNameList {
  724. edbName := v.EdbName + "(" + v.SourceName + ")"
  725. err = models.ModifyEdbInfoNameSource(edbName, v.EdbInfoId)
  726. if err != nil {
  727. br.Msg = "保存失败"
  728. br.ErrMsg = "修改指标名称失败,Err:" + err.Error()
  729. return
  730. }
  731. }
  732. }
  733. }
  734. // 更新指标最大最小值
  735. err, errMsg = models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  736. if err != nil {
  737. br.Msg = errMsg
  738. br.ErrMsg = err.Error()
  739. return
  740. }
  741. newEdbInfo := new(models.EdbInfoEditRecord)
  742. newEdbInfo.EdbName = req.EdbName
  743. newEdbInfo.Frequency = req.Frequency
  744. newEdbInfo.Unit = req.Unit
  745. newEdbInfo.OperateUserId = req.AdminId
  746. newEdbInfo.OperateUserRealName = req.AdminName
  747. err = services.AddEditEdbInfoRecord(edbInfo, newEdbInfo)
  748. if err != nil {
  749. br.Msg = "保存失败"
  750. br.ErrMsg = "保存失败,Err:" + err.Error()
  751. return
  752. }
  753. // 判断是否需要禁用
  754. go services.DisableEdbInfoNoUpdate(edbInfo)
  755. resp := models.AddEdbInfoResp{
  756. EdbInfoId: edbInfo.EdbInfoId,
  757. UniqueCode: edbInfo.UniqueCode,
  758. }
  759. br.Ret = 200
  760. br.Success = true
  761. br.Msg = "保存成功"
  762. br.Data = resp
  763. br.IsAddLog = true
  764. }
  765. // BatchEdit
  766. // @Title 累计值转月-同比值-同差等计算新增
  767. // @Description 累计值转月-同比值-同差等计算新增接口
  768. // @Param request body models.EdbInfoCalculateBatchSaveReq true "type json string"
  769. // @Success Ret=200 返回指标id
  770. // @router /batch/edit [post]
  771. func (this *CalculateController) BatchEdit() {
  772. br := new(models.BaseResponse).Init()
  773. defer func() {
  774. this.Data["json"] = br
  775. this.ServeJSON()
  776. }()
  777. var req models.EdbInfoCalculateBatchEditReq
  778. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  779. if err != nil {
  780. br.Msg = "参数解析异常!"
  781. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  782. return
  783. }
  784. if req.EdbName == "" {
  785. br.Msg = "指标名称不能为空"
  786. return
  787. }
  788. if req.Frequency == "" {
  789. br.Msg = "频率不能为空"
  790. return
  791. }
  792. if req.Unit == "" {
  793. br.Msg = "单位不能为空"
  794. return
  795. }
  796. if req.ClassifyId <= 0 {
  797. br.Msg = "请选择分类"
  798. return
  799. }
  800. if req.EdbInfoId <= 0 {
  801. br.Msg = "请选择指标"
  802. return
  803. }
  804. // 根据指标名称和指标ID校验库中是否还存在其他同名指标
  805. existEdbName, err := logic.CheckExistByEdbNameAndEdbInfoId(utils.EDB_INFO_TYPE, req.EdbInfoId, req.EdbName, this.Lang)
  806. if err != nil {
  807. br.Msg = "判断指标名称是否存在失败"
  808. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  809. return
  810. }
  811. if existEdbName {
  812. br.Msg = "指标名称已存在,请重新填写"
  813. br.ErrMsg = "指标名称已存在,请重新填写"
  814. br.IsSendEmail = false
  815. return
  816. }
  817. edbInfo, err := models.GetEdbInfoById(req.EdbInfoId)
  818. if err != nil {
  819. if err.Error() == utils.ErrNoRow() {
  820. br.Msg = "指标已被删除,请刷新页面"
  821. br.ErrMsg = "指标已被删除,请刷新页面:Err:" + err.Error()
  822. return
  823. }
  824. br.Msg = "获取指标信息失败"
  825. br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
  826. return
  827. }
  828. // 记录原始指标信息
  829. oldEdbName := edbInfo.EdbName
  830. oldFrequency := edbInfo.Frequency
  831. oldUnit := edbInfo.Unit
  832. // 基础指标id
  833. fromEdbInfoId := req.FromEdbInfoId
  834. if req.Source <= 0 {
  835. req.Source = edbInfo.Source
  836. }
  837. var formulaInt int
  838. var nhccDate models.NhccDate // 拟合残差的日期
  839. // 初期的参数校验
  840. switch req.Source {
  841. 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:
  842. if req.Formula == "" {
  843. br.Msg = "请填写N值"
  844. return
  845. }
  846. formulaInt, _ = strconv.Atoi(req.Formula)
  847. if formulaInt <= 0 {
  848. br.Msg = "N值输入错误,请重新输入"
  849. return
  850. }
  851. case utils.DATA_SOURCE_CALCULATE_ZJPJ:
  852. //直接拼接指标
  853. //校验时间格式
  854. _, err = time.ParseInLocation(utils.FormatDate, req.Formula, time.Local)
  855. if err != nil {
  856. br.Msg = "拼接日期有误,请重新输入"
  857. return
  858. }
  859. case utils.DATA_SOURCE_CALCULATE_NHCC: //拟合残差指标
  860. //指标校验
  861. if len(req.EdbInfoIdArr) != 2 {
  862. br.Msg = "选择的指标异常,请重新选择"
  863. return
  864. }
  865. fromEdbInfoId = req.EdbInfoIdArr[0].EdbInfoId
  866. //校验时间格式
  867. //数据格式:2022-11-01,2022-11-10
  868. timeList := strings.Split(req.Formula, ",")
  869. if len(timeList) != 2 {
  870. br.Msg = "选择时间有误,请重新输入"
  871. return
  872. }
  873. startDate, err := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
  874. if err != nil {
  875. br.Msg = "开始日期有误,请重新输入"
  876. return
  877. }
  878. endDate, err := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
  879. if err != nil {
  880. br.Msg = "结束日期有误,请重新输入"
  881. return
  882. }
  883. if utils.GetTimeSubDay(startDate, endDate) < 2 {
  884. br.Msg = "日期间隔不得少于两天"
  885. return
  886. }
  887. nhccDate.StartDate = startDate
  888. nhccDate.EndDate = endDate
  889. case utils.DATA_SOURCE_CALCULATE_JP:
  890. if req.Formula != "期末值" && req.Formula != "平均值" {
  891. br.Msg = "数据取值类型错误:" + req.Formula
  892. return
  893. }
  894. case utils.DATA_SOURCE_CALCULATE_ZSXY: // 指数修匀
  895. if req.Formula == "" {
  896. br.Msg = "请填写alpha值"
  897. return
  898. }
  899. a, e := strconv.ParseFloat(req.Formula, 64)
  900. if e != nil {
  901. br.Msg = "alpha值输入错误, 请重新输入"
  902. return
  903. }
  904. if a <= 0 || a >= 1 {
  905. br.Msg = "alpha值输入错误, 请重新输入"
  906. return
  907. }
  908. }
  909. var fromEdbInfo *models.EdbInfo
  910. if fromEdbInfoId > 0 {
  911. // 获取基础指标信息
  912. fromEdbInfo, err = models.GetEdbInfoById(fromEdbInfoId)
  913. if err != nil {
  914. if err.Error() == utils.ErrNoRow() {
  915. br.Msg = "指标已被删除,请刷新页面"
  916. br.ErrMsg = "指标已被删除,请刷新页面:Err:" + err.Error()
  917. return
  918. }
  919. br.Msg = "获取指标信息失败"
  920. br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
  921. return
  922. }
  923. }
  924. //dataUpdateTime := time.Now().Format(utils.FormatDateTime)
  925. // 额外赋值
  926. switch this.Lang {
  927. case utils.EnLangVersion:
  928. req.EdbNameEn = req.EdbName
  929. req.UnitEn = req.Unit
  930. req.EdbName = edbInfo.EdbName
  931. req.Unit = edbInfo.Unit
  932. default:
  933. req.EdbNameEn = edbInfo.EdbNameEn
  934. req.UnitEn = edbInfo.UnitEn
  935. }
  936. var sourName, errMsg string
  937. var edbInfoId int
  938. var baseEdbInfoModel models.BaseEdbInfoInterface
  939. editParams := models.EditCalculateBatchParams{
  940. Req: &req,
  941. EdbInfo: edbInfo,
  942. FromEdbInfo: fromEdbInfo,
  943. }
  944. switch req.Source {
  945. case utils.DATA_SOURCE_CALCULATE_LJZZY:
  946. sourName = "累计值转月值"
  947. if fromEdbInfo.Frequency != "月度" {
  948. br.Msg = "请选择月度指标"
  949. return
  950. }
  951. err = models.EditCalculateLjzzy(edbInfo, &req, fromEdbInfo)
  952. case utils.DATA_SOURCE_CALCULATE_TBZ:
  953. sourName = "同比值"
  954. err = models.EditCalculateTbz(edbInfo, &req, fromEdbInfo)
  955. case utils.DATA_SOURCE_CALCULATE_TCZ:
  956. fmt.Println("start edit", time.Now())
  957. sourName = "同差值"
  958. err = models.EditCalculateTcz(edbInfo, &req, fromEdbInfo)
  959. fmt.Println("end edit", time.Now())
  960. case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS:
  961. sourName = "N数值移动平均计算"
  962. err = models.EditCalculateNszydpjjs(edbInfo, &req, fromEdbInfo, formulaInt, edbInfo.CalculateFormula)
  963. case utils.DATA_SOURCE_CALCULATE_HBZ:
  964. checkCount, tmpErr := models.GetLteZeroEdbDataCount(fromEdbInfo.Source, fromEdbInfo.SubSource, req.FromEdbInfoId)
  965. if tmpErr != nil && tmpErr.Error() != utils.ErrNoRow() {
  966. br.Msg = "判断环比值是否可计算失败"
  967. br.ErrMsg = "判断环比值是否可计算失败,Err:" + tmpErr.Error()
  968. return
  969. }
  970. if checkCount > 0 {
  971. br.Msg = "原始数据中存在0或负数,该指标不能进行环比运算"
  972. br.ErrMsg = "原始数据中出现0和负值时,提示该指标不能进行环比运算"
  973. return
  974. }
  975. sourName = "环比值"
  976. err = models.EditCalculateHbz(edbInfo, &req, fromEdbInfo, formulaInt, edbInfo.CalculateFormula)
  977. case utils.DATA_SOURCE_CALCULATE_HCZ:
  978. sourName = "环差值"
  979. err = models.EditCalculateHcz(edbInfo, &req, fromEdbInfo, formulaInt, edbInfo.CalculateFormula)
  980. case utils.DATA_SOURCE_CALCULATE_BP:
  981. sourName = utils.DATA_SOURCE_NAME_CALCULATE_BP
  982. err = models.EditCalculateBp(edbInfo, &req, fromEdbInfo)
  983. case utils.DATA_SOURCE_CALCULATE_TIME_SHIFT:
  984. sourName = "时间移位"
  985. err = models.EditCalculateTimeShift(edbInfo, &req, fromEdbInfo)
  986. case utils.DATA_SOURCE_CALCULATE_PHASE_SHIFT:
  987. sourName = "期数移位"
  988. err = models.EditCalculatePhaseShift(edbInfo, &req, fromEdbInfo)
  989. case utils.DATA_SOURCE_CALCULATE_ZJPJ:
  990. sourName = "直接拼接"
  991. if len(req.EdbInfoIdArr) != 1 {
  992. br.Msg = "请传入拼接日期之后的指标"
  993. br.ErrMsg = "请传入拼接日期之后的指标"
  994. return
  995. }
  996. secondEdbInfoReq := req.EdbInfoIdArr[0]
  997. secondEdbInfo, tmpErr := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  998. if tmpErr != nil {
  999. br.Msg = "获取拼接日期之后的指标信息失败"
  1000. br.ErrMsg = "获取拼接日期之后的指标信息失败:Err:" + tmpErr.Error()
  1001. return
  1002. }
  1003. if fromEdbInfo.EdbInfoId == secondEdbInfo.EdbInfoId {
  1004. br.Msg = "两个指标不允许为同一个"
  1005. br.ErrMsg = "两个指标不允许为同一个"
  1006. return
  1007. }
  1008. err = models.EditCalculateZjpj(&req, edbInfo, fromEdbInfo, secondEdbInfo)
  1009. case utils.DATA_SOURCE_CALCULATE_LJZTBPJ: //累计值同比拼接
  1010. sourName = "累计值同比拼接"
  1011. if fromEdbInfo.Frequency != "月度" {
  1012. br.Msg = "待拼接指标只能筛选月度指标"
  1013. br.ErrMsg = "待拼接指标只能筛选月度指标"
  1014. return
  1015. }
  1016. if len(req.EdbInfoIdArr) != 1 {
  1017. br.Msg = "请传入同比值指标"
  1018. br.ErrMsg = "请传入同比值指标"
  1019. return
  1020. }
  1021. secondEdbInfoReq := req.EdbInfoIdArr[0]
  1022. tbzEdbInfo, tmpErr := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  1023. if tmpErr != nil {
  1024. br.Msg = "获取同比值指标信息失败"
  1025. br.ErrMsg = "获取同比值指标信息失败:Err:" + tmpErr.Error()
  1026. return
  1027. }
  1028. if tbzEdbInfo.Source != utils.DATA_SOURCE_CALCULATE_TBZ {
  1029. br.Msg = "指标必须是传入同比值指标类型"
  1030. br.ErrMsg = "指标必须是传入同比值指标类型"
  1031. return
  1032. }
  1033. if tbzEdbInfo.Frequency != "月度" {
  1034. br.Msg = "同比值指标只能筛选月度指标"
  1035. br.ErrMsg = "同比值指标只能筛选月度指标"
  1036. return
  1037. }
  1038. if fromEdbInfo.EdbInfoId == tbzEdbInfo.EdbInfoId {
  1039. br.Msg = "两个指标不允许为同一个"
  1040. br.ErrMsg = "两个指标不允许为同一个"
  1041. return
  1042. }
  1043. err = models.EditCalculateLjztbpj(&req, edbInfo, fromEdbInfo, tbzEdbInfo)
  1044. case utils.DATA_SOURCE_CALCULATE_CJJX:
  1045. sourName = "超季节性"
  1046. err = models.EditCalculateCjjx(&req, edbInfo, fromEdbInfo, formulaInt)
  1047. case utils.DATA_SOURCE_CALCULATE_NHCC:
  1048. sourName = "拟合残差"
  1049. secondEdbInfoReq := req.EdbInfoIdArr[1]
  1050. secondEdbInfo, tmpErr := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  1051. if tmpErr != nil {
  1052. br.Msg = "获取因变量的指标信息失败"
  1053. br.ErrMsg = "获取因变量的指标信息失败:Err:" + tmpErr.Error()
  1054. return
  1055. }
  1056. if fromEdbInfo.EdbInfoId == secondEdbInfo.EdbInfoId {
  1057. br.Msg = "两个指标不允许为同一个"
  1058. br.ErrMsg = "两个指标不允许为同一个"
  1059. return
  1060. }
  1061. err, errMsg = models.EditCalculateNhcc(&req, edbInfo, fromEdbInfo, secondEdbInfo, nhccDate)
  1062. case utils.DATA_SOURCE_CALCULATE_JP:
  1063. if !models.CheckFrequency(fromEdbInfo.Frequency, req.Frequency) {
  1064. br.Msg = "频度异常,不允许低频降频到高频"
  1065. return
  1066. }
  1067. sourName = utils.DATA_SOURCE_NAME_CALCULATE_JP
  1068. err = models.EditCalculateJp(edbInfo, &req, fromEdbInfo)
  1069. case utils.DATA_SOURCE_CALCULATE_NH:
  1070. sourName = utils.DATA_SOURCE_NAME_CALCULATE_NH
  1071. err = models.EditCalculateNh(edbInfo, &req, fromEdbInfo)
  1072. case utils.DATA_SOURCE_CALCULATE_KSZS:
  1073. //关联的指标信息
  1074. if len(req.EdbInfoIdArr) < 2 {
  1075. br.Msg = "指标数量不能小于2个,请重新选择"
  1076. br.ErrMsg = "指标数量不能小于2个,请重新选择"
  1077. br.IsSendEmail = false
  1078. return
  1079. }
  1080. sourName = utils.DATA_SOURCE_NAME_CALCULATE_KSZS
  1081. err = models.EditCalculateKszs(edbInfo, &req)
  1082. case utils.DATA_SOURCE_CALCULATE_CORRELATION:
  1083. //关联的指标信息
  1084. if len(req.EdbInfoIdArr) < 2 {
  1085. br.Msg = "指标数量不能小于2个,请重新选择"
  1086. br.ErrMsg = "指标数量不能小于2个,请重新选择"
  1087. br.IsSendEmail = false
  1088. return
  1089. }
  1090. sourName = utils.DATA_SOURCE_NAME_CALCULATE_CORRELATION
  1091. err, errMsg = models.EditCalculateCorrelation(edbInfo, &req)
  1092. case utils.DATA_SOURCE_CALCULATE_RJZ:
  1093. if req.Frequency == "日度" {
  1094. br.Msg = "日度指标无需进行日均值计算"
  1095. return
  1096. }
  1097. if req.Frequency != fromEdbInfo.Frequency {
  1098. br.Msg = "当前频度和原指标频度不一致"
  1099. return
  1100. }
  1101. if req.Unit != fromEdbInfo.Unit {
  1102. br.Msg = "单位只允许为和原指标频度保持一致,禁止选择其他单位"
  1103. return
  1104. }
  1105. sourName = "日均值"
  1106. err = models.EditCalculateRjz(edbInfo, &req, fromEdbInfo)
  1107. default:
  1108. // 获取通用的数据源处理服务
  1109. baseEdbInfoModel = models.GetBaseEdbInfoModel(req.Source)
  1110. // 没有找到的话,那么就直接返回报错吧
  1111. if baseEdbInfoModel == nil {
  1112. br.Msg = "无效计算方式"
  1113. br.ErrMsg = "无效计算方式,source:" + strconv.Itoa(req.Source)
  1114. return
  1115. }
  1116. sourName = baseEdbInfoModel.GetSourceName()
  1117. err, errMsg = baseEdbInfoModel.Edit(editParams)
  1118. }
  1119. if err != nil {
  1120. br.Msg = "生成" + sourName + "失败"
  1121. if errMsg != `` {
  1122. br.Msg = errMsg
  1123. }
  1124. br.Msg = "生成" + sourName + "失败 Err:" + err.Error()
  1125. return
  1126. }
  1127. if edbInfo == nil {
  1128. br.Msg = "生成" + sourName + "失败"
  1129. br.ErrMsg = "生成" + sourName + "失败,指标ID错误:" + strconv.Itoa(edbInfoId)
  1130. return
  1131. }
  1132. edbInfoId = edbInfo.EdbInfoId
  1133. //处理同名指标
  1134. {
  1135. edbNameList, err := models.GetEdbInfoByName(req.EdbName)
  1136. if err != nil {
  1137. br.Msg = "保存失败"
  1138. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  1139. return
  1140. }
  1141. if len(edbNameList) >= 2 {
  1142. for _, v := range edbNameList {
  1143. edbName := v.EdbName + "(" + v.SourceName + ")"
  1144. err = models.ModifyEdbInfoNameSource(edbName, v.EdbInfoId)
  1145. if err != nil {
  1146. br.Msg = "保存失败"
  1147. br.ErrMsg = "修改指标名称失败,Err:" + err.Error()
  1148. return
  1149. }
  1150. }
  1151. }
  1152. }
  1153. // 更新指标最大最小值
  1154. //err, errMsg = models.UnifiedModifyEdbInfoMaxAndMinInfoTmp(edbInfo, dataUpdateTime)
  1155. err, errMsg = models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  1156. if err != nil {
  1157. br.Msg = errMsg
  1158. br.ErrMsg = err.Error()
  1159. return
  1160. }
  1161. // 记录指标的操作记录
  1162. oldEdbInfo := new(models.EdbInfo)
  1163. oldEdbInfo.EdbInfoId = edbInfo.EdbInfoId
  1164. oldEdbInfo.EdbName = oldEdbName
  1165. oldEdbInfo.Frequency = oldFrequency
  1166. oldEdbInfo.Unit = oldUnit
  1167. newEdbInfoRecord := new(models.EdbInfoEditRecord)
  1168. newEdbInfoRecord.EdbName = req.EdbName
  1169. newEdbInfoRecord.Frequency = req.Frequency
  1170. newEdbInfoRecord.Unit = req.Unit
  1171. newEdbInfoRecord.OperateUserId = req.AdminId
  1172. newEdbInfoRecord.OperateUserRealName = req.AdminName
  1173. err = services.AddEditEdbInfoRecord(oldEdbInfo, newEdbInfoRecord)
  1174. if err != nil {
  1175. br.Msg = "保存失败"
  1176. br.ErrMsg = "记录指标操作记录失败,Err:" + err.Error()
  1177. }
  1178. // 重置计算指标中的引用关系
  1179. go services.ResetEdbRelation(edbInfoId)
  1180. resp := models.AddEdbInfoResp{
  1181. EdbInfoId: edbInfo.EdbInfoId,
  1182. UniqueCode: edbInfo.UniqueCode,
  1183. }
  1184. br.Ret = 200
  1185. br.Success = true
  1186. br.Msg = "保存成功"
  1187. br.Data = resp
  1188. br.IsAddLog = true
  1189. }
  1190. // Refresh
  1191. // @Title 刷新计算指标接口
  1192. // @Description 刷新计算指标接口
  1193. // @Success 200 {object} models.RefreshEdbInfoReq
  1194. // @router /refresh [post]
  1195. func (this *CalculateController) Refresh() {
  1196. br := new(models.BaseResponse).Init()
  1197. var cacheKey string
  1198. defer func() {
  1199. this.Data["json"] = br
  1200. this.ServeJSON()
  1201. }()
  1202. var req models.RefreshEdbInfoReq
  1203. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1204. if err != nil {
  1205. br.Msg = "参数解析异常!"
  1206. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1207. return
  1208. }
  1209. if req.EdbCode == "" {
  1210. br.Msg = "请输入指标编码!"
  1211. br.ErrMsg = "请输入指标编码,指标编码为空"
  1212. return
  1213. }
  1214. if req.EdbInfoId <= 0 {
  1215. br.Msg = "请输入指标ID!"
  1216. br.ErrMsg = "请输入指标ID"
  1217. return
  1218. }
  1219. edbInfo, err := models.GetEdbInfoById(req.EdbInfoId)
  1220. if err != nil {
  1221. br.Msg = "指标不存在!"
  1222. br.ErrMsg = "指标不存在"
  1223. return
  1224. }
  1225. cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(edbInfo.Source) + "_" + req.EdbCode
  1226. if utils.Rc.IsExist(cacheKey) {
  1227. br.Ret = 501
  1228. br.Success = true
  1229. br.Msg = "系统处理中,请稍后重试"
  1230. return
  1231. }
  1232. // 刷新指标
  1233. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  1234. defer func() {
  1235. _ = utils.Rc.Delete(cacheKey)
  1236. }()
  1237. startDate := req.StartDate
  1238. var errMsg string
  1239. endDate := time.Now().Format(utils.FormatDate)
  1240. edbInfoId := edbInfo.EdbInfoId
  1241. source := edbInfo.Source
  1242. subSource := edbInfo.SubSource
  1243. var baseEdbInfoModel models.BaseEdbInfoInterface
  1244. refreshParams := models.RefreshParams{
  1245. EdbInfo: edbInfo,
  1246. StartDate: "", // 更新所有
  1247. EndDate: "", // 更新所有数据
  1248. }
  1249. switch source {
  1250. case utils.DATA_SOURCE_CALCULATE:
  1251. //startDate = edbInfo.StartDate
  1252. //sTime, err := time.ParseInLocation(utils.FormatDate, edbInfo.EndDate,time.Local)
  1253. //if err != nil {
  1254. // return
  1255. //}
  1256. //startDate = sTime.Format(utils.FormatDate)
  1257. startDate = ""
  1258. var edbInfoIdBytes []string
  1259. calculateMap, err := models.GetEdbInfoCalculateDetailList(edbInfo.EdbInfoId)
  1260. if err != nil {
  1261. errMsg = "GetEdbInfoCalculateDetail Err:" + err.Error()
  1262. break
  1263. }
  1264. var formulaStr string
  1265. edbInfoList := make([]*models.EdbInfo, 0)
  1266. edbInfoTag := make(map[string]int)
  1267. for _, v := range calculateMap {
  1268. edbInfoTag[v.FromTag] = v.FromEdbInfoId
  1269. formulaStr += v.FromTag + ","
  1270. edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
  1271. edbInfo, _ := models.GetEdbInfoById(v.FromEdbInfoId)
  1272. edbInfoList = append(edbInfoList, edbInfo)
  1273. }
  1274. err = models.RefreshAllCalculate(edbInfoList, edbInfoTag, edbInfo.EdbInfoId, source, subSource, edbInfo.EdbCode, edbInfo.CalculateFormula, startDate, endDate, edbInfoIdBytes, edbInfo.EmptyType, edbInfo.MaxEmptyType, edbInfo.Extra)
  1275. if err != nil && err.Error() != utils.ErrNoRow() {
  1276. errMsg = "RefreshCalculate Err:" + err.Error()
  1277. break
  1278. }
  1279. case utils.DATA_SOURCE_CALCULATE_LJZZY: //刷新累计值转月值
  1280. calculateLjzzy, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1281. if err != nil {
  1282. errMsg = "GetEdbInfoCalculateLjzzyDetail Err:" + err.Error()
  1283. break
  1284. }
  1285. fromEdbInfo, err := models.GetEdbInfoById(calculateLjzzy.FromEdbInfoId)
  1286. if err != nil {
  1287. errMsg = "GetEdbInfoById Err:" + err.Error()
  1288. break
  1289. }
  1290. //startDate = edbInfo.StartDate
  1291. //endDate = time.Now().Format(utils.FormatDate)
  1292. endDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
  1293. startDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
  1294. err = models.RefreshAllCalculateLjzzy(edbInfoId, source, subSource, fromEdbInfo, calculateLjzzy.EdbCode, startDate, endDate)
  1295. if err != nil && err.Error() != utils.ErrNoRow() {
  1296. errMsg = "RefreshAllCalculateLjzzy Err:" + err.Error()
  1297. break
  1298. }
  1299. case utils.DATA_SOURCE_CALCULATE_TBZ: //刷新同比值
  1300. calculateTbz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1301. if err != nil {
  1302. errMsg = "GetEdbInfoCalculateTbzDetail Err:" + err.Error()
  1303. break
  1304. }
  1305. fromEdbInfo, err := models.GetEdbInfoById(calculateTbz.FromEdbInfoId)
  1306. if err != nil {
  1307. errMsg = "GetEdbInfoById Err:" + err.Error()
  1308. break
  1309. }
  1310. //startDate = edbInfo.StartDate
  1311. endDate = time.Now().Format(utils.FormatDate)
  1312. err = models.RefreshAllCalculateTbz(edbInfoId, source, subSource, fromEdbInfo, calculateTbz.EdbCode, startDate, endDate)
  1313. if err != nil && err.Error() != utils.ErrNoRow() {
  1314. errMsg = "RefreshAllCalculateTbz Err:" + err.Error()
  1315. break
  1316. }
  1317. case utils.DATA_SOURCE_CALCULATE_TCZ: //同差值
  1318. calculateTcz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1319. if err != nil {
  1320. errMsg = "GetEdbInfoCalculateTczDetail Err:" + err.Error()
  1321. break
  1322. }
  1323. fromEdbInfo, err := models.GetEdbInfoById(calculateTcz.FromEdbInfoId)
  1324. if err != nil {
  1325. errMsg = "GetEdbInfoById Err:" + err.Error()
  1326. break
  1327. }
  1328. //startDate = edbInfo.StartDate
  1329. endDate = time.Now().Format(utils.FormatDate)
  1330. err = models.RefreshAllCalculateTcz(edbInfoId, source, subSource, fromEdbInfo, calculateTcz.EdbCode, startDate, endDate)
  1331. if err != nil && err.Error() != utils.ErrNoRow() {
  1332. errMsg = "RefreshCalculateTcz Err:" + err.Error()
  1333. break
  1334. }
  1335. case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS: //N数值移动平均计算
  1336. calculateNszydpjjs, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1337. if err != nil {
  1338. errMsg = "GetEdbInfoCalculateNszydpjjsDetail Err:" + err.Error()
  1339. break
  1340. }
  1341. fromEdbInfo, err := models.GetEdbInfoById(calculateNszydpjjs.FromEdbInfoId)
  1342. if err != nil {
  1343. errMsg = "GetEdbInfoById Err:" + err.Error()
  1344. break
  1345. }
  1346. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  1347. //startDate = edbInfo.StartDate
  1348. startDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
  1349. err = models.RefreshAllCalculateNszydpjjs(edbInfoId, edbInfo.Source, edbInfo.SubSource, formulaInt, fromEdbInfo, calculateNszydpjjs.EdbCode, startDate)
  1350. if err != nil && err.Error() != utils.ErrNoRow() {
  1351. errMsg = "RefreshCalculateNszydpjjs Err:" + err.Error()
  1352. break
  1353. }
  1354. case utils.DATA_SOURCE_CALCULATE_HBZ: //刷新环比值
  1355. calculateTbz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1356. if err != nil {
  1357. errMsg = "GetEdbInfoCalculateHbzDetail Err:" + err.Error()
  1358. break
  1359. }
  1360. fromEdbInfo, err := models.GetEdbInfoById(calculateTbz.FromEdbInfoId)
  1361. if err != nil {
  1362. errMsg = "GetEdbInfoById Err:" + err.Error()
  1363. break
  1364. }
  1365. startDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
  1366. endDate = time.Now().Format(utils.FormatDate)
  1367. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  1368. err = models.RefreshAllCalculateHbz(edbInfoId, source, subSource, fromEdbInfo, calculateTbz.EdbCode, startDate, endDate, formulaInt)
  1369. if err != nil && err.Error() != utils.ErrNoRow() {
  1370. errMsg = "RefreshAllCalculateHbz Err:" + err.Error()
  1371. break
  1372. }
  1373. case utils.DATA_SOURCE_CALCULATE_HCZ: //刷新环差值
  1374. calculateTbz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1375. if err != nil {
  1376. errMsg = "GetEdbInfoCalculateHczDetail Err:" + err.Error()
  1377. break
  1378. }
  1379. fromEdbInfo, err := models.GetEdbInfoById(calculateTbz.FromEdbInfoId)
  1380. if err != nil {
  1381. errMsg = "GetEdbInfoById Err:" + err.Error()
  1382. break
  1383. }
  1384. startDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
  1385. endDate = time.Now().Format(utils.FormatDate)
  1386. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  1387. err = models.RefreshAllCalculateHcz(edbInfoId, source, subSource, fromEdbInfo, calculateTbz.EdbCode, startDate, endDate, formulaInt)
  1388. if err != nil && err.Error() != utils.ErrNoRow() {
  1389. errMsg = "RefreshAllCalculateHcz Err:" + err.Error()
  1390. break
  1391. }
  1392. case utils.DATA_SOURCE_CALCULATE_BP: //刷新升频
  1393. calculateTbz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1394. if err != nil {
  1395. errMsg = "GetEdbInfoCalculateTbzDetail Err:" + err.Error()
  1396. break
  1397. }
  1398. fromEdbInfo, err := models.GetEdbInfoById(calculateTbz.FromEdbInfoId)
  1399. if err != nil {
  1400. errMsg = "GetEdbInfoById Err:" + err.Error()
  1401. break
  1402. }
  1403. //startDate = edbInfo.StartDate
  1404. endDate = time.Now().Format(utils.FormatDate)
  1405. err = models.RefreshAllCalculateBp(edbInfoId, source, subSource, fromEdbInfo, calculateTbz.EdbCode, startDate, endDate, edbInfo.EmptyType)
  1406. if err != nil && err.Error() != utils.ErrNoRow() {
  1407. errMsg = "RefreshAllCalculateBp Err:" + err.Error()
  1408. break
  1409. }
  1410. case utils.DATA_SOURCE_CALCULATE_TIME_SHIFT:
  1411. calculate, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1412. if err != nil {
  1413. errMsg = "GetEdbInfoCalculateTbzDetail Err:" + err.Error()
  1414. break
  1415. }
  1416. fromEdbInfo, err := models.GetEdbInfoById(calculate.FromEdbInfoId)
  1417. if err != nil {
  1418. errMsg = "GetEdbInfoById Err:" + err.Error()
  1419. break
  1420. }
  1421. //startDate = edbInfo.StartDate
  1422. startDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
  1423. endDate = time.Now().Format(utils.FormatDate)
  1424. formulaInt, _ := strconv.Atoi(calculate.CalculateFormula)
  1425. err = models.RefreshAllCalculateTimeShift(edbInfoId, source, subSource, formulaInt, calculate.MoveType, fromEdbInfo, calculate.EdbCode, startDate, endDate, calculate.MoveFrequency)
  1426. if err != nil && err.Error() != utils.ErrNoRow() {
  1427. errMsg = "RefreshAllCalculateTimeShift Err:" + err.Error()
  1428. }
  1429. case utils.DATA_SOURCE_CALCULATE_PHASE_SHIFT: // 期数位移
  1430. calculate, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1431. if err != nil {
  1432. errMsg = "GetEdbInfoCalculateMappingDetail Err:" + err.Error()
  1433. break
  1434. }
  1435. fromEdbInfo, err := models.GetEdbInfoById(calculate.FromEdbInfoId)
  1436. if err != nil {
  1437. errMsg = "GetEdbInfoById Err:" + err.Error()
  1438. break
  1439. }
  1440. //startDate = edbInfo.StartDate
  1441. startDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
  1442. endDate = time.Now().Format(utils.FormatDate)
  1443. formulaInt, _ := strconv.Atoi(calculate.CalculateFormula)
  1444. err = models.RefreshAllCalculatePhaseShift(edbInfoId, source, subSource, formulaInt, calculate.MoveType, fromEdbInfo, calculate.EdbCode, startDate, endDate, calculate.MoveFrequency)
  1445. if err != nil && err.Error() != utils.ErrNoRow() {
  1446. errMsg = "RefreshAllCalculatePhaseShift Err:" + err.Error()
  1447. }
  1448. case utils.DATA_SOURCE_RESIDUAL_ANALYSIS: // 残差分析
  1449. calculate, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1450. if err != nil {
  1451. errMsg = "GetEdbInfoCalculateMappingDetail Err:" + err.Error()
  1452. break
  1453. }
  1454. fromEdbInfo, err := models.GetEdbInfoById(calculate.FromEdbInfoId)
  1455. if err != nil {
  1456. errMsg = "GetEdbInfoById Err:" + err.Error()
  1457. break
  1458. }
  1459. //startDate = edbInfo.StartDate
  1460. startDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
  1461. endDate = time.Now().Format(utils.FormatDate)
  1462. formulaInt, _ := strconv.Atoi(calculate.CalculateFormula)
  1463. err = models.RefreshAllCalculateResidualAnalysis(edbInfoId, source, subSource, formulaInt, calculate.MoveType, fromEdbInfo, calculate.EdbCode, startDate, endDate, calculate.MoveFrequency)
  1464. if err != nil && err.Error() != utils.ErrNoRow() {
  1465. errMsg = "RefreshAllCalculateResidualAnalysis Err:" + err.Error()
  1466. }
  1467. case utils.DATA_SOURCE_CALCULATE_ZJPJ: //刷新直接拼接
  1468. err = models.RefreshAllCalculateZjpj(edbInfo)
  1469. if err != nil && err.Error() != utils.ErrNoRow() {
  1470. errMsg = "RefreshAllCalculateZjpj Err:" + err.Error()
  1471. break
  1472. }
  1473. case utils.DATA_SOURCE_CALCULATE_LJZTBPJ: //刷新累计值同比拼接
  1474. err = models.RefreshAllCalculateLjztbpj(edbInfo)
  1475. if err != nil && err.Error() != utils.ErrNoRow() {
  1476. errMsg = "RefreshAllCalculateLjztbpj Err:" + err.Error()
  1477. break
  1478. }
  1479. case utils.DATA_SOURCE_PYTHON: //python代码运算
  1480. edbPythonCode, err := models.GetEdbPythonCodeById(edbInfo.EdbInfoId)
  1481. if err != nil {
  1482. errMsg = "获取python代码失败 Err:" + err.Error()
  1483. break
  1484. }
  1485. edbData, err, errMsg := services.ExecPythonCode(edbInfo.EdbCode, edbPythonCode.PythonCode)
  1486. if err != nil {
  1487. br.Msg = "获取数据失败"
  1488. br.ErrMsg = "python代码获取数据失败,err:" + err.Error()
  1489. if errMsg != "" {
  1490. br.Msg = errMsg
  1491. }
  1492. return
  1493. }
  1494. err = models.RefreshAllPythonEdb(edbInfo, edbData)
  1495. if err != nil && err.Error() != utils.ErrNoRow() {
  1496. errMsg = "RefreshAllPythonEdb Err:" + err.Error()
  1497. break
  1498. }
  1499. case utils.DATA_SOURCE_CALCULATE_CJJX: //超季节性
  1500. calculateCjjx, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1501. if err != nil {
  1502. errMsg = "GetEdbInfoCalculateNszydpjjsDetail Err:" + err.Error()
  1503. break
  1504. }
  1505. fromEdbInfo, err := models.GetEdbInfoById(calculateCjjx.FromEdbInfoId)
  1506. if err != nil {
  1507. errMsg = "GetEdbInfoById Err:" + err.Error()
  1508. break
  1509. }
  1510. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  1511. startDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
  1512. err = models.RefreshAllCalculateCjjx(edbInfoId, edbInfo.Source, edbInfo.SubSource, fromEdbInfo, calculateCjjx.EdbCode, startDate, "", edbInfo.Calendar, formulaInt)
  1513. if err != nil && err.Error() != utils.ErrNoRow() {
  1514. errMsg = "RefreshAllCalculateCjjx Err:" + err.Error()
  1515. break
  1516. }
  1517. case utils.DATA_SOURCE_CALCULATE_NHCC: //nhcc
  1518. err = models.RefreshAllCalculateNhcc(edbInfo)
  1519. if err != nil && err.Error() != utils.ErrNoRow() {
  1520. errMsg = "RefreshAllCalculateNhcc Err:" + err.Error()
  1521. break
  1522. }
  1523. case utils.DATA_SOURCE_CALCULATE_ADJUST:
  1524. calculateMapping, err := models.GetEdbInfoCalculateMappingDetail(edbInfo.EdbInfoId)
  1525. if err != nil {
  1526. br.Msg = "获取来源指标异常!"
  1527. br.ErrMsg = "获取来源指标异常,Err:" + err.Error()
  1528. return
  1529. }
  1530. fromEdbInfo, err := models.GetEdbInfoById(calculateMapping.FromEdbInfoId)
  1531. if err != nil {
  1532. br.Msg = "来源指标不存在!"
  1533. br.ErrMsg = "来源指标不存在"
  1534. return
  1535. }
  1536. err = models.RefreshAllAdjustEdb(edbInfo, fromEdbInfo)
  1537. if err != nil && err.Error() != utils.ErrNoRow() {
  1538. errMsg = "RefreshAllAdjustEdb Err:" + err.Error()
  1539. break
  1540. }
  1541. case utils.DATA_SOURCE_CALCULATE_JP: //刷新降频
  1542. calculateMapping, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1543. if err != nil {
  1544. errMsg = "GetEdbInfoCalculateTbzDetail Err:" + err.Error()
  1545. break
  1546. }
  1547. fromEdbInfo, err := models.GetEdbInfoById(calculateMapping.FromEdbInfoId)
  1548. if err != nil {
  1549. errMsg = "GetEdbInfoById Err:" + err.Error()
  1550. break
  1551. }
  1552. //startDate = edbInfo.StartDate
  1553. endDate = time.Now().Format(utils.FormatDate)
  1554. err = models.RefreshAllCalculateJp(edbInfoId, source, subSource, fromEdbInfo, edbInfo.EdbCode, edbInfo.Frequency, edbInfo.CalculateFormula)
  1555. if err != nil && err.Error() != utils.ErrNoRow() {
  1556. errMsg = "RefreshAllCalculateJp Err:" + err.Error()
  1557. break
  1558. }
  1559. case utils.DATA_SOURCE_CALCULATE_NH: //刷新年化
  1560. calculateMapping, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1561. if err != nil {
  1562. errMsg = "GetEdbInfoCalculateTbzDetail Err:" + err.Error()
  1563. break
  1564. }
  1565. fromEdbInfo, err := models.GetEdbInfoById(calculateMapping.FromEdbInfoId)
  1566. if err != nil {
  1567. errMsg = "GetEdbInfoById Err:" + err.Error()
  1568. break
  1569. }
  1570. //startDate = edbInfo.StartDate
  1571. endDate = time.Now().Format(utils.FormatDate)
  1572. err = models.RefreshAllCalculateNh(edbInfoId, source, subSource, fromEdbInfo, edbInfo.EdbCode)
  1573. if err != nil && err.Error() != utils.ErrNoRow() {
  1574. errMsg = "RefreshAllCalculateNh Err:" + err.Error()
  1575. break
  1576. }
  1577. case utils.DATA_SOURCE_CALCULATE_KSZS: //刷新扩散指数
  1578. //startDate = edbInfo.StartDate
  1579. endDate = time.Now().Format(utils.FormatDate)
  1580. err = models.RefreshAllCalculateKszs(edbInfo)
  1581. if err != nil && err.Error() != utils.ErrNoRow() {
  1582. errMsg = "RefreshAllCalculateKszs Err:" + err.Error()
  1583. break
  1584. }
  1585. case utils.DATA_SOURCE_STOCK_PLANT: //存量装置
  1586. //startDate = edbInfo.StartDate
  1587. endDate = time.Now().Format(utils.FormatDate)
  1588. req.StartDate = `` // 开始日期也给过滤吧,万一研究员补充了之前的数据呢,还是过滤掉吧
  1589. err = models.RefreshEdbDataFromStockPlant(req.EdbInfoId, req.EdbCode, req.StartDate)
  1590. if err != nil && err.Error() != utils.ErrNoRow() {
  1591. errMsg = "RefreshEdbDataFromStockPlant Err:" + err.Error()
  1592. break
  1593. }
  1594. case utils.DATA_SOURCE_CALCULATE_CORRELATION:
  1595. err, errMsg = models.RefreshAllCalculateCorrelation(edbInfo)
  1596. case utils.DATA_SOURCE_CALCULATE_RJZ: //刷新日均值
  1597. calculateRjz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1598. if err != nil {
  1599. errMsg = "GetEdbInfoCalculateMappingDetail Err:" + err.Error()
  1600. break
  1601. }
  1602. fromEdbInfo, err := models.GetEdbInfoById(calculateRjz.FromEdbInfoId)
  1603. if err != nil {
  1604. errMsg = "GetEdbInfoById Err:" + err.Error()
  1605. break
  1606. }
  1607. //startDate = edbInfo.StartDate
  1608. endDate = time.Now().Format(utils.FormatDate)
  1609. err = models.RefreshAllCalculateRjz(edbInfoId, source, fromEdbInfo, calculateRjz.EdbCode, startDate, endDate)
  1610. if err != nil && err.Error() != utils.ErrNoRow() {
  1611. errMsg = "RefreshAllCalculateRjz Err:" + err.Error()
  1612. break
  1613. }
  1614. case utils.DATA_SOURCE_CALCULATE_STL: // STL趋势分解
  1615. msg, err := services.RefreshStlData(edbInfoId)
  1616. if err != nil {
  1617. if msg == "" {
  1618. errMsg = "RefreshStlData Err:" + err.Error()
  1619. } else {
  1620. errMsg = msg + " Err:" + err.Error()
  1621. }
  1622. break
  1623. }
  1624. default:
  1625. // 获取通用的数据源处理服务
  1626. baseEdbInfoModel = models.GetBaseEdbInfoModel(source)
  1627. // 没有找到的话,那么就直接返回报错吧
  1628. if baseEdbInfoModel == nil {
  1629. br.Msg = "来源异常,请联系相关开发!"
  1630. br.ErrMsg = "来源异常,请联系相关开发"
  1631. return
  1632. }
  1633. err, errMsg = baseEdbInfoModel.Refresh(refreshParams)
  1634. }
  1635. if errMsg != `` {
  1636. br.Msg = "刷新指标失败!"
  1637. br.ErrMsg = "刷新指标失败,err:" + errMsg
  1638. return
  1639. }
  1640. // 更新指标最大最小值
  1641. err, errMsg = models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  1642. if err != nil {
  1643. br.Msg = errMsg
  1644. br.ErrMsg = err.Error()
  1645. return
  1646. }
  1647. // 更新ES
  1648. go logic.UpdateEs(edbInfoId)
  1649. br.Ret = 200
  1650. br.Success = true
  1651. br.Msg = "获取成功"
  1652. }
  1653. // SaveAdjust
  1654. // @Title 保存数据调整指标接口
  1655. // @Description 保存数据调整指标接口
  1656. // @Success 200 {object} models.SaveAdjustEdbReq
  1657. // @router /adjust/save [post]
  1658. func (this *CalculateController) SaveAdjust() {
  1659. br := new(models.BaseResponse).Init()
  1660. defer func() {
  1661. this.Data["json"] = br
  1662. this.ServeJSON()
  1663. }()
  1664. var req models.SaveAdjustEdbReq
  1665. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1666. if err != nil {
  1667. br.Msg = "参数解析异常!"
  1668. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1669. return
  1670. }
  1671. req.EdbName = strings.Trim(req.EdbName, " ")
  1672. if req.EdbName == "" {
  1673. br.Msg = "指标名称不能为空"
  1674. return
  1675. }
  1676. if req.Frequency == "" {
  1677. br.Msg = "频率不能为空"
  1678. return
  1679. }
  1680. if req.Unit == "" {
  1681. br.Msg = "单位不能为空"
  1682. return
  1683. }
  1684. if req.ClassifyId <= 0 {
  1685. br.Msg = "请选择分类"
  1686. return
  1687. }
  1688. if len(req.DataList) == 0 {
  1689. br.Msg = "请填写数据"
  1690. return
  1691. }
  1692. //加入缓存机制,避免创建同一个名称的指标 start
  1693. redisKey := fmt.Sprint("edb_info:adjust:add:", utils.DATA_SOURCE_CALCULATE_ADJUST, ":", req.EdbName)
  1694. isExist := utils.Rc.IsExist(redisKey)
  1695. if isExist {
  1696. br.Msg = "指标正在处理,请勿重复提交"
  1697. return
  1698. } else {
  1699. //设置3分钟缓存
  1700. utils.Rc.SetNX(redisKey, 1, time.Second*300)
  1701. defer func() {
  1702. _ = utils.Rc.Delete(redisKey)
  1703. }()
  1704. }
  1705. // 校验指标名称是否存在
  1706. existEdbName, err := logic.CheckExistByEdbNameAndEdbInfoId(0, req.EdbInfoId, req.EdbName, this.Lang)
  1707. if err != nil {
  1708. br.Msg = "判断指标名称是否存在失败"
  1709. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  1710. return
  1711. }
  1712. if existEdbName {
  1713. br.Msg = "指标名称已存在,请重新填写"
  1714. br.ErrMsg = "指标名称已存在,请重新填写"
  1715. return
  1716. }
  1717. edbInfo, err, errMsg := models.SaveAdjustEdb(req, this.Lang)
  1718. if err != nil {
  1719. br.Msg = errMsg
  1720. br.Msg = "添加指标失败,Err:" + err.Error()
  1721. return
  1722. }
  1723. //处理同名指标
  1724. {
  1725. edbNameList, err := models.GetEdbInfoByName(req.EdbName)
  1726. if err != nil {
  1727. br.Msg = "保存失败"
  1728. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  1729. return
  1730. }
  1731. if len(edbNameList) >= 2 {
  1732. for _, v := range edbNameList {
  1733. edbName := v.EdbName + "(" + v.SourceName + ")"
  1734. err = models.ModifyEdbInfoNameSource(edbName, v.EdbInfoId)
  1735. if err != nil {
  1736. br.Msg = "保存失败"
  1737. br.ErrMsg = "修改指标名称失败,Err:" + err.Error()
  1738. return
  1739. }
  1740. }
  1741. }
  1742. }
  1743. // 更新指标最大最小值
  1744. err, errMsg = models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  1745. if err != nil {
  1746. br.Msg = errMsg
  1747. br.ErrMsg = err.Error()
  1748. return
  1749. }
  1750. resp := models.AddEdbInfoResp{
  1751. EdbInfoId: edbInfo.EdbInfoId,
  1752. UniqueCode: edbInfo.UniqueCode,
  1753. }
  1754. // 判断是否需要禁用
  1755. go services.DisableEdbInfoNoUpdate(edbInfo)
  1756. // 重置计算指标中的引用关系
  1757. go services.ResetEdbRelation(edbInfo.EdbInfoId)
  1758. br.Ret = 200
  1759. br.Success = true
  1760. br.Msg = "保存成功"
  1761. br.Data = resp
  1762. br.IsAddLog = true
  1763. }
  1764. // CalculateComputeCorrelation
  1765. // @Title 拟合残差计算相关性
  1766. // @Description 拟合残差计算相关性接口
  1767. // @Param request body models.EdbInfoCalculateBatchSaveReq true "type json string"
  1768. // @Success Ret=200 返回指标id
  1769. // @router /compute_correlation [post]
  1770. func (this *CalculateController) CalculateComputeCorrelation() {
  1771. br := new(models.BaseResponse).Init()
  1772. defer func() {
  1773. this.Data["json"] = br
  1774. this.ServeJSON()
  1775. }()
  1776. var req models.EdbInfoCalculateBatchSaveReq
  1777. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1778. if err != nil {
  1779. br.Msg = "参数解析异常!"
  1780. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1781. return
  1782. }
  1783. if len(req.EdbInfoIdArr) != 2 {
  1784. br.Msg = "选择的指标异常,请重新选择"
  1785. return
  1786. }
  1787. //校验时间格式
  1788. //数据格式:2022-11-01,2022-11-10
  1789. timeList := strings.Split(req.Formula, ",")
  1790. if len(timeList) != 2 {
  1791. br.Msg = "选择时间有误,请重新输入"
  1792. return
  1793. }
  1794. startDate, err := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
  1795. if err != nil {
  1796. br.Msg = "开始日期有误,请重新输入"
  1797. return
  1798. }
  1799. endDate, err := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
  1800. if err != nil {
  1801. br.Msg = "结束日期有误,请重新输入"
  1802. return
  1803. }
  1804. if utils.GetTimeSubDay(startDate, endDate) < 2 {
  1805. br.Msg = "日期间隔不得少于两天"
  1806. return
  1807. }
  1808. var nhccDate models.NhccDate // 拟合残差的日期
  1809. nhccDate.StartDate = startDate
  1810. nhccDate.EndDate = endDate
  1811. // 基础指标id
  1812. fromEdbInfoId := req.EdbInfoIdArr[0].EdbInfoId
  1813. if fromEdbInfoId <= 0 {
  1814. br.Msg = "请选择指标"
  1815. return
  1816. }
  1817. // 来源指标(自变量指标)
  1818. fromEdbInfo, err := models.GetEdbInfoById(fromEdbInfoId)
  1819. if err != nil {
  1820. br.Msg = "获取指标信息失败"
  1821. br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
  1822. return
  1823. }
  1824. // 因变量指标
  1825. secondEdbInfoReq := req.EdbInfoIdArr[1]
  1826. secondEdbInfo, tmpErr := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  1827. if tmpErr != nil {
  1828. br.Msg = "获取因变量的指标信息失败"
  1829. br.ErrMsg = "获取因变量的指标信息失败:Err:" + tmpErr.Error()
  1830. return
  1831. }
  1832. if fromEdbInfo.EdbInfoId == secondEdbInfo.EdbInfoId {
  1833. br.Msg = "两个指标不允许为同一个"
  1834. br.ErrMsg = "两个指标不允许为同一个"
  1835. return
  1836. }
  1837. aVal, bVal, rVal, err, errMsg := models.CalculateComputeCorrelation(&req, fromEdbInfo, secondEdbInfo, nhccDate)
  1838. if err != nil {
  1839. br.Msg = "计算失败"
  1840. if errMsg != `` {
  1841. br.Msg = errMsg
  1842. }
  1843. br.ErrMsg = "计算失败,ERR:" + err.Error()
  1844. return
  1845. }
  1846. connStr := ``
  1847. if bVal >= 0 {
  1848. connStr = `+`
  1849. }
  1850. valStr := fmt.Sprintf("y = %sx %s %s r=%f", utils.SubFloatToString(aVal, 4), connStr, utils.SubFloatToString(bVal, 4), rVal)
  1851. br.Ret = 200
  1852. br.Success = true
  1853. br.Msg = "计算成功"
  1854. br.Data = valStr
  1855. br.IsAddLog = true
  1856. }
  1857. // ResetCustomAnalysisData
  1858. // @Title 自定义表格数据重置
  1859. // @Description 自定义表格数据重置
  1860. // @Param request body models.ResetCustomAnalysisData true "type json string"
  1861. // @Success Ret=200 返回指标id
  1862. // @router /custom_analysis/reset [post]
  1863. func (this *CalculateController) ResetCustomAnalysisData() {
  1864. br := new(models.BaseResponse).Init()
  1865. defer func() {
  1866. this.Data["json"] = br
  1867. this.ServeJSON()
  1868. }()
  1869. var req models.ResetCustomAnalysisData
  1870. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1871. if err != nil {
  1872. br.Msg = "参数解析异常!"
  1873. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1874. br.IsSendEmail = false
  1875. return
  1876. }
  1877. if req.EdbInfoId <= 0 {
  1878. br.Msg = "请选择指标"
  1879. br.ErrMsg = br.Msg
  1880. br.IsSendEmail = false
  1881. return
  1882. }
  1883. if len(req.DateList) <= 0 {
  1884. br.Msg = "请传入日期序列"
  1885. br.ErrMsg = br.Msg
  1886. br.IsSendEmail = false
  1887. return
  1888. }
  1889. if len(req.DataList) <= 0 {
  1890. br.Msg = "请传入数据序列"
  1891. br.ErrMsg = br.Msg
  1892. br.IsSendEmail = false
  1893. return
  1894. }
  1895. edbInfo, err := models.GetEdbInfoById(req.EdbInfoId)
  1896. if err != nil {
  1897. if err.Error() == utils.ErrNoRow() {
  1898. br.Msg = "指标已被删除,请刷新页面"
  1899. br.ErrMsg = "指标已被删除,请刷新页面:Err:" + err.Error()
  1900. return
  1901. }
  1902. br.Msg = "获取指标信息失败"
  1903. br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
  1904. return
  1905. }
  1906. baseEdbInfoModel := models.CustomAnalysis{}
  1907. err, errMsg := baseEdbInfoModel.ResetData(edbInfo, req.DateList, req.DataList)
  1908. if err != nil {
  1909. br.Msg = "更新数据失败"
  1910. if errMsg != `` {
  1911. br.Msg = errMsg
  1912. }
  1913. br.ErrMsg = "更新数据失败 Err:" + err.Error()
  1914. return
  1915. }
  1916. if edbInfo == nil {
  1917. br.Msg = "更新数据失败"
  1918. br.ErrMsg = "更新数据失败,指标ID错误:"
  1919. return
  1920. }
  1921. // 更新指标最大最小值
  1922. err, errMsg = models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  1923. if err != nil {
  1924. br.Msg = errMsg
  1925. br.ErrMsg = err.Error()
  1926. return
  1927. }
  1928. resp := models.AddEdbInfoResp{
  1929. EdbInfoId: edbInfo.EdbInfoId,
  1930. UniqueCode: edbInfo.UniqueCode,
  1931. }
  1932. br.Ret = 200
  1933. br.Success = true
  1934. br.Msg = "处理成功"
  1935. br.Data = resp
  1936. br.IsAddLog = true
  1937. }
  1938. // Calculate
  1939. // @Title 基础数据计算
  1940. // @Description 拟合残差计算相关性接口
  1941. // @Param request body models.EdbInfoCalculateBatchSaveReq true "type json string"
  1942. // @Success Ret=200 返回指标id
  1943. // @router /base [post]
  1944. func (this *CalculateController) Calculate() {
  1945. br := new(models.BaseResponse).Init()
  1946. defer func() {
  1947. this.Data["json"] = br
  1948. this.ServeJSON()
  1949. }()
  1950. var req models.BaseCalculateBySearchData
  1951. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1952. if err != nil {
  1953. br.Msg = "参数解析异常!"
  1954. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1955. return
  1956. }
  1957. dateDataMap := make(map[time.Time]float64)
  1958. var errMsg string
  1959. dataList, err := models.EdbInfoSearchDataToData(req.DataList)
  1960. if err != nil {
  1961. br.Msg = "计算失败,数据异常!"
  1962. br.ErrMsg = "计算失败,数据异常,Err:" + err.Error()
  1963. return
  1964. }
  1965. baseCalculate := models.BaseCalculate{
  1966. DataList: dataList,
  1967. Frequency: req.Frequency,
  1968. Formula: req.Formula,
  1969. Calendar: req.Calendar,
  1970. MoveType: req.MoveType,
  1971. MoveFrequency: req.MoveFrequency,
  1972. FromFrequency: req.FromFrequency,
  1973. Source: req.Source,
  1974. }
  1975. //1:累计值转月;2:累计值转季;3:同比值;4:同差值;5:N数值移动平均数计算;6:环比值;7:环差值;8:升频;9:降频;10:时间移位;11:超季节性;12:年化;13:累计值;14:累计值年初至今;15:指数修匀;16:日均值
  1976. switch baseCalculate.Source {
  1977. case 1:
  1978. dateDataMap, err, errMsg = baseCalculate.Ljzzy()
  1979. case 2:
  1980. dateDataMap, err, errMsg = baseCalculate.Ljzzj()
  1981. case 3:
  1982. dateDataMap, err, errMsg = baseCalculate.Tbz()
  1983. case 4:
  1984. dateDataMap, err, errMsg = baseCalculate.Tcz()
  1985. case 5:
  1986. dateDataMap, err, errMsg = baseCalculate.Nszydpjjs()
  1987. case 6:
  1988. dateDataMap, err, errMsg = baseCalculate.Hbz()
  1989. case 7:
  1990. dateDataMap, err, errMsg = baseCalculate.Hcz()
  1991. case 8:
  1992. dateDataMap, err, errMsg = baseCalculate.UpFrequency()
  1993. case 9:
  1994. dateDataMap, err, errMsg = baseCalculate.DownFrequency()
  1995. case 10:
  1996. dateDataMap, err, errMsg = baseCalculate.TimeShift()
  1997. case 11:
  1998. dateDataMap, err, errMsg = baseCalculate.Cjjx()
  1999. case 12:
  2000. dateDataMap, err, errMsg = baseCalculate.Annualized()
  2001. case 13:
  2002. dateDataMap, err, errMsg = baseCalculate.Ljz()
  2003. case 14:
  2004. dateDataMap, err, errMsg = baseCalculate.LjzNczj()
  2005. case 15:
  2006. dateDataMap, err, errMsg = baseCalculate.ExponentialSmoothing()
  2007. case 16:
  2008. dateDataMap, err, errMsg = baseCalculate.Rjz()
  2009. default:
  2010. errMsg = "错误的计算类型"
  2011. err = errors.New(errMsg + ":" + strconv.Itoa(baseCalculate.Source))
  2012. }
  2013. if err != nil {
  2014. br.Msg = "计算失败"
  2015. if errMsg != `` {
  2016. br.Msg = errMsg
  2017. }
  2018. br.ErrMsg = err.Error()
  2019. return
  2020. }
  2021. dateStrDataMap, dateList := models.GetDateDataAndDateList(dateDataMap)
  2022. br.Ret = 200
  2023. br.Success = true
  2024. br.Msg = "计算成功"
  2025. br.Data = models.BaseCalculateResp{
  2026. DataMap: dateStrDataMap,
  2027. DateList: dateList,
  2028. }
  2029. br.IsAddLog = true
  2030. }
  2031. // BatchSaveMulti
  2032. // @Title 批量计算 累计值转月-同比值-同差等计算新增
  2033. // @Description 批量计算 累计值转月-同比值-同差等计算新增接口
  2034. // @Param request body models.EdbInfoCalculateBatchSaveReq true "type json string"
  2035. // @Success Ret=200 返回指标id
  2036. // @router /batch/save/multi [post]
  2037. func (this *CalculateController) BatchSaveMulti() {
  2038. br := new(models.BaseResponse).Init()
  2039. defer func() {
  2040. if br.ErrMsg == "" {
  2041. br.IsSendEmail = false
  2042. }
  2043. this.Data["json"] = br
  2044. this.ServeJSON()
  2045. }()
  2046. var req models.CalculateBatchSaveReq
  2047. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  2048. if err != nil {
  2049. br.Msg = "参数解析异常!"
  2050. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2051. return
  2052. }
  2053. resp := models.BatchEdbInfoCalculateBatchSaveResp{
  2054. Fail: make([]models.BatchEdbInfoCalculateBatchSaveFailResp, 0),
  2055. Success: make([]models.BatchEdbInfoCalculateBatchSaveSuccessResp, 0),
  2056. }
  2057. redisKeyList := make([]string, 0) //需要清理的缓存key列表
  2058. defer func() {
  2059. for _, redisKey := range redisKeyList {
  2060. utils.Rc.Delete(redisKey)
  2061. }
  2062. }()
  2063. var wg sync.WaitGroup
  2064. // 校验参数
  2065. for _, item := range req.EdbList {
  2066. calculateId := item.CalculateId
  2067. //加入缓存机制,避免创建同一个名称的指标 start
  2068. redisKey := fmt.Sprint("edb_info:calculate:batch:save:", req.AdminId, ":", req.Source, ":", calculateId)
  2069. isExist := utils.Rc.IsExist(redisKey)
  2070. if isExist {
  2071. resp.Fail = append(resp.Fail, models.BatchEdbInfoCalculateBatchSaveFailResp{
  2072. CalculateId: calculateId,
  2073. Msg: "指标正在处理,请勿重复提交",
  2074. })
  2075. continue
  2076. } else {
  2077. //设置3分钟缓存
  2078. utils.Rc.SetNX(redisKey, 1, time.Second*300)
  2079. redisKeyList = append(redisKeyList, redisKey)
  2080. }
  2081. wg.Add(1)
  2082. reqItem := models.EdbInfoCalculateBatchSaveReq{
  2083. AdminId: req.AdminId,
  2084. AdminName: req.AdminName,
  2085. EdbInfoId: item.EdbInfoId,
  2086. EdbName: item.EdbName,
  2087. Frequency: item.Frequency,
  2088. Unit: item.Unit,
  2089. ClassifyId: item.ClassifyId,
  2090. Formula: req.Formula, //N数值移动平均计算、环比值、环差值
  2091. FromEdbInfoId: item.FromEdbInfoId,
  2092. CalculateFormula: req.CalculateFormula,
  2093. Source: req.Source,
  2094. MoveType: req.MoveType,
  2095. MoveFrequency: req.MoveFrequency,
  2096. EdbInfoIdArr: req.EdbInfoIdArr,
  2097. Calendar: req.Calendar,
  2098. EmptyType: req.EmptyType,
  2099. MaxEmptyType: req.MaxEmptyType,
  2100. Extra: req.Extra,
  2101. }
  2102. var edbInfo *models.EdbInfo
  2103. uniqueCode := ""
  2104. errMsg := ""
  2105. go func() {
  2106. defer func() {
  2107. // todo 批量报错处理
  2108. if err != nil {
  2109. // 整理报错的指标
  2110. utils.FileLog.Info(fmt.Sprintf("批量指标计算,保存失败 Err:%v, ErrMsg:%s", err, errMsg))
  2111. resp.Fail = append(resp.Fail, models.BatchEdbInfoCalculateBatchSaveFailResp{
  2112. CalculateId: calculateId,
  2113. Msg: err.Error(),
  2114. ErrMsg: errMsg,
  2115. })
  2116. } else {
  2117. // 整理成功的指标
  2118. //todo 整理执行成功指标信息
  2119. resp.Success = append(resp.Success, models.BatchEdbInfoCalculateBatchSaveSuccessResp{
  2120. ClassifyId: reqItem.ClassifyId,
  2121. CalculateId: calculateId,
  2122. EdbInfoId: edbInfo.EdbInfoId,
  2123. UniqueCode: uniqueCode,
  2124. })
  2125. }
  2126. wg.Done()
  2127. }()
  2128. edbInfo, uniqueCode, err, errMsg = services.EdbCalculateBatchSave(reqItem, this.Lang)
  2129. return
  2130. }()
  2131. }
  2132. wg.Wait()
  2133. br.Ret = 200
  2134. br.Success = true
  2135. br.Msg = "保存成功"
  2136. br.Data = resp
  2137. br.IsAddLog = true
  2138. }
  2139. // BatchEditMulti
  2140. // @Title 批量计算 累计值转月-同比值-同差等计算编辑
  2141. // @Description 批量计算 累计值转月-同比值-同差等计算编辑接口
  2142. // @Param request body models.EdbInfoCalculateBatchSaveReq true "type json string"
  2143. // @Success Ret=200 返回指标id
  2144. // @router /batch/edit/multi [post]
  2145. func (this *CalculateController) BatchEditMulti() {
  2146. br := new(models.BaseResponse).Init()
  2147. defer func() {
  2148. if br.ErrMsg == "" {
  2149. br.IsSendEmail = false
  2150. }
  2151. this.Data["json"] = br
  2152. this.ServeJSON()
  2153. }()
  2154. var req models.CalculateBatchSaveReq
  2155. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  2156. if err != nil {
  2157. br.Msg = "参数解析异常!"
  2158. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2159. return
  2160. }
  2161. resp := models.BatchEdbInfoCalculateBatchSaveResp{
  2162. Fail: make([]models.BatchEdbInfoCalculateBatchSaveFailResp, 0),
  2163. Success: make([]models.BatchEdbInfoCalculateBatchSaveSuccessResp, 0),
  2164. }
  2165. redisKeyList := make([]string, 0) //需要清理的缓存key列表
  2166. defer func() {
  2167. for _, redisKey := range redisKeyList {
  2168. utils.Rc.Delete(redisKey)
  2169. }
  2170. }()
  2171. var wg sync.WaitGroup
  2172. // 校验参数
  2173. for _, item := range req.EdbList {
  2174. calculateId := item.CalculateId
  2175. if item.EdbInfoId <= 0 {
  2176. resp.Fail = append(resp.Fail, models.BatchEdbInfoCalculateBatchSaveFailResp{
  2177. CalculateId: calculateId,
  2178. Msg: "请选择要编辑的指标",
  2179. })
  2180. continue
  2181. }
  2182. //加入缓存机制,避免创建同一个名称的指标 start
  2183. redisKey := fmt.Sprint("edb_info:calculate:batch:edit:", req.AdminId, ":", req.Source, ":", item.EdbInfoId)
  2184. isExist := utils.Rc.IsExist(redisKey)
  2185. if isExist {
  2186. resp.Fail = append(resp.Fail, models.BatchEdbInfoCalculateBatchSaveFailResp{
  2187. CalculateId: calculateId,
  2188. Msg: "指标正在处理,请勿重复提交",
  2189. })
  2190. continue
  2191. } else {
  2192. //设置3分钟缓存
  2193. utils.Rc.SetNX(redisKey, 1, time.Second*300)
  2194. redisKeyList = append(redisKeyList, redisKey)
  2195. }
  2196. wg.Add(1)
  2197. reqItem := models.EdbInfoCalculateBatchEditReq{
  2198. EdbInfoId: item.EdbInfoId,
  2199. EdbName: item.EdbName,
  2200. Frequency: item.Frequency,
  2201. Unit: item.Unit,
  2202. ClassifyId: item.ClassifyId,
  2203. AdminId: req.AdminId,
  2204. AdminName: req.AdminName,
  2205. Formula: req.Formula, //N数值移动平均计算、环比值、环差值
  2206. FromEdbInfoId: item.FromEdbInfoId,
  2207. CalculateFormula: req.CalculateFormula,
  2208. Source: req.Source,
  2209. MoveType: req.MoveType,
  2210. MoveFrequency: req.MoveFrequency,
  2211. EdbInfoIdArr: req.EdbInfoIdArr,
  2212. Calendar: req.Calendar,
  2213. EmptyType: req.EmptyType,
  2214. MaxEmptyType: req.MaxEmptyType,
  2215. Extra: req.Extra,
  2216. }
  2217. var errMsg string
  2218. var edbInfo *models.EdbInfo
  2219. uniqueCode := ""
  2220. go func() {
  2221. defer func() {
  2222. // todo 批量报错处理
  2223. if err != nil {
  2224. // 整理报错的指标
  2225. utils.FileLog.Info(fmt.Sprintf("批量指标计算,保存失败 Err:%v, ErrMsg:%s", err, errMsg))
  2226. resp.Fail = append(resp.Fail, models.BatchEdbInfoCalculateBatchSaveFailResp{
  2227. CalculateId: calculateId,
  2228. Msg: err.Error(),
  2229. ErrMsg: errMsg,
  2230. })
  2231. } else {
  2232. // 整理成功的指标
  2233. //todo 整理执行成功指标信息
  2234. resp.Success = append(resp.Success, models.BatchEdbInfoCalculateBatchSaveSuccessResp{
  2235. ClassifyId: reqItem.ClassifyId,
  2236. CalculateId: calculateId,
  2237. EdbInfoId: edbInfo.EdbInfoId,
  2238. UniqueCode: uniqueCode,
  2239. })
  2240. }
  2241. wg.Done()
  2242. }()
  2243. edbInfo, uniqueCode, err, errMsg = services.EdbCalculateBatchEdit(reqItem)
  2244. return
  2245. }()
  2246. }
  2247. wg.Wait()
  2248. if err != nil {
  2249. br.Msg = "批量指标计算,保存失败"
  2250. br.ErrMsg = "批量指标计算,保存失败,Err:" + err.Error()
  2251. return
  2252. }
  2253. br.Ret = 200
  2254. br.Success = true
  2255. br.Msg = "保存成功"
  2256. br.Data = resp
  2257. br.IsAddLog = true
  2258. }
  2259. // StepCalculate
  2260. // @Title 多步骤计算
  2261. // @Description 多步骤计算
  2262. // @Param request body models.StepCalculateBySearchData true "type json string"
  2263. // @Success Ret=200 返回指标id
  2264. // @router /base/step_calculate [post]
  2265. func (this *CalculateController) StepCalculate() {
  2266. br := new(models.BaseResponse).Init()
  2267. defer func() {
  2268. if br.ErrMsg == "" {
  2269. br.IsSendEmail = false
  2270. }
  2271. this.Data["json"] = br
  2272. this.ServeJSON()
  2273. }()
  2274. var req models.StepCalculateBySearchData
  2275. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  2276. br.Msg = "参数解析异常"
  2277. br.ErrMsg = fmt.Sprintf("参数解析失败, Err: %v", e)
  2278. return
  2279. }
  2280. //sort.Slice(req.Calculates, func(i, j int) bool {
  2281. // return req.Calculates[i].Sort < req.Calculates[j].Sort
  2282. //})
  2283. //
  2284. //var errMsg string
  2285. //originData, e := models.EdbInfoSearchDataToData(req.DataList)
  2286. //if e != nil {
  2287. // br.Msg = "基础数据异常"
  2288. // br.ErrMsg = fmt.Sprintf("计算失败, 基础数据异常, Err: %v", e)
  2289. // return
  2290. //}
  2291. //
  2292. //calculateData := originData
  2293. //dateDataMap := make(map[time.Time]float64)
  2294. //for _, v := range req.Calculates {
  2295. // baseCalculate := models.BaseCalculate{
  2296. // DataList: calculateData,
  2297. // Frequency: v.Frequency,
  2298. // Formula: v.Formula,
  2299. // Calendar: v.Calendar,
  2300. // MoveType: v.MoveType,
  2301. // MoveFrequency: v.MoveFrequency,
  2302. // FromFrequency: v.FromFrequency,
  2303. // Source: v.Source,
  2304. // }
  2305. //
  2306. // // 计算方式
  2307. // switch baseCalculate.Source {
  2308. // case utils.EdbBaseCalculateLjzzy:
  2309. // dateDataMap, e, errMsg = baseCalculate.Ljzzy()
  2310. // case utils.EdbBaseCalculateLjzzj:
  2311. // dateDataMap, e, errMsg = baseCalculate.Ljzzj()
  2312. // case utils.EdbBaseCalculateTbz:
  2313. // dateDataMap, e, errMsg = baseCalculate.Tbz()
  2314. // case utils.EdbBaseCalculateTcz:
  2315. // dateDataMap, e, errMsg = baseCalculate.Tcz()
  2316. // case utils.EdbBaseCalculateNszydpjjs:
  2317. // dateDataMap, e, errMsg = baseCalculate.Nszydpjjs()
  2318. // case utils.EdbBaseCalculateHbz:
  2319. // dateDataMap, e, errMsg = baseCalculate.Hbz()
  2320. // case utils.EdbBaseCalculateHcz:
  2321. // dateDataMap, e, errMsg = baseCalculate.Hcz()
  2322. // case utils.EdbBaseCalculateUpFrequency:
  2323. // dateDataMap, e, errMsg = baseCalculate.UpFrequency()
  2324. // case utils.EdbBaseCalculateDownFrequency:
  2325. // dateDataMap, e, errMsg = baseCalculate.DownFrequency()
  2326. // case utils.EdbBaseCalculateTimeShift:
  2327. // dateDataMap, e, errMsg = baseCalculate.TimeShift()
  2328. // case utils.EdbBaseCalculateCjjx:
  2329. // dateDataMap, e, errMsg = baseCalculate.Cjjx()
  2330. // case utils.EdbBaseCalculateAnnualized:
  2331. // dateDataMap, e, errMsg = baseCalculate.Annualized()
  2332. // case utils.EdbBaseCalculateLjz:
  2333. // dateDataMap, e, errMsg = baseCalculate.Ljz()
  2334. // case utils.EdbBaseCalculateLjzNczj:
  2335. // dateDataMap, e, errMsg = baseCalculate.LjzNczj()
  2336. // case utils.EdbBaseCalculateExponentialSmoothing:
  2337. // dateDataMap, e, errMsg = baseCalculate.ExponentialSmoothing()
  2338. // case utils.EdbBaseCalculateRjz:
  2339. // dateDataMap, e, errMsg = baseCalculate.Rjz()
  2340. // default:
  2341. // errMsg = "计算方式无效"
  2342. // e = fmt.Errorf("%s:%d", errMsg, baseCalculate.Source)
  2343. // }
  2344. // if e != nil {
  2345. // br.Msg = "计算失败"
  2346. // if errMsg != "" {
  2347. // br.Msg = errMsg
  2348. // }
  2349. // br.ErrMsg = e.Error()
  2350. // return
  2351. // }
  2352. //
  2353. // calculateData = models.TransDateData2EdbData(dateDataMap)
  2354. //}
  2355. resultData, dates, errMsg, e := services.StepCalculate(req.DataList, req.Calculates)
  2356. if e != nil {
  2357. br.Msg = "计算失败"
  2358. if errMsg != "" {
  2359. br.Msg = errMsg
  2360. }
  2361. br.ErrMsg = fmt.Sprintf("分步骤计算失败, Err: %v", e)
  2362. return
  2363. }
  2364. br.Ret = 200
  2365. br.Success = true
  2366. br.Msg = "计算成功"
  2367. br.Data = models.BaseCalculateResp{
  2368. DataMap: resultData,
  2369. DateList: dates,
  2370. }
  2371. br.IsAddLog = true
  2372. }