base_from_calculate.go 74 KB

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