base_from_calculate.go 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "hongze/hongze_edb_lib/models"
  6. "hongze/hongze_edb_lib/services"
  7. "hongze/hongze_edb_lib/utils"
  8. "strconv"
  9. "strings"
  10. "time"
  11. )
  12. // CalculateController 计算指标
  13. type CalculateController struct {
  14. BaseAuthController
  15. }
  16. // Add
  17. // @Title 新增计算指标接口
  18. // @Description 新增计算指标接口
  19. // @Success 200 {object} models.EdbInfoCalculateSaveReq
  20. // @router /add [post]
  21. func (this *CalculateController) Add() {
  22. br := new(models.BaseResponse).Init()
  23. defer func() {
  24. this.Data["json"] = br
  25. this.ServeJSON()
  26. }()
  27. var req models.EdbInfoCalculateSaveReq
  28. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  29. if err != nil {
  30. br.Msg = "参数解析异常!"
  31. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  32. return
  33. }
  34. req.EdbName = strings.Trim(req.EdbName, " ")
  35. if req.EdbName == "" {
  36. br.Msg = "指标名称不能为空"
  37. return
  38. }
  39. if req.Frequency == "" {
  40. br.Msg = "频率不能为空"
  41. return
  42. }
  43. if req.Unit == "" {
  44. br.Msg = "单位不能为空"
  45. return
  46. }
  47. if req.ClassifyId <= 0 {
  48. br.Msg = "请选择分类"
  49. return
  50. }
  51. if len(req.EdbInfoIdArr) <= 0 {
  52. br.Msg = "请选择指标"
  53. return
  54. }
  55. if req.CalculateFormula == "" {
  56. br.Msg = "请填写指标"
  57. return
  58. }
  59. calculateFormula := req.CalculateFormula
  60. calculateFormula = strings.Replace(calculateFormula, "(", "(", -1)
  61. calculateFormula = strings.Replace(calculateFormula, ")", ")", -1)
  62. calculateFormula = strings.Replace(calculateFormula, ",", ",", -1)
  63. calculateFormula = strings.Replace(calculateFormula, "。", ".", -1)
  64. calculateFormula = strings.Replace(calculateFormula, "%", "*0.01", -1)
  65. req.CalculateFormula = calculateFormula
  66. //加入缓存机制,避免创建同一个名称的指标 start
  67. redisKey := fmt.Sprint("edb_lib:edb_info:calculate:add:", utils.DATA_SOURCE_CALCULATE, ":", req.EdbName)
  68. isExist := utils.Rc.IsExist(redisKey)
  69. if isExist {
  70. br.Msg = "指标正在处理,请勿重复提交"
  71. return
  72. } else {
  73. //设置3分钟缓存
  74. utils.Rc.SetNX(redisKey, 1, time.Second*300)
  75. defer func() {
  76. utils.Rc.Delete(redisKey)
  77. }()
  78. }
  79. //判断是否重复指标
  80. edbInfoMap := make(map[int]string)
  81. //移除研究员选择指标中的未使用的指标
  82. {
  83. //转大写的计算公式
  84. upperCalculateFormulaStr := strings.ToUpper(req.CalculateFormula)
  85. //用到的指标
  86. newEdbInfoIdArr := make([]models.EdbInfoFromTag, 0)
  87. for _, tmpEdbInfo := range req.EdbInfoIdArr {
  88. _, ok := edbInfoMap[tmpEdbInfo.EdbInfoId]
  89. if ok {
  90. br.Msg = "选择指标失败,请勿选择重复指标!"
  91. return
  92. }
  93. edbInfoMap[tmpEdbInfo.EdbInfoId] = tmpEdbInfo.FromTag
  94. upperFromTag := strings.ToUpper(tmpEdbInfo.FromTag)
  95. if strings.Contains(upperCalculateFormulaStr, upperFromTag) {
  96. newEdbInfoIdArr = append(newEdbInfoIdArr, tmpEdbInfo)
  97. }
  98. }
  99. req.EdbInfoIdArr = newEdbInfoIdArr
  100. }
  101. var condition string
  102. var pars []interface{}
  103. condition += " AND edb_name=? "
  104. pars = append(pars, req.EdbName)
  105. count, err := models.GetEdbInfoCountByCondition(condition, pars)
  106. if err != nil {
  107. br.Msg = "判断指标名称是否存在失败"
  108. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  109. return
  110. }
  111. if count > 0 {
  112. br.Msg = "指标名称已存在,请重新填写"
  113. br.ErrMsg = "指标名称已存在,请重新填写"
  114. br.IsSendEmail = false
  115. return
  116. }
  117. //检验公式
  118. var formulaStr string
  119. var edbInfoIdBytes []string
  120. for _, v := range req.EdbInfoIdArr {
  121. formulaStr += v.FromTag + ","
  122. edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
  123. }
  124. formulaMap := services.CheckFormula(req.CalculateFormula)
  125. for _, v := range formulaMap {
  126. if !strings.Contains(formulaStr, v) {
  127. br.Msg = "公式错误,请重新填写"
  128. return
  129. }
  130. }
  131. //关联的指标信息
  132. edbInfoList := make([]*models.EdbInfo, 0)
  133. //关联的指标数据表
  134. calculateMappingList := make([]*models.EdbInfoCalculateMapping, 0)
  135. for k, v := range req.EdbInfoIdArr {
  136. fromEdbInfo, err := models.GetEdbInfoById(v.EdbInfoId)
  137. if err != nil {
  138. if err.Error() == utils.ErrNoRow() {
  139. br.Msg = "生成计算指标失败"
  140. br.Msg = "指标 " + strconv.Itoa(v.EdbInfoId) + " 不存在"
  141. return
  142. }
  143. br.Msg = "生成计算指标失败"
  144. br.Msg = "获取指标失败:Err:" + err.Error()
  145. return
  146. }
  147. edbInfoList = append(edbInfoList, fromEdbInfo)
  148. //关联关系表
  149. {
  150. calculateMappingItem := &models.EdbInfoCalculateMapping{
  151. EdbInfoCalculateMappingId: 0,
  152. EdbInfoId: 0,
  153. Source: utils.DATA_SOURCE_CALCULATE,
  154. SourceName: "指标运算",
  155. EdbCode: "",
  156. FromEdbInfoId: fromEdbInfo.EdbInfoId,
  157. FromEdbCode: fromEdbInfo.EdbCode,
  158. FromEdbName: fromEdbInfo.EdbName,
  159. FromSource: fromEdbInfo.Source,
  160. FromSourceName: fromEdbInfo.SourceName,
  161. FromTag: v.FromTag,
  162. Sort: k + 1,
  163. CreateTime: time.Now(),
  164. ModifyTime: time.Now(),
  165. }
  166. //calculateMappingItem.EdbCode = edbCode
  167. //calculateMappingItem.EdbInfoId = int(edbInfoId)
  168. calculateMappingList = append(calculateMappingList, calculateMappingItem)
  169. }
  170. }
  171. ok, _ := models.CheckFormula2(edbInfoList, formulaMap, calculateFormula, edbInfoIdBytes)
  172. if !ok {
  173. br.Msg = "生成计算指标失败,请使用正确的计算公式"
  174. return
  175. }
  176. randStr := utils.GetRandDigit(4)
  177. edbCode := `C` + time.Now().Format("060102") + randStr
  178. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  179. uniqueCode := utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  180. // 新增计算指标
  181. edbInfo, err := models.AddCalculateInfo(req, calculateMappingList, edbInfoList, edbCode, uniqueCode, edbInfoIdBytes)
  182. if err != nil {
  183. br.Msg = "生成计算指标失败"
  184. br.Msg = "生成计算指标失败,AddEdbInfo Err:" + err.Error()
  185. return
  186. }
  187. //处理同名指标
  188. {
  189. edbNameList, err := models.GetEdbInfoByName(req.EdbName)
  190. if err != nil {
  191. br.Msg = "保存失败"
  192. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  193. return
  194. }
  195. if len(edbNameList) >= 2 {
  196. for _, v := range edbNameList {
  197. edbName := v.EdbName + "(" + v.SourceName + ")"
  198. err = models.ModifyEdbInfoNameSource(edbName, v.EdbInfoId)
  199. if err != nil {
  200. br.Msg = "保存失败"
  201. br.ErrMsg = "修改指标名称失败,Err:" + err.Error()
  202. return
  203. }
  204. }
  205. }
  206. }
  207. // 更新指标最大最小值
  208. err, errMsg := models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  209. if err != nil {
  210. br.Msg = errMsg
  211. br.ErrMsg = err.Error()
  212. return
  213. }
  214. resp := models.AddEdbInfoResp{
  215. EdbInfoId: edbInfo.EdbInfoId,
  216. UniqueCode: uniqueCode,
  217. }
  218. br.Ret = 200
  219. br.Success = true
  220. br.Msg = "保存成功"
  221. br.Data = resp
  222. br.IsAddLog = true
  223. }
  224. // Edit
  225. // @Title 编辑计算指标接口
  226. // @Description 编辑计算指标接口
  227. // @Success 200 {object} models.EdbInfoCalculateSaveReq
  228. // @router /edit [post]
  229. func (this *CalculateController) Edit() {
  230. br := new(models.BaseResponse).Init()
  231. defer func() {
  232. this.Data["json"] = br
  233. this.ServeJSON()
  234. }()
  235. var req models.EdbInfoCalculateSaveReq
  236. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  237. if err != nil {
  238. br.Msg = "参数解析异常!"
  239. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  240. return
  241. }
  242. if req.EdbInfoId <= 0 {
  243. br.Msg = "参数错误"
  244. br.ErrMsg = "指标ID:" + strconv.Itoa(req.EdbInfoId)
  245. return
  246. }
  247. req.EdbName = strings.Trim(req.EdbName, " ")
  248. if req.EdbName == "" {
  249. br.Msg = "指标名称不能为空"
  250. return
  251. }
  252. if req.Frequency == "" {
  253. br.Msg = "频率不能为空"
  254. return
  255. }
  256. if req.Unit == "" {
  257. br.Msg = "单位不能为空"
  258. return
  259. }
  260. if req.ClassifyId <= 0 {
  261. br.Msg = "请选择分类"
  262. return
  263. }
  264. if len(req.EdbInfoIdArr) <= 0 {
  265. br.Msg = "请选择指标"
  266. return
  267. }
  268. if req.CalculateFormula == "" {
  269. br.Msg = "请填写指标"
  270. return
  271. }
  272. calculateFormula := req.CalculateFormula
  273. calculateFormula = strings.Replace(calculateFormula, "(", "(", -1)
  274. calculateFormula = strings.Replace(calculateFormula, ")", ")", -1)
  275. calculateFormula = strings.Replace(calculateFormula, ",", ",", -1)
  276. calculateFormula = strings.Replace(calculateFormula, "。", ".", -1)
  277. calculateFormula = strings.Replace(calculateFormula, "%", "*0.01", -1)
  278. req.CalculateFormula = calculateFormula
  279. fmt.Println("calculateFormula:" + calculateFormula)
  280. //判断是否重复指标
  281. edbInfoMap := make(map[int]string)
  282. //移除研究员选择指标中的未使用的指标
  283. {
  284. //转大写的计算公式
  285. upperCalculateFormulaStr := strings.ToUpper(req.CalculateFormula)
  286. //用到的指标
  287. newEdbInfoIdArr := make([]models.EdbInfoFromTag, 0)
  288. for _, tmpEdbInfo := range req.EdbInfoIdArr {
  289. _, ok := edbInfoMap[tmpEdbInfo.EdbInfoId]
  290. if ok {
  291. br.Msg = "选择指标失败,请勿选择重复指标!"
  292. return
  293. }
  294. edbInfoMap[tmpEdbInfo.EdbInfoId] = tmpEdbInfo.FromTag
  295. upperFromTag := strings.ToUpper(tmpEdbInfo.FromTag)
  296. if strings.Contains(upperCalculateFormulaStr, upperFromTag) {
  297. newEdbInfoIdArr = append(newEdbInfoIdArr, tmpEdbInfo)
  298. }
  299. }
  300. req.EdbInfoIdArr = newEdbInfoIdArr
  301. }
  302. var condition string
  303. var pars []interface{}
  304. condition += " AND edb_name=? "
  305. pars = append(pars, req.EdbName)
  306. condition += " AND edb_info_id<>? "
  307. pars = append(pars, req.EdbInfoId)
  308. count, err := models.GetEdbInfoCountByCondition(condition, pars)
  309. if err != nil {
  310. br.Msg = "判断指标名称是否存在失败"
  311. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  312. return
  313. }
  314. if count > 0 {
  315. br.Msg = "指标名称已存在,请重新填写"
  316. br.ErrMsg = "指标名称已存在,请重新填写"
  317. br.IsSendEmail = false
  318. return
  319. }
  320. //判断公式,指标是否有改动
  321. edbInfoDetail, err := models.GetEdbInfoById(req.EdbInfoId)
  322. if err != nil {
  323. br.Msg = "修改失败"
  324. br.Msg = "获取指标信息失败,GetEdbInfoById Err:" + err.Error()
  325. return
  326. }
  327. if edbInfoDetail == nil {
  328. br.Msg = "修改失败"
  329. br.Msg = "指标信息不存在,EdbInfoId:" + strconv.Itoa(req.EdbInfoId)
  330. return
  331. }
  332. var needCalculate bool
  333. if edbInfoDetail.CalculateFormula != req.CalculateFormula {
  334. needCalculate = true
  335. }
  336. //检验公式
  337. var formulaStr string
  338. var edbInfoIdBytes []string
  339. var edbInfoIdArr []string
  340. for _, v := range req.EdbInfoIdArr {
  341. formulaStr += v.FromTag + ","
  342. edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
  343. edbInfoIdArr = append(edbInfoIdArr, strconv.Itoa(v.EdbInfoId))
  344. if v.EdbInfoId == req.EdbInfoId {
  345. br.Msg = "计算指标中包含此指标,不能修改"
  346. return
  347. }
  348. }
  349. if needCalculate == false {
  350. edbInfoIdStr := strings.Join(edbInfoIdArr, ",")
  351. fromEdbInfoId, err := models.GetCalculateEdbInfo(req.EdbInfoId)
  352. if err != nil {
  353. br.Msg = "修改失败"
  354. br.Msg = "获取数据失败,GetCalculateEdbInfo:" + err.Error()
  355. return
  356. }
  357. if edbInfoIdStr != fromEdbInfoId {
  358. needCalculate = true
  359. }
  360. }
  361. formulaMap := services.CheckFormula(req.CalculateFormula)
  362. for _, v := range formulaMap {
  363. if !strings.Contains(formulaStr, v) {
  364. br.Msg = "公式错误,请重新填写"
  365. return
  366. }
  367. }
  368. //重新计算
  369. err, errMsg := models.EditCalculateInfo(edbInfoDetail, req, formulaMap, edbInfoIdBytes, needCalculate)
  370. if err != nil {
  371. br.Msg = "生成计算指标失败"
  372. if errMsg != `` {
  373. br.Msg = errMsg
  374. }
  375. br.Msg = "生成计算指标失败,AddEdbInfo Err:" + err.Error()
  376. return
  377. }
  378. // 更新指标最大最小值
  379. err, errMsg = models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfoDetail)
  380. if err != nil {
  381. br.Msg = errMsg
  382. br.ErrMsg = err.Error()
  383. return
  384. }
  385. resp := models.AddEdbInfoResp{
  386. EdbInfoId: edbInfoDetail.EdbInfoId,
  387. UniqueCode: edbInfoDetail.UniqueCode,
  388. }
  389. br.Ret = 200
  390. br.Success = true
  391. br.Msg = "保存成功"
  392. br.Data = resp
  393. br.IsAddLog = true
  394. }
  395. // BatchSave
  396. // @Title 累计值转月-同比值-同差等计算新增
  397. // @Description 累计值转月-同比值-同差等计算新增接口
  398. // @Param request body models.EdbInfoCalculateBatchSaveReq true "type json string"
  399. // @Success Ret=200 返回指标id
  400. // @router /batch/save [post]
  401. func (this *CalculateController) BatchSave() {
  402. br := new(models.BaseResponse).Init()
  403. defer func() {
  404. this.Data["json"] = br
  405. this.ServeJSON()
  406. }()
  407. var req models.EdbInfoCalculateBatchSaveReq
  408. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  409. if err != nil {
  410. br.Msg = "参数解析异常!"
  411. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  412. return
  413. }
  414. req.EdbName = strings.Trim(req.EdbName, " ")
  415. if req.EdbName == "" {
  416. br.Msg = "指标名称不能为空"
  417. return
  418. }
  419. if req.Frequency == "" {
  420. br.Msg = "频率不能为空"
  421. return
  422. }
  423. if req.Unit == "" {
  424. br.Msg = "单位不能为空"
  425. return
  426. }
  427. if req.ClassifyId <= 0 {
  428. br.Msg = "请选择分类"
  429. return
  430. }
  431. // 基础指标id
  432. fromEdbInfoId := req.FromEdbInfoId
  433. var formulaInt int
  434. var nhccDate models.NhccDate // 拟合残差的日期
  435. switch req.Source {
  436. 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:
  437. if req.Formula == "" {
  438. br.Msg = "请填写N值"
  439. return
  440. }
  441. formulaInt, _ = strconv.Atoi(req.Formula)
  442. if formulaInt <= 0 {
  443. br.Msg = "N值输入错误,请重新输入"
  444. return
  445. }
  446. case utils.DATA_SOURCE_CALCULATE_ZJPJ:
  447. //直接拼接指标
  448. //校验时间格式
  449. _, err = time.ParseInLocation(utils.FormatDate, req.Formula, time.Local)
  450. if err != nil {
  451. br.Msg = "拼接日期有误,请重新输入"
  452. return
  453. }
  454. case utils.DATA_SOURCE_CALCULATE_NHCC: //拟合残差指标
  455. //指标校验
  456. if len(req.EdbInfoIdArr) != 2 {
  457. br.Msg = "选择的指标异常,请重新选择"
  458. return
  459. }
  460. fromEdbInfoId = req.EdbInfoIdArr[0].EdbInfoId
  461. //校验时间格式
  462. //数据格式:2022-11-01,2022-11-10
  463. timeList := strings.Split(req.Formula, ",")
  464. if len(timeList) != 2 {
  465. br.Msg = "选择时间有误,请重新输入"
  466. return
  467. }
  468. startDate, err := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
  469. if err != nil {
  470. br.Msg = "开始日期有误,请重新输入"
  471. return
  472. }
  473. endDate, err := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
  474. if err != nil {
  475. br.Msg = "结束日期有误,请重新输入"
  476. return
  477. }
  478. if utils.GetTimeSubDay(startDate, endDate) < 2 {
  479. br.Msg = "日期间隔不得少于两天"
  480. return
  481. }
  482. nhccDate.StartDate = startDate
  483. nhccDate.EndDate = endDate
  484. }
  485. if fromEdbInfoId <= 0 {
  486. br.Msg = "请选择指标"
  487. return
  488. }
  489. //加入缓存机制,避免创建同一个名称的指标 start
  490. redisKey := fmt.Sprint("edb_lib:edb_info:calculate:batch:save:", req.Source, ":", req.EdbName)
  491. isExist := utils.Rc.IsExist(redisKey)
  492. if isExist {
  493. br.Msg = "指标正在处理,请勿重复提交"
  494. return
  495. } else {
  496. //设置3分钟缓存
  497. utils.Rc.SetNX(redisKey, 1, time.Second*300)
  498. defer func() {
  499. utils.Rc.Delete(redisKey)
  500. }()
  501. }
  502. //加入缓存机制,避免创建同一个名称的指标 end
  503. var condition string
  504. var pars []interface{}
  505. condition += " AND edb_name=? "
  506. pars = append(pars, req.EdbName)
  507. count, err := models.GetEdbInfoCountByCondition(condition, pars)
  508. if err != nil {
  509. br.Msg = "判断指标名称是否存在失败"
  510. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  511. return
  512. }
  513. if count > 0 {
  514. br.Msg = "指标名称已存在,请重新填写"
  515. br.ErrMsg = "指标名称已存在,请重新填写"
  516. br.IsSendEmail = false
  517. return
  518. }
  519. fromEdbInfo, err := models.GetEdbInfoById(fromEdbInfoId)
  520. if err != nil {
  521. br.Msg = "获取指标信息失败"
  522. br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
  523. return
  524. }
  525. //生成指标编码
  526. randStr := utils.GetRandDigit(4)
  527. edbCode := `C` + time.Now().Format("060102") + randStr
  528. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10)
  529. uniqueCode := utils.MD5(utils.DATA_PREFIX + "_" + timestamp)
  530. sysUserId := req.AdminId
  531. sysUserName := req.AdminName
  532. var sourName string
  533. var edbInfoId int
  534. var edbInfo *models.EdbInfo
  535. switch req.Source {
  536. case utils.DATA_SOURCE_CALCULATE_LJZZY:
  537. sourName = "累计值转月值"
  538. if fromEdbInfo.Frequency != "月度" {
  539. br.Msg = "请选择月度指标"
  540. return
  541. }
  542. edbInfo, err = models.AddCalculateLjzzy(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
  543. case utils.DATA_SOURCE_CALCULATE_TBZ:
  544. sourName = "同比值"
  545. edbInfo, err = models.AddCalculateTbz(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
  546. case utils.DATA_SOURCE_CALCULATE_TCZ:
  547. sourName = "同差值"
  548. edbInfo, err = models.AddCalculateTcz(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
  549. case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS:
  550. sourName = "N数值移动平均计算"
  551. edbInfo, err = models.AddCalculateNszydpjjs(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName, formulaInt)
  552. case utils.DATA_SOURCE_CALCULATE_HBZ:
  553. var condition string
  554. var pars []interface{}
  555. condition += " AND edb_info_id =? "
  556. pars = append(pars, fromEdbInfoId)
  557. condition += " AND value <=0 "
  558. checkCount, err := models.GetEdbDataCount(condition, pars, fromEdbInfo.Source)
  559. if err != nil && err.Error() != utils.ErrNoRow() {
  560. br.Msg = "判断环比值是否可计算失败"
  561. br.ErrMsg = "判断环比值是否可计算失败,Err:" + err.Error()
  562. return
  563. }
  564. if checkCount > 0 {
  565. br.Msg = "原始数据中存在0或负数,该指标不能进行环比运算"
  566. br.ErrMsg = "原始数据中出现0和负值时,提示该指标不能进行环比运算"
  567. return
  568. }
  569. sourName = "环比值"
  570. edbInfo, err = models.AddCalculateHbz(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName, formulaInt)
  571. case utils.DATA_SOURCE_CALCULATE_HCZ:
  572. sourName = "环差值"
  573. edbInfo, err = models.AddCalculateHcz(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName, formulaInt)
  574. case utils.DATA_SOURCE_CALCULATE_BP:
  575. //if fromEdbInfo.Frequency == "日度" {
  576. // br.Msg = "日度指标,无法进行变频操作"
  577. // br.ErrMsg = "日度指标,无法进行变频操作:edbcode:" + fromEdbInfo.EdbCode
  578. // return
  579. //}
  580. sourName = "变频"
  581. edbInfo, err = models.AddCalculateBp(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
  582. case utils.DATA_SOURCE_CALCULATE_TIME_SHIFT:
  583. sourName = "时间移位"
  584. edbInfo, err = models.AddCalculateTimeShift(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
  585. case utils.DATA_SOURCE_CALCULATE_ZJPJ:
  586. sourName = "直接拼接"
  587. if len(req.EdbInfoIdArr) != 1 {
  588. br.Msg = "请传入拼接日期之后的指标"
  589. br.ErrMsg = "请传入拼接日期之后的指标"
  590. return
  591. }
  592. secondEdbInfoReq := req.EdbInfoIdArr[0]
  593. secondEdbInfo, err := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  594. if err != nil {
  595. br.Msg = "获取拼接日期之后的指标信息失败"
  596. br.ErrMsg = "获取拼接日期之后的指标信息失败:Err:" + err.Error()
  597. return
  598. }
  599. if fromEdbInfo.EdbInfoId == secondEdbInfo.EdbInfoId {
  600. br.Msg = "两个指标不允许为同一个"
  601. br.ErrMsg = "两个指标不允许为同一个"
  602. return
  603. }
  604. edbInfo, err = models.AddCalculateZjpj(&req, fromEdbInfo, secondEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
  605. case utils.DATA_SOURCE_CALCULATE_LJZTBPJ:
  606. sourName = "累计值同比拼接"
  607. if fromEdbInfo.Frequency != "月度" {
  608. br.Msg = "待拼接指标只能筛选月度指标"
  609. br.ErrMsg = "待拼接指标只能筛选月度指标"
  610. return
  611. }
  612. if len(req.EdbInfoIdArr) != 1 {
  613. br.Msg = "请传入同比值指标"
  614. br.ErrMsg = "请传入同比值指标"
  615. return
  616. }
  617. secondEdbInfoReq := req.EdbInfoIdArr[0]
  618. tbzEdbInfo, err := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  619. if err != nil {
  620. br.Msg = "获取同比值指标信息失败"
  621. br.ErrMsg = "获取同比值指标信息失败:Err:" + err.Error()
  622. return
  623. }
  624. if tbzEdbInfo.Source != utils.DATA_SOURCE_CALCULATE_TBZ {
  625. br.Msg = "指标必须是传入同比值指标类型"
  626. br.ErrMsg = "指标必须是传入同比值指标类型"
  627. return
  628. }
  629. if tbzEdbInfo.Frequency != "月度" {
  630. br.Msg = "同比值指标只能筛选月度指标"
  631. br.ErrMsg = "同比值指标只能筛选月度指标"
  632. return
  633. }
  634. if fromEdbInfo.EdbInfoId == tbzEdbInfo.EdbInfoId {
  635. br.Msg = "两个指标不允许为同一个"
  636. br.ErrMsg = "两个指标不允许为同一个"
  637. return
  638. }
  639. edbInfo, err = models.AddCalculateLjztbpj(&req, fromEdbInfo, tbzEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName)
  640. case utils.DATA_SOURCE_CALCULATE_CJJX:
  641. sourName = "超季节性"
  642. edbInfo, err = models.AddCalculateCjjx(&req, fromEdbInfo, edbCode, uniqueCode, sysUserId, sysUserName, formulaInt)
  643. case utils.DATA_SOURCE_CALCULATE_NHCC:
  644. sourName = "拟合残差"
  645. secondEdbInfoReq := req.EdbInfoIdArr[1]
  646. secondEdbInfo, err := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  647. if err != nil {
  648. br.Msg = "获取因变量的指标信息失败"
  649. br.ErrMsg = "获取因变量的指标信息失败:Err:" + err.Error()
  650. return
  651. }
  652. if fromEdbInfo.EdbInfoId == secondEdbInfo.EdbInfoId {
  653. br.Msg = "两个指标不允许为同一个"
  654. br.ErrMsg = "两个指标不允许为同一个"
  655. return
  656. }
  657. edbInfo, err = models.AddCalculateNhcc(&req, fromEdbInfo, secondEdbInfo, edbCode, uniqueCode, nhccDate, sysUserId, sysUserName)
  658. default:
  659. br.Msg = "无效计算方式"
  660. br.ErrMsg = "无效计算方式,source:" + strconv.Itoa(req.Source)
  661. return
  662. }
  663. if err != nil {
  664. br.Msg = "生成" + sourName + "失败"
  665. br.Msg = "生成" + sourName + "失败 Err:" + err.Error()
  666. return
  667. }
  668. if edbInfo == nil {
  669. br.Msg = "生成" + sourName + "失败"
  670. br.ErrMsg = "生成" + sourName + "失败,指标ID错误:" + strconv.Itoa(edbInfoId)
  671. return
  672. }
  673. edbInfoId = edbInfo.EdbInfoId
  674. //处理同名指标
  675. {
  676. edbNameList, err := models.GetEdbInfoByName(req.EdbName)
  677. if err != nil {
  678. br.Msg = "保存失败"
  679. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  680. return
  681. }
  682. if len(edbNameList) >= 2 {
  683. for _, v := range edbNameList {
  684. edbName := v.EdbName + "(" + v.SourceName + ")"
  685. err = models.ModifyEdbInfoNameSource(edbName, v.EdbInfoId)
  686. if err != nil {
  687. br.Msg = "保存失败"
  688. br.ErrMsg = "修改指标名称失败,Err:" + err.Error()
  689. return
  690. }
  691. }
  692. }
  693. }
  694. // 更新指标最大最小值
  695. err, errMsg := models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  696. if err != nil {
  697. br.Msg = errMsg
  698. br.ErrMsg = err.Error()
  699. return
  700. }
  701. resp := models.AddEdbInfoResp{
  702. EdbInfoId: edbInfo.EdbInfoId,
  703. UniqueCode: edbInfo.UniqueCode,
  704. }
  705. br.Ret = 200
  706. br.Success = true
  707. br.Msg = "保存成功"
  708. br.Data = resp
  709. br.IsAddLog = true
  710. }
  711. // BatchEdit
  712. // @Title 累计值转月-同比值-同差等计算新增
  713. // @Description 累计值转月-同比值-同差等计算新增接口
  714. // @Param request body models.EdbInfoCalculateBatchSaveReq true "type json string"
  715. // @Success Ret=200 返回指标id
  716. // @router /batch/edit [post]
  717. func (this *CalculateController) BatchEdit() {
  718. br := new(models.BaseResponse).Init()
  719. defer func() {
  720. this.Data["json"] = br
  721. this.ServeJSON()
  722. }()
  723. var req models.EdbInfoCalculateBatchEditReq
  724. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  725. if err != nil {
  726. br.Msg = "参数解析异常!"
  727. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  728. return
  729. }
  730. if req.EdbName == "" {
  731. br.Msg = "指标名称不能为空"
  732. return
  733. }
  734. if req.Frequency == "" {
  735. br.Msg = "频率不能为空"
  736. return
  737. }
  738. if req.Unit == "" {
  739. br.Msg = "单位不能为空"
  740. return
  741. }
  742. if req.ClassifyId <= 0 {
  743. br.Msg = "请选择分类"
  744. return
  745. }
  746. if req.EdbInfoId <= 0 {
  747. br.Msg = "请选择指标"
  748. return
  749. }
  750. var condition string
  751. var pars []interface{}
  752. condition += " AND edb_name=? "
  753. pars = append(pars, req.EdbName)
  754. condition += " AND edb_info_id<>? "
  755. pars = append(pars, req.EdbInfoId)
  756. count, err := models.GetEdbInfoCountByCondition(condition, pars)
  757. if err != nil {
  758. br.Msg = "判断指标名称是否存在失败"
  759. br.ErrMsg = "判断指标名称是否存在失败,Err:" + err.Error()
  760. return
  761. }
  762. if count > 0 {
  763. br.Msg = "指标名称已存在,请重新填写"
  764. br.ErrMsg = "指标名称已存在,请重新填写"
  765. br.IsSendEmail = false
  766. return
  767. }
  768. edbInfo, err := models.GetEdbInfoById(req.EdbInfoId)
  769. if err != nil {
  770. if err.Error() == utils.ErrNoRow() {
  771. br.Msg = "指标已被删除,请刷新页面"
  772. br.ErrMsg = "指标已被删除,请刷新页面:Err:" + err.Error()
  773. return
  774. }
  775. br.Msg = "获取指标信息失败"
  776. br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
  777. return
  778. }
  779. // 基础指标id
  780. fromEdbInfoId := req.FromEdbInfoId
  781. if req.Source <= 0 {
  782. req.Source = edbInfo.Source
  783. }
  784. var formulaInt int
  785. var nhccDate models.NhccDate // 拟合残差的日期
  786. // 初期的参数校验
  787. switch req.Source {
  788. 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:
  789. if req.Formula == "" {
  790. br.Msg = "请填写N值"
  791. return
  792. }
  793. formulaInt, _ = strconv.Atoi(req.Formula)
  794. if formulaInt <= 0 {
  795. br.Msg = "N值输入错误,请重新输入"
  796. return
  797. }
  798. case utils.DATA_SOURCE_CALCULATE_ZJPJ:
  799. //直接拼接指标
  800. //校验时间格式
  801. _, err = time.ParseInLocation(utils.FormatDate, req.Formula, time.Local)
  802. if err != nil {
  803. br.Msg = "拼接日期有误,请重新输入"
  804. return
  805. }
  806. case utils.DATA_SOURCE_CALCULATE_NHCC: //拟合残差指标
  807. //指标校验
  808. if len(req.EdbInfoIdArr) != 2 {
  809. br.Msg = "选择的指标异常,请重新选择"
  810. return
  811. }
  812. fromEdbInfoId = req.EdbInfoIdArr[0].EdbInfoId
  813. //校验时间格式
  814. //数据格式:2022-11-01,2022-11-10
  815. timeList := strings.Split(req.Formula, ",")
  816. if len(timeList) != 2 {
  817. br.Msg = "选择时间有误,请重新输入"
  818. return
  819. }
  820. startDate, err := time.ParseInLocation(utils.FormatDate, timeList[0], time.Local)
  821. if err != nil {
  822. br.Msg = "开始日期有误,请重新输入"
  823. return
  824. }
  825. endDate, err := time.ParseInLocation(utils.FormatDate, timeList[1], time.Local)
  826. if err != nil {
  827. br.Msg = "结束日期有误,请重新输入"
  828. return
  829. }
  830. if utils.GetTimeSubDay(startDate, endDate) < 2 {
  831. br.Msg = "日期间隔不得少于两天"
  832. return
  833. }
  834. nhccDate.StartDate = startDate
  835. nhccDate.EndDate = endDate
  836. }
  837. // 获取基础指标信息
  838. fromEdbInfo, err := models.GetEdbInfoById(fromEdbInfoId)
  839. if err != nil {
  840. if err.Error() == utils.ErrNoRow() {
  841. br.Msg = "指标已被删除,请刷新页面"
  842. br.ErrMsg = "指标已被删除,请刷新页面:Err:" + err.Error()
  843. return
  844. }
  845. br.Msg = "获取指标信息失败"
  846. br.ErrMsg = "获取指标信息失败:Err:" + err.Error()
  847. return
  848. }
  849. var sourName string
  850. var edbInfoId int
  851. switch req.Source {
  852. case utils.DATA_SOURCE_CALCULATE_LJZZY:
  853. sourName = "累计值转月值"
  854. if fromEdbInfo.Frequency != "月度" {
  855. br.Msg = "请选择月度指标"
  856. return
  857. }
  858. err = models.EditCalculateLjzzy(edbInfo, &req, fromEdbInfo)
  859. case utils.DATA_SOURCE_CALCULATE_TBZ:
  860. sourName = "同比值"
  861. err = models.EditCalculateTbz(edbInfo, &req, fromEdbInfo)
  862. case utils.DATA_SOURCE_CALCULATE_TCZ:
  863. fmt.Println("start edit", time.Now())
  864. sourName = "同差值"
  865. err = models.EditCalculateTcz(edbInfo, &req, fromEdbInfo)
  866. fmt.Println("end edit", time.Now())
  867. case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS:
  868. sourName = "N数值移动平均计算"
  869. err = models.EditCalculateNszydpjjs(edbInfo, &req, fromEdbInfo, formulaInt, edbInfo.CalculateFormula)
  870. case utils.DATA_SOURCE_CALCULATE_HBZ:
  871. var condition string
  872. var pars []interface{}
  873. condition += " AND edb_info_id =? "
  874. pars = append(pars, req.FromEdbInfoId)
  875. condition += " AND value <=0 "
  876. checkCount, err := models.GetEdbDataCount(condition, pars, fromEdbInfo.Source)
  877. if err != nil && err.Error() != utils.ErrNoRow() {
  878. br.Msg = "判断环比值是否可计算失败"
  879. br.ErrMsg = "判断环比值是否可计算失败,Err:" + err.Error()
  880. return
  881. }
  882. if checkCount > 0 {
  883. br.Msg = "原始数据中存在0或负数,该指标不能进行环比运算"
  884. br.ErrMsg = "原始数据中出现0和负值时,提示该指标不能进行环比运算"
  885. return
  886. }
  887. sourName = "环比值"
  888. err = models.EditCalculateHbz(edbInfo, &req, fromEdbInfo, formulaInt, edbInfo.CalculateFormula)
  889. case utils.DATA_SOURCE_CALCULATE_HCZ:
  890. sourName = "环差值"
  891. err = models.EditCalculateHcz(edbInfo, &req, fromEdbInfo, formulaInt, edbInfo.CalculateFormula)
  892. case utils.DATA_SOURCE_CALCULATE_BP:
  893. sourName = "变频"
  894. err = models.EditCalculateBp(edbInfo, &req, fromEdbInfo)
  895. case utils.DATA_SOURCE_CALCULATE_TIME_SHIFT:
  896. sourName = "时间移位"
  897. err = models.EditCalculateTimeShift(edbInfo, &req, fromEdbInfo)
  898. case utils.DATA_SOURCE_CALCULATE_ZJPJ:
  899. sourName = "直接拼接"
  900. if len(req.EdbInfoIdArr) != 1 {
  901. br.Msg = "请传入拼接日期之后的指标"
  902. br.ErrMsg = "请传入拼接日期之后的指标"
  903. return
  904. }
  905. secondEdbInfoReq := req.EdbInfoIdArr[0]
  906. secondEdbInfo, err := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  907. if err != nil {
  908. br.Msg = "获取拼接日期之后的指标信息失败"
  909. br.ErrMsg = "获取拼接日期之后的指标信息失败:Err:" + err.Error()
  910. return
  911. }
  912. if fromEdbInfo.EdbInfoId == secondEdbInfo.EdbInfoId {
  913. br.Msg = "两个指标不允许为同一个"
  914. br.ErrMsg = "两个指标不允许为同一个"
  915. return
  916. }
  917. err = models.EditCalculateZjpj(&req, edbInfo, fromEdbInfo, secondEdbInfo)
  918. case utils.DATA_SOURCE_CALCULATE_LJZTBPJ: //累计值同比拼接
  919. sourName = "累计值同比拼接"
  920. if fromEdbInfo.Frequency != "月度" {
  921. br.Msg = "待拼接指标只能筛选月度指标"
  922. br.ErrMsg = "待拼接指标只能筛选月度指标"
  923. return
  924. }
  925. if len(req.EdbInfoIdArr) != 1 {
  926. br.Msg = "请传入同比值指标"
  927. br.ErrMsg = "请传入同比值指标"
  928. return
  929. }
  930. secondEdbInfoReq := req.EdbInfoIdArr[0]
  931. tbzEdbInfo, err := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  932. if err != nil {
  933. br.Msg = "获取同比值指标信息失败"
  934. br.ErrMsg = "获取同比值指标信息失败:Err:" + err.Error()
  935. return
  936. }
  937. if tbzEdbInfo.Source != utils.DATA_SOURCE_CALCULATE_TBZ {
  938. br.Msg = "指标必须是传入同比值指标类型"
  939. br.ErrMsg = "指标必须是传入同比值指标类型"
  940. return
  941. }
  942. if tbzEdbInfo.Frequency != "月度" {
  943. br.Msg = "同比值指标只能筛选月度指标"
  944. br.ErrMsg = "同比值指标只能筛选月度指标"
  945. return
  946. }
  947. if fromEdbInfo.EdbInfoId == tbzEdbInfo.EdbInfoId {
  948. br.Msg = "两个指标不允许为同一个"
  949. br.ErrMsg = "两个指标不允许为同一个"
  950. return
  951. }
  952. err = models.EditCalculateLjztbpj(&req, edbInfo, fromEdbInfo, tbzEdbInfo)
  953. case utils.DATA_SOURCE_CALCULATE_CJJX:
  954. sourName = "超季节性"
  955. err = models.EditCalculateCjjx(&req, edbInfo, fromEdbInfo, formulaInt)
  956. case utils.DATA_SOURCE_CALCULATE_NHCC:
  957. sourName = "拟合残差"
  958. secondEdbInfoReq := req.EdbInfoIdArr[1]
  959. secondEdbInfo, err := models.GetEdbInfoById(secondEdbInfoReq.EdbInfoId)
  960. if err != nil {
  961. br.Msg = "获取因变量的指标信息失败"
  962. br.ErrMsg = "获取因变量的指标信息失败:Err:" + err.Error()
  963. return
  964. }
  965. if fromEdbInfo.EdbInfoId == secondEdbInfo.EdbInfoId {
  966. br.Msg = "两个指标不允许为同一个"
  967. br.ErrMsg = "两个指标不允许为同一个"
  968. return
  969. }
  970. err = models.EditCalculateNhcc(&req, edbInfo, fromEdbInfo, secondEdbInfo, nhccDate)
  971. default:
  972. br.Msg = "无效计算方式"
  973. br.ErrMsg = "无效计算方式,source:" + strconv.Itoa(req.Source)
  974. return
  975. }
  976. if err != nil {
  977. br.Msg = "生成" + sourName + "失败"
  978. br.Msg = "生成" + sourName + "失败 Err:" + err.Error()
  979. return
  980. }
  981. if edbInfo == nil {
  982. br.Msg = "生成" + sourName + "失败"
  983. br.ErrMsg = "生成" + sourName + "失败,指标ID错误:" + strconv.Itoa(edbInfoId)
  984. return
  985. }
  986. edbInfoId = edbInfo.EdbInfoId
  987. //处理同名指标
  988. {
  989. edbNameList, err := models.GetEdbInfoByName(req.EdbName)
  990. if err != nil {
  991. br.Msg = "保存失败"
  992. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  993. return
  994. }
  995. if len(edbNameList) >= 2 {
  996. for _, v := range edbNameList {
  997. edbName := v.EdbName + "(" + v.SourceName + ")"
  998. err = models.ModifyEdbInfoNameSource(edbName, v.EdbInfoId)
  999. if err != nil {
  1000. br.Msg = "保存失败"
  1001. br.ErrMsg = "修改指标名称失败,Err:" + err.Error()
  1002. return
  1003. }
  1004. }
  1005. }
  1006. }
  1007. // 更新指标最大最小值
  1008. err, errMsg := models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  1009. if err != nil {
  1010. br.Msg = errMsg
  1011. br.ErrMsg = err.Error()
  1012. return
  1013. }
  1014. resp := models.AddEdbInfoResp{
  1015. EdbInfoId: edbInfo.EdbInfoId,
  1016. UniqueCode: edbInfo.UniqueCode,
  1017. }
  1018. br.Ret = 200
  1019. br.Success = true
  1020. br.Msg = "保存成功"
  1021. br.Data = resp
  1022. br.IsAddLog = true
  1023. }
  1024. // Refresh
  1025. // @Title 刷新计算指标接口
  1026. // @Description 刷新计算指标接口
  1027. // @Success 200 {object} models.RefreshEdbInfoReq
  1028. // @router /refresh [post]
  1029. func (this *CalculateController) Refresh() {
  1030. br := new(models.BaseResponse).Init()
  1031. var cacheKey string
  1032. defer func() {
  1033. this.Data["json"] = br
  1034. this.ServeJSON()
  1035. }()
  1036. var req models.RefreshEdbInfoReq
  1037. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1038. if err != nil {
  1039. br.Msg = "参数解析异常!"
  1040. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1041. return
  1042. }
  1043. if req.EdbCode == "" {
  1044. br.Msg = "请输入指标编码!"
  1045. br.ErrMsg = "请输入指标编码,指标编码为空"
  1046. return
  1047. }
  1048. if req.EdbInfoId <= 0 {
  1049. br.Msg = "请输入指标ID!"
  1050. br.ErrMsg = "请输入指标ID"
  1051. return
  1052. }
  1053. edbInfo, err := models.GetEdbInfoById(req.EdbInfoId)
  1054. if err != nil {
  1055. br.Msg = "指标不存在!"
  1056. br.ErrMsg = "指标不存在"
  1057. return
  1058. }
  1059. cacheKey = utils.CACHE_EDB_DATA_REFRESH + strconv.Itoa(edbInfo.Source) + "_" + req.EdbCode
  1060. if utils.Rc.IsExist(cacheKey) {
  1061. br.Ret = 501
  1062. br.Success = true
  1063. br.Msg = "系统处理中,请稍后重试"
  1064. return
  1065. }
  1066. // 刷新指标
  1067. utils.Rc.SetNX(cacheKey, 1, 1*time.Minute)
  1068. defer func() {
  1069. utils.Rc.Delete(cacheKey)
  1070. }()
  1071. startDate := req.StartDate
  1072. var errMsg string
  1073. endDate := time.Now().Format(utils.FormatDate)
  1074. edbInfoId := edbInfo.EdbInfoId
  1075. source := edbInfo.Source
  1076. switch source {
  1077. case utils.DATA_SOURCE_CALCULATE:
  1078. //startDate = edbInfo.StartDate
  1079. //sTime, err := time.Parse(utils.FormatDate, edbInfo.EndDate)
  1080. //if err != nil {
  1081. // return
  1082. //}
  1083. //startDate = sTime.Format(utils.FormatDate)
  1084. startDate = ""
  1085. var edbInfoIdBytes []string
  1086. calculateMap, err := models.GetEdbInfoCalculateDetailList(edbInfo.EdbInfoId)
  1087. if err != nil {
  1088. errMsg = "GetEdbInfoCalculateDetail Err:" + err.Error()
  1089. break
  1090. }
  1091. var formulaStr string
  1092. edbInfoList := make([]*models.EdbInfo, 0)
  1093. for _, v := range calculateMap {
  1094. formulaStr += v.FromTag + ","
  1095. edbInfoIdBytes = append(edbInfoIdBytes, v.FromTag)
  1096. edbInfo, _ := models.GetEdbInfoById(v.FromEdbInfoId)
  1097. edbInfoList = append(edbInfoList, edbInfo)
  1098. }
  1099. err = models.RefreshAllCalculate(edbInfoList, edbInfo.EdbInfoId, source, edbInfo.EdbCode, edbInfo.CalculateFormula, startDate, endDate, edbInfoIdBytes)
  1100. if err != nil && err.Error() != utils.ErrNoRow() {
  1101. errMsg = "RefreshCalculate Err:" + err.Error()
  1102. break
  1103. }
  1104. case utils.DATA_SOURCE_CALCULATE_LJZZY: //刷新累计值转月值
  1105. calculateLjzzy, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1106. if err != nil {
  1107. errMsg = "GetEdbInfoCalculateLjzzyDetail Err:" + err.Error()
  1108. break
  1109. }
  1110. fromEdbInfo, err := models.GetEdbInfoById(calculateLjzzy.FromEdbInfoId)
  1111. if err != nil {
  1112. errMsg = "GetEdbInfoById Err:" + err.Error()
  1113. break
  1114. }
  1115. //startDate = edbInfo.StartDate
  1116. endDate = time.Now().Format(utils.FormatDate)
  1117. err = models.RefreshAllCalculateLjzzy(edbInfoId, source, fromEdbInfo, calculateLjzzy.EdbCode, startDate, endDate)
  1118. if err != nil && err.Error() != utils.ErrNoRow() {
  1119. errMsg = "RefreshAllCalculateLjzzy Err:" + err.Error()
  1120. break
  1121. }
  1122. case utils.DATA_SOURCE_CALCULATE_TBZ: //刷新同比值
  1123. calculateTbz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1124. if err != nil {
  1125. errMsg = "GetEdbInfoCalculateTbzDetail Err:" + err.Error()
  1126. break
  1127. }
  1128. fromEdbInfo, err := models.GetEdbInfoById(calculateTbz.FromEdbInfoId)
  1129. if err != nil {
  1130. errMsg = "GetEdbInfoById Err:" + err.Error()
  1131. break
  1132. }
  1133. //startDate = edbInfo.StartDate
  1134. endDate = time.Now().Format(utils.FormatDate)
  1135. err = models.RefreshAllCalculateTbz(edbInfoId, source, fromEdbInfo, calculateTbz.EdbCode, startDate, endDate)
  1136. if err != nil && err.Error() != utils.ErrNoRow() {
  1137. errMsg = "RefreshAllCalculateTbz Err:" + err.Error()
  1138. break
  1139. }
  1140. case utils.DATA_SOURCE_CALCULATE_TCZ: //同差值
  1141. calculateTcz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1142. if err != nil {
  1143. errMsg = "GetEdbInfoCalculateTczDetail Err:" + err.Error()
  1144. break
  1145. }
  1146. fromEdbInfo, err := models.GetEdbInfoById(calculateTcz.FromEdbInfoId)
  1147. if err != nil {
  1148. errMsg = "GetEdbInfoById Err:" + err.Error()
  1149. break
  1150. }
  1151. //startDate = edbInfo.StartDate
  1152. endDate = time.Now().Format(utils.FormatDate)
  1153. err = models.RefreshAllCalculateTcz(edbInfoId, source, fromEdbInfo, calculateTcz.EdbCode, startDate, endDate)
  1154. if err != nil && err.Error() != utils.ErrNoRow() {
  1155. errMsg = "RefreshCalculateTcz Err:" + err.Error()
  1156. break
  1157. }
  1158. case utils.DATA_SOURCE_CALCULATE_NSZYDPJJS: //N数值移动平均计算
  1159. calculateNszydpjjs, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1160. if err != nil {
  1161. errMsg = "GetEdbInfoCalculateNszydpjjsDetail Err:" + err.Error()
  1162. break
  1163. }
  1164. fromEdbInfo, err := models.GetEdbInfoById(calculateNszydpjjs.FromEdbInfoId)
  1165. if err != nil {
  1166. errMsg = "GetEdbInfoById Err:" + err.Error()
  1167. break
  1168. }
  1169. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  1170. //startDate = edbInfo.StartDate
  1171. err = models.RefreshAllCalculateNszydpjjs(edbInfoId, edbInfo.Source, formulaInt, fromEdbInfo, calculateNszydpjjs.EdbCode, startDate)
  1172. if err != nil && err.Error() != utils.ErrNoRow() {
  1173. errMsg = "RefreshCalculateNszydpjjs Err:" + err.Error()
  1174. break
  1175. }
  1176. case utils.DATA_SOURCE_CALCULATE_HBZ: //刷新环比值
  1177. calculateTbz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1178. if err != nil {
  1179. errMsg = "GetEdbInfoCalculateHbzDetail Err:" + err.Error()
  1180. break
  1181. }
  1182. fromEdbInfo, err := models.GetEdbInfoById(calculateTbz.FromEdbInfoId)
  1183. if err != nil {
  1184. errMsg = "GetEdbInfoById Err:" + err.Error()
  1185. break
  1186. }
  1187. //startDate = edbInfo.StartDate
  1188. endDate = time.Now().Format(utils.FormatDate)
  1189. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  1190. err = models.RefreshAllCalculateHbz(edbInfoId, source, fromEdbInfo, calculateTbz.EdbCode, startDate, endDate, formulaInt)
  1191. if err != nil && err.Error() != utils.ErrNoRow() {
  1192. errMsg = "RefreshAllCalculateHbz Err:" + err.Error()
  1193. break
  1194. }
  1195. case utils.DATA_SOURCE_CALCULATE_HCZ: //刷新环差值
  1196. calculateTbz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1197. if err != nil {
  1198. errMsg = "GetEdbInfoCalculateHczDetail Err:" + err.Error()
  1199. break
  1200. }
  1201. fromEdbInfo, err := models.GetEdbInfoById(calculateTbz.FromEdbInfoId)
  1202. if err != nil {
  1203. errMsg = "GetEdbInfoById Err:" + err.Error()
  1204. break
  1205. }
  1206. //startDate = edbInfo.StartDate
  1207. endDate = time.Now().Format(utils.FormatDate)
  1208. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  1209. err = models.RefreshAllCalculateHcz(edbInfoId, source, fromEdbInfo, calculateTbz.EdbCode, startDate, endDate, formulaInt)
  1210. if err != nil && err.Error() != utils.ErrNoRow() {
  1211. errMsg = "RefreshAllCalculateHcz Err:" + err.Error()
  1212. break
  1213. }
  1214. case utils.DATA_SOURCE_CALCULATE_BP: //刷新变频
  1215. calculateTbz, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1216. if err != nil {
  1217. errMsg = "GetEdbInfoCalculateTbzDetail Err:" + err.Error()
  1218. break
  1219. }
  1220. fromEdbInfo, err := models.GetEdbInfoById(calculateTbz.FromEdbInfoId)
  1221. if err != nil {
  1222. errMsg = "GetEdbInfoById Err:" + err.Error()
  1223. break
  1224. }
  1225. //startDate = edbInfo.StartDate
  1226. endDate = time.Now().Format(utils.FormatDate)
  1227. err = models.RefreshAllCalculateBp(edbInfoId, source, fromEdbInfo, calculateTbz.EdbCode, startDate, endDate)
  1228. if err != nil && err.Error() != utils.ErrNoRow() {
  1229. errMsg = "RefreshAllCalculateBp Err:" + err.Error()
  1230. break
  1231. }
  1232. case utils.DATA_SOURCE_CALCULATE_TIME_SHIFT:
  1233. calculate, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1234. if err != nil {
  1235. errMsg = "GetEdbInfoCalculateTbzDetail Err:" + err.Error()
  1236. break
  1237. }
  1238. fromEdbInfo, err := models.GetEdbInfoById(calculate.FromEdbInfoId)
  1239. if err != nil {
  1240. errMsg = "GetEdbInfoById Err:" + err.Error()
  1241. break
  1242. }
  1243. //startDate = edbInfo.StartDate
  1244. endDate = time.Now().Format(utils.FormatDate)
  1245. formulaInt, _ := strconv.Atoi(calculate.CalculateFormula)
  1246. err = models.RefreshAllCalculateTimeShift(edbInfoId, source, formulaInt, calculate.MoveType, fromEdbInfo, calculate.EdbCode, startDate, endDate, calculate.MoveFrequency)
  1247. if err != nil && err.Error() != utils.ErrNoRow() {
  1248. errMsg = "RefreshAllCalculateTimeShift Err:" + err.Error()
  1249. }
  1250. case utils.DATA_SOURCE_CALCULATE_ZJPJ: //刷新直接拼接
  1251. err = models.RefreshAllCalculateZjpj(edbInfo)
  1252. if err != nil && err.Error() != utils.ErrNoRow() {
  1253. errMsg = "RefreshAllCalculateZjpj Err:" + err.Error()
  1254. break
  1255. }
  1256. case utils.DATA_SOURCE_CALCULATE_LJZTBPJ: //刷新累计值同比拼接
  1257. err = models.RefreshAllCalculateLjztbpj(edbInfo)
  1258. if err != nil && err.Error() != utils.ErrNoRow() {
  1259. errMsg = "RefreshAllCalculateLjztbpj Err:" + err.Error()
  1260. break
  1261. }
  1262. case utils.DATA_SOURCE_PYTHON: //python代码运算
  1263. edbPythonCode, err := models.GetEdbPythonCodeById(edbInfo.EdbInfoId)
  1264. if err != nil {
  1265. errMsg = "获取python代码失败 Err:" + err.Error()
  1266. break
  1267. }
  1268. edbData, err, errMsg := services.ExecPythonCode(edbInfo.EdbCode, edbPythonCode.PythonCode)
  1269. if err != nil {
  1270. br.Msg = "获取数据失败"
  1271. br.ErrMsg = "python代码获取数据失败,err:" + err.Error()
  1272. if errMsg != "" {
  1273. br.ErrMsg = errMsg
  1274. }
  1275. return
  1276. }
  1277. err = models.RefreshAllPythonEdb(edbInfo, edbData)
  1278. if err != nil && err.Error() != utils.ErrNoRow() {
  1279. errMsg = "RefreshAllPythonEdb Err:" + err.Error()
  1280. break
  1281. }
  1282. case utils.DATA_SOURCE_CALCULATE_CJJX: //超季节性
  1283. calculateCjjx, err := models.GetEdbInfoCalculateMappingDetail(edbInfoId)
  1284. if err != nil {
  1285. errMsg = "GetEdbInfoCalculateNszydpjjsDetail Err:" + err.Error()
  1286. break
  1287. }
  1288. fromEdbInfo, err := models.GetEdbInfoById(calculateCjjx.FromEdbInfoId)
  1289. if err != nil {
  1290. errMsg = "GetEdbInfoById Err:" + err.Error()
  1291. break
  1292. }
  1293. formulaInt, _ := strconv.Atoi(edbInfo.CalculateFormula)
  1294. //startDate = edbInfo.StartDate
  1295. err = models.RefreshAllCalculateCjjx(edbInfoId, edbInfo.Source, fromEdbInfo, calculateCjjx.EdbCode, startDate, "", formulaInt)
  1296. if err != nil && err.Error() != utils.ErrNoRow() {
  1297. errMsg = "RefreshAllCalculateCjjx Err:" + err.Error()
  1298. break
  1299. }
  1300. case utils.DATA_SOURCE_CALCULATE_NHCC: //nhcc
  1301. err = models.RefreshAllCalculateNhcc(edbInfo)
  1302. if err != nil && err.Error() != utils.ErrNoRow() {
  1303. errMsg = "RefreshAllCalculateNhcc Err:" + err.Error()
  1304. break
  1305. }
  1306. default:
  1307. br.Msg = "来源异常,请联系相关开发!"
  1308. br.ErrMsg = "来源异常,请联系相关开发"
  1309. return
  1310. }
  1311. if errMsg != `` {
  1312. br.Msg = "刷新指标失败!"
  1313. br.ErrMsg = "刷新指标失败,err:" + errMsg
  1314. return
  1315. }
  1316. // 更新指标最大最小值
  1317. err, errMsg = models.UnifiedModifyEdbInfoMaxAndMinInfo(edbInfo)
  1318. if err != nil {
  1319. br.Msg = errMsg
  1320. br.ErrMsg = err.Error()
  1321. return
  1322. }
  1323. br.Ret = 200
  1324. br.Success = true
  1325. br.Msg = "获取成功"
  1326. }