base_from_calculate.go 71 KB

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