warehouse.go 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816
  1. package trade_analysis
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/data_manage"
  7. tradeAnalysisModel "eta/eta_api/models/data_manage/trade_analysis"
  8. tradeAnalysisRequest "eta/eta_api/models/data_manage/trade_analysis/request"
  9. tradeAnalysisResponse "eta/eta_api/models/data_manage/trade_analysis/response"
  10. "eta/eta_api/services/alarm_msg"
  11. "eta/eta_api/services/data"
  12. tradeAnalysisService "eta/eta_api/services/data/trade_analysis"
  13. "eta/eta_api/utils"
  14. "fmt"
  15. "github.com/rdlucklib/rdluck_tools/paging"
  16. "sort"
  17. "strconv"
  18. "strings"
  19. "time"
  20. )
  21. // WarehouseController 建仓过程
  22. type WarehouseController struct {
  23. controllers.BaseAuthController
  24. }
  25. // Preview
  26. // @Title 图表预览/编辑页预览
  27. // @Description 图表预览/编辑页预览
  28. // @Param request body tradeAnalysisRequest.WarehousePreviewReq true "type json string"
  29. // @Success 200 {object} request.WarehousePreviewReq
  30. // @router /warehouse/chart/preview [post]
  31. func (this *WarehouseController) Preview() {
  32. br := new(models.BaseResponse).Init()
  33. defer func() {
  34. if br.ErrMsg == "" {
  35. br.IsSendEmail = false
  36. }
  37. this.Data["json"] = br
  38. this.ServeJSON()
  39. }()
  40. sysUser := this.SysUser
  41. if sysUser == nil {
  42. br.Msg = "请登录"
  43. br.ErrMsg = "请登录,SysUser Is Empty"
  44. br.Ret = 408
  45. return
  46. }
  47. var req tradeAnalysisRequest.WarehousePreviewReq
  48. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  49. br.Msg = "参数解析异常!"
  50. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  51. return
  52. }
  53. // 非编辑页预览时需要校验参数
  54. if req.ChartInfoId <= 0 {
  55. if req.ExtraConfig == nil {
  56. br.Msg = "图表参数有误"
  57. return
  58. }
  59. pass, tips := tradeAnalysisService.CheckWarehouseChartExtraConfig(*req.ExtraConfig)
  60. if !pass {
  61. br.Msg = tips
  62. return
  63. }
  64. }
  65. resp := new(tradeAnalysisResponse.WarehouseChartDetailResp)
  66. resp.WarehouseCharts = make([]*data_manage.ChartInfoDetailResp, 0)
  67. var (
  68. multiConfigId int
  69. extraConfig tradeAnalysisModel.WarehouseExtraConfig
  70. configBuy, configSold, configPure tradeAnalysisModel.WarehouseChartPars
  71. chartBuy, chartSold, chartPure *data_manage.ChartInfoView
  72. )
  73. configBuy.WarehouseChartType = tradeAnalysisModel.WarehouseBuyChartType
  74. configSold.WarehouseChartType = tradeAnalysisModel.WarehouseSoldChartType
  75. configPure.WarehouseChartType = tradeAnalysisModel.WarehousePureBuyChartType
  76. // 编辑页预览, 图表默认取自己的配置, 入参有值时则均取入参
  77. if req.ChartInfoId > 0 {
  78. chartInfoView, e := data_manage.GetChartInfoViewById(req.ChartInfoId)
  79. if e != nil {
  80. if e.Error() == utils.ErrNoRow() {
  81. br.Msg = "图表已被删除, 请刷新页面"
  82. return
  83. }
  84. br.Msg = "获取失败"
  85. br.ErrMsg = fmt.Sprintf("获取图表信息失败, %v", e)
  86. return
  87. }
  88. if chartInfoView.ExtraConfig == `` {
  89. br.Msg = "图表配置信息错误"
  90. return
  91. }
  92. if e = json.Unmarshal([]byte(chartInfoView.ExtraConfig), &extraConfig); e != nil {
  93. br.Msg = "配置信息错误"
  94. br.ErrMsg = fmt.Sprintf("图表配置信息错误, %v", e)
  95. return
  96. }
  97. multiConfigId = extraConfig.MultipleGraphConfigId
  98. // 图表的图例设置
  99. seriesList, e := data_manage.GetChartSeriesByChartInfoId(req.ChartInfoId)
  100. if e != nil {
  101. br.Msg = "获取失败"
  102. br.ErrMsg = fmt.Sprintf("获取图例失败, %v", e)
  103. return
  104. }
  105. saveItems := make([]*data_manage.ChartSaveItem, 0)
  106. for _, v := range seriesList {
  107. saveItems = append(saveItems, &data_manage.ChartSaveItem{
  108. UniqueFlag: v.UniqueFlag,
  109. EdbAliasName: v.SeriesName,
  110. IsAxis: v.IsAxis,
  111. })
  112. }
  113. // 图表取自己的日期配置
  114. switch extraConfig.WarehouseChartType {
  115. case tradeAnalysisModel.WarehouseBuyChartType:
  116. chartBuy = chartInfoView
  117. configBuy.WarehouseChartType = extraConfig.WarehouseChartType
  118. configBuy.DateType = chartInfoView.DateType
  119. configBuy.DateTypeNum = chartInfoView.DateTypeNum
  120. configBuy.StartDate = chartInfoView.StartDate
  121. configBuy.EndDate = chartInfoView.EndDate
  122. configBuy.ChartEdbInfoList = saveItems
  123. case tradeAnalysisModel.WarehouseSoldChartType:
  124. chartSold = chartInfoView
  125. configSold.WarehouseChartType = extraConfig.WarehouseChartType
  126. configSold.DateType = chartInfoView.DateType
  127. configSold.DateTypeNum = chartInfoView.DateTypeNum
  128. configSold.StartDate = chartInfoView.StartDate
  129. configSold.EndDate = chartInfoView.EndDate
  130. configSold.ChartEdbInfoList = saveItems
  131. case tradeAnalysisModel.WarehousePureBuyChartType:
  132. chartPure = chartInfoView
  133. configPure.WarehouseChartType = extraConfig.WarehouseChartType
  134. configPure.DateType = chartInfoView.DateType
  135. configPure.DateTypeNum = chartInfoView.DateTypeNum
  136. configPure.StartDate = chartInfoView.StartDate
  137. configPure.EndDate = chartInfoView.EndDate
  138. configPure.ChartEdbInfoList = saveItems
  139. }
  140. }
  141. // 入参有图表配置那么均取入参的
  142. if req.ExtraConfig != nil {
  143. extraConfig = *req.ExtraConfig
  144. }
  145. if len(req.ChartsConfig) > 0 {
  146. for _, v := range req.ChartsConfig {
  147. if v.WarehouseChartType == tradeAnalysisModel.WarehouseBuyChartType {
  148. configBuy = v
  149. continue
  150. }
  151. if v.WarehouseChartType == tradeAnalysisModel.WarehouseSoldChartType {
  152. configSold = v
  153. continue
  154. }
  155. if v.WarehouseChartType == tradeAnalysisModel.WarehousePureBuyChartType {
  156. configPure = v
  157. }
  158. }
  159. }
  160. // 查询配置关联的指标
  161. multiEdbBuy, multiEdbSold, multiEdbPure := make([]*tradeAnalysisModel.WarehouseEdbSaveItem, 0), make([]*tradeAnalysisModel.WarehouseEdbSaveItem, 0), make([]*tradeAnalysisModel.WarehouseEdbSaveItem, 0)
  162. if multiConfigId > 0 {
  163. multiEdbList, e := data_manage.GetMultipleGraphConfigEdbMappingListByIdAndSource(multiConfigId, utils.CHART_SOURCE_TRADE_ANALYSIS_PROCESS)
  164. if e != nil {
  165. br.Msg = "获取失败"
  166. br.ErrMsg = fmt.Sprintf("获取配置与图表指标关系失败, %v", e)
  167. return
  168. }
  169. var edbIds []int
  170. for _, v := range multiEdbList {
  171. edbIds = append(edbIds, v.EdbInfoId)
  172. }
  173. if len(edbIds) > 0 {
  174. edbList, e := data_manage.GetEdbInfoByIdList(edbIds)
  175. if e != nil {
  176. br.Msg = "获取失败"
  177. br.ErrMsg = fmt.Sprintf("获取配置关联指标信息失败, %v", e)
  178. return
  179. }
  180. for _, v := range edbList {
  181. if v.CalculateFormula == "" {
  182. utils.FileLog.Info(fmt.Sprintf("持仓分析图表详情-指标计算公式为空, EdbInfoId: %d", v.EdbInfoId))
  183. continue
  184. }
  185. var conf tradeAnalysisModel.WarehouseExtraConfig
  186. if e = json.Unmarshal([]byte(v.CalculateFormula), &conf); e != nil {
  187. utils.FileLog.Info(fmt.Sprintf("持仓分析图表详情-指标计算公式解析失败, EdbInfoId: %d, err: %v", v.EdbInfoId, e))
  188. continue
  189. }
  190. if len(conf.Companies) != 1 {
  191. utils.FileLog.Info(fmt.Sprintf("持仓分析图表详情-指标计算公式信息有误, EdbInfoId: %d", v.EdbInfoId))
  192. continue
  193. }
  194. item := new(tradeAnalysisModel.WarehouseEdbSaveItem)
  195. item.EdbInfoId = v.EdbInfoId
  196. item.EdbName = v.EdbName
  197. item.Unit = v.Unit
  198. item.Frequency = v.Frequency
  199. item.ClassifyId = v.ClassifyId
  200. item.UniqueFlag = conf.Companies[0]
  201. switch conf.WarehouseChartType {
  202. case tradeAnalysisModel.WarehouseBuyChartType:
  203. multiEdbBuy = append(multiEdbBuy, item)
  204. case tradeAnalysisModel.WarehouseSoldChartType:
  205. multiEdbSold = append(multiEdbSold, item)
  206. case tradeAnalysisModel.WarehousePureBuyChartType:
  207. multiEdbPure = append(multiEdbPure, item)
  208. }
  209. }
  210. }
  211. }
  212. // 获取指标数据, 该图表未用实际指标, 为了统一数据格式用ChartEdbInfoMapping
  213. companyTradeData, e := tradeAnalysisService.GetWarehouseTradeData(extraConfig.Exchange, extraConfig.ClassifyName, extraConfig.Contracts, extraConfig.Companies, extraConfig.PredictRatio)
  214. if e != nil {
  215. br.Msg = "获取失败"
  216. br.ErrMsg = fmt.Sprintf("获取期货公司持仓加总数据失败, %v", e)
  217. return
  218. }
  219. // 获取三种图表
  220. buyChartResp, e := tradeAnalysisService.GetWarehouseChartResp(chartBuy, companyTradeData, multiEdbBuy, extraConfig, configBuy)
  221. if e != nil {
  222. br.Msg = "获取失败"
  223. br.ErrMsg = fmt.Sprintf("获取建仓过程多单图表数据失败, %v", e)
  224. return
  225. }
  226. soldChartResp, e := tradeAnalysisService.GetWarehouseChartResp(chartSold, companyTradeData, multiEdbSold, extraConfig, configSold)
  227. if e != nil {
  228. br.Msg = "获取失败"
  229. br.ErrMsg = fmt.Sprintf("获取建仓过程空单图表数据失败, %v", e)
  230. return
  231. }
  232. pureChartResp, e := tradeAnalysisService.GetWarehouseChartResp(chartPure, companyTradeData, multiEdbPure, extraConfig, configPure)
  233. if e != nil {
  234. br.Msg = "获取失败"
  235. br.ErrMsg = fmt.Sprintf("获取建仓过程净多单图表数据失败, %v", e)
  236. return
  237. }
  238. resp.MultipleGraphConfigId = multiConfigId
  239. resp.WarehouseCharts = append(resp.WarehouseCharts, buyChartResp, soldChartResp, pureChartResp)
  240. // 多图配置
  241. if multiConfigId == 0 {
  242. multipleGraphConfig := &data_manage.MultipleGraphConfig{
  243. SysUserId: sysUser.AdminId,
  244. SysUserRealName: sysUser.RealName,
  245. ModifyTime: time.Now(),
  246. CreateTime: time.Now(),
  247. }
  248. if e = data_manage.AddMultipleGraphConfig(multipleGraphConfig); e != nil {
  249. br.Msg = "获取失败"
  250. br.ErrMsg = fmt.Sprintf("新增建仓过程多图配置失败, %v", e)
  251. return
  252. }
  253. resp.MultipleGraphConfigId = multipleGraphConfig.MultipleGraphConfigId
  254. }
  255. br.Data = resp
  256. br.Ret = 200
  257. br.Success = true
  258. br.Msg = "操作成功"
  259. }
  260. // Add
  261. // @Title 新增图表接口
  262. // @Description 新增图表接口
  263. // @Param request body data_manage.AddChartInfoReq true "type json string"
  264. // @Success 200 {object} data_manage.AddChartInfoResp
  265. // @router /warehouse/chart/add [post]
  266. func (this *WarehouseController) Add() {
  267. br := new(models.BaseResponse).Init()
  268. defer func() {
  269. if br.ErrMsg == "" {
  270. br.IsSendEmail = false
  271. }
  272. this.Data["json"] = br
  273. this.ServeJSON()
  274. }()
  275. sysUser := this.SysUser
  276. if sysUser == nil {
  277. br.Msg = "请登录"
  278. br.ErrMsg = "请登录,SysUser Is Empty"
  279. br.Ret = 408
  280. return
  281. }
  282. deleteCache := true
  283. cacheKey := "CACHE_CHART_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  284. defer func() {
  285. if deleteCache {
  286. _ = utils.Rc.Delete(cacheKey)
  287. }
  288. }()
  289. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  290. deleteCache = false
  291. br.Msg = "系统处理中,请稍后重试!"
  292. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  293. return
  294. }
  295. var req data_manage.AddChartInfoReq
  296. if err := json.Unmarshal(this.Ctx.Input.RequestBody, &req); err != nil {
  297. br.Msg = "参数解析异常!"
  298. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  299. return
  300. }
  301. // 图表校验
  302. req.ChartName = strings.Trim(req.ChartName, " ")
  303. if req.ChartName == "" {
  304. br.Msg = "请填写图表名称"
  305. return
  306. }
  307. if req.ChartClassifyId <= 0 {
  308. br.Msg = "请选择分类"
  309. return
  310. }
  311. var extraConfig tradeAnalysisModel.WarehouseExtraConfig
  312. if req.ExtraConfig == `` {
  313. br.Msg = "配置有误"
  314. br.ErrMsg = fmt.Sprintf("建仓图表配置有误, conf: %s", req.ExtraConfig)
  315. return
  316. }
  317. if e := json.Unmarshal([]byte(req.ExtraConfig), &extraConfig); e != nil {
  318. br.Msg = "配置有误"
  319. br.ErrMsg = fmt.Sprintf("建仓图表配置解析失败, err: %v; conf: %s", e, req.ExtraConfig)
  320. return
  321. }
  322. pass, tips := tradeAnalysisService.CheckWarehouseChartExtraConfig(extraConfig)
  323. if !pass {
  324. br.Msg = tips
  325. return
  326. }
  327. // 校验图例
  328. if len(req.ChartEdbInfoList) == 0 {
  329. br.Msg = "请填写图例"
  330. return
  331. }
  332. if len(req.ChartEdbInfoList) != len(extraConfig.Companies) {
  333. br.Msg = "图例不一致"
  334. br.ErrMsg = fmt.Sprintf("图例不一致, len: %d, companies: %d", len(req.ChartEdbInfoList), len(extraConfig.Companies))
  335. return
  336. }
  337. flagExist := make(map[string]bool)
  338. for _, v := range req.ChartEdbInfoList {
  339. if v.UniqueFlag == "" {
  340. br.Msg = ""
  341. }
  342. if flagExist[v.UniqueFlag] {
  343. br.Msg = "图例异常"
  344. br.ErrMsg = fmt.Sprintf("建仓图表图例异常, %s", v.UniqueFlag)
  345. return
  346. }
  347. }
  348. // 校验分类
  349. _, e := data_manage.GetChartClassifyById(req.ChartClassifyId)
  350. if e != nil {
  351. if e.Error() == utils.ErrNoRow() {
  352. br.Msg = "分类不存在, 请刷新页面"
  353. return
  354. }
  355. br.Msg = "操作失败"
  356. br.ErrMsg = fmt.Sprintf("获取分类失败, %v", e)
  357. return
  358. }
  359. // 校验图表是否存在
  360. {
  361. var cond string
  362. var pars []interface{}
  363. switch this.Lang {
  364. case utils.EnLangVersion:
  365. cond += " AND chart_name_en = ? AND source = ? "
  366. default:
  367. cond += " AND chart_name = ? AND source = ? "
  368. }
  369. pars = append(pars, req.ChartName, utils.CHART_SOURCE_TRADE_ANALYSIS_PROCESS)
  370. count, e := data_manage.GetChartInfoCountByCondition(cond, pars)
  371. if e != nil {
  372. br.Msg = "操作失败"
  373. br.ErrMsg = fmt.Sprintf("获取同名图表计数失败, %v", e)
  374. return
  375. }
  376. if count > 0 {
  377. br.Msg = "图表名称已存在, 请重新填写"
  378. return
  379. }
  380. }
  381. // 新增图表
  382. chartInfo, e := tradeAnalysisService.AddWarehouseChart(req, extraConfig, sysUser.AdminId, sysUser.RealName)
  383. if e != nil {
  384. br.Msg = "操作失败"
  385. br.ErrMsg = fmt.Sprintf("新增持仓分析图表失败, %v", e)
  386. return
  387. }
  388. // 新增操作日志
  389. {
  390. chartLog := new(data_manage.ChartInfoLog)
  391. chartLog.ChartInfoId = chartInfo.ChartInfoId
  392. chartLog.ChartName = req.ChartName
  393. chartLog.ChartClassifyId = req.ChartClassifyId
  394. chartLog.SysUserId = sysUser.AdminId
  395. chartLog.SysUserRealName = sysUser.RealName
  396. chartLog.UniqueCode = chartInfo.UniqueCode
  397. chartLog.CreateTime = time.Now()
  398. chartLog.Content = string(this.Ctx.Input.RequestBody)
  399. chartLog.Status = "新增持仓分析图表"
  400. chartLog.Method = this.Ctx.Input.URI()
  401. go data_manage.AddChartInfoLog(chartLog)
  402. }
  403. resp := new(data_manage.AddChartInfoResp)
  404. resp.ChartInfoId = chartInfo.ChartInfoId
  405. resp.UniqueCode = chartInfo.UniqueCode
  406. resp.ChartType = chartInfo.ChartType
  407. br.Ret = 200
  408. br.Success = true
  409. br.Msg = "保存成功"
  410. br.Data = resp
  411. br.IsAddLog = true
  412. }
  413. // Edit
  414. // @Title 编辑图表接口
  415. // @Description 编辑图表接口
  416. // @Param request body data_manage.EditChartInfoReq true "type json string"
  417. // @Success Ret=200 保存成功
  418. // @router /warehouse/chart/edit [post]
  419. func (this *WarehouseController) Edit() {
  420. br := new(models.BaseResponse).Init()
  421. defer func() {
  422. if br.ErrMsg == "" {
  423. br.IsSendEmail = false
  424. }
  425. this.Data["json"] = br
  426. this.ServeJSON()
  427. }()
  428. sysUser := this.SysUser
  429. if sysUser == nil {
  430. br.Msg = "请登录"
  431. br.ErrMsg = "请登录,SysUser Is Empty"
  432. br.Ret = 408
  433. return
  434. }
  435. var req data_manage.EditChartInfoReq
  436. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  437. br.Msg = "参数解析失败"
  438. br.ErrMsg = fmt.Sprintf("参数解析失败, %v", e)
  439. return
  440. }
  441. // 图表校验
  442. req.ChartName = strings.Trim(req.ChartName, " ")
  443. if req.ChartName == "" {
  444. br.Msg = "请填写图表名称"
  445. return
  446. }
  447. if req.ChartClassifyId <= 0 {
  448. br.Msg = "请选择分类"
  449. return
  450. }
  451. var extraConfig tradeAnalysisModel.WarehouseExtraConfig
  452. if req.ExtraConfig == `` {
  453. br.Msg = "配置有误"
  454. br.ErrMsg = fmt.Sprintf("建仓图表配置有误, conf: %s", req.ExtraConfig)
  455. return
  456. }
  457. if e := json.Unmarshal([]byte(req.ExtraConfig), &extraConfig); e != nil {
  458. br.Msg = "配置有误"
  459. br.ErrMsg = fmt.Sprintf("建仓图表配置解析失败, err: %v; conf: %s", e, req.ExtraConfig)
  460. return
  461. }
  462. pass, tips := tradeAnalysisService.CheckWarehouseChartExtraConfig(extraConfig)
  463. if !pass {
  464. br.Msg = tips
  465. return
  466. }
  467. // 图表校验
  468. chartItem, e := data_manage.GetChartInfoById(req.ChartInfoId)
  469. if e != nil {
  470. if e.Error() == utils.ErrNoRow() {
  471. br.Msg = "图表不存在, 请刷新页面"
  472. return
  473. }
  474. br.Msg = "操作失败"
  475. br.ErrMsg = fmt.Sprintf("获取图表信息失败, %v", e)
  476. return
  477. }
  478. if chartItem.Source != utils.CHART_SOURCE_TRADE_ANALYSIS_PROCESS {
  479. br.Msg = "图表类型异常"
  480. br.ErrMsg = fmt.Sprintf("图表类型异常: %d", chartItem.Source)
  481. return
  482. }
  483. // 图表操作权限
  484. ok := data.CheckOpChartPermission(sysUser, chartItem.SysUserId, true)
  485. if !ok {
  486. br.Msg = "没有该图表的操作权限"
  487. return
  488. }
  489. // 校验图例
  490. if len(req.ChartEdbInfoList) == 0 {
  491. br.Msg = "请填写图例"
  492. return
  493. }
  494. if len(req.ChartEdbInfoList) != len(extraConfig.Companies) {
  495. br.Msg = "图例不一致"
  496. br.ErrMsg = fmt.Sprintf("图例不一致, len: %d, companies: %d", len(req.ChartEdbInfoList), len(extraConfig.Companies))
  497. return
  498. }
  499. flagExist := make(map[string]bool)
  500. for _, v := range req.ChartEdbInfoList {
  501. if v.UniqueFlag == "" {
  502. br.Msg = ""
  503. }
  504. if flagExist[v.UniqueFlag] {
  505. br.Msg = "图例异常"
  506. br.ErrMsg = fmt.Sprintf("建仓图表图例异常, %s", v.UniqueFlag)
  507. return
  508. }
  509. }
  510. // 校验分类
  511. _, e = data_manage.GetChartClassifyById(req.ChartClassifyId)
  512. if e != nil {
  513. if e.Error() == utils.ErrNoRow() {
  514. br.Msg = "分类不存在, 请刷新页面"
  515. return
  516. }
  517. br.Msg = "操作失败"
  518. br.ErrMsg = fmt.Sprintf("获取分类失败, %v", e)
  519. return
  520. }
  521. // 校验图表是否存在
  522. {
  523. var cond string
  524. var pars []interface{}
  525. cond += " AND chart_info_id <> ? "
  526. pars = append(pars, req.ChartInfoId)
  527. switch this.Lang {
  528. case utils.EnLangVersion:
  529. cond += " AND chart_name_en = ? AND source = ? "
  530. default:
  531. cond += " AND chart_name = ? AND source = ? "
  532. }
  533. pars = append(pars, req.ChartName, utils.CHART_SOURCE_TRADE_ANALYSIS_PROCESS)
  534. count, e := data_manage.GetChartInfoCountByCondition(cond, pars)
  535. if e != nil {
  536. br.Msg = "操作失败"
  537. br.ErrMsg = fmt.Sprintf("获取同名图表计数失败, %v", e)
  538. return
  539. }
  540. if count > 0 {
  541. br.Msg = "图表名称已存在, 请重新填写"
  542. return
  543. }
  544. }
  545. // 更新图表
  546. chartItem, e = tradeAnalysisService.EditWarehouseChart(req)
  547. if e != nil {
  548. br.Msg = "操作失败"
  549. br.ErrMsg = fmt.Sprintf("新增持仓分析图表失败, %v", e)
  550. return
  551. }
  552. resp := new(data_manage.AddChartInfoResp)
  553. resp.ChartInfoId = chartItem.ChartInfoId
  554. resp.UniqueCode = chartItem.UniqueCode
  555. resp.ChartType = req.ChartType
  556. // 新增操作日志
  557. {
  558. chartLog := new(data_manage.ChartInfoLog)
  559. chartLog.ChartName = chartItem.ChartName
  560. chartLog.ChartInfoId = req.ChartInfoId
  561. chartLog.ChartClassifyId = chartItem.ChartClassifyId
  562. chartLog.SysUserId = sysUser.AdminId
  563. chartLog.SysUserRealName = sysUser.RealName
  564. chartLog.UniqueCode = chartItem.UniqueCode
  565. chartLog.CreateTime = time.Now()
  566. chartLog.Content = string(this.Ctx.Input.RequestBody)
  567. chartLog.Status = "编辑持仓分析图表"
  568. chartLog.Method = this.Ctx.Input.URL()
  569. go data_manage.AddChartInfoLog(chartLog)
  570. }
  571. br.Ret = 200
  572. br.Success = true
  573. br.Msg = "保存成功"
  574. br.Data = resp
  575. br.IsAddLog = true
  576. }
  577. // Detail
  578. // @Title 获取图表详情
  579. // @Description 获取图表详情接口
  580. // @Param ChartInfoId query int true "图表id"
  581. // @Param DateType query int true "日期类型:1:00年至今,2:10年至今,3:15年至今,4:年初至今,5:自定义时间"
  582. // @Param StartDate query string true "自定义开始日期"
  583. // @Param EndDate query string true "自定义结束日期"
  584. // @Param Calendar query string true "公历/农历"
  585. // @Param SeasonStartDate query string true "季节性图开始日期"
  586. // @Param SeasonEndDate query string true "季节性图结束日期"
  587. // @Param EdbInfoId query string true "指标ID,多个用英文逗号隔开"
  588. // @Param ChartType query int true "生成样式:1:曲线图,2:季节性图"
  589. // @Success 200 {object} data_manage.ChartInfoDetailResp
  590. // @router /warehouse/chart/detail [get]
  591. func (this *WarehouseController) Detail() {
  592. br := new(models.BaseResponse).Init()
  593. defer func() {
  594. if br.ErrMsg == "" {
  595. br.IsSendEmail = false
  596. }
  597. this.Data["json"] = br
  598. this.ServeJSON()
  599. }()
  600. sysUser := this.SysUser
  601. if sysUser == nil {
  602. br.Msg = "请登录"
  603. br.ErrMsg = "请登录,SysUser Is Empty"
  604. br.Ret = 408
  605. return
  606. }
  607. chartInfoId, _ := this.GetInt("ChartInfoId")
  608. if chartInfoId <= 0 {
  609. br.Msg = "参数有误"
  610. return
  611. }
  612. dateType, _ := this.GetInt("DateType")
  613. startDate := this.GetString("StartDate")
  614. endDate := this.GetString("EndDate")
  615. dateTypeNum, _ := this.GetInt("DateTypeNum")
  616. var err error
  617. chartInfo := new(data_manage.ChartInfoView)
  618. chartInfo, err = data_manage.GetChartInfoViewById(chartInfoId)
  619. if err != nil {
  620. if err.Error() == utils.ErrNoRow() {
  621. br.Msg = "图被删除,请刷新页面"
  622. br.ErrMsg = "图被删除,请刷新页面,Err:" + err.Error()
  623. return
  624. }
  625. br.Msg = "获取失败"
  626. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  627. return
  628. }
  629. if dateType <= 0 {
  630. dateType = chartInfo.DateType
  631. }
  632. if startDate == "" {
  633. startDate = chartInfo.StartDate
  634. }
  635. if endDate == "" {
  636. endDate = chartInfo.EndDate
  637. }
  638. if dateTypeNum <= 0 {
  639. dateTypeNum = chartInfo.DateTypeNum
  640. }
  641. // 持仓分析图表配置校验
  642. var extraConfig tradeAnalysisModel.WarehouseExtraConfig
  643. if chartInfo.ExtraConfig == `` {
  644. br.Msg = "配置信息错误"
  645. return
  646. }
  647. if e := json.Unmarshal([]byte(chartInfo.ExtraConfig), &extraConfig); e != nil {
  648. br.Msg = "配置信息错误"
  649. br.ErrMsg = fmt.Sprintf("图表配置信息错误, %v", e)
  650. return
  651. }
  652. // 获取图表数据
  653. companyTradeData, e := tradeAnalysisService.GetWarehouseTradeData(extraConfig.Exchange, extraConfig.ClassifyName, extraConfig.Contracts, extraConfig.Companies, extraConfig.PredictRatio)
  654. if e != nil {
  655. br.Msg = "获取失败"
  656. br.ErrMsg = fmt.Sprintf("获取期货公司持仓加总数据失败, %v", e)
  657. return
  658. }
  659. var chartConfig tradeAnalysisModel.WarehouseChartPars
  660. chartConfig.WarehouseChartType = extraConfig.WarehouseChartType
  661. chartConfig.DateType = dateType
  662. chartConfig.StartDate = startDate
  663. chartConfig.EndDate = endDate
  664. chartConfig.DateTypeNum = dateTypeNum
  665. // 获取图表的图例设置
  666. seriesList, e := data_manage.GetChartSeriesByChartInfoId(chartInfoId)
  667. if e != nil {
  668. br.Msg = "获取失败"
  669. br.ErrMsg = fmt.Sprintf("获取图例失败, %v", e)
  670. return
  671. }
  672. saveItems := make([]*data_manage.ChartSaveItem, 0)
  673. for _, v := range seriesList {
  674. saveItems = append(saveItems, &data_manage.ChartSaveItem{
  675. UniqueFlag: v.UniqueFlag,
  676. EdbAliasName: v.SeriesName,
  677. IsAxis: v.IsAxis,
  678. })
  679. }
  680. chartConfig.ChartEdbInfoList = saveItems
  681. // 查询配置关联的指标
  682. multiEdb := make([]*tradeAnalysisModel.WarehouseEdbSaveItem, 0)
  683. if extraConfig.MultipleGraphConfigId > 0 {
  684. multiEdbList, e := data_manage.GetMultipleGraphConfigEdbMappingListByIdAndSource(extraConfig.MultipleGraphConfigId, utils.CHART_SOURCE_TRADE_ANALYSIS_PROCESS)
  685. if e != nil {
  686. br.Msg = "获取失败"
  687. br.ErrMsg = fmt.Sprintf("获取配置与图表指标关系失败, %v", e)
  688. return
  689. }
  690. var edbIds []int
  691. for _, v := range multiEdbList {
  692. edbIds = append(edbIds, v.EdbInfoId)
  693. }
  694. if len(edbIds) > 0 {
  695. edbList, e := data_manage.GetEdbInfoByIdList(edbIds)
  696. if e != nil {
  697. br.Msg = "获取失败"
  698. br.ErrMsg = fmt.Sprintf("获取配置关联指标信息失败, %v", e)
  699. return
  700. }
  701. for _, v := range edbList {
  702. if v.CalculateFormula == "" {
  703. utils.FileLog.Info(fmt.Sprintf("持仓分析图表详情-指标计算公式为空, EdbInfoId: %d", v.EdbInfoId))
  704. continue
  705. }
  706. var conf tradeAnalysisModel.WarehouseExtraConfig
  707. if e = json.Unmarshal([]byte(v.CalculateFormula), &conf); e != nil {
  708. utils.FileLog.Info(fmt.Sprintf("持仓分析图表详情-指标计算公式解析失败, EdbInfoId: %d, err: %v", v.EdbInfoId, e))
  709. continue
  710. }
  711. if len(conf.Companies) != 1 {
  712. utils.FileLog.Info(fmt.Sprintf("持仓分析图表详情-指标计算公式信息有误, EdbInfoId: %d", v.EdbInfoId))
  713. continue
  714. }
  715. item := new(tradeAnalysisModel.WarehouseEdbSaveItem)
  716. item.EdbInfoId = v.EdbInfoId
  717. item.EdbName = v.EdbName
  718. item.Unit = v.Unit
  719. item.Frequency = v.Frequency
  720. item.ClassifyId = v.ClassifyId
  721. item.UniqueFlag = conf.Companies[0]
  722. if conf.WarehouseChartType == extraConfig.WarehouseChartType {
  723. multiEdb = append(multiEdb, item)
  724. }
  725. }
  726. }
  727. }
  728. chartResp, e := tradeAnalysisService.GetWarehouseChartResp(chartInfo, companyTradeData, multiEdb, extraConfig, chartConfig)
  729. if e != nil {
  730. br.Msg = "获取失败"
  731. br.ErrMsg = fmt.Sprintf("获取图表详情失败, %v", e)
  732. return
  733. }
  734. resp := chartResp
  735. // 判断是否加入我的图库
  736. if chartInfoId > 0 && chartInfo != nil {
  737. {
  738. var myChartCondition string
  739. var myChartPars []interface{}
  740. myChartCondition += ` AND a.admin_id=? `
  741. myChartPars = append(myChartPars, sysUser.AdminId)
  742. myChartCondition += ` AND a.chart_info_id=? `
  743. myChartPars = append(myChartPars, chartInfo.ChartInfoId)
  744. myChartList, err := data_manage.GetMyChartByCondition(myChartCondition, myChartPars)
  745. if err != nil && err.Error() != utils.ErrNoRow() {
  746. br.Msg = "获取失败"
  747. br.ErrMsg = "获取我的图表信息失败,GetMyChartByCondition,Err:" + err.Error()
  748. return
  749. }
  750. if myChartList != nil && len(myChartList) > 0 {
  751. chartInfo.IsAdd = true
  752. chartInfo.MyChartId = myChartList[0].MyChartId
  753. chartInfo.MyChartClassifyId = myChartList[0].MyChartClassifyId
  754. }
  755. }
  756. }
  757. // 图表操作权限
  758. edbList := make([]*data_manage.ChartEdbInfoMapping, 0)
  759. chartInfo.IsEdit = data.CheckOpChartPermission(sysUser, chartInfo.SysUserId, true)
  760. // 判断是否需要展示英文标识
  761. //chartInfo.IsEnChart = data.CheckIsEnChart(chartInfo.ChartNameEn, edbList, chartInfo.Source, chartInfo.ChartType)
  762. // 数据来源
  763. sourceNameList, sourceNameEnList := data.GetEdbSourceByEdbInfoIdList(edbList)
  764. sourceNameList = append(sourceNameList, utils.SourceNameTradeAnalysis)
  765. sourceNameEnList = append(sourceNameEnList, utils.SourceNameTradeAnalysis)
  766. chartInfo.ChartSource = strings.Join(sourceNameList, ",")
  767. chartInfo.ChartSourceEn = strings.Join(sourceNameEnList, ",")
  768. // 另存为
  769. chartInfo.Button = data_manage.ChartViewButton{
  770. IsEdit: chartInfo.IsEdit,
  771. IsEnChart: chartInfo.IsEnChart,
  772. IsAdd: chartInfo.IsAdd,
  773. IsCopy: chartInfo.IsEdit,
  774. IsSetName: chartInfo.IsSetName,
  775. }
  776. // 图表当前分类的分类树
  777. classifyLevels := make([]string, 0)
  778. {
  779. list, e := data_manage.GetChartClassifyAllBySource(utils.CHART_SOURCE_TRADE_ANALYSIS_PROCESS)
  780. if e != nil {
  781. br.Msg = "获取失败"
  782. br.ErrMsg = fmt.Sprintf("获取图表分类失败, Err: %v", e)
  783. return
  784. }
  785. parents := data.GetChartClassifyParentRecursive(list, chartInfo.ChartClassifyId)
  786. sort.Slice(parents, func(i, j int) bool {
  787. return parents[i].Level < parents[i].Level
  788. })
  789. for _, v := range parents {
  790. classifyLevels = append(classifyLevels, v.UniqueCode)
  791. }
  792. }
  793. resp.ClassifyLevels = classifyLevels
  794. br.Ret = 200
  795. br.Success = true
  796. br.Msg = "获取成功"
  797. br.Data = resp
  798. }
  799. // List
  800. // @Title 持仓分析图表列表接口
  801. // @Description 持仓分析图表列表接口
  802. // @Param PageSize query int true "每页数据条数"
  803. // @Param CurrentIndex query int true "当前页页码,从1开始"
  804. // @Param ChartClassifyId query int true "分类id"
  805. // @Param Keyword query string true "搜索关键词"
  806. // @Param IsShowMe query bool true "是否只看我的,true、false"
  807. // @Success 200 {object} data_manage.ChartListResp
  808. // @router /warehouse/chart/list [get]
  809. func (this *WarehouseController) List() {
  810. br := new(models.BaseResponse).Init()
  811. defer func() {
  812. if br.ErrMsg == "" {
  813. br.IsSendEmail = false
  814. }
  815. this.Data["json"] = br
  816. this.ServeJSON()
  817. }()
  818. sysUser := this.SysUser
  819. if sysUser == nil {
  820. br.Msg = "请登录"
  821. br.ErrMsg = "请登录,SysUser Is Empty"
  822. br.Ret = 408
  823. return
  824. }
  825. chartClassifyId, _ := this.GetInt("ChartClassifyId")
  826. pageSize, _ := this.GetInt("PageSize")
  827. currentIndex, _ := this.GetInt("CurrentIndex")
  828. keyword := this.GetString("KeyWord")
  829. var total int
  830. page := paging.GetPaging(currentIndex, pageSize, total)
  831. var startSize int
  832. if pageSize <= 0 {
  833. pageSize = utils.PageSize20
  834. }
  835. if currentIndex <= 0 {
  836. currentIndex = 1
  837. }
  838. startSize = paging.StartIndex(currentIndex, pageSize)
  839. source := utils.CHART_SOURCE_TRADE_ANALYSIS_PROCESS
  840. var condition string
  841. var pars []interface{}
  842. // 普通图表
  843. condition += ` AND source = ? `
  844. pars = append(pars, source)
  845. if chartClassifyId > 0 {
  846. classifyIds, err := data_manage.GetChartClassify(chartClassifyId)
  847. if err != nil && err.Error() != utils.ErrNoRow() {
  848. br.Msg = "获取图表信息失败"
  849. br.ErrMsg = "获取信息失败,GetChartClassify,Err:" + err.Error()
  850. return
  851. }
  852. if classifyIds == "" {
  853. resp := new(data_manage.ChartListResp)
  854. page = paging.GetPaging(currentIndex, pageSize, 0)
  855. resp.Paging = page
  856. resp.List = make([]*data_manage.ChartInfoView, 0)
  857. br.Ret = 200
  858. br.Success = true
  859. br.Msg = "获取成功"
  860. br.Data = resp
  861. return
  862. }
  863. condition += " AND chart_classify_id IN(" + classifyIds + ") "
  864. }
  865. if keyword != "" {
  866. //将关键词按照空格分割
  867. keywords := strings.Split(keyword, " ")
  868. condition += ` AND ( chart_name LIKE '%` + keywords[0] + `%' `
  869. for k, key := range keywords {
  870. if k == 0 {
  871. continue
  872. }
  873. condition += ` OR chart_name LIKE '%` + key + `%' `
  874. }
  875. condition += ` )`
  876. }
  877. //只看我的
  878. isShowMe, _ := this.GetBool("IsShowMe")
  879. if isShowMe {
  880. condition += ` AND sys_user_id = ? `
  881. pars = append(pars, sysUser.AdminId)
  882. }
  883. // 获取当前账号的不可见指标
  884. noPermissionChartIdList := make([]int, 0)
  885. {
  886. obj := data_manage.EdbInfoNoPermissionAdmin{}
  887. confList, err := obj.GetAllChartListByAdminId(this.SysUser.AdminId)
  888. if err != nil && err.Error() != utils.ErrNoRow() {
  889. br.Msg = "获取失败"
  890. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
  891. return
  892. }
  893. for _, v := range confList {
  894. noPermissionChartIdList = append(noPermissionChartIdList, v.ChartInfoId)
  895. }
  896. }
  897. lenNoPermissionChartIdList := len(noPermissionChartIdList)
  898. if lenNoPermissionChartIdList > 0 {
  899. condition += ` AND chart_info_id not in (` + utils.GetOrmInReplace(lenNoPermissionChartIdList) + `) `
  900. pars = append(pars, noPermissionChartIdList)
  901. }
  902. //获取图表信息
  903. list, err := data_manage.GetChartListByCondition(condition, pars, startSize, pageSize)
  904. if err != nil && err.Error() != utils.ErrNoRow() {
  905. br.Success = true
  906. br.Msg = "获取图表信息失败"
  907. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  908. return
  909. }
  910. myChartList, err := data_manage.GetMyChartListByAdminId(sysUser.AdminId)
  911. if err != nil && err.Error() != utils.ErrNoRow() {
  912. br.Msg = "获取图表信息失败"
  913. br.ErrMsg = "获取我的图表信息失败,Err:" + err.Error()
  914. return
  915. }
  916. myChartMap := make(map[int]*data_manage.MyChartView)
  917. for _, v := range myChartList {
  918. myChartMap[v.ChartInfoId] = v
  919. }
  920. listLen := len(list)
  921. chartEdbMap := make(map[int][]*data_manage.ChartEdbInfoMapping)
  922. if listLen > 0 {
  923. chartInfoIds := ""
  924. for _, v := range list {
  925. chartInfoIds += strconv.Itoa(v.ChartInfoId) + ","
  926. }
  927. if chartInfoIds != "" {
  928. chartInfoIds = strings.Trim(chartInfoIds, ",")
  929. //判断是否需要展示英文标识
  930. edbList, e := data_manage.GetChartEdbMappingListByChartInfoIds(chartInfoIds)
  931. if e != nil {
  932. br.Msg = "获取失败"
  933. br.ErrMsg = "获取图表,指标信息失败,Err:" + e.Error()
  934. return
  935. }
  936. for _, v := range edbList {
  937. chartEdbMap[v.ChartInfoId] = append(chartEdbMap[v.ChartInfoId], v)
  938. }
  939. }
  940. }
  941. for i := 0; i < listLen; i++ {
  942. //判断是否需要展示英文标识
  943. if edbTmpList, ok := chartEdbMap[list[i].ChartInfoId]; ok {
  944. list[i].IsEnChart = data.CheckIsEnChart(list[i].ChartNameEn, edbTmpList, list[i].Source, list[i].ChartType)
  945. }
  946. if existItem, ok := myChartMap[list[i].ChartInfoId]; ok {
  947. list[i].IsAdd = true
  948. list[i].MyChartId = existItem.MyChartId
  949. list[i].MyChartClassifyId = existItem.MyChartClassifyId
  950. }
  951. }
  952. resp := new(data_manage.ChartListResp)
  953. if list == nil || len(list) <= 0 || (err != nil && err.Error() == utils.ErrNoRow()) {
  954. items := make([]*data_manage.ChartInfoView, 0)
  955. resp.Paging = page
  956. resp.List = items
  957. br.Ret = 200
  958. br.Success = true
  959. br.Msg = "获取成功"
  960. return
  961. }
  962. dataCount, err := data_manage.GetChartListCountByCondition(condition, pars)
  963. if err != nil && err.Error() != utils.ErrNoRow() {
  964. br.Msg = "获取指标信息失败"
  965. br.ErrMsg = "获取指标数据总数失败,Err:" + err.Error()
  966. return
  967. }
  968. page = paging.GetPaging(currentIndex, pageSize, dataCount)
  969. resp.Paging = page
  970. resp.List = list
  971. br.Ret = 200
  972. br.Success = true
  973. br.Msg = "获取成功"
  974. br.Data = resp
  975. }
  976. // Copy
  977. // @Title 复制并新增图表接口
  978. // @Description 新增图表接口
  979. // @Params request body data_manage.CopyAddChartInfoReq true "type json string"
  980. // @Success 200 {object} data_manage.AddChartInfoResp
  981. // @router /warehouse/chart/copy [post]
  982. func (this *WarehouseController) Copy() {
  983. br := new(models.BaseResponse).Init()
  984. defer func() {
  985. if br.ErrMsg == "" {
  986. br.IsSendEmail = false
  987. }
  988. this.Data["json"] = br
  989. this.ServeJSON()
  990. }()
  991. sysUser := this.SysUser
  992. if sysUser == nil {
  993. br.Msg = "请登录"
  994. br.ErrMsg = "请登录,SysUser Is Empty"
  995. br.Ret = 408
  996. return
  997. }
  998. var req data_manage.CopyAddChartInfoReq
  999. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1000. if err != nil {
  1001. br.Msg = "参数解析异常!"
  1002. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1003. return
  1004. }
  1005. if req.ChartInfoId <= 0 {
  1006. br.Msg = "参数有误"
  1007. br.ErrMsg = fmt.Sprintf("参数有误, ChartInfoId: %d", req.ChartInfoId)
  1008. return
  1009. }
  1010. req.ChartName = strings.TrimSpace(req.ChartName)
  1011. if req.ChartName == "" {
  1012. br.Msg = "请输入图表名称"
  1013. return
  1014. }
  1015. if req.ChartClassifyId <= 0 {
  1016. br.Msg = "请选择图表分类"
  1017. return
  1018. }
  1019. deleteCache := true
  1020. cacheKey := "CACHE_CHART_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  1021. defer func() {
  1022. if deleteCache {
  1023. utils.Rc.Delete(cacheKey)
  1024. }
  1025. }()
  1026. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  1027. deleteCache = false
  1028. br.Msg = "系统处理中,请稍后重试!"
  1029. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  1030. return
  1031. }
  1032. chartSource := utils.CHART_SOURCE_TRADE_ANALYSIS_PROCESS
  1033. // 校验分类、图表名称
  1034. {
  1035. var cond string
  1036. var pars []interface{}
  1037. switch this.Lang {
  1038. case utils.EnLangVersion:
  1039. cond += " AND chart_name_en = ? AND source = ? "
  1040. default:
  1041. cond += " AND chart_name = ? AND source = ? "
  1042. }
  1043. pars = append(pars, req.ChartName, chartSource)
  1044. count, e := data_manage.GetChartInfoCountByCondition(cond, pars)
  1045. if e != nil {
  1046. br.Msg = "保存失败"
  1047. br.ErrMsg = fmt.Sprintf("获取同名图表失败, Err: %v", e)
  1048. return
  1049. }
  1050. if count > 0 {
  1051. br.Msg = "图表名称已存在, 请重新填写"
  1052. return
  1053. }
  1054. _, e = data_manage.GetChartClassifyById(req.ChartClassifyId)
  1055. if e != nil {
  1056. if e.Error() == utils.ErrNoRow() {
  1057. br.Msg = "分类不存在"
  1058. return
  1059. }
  1060. br.Msg = "保存失败"
  1061. br.ErrMsg = fmt.Sprintf("获取图表分类失败, Err: %v", e)
  1062. return
  1063. }
  1064. }
  1065. // 原图表
  1066. originChart, e := data_manage.GetChartInfoById(req.ChartInfoId)
  1067. if e != nil {
  1068. if e.Error() == utils.ErrNoRow() {
  1069. br.Msg = "原图表不存在"
  1070. return
  1071. }
  1072. br.Msg = "保存失败"
  1073. br.ErrMsg = fmt.Sprintf("获取原图表信息失败, Err: %v", e)
  1074. return
  1075. }
  1076. chartInfo := new(data_manage.ChartInfo)
  1077. newChart, e := tradeAnalysisService.CopyWarehouseChart(req.ChartClassifyId, req.ChartName, originChart, sysUser.AdminId, sysUser.RealName)
  1078. if e != nil {
  1079. br.Msg = "保存失败"
  1080. br.ErrMsg = fmt.Sprintf("复制图表失败, %v", e)
  1081. return
  1082. }
  1083. chartInfo = newChart
  1084. // 新增操作日志
  1085. {
  1086. chartLog := new(data_manage.ChartInfoLog)
  1087. chartLog.ChartInfoId = chartInfo.ChartInfoId
  1088. chartLog.ChartName = req.ChartName
  1089. chartLog.ChartClassifyId = req.ChartClassifyId
  1090. chartLog.SysUserId = sysUser.AdminId
  1091. chartLog.SysUserRealName = sysUser.RealName
  1092. chartLog.UniqueCode = chartInfo.UniqueCode
  1093. chartLog.CreateTime = time.Now()
  1094. chartLog.Content = string(this.Ctx.Input.RequestBody)
  1095. chartLog.Status = "复制区间计算图表"
  1096. chartLog.Method = this.Ctx.Input.URI()
  1097. go data_manage.AddChartInfoLog(chartLog)
  1098. }
  1099. br.Ret = 200
  1100. br.Success = true
  1101. br.Msg = "保存成功"
  1102. br.Data = data_manage.AddChartInfoResp{
  1103. ChartInfoId: chartInfo.ChartInfoId,
  1104. UniqueCode: chartInfo.UniqueCode,
  1105. ChartType: chartInfo.ChartType,
  1106. }
  1107. br.IsAddLog = true
  1108. }
  1109. // Refresh
  1110. // @Title 图表刷新接口
  1111. // @Description 图表刷新接口
  1112. // @Param ChartInfoId query int true "图表id"
  1113. // @Param UniqueCode query string true "唯一code"
  1114. // @Success Ret=200 刷新成功
  1115. // @router /warehouse/chart/refresh [get]
  1116. func (this *WarehouseController) Refresh() {
  1117. br := new(models.BaseResponse).Init()
  1118. defer func() {
  1119. if br.ErrMsg == "" {
  1120. br.IsSendEmail = false
  1121. }
  1122. this.Data["json"] = br
  1123. this.ServeJSON()
  1124. }()
  1125. sysUser := this.SysUser
  1126. if sysUser == nil {
  1127. br.Msg = "请登录"
  1128. br.ErrMsg = "请登录,SysUser Is Empty"
  1129. br.Ret = 408
  1130. return
  1131. }
  1132. chartInfoId, _ := this.GetInt("ChartInfoId")
  1133. uniqueCode := this.GetString("UniqueCode")
  1134. if chartInfoId <= 0 && uniqueCode == `` {
  1135. br.Msg = "参数错误"
  1136. br.ErrMsg = "参数错误:chartInfoId:" + strconv.Itoa(chartInfoId) + ",UniqueCode:" + uniqueCode
  1137. return
  1138. }
  1139. var chartInfo *data_manage.ChartInfo
  1140. var err error
  1141. if chartInfoId > 0 {
  1142. chartInfo, err = data_manage.GetChartInfoById(chartInfoId)
  1143. } else {
  1144. chartInfo, err = data_manage.GetChartInfoByUniqueCode(uniqueCode)
  1145. }
  1146. if err != nil {
  1147. if err.Error() == utils.ErrNoRow() {
  1148. br.Msg = "图表已被删除,无需刷新"
  1149. br.ErrMsg = "获取指标信息失败,Err:" + err.Error()
  1150. return
  1151. }
  1152. br.Msg = "刷新失败"
  1153. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  1154. return
  1155. }
  1156. // 清除图表缓存
  1157. {
  1158. key := utils.HZ_CHART_LIB_DETAIL + chartInfo.UniqueCode
  1159. _ = utils.Rc.Delete(key)
  1160. }
  1161. br.Ret = 200
  1162. br.Success = true
  1163. br.Msg = "刷新成功"
  1164. }
  1165. // SearchByEs
  1166. // @Title 图表模糊搜索(从es获取)
  1167. // @Description 图表模糊搜索(从es获取)
  1168. // @Param Keyword query string true "图表名称"
  1169. // @Param IsShowMe query bool true "是否只看我的,true、false"
  1170. // @Param Source query int true "来源,3:相关性,4:滚动相关性,默认0:全部"
  1171. // @Success 200 {object} data_manage.ChartInfo
  1172. // @router /warehouse/chart/search_by_es [get]
  1173. func (this *WarehouseController) SearchByEs() {
  1174. br := new(models.BaseResponse).Init()
  1175. defer func() {
  1176. this.Data["json"] = br
  1177. this.ServeJSON()
  1178. }()
  1179. sysUser := this.SysUser
  1180. if sysUser == nil {
  1181. br.Msg = "请登录"
  1182. br.ErrMsg = "请登录,SysUser Is Empty"
  1183. br.Ret = 408
  1184. return
  1185. }
  1186. pageSize, _ := this.GetInt("PageSize")
  1187. currentIndex, _ := this.GetInt("CurrentIndex")
  1188. var startSize int
  1189. if pageSize <= 0 {
  1190. pageSize = utils.PageSize20
  1191. }
  1192. if currentIndex <= 0 {
  1193. currentIndex = 1
  1194. }
  1195. startSize = paging.StartIndex(currentIndex, pageSize)
  1196. keyword := this.GetString("Keyword")
  1197. //只看我的
  1198. isShowMe, _ := this.GetBool("IsShowMe")
  1199. showSysId := 0
  1200. if isShowMe {
  1201. showSysId = sysUser.AdminId
  1202. }
  1203. sourceList := make([]int, 0)
  1204. sourceList = append(sourceList, utils.CHART_SOURCE_TRADE_ANALYSIS_PROCESS)
  1205. var searchList []*data_manage.ChartInfoMore
  1206. var total int64
  1207. var err error
  1208. // 获取当前账号的不可见指标
  1209. noPermissionChartIdList := make([]int, 0)
  1210. {
  1211. obj := data_manage.EdbInfoNoPermissionAdmin{}
  1212. confList, err := obj.GetAllChartListByAdminId(this.SysUser.AdminId)
  1213. if err != nil && err.Error() != utils.ErrNoRow() {
  1214. br.Msg = "获取失败"
  1215. br.ErrMsg = "获取不可见指标配置数据失败,Err:" + err.Error()
  1216. return
  1217. }
  1218. for _, v := range confList {
  1219. noPermissionChartIdList = append(noPermissionChartIdList, v.ChartInfoId)
  1220. }
  1221. }
  1222. if keyword != "" {
  1223. searchList, total, err = data.EsSearchChartInfo(keyword, showSysId, sourceList, noPermissionChartIdList, startSize, pageSize)
  1224. } else {
  1225. total, searchList, err = data_manage.ChartInfoSearchByEmptyKeyWord(showSysId, sourceList, noPermissionChartIdList, startSize, pageSize)
  1226. if err != nil && err.Error() != utils.ErrNoRow() {
  1227. br.Msg = "获取失败"
  1228. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  1229. return
  1230. }
  1231. }
  1232. finalList := make([]*data_manage.ChartInfoMore, 0)
  1233. if len(searchList) > 0 {
  1234. chartInfoIds := ""
  1235. chartEdbMap := make(map[int][]*data_manage.ChartEdbInfoMapping)
  1236. for _, v := range searchList {
  1237. chartInfoIds += strconv.Itoa(v.ChartInfoId) + ","
  1238. }
  1239. if chartInfoIds != "" {
  1240. chartInfoIds = strings.Trim(chartInfoIds, ",")
  1241. //判断是否需要展示英文标识
  1242. edbList, e := data_manage.GetChartEdbMappingListByChartInfoIds(chartInfoIds)
  1243. if e != nil {
  1244. br.Msg = "获取失败"
  1245. br.ErrMsg = "获取图表,指标信息失败,Err:" + e.Error()
  1246. return
  1247. }
  1248. for _, v := range edbList {
  1249. chartEdbMap[v.ChartInfoId] = append(chartEdbMap[v.ChartInfoId], v)
  1250. }
  1251. }
  1252. for _, v := range searchList {
  1253. tmp := new(data_manage.ChartInfoMore)
  1254. tmp.ChartInfo = v.ChartInfo
  1255. // 图表数据权限
  1256. tmp.HaveOperaAuth = true
  1257. //判断是否需要展示英文标识
  1258. if edbTmpList, ok := chartEdbMap[v.ChartInfoId]; ok {
  1259. tmp.IsEnChart = data.CheckIsEnChart(v.ChartNameEn, edbTmpList, v.Source, v.ChartType)
  1260. }
  1261. tmp.SearchText = v.SearchText
  1262. if tmp.SearchText == "" {
  1263. tmp.SearchText = v.ChartName
  1264. }
  1265. finalList = append(finalList, tmp)
  1266. }
  1267. }
  1268. //新增搜索词记录
  1269. {
  1270. searchKeyword := new(data_manage.SearchKeyword)
  1271. searchKeyword.KeyWord = keyword
  1272. searchKeyword.CreateTime = time.Now()
  1273. go data_manage.AddSearchKeyword(searchKeyword)
  1274. }
  1275. page := paging.GetPaging(currentIndex, pageSize, int(total))
  1276. resp := data_manage.ChartInfoListByEsResp{
  1277. Paging: page,
  1278. List: finalList,
  1279. }
  1280. br.Ret = 200
  1281. br.Success = true
  1282. br.Msg = "获取成功"
  1283. br.Data = resp
  1284. }
  1285. // ChartInfoSave
  1286. // @Title 保存图表接口
  1287. // @Description 保存图表接口
  1288. // @Param request body data_manage.SaveChartInfoReq true "type json string"
  1289. // @Success Ret=200 返回图表id
  1290. // @router /warehouse/chart/save [post]
  1291. func (this *WarehouseController) ChartInfoSave() {
  1292. br := new(models.BaseResponse).Init()
  1293. defer func() {
  1294. this.Data["json"] = br
  1295. this.ServeJSON()
  1296. }()
  1297. sysUser := this.SysUser
  1298. if sysUser == nil {
  1299. br.Msg = "请登录"
  1300. br.ErrMsg = "请登录,SysUser Is Empty"
  1301. br.Ret = 408
  1302. return
  1303. }
  1304. var req data_manage.SaveChartInfoReq
  1305. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1306. if err != nil {
  1307. br.Msg = "参数解析异常!"
  1308. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1309. return
  1310. }
  1311. if req.ChartInfoId <= 0 {
  1312. br.Msg = "参数错误!"
  1313. return
  1314. }
  1315. chartItem, err := data_manage.GetChartInfoById(req.ChartInfoId)
  1316. if err != nil {
  1317. if err.Error() == utils.ErrNoRow() {
  1318. br.Msg = "图表已被删除,请刷新页面!"
  1319. br.ErrMsg = "图表已被删除,请刷新页面,ChartInfoId:" + strconv.Itoa(req.ChartInfoId)
  1320. return
  1321. }
  1322. br.Msg = "获取图表信息失败!"
  1323. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  1324. return
  1325. }
  1326. chartItem.StartDate = req.StartDate
  1327. chartItem.EndDate = req.EndDate
  1328. chartItem.DateType = req.DateType
  1329. chartItem.DateTypeNum = req.DateTypeNum
  1330. chartItem.LeftMin = req.LeftMin
  1331. chartItem.LeftMax = req.LeftMax
  1332. chartItem.RightMin = req.RightMin
  1333. chartItem.RightMax = req.RightMax
  1334. chartItem.Right2Min = req.Right2Min
  1335. chartItem.Right2Max = req.Right2Max
  1336. chartItem.MinMaxSave = req.MinMaxSave
  1337. updateCols := []string{"StartDate", "EndDate", "DateType", "DateTypeNum", "LeftMin", "LeftMax", "RightMin", "RightMax", "Right2Min", "Right2Max", "MinMaxSave"}
  1338. if e := chartItem.Update(updateCols); e != nil {
  1339. br.Msg = "保存失败"
  1340. br.ErrMsg = fmt.Sprintf("更新图表基础信息失败, %v", e)
  1341. return
  1342. }
  1343. key := utils.HZ_CHART_LIB_DETAIL + chartItem.UniqueCode
  1344. if utils.Re == nil && utils.Rc != nil {
  1345. _ = utils.Rc.Delete(key)
  1346. }
  1347. // 更新ES
  1348. go func() {
  1349. data.EsAddOrEditChartInfo(chartItem.ChartInfoId)
  1350. data.EsAddOrEditMyChartInfoByChartInfoId(chartItem.ChartInfoId)
  1351. }()
  1352. //新增操作日志
  1353. {
  1354. chartLog := new(data_manage.ChartInfoLog)
  1355. chartLog.ChartName = chartItem.ChartName
  1356. chartLog.ChartInfoId = req.ChartInfoId
  1357. chartLog.ChartClassifyId = chartItem.ChartClassifyId
  1358. chartLog.SysUserId = sysUser.AdminId
  1359. chartLog.SysUserRealName = sysUser.RealName
  1360. chartLog.UniqueCode = chartItem.UniqueCode
  1361. chartLog.CreateTime = time.Now()
  1362. chartLog.Content = string(this.Ctx.Input.RequestBody)
  1363. chartLog.Status = "修改配置项"
  1364. chartLog.Method = this.Ctx.Input.URI()
  1365. go data_manage.AddChartInfoLog(chartLog)
  1366. }
  1367. br.Ret = 200
  1368. br.Success = true
  1369. br.Msg = "保存成功"
  1370. br.IsAddLog = true
  1371. }
  1372. // EdbSaveCheck
  1373. // @Title 保存指标校验
  1374. // @Description 保存指标校验
  1375. // @Param request body request.WarehouseEdbSaveReq true "type json string"
  1376. // @Success Ret=200 返回指标id
  1377. // @router /warehouse/edb/save_check [post]
  1378. func (this *WarehouseController) EdbSaveCheck() {
  1379. br := new(models.BaseResponse).Init()
  1380. defer func() {
  1381. if br.ErrMsg == "" {
  1382. br.IsSendEmail = false
  1383. }
  1384. this.Data["json"] = br
  1385. this.ServeJSON()
  1386. }()
  1387. sysUser := this.SysUser
  1388. if sysUser == nil {
  1389. br.Msg = "请登录"
  1390. br.ErrMsg = "请登录,SysUser Is Empty"
  1391. br.Ret = 408
  1392. return
  1393. }
  1394. var req tradeAnalysisRequest.WarehouseEdbSaveReq
  1395. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  1396. br.Msg = "参数解析异常"
  1397. br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
  1398. return
  1399. }
  1400. // 参数校验
  1401. if !req.IsSaveAs && req.MultipleGraphConfigId <= 0 {
  1402. br.Msg = "多图配置有误"
  1403. br.ErrMsg = fmt.Sprintf("配置ID有误, %d", req.MultipleGraphConfigId)
  1404. return
  1405. }
  1406. //var extraConfig tradeAnalysisModel.WarehouseExtraConfig
  1407. extraConfig := *req.ExtraConfig
  1408. //if req.ExtraConfig == `` {
  1409. // br.Msg = "配置有误"
  1410. // br.ErrMsg = fmt.Sprintf("建仓图表配置有误, conf: %s", req.ExtraConfig)
  1411. // return
  1412. //}
  1413. //if e := json.Unmarshal([]byte(req.ExtraConfig), &extraConfig); e != nil {
  1414. // br.Msg = "配置有误"
  1415. // br.ErrMsg = fmt.Sprintf("建仓图表配置解析失败, err: %v; conf: %s", e, req.ExtraConfig)
  1416. // return
  1417. //}
  1418. pass, tips := tradeAnalysisService.CheckWarehouseChartExtraConfig(extraConfig)
  1419. if !pass {
  1420. br.Msg = tips
  1421. return
  1422. }
  1423. if extraConfig.WarehouseChartType <= 0 {
  1424. br.Msg = "配置类型有误"
  1425. br.ErrMsg = fmt.Sprintf("配置类型有误, %d", extraConfig.WarehouseChartType)
  1426. return
  1427. }
  1428. // 根据extraConfig与已绑定关系的指标进行比对, 看是否需要新增指标
  1429. multiEdbList, e := data_manage.GetMultipleGraphConfigEdbMappingListByIdAndSource(req.MultipleGraphConfigId, utils.CHART_SOURCE_TRADE_ANALYSIS_PROCESS)
  1430. if e != nil {
  1431. br.Msg = "操作失败"
  1432. br.ErrMsg = fmt.Sprintf("获取配置与图表指标关系失败, %v", e)
  1433. return
  1434. }
  1435. resp := make([]*tradeAnalysisModel.WarehouseEdbSaveItem, 0)
  1436. // 校验是否需要新增指标
  1437. newEdbList, _, e := tradeAnalysisService.CheckEdbSave(extraConfig, multiEdbList, req.IsSaveAs)
  1438. if e != nil {
  1439. br.Msg = "操作失败"
  1440. br.ErrMsg = fmt.Sprintf("校验指标是否需要新增失败, %v", e)
  1441. return
  1442. }
  1443. if len(newEdbList) > 0 {
  1444. resp = append(resp, newEdbList...)
  1445. }
  1446. br.Data = resp
  1447. br.Ret = 200
  1448. br.Success = true
  1449. br.Msg = "操作成功"
  1450. }
  1451. // EdbSave
  1452. // @Title 保存指标接口
  1453. // @Description 保存指标接口
  1454. // @Param request body request.WarehouseEdbSaveReq true "type json string"
  1455. // @Success Ret=200 返回指标id
  1456. // @router /warehouse/edb/save [post]
  1457. func (this *WarehouseController) EdbSave() {
  1458. br := new(models.BaseResponse).Init()
  1459. defer func() {
  1460. if br.ErrMsg == "" {
  1461. br.IsSendEmail = false
  1462. }
  1463. this.Data["json"] = br
  1464. this.ServeJSON()
  1465. }()
  1466. sysUser := this.SysUser
  1467. if sysUser == nil {
  1468. br.Msg = "请登录"
  1469. br.ErrMsg = "请登录,SysUser Is Empty"
  1470. br.Ret = 408
  1471. return
  1472. }
  1473. var req tradeAnalysisRequest.WarehouseEdbSaveReq
  1474. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  1475. br.Msg = "参数解析异常"
  1476. br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
  1477. return
  1478. }
  1479. cacheKey := "CACHE_CHART_EDB_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  1480. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  1481. br.Msg = "系统处理中,请稍后重试!"
  1482. return
  1483. }
  1484. defer func() {
  1485. _ = utils.Rc.Delete(cacheKey)
  1486. }()
  1487. // 参数校验
  1488. if !req.IsSaveAs && req.MultipleGraphConfigId <= 0 {
  1489. br.Msg = "多图配置有误"
  1490. br.ErrMsg = fmt.Sprintf("配置ID有误, %d", req.MultipleGraphConfigId)
  1491. return
  1492. }
  1493. extraConfig := *req.ExtraConfig
  1494. pass, tips := tradeAnalysisService.CheckWarehouseChartExtraConfig(extraConfig)
  1495. if !pass {
  1496. br.Msg = tips
  1497. return
  1498. }
  1499. if len(req.EdbInfoList) == 0 {
  1500. br.Msg = "请选择指标"
  1501. return
  1502. }
  1503. edbNameExist := make(map[string]bool)
  1504. for _, v := range req.EdbInfoList {
  1505. if v.UniqueFlag == "" {
  1506. br.Msg = "指标标识有误"
  1507. br.ErrMsg = fmt.Sprintf("指标标识有误, %s", v.UniqueFlag)
  1508. return
  1509. }
  1510. // 仅新增的时候校验下面的参数
  1511. if v.EdbInfoId > 0 {
  1512. continue
  1513. }
  1514. edbName := strings.TrimSpace(v.EdbName)
  1515. if edbName == "" {
  1516. br.Msg = "请输入指标名称"
  1517. return
  1518. }
  1519. if edbNameExist[edbName] {
  1520. br.Msg = "指标名称重复, 请重新输入"
  1521. return
  1522. }
  1523. edbNameExist[edbName] = true
  1524. if v.Unit == "" {
  1525. br.Msg = "请输入单位"
  1526. return
  1527. }
  1528. if v.Frequency == "" {
  1529. br.Msg = "请选择频度"
  1530. return
  1531. }
  1532. if v.ClassifyId <= 0 {
  1533. br.Msg = "请选择分类"
  1534. return
  1535. }
  1536. }
  1537. resp := tradeAnalysisResponse.WarehouseEdbSaveResp{
  1538. Fail: make([]*tradeAnalysisModel.WarehouseEdbSaveRespItem, 0),
  1539. Success: make([]*tradeAnalysisModel.WarehouseEdbSaveRespItem, 0),
  1540. }
  1541. // 获取需要移除关联的指标
  1542. multiEdbList, e := data_manage.GetMultipleGraphConfigEdbMappingListByIdAndSource(req.MultipleGraphConfigId, utils.CHART_SOURCE_TRADE_ANALYSIS_PROCESS)
  1543. if e != nil {
  1544. br.Msg = "操作失败"
  1545. br.ErrMsg = fmt.Sprintf("获取配置与图表指标关系失败, %v", e)
  1546. return
  1547. }
  1548. _, removeIds, e := tradeAnalysisService.CheckEdbSave(extraConfig, multiEdbList, req.IsSaveAs)
  1549. if e != nil {
  1550. br.Msg = "操作失败"
  1551. br.ErrMsg = fmt.Sprintf("校验指标是否需要新增失败, %v", e)
  1552. return
  1553. }
  1554. // 根据EdbInfoId判断是否需要更新指标
  1555. var newEdbIds []int
  1556. edbUpdateCols := []string{"CalculateFormula", "ModifyTime"}
  1557. newEdbRefresh := make([]*data_manage.EdbInfo, 0)
  1558. for _, v := range req.EdbInfoList {
  1559. // 图表配置-即指标计算公式
  1560. conf := extraConfig
  1561. conf.Companies = []string{v.UniqueFlag}
  1562. b, e := json.Marshal(conf)
  1563. if e != nil {
  1564. resp.Fail = append(resp.Fail, &tradeAnalysisModel.WarehouseEdbSaveRespItem{
  1565. WarehouseEdbSaveItem: *v,
  1566. Tips: "获取失败",
  1567. ErrMsg: fmt.Sprintf("指标配置JSON格式化异常, %v", e),
  1568. })
  1569. continue
  1570. }
  1571. formula := string(b)
  1572. //edb.CalculateFormula = string(b)
  1573. // 更新指标
  1574. edbInfo := new(data_manage.EdbInfo)
  1575. if v.EdbInfoId > 0 {
  1576. edb, e := data_manage.GetEdbInfoById(v.EdbInfoId)
  1577. if e != nil {
  1578. resp.Fail = append(resp.Fail, &tradeAnalysisModel.WarehouseEdbSaveRespItem{
  1579. WarehouseEdbSaveItem: *v,
  1580. Tips: "获取失败",
  1581. ErrMsg: fmt.Sprintf("获取失败, %v", e),
  1582. })
  1583. continue
  1584. }
  1585. edb.CalculateFormula = formula
  1586. edb.ModifyTime = time.Now().Local()
  1587. if e = edb.Update(edbUpdateCols); e != nil {
  1588. resp.Fail = append(resp.Fail, &tradeAnalysisModel.WarehouseEdbSaveRespItem{
  1589. WarehouseEdbSaveItem: *v,
  1590. Tips: "更新失败",
  1591. ErrMsg: fmt.Sprintf("更新指标计算公式失败, %v", e),
  1592. })
  1593. continue
  1594. }
  1595. edbInfo = edb
  1596. // 刷新指标数据
  1597. res, e := data.RefreshEdbData(edbInfo.EdbInfoId, edbInfo.Source, edbInfo.SubSource, edbInfo.EdbCode, utils.BASE_START_DATE)
  1598. if e != nil {
  1599. resp.Fail = append(resp.Fail, &tradeAnalysisModel.WarehouseEdbSaveRespItem{
  1600. WarehouseEdbSaveItem: *v,
  1601. Tips: "刷新失败",
  1602. ErrMsg: fmt.Sprintf("刷新失败, %v", e),
  1603. })
  1604. continue
  1605. }
  1606. if res.Ret != 200 {
  1607. resp.Fail = append(resp.Fail, &tradeAnalysisModel.WarehouseEdbSaveRespItem{
  1608. WarehouseEdbSaveItem: *v,
  1609. Tips: "刷新失败",
  1610. ErrMsg: fmt.Sprintf("刷新失败, Ret: %d, err: %v", res.Ret, e),
  1611. })
  1612. continue
  1613. }
  1614. }
  1615. // 新增指标
  1616. if v.EdbInfoId == 0 {
  1617. edbCode, e := utils.GenerateEdbCode(1, "")
  1618. if e != nil {
  1619. br.Msg = "操作失败"
  1620. br.ErrMsg = fmt.Sprintf("生成指标编码失败, %v", e)
  1621. return
  1622. }
  1623. // PS:新增的开始结束日期为均为初始化日期
  1624. edb, e, errMsg, _ := data.EdbInfoAdd(utils.DATA_SOURCE_TRADE_ANALYSIS, utils.DATA_SUB_SOURCE_EDB, v.ClassifyId, edbCode, v.EdbName, v.Frequency, v.Unit, utils.BASE_START_DATE, utils.BASE_START_DATE, sysUser.AdminId, sysUser.RealName, this.Lang)
  1625. if e != nil {
  1626. resp.Fail = append(resp.Fail, &tradeAnalysisModel.WarehouseEdbSaveRespItem{
  1627. WarehouseEdbSaveItem: *v,
  1628. Tips: errMsg,
  1629. ErrMsg: fmt.Sprintf("新增失败, %v", e),
  1630. })
  1631. continue
  1632. }
  1633. edb.CalculateFormula = formula
  1634. edb.ModifyTime = time.Now().Local()
  1635. if e = edb.Update(edbUpdateCols); e != nil {
  1636. resp.Fail = append(resp.Fail, &tradeAnalysisModel.WarehouseEdbSaveRespItem{
  1637. WarehouseEdbSaveItem: *v,
  1638. Tips: "更新失败",
  1639. ErrMsg: fmt.Sprintf("更新指标计算公式失败, %v", e),
  1640. })
  1641. continue
  1642. }
  1643. newEdbIds = append(newEdbIds, edb.EdbInfoId)
  1644. edbInfo = edb
  1645. // 新增指标的刷新走异步吧, 指标如果没数据, 用户自己删了就行
  1646. newEdbRefresh = append(newEdbRefresh, edbInfo)
  1647. }
  1648. v.EdbInfoId = edbInfo.EdbInfoId
  1649. resp.Success = append(resp.Success, &tradeAnalysisModel.WarehouseEdbSaveRespItem{
  1650. WarehouseEdbSaveItem: *v,
  1651. Tips: "操作成功",
  1652. })
  1653. }
  1654. // 移除关联的指标
  1655. if len(removeIds) > 0 {
  1656. if e = data_manage.DeleteMultipleGraphConfigEdbMappingByEdbIds(req.MultipleGraphConfigId, utils.CHART_SOURCE_TRADE_ANALYSIS_PROCESS, removeIds); e != nil {
  1657. br.Msg = "操作失败"
  1658. br.ErrMsg = fmt.Sprintf("移除配置与指标关联失败, %v", e)
  1659. return
  1660. }
  1661. }
  1662. // 新增指标关联
  1663. if !req.IsSaveAs && len(newEdbIds) > 0 {
  1664. newMultiEdb := make([]*data_manage.MultipleGraphConfigEdbMapping, 0)
  1665. for _, v := range newEdbIds {
  1666. newMultiEdb = append(newMultiEdb, &data_manage.MultipleGraphConfigEdbMapping{
  1667. MultipleGraphConfigId: req.MultipleGraphConfigId,
  1668. EdbInfoId: v,
  1669. Source: utils.CHART_SOURCE_TRADE_ANALYSIS_PROCESS,
  1670. ModifyTime: time.Now(),
  1671. CreateTime: time.Now(),
  1672. })
  1673. }
  1674. if len(newMultiEdb) > 0 {
  1675. if e = data_manage.AddMultipleGraphConfigEdbMappingList(newMultiEdb); e != nil {
  1676. br.Msg = "操作失败"
  1677. br.ErrMsg = fmt.Sprintf("新增指标关联失败, %v", e)
  1678. return
  1679. }
  1680. }
  1681. }
  1682. // 异步刷新新增指标
  1683. if len(newEdbRefresh) > 0 {
  1684. go func() {
  1685. var refreshTips string
  1686. for _, edb := range newEdbRefresh {
  1687. _, e = data.RefreshEdbData(edb.EdbInfoId, edb.Source, edb.SubSource, edb.EdbCode, utils.BASE_START_DATE)
  1688. if e != nil {
  1689. refreshTips += fmt.Sprintf("刷新指标失败: %s, err: %v\n", edb.EdbCode, e)
  1690. }
  1691. }
  1692. if refreshTips != "" {
  1693. utils.FileLog.Info(refreshTips)
  1694. alarm_msg.SendAlarmMsg(refreshTips, 2)
  1695. }
  1696. }()
  1697. }
  1698. br.Data = resp
  1699. br.Ret = 200
  1700. br.Success = true
  1701. br.Msg = "保存成功"
  1702. br.IsAddLog = true
  1703. }