base_from_calculate.go 75 KB

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