warehouse.go 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812
  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.ChartInfo
  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
  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. finalList = append(finalList, tmp)
  1262. }
  1263. }
  1264. //新增搜索词记录
  1265. {
  1266. searchKeyword := new(data_manage.SearchKeyword)
  1267. searchKeyword.KeyWord = keyword
  1268. searchKeyword.CreateTime = time.Now()
  1269. go data_manage.AddSearchKeyword(searchKeyword)
  1270. }
  1271. page := paging.GetPaging(currentIndex, pageSize, int(total))
  1272. resp := data_manage.ChartInfoListByEsResp{
  1273. Paging: page,
  1274. List: finalList,
  1275. }
  1276. br.Ret = 200
  1277. br.Success = true
  1278. br.Msg = "获取成功"
  1279. br.Data = resp
  1280. }
  1281. // ChartInfoSave
  1282. // @Title 保存图表接口
  1283. // @Description 保存图表接口
  1284. // @Param request body data_manage.SaveChartInfoReq true "type json string"
  1285. // @Success Ret=200 返回图表id
  1286. // @router /warehouse/chart/save [post]
  1287. func (this *WarehouseController) ChartInfoSave() {
  1288. br := new(models.BaseResponse).Init()
  1289. defer func() {
  1290. this.Data["json"] = br
  1291. this.ServeJSON()
  1292. }()
  1293. sysUser := this.SysUser
  1294. if sysUser == nil {
  1295. br.Msg = "请登录"
  1296. br.ErrMsg = "请登录,SysUser Is Empty"
  1297. br.Ret = 408
  1298. return
  1299. }
  1300. var req data_manage.SaveChartInfoReq
  1301. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1302. if err != nil {
  1303. br.Msg = "参数解析异常!"
  1304. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1305. return
  1306. }
  1307. if req.ChartInfoId <= 0 {
  1308. br.Msg = "参数错误!"
  1309. return
  1310. }
  1311. chartItem, err := data_manage.GetChartInfoById(req.ChartInfoId)
  1312. if err != nil {
  1313. if err.Error() == utils.ErrNoRow() {
  1314. br.Msg = "图表已被删除,请刷新页面!"
  1315. br.ErrMsg = "图表已被删除,请刷新页面,ChartInfoId:" + strconv.Itoa(req.ChartInfoId)
  1316. return
  1317. }
  1318. br.Msg = "获取图表信息失败!"
  1319. br.ErrMsg = "获取图表信息失败,Err:" + err.Error()
  1320. return
  1321. }
  1322. chartItem.StartDate = req.StartDate
  1323. chartItem.EndDate = req.EndDate
  1324. chartItem.DateType = req.DateType
  1325. chartItem.DateTypeNum = req.DateTypeNum
  1326. chartItem.LeftMin = req.LeftMin
  1327. chartItem.LeftMax = req.LeftMax
  1328. chartItem.RightMin = req.RightMin
  1329. chartItem.RightMax = req.RightMax
  1330. chartItem.Right2Min = req.Right2Min
  1331. chartItem.Right2Max = req.Right2Max
  1332. chartItem.MinMaxSave = req.MinMaxSave
  1333. updateCols := []string{"StartDate", "EndDate", "DateType", "DateTypeNum", "LeftMin", "LeftMax", "RightMin", "RightMax", "Right2Min", "Right2Max", "MinMaxSave"}
  1334. if e := chartItem.Update(updateCols); e != nil {
  1335. br.Msg = "保存失败"
  1336. br.ErrMsg = fmt.Sprintf("更新图表基础信息失败, %v", e)
  1337. return
  1338. }
  1339. key := utils.HZ_CHART_LIB_DETAIL + chartItem.UniqueCode
  1340. if utils.Re == nil && utils.Rc != nil {
  1341. _ = utils.Rc.Delete(key)
  1342. }
  1343. // 更新ES
  1344. go func() {
  1345. data.EsAddOrEditChartInfo(chartItem.ChartInfoId)
  1346. data.EsAddOrEditMyChartInfoByChartInfoId(chartItem.ChartInfoId)
  1347. }()
  1348. //新增操作日志
  1349. {
  1350. chartLog := new(data_manage.ChartInfoLog)
  1351. chartLog.ChartName = chartItem.ChartName
  1352. chartLog.ChartInfoId = req.ChartInfoId
  1353. chartLog.ChartClassifyId = chartItem.ChartClassifyId
  1354. chartLog.SysUserId = sysUser.AdminId
  1355. chartLog.SysUserRealName = sysUser.RealName
  1356. chartLog.UniqueCode = chartItem.UniqueCode
  1357. chartLog.CreateTime = time.Now()
  1358. chartLog.Content = string(this.Ctx.Input.RequestBody)
  1359. chartLog.Status = "修改配置项"
  1360. chartLog.Method = this.Ctx.Input.URI()
  1361. go data_manage.AddChartInfoLog(chartLog)
  1362. }
  1363. br.Ret = 200
  1364. br.Success = true
  1365. br.Msg = "保存成功"
  1366. br.IsAddLog = true
  1367. }
  1368. // EdbSaveCheck
  1369. // @Title 保存指标校验
  1370. // @Description 保存指标校验
  1371. // @Param request body request.WarehouseEdbSaveReq true "type json string"
  1372. // @Success Ret=200 返回指标id
  1373. // @router /warehouse/edb/save_check [post]
  1374. func (this *WarehouseController) EdbSaveCheck() {
  1375. br := new(models.BaseResponse).Init()
  1376. defer func() {
  1377. if br.ErrMsg == "" {
  1378. br.IsSendEmail = false
  1379. }
  1380. this.Data["json"] = br
  1381. this.ServeJSON()
  1382. }()
  1383. sysUser := this.SysUser
  1384. if sysUser == nil {
  1385. br.Msg = "请登录"
  1386. br.ErrMsg = "请登录,SysUser Is Empty"
  1387. br.Ret = 408
  1388. return
  1389. }
  1390. var req tradeAnalysisRequest.WarehouseEdbSaveReq
  1391. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  1392. br.Msg = "参数解析异常"
  1393. br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
  1394. return
  1395. }
  1396. // 参数校验
  1397. if !req.IsSaveAs && req.MultipleGraphConfigId <= 0 {
  1398. br.Msg = "多图配置有误"
  1399. br.ErrMsg = fmt.Sprintf("配置ID有误, %d", req.MultipleGraphConfigId)
  1400. return
  1401. }
  1402. //var extraConfig tradeAnalysisModel.WarehouseExtraConfig
  1403. extraConfig := *req.ExtraConfig
  1404. //if req.ExtraConfig == `` {
  1405. // br.Msg = "配置有误"
  1406. // br.ErrMsg = fmt.Sprintf("建仓图表配置有误, conf: %s", req.ExtraConfig)
  1407. // return
  1408. //}
  1409. //if e := json.Unmarshal([]byte(req.ExtraConfig), &extraConfig); e != nil {
  1410. // br.Msg = "配置有误"
  1411. // br.ErrMsg = fmt.Sprintf("建仓图表配置解析失败, err: %v; conf: %s", e, req.ExtraConfig)
  1412. // return
  1413. //}
  1414. pass, tips := tradeAnalysisService.CheckWarehouseChartExtraConfig(extraConfig)
  1415. if !pass {
  1416. br.Msg = tips
  1417. return
  1418. }
  1419. if extraConfig.WarehouseChartType <= 0 {
  1420. br.Msg = "配置类型有误"
  1421. br.ErrMsg = fmt.Sprintf("配置类型有误, %d", extraConfig.WarehouseChartType)
  1422. return
  1423. }
  1424. // 根据extraConfig与已绑定关系的指标进行比对, 看是否需要新增指标
  1425. multiEdbList, e := data_manage.GetMultipleGraphConfigEdbMappingListByIdAndSource(req.MultipleGraphConfigId, utils.CHART_SOURCE_TRADE_ANALYSIS_PROCESS)
  1426. if e != nil {
  1427. br.Msg = "操作失败"
  1428. br.ErrMsg = fmt.Sprintf("获取配置与图表指标关系失败, %v", e)
  1429. return
  1430. }
  1431. resp := make([]*tradeAnalysisModel.WarehouseEdbSaveItem, 0)
  1432. // 校验是否需要新增指标
  1433. newEdbList, _, e := tradeAnalysisService.CheckEdbSave(extraConfig, multiEdbList, req.IsSaveAs)
  1434. if e != nil {
  1435. br.Msg = "操作失败"
  1436. br.ErrMsg = fmt.Sprintf("校验指标是否需要新增失败, %v", e)
  1437. return
  1438. }
  1439. if len(newEdbList) > 0 {
  1440. resp = append(resp, newEdbList...)
  1441. }
  1442. br.Data = resp
  1443. br.Ret = 200
  1444. br.Success = true
  1445. br.Msg = "操作成功"
  1446. }
  1447. // EdbSave
  1448. // @Title 保存指标接口
  1449. // @Description 保存指标接口
  1450. // @Param request body request.WarehouseEdbSaveReq true "type json string"
  1451. // @Success Ret=200 返回指标id
  1452. // @router /warehouse/edb/save [post]
  1453. func (this *WarehouseController) EdbSave() {
  1454. br := new(models.BaseResponse).Init()
  1455. defer func() {
  1456. if br.ErrMsg == "" {
  1457. br.IsSendEmail = false
  1458. }
  1459. this.Data["json"] = br
  1460. this.ServeJSON()
  1461. }()
  1462. sysUser := this.SysUser
  1463. if sysUser == nil {
  1464. br.Msg = "请登录"
  1465. br.ErrMsg = "请登录,SysUser Is Empty"
  1466. br.Ret = 408
  1467. return
  1468. }
  1469. var req tradeAnalysisRequest.WarehouseEdbSaveReq
  1470. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  1471. br.Msg = "参数解析异常"
  1472. br.ErrMsg = fmt.Sprintf("参数解析异常, %v", e)
  1473. return
  1474. }
  1475. cacheKey := "CACHE_CHART_EDB_INFO_ADD_" + strconv.Itoa(sysUser.AdminId)
  1476. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  1477. br.Msg = "系统处理中,请稍后重试!"
  1478. return
  1479. }
  1480. defer func() {
  1481. _ = utils.Rc.Delete(cacheKey)
  1482. }()
  1483. // 参数校验
  1484. if !req.IsSaveAs && req.MultipleGraphConfigId <= 0 {
  1485. br.Msg = "多图配置有误"
  1486. br.ErrMsg = fmt.Sprintf("配置ID有误, %d", req.MultipleGraphConfigId)
  1487. return
  1488. }
  1489. extraConfig := *req.ExtraConfig
  1490. pass, tips := tradeAnalysisService.CheckWarehouseChartExtraConfig(extraConfig)
  1491. if !pass {
  1492. br.Msg = tips
  1493. return
  1494. }
  1495. if len(req.EdbInfoList) == 0 {
  1496. br.Msg = "请选择指标"
  1497. return
  1498. }
  1499. edbNameExist := make(map[string]bool)
  1500. for _, v := range req.EdbInfoList {
  1501. if v.UniqueFlag == "" {
  1502. br.Msg = "指标标识有误"
  1503. br.ErrMsg = fmt.Sprintf("指标标识有误, %s", v.UniqueFlag)
  1504. return
  1505. }
  1506. // 仅新增的时候校验下面的参数
  1507. if v.EdbInfoId > 0 {
  1508. continue
  1509. }
  1510. edbName := strings.TrimSpace(v.EdbName)
  1511. if edbName == "" {
  1512. br.Msg = "请输入指标名称"
  1513. return
  1514. }
  1515. if edbNameExist[edbName] {
  1516. br.Msg = "指标名称重复, 请重新输入"
  1517. return
  1518. }
  1519. edbNameExist[edbName] = true
  1520. if v.Unit == "" {
  1521. br.Msg = "请输入单位"
  1522. return
  1523. }
  1524. if v.Frequency == "" {
  1525. br.Msg = "请选择频度"
  1526. return
  1527. }
  1528. if v.ClassifyId <= 0 {
  1529. br.Msg = "请选择分类"
  1530. return
  1531. }
  1532. }
  1533. resp := tradeAnalysisResponse.WarehouseEdbSaveResp{
  1534. Fail: make([]*tradeAnalysisModel.WarehouseEdbSaveRespItem, 0),
  1535. Success: make([]*tradeAnalysisModel.WarehouseEdbSaveRespItem, 0),
  1536. }
  1537. // 获取需要移除关联的指标
  1538. multiEdbList, e := data_manage.GetMultipleGraphConfigEdbMappingListByIdAndSource(req.MultipleGraphConfigId, utils.CHART_SOURCE_TRADE_ANALYSIS_PROCESS)
  1539. if e != nil {
  1540. br.Msg = "操作失败"
  1541. br.ErrMsg = fmt.Sprintf("获取配置与图表指标关系失败, %v", e)
  1542. return
  1543. }
  1544. _, removeIds, e := tradeAnalysisService.CheckEdbSave(extraConfig, multiEdbList, req.IsSaveAs)
  1545. if e != nil {
  1546. br.Msg = "操作失败"
  1547. br.ErrMsg = fmt.Sprintf("校验指标是否需要新增失败, %v", e)
  1548. return
  1549. }
  1550. // 根据EdbInfoId判断是否需要更新指标
  1551. var newEdbIds []int
  1552. edbUpdateCols := []string{"CalculateFormula", "ModifyTime"}
  1553. newEdbRefresh := make([]*data_manage.EdbInfo, 0)
  1554. for _, v := range req.EdbInfoList {
  1555. // 图表配置-即指标计算公式
  1556. conf := extraConfig
  1557. conf.Companies = []string{v.UniqueFlag}
  1558. b, e := json.Marshal(conf)
  1559. if e != nil {
  1560. resp.Fail = append(resp.Fail, &tradeAnalysisModel.WarehouseEdbSaveRespItem{
  1561. WarehouseEdbSaveItem: *v,
  1562. Tips: "获取失败",
  1563. ErrMsg: fmt.Sprintf("指标配置JSON格式化异常, %v", e),
  1564. })
  1565. continue
  1566. }
  1567. formula := string(b)
  1568. //edb.CalculateFormula = string(b)
  1569. // 更新指标
  1570. edbInfo := new(data_manage.EdbInfo)
  1571. if v.EdbInfoId > 0 {
  1572. edb, e := data_manage.GetEdbInfoById(v.EdbInfoId)
  1573. if e != nil {
  1574. resp.Fail = append(resp.Fail, &tradeAnalysisModel.WarehouseEdbSaveRespItem{
  1575. WarehouseEdbSaveItem: *v,
  1576. Tips: "获取失败",
  1577. ErrMsg: fmt.Sprintf("获取失败, %v", e),
  1578. })
  1579. continue
  1580. }
  1581. edb.CalculateFormula = formula
  1582. edb.ModifyTime = time.Now().Local()
  1583. if e = edb.Update(edbUpdateCols); e != nil {
  1584. resp.Fail = append(resp.Fail, &tradeAnalysisModel.WarehouseEdbSaveRespItem{
  1585. WarehouseEdbSaveItem: *v,
  1586. Tips: "更新失败",
  1587. ErrMsg: fmt.Sprintf("更新指标计算公式失败, %v", e),
  1588. })
  1589. continue
  1590. }
  1591. edbInfo = edb
  1592. // 刷新指标数据
  1593. res, e := data.RefreshEdbData(edbInfo.EdbInfoId, edbInfo.Source, edbInfo.SubSource, edbInfo.EdbCode, utils.BASE_START_DATE)
  1594. if e != nil {
  1595. resp.Fail = append(resp.Fail, &tradeAnalysisModel.WarehouseEdbSaveRespItem{
  1596. WarehouseEdbSaveItem: *v,
  1597. Tips: "刷新失败",
  1598. ErrMsg: fmt.Sprintf("刷新失败, %v", e),
  1599. })
  1600. continue
  1601. }
  1602. if res.Ret != 200 {
  1603. resp.Fail = append(resp.Fail, &tradeAnalysisModel.WarehouseEdbSaveRespItem{
  1604. WarehouseEdbSaveItem: *v,
  1605. Tips: "刷新失败",
  1606. ErrMsg: fmt.Sprintf("刷新失败, Ret: %d, err: %v", res.Ret, e),
  1607. })
  1608. continue
  1609. }
  1610. }
  1611. // 新增指标
  1612. if v.EdbInfoId == 0 {
  1613. edbCode, e := utils.GenerateEdbCode(1, "")
  1614. if e != nil {
  1615. br.Msg = "操作失败"
  1616. br.ErrMsg = fmt.Sprintf("生成指标编码失败, %v", e)
  1617. return
  1618. }
  1619. // PS:新增的开始结束日期为均为初始化日期
  1620. 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)
  1621. if e != nil {
  1622. resp.Fail = append(resp.Fail, &tradeAnalysisModel.WarehouseEdbSaveRespItem{
  1623. WarehouseEdbSaveItem: *v,
  1624. Tips: errMsg,
  1625. ErrMsg: fmt.Sprintf("新增失败, %v", e),
  1626. })
  1627. continue
  1628. }
  1629. edb.CalculateFormula = formula
  1630. edb.ModifyTime = time.Now().Local()
  1631. if e = edb.Update(edbUpdateCols); e != nil {
  1632. resp.Fail = append(resp.Fail, &tradeAnalysisModel.WarehouseEdbSaveRespItem{
  1633. WarehouseEdbSaveItem: *v,
  1634. Tips: "更新失败",
  1635. ErrMsg: fmt.Sprintf("更新指标计算公式失败, %v", e),
  1636. })
  1637. continue
  1638. }
  1639. newEdbIds = append(newEdbIds, edb.EdbInfoId)
  1640. edbInfo = edb
  1641. // 新增指标的刷新走异步吧, 指标如果没数据, 用户自己删了就行
  1642. newEdbRefresh = append(newEdbRefresh, edbInfo)
  1643. }
  1644. v.EdbInfoId = edbInfo.EdbInfoId
  1645. resp.Success = append(resp.Success, &tradeAnalysisModel.WarehouseEdbSaveRespItem{
  1646. WarehouseEdbSaveItem: *v,
  1647. Tips: "操作成功",
  1648. })
  1649. }
  1650. // 移除关联的指标
  1651. if len(removeIds) > 0 {
  1652. if e = data_manage.DeleteMultipleGraphConfigEdbMappingByEdbIds(req.MultipleGraphConfigId, utils.CHART_SOURCE_TRADE_ANALYSIS_PROCESS, removeIds); e != nil {
  1653. br.Msg = "操作失败"
  1654. br.ErrMsg = fmt.Sprintf("移除配置与指标关联失败, %v", e)
  1655. return
  1656. }
  1657. }
  1658. // 新增指标关联
  1659. if !req.IsSaveAs && len(newEdbIds) > 0 {
  1660. newMultiEdb := make([]*data_manage.MultipleGraphConfigEdbMapping, 0)
  1661. for _, v := range newEdbIds {
  1662. newMultiEdb = append(newMultiEdb, &data_manage.MultipleGraphConfigEdbMapping{
  1663. MultipleGraphConfigId: req.MultipleGraphConfigId,
  1664. EdbInfoId: v,
  1665. Source: utils.CHART_SOURCE_TRADE_ANALYSIS_PROCESS,
  1666. ModifyTime: time.Now(),
  1667. CreateTime: time.Now(),
  1668. })
  1669. }
  1670. if len(newMultiEdb) > 0 {
  1671. if e = data_manage.AddMultipleGraphConfigEdbMappingList(newMultiEdb); e != nil {
  1672. br.Msg = "操作失败"
  1673. br.ErrMsg = fmt.Sprintf("新增指标关联失败, %v", e)
  1674. return
  1675. }
  1676. }
  1677. }
  1678. // 异步刷新新增指标
  1679. if len(newEdbRefresh) > 0 {
  1680. go func() {
  1681. var refreshTips string
  1682. for _, edb := range newEdbRefresh {
  1683. _, e = data.RefreshEdbData(edb.EdbInfoId, edb.Source, edb.SubSource, edb.EdbCode, utils.BASE_START_DATE)
  1684. if e != nil {
  1685. refreshTips += fmt.Sprintf("刷新指标失败: %s, err: %v\n", edb.EdbCode, e)
  1686. }
  1687. }
  1688. if refreshTips != "" {
  1689. utils.FileLog.Info(refreshTips)
  1690. alarm_msg.SendAlarmMsg(refreshTips, 2)
  1691. }
  1692. }()
  1693. }
  1694. br.Data = resp
  1695. br.Ret = 200
  1696. br.Success = true
  1697. br.Msg = "保存成功"
  1698. br.IsAddLog = true
  1699. }