base_from_radish_research.go 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. package data_manage
  2. import (
  3. "encoding/json"
  4. "eta/eta_api/controllers"
  5. "eta/eta_api/models"
  6. "eta/eta_api/models/data_manage"
  7. dataSourceModel "eta/eta_api/models/data_source"
  8. "eta/eta_api/models/system"
  9. "eta/eta_api/services/data"
  10. "eta/eta_api/services/elastic"
  11. etaTrialService "eta/eta_api/services/eta_trial"
  12. "eta/eta_api/utils"
  13. "fmt"
  14. "github.com/rdlucklib/rdluck_tools/paging"
  15. "github.com/tealeg/xlsx"
  16. "os"
  17. "path/filepath"
  18. "strconv"
  19. "strings"
  20. "time"
  21. )
  22. // BaseFromRadishResearchController 萝卜投研
  23. type BaseFromRadishResearchController struct {
  24. controllers.BaseAuthController
  25. }
  26. // IndexPageList
  27. // @Title 指标列表-分页
  28. // @Description 指标列表-分页
  29. // @Success 200 {object} data_manage.RadishResearchIndexPageListResp
  30. // @router /radish_research/index/page_list [get]
  31. func (this *BaseFromRadishResearchController) IndexPageList() {
  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 params data_manage.RadishResearchIndexListForm
  48. if e := this.ParseForm(&params); e != nil {
  49. br.Msg = "参数解析失败"
  50. br.ErrMsg = fmt.Sprintf("参数解析失败, err: %v", e)
  51. return
  52. }
  53. resp := new(data_manage.RadishResearchIndexPageListResp)
  54. resp.List = make([]*data_manage.BaseFromRadishResearchIndexItem, 0)
  55. // 分页查询
  56. var startSize int
  57. if params.PageSize <= 0 {
  58. params.PageSize = utils.PageSize20
  59. }
  60. if params.CurrentIndex <= 0 {
  61. params.CurrentIndex = 1
  62. }
  63. startSize = utils.StartIndex(params.CurrentIndex, params.PageSize)
  64. // 筛选项
  65. indexOb := new(data_manage.BaseFromRadishResearchIndex)
  66. var (
  67. cond string
  68. pars []interface{}
  69. classifyIds []int
  70. )
  71. params.ClassifyIds = strings.TrimSpace(params.ClassifyIds)
  72. params.Frequencies = strings.TrimSpace(params.Frequencies)
  73. params.Keyword = strings.TrimSpace(params.Keyword)
  74. if params.ClassifyIds != "" {
  75. idsArr := strings.Split(params.ClassifyIds, ",")
  76. for _, v := range idsArr {
  77. id, _ := strconv.Atoi(v)
  78. if id > 0 {
  79. classifyIds = append(classifyIds, id)
  80. }
  81. }
  82. if len(classifyIds) > 0 {
  83. cond += fmt.Sprintf(` AND %s IN ?`, indexOb.Cols().ClassifyId)
  84. pars = append(pars, classifyIds)
  85. }
  86. }
  87. if params.Frequencies != "" {
  88. freArr := strings.Split(params.Frequencies, ",")
  89. if len(freArr) > 0 {
  90. cond += fmt.Sprintf(` AND %s IN ?`, indexOb.Cols().Frequency)
  91. pars = append(pars, freArr)
  92. }
  93. }
  94. if params.Keyword != "" {
  95. kw := fmt.Sprint("%", params.Keyword, "%")
  96. cond += fmt.Sprintf(` AND (%s LIKE ? OR %s LIKE ?)`, indexOb.Cols().IndexName, indexOb.Cols().IndexCode)
  97. pars = append(pars, kw, kw)
  98. }
  99. if params.IgnoreEdbExist {
  100. cond += fmt.Sprintf(` AND %s = 0`, indexOb.Cols().EdbExist)
  101. }
  102. // 列表合计
  103. total, e := indexOb.GetCountByCondition(cond, pars)
  104. if e != nil {
  105. br.Msg = "获取失败"
  106. br.ErrMsg = fmt.Sprintf("获取指标总数失败, %v", e)
  107. return
  108. }
  109. if total <= 0 {
  110. page := paging.GetPaging(params.CurrentIndex, params.PageSize, 0)
  111. resp.Paging = page
  112. br.Data = resp
  113. br.Ret = 200
  114. br.Success = true
  115. br.Msg = "获取成功"
  116. return
  117. }
  118. items, e := indexOb.GetPageItemsByCondition(cond, pars, []string{}, "", startSize, params.PageSize)
  119. if e != nil {
  120. br.Msg = "获取失败"
  121. br.ErrMsg = fmt.Sprintf("获取指标列表失败, %v", e)
  122. return
  123. }
  124. for _, v := range items {
  125. t := v.Format2Item()
  126. resp.List = append(resp.List, t)
  127. }
  128. page := paging.GetPaging(params.CurrentIndex, params.PageSize, total)
  129. resp.Paging = page
  130. br.Data = resp
  131. br.Ret = 200
  132. br.Success = true
  133. br.Msg = "获取成功"
  134. }
  135. // IndexDetail
  136. // @Title 指标详情
  137. // @Description 指标详情
  138. // @Param IndexId query string true "指标ID"
  139. // @Success 200 {object} data_manage.BaseFromRadishResearchIndexDetail
  140. // @router /radish_research/index/detail [get]
  141. func (this *BaseFromRadishResearchController) IndexDetail() {
  142. br := new(models.BaseResponse).Init()
  143. defer func() {
  144. if br.ErrMsg == "" {
  145. br.IsSendEmail = false
  146. }
  147. this.Data["json"] = br
  148. this.ServeJSON()
  149. }()
  150. sysUser := this.SysUser
  151. if sysUser == nil {
  152. br.Msg = "请登录"
  153. br.ErrMsg = "请登录,SysUser Is Empty"
  154. br.Ret = 408
  155. return
  156. }
  157. indexId, _ := this.GetInt("IndexId")
  158. if indexId <= 0 {
  159. br.Msg = "参数有误"
  160. br.ErrMsg = fmt.Sprintf("参数有误, IndexId: %d", indexId)
  161. return
  162. }
  163. resp := new(data_manage.BaseFromRadishResearchIndexDetail)
  164. indexOb := new(data_manage.BaseFromRadishResearchIndex)
  165. item, e := indexOb.GetItemById(indexId)
  166. if e != nil {
  167. if utils.IsErrNoRow(e) {
  168. br.Msg = "指标不存在, 请刷新页面"
  169. return
  170. }
  171. br.Msg = "操作失败"
  172. br.ErrMsg = fmt.Sprintf("获取指标失败, %v", e)
  173. return
  174. }
  175. if item != nil && item.BaseFromRadishResearchIndexId <= 0 {
  176. br.Msg = "指标不存在, 请刷新页面"
  177. return
  178. }
  179. resp.BaseFromRadishResearchIndexItem = item.Format2Item()
  180. resp.DataList = make([]*data_manage.BaseFromRadishResearchDataItem, 0)
  181. dataOb := new(data_manage.BaseFromRadishResearchData)
  182. cond := fmt.Sprintf(` AND %s = ?`, dataOb.Cols().IndexCode)
  183. pars := make([]interface{}, 0)
  184. pars = append(pars, item.IndexCode)
  185. dataList, e := dataOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s DESC", dataOb.Cols().DataTime))
  186. if e != nil {
  187. br.Msg = "获取失败"
  188. br.ErrMsg = fmt.Sprintf("获取指标数据失败, %v", e)
  189. return
  190. }
  191. for _, v := range dataList {
  192. resp.DataList = append(resp.DataList, v.Format2Item())
  193. }
  194. br.Data = resp
  195. br.Ret = 200
  196. br.Success = true
  197. br.Msg = "获取成功"
  198. }
  199. // IndexEdit
  200. // @Title 编辑指标
  201. // @Description 编辑指标
  202. // @Success 200 {object} data_manage.RadishResearchIndexEditReq
  203. // @router /radish_research/index/edit [post]
  204. func (this *BaseFromRadishResearchController) IndexEdit() {
  205. br := new(models.BaseResponse).Init()
  206. defer func() {
  207. if br.ErrMsg == "" {
  208. br.IsSendEmail = false
  209. }
  210. this.Data["json"] = br
  211. this.ServeJSON()
  212. }()
  213. sysUser := this.SysUser
  214. if sysUser == nil {
  215. br.Msg = "请登录"
  216. br.ErrMsg = "请登录,SysUser Is Empty"
  217. br.Ret = 408
  218. return
  219. }
  220. var req data_manage.RadishResearchIndexEditReq
  221. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  222. br.Msg = "参数解析异常!"
  223. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  224. return
  225. }
  226. if req.IndexId <= 0 {
  227. br.Msg = "参数有误"
  228. br.ErrMsg = fmt.Sprintf("参数有误, IndexId: %d", req.IndexId)
  229. return
  230. }
  231. indexOb := new(data_manage.BaseFromRadishResearchIndex)
  232. item, e := indexOb.GetItemById(req.IndexId)
  233. if e != nil {
  234. if utils.IsErrNoRow(e) {
  235. br.Msg = "指标不存在, 请刷新页面"
  236. return
  237. }
  238. br.Msg = "操作失败"
  239. br.ErrMsg = fmt.Sprintf("获取指标失败, %v", e)
  240. return
  241. }
  242. if item != nil && item.BaseFromRadishResearchIndexId <= 0 {
  243. br.Msg = "指标不存在, 请刷新页面"
  244. return
  245. }
  246. // 更新指标(这里不多限制,允许移到未分类)
  247. item.ClassifyId = req.ClassifyId
  248. item.ModifyTime = time.Now().Local()
  249. updateCols := []string{indexOb.Cols().ClassifyId, indexOb.Cols().ModifyTime}
  250. if e = item.Update(updateCols); e != nil {
  251. br.Msg = "操作失败"
  252. br.ErrMsg = fmt.Sprintf("更新指标分类失败, %v", e)
  253. return
  254. }
  255. // 更新ES
  256. go func() {
  257. indexItem := new(dataSourceModel.SearchDataSource)
  258. indexItem.PrimaryId = item.BaseFromRadishResearchIndexId
  259. indexItem.IndexCode = item.IndexCode
  260. indexItem.IndexName = item.IndexName
  261. indexItem.ClassifyId = item.ClassifyId
  262. indexItem.Unit = item.Unit
  263. indexItem.Frequency = item.Frequency
  264. indexItem.StartDate = item.StartDate.Format(utils.FormatDate)
  265. indexItem.EndDate = item.EndDate.Format(utils.FormatDate)
  266. indexItem.LatestValue = fmt.Sprint(item.LatestValue)
  267. indexItem.Source = utils.DATA_SOURCE_RADISH_RESEARCH
  268. indexItem.SourceName = utils.DATA_SOURCE_NAME_RADISH_RESEARCH
  269. indexItem.IsDeleted = 0
  270. indexItem.CreateTime = item.CreateTime.Format(utils.FormatDateTime)
  271. indexItem.ModifyTime = item.ModifyTime.Format(utils.FormatDateTime)
  272. docId := fmt.Sprintf("%d-%d", utils.DATA_SOURCE_RADISH_RESEARCH, item.BaseFromRadishResearchIndexId)
  273. if e := elastic.EsAddOrEditDataSourceIndex(utils.EsDataSourceIndexName, docId, indexItem); e != nil {
  274. utils.FileLog.Warning("RadishResearch-写入指标ES失败, %v", e)
  275. return
  276. }
  277. }()
  278. br.Ret = 200
  279. br.Success = true
  280. br.Msg = "操作成功"
  281. }
  282. // IndexRemove
  283. // @Title 删除指标
  284. // @Description 删除指标
  285. // @Success 200 {object} data_manage.RadishResearchIndexRemoveReq
  286. // @router /radish_research/index/remove [post]
  287. func (this *BaseFromRadishResearchController) IndexRemove() {
  288. br := new(models.BaseResponse).Init()
  289. defer func() {
  290. if br.ErrMsg == "" {
  291. br.IsSendEmail = false
  292. }
  293. this.Data["json"] = br
  294. this.ServeJSON()
  295. }()
  296. sysUser := this.SysUser
  297. if sysUser == nil {
  298. br.Msg = "请登录"
  299. br.ErrMsg = "请登录,SysUser Is Empty"
  300. br.Ret = 408
  301. return
  302. }
  303. var req data_manage.RadishResearchIndexRemoveReq
  304. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  305. br.Msg = "参数解析异常!"
  306. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  307. return
  308. }
  309. if req.IndexId <= 0 {
  310. br.Msg = "参数有误"
  311. br.ErrMsg = fmt.Sprintf("参数有误, IndexId: %d", req.IndexId)
  312. return
  313. }
  314. indexOb := new(data_manage.BaseFromRadishResearchIndex)
  315. item, e := indexOb.GetItemById(req.IndexId)
  316. if e != nil {
  317. if utils.IsErrNoRow(e) {
  318. br.Msg = "指标不存在, 请刷新页面"
  319. return
  320. }
  321. br.Msg = "操作失败"
  322. br.ErrMsg = fmt.Sprintf("获取指标失败, %v", e)
  323. return
  324. }
  325. if item != nil && item.BaseFromRadishResearchIndexId <= 0 {
  326. br.Msg = "指标不存在, 请刷新页面"
  327. return
  328. }
  329. if item.EdbExist == 1 {
  330. br.Msg = "指标已被引用, 不允许删除"
  331. return
  332. }
  333. if e = indexOb.RemoveIndexAndData(item.IndexCode); e != nil {
  334. br.Msg = "操作失败"
  335. br.ErrMsg = fmt.Sprintf("删除指标和数据失败, %v", e)
  336. return
  337. }
  338. // 更新ES
  339. go func() {
  340. indexItem := new(dataSourceModel.SearchDataSource)
  341. indexItem.PrimaryId = item.BaseFromRadishResearchIndexId
  342. indexItem.IndexCode = item.IndexCode
  343. indexItem.IndexName = item.IndexName
  344. indexItem.ClassifyId = item.ClassifyId
  345. indexItem.Unit = item.Unit
  346. indexItem.Frequency = item.Frequency
  347. indexItem.StartDate = item.StartDate.Format(utils.FormatDate)
  348. indexItem.EndDate = item.EndDate.Format(utils.FormatDate)
  349. indexItem.LatestValue = fmt.Sprint(item.LatestValue)
  350. indexItem.Source = utils.DATA_SOURCE_RADISH_RESEARCH
  351. indexItem.SourceName = utils.DATA_SOURCE_NAME_RADISH_RESEARCH
  352. indexItem.IsDeleted = 1 // 标记已删除
  353. indexItem.CreateTime = item.CreateTime.Format(utils.FormatDateTime)
  354. indexItem.ModifyTime = item.ModifyTime.Format(utils.FormatDateTime)
  355. docId := fmt.Sprintf("%d-%d", utils.DATA_SOURCE_RADISH_RESEARCH, item.BaseFromRadishResearchIndexId)
  356. if e := elastic.EsAddOrEditDataSourceIndex(utils.EsDataSourceIndexName, docId, indexItem); e != nil {
  357. utils.FileLog.Warning("RadishResearch-写入指标ES失败, %v", e)
  358. return
  359. }
  360. }()
  361. br.Ret = 200
  362. br.Success = true
  363. br.Msg = "操作成功"
  364. }
  365. // IndexExport
  366. // @Title 导出指标数据
  367. // @Description 导出指标数据
  368. // @Param ClassifyId query int false "分类Id"
  369. // @Success 200 导出成功
  370. // @router /radish_research/index/export [get]
  371. func (this *BaseFromRadishResearchController) IndexExport() {
  372. br := new(models.BaseResponse).Init()
  373. defer func() {
  374. if br.ErrMsg == "" {
  375. br.IsSendEmail = false
  376. }
  377. this.Data["json"] = br
  378. this.ServeJSON()
  379. }()
  380. sysUser := this.SysUser
  381. if sysUser == nil {
  382. br.Msg = "请登录"
  383. br.ErrMsg = "请登录,SysUser Is Empty"
  384. br.Ret = 408
  385. return
  386. }
  387. classifyId, _ := this.GetInt("ClassifyId")
  388. dir, _ := os.Executable()
  389. exPath := filepath.Dir(dir)
  390. downFile := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  391. xlsxFile := xlsx.NewFile()
  392. // 获取指标数据
  393. indexOb := new(data_manage.BaseFromRadishResearchIndex)
  394. cond := fmt.Sprintf(` AND %s = ?`, indexOb.Cols().ClassifyId)
  395. pars := make([]interface{}, 0)
  396. pars = append(pars, classifyId)
  397. indexes, e := indexOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC, %s ASC", indexOb.Cols().Sort, indexOb.Cols().PrimaryId))
  398. if e != nil {
  399. br.Msg = "获取失败"
  400. br.ErrMsg = fmt.Sprintf("获取指标失败, %v", e)
  401. return
  402. }
  403. if len(indexes) == 0 {
  404. // 无数据返回空文件
  405. if e := xlsxFile.Save(downFile); e != nil {
  406. sheet, e := xlsxFile.AddSheet("无数据")
  407. if e != nil {
  408. br.Msg = "获取失败"
  409. br.ErrMsg = fmt.Sprintf("新增Sheet失败, %v", e)
  410. return
  411. }
  412. rowSecName := sheet.AddRow()
  413. celSecName := rowSecName.AddCell()
  414. celSecName.SetValue("")
  415. if e = xlsxFile.Save(downFile); e != nil {
  416. br.Msg = "获取失败"
  417. br.ErrMsg = fmt.Sprintf("保存文件失败, %v", e)
  418. return
  419. }
  420. }
  421. return
  422. }
  423. // 划分指标频度
  424. frequencyIndex := make(map[string][]*data_manage.BaseFromRadishResearchIndex)
  425. frequencyIndexIds := make(map[string][]int)
  426. for _, v := range indexes {
  427. if frequencyIndex[v.Frequency] == nil {
  428. frequencyIndex[v.Frequency] = make([]*data_manage.BaseFromRadishResearchIndex, 0)
  429. }
  430. if frequencyIndexIds[v.Frequency] == nil {
  431. frequencyIndexIds[v.Frequency] = make([]int, 0)
  432. }
  433. frequencyIndexIds[v.Frequency] = append(frequencyIndexIds[v.Frequency], v.BaseFromRadishResearchIndexId)
  434. frequencyIndex[v.Frequency] = append(frequencyIndex[v.Frequency], v)
  435. }
  436. frequencyArr := []string{"日度", "周度", "旬度", "月度", "季度", "半年度", "年度"}
  437. //frequencyMap := map[string]string{
  438. // "日度": "Daily",
  439. // "周度": "Weekly",
  440. // "旬度": "ten-day",
  441. // "月度": "Monthly",
  442. // "季度": "Quarterly",
  443. // "半年度": "Semi-annual",
  444. // "年度": "Annual",
  445. //}
  446. dataOb := new(data_manage.BaseFromRadishResearchData)
  447. for _, frequency := range frequencyArr {
  448. // 获取对应频度指标
  449. secNameList := frequencyIndex[frequency]
  450. if len(secNameList) == 0 {
  451. continue
  452. }
  453. //sheetName := fmt.Sprintf("%s(%s)", frequency, frequencyMap[frequency])
  454. sheetNew, e := xlsxFile.AddSheet(frequency)
  455. if e != nil {
  456. utils.FileLog.Warning(fmt.Sprintf("萝卜投研导出-AddSheet err: %v", e))
  457. continue
  458. }
  459. secNameRow := sheetNew.AddRow()
  460. frequencyRow := sheetNew.AddRow()
  461. unitRow := sheetNew.AddRow()
  462. updateTimeRow := sheetNew.AddRow()
  463. // 指标日期序列
  464. indexIds := frequencyIndexIds[frequency]
  465. dataTimeList, e := dataOb.GetDataTimeByIndexIds(indexIds)
  466. if e != nil {
  467. utils.FileLog.Warning(fmt.Sprintf("萝卜投研导出-GetDataTimeByIndexIds err: %v", e))
  468. continue
  469. }
  470. // 添加excel左侧指标日期
  471. setRowIndex := 4
  472. for rk, dv := range dataTimeList {
  473. rowIndex := setRowIndex + rk
  474. row := sheetNew.Row(rowIndex)
  475. displayDate, _ := time.Parse(utils.FormatDate, dv)
  476. displayDateCell := row.AddCell()
  477. style := new(xlsx.Style)
  478. style.ApplyAlignment = true
  479. style.Alignment.WrapText = true
  480. displayDateCell.SetStyle(style)
  481. displayDateCell.SetDate(displayDate)
  482. }
  483. for k, sv := range secNameList {
  484. // 获取数据
  485. dataCond := fmt.Sprintf(` AND %s = ?`, dataOb.Cols().IndexCode)
  486. dataPars := make([]interface{}, 0)
  487. dataPars = append(dataPars, sv.IndexCode)
  488. dataList, e := dataOb.GetItemsByCondition(dataCond, dataPars, []string{}, fmt.Sprintf("%s DESC", dataOb.Cols().DataTime))
  489. if e != nil {
  490. utils.FileLog.Warning(fmt.Sprintf("萝卜投研导出-GetIndexDataByCondition err: %v", e))
  491. continue
  492. }
  493. if k == 0 {
  494. secNameRow.AddCell().SetValue("指标名称")
  495. frequencyRow.AddCell().SetValue("频度")
  496. unitRow.AddCell().SetValue("单位")
  497. updateTimeRow.AddCell().SetValue("更新时间")
  498. minCol := k * 3
  499. sheetNew.SetColWidth(minCol, minCol, 15)
  500. }
  501. if len(dataList) == 0 {
  502. continue
  503. }
  504. secNameRow.AddCell().SetValue(sv.IndexName)
  505. frequencyRow.AddCell().SetValue(sv.Frequency)
  506. unitRow.AddCell().SetValue(sv.Unit)
  507. updateTimeRow.AddCell().SetValue(sv.ModifyTime)
  508. dataInfoMap := make(map[string]*data_manage.BaseFromRadishResearchData)
  509. for _, v := range dataList {
  510. dt := v.DataTime.Format(utils.FormatDate)
  511. dataInfoMap[dt] = v
  512. }
  513. for rk, dtv := range dataTimeList {
  514. rowIndex := setRowIndex + rk
  515. row := sheetNew.Row(rowIndex)
  516. displayDateCell := row.AddCell()
  517. tmpData, ok := dataInfoMap[dtv]
  518. if ok {
  519. displayDateCell.SetValue(tmpData.Value)
  520. }
  521. }
  522. }
  523. }
  524. // 保存文件出错返回空文件
  525. if e := xlsxFile.Save(downFile); e != nil {
  526. sheet, e := xlsxFile.AddSheet("无数据")
  527. if e != nil {
  528. br.Msg = "获取失败"
  529. br.ErrMsg = fmt.Sprintf("新增Sheet失败, %v", e)
  530. return
  531. }
  532. rowSecName := sheet.AddRow()
  533. celSecName := rowSecName.AddCell()
  534. celSecName.SetValue("")
  535. if e = xlsxFile.Save(downFile); e != nil {
  536. br.Msg = "获取失败"
  537. br.ErrMsg = fmt.Sprintf("保存文件失败, %v", e)
  538. return
  539. }
  540. }
  541. fileName := fmt.Sprintf("%s%s%s", utils.DATA_SOURCE_NAME_RADISH_RESEARCH, time.Now().Format("06.01.02"), ".xlsx")
  542. this.Ctx.Output.Download(downFile, fileName)
  543. defer func() {
  544. _ = os.Remove(downFile)
  545. }()
  546. br.Ret = 200
  547. br.Success = true
  548. br.Msg = "success"
  549. }
  550. // IndexSelect
  551. // @Title 批量加入指标库-选择指标
  552. // @Description 批量加入指标库-选择指标
  553. // @Success 200 {object} data_manage.BaseFromRadishResearchIndexItem
  554. // @router /radish_research/index/select [post]
  555. func (this *BaseFromRadishResearchController) IndexSelect() {
  556. br := new(models.BaseResponse).Init()
  557. defer func() {
  558. if br.ErrMsg == "" {
  559. br.IsSendEmail = false
  560. }
  561. this.Data["json"] = br
  562. this.ServeJSON()
  563. }()
  564. sysUser := this.SysUser
  565. if sysUser == nil {
  566. br.Msg = "请登录"
  567. br.ErrMsg = "请登录,SysUser Is Empty"
  568. br.Ret = 408
  569. return
  570. }
  571. var req data_manage.RadishResearchIndexSelectReq
  572. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  573. br.Msg = "参数解析异常!"
  574. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  575. return
  576. }
  577. resp := make([]*data_manage.BaseFromRadishResearchIndexItem, 0)
  578. indexOb := new(data_manage.BaseFromRadishResearchIndex)
  579. var (
  580. cond string
  581. pars []interface{}
  582. )
  583. // 忽略已加入指标库的
  584. cond += fmt.Sprintf(` AND %s = 0`, indexOb.Cols().EdbExist)
  585. if len(req.ClassifyIds) > 0 {
  586. cond += fmt.Sprintf(` AND %s IN ?`, indexOb.Cols().ClassifyId)
  587. pars = append(pars, req.ClassifyIds)
  588. }
  589. if len(req.Frequencies) > 0 {
  590. cond += fmt.Sprintf(` AND %s IN ?`, indexOb.Cols().Frequency)
  591. pars = append(pars, req.Frequencies)
  592. }
  593. req.Keyword = strings.TrimSpace(req.Keyword)
  594. if req.Keyword != "" {
  595. kw := fmt.Sprint("%", req.Keyword, "%")
  596. cond += fmt.Sprintf(` AND (%s LIKE ? OR %s LIKE ?)`, indexOb.Cols().IndexName, indexOb.Cols().IndexCode)
  597. pars = append(pars, kw, kw)
  598. }
  599. // 列表全选-SelectAll-true: IndexCodes为排除的指标, SelectAll-false: IndexCodes为选择的指标
  600. if len(req.IndexCodes) > 0 {
  601. if req.SelectAll {
  602. cond += fmt.Sprintf(` AND %s NOT IN ?`, indexOb.Cols().IndexCode)
  603. } else {
  604. cond += fmt.Sprintf(` AND %s IN ?`, indexOb.Cols().IndexCode)
  605. }
  606. pars = append(pars, req.IndexCodes)
  607. }
  608. items, e := indexOb.GetItemsByCondition(cond, pars, []string{}, "")
  609. if e != nil {
  610. br.Msg = "获取失败"
  611. br.ErrMsg = fmt.Sprintf("获取指标列表失败, %v", e)
  612. return
  613. }
  614. if len(items) > 30 {
  615. br.Msg = "批量添加指标数量不得超过30个"
  616. return
  617. }
  618. for _, v := range items {
  619. t := v.Format2Item()
  620. resp = append(resp, t)
  621. }
  622. br.Data = resp
  623. br.Ret = 200
  624. br.Success = true
  625. br.Msg = "获取成功"
  626. }
  627. // EdbAdd
  628. // @Title 加入指标库
  629. // @Description 加入指标库
  630. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  631. // @Success Ret=200 保存成功
  632. // @router /radish_research/edb/add [post]
  633. func (this *BaseFromRadishResearchController) EdbAdd() {
  634. br := new(models.BaseResponse).Init()
  635. defer func() {
  636. if br.ErrMsg == "" {
  637. br.IsSendEmail = false
  638. }
  639. this.Data["json"] = br
  640. this.ServeJSON()
  641. }()
  642. sysUser := this.SysUser
  643. if sysUser == nil {
  644. br.Msg = "请登录"
  645. br.ErrMsg = "请登录,SysUser Is Empty"
  646. br.Ret = 408
  647. return
  648. }
  649. deleteCache := true
  650. cacheKey := fmt.Sprintf("CACHE_EDB_INFO_ADD_%d_%d", utils.DATA_SOURCE_RADISH_RESEARCH, sysUser.AdminId)
  651. defer func() {
  652. if deleteCache {
  653. _ = utils.Rc.Delete(cacheKey)
  654. }
  655. }()
  656. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  657. deleteCache = false
  658. br.Msg = "系统处理中,请稍后重试"
  659. return
  660. }
  661. var req data_manage.AddEdbInfoReq
  662. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  663. br.Msg = "参数解析异常"
  664. br.ErrMsg = fmt.Sprintf("参数解析失败, %v", e)
  665. return
  666. }
  667. req.EdbCode = strings.TrimSpace(req.EdbCode)
  668. req.EdbName = strings.TrimSpace(req.EdbName)
  669. if req.EdbCode == "" {
  670. br.Msg = "指标编码不能为空"
  671. return
  672. }
  673. if req.EdbName == "" {
  674. br.Msg = "指标名称不能为空"
  675. return
  676. }
  677. if req.Frequency == "" {
  678. br.Msg = "频率不能为空"
  679. return
  680. }
  681. if req.Unit == "" {
  682. br.Msg = "单位不能为空"
  683. return
  684. }
  685. if req.ClassifyId <= 0 {
  686. br.Msg = "请选择分类"
  687. return
  688. }
  689. // 是否加入过指标库
  690. exist, e := data_manage.GetEdbInfoByEdbCode(utils.DATA_SOURCE_RADISH_RESEARCH, req.EdbCode)
  691. if e != nil && !utils.IsErrNoRow(e) {
  692. br.Msg = "操作失败"
  693. br.ErrMsg = fmt.Sprintf("校验是否加入过指标库失败, %v", e)
  694. return
  695. }
  696. if exist != nil && exist.EdbInfoId > 0 {
  697. br.Msg = "指标库已存在,请刷新页面"
  698. return
  699. }
  700. // 指标入库
  701. edbInfo, err, errMsg, isSendEmail := data.EdbInfoAdd(utils.DATA_SOURCE_RADISH_RESEARCH, utils.DATA_SUB_SOURCE_EDB, req.ClassifyId, req.EdbCode, req.EdbName, req.Frequency, req.Unit, req.StartDate, req.EndDate, sysUser.AdminId, sysUser.RealName, this.Lang)
  702. if err != nil {
  703. br.Msg = "保存失败"
  704. if errMsg != `` {
  705. br.Msg = errMsg
  706. }
  707. br.ErrMsg = err.Error()
  708. br.IsSendEmail = isSendEmail
  709. return
  710. }
  711. // 刷新指标数据
  712. refreshRes, e := data.RefreshEdbData(edbInfo.EdbInfoId, edbInfo.Source, edbInfo.SubSource, edbInfo.EdbCode, "")
  713. if e != nil {
  714. br.Msg = "操作失败"
  715. br.ErrMsg = fmt.Sprintf("刷新指标数据失败, %v", e)
  716. return
  717. }
  718. if refreshRes != nil && refreshRes.Ret != 200 {
  719. br.Msg = "操作失败"
  720. br.ErrMsg = fmt.Sprintf("刷新指标数据失败, Ret: %d, Msg: %s, ErrMsg: %s", refreshRes.Ret, refreshRes.Msg, refreshRes.ErrMsg)
  721. return
  722. }
  723. // 更新指标EdbExist
  724. indexOb := new(data_manage.BaseFromRadishResearchIndex)
  725. if e := indexOb.UpdateEdbExists(1, []string{req.EdbCode}); e != nil {
  726. br.Msg = "操作失败"
  727. br.ErrMsg = fmt.Sprintf("更新数据源EdbExist失败, %v", e)
  728. return
  729. }
  730. //新增操作日志
  731. {
  732. edbLog := new(data_manage.EdbInfoLog)
  733. edbLog.EdbInfoId = edbInfo.EdbInfoId
  734. edbLog.SourceName = edbInfo.SourceName
  735. edbLog.Source = edbInfo.Source
  736. edbLog.EdbCode = edbInfo.EdbCode
  737. edbLog.EdbName = edbInfo.EdbName
  738. edbLog.ClassifyId = edbInfo.ClassifyId
  739. edbLog.SysUserId = sysUser.AdminId
  740. edbLog.SysUserRealName = sysUser.RealName
  741. edbLog.CreateTime = time.Now()
  742. edbLog.Content = string(this.Ctx.Input.RequestBody)
  743. edbLog.Status = "新增指标"
  744. edbLog.Method = this.Ctx.Input.URI()
  745. go data_manage.AddEdbInfoLog(edbLog)
  746. }
  747. // 更新es
  748. go data.AddOrEditEdbInfoToEs(edbInfo.EdbInfoId)
  749. // 试用平台更新用户累计新增指标数
  750. if utils.BusinessCode == utils.BusinessCodeSandbox {
  751. go func() {
  752. adminItem, e := system.GetSysAdminById(sysUser.AdminId)
  753. if e != nil {
  754. return
  755. }
  756. if adminItem != nil && adminItem.AdminId <= 0 {
  757. return
  758. }
  759. if adminItem.DepartmentName != "ETA试用客户" {
  760. return
  761. }
  762. var r etaTrialService.EtaTrialUserReq
  763. r.Mobile = adminItem.Mobile
  764. _, _ = etaTrialService.UpdateUserIndexNum(r)
  765. }()
  766. }
  767. resp := new(data_manage.AddEdbInfoResp)
  768. resp.EdbInfoId = edbInfo.EdbInfoId
  769. resp.UniqueCode = edbInfo.UniqueCode
  770. br.Ret = 200
  771. br.Success = true
  772. br.Msg = "保存成功"
  773. br.Data = resp
  774. br.IsAddLog = true
  775. }
  776. // EdbNameCheck
  777. // @Title 批量加入指标库-重名校验
  778. // @Description 批量加入指标库-重名校验
  779. // @Success 200 {object} data_manage.EdbNameCheckResult
  780. // @router /radish_research/edb/name_check [post]
  781. func (this *BaseFromRadishResearchController) EdbNameCheck() {
  782. br := new(models.BaseResponse).Init()
  783. defer func() {
  784. if br.ErrMsg == "" {
  785. br.IsSendEmail = false
  786. }
  787. this.Data["json"] = br
  788. this.ServeJSON()
  789. }()
  790. sysUser := this.SysUser
  791. if sysUser == nil {
  792. br.Msg = "请登录"
  793. br.ErrMsg = "请登录,SysUser Is Empty"
  794. br.Ret = 408
  795. return
  796. }
  797. var req []*data_manage.NameCheckEdbInfoReq
  798. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  799. br.Msg = "参数解析异常!"
  800. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  801. return
  802. }
  803. if len(req) == 0 {
  804. br.Msg = "请选择指标"
  805. return
  806. }
  807. codeMaxT := 30
  808. codeLen := len(req)
  809. if codeLen > codeMaxT {
  810. br.Msg = "批量添加指标数量不得超过30个"
  811. return
  812. }
  813. indexNames := make([]string, 0)
  814. resp := make([]*data_manage.EdbNameCheckResult, 0)
  815. nameCount := make(map[string]int)
  816. for _, v := range req {
  817. v.EdbCode = strings.TrimSpace(v.EdbCode)
  818. if v.EdbCode == "" {
  819. br.Msg = "指标ID不可为空"
  820. return
  821. }
  822. v.EdbName = strings.TrimSpace(v.EdbName)
  823. if v.EdbName == "" {
  824. br.Msg = "请输入指标名称"
  825. return
  826. }
  827. nameCount[v.EdbName] += 1
  828. indexNames = append(indexNames, v.EdbName)
  829. resp = append(resp, &data_manage.EdbNameCheckResult{
  830. EdbCode: v.EdbCode,
  831. EdbName: v.EdbName,
  832. })
  833. }
  834. // 本次提交的名称中也不允许重复
  835. for _, v := range resp {
  836. if nameCount[v.EdbName] > 1 {
  837. v.Exist = true
  838. }
  839. }
  840. // 重名校验
  841. edbList, e := data_manage.GetEdbInfoByNameArr(indexNames, utils.EDB_INFO_TYPE)
  842. if e != nil {
  843. br.Msg = "操作失败"
  844. br.ErrMsg = "获取重名指标失败, Err: " + e.Error()
  845. return
  846. }
  847. nameExists := make(map[string]bool)
  848. for _, v := range edbList {
  849. nameExists[v.EdbName] = true
  850. }
  851. if len(nameExists) > 0 {
  852. for _, v := range resp {
  853. v.Exist = nameExists[v.EdbName]
  854. }
  855. }
  856. br.Data = resp
  857. br.Msg = "校验成功"
  858. br.Ret = 200
  859. br.Success = true
  860. }
  861. // EdbMultiAdd
  862. // @Title 批量加入指标库
  863. // @Description 批量加入指标库
  864. // @Param request body data_manage.AddEdbInfoReq true "type json string"
  865. // @Success 200 string "操作成功"
  866. // @router /radish_research/edb/multi_add [post]
  867. func (this *BaseFromRadishResearchController) EdbMultiAdd() {
  868. br := new(models.BaseResponse).Init()
  869. defer func() {
  870. if br.ErrMsg == "" {
  871. br.IsSendEmail = false
  872. }
  873. this.Data["json"] = br
  874. this.ServeJSON()
  875. }()
  876. sysUser := this.SysUser
  877. if sysUser == nil {
  878. br.Msg = "请登录"
  879. br.ErrMsg = "请登录,SysUser Is Empty"
  880. br.Ret = 408
  881. return
  882. }
  883. deleteCache := true
  884. cacheKey := fmt.Sprintf("CACHE_EDB_INFO_BATCH_ADD_%d_%d", utils.DATA_SOURCE_RADISH_RESEARCH, sysUser.AdminId)
  885. defer func() {
  886. if deleteCache {
  887. _ = utils.Rc.Delete(cacheKey)
  888. }
  889. }()
  890. if !utils.Rc.SetNX(cacheKey, 1, 30*time.Second) {
  891. deleteCache = false
  892. br.Msg = "系统处理中,请稍后重试!"
  893. br.ErrMsg = "系统处理中,请稍后重试!" + sysUser.RealName + ";data:" + string(this.Ctx.Input.RequestBody)
  894. return
  895. }
  896. var req []*data_manage.AddEdbInfoReq
  897. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  898. br.Msg = "参数解析异常!"
  899. br.ErrMsg = "参数解析失败,Err:" + e.Error()
  900. return
  901. }
  902. if len(req) == 0 {
  903. br.Msg = "请选择指标"
  904. return
  905. }
  906. if len(req) > 30 {
  907. br.Msg = "批量添加指标数量不得超过30个"
  908. return
  909. }
  910. for _, v := range req {
  911. v.EdbCode = strings.TrimSpace(v.EdbCode)
  912. if v.EdbCode == "" {
  913. br.Msg = "指标ID不可为空"
  914. return
  915. }
  916. v.EdbName = strings.TrimSpace(v.EdbName)
  917. if v.EdbName == "" {
  918. br.Msg = "请输入指标名称"
  919. return
  920. }
  921. v.Frequency = strings.TrimSpace(v.Frequency)
  922. if v.Frequency == "" {
  923. br.Msg = "请选择频度"
  924. return
  925. }
  926. v.Unit = strings.TrimSpace(v.Unit)
  927. if v.Unit == "" {
  928. br.Msg = "请输入单位"
  929. return
  930. }
  931. if v.ClassifyId <= 0 {
  932. br.Msg = "请选择分类"
  933. return
  934. }
  935. }
  936. // 限定同一时间最多批量新增30个指标
  937. var successCodes []string
  938. for _, v := range req {
  939. // 是否加入过指标库
  940. exist, e := data_manage.GetEdbInfoByEdbCode(utils.DATA_SOURCE_RADISH_RESEARCH, v.EdbCode)
  941. if e != nil && !utils.IsErrNoRow(e) {
  942. br.Msg = "操作失败"
  943. br.ErrMsg = fmt.Sprintf("校验是否加入过指标库失败, %v", e)
  944. return
  945. }
  946. if exist != nil && exist.EdbInfoId > 0 {
  947. // 加入过指标库这里直接忽略掉
  948. continue
  949. }
  950. // 指标入库
  951. edbInfo, err, errMsg, isSendEmail := data.EdbInfoAdd(utils.DATA_SOURCE_RADISH_RESEARCH, utils.DATA_SUB_SOURCE_EDB, v.ClassifyId, v.EdbCode, v.EdbName, v.Frequency, v.Unit, v.StartDate, v.EndDate, sysUser.AdminId, sysUser.RealName, this.Lang)
  952. if err != nil {
  953. br.Msg = "保存失败"
  954. if errMsg != `` {
  955. br.Msg = errMsg
  956. }
  957. br.ErrMsg = err.Error()
  958. br.IsSendEmail = isSendEmail
  959. return
  960. }
  961. successCodes = append(successCodes, v.EdbCode)
  962. // 刷新指标数据
  963. refreshRes, e := data.RefreshEdbData(edbInfo.EdbInfoId, edbInfo.Source, edbInfo.SubSource, edbInfo.EdbCode, "")
  964. if e != nil {
  965. br.Msg = "操作失败"
  966. br.ErrMsg = fmt.Sprintf("刷新指标数据失败, %v", e)
  967. return
  968. }
  969. if refreshRes != nil && refreshRes.Ret != 200 {
  970. br.Msg = "操作失败"
  971. br.ErrMsg = fmt.Sprintf("刷新指标数据失败, Ret: %d, Msg: %s, ErrMsg: %s", refreshRes.Ret, refreshRes.Msg, refreshRes.ErrMsg)
  972. return
  973. }
  974. // 试用平台更新用户累计新增指标数
  975. if utils.BusinessCode == utils.BusinessCodeSandbox {
  976. go func() {
  977. adminItem, e := system.GetSysAdminById(sysUser.AdminId)
  978. if e != nil {
  979. tips := fmt.Sprintf("试用平台更新用户累计新增指标数-获取用户失败, Err: " + e.Error())
  980. utils.FileLog.Info(tips)
  981. return
  982. }
  983. if adminItem.DepartmentName != "ETA试用客户" {
  984. return
  985. }
  986. var ur etaTrialService.EtaTrialUserReq
  987. ur.Mobile = adminItem.Mobile
  988. _, _ = etaTrialService.UpdateUserIndexNum(ur)
  989. }()
  990. }
  991. // 新增操作日志
  992. {
  993. edbLog := new(data_manage.EdbInfoLog)
  994. edbLog.EdbInfoId = edbInfo.EdbInfoId
  995. edbLog.SourceName = edbInfo.SourceName
  996. edbLog.Source = edbInfo.Source
  997. edbLog.EdbCode = edbInfo.EdbCode
  998. edbLog.EdbName = edbInfo.EdbName
  999. edbLog.ClassifyId = edbInfo.ClassifyId
  1000. edbLog.SysUserId = sysUser.AdminId
  1001. edbLog.SysUserRealName = sysUser.RealName
  1002. edbLog.CreateTime = time.Now()
  1003. edbLog.Content = string(this.Ctx.Input.RequestBody)
  1004. edbLog.Status = "新增指标"
  1005. edbLog.Method = this.Ctx.Input.URI()
  1006. go data_manage.AddEdbInfoLog(edbLog)
  1007. }
  1008. // 更新es
  1009. go data.AddOrEditEdbInfoToEs(edbInfo.EdbInfoId)
  1010. }
  1011. // 更新指标EdbExist
  1012. if len(successCodes) > 0 {
  1013. indexOb := new(data_manage.BaseFromRadishResearchIndex)
  1014. if e := indexOb.UpdateEdbExists(1, successCodes); e != nil {
  1015. br.Msg = "操作失败"
  1016. br.ErrMsg = fmt.Sprintf("更新数据源EdbExist失败, %v", e)
  1017. return
  1018. }
  1019. }
  1020. br.Msg = "操作成功"
  1021. br.Ret = 200
  1022. br.Success = true
  1023. br.IsAddLog = true
  1024. }