base_from_calculate.go 69 KB

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