base_from_calculate.go 76 KB

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