base_from_calculate.go 71 KB

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