base_from_calculate.go 61 KB

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