base_from_calculate.go 71 KB

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