base_from_calculate.go 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519
  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(0, 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(0, 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.AddEditEdbInfoRcord(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(0, 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.AddEditEdbInfoRcord(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_CALCULATE_ZJPJ: //刷新直接拼接
  1449. err = models.RefreshAllCalculateZjpj(edbInfo)
  1450. if err != nil && err.Error() != utils.ErrNoRow() {
  1451. errMsg = "RefreshAllCalculateZjpj Err:" + err.Error()
  1452. break
  1453. }
  1454. case utils.DATA_SOURCE_CALCULATE_LJZTBPJ: //刷新累计值同比拼接
  1455. err = models.RefreshAllCalculateLjztbpj(edbInfo)
  1456. if err != nil && err.Error() != utils.ErrNoRow() {
  1457. errMsg = "RefreshAllCalculateLjztbpj Err:" + err.Error()
  1458. break
  1459. }
  1460. case utils.DATA_SOURCE_PYTHON: //python代码运算
  1461. edbPythonCode, err := models.GetEdbPythonCodeById(edbInfo.EdbInfoId)
  1462. if err != nil {
  1463. errMsg = "获取python代码失败 Err:" + err.Error()
  1464. break
  1465. }
  1466. edbData, err, errMsg := services.ExecPythonCode(edbInfo.EdbCode, edbPythonCode.PythonCode)
  1467. if err != nil {
  1468. br.Msg = "获取数据失败"
  1469. br.ErrMsg = "python代码获取数据失败,err:" + err.Error()
  1470. if errMsg != "" {
  1471. br.Msg = errMsg
  1472. }
  1473. return
  1474. }
  1475. err = models.RefreshAllPythonEdb(edbInfo, edbData)
  1476. if err != nil && err.Error() != utils.ErrNoRow() {
  1477. errMsg = "RefreshAllPythonEdb Err:" + err.Error()
  1478. break
  1479. }
  1480. case utils.DATA_SOURCE_CALCULATE_CJJX: //超季节性
  1481. calculateCjjx, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1482. if err != nil {
  1483. errMsg = "GetEdbInfoCalculateNszydpjjsDetail Err:" + err.Error()
  1484. break
  1485. }
  1486. fromEdbInfo, err := models.GetEdbInfoById(calculateCjjx.FromEdbInfoId)
  1487. if err != nil {
  1488. errMsg = "GetEdbInfoById Err:" + err.Error()
  1489. break
  1490. }
  1491. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  1492. startDate = `` //只要填写日期,就会出现问题,还是把日期给去掉吧
  1493. err = models.RefreshAllCalculateCjjx(edbInfoId, edbInfo.Source, edbInfo.SubSource, fromEdbInfo, calculateCjjx.EdbCode, startDate, "", edbInfo.Calendar, formulaInt)
  1494. if err != nil && err.Error() != utils.ErrNoRow() {
  1495. errMsg = "RefreshAllCalculateCjjx Err:" + err.Error()
  1496. break
  1497. }
  1498. case utils.DATA_SOURCE_CALCULATE_NHCC: //nhcc
  1499. err = models.RefreshAllCalculateNhcc(edbInfo)
  1500. if err != nil && err.Error() != utils.ErrNoRow() {
  1501. errMsg = "RefreshAllCalculateNhcc Err:" + err.Error()
  1502. break
  1503. }
  1504. case utils.DATA_SOURCE_CALCULATE_ADJUST:
  1505. calculateMapping, err := models.GetEdbInfoCalculateMappingDetail(edbInfo.EdbInfoId)
  1506. if err != nil {
  1507. br.Msg = "获取来源指标异常!"
  1508. br.ErrMsg = "获取来源指标异常,Err:" + err.Error()
  1509. return
  1510. }
  1511. fromEdbInfo, err := models.GetEdbInfoById(calculateMapping.FromEdbInfoId)
  1512. if err != nil {
  1513. br.Msg = "来源指标不存在!"
  1514. br.ErrMsg = "来源指标不存在"
  1515. return
  1516. }
  1517. err = models.RefreshAllAdjustEdb(edbInfo, fromEdbInfo)
  1518. if err != nil && err.Error() != utils.ErrNoRow() {
  1519. errMsg = "RefreshAllAdjustEdb Err:" + err.Error()
  1520. break
  1521. }
  1522. case utils.DATA_SOURCE_CALCULATE_JP: //刷新降频
  1523. calculateMapping, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1524. if err != nil {
  1525. errMsg = "GetEdbInfoCalculateTbzDetail Err:" + err.Error()
  1526. break
  1527. }
  1528. fromEdbInfo, err := models.GetEdbInfoById(calculateMapping.FromEdbInfoId)
  1529. if err != nil {
  1530. errMsg = "GetEdbInfoById Err:" + err.Error()
  1531. break
  1532. }
  1533. //startDate = edbInfo.StartDate
  1534. endDate = time.Now().Format(utils.FormatDate)
  1535. err = models.RefreshAllCalculateJp(edbInfoId, source, subSource, fromEdbInfo, edbInfo.EdbCode, edbInfo.Frequency, edbInfo.CalculateFormula)
  1536. if err != nil && err.Error() != utils.ErrNoRow() {
  1537. errMsg = "RefreshAllCalculateJp Err:" + err.Error()
  1538. break
  1539. }
  1540. case utils.DATA_SOURCE_CALCULATE_NH: //刷新年化
  1541. calculateMapping, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1542. if err != nil {
  1543. errMsg = "GetEdbInfoCalculateTbzDetail Err:" + err.Error()
  1544. break
  1545. }
  1546. fromEdbInfo, err := models.GetEdbInfoById(calculateMapping.FromEdbInfoId)
  1547. if err != nil {
  1548. errMsg = "GetEdbInfoById Err:" + err.Error()
  1549. break
  1550. }
  1551. //startDate = edbInfo.StartDate
  1552. endDate = time.Now().Format(utils.FormatDate)
  1553. err = models.RefreshAllCalculateNh(edbInfoId, source, subSource, fromEdbInfo, edbInfo.EdbCode)
  1554. if err != nil && err.Error() != utils.ErrNoRow() {
  1555. errMsg = "RefreshAllCalculateNh Err:" + err.Error()
  1556. break
  1557. }
  1558. case utils.DATA_SOURCE_CALCULATE_KSZS: //刷新扩散指数
  1559. //startDate = edbInfo.StartDate
  1560. endDate = time.Now().Format(utils.FormatDate)
  1561. err = models.RefreshAllCalculateKszs(edbInfo)
  1562. if err != nil && err.Error() != utils.ErrNoRow() {
  1563. errMsg = "RefreshAllCalculateKszs Err:" + err.Error()
  1564. break
  1565. }
  1566. case utils.DATA_SOURCE_STOCK_PLANT: //存量装置
  1567. //startDate = edbInfo.StartDate
  1568. endDate = time.Now().Format(utils.FormatDate)
  1569. req.StartDate = `` // 开始日期也给过滤吧,万一研究员补充了之前的数据呢,还是过滤掉吧
  1570. err = models.RefreshEdbDataFromStockPlant(req.EdbInfoId, req.EdbCode, req.StartDate)
  1571. if err != nil && err.Error() != utils.ErrNoRow() {
  1572. errMsg = "RefreshEdbDataFromStockPlant Err:" + err.Error()
  1573. break
  1574. }
  1575. case utils.DATA_SOURCE_CALCULATE_CORRELATION:
  1576. err, errMsg = models.RefreshAllCalculateCorrelation(edbInfo)
  1577. case utils.DATA_SOURCE_CALCULATE_RJZ: //刷新日均值
  1578. calculateRjz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1579. if err != nil {
  1580. errMsg = "GetEdbInfoCalculateMappingDetail Err:" + err.Error()
  1581. break
  1582. }
  1583. fromEdbInfo, err := models.GetEdbInfoById(calculateRjz.FromEdbInfoId)
  1584. if err != nil {
  1585. errMsg = "GetEdbInfoById Err:" + err.Error()
  1586. break
  1587. }
  1588. //startDate = edbInfo.StartDate
  1589. endDate = time.Now().Format(utils.FormatDate)
  1590. err = models.RefreshAllCalculateRjz(edbInfoId, source, fromEdbInfo, calculateRjz.EdbCode, startDate, endDate)
  1591. if err != nil && err.Error() != utils.ErrNoRow() {
  1592. errMsg = "RefreshAllCalculateRjz Err:" + err.Error()
  1593. break
  1594. }
  1595. default:
  1596. // 获取通用的数据源处理服务
  1597. baseEdbInfoModel = models.GetBaseEdbInfoModel(source)
  1598. // 没有找到的话,那么就直接返回报错吧
  1599. if baseEdbInfoModel == nil {
  1600. br.Msg = "来源异常,请联系相关开发!"
  1601. br.ErrMsg = "来源异常,请联系相关开发"
  1602. return
  1603. }
  1604. err, errMsg = baseEdbInfoModel.Refresh(refreshParams)
  1605. }
  1606. if errMsg != `` {
  1607. br.Msg = "刷新指标失败!"
  1608. br.ErrMsg = "刷新指标失败,err:" + errMsg
  1609. return
  1610. }
  1611. // 更新指标最大最小值
  1612. err, errMsg = models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  1613. if err != nil {
  1614. br.Msg = errMsg
  1615. br.ErrMsg = err.Error()
  1616. return
  1617. }
  1618. // 更新ES
  1619. go logic.UpdateEs(edbInfoId)
  1620. br.Ret = 200
  1621. br.Success = true
  1622. br.Msg = "获取成功"
  1623. }
  1624. // SaveAdjust
  1625. // @Title 保存数据调整指标接口
  1626. // @Description 保存数据调整指标接口
  1627. // @Success 200 {object} models.SaveAdjustEdbReq
  1628. // @router /adjust/save [post]
  1629. func (this *CalculateController) SaveAdjust() {
  1630. br := new(models.BaseResponse).Init()
  1631. defer func() {
  1632. this.Data["json"] = br
  1633. this.ServeJSON()
  1634. }()
  1635. var req models.SaveAdjustEdbReq
  1636. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1637. if err != nil {
  1638. br.Msg = "参数解析异常!"
  1639. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1640. return
  1641. }
  1642. req.EdbName = strings.Trim(req.EdbName, " ")
  1643. if req.EdbName == "" {
  1644. br.Msg = "指标名称不能为空"
  1645. return
  1646. }
  1647. if req.Frequency == "" {
  1648. br.Msg = "频率不能为空"
  1649. return
  1650. }
  1651. if req.Unit == "" {
  1652. br.Msg = "单位不能为空"
  1653. return
  1654. }
  1655. if req.ClassifyId <= 0 {
  1656. br.Msg = "请选择分类"
  1657. return
  1658. }
  1659. if len(req.DataList) == 0 {
  1660. br.Msg = "请填写数据"
  1661. return
  1662. }
  1663. //加入缓存机制,避免创建同一个名称的指标 start
  1664. redisKey := fmt.Sprint("edb_info:adjust:add:", utils.DATA_SOURCE_CALCULATE_ADJUST, ":", req.EdbName)
  1665. isExist := utils.Rc.IsExist(redisKey)
  1666. if isExist {
  1667. br.Msg = "指标正在处理,请勿重复提交"
  1668. return
  1669. } else {
  1670. //设置3分钟缓存
  1671. utils.Rc.SetNX(redisKey, 1, time.Second*300)
  1672. defer func() {
  1673. _ = utils.Rc.Delete(redisKey)
  1674. }()
  1675. }
  1676. //获取指标数据
  1677. var condition string
  1678. var pars []interface{}
  1679. condition += " AND edb_name=? "
  1680. pars = append(pars, req.EdbName)
  1681. if req.EdbInfoId > 0 {
  1682. condition += " AND edb_info_id != ? "
  1683. pars = append(pars, req.EdbInfoId)
  1684. }
  1685. count, err := models.GetEdbInfoCountByCondition(condition, pars)
  1686. if err != nil {
  1687. br.Msg = "判断指标名称是否存在失败"
  1688. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  1689. return
  1690. }
  1691. if count > 0 {
  1692. br.Msg = "指标名称已存在,请重新填写"
  1693. br.ErrMsg = "指标名称已存在,请重新填写"
  1694. return
  1695. }
  1696. edbInfo, err, errMsg := models.SaveAdjustEdb(req)
  1697. if err != nil {
  1698. br.Msg = errMsg
  1699. br.Msg = "添加指标失败,Err:" + err.Error()
  1700. return
  1701. }
  1702. //处理同名指标
  1703. {
  1704. edbNameList, err := models.GetEdbInfoByName(req.EdbName)
  1705. if err != nil {
  1706. br.Msg = "保存失败"
  1707. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  1708. return
  1709. }
  1710. if len(edbNameList) >= 2 {
  1711. for _, v := range edbNameList {
  1712. edbName := v.EdbName + "(" + v.SourceName + ")"
  1713. err = models.ModifyEdbInfoNameSource(edbName, v.EdbInfoId)
  1714. if err != nil {
  1715. br.Msg = "保存失败"
  1716. br.ErrMsg = "修改指标名称失败,Err:" + err.Error()
  1717. return
  1718. }
  1719. }
  1720. }
  1721. }
  1722. // 更新指标最大最小值
  1723. err, errMsg = models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  1724. if err != nil {
  1725. br.Msg = errMsg
  1726. br.ErrMsg = err.Error()
  1727. return
  1728. }
  1729. resp := models.AddEdbInfoResp{
  1730. EdbInfoId: edbInfo.EdbInfoId,
  1731. UniqueCode: edbInfo.UniqueCode,
  1732. }
  1733. // 判断是否需要禁用
  1734. go services.DisableEdbInfoNoUpdate(edbInfo)
  1735. // 重置计算指标中的引用关系
  1736. go services.ResetEdbRelation(edbInfo.EdbInfoId)
  1737. br.Ret = 200
  1738. br.Success = true
  1739. br.Msg = "保存成功"
  1740. br.Data = resp
  1741. br.IsAddLog = true
  1742. }
  1743. // CalculateComputeCorrelation
  1744. // @Title 拟合残差计算相关性
  1745. // @Description 拟合残差计算相关性接口
  1746. // @Param request body models.EdbInfoCalculateBatchSaveReq true "type json string"
  1747. // @Success Ret=200 返回指标id
  1748. // @router /compute_correlation [post]
  1749. func (this *CalculateController) CalculateComputeCorrelation() {
  1750. br := new(models.BaseResponse).Init()
  1751. defer func() {
  1752. this.Data["json"] = br
  1753. this.ServeJSON()
  1754. }()
  1755. var req models.EdbInfoCalculateBatchSaveReq
  1756. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1757. if err != nil {
  1758. br.Msg = "参数解析异常!"
  1759. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1760. return
  1761. }
  1762. if len(req.EdbInfoIdArr) != 2 {
  1763. br.Msg = "选择的指标异常,请重新选择"
  1764. return
  1765. }
  1766. //校验时间格式
  1767. //数据格式:2022-11-01,2022-11-10
  1768. timeList := strings.Split(req.Formula, ",")
  1769. if len(timeList) != 2 {
  1770. br.Msg = "选择时间有误,请重新输入"
  1771. return
  1772. }
  1773. startDate, err := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
  1774. if err != nil {
  1775. br.Msg = "开始日期有误,请重新输入"
  1776. return
  1777. }
  1778. endDate, err := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
  1779. if err != nil {
  1780. br.Msg = "结束日期有误,请重新输入"
  1781. return
  1782. }
  1783. if utils.GetTimeSubDay(startDate, endDate) < 2 {
  1784. br.Msg = "日期间隔不得少于两天"
  1785. return
  1786. }
  1787. var nhccDate models.NhccDate // 拟合残差的日期
  1788. nhccDate.StartDate = startDate
  1789. nhccDate.EndDate = endDate
  1790. // 基础指标id
  1791. fromEdbInfoId := req.EdbInfoIdArr[0].EdbInfoId
  1792. if fromEdbInfoId <= 0 {
  1793. br.Msg = "请选择指标"
  1794. return
  1795. }
  1796. // 来源指标(自变量指标)
  1797. fromEdbInfo, err := models.GetEdbInfoById(fromEdbInfoId)
  1798. if err != nil {
  1799. br.Msg = "获取指标信息失败"
  1800. br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
  1801. return
  1802. }
  1803. // 因变量指标
  1804. secondEdbInfoReq := req.EdbInfoIdArr[1]
  1805. secondEdbInfo, tmpErr := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  1806. if tmpErr != nil {
  1807. br.Msg = "获取因变量的指标信息失败"
  1808. br.ErrMsg = "获取因变量的指标信息失败:Err:" + tmpErr.Error()
  1809. return
  1810. }
  1811. if fromEdbInfo.EdbInfoId == secondEdbInfo.EdbInfoId {
  1812. br.Msg = "两个指标不允许为同一个"
  1813. br.ErrMsg = "两个指标不允许为同一个"
  1814. return
  1815. }
  1816. aVal, bVal, rVal, err, errMsg := models.CalculateComputeCorrelation(&req, fromEdbInfo, secondEdbInfo, nhccDate)
  1817. if err != nil {
  1818. br.Msg = "计算失败"
  1819. if errMsg != `` {
  1820. br.Msg = errMsg
  1821. }
  1822. br.ErrMsg = "计算失败,ERR:" + err.Error()
  1823. return
  1824. }
  1825. connStr := ``
  1826. if bVal >= 0 {
  1827. connStr = `+`
  1828. }
  1829. valStr := fmt.Sprintf("y = %sx %s %s R²=%f", utils.SubFloatToString(aVal, 4), connStr, utils.SubFloatToString(bVal, 4), rVal)
  1830. br.Ret = 200
  1831. br.Success = true
  1832. br.Msg = "计算成功"
  1833. br.Data = valStr
  1834. br.IsAddLog = true
  1835. }
  1836. // ResetCustomAnalysisData
  1837. // @Title 自定义表格数据重置
  1838. // @Description 自定义表格数据重置
  1839. // @Param request body models.ResetCustomAnalysisData true "type json string"
  1840. // @Success Ret=200 返回指标id
  1841. // @router /custom_analysis/reset [post]
  1842. func (this *CalculateController) ResetCustomAnalysisData() {
  1843. br := new(models.BaseResponse).Init()
  1844. defer func() {
  1845. this.Data["json"] = br
  1846. this.ServeJSON()
  1847. }()
  1848. var req models.ResetCustomAnalysisData
  1849. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1850. if err != nil {
  1851. br.Msg = "参数解析异常!"
  1852. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1853. br.IsSendEmail = false
  1854. return
  1855. }
  1856. if req.EdbInfoId <= 0 {
  1857. br.Msg = "请选择指标"
  1858. br.ErrMsg = br.Msg
  1859. br.IsSendEmail = false
  1860. return
  1861. }
  1862. if len(req.DateList) <= 0 {
  1863. br.Msg = "请传入日期序列"
  1864. br.ErrMsg = br.Msg
  1865. br.IsSendEmail = false
  1866. return
  1867. }
  1868. if len(req.DataList) <= 0 {
  1869. br.Msg = "请传入数据序列"
  1870. br.ErrMsg = br.Msg
  1871. br.IsSendEmail = false
  1872. return
  1873. }
  1874. edbInfo, err := models.GetEdbInfoById(req.EdbInfoId)
  1875. if err != nil {
  1876. if err.Error() == utils.ErrNoRow() {
  1877. br.Msg = "指标已被删除,请刷新页面"
  1878. br.ErrMsg = "指标已被删除,请刷新页面:Err:" + err.Error()
  1879. return
  1880. }
  1881. br.Msg = "获取指标信息失败"
  1882. br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
  1883. return
  1884. }
  1885. baseEdbInfoModel := models.CustomAnalysis{}
  1886. err, errMsg := baseEdbInfoModel.ResetData(edbInfo, req.DateList, req.DataList)
  1887. if err != nil {
  1888. br.Msg = "更新数据失败"
  1889. if errMsg != `` {
  1890. br.Msg = errMsg
  1891. }
  1892. br.ErrMsg = "更新数据失败 Err:" + err.Error()
  1893. return
  1894. }
  1895. if edbInfo == nil {
  1896. br.Msg = "更新数据失败"
  1897. br.ErrMsg = "更新数据失败,指标ID错误:"
  1898. return
  1899. }
  1900. // 更新指标最大最小值
  1901. err, errMsg = models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  1902. if err != nil {
  1903. br.Msg = errMsg
  1904. br.ErrMsg = err.Error()
  1905. return
  1906. }
  1907. resp := models.AddEdbInfoResp{
  1908. EdbInfoId: edbInfo.EdbInfoId,
  1909. UniqueCode: edbInfo.UniqueCode,
  1910. }
  1911. br.Ret = 200
  1912. br.Success = true
  1913. br.Msg = "处理成功"
  1914. br.Data = resp
  1915. br.IsAddLog = true
  1916. }
  1917. // Calculate
  1918. // @Title 基础数据计算
  1919. // @Description 拟合残差计算相关性接口
  1920. // @Param request body models.EdbInfoCalculateBatchSaveReq true "type json string"
  1921. // @Success Ret=200 返回指标id
  1922. // @router /base [post]
  1923. func (this *CalculateController) Calculate() {
  1924. br := new(models.BaseResponse).Init()
  1925. defer func() {
  1926. this.Data["json"] = br
  1927. this.ServeJSON()
  1928. }()
  1929. var req models.BaseCalculateBySearchData
  1930. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1931. if err != nil {
  1932. br.Msg = "参数解析异常!"
  1933. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1934. return
  1935. }
  1936. dateDataMap := make(map[time.Time]float64)
  1937. var errMsg string
  1938. dataList, err := models.EdbInfoSearchDataToData(req.DataList)
  1939. if err != nil {
  1940. br.Msg = "计算失败,数据异常!"
  1941. br.ErrMsg = "计算失败,数据异常,Err:" + err.Error()
  1942. return
  1943. }
  1944. baseCalculate := models.BaseCalculate{
  1945. DataList: dataList,
  1946. Frequency: req.Frequency,
  1947. Formula: req.Formula,
  1948. Calendar: req.Calendar,
  1949. MoveType: req.MoveType,
  1950. MoveFrequency: req.MoveFrequency,
  1951. FromFrequency: req.FromFrequency,
  1952. Source: req.Source,
  1953. }
  1954. //1:累计值转月;2:累计值转季;3:同比值;4:同差值;5:N数值移动平均数计算;6:环比值;7:环差值;8:升频;9:降频;10:时间移位;11:超季节性;12:年化;13:累计值;14:累计值年初至今;15:指数修匀;16:日均值
  1955. switch baseCalculate.Source {
  1956. case 1:
  1957. dateDataMap, err, errMsg = baseCalculate.Ljzzy()
  1958. case 2:
  1959. dateDataMap, err, errMsg = baseCalculate.Ljzzj()
  1960. case 3:
  1961. dateDataMap, err, errMsg = baseCalculate.Tbz()
  1962. case 4:
  1963. dateDataMap, err, errMsg = baseCalculate.Tcz()
  1964. case 5:
  1965. dateDataMap, err, errMsg = baseCalculate.Nszydpjjs()
  1966. case 6:
  1967. dateDataMap, err, errMsg = baseCalculate.Hbz()
  1968. case 7:
  1969. dateDataMap, err, errMsg = baseCalculate.Hcz()
  1970. case 8:
  1971. dateDataMap, err, errMsg = baseCalculate.UpFrequency()
  1972. case 9:
  1973. dateDataMap, err, errMsg = baseCalculate.DownFrequency()
  1974. case 10:
  1975. dateDataMap, err, errMsg = baseCalculate.TimeShift()
  1976. case 11:
  1977. dateDataMap, err, errMsg = baseCalculate.Cjjx()
  1978. case 12:
  1979. dateDataMap, err, errMsg = baseCalculate.Annualized()
  1980. case 13:
  1981. dateDataMap, err, errMsg = baseCalculate.Ljz()
  1982. case 14:
  1983. dateDataMap, err, errMsg = baseCalculate.LjzNczj()
  1984. case 15:
  1985. dateDataMap, err, errMsg = baseCalculate.ExponentialSmoothing()
  1986. case 16:
  1987. dateDataMap, err, errMsg = baseCalculate.Rjz()
  1988. default:
  1989. errMsg = "错误的计算类型"
  1990. err = errors.New(errMsg + ":" + strconv.Itoa(baseCalculate.Source))
  1991. }
  1992. if err != nil {
  1993. br.Msg = "计算失败"
  1994. if errMsg != `` {
  1995. br.Msg = errMsg
  1996. }
  1997. br.ErrMsg = err.Error()
  1998. return
  1999. }
  2000. dateStrDataMap, dateList := models.GetDateDataAndDateList(dateDataMap)
  2001. br.Ret = 200
  2002. br.Success = true
  2003. br.Msg = "计算成功"
  2004. br.Data = models.BaseCalculateResp{
  2005. DataMap: dateStrDataMap,
  2006. DateList: dateList,
  2007. }
  2008. br.IsAddLog = true
  2009. }
  2010. // BatchSaveMulti
  2011. // @Title 批量计算 累计值转月-同比值-同差等计算新增
  2012. // @Description 批量计算 累计值转月-同比值-同差等计算新增接口
  2013. // @Param request body models.EdbInfoCalculateBatchSaveReq true "type json string"
  2014. // @Success Ret=200 返回指标id
  2015. // @router /batch/save/multi [post]
  2016. func (this *CalculateController) BatchSaveMulti() {
  2017. br := new(models.BaseResponse).Init()
  2018. defer func() {
  2019. if br.ErrMsg == "" {
  2020. br.IsSendEmail = false
  2021. }
  2022. this.Data["json"] = br
  2023. this.ServeJSON()
  2024. }()
  2025. var req models.CalculateBatchSaveReq
  2026. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  2027. if err != nil {
  2028. br.Msg = "参数解析异常!"
  2029. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2030. return
  2031. }
  2032. resp := models.BatchEdbInfoCalculateBatchSaveResp{
  2033. Fail: make([]models.BatchEdbInfoCalculateBatchSaveFailResp, 0),
  2034. Success: make([]models.BatchEdbInfoCalculateBatchSaveSuccessResp, 0),
  2035. }
  2036. redisKeyList := make([]string, 0) //需要清理的缓存key列表
  2037. defer func() {
  2038. for _, redisKey := range redisKeyList {
  2039. utils.Rc.Delete(redisKey)
  2040. }
  2041. }()
  2042. var wg sync.WaitGroup
  2043. // 校验参数
  2044. for _, item := range req.EdbList {
  2045. calculateId := item.CalculateId
  2046. //加入缓存机制,避免创建同一个名称的指标 start
  2047. redisKey := fmt.Sprint("edb_info:calculate:batch:save:", req.AdminId, ":", req.Source, ":", calculateId)
  2048. isExist := utils.Rc.IsExist(redisKey)
  2049. if isExist {
  2050. resp.Fail = append(resp.Fail, models.BatchEdbInfoCalculateBatchSaveFailResp{
  2051. CalculateId: calculateId,
  2052. Msg: "指标正在处理,请勿重复提交",
  2053. })
  2054. continue
  2055. } else {
  2056. //设置3分钟缓存
  2057. utils.Rc.SetNX(redisKey, 1, time.Second*300)
  2058. redisKeyList = append(redisKeyList, redisKey)
  2059. }
  2060. wg.Add(1)
  2061. reqItem := models.EdbInfoCalculateBatchSaveReq{
  2062. AdminId: req.AdminId,
  2063. AdminName: req.AdminName,
  2064. EdbInfoId: item.EdbInfoId,
  2065. EdbName: item.EdbName,
  2066. Frequency: item.Frequency,
  2067. Unit: item.Unit,
  2068. ClassifyId: item.ClassifyId,
  2069. Formula: req.Formula, //N数值移动平均计算、环比值、环差值
  2070. FromEdbInfoId: item.FromEdbInfoId,
  2071. CalculateFormula: req.CalculateFormula,
  2072. Source: req.Source,
  2073. MoveType: req.MoveType,
  2074. MoveFrequency: req.MoveFrequency,
  2075. EdbInfoIdArr: req.EdbInfoIdArr,
  2076. Calendar: req.Calendar,
  2077. EmptyType: req.EmptyType,
  2078. MaxEmptyType: req.MaxEmptyType,
  2079. Extra: req.Extra,
  2080. }
  2081. var edbInfo *models.EdbInfo
  2082. uniqueCode := ""
  2083. errMsg := ""
  2084. go func() {
  2085. defer func() {
  2086. // todo 批量报错处理
  2087. if err != nil {
  2088. // 整理报错的指标
  2089. utils.FileLog.Info(fmt.Sprintf("批量指标计算,保存失败 Err:%v, ErrMsg:%s", err, errMsg))
  2090. resp.Fail = append(resp.Fail, models.BatchEdbInfoCalculateBatchSaveFailResp{
  2091. CalculateId: calculateId,
  2092. Msg: err.Error(),
  2093. ErrMsg: errMsg,
  2094. })
  2095. } else {
  2096. // 整理成功的指标
  2097. //todo 整理执行成功指标信息
  2098. resp.Success = append(resp.Success, models.BatchEdbInfoCalculateBatchSaveSuccessResp{
  2099. ClassifyId: reqItem.ClassifyId,
  2100. CalculateId: calculateId,
  2101. EdbInfoId: edbInfo.EdbInfoId,
  2102. UniqueCode: uniqueCode,
  2103. })
  2104. }
  2105. wg.Done()
  2106. }()
  2107. edbInfo, uniqueCode, err, errMsg = services.EdbCalculateBatchSave(reqItem, this.Lang)
  2108. return
  2109. }()
  2110. }
  2111. wg.Wait()
  2112. br.Ret = 200
  2113. br.Success = true
  2114. br.Msg = "保存成功"
  2115. br.Data = resp
  2116. br.IsAddLog = true
  2117. }
  2118. // BatchEditMulti
  2119. // @Title 批量计算 累计值转月-同比值-同差等计算编辑
  2120. // @Description 批量计算 累计值转月-同比值-同差等计算编辑接口
  2121. // @Param request body models.EdbInfoCalculateBatchSaveReq true "type json string"
  2122. // @Success Ret=200 返回指标id
  2123. // @router /batch/edit/multi [post]
  2124. func (this *CalculateController) BatchEditMulti() {
  2125. br := new(models.BaseResponse).Init()
  2126. defer func() {
  2127. if br.ErrMsg == "" {
  2128. br.IsSendEmail = false
  2129. }
  2130. this.Data["json"] = br
  2131. this.ServeJSON()
  2132. }()
  2133. var req models.CalculateBatchSaveReq
  2134. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  2135. if err != nil {
  2136. br.Msg = "参数解析异常!"
  2137. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2138. return
  2139. }
  2140. resp := models.BatchEdbInfoCalculateBatchSaveResp{
  2141. Fail: make([]models.BatchEdbInfoCalculateBatchSaveFailResp, 0),
  2142. Success: make([]models.BatchEdbInfoCalculateBatchSaveSuccessResp, 0),
  2143. }
  2144. redisKeyList := make([]string, 0) //需要清理的缓存key列表
  2145. defer func() {
  2146. for _, redisKey := range redisKeyList {
  2147. utils.Rc.Delete(redisKey)
  2148. }
  2149. }()
  2150. var wg sync.WaitGroup
  2151. // 校验参数
  2152. for _, item := range req.EdbList {
  2153. calculateId := item.CalculateId
  2154. if item.EdbInfoId <= 0 {
  2155. resp.Fail = append(resp.Fail, models.BatchEdbInfoCalculateBatchSaveFailResp{
  2156. CalculateId: calculateId,
  2157. Msg: "请选择要编辑的指标",
  2158. })
  2159. continue
  2160. }
  2161. //加入缓存机制,避免创建同一个名称的指标 start
  2162. redisKey := fmt.Sprint("edb_info:calculate:batch:edit:", req.AdminId, ":", req.Source, ":", item.EdbInfoId)
  2163. isExist := utils.Rc.IsExist(redisKey)
  2164. if isExist {
  2165. resp.Fail = append(resp.Fail, models.BatchEdbInfoCalculateBatchSaveFailResp{
  2166. CalculateId: calculateId,
  2167. Msg: "指标正在处理,请勿重复提交",
  2168. })
  2169. continue
  2170. } else {
  2171. //设置3分钟缓存
  2172. utils.Rc.SetNX(redisKey, 1, time.Second*300)
  2173. redisKeyList = append(redisKeyList, redisKey)
  2174. }
  2175. wg.Add(1)
  2176. reqItem := models.EdbInfoCalculateBatchEditReq{
  2177. EdbInfoId: item.EdbInfoId,
  2178. EdbName: item.EdbName,
  2179. Frequency: item.Frequency,
  2180. Unit: item.Unit,
  2181. ClassifyId: item.ClassifyId,
  2182. AdminId: req.AdminId,
  2183. AdminName: req.AdminName,
  2184. Formula: req.Formula, //N数值移动平均计算、环比值、环差值
  2185. FromEdbInfoId: item.FromEdbInfoId,
  2186. CalculateFormula: req.CalculateFormula,
  2187. Source: req.Source,
  2188. MoveType: req.MoveType,
  2189. MoveFrequency: req.MoveFrequency,
  2190. EdbInfoIdArr: req.EdbInfoIdArr,
  2191. Calendar: req.Calendar,
  2192. EmptyType: req.EmptyType,
  2193. MaxEmptyType: req.MaxEmptyType,
  2194. Extra: req.Extra,
  2195. }
  2196. var errMsg string
  2197. var edbInfo *models.EdbInfo
  2198. uniqueCode := ""
  2199. go func() {
  2200. defer func() {
  2201. // todo 批量报错处理
  2202. if err != nil {
  2203. // 整理报错的指标
  2204. utils.FileLog.Info(fmt.Sprintf("批量指标计算,保存失败 Err:%v, ErrMsg:%s", err, errMsg))
  2205. resp.Fail = append(resp.Fail, models.BatchEdbInfoCalculateBatchSaveFailResp{
  2206. CalculateId: calculateId,
  2207. Msg: err.Error(),
  2208. ErrMsg: errMsg,
  2209. })
  2210. } else {
  2211. // 整理成功的指标
  2212. //todo 整理执行成功指标信息
  2213. resp.Success = append(resp.Success, models.BatchEdbInfoCalculateBatchSaveSuccessResp{
  2214. ClassifyId: reqItem.ClassifyId,
  2215. CalculateId: calculateId,
  2216. EdbInfoId: edbInfo.EdbInfoId,
  2217. UniqueCode: uniqueCode,
  2218. })
  2219. }
  2220. wg.Done()
  2221. }()
  2222. edbInfo, uniqueCode, err, errMsg = services.EdbCalculateBatchEdit(reqItem)
  2223. return
  2224. }()
  2225. }
  2226. wg.Wait()
  2227. if err != nil {
  2228. br.Msg = "批量指标计算,保存失败"
  2229. br.ErrMsg = "批量指标计算,保存失败,Err:" + err.Error()
  2230. return
  2231. }
  2232. br.Ret = 200
  2233. br.Success = true
  2234. br.Msg = "保存成功"
  2235. br.Data = resp
  2236. br.IsAddLog = true
  2237. }
  2238. // StepCalculate
  2239. // @Title 多步骤计算
  2240. // @Description 多步骤计算
  2241. // @Param request body models.StepCalculateBySearchData true "type json string"
  2242. // @Success Ret=200 返回指标id
  2243. // @router /base/step_calculate [post]
  2244. func (this *CalculateController) StepCalculate() {
  2245. br := new(models.BaseResponse).Init()
  2246. defer func() {
  2247. if br.ErrMsg == "" {
  2248. br.IsSendEmail = false
  2249. }
  2250. this.Data["json"] = br
  2251. this.ServeJSON()
  2252. }()
  2253. var req models.StepCalculateBySearchData
  2254. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  2255. br.Msg = "参数解析异常"
  2256. br.ErrMsg = fmt.Sprintf("参数解析失败, Err: %v", e)
  2257. return
  2258. }
  2259. //sort.Slice(req.Calculates, func(i, j int) bool {
  2260. // return req.Calculates[i].Sort < req.Calculates[j].Sort
  2261. //})
  2262. //
  2263. //var errMsg string
  2264. //originData, e := models.EdbInfoSearchDataToData(req.DataList)
  2265. //if e != nil {
  2266. // br.Msg = "基础数据异常"
  2267. // br.ErrMsg = fmt.Sprintf("计算失败, 基础数据异常, Err: %v", e)
  2268. // return
  2269. //}
  2270. //
  2271. //calculateData := originData
  2272. //dateDataMap := make(map[time.Time]float64)
  2273. //for _, v := range req.Calculates {
  2274. // baseCalculate := models.BaseCalculate{
  2275. // DataList: calculateData,
  2276. // Frequency: v.Frequency,
  2277. // Formula: v.Formula,
  2278. // Calendar: v.Calendar,
  2279. // MoveType: v.MoveType,
  2280. // MoveFrequency: v.MoveFrequency,
  2281. // FromFrequency: v.FromFrequency,
  2282. // Source: v.Source,
  2283. // }
  2284. //
  2285. // // 计算方式
  2286. // switch baseCalculate.Source {
  2287. // case utils.EdbBaseCalculateLjzzy:
  2288. // dateDataMap, e, errMsg = baseCalculate.Ljzzy()
  2289. // case utils.EdbBaseCalculateLjzzj:
  2290. // dateDataMap, e, errMsg = baseCalculate.Ljzzj()
  2291. // case utils.EdbBaseCalculateTbz:
  2292. // dateDataMap, e, errMsg = baseCalculate.Tbz()
  2293. // case utils.EdbBaseCalculateTcz:
  2294. // dateDataMap, e, errMsg = baseCalculate.Tcz()
  2295. // case utils.EdbBaseCalculateNszydpjjs:
  2296. // dateDataMap, e, errMsg = baseCalculate.Nszydpjjs()
  2297. // case utils.EdbBaseCalculateHbz:
  2298. // dateDataMap, e, errMsg = baseCalculate.Hbz()
  2299. // case utils.EdbBaseCalculateHcz:
  2300. // dateDataMap, e, errMsg = baseCalculate.Hcz()
  2301. // case utils.EdbBaseCalculateUpFrequency:
  2302. // dateDataMap, e, errMsg = baseCalculate.UpFrequency()
  2303. // case utils.EdbBaseCalculateDownFrequency:
  2304. // dateDataMap, e, errMsg = baseCalculate.DownFrequency()
  2305. // case utils.EdbBaseCalculateTimeShift:
  2306. // dateDataMap, e, errMsg = baseCalculate.TimeShift()
  2307. // case utils.EdbBaseCalculateCjjx:
  2308. // dateDataMap, e, errMsg = baseCalculate.Cjjx()
  2309. // case utils.EdbBaseCalculateAnnualized:
  2310. // dateDataMap, e, errMsg = baseCalculate.Annualized()
  2311. // case utils.EdbBaseCalculateLjz:
  2312. // dateDataMap, e, errMsg = baseCalculate.Ljz()
  2313. // case utils.EdbBaseCalculateLjzNczj:
  2314. // dateDataMap, e, errMsg = baseCalculate.LjzNczj()
  2315. // case utils.EdbBaseCalculateExponentialSmoothing:
  2316. // dateDataMap, e, errMsg = baseCalculate.ExponentialSmoothing()
  2317. // case utils.EdbBaseCalculateRjz:
  2318. // dateDataMap, e, errMsg = baseCalculate.Rjz()
  2319. // default:
  2320. // errMsg = "计算方式无效"
  2321. // e = fmt.Errorf("%s:%d", errMsg, baseCalculate.Source)
  2322. // }
  2323. // if e != nil {
  2324. // br.Msg = "计算失败"
  2325. // if errMsg != "" {
  2326. // br.Msg = errMsg
  2327. // }
  2328. // br.ErrMsg = e.Error()
  2329. // return
  2330. // }
  2331. //
  2332. // calculateData = models.TransDateData2EdbData(dateDataMap)
  2333. //}
  2334. resultData, dates, errMsg, e := services.StepCalculate(req.DataList, req.Calculates)
  2335. if e != nil {
  2336. br.Msg = "计算失败"
  2337. if errMsg != "" {
  2338. br.Msg = errMsg
  2339. }
  2340. br.ErrMsg = fmt.Sprintf("分步骤计算失败, Err: %v", e)
  2341. return
  2342. }
  2343. br.Ret = 200
  2344. br.Success = true
  2345. br.Msg = "计算成功"
  2346. br.Data = models.BaseCalculateResp{
  2347. DataMap: resultData,
  2348. DateList: dates,
  2349. }
  2350. br.IsAddLog = true
  2351. }