base_from_calculate.go 77 KB

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