trade_analysis_table.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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/excel"
  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"
  11. tradeAnalysisService "eta/eta_api/services/data/trade_analysis"
  12. "eta/eta_api/utils"
  13. "fmt"
  14. "github.com/tealeg/xlsx"
  15. "os"
  16. "path/filepath"
  17. "reflect"
  18. "strconv"
  19. "strings"
  20. "time"
  21. )
  22. // TradeAnalysisTableController 多空分析表格
  23. type TradeAnalysisTableController struct {
  24. controllers.BaseAuthController
  25. }
  26. // Preview
  27. // @Title 表格预览
  28. // @Description 表格预览
  29. // @Param request body request.TablePreviewReq true "type json string"
  30. // @Success 200 {object} response.TableResp
  31. // @router /table/preview [post]
  32. func (this *TradeAnalysisTableController) Preview() {
  33. br := new(models.BaseResponse).Init()
  34. defer func() {
  35. if br.ErrMsg == "" {
  36. br.IsSendEmail = false
  37. }
  38. this.Data["json"] = br
  39. this.ServeJSON()
  40. }()
  41. sysUser := this.SysUser
  42. if sysUser == nil {
  43. br.Msg = "请登录"
  44. br.ErrMsg = "请登录,SysUser Is Empty"
  45. br.Ret = 408
  46. return
  47. }
  48. var req tradeAnalysisRequest.TablePreviewReq
  49. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  50. br.Msg = "参数解析异常"
  51. br.ErrMsg = fmt.Sprintf("参数解析异常: %v", e)
  52. return
  53. }
  54. // 校验配置
  55. pass, tips := tradeAnalysisService.CheckAnalysisTableExtraConfig(req.TableConfig)
  56. if !pass {
  57. br.Msg = tips
  58. return
  59. }
  60. // 根据配置获取表格行数据
  61. tableRows, e := tradeAnalysisService.GetTableRowsDataByConfig(req.TableConfig)
  62. if e != nil {
  63. br.Msg = "操作失败"
  64. br.ErrMsg = fmt.Sprintf("获取表格行数据失败, %v", e)
  65. return
  66. }
  67. // 响应
  68. resp := new(tradeAnalysisResponse.TableResp)
  69. resp.TableData = tableRows
  70. resp.TableName = strings.TrimSpace(req.TableName)
  71. resp.ClassifyId = req.ClassifyId
  72. resp.TableConfig = req.TableConfig
  73. resp.TableCols = make([]*tradeAnalysisModel.TradeAnalysisTableColumnItem, 0)
  74. if len(req.TableCols) > 0 {
  75. resp.TableCols = req.TableCols
  76. }
  77. if len(req.TableCols) == 0 {
  78. // 获取默认的表头信息
  79. colOb := new(tradeAnalysisModel.TradeAnalysisTableColumn)
  80. cond := fmt.Sprintf(` AND %s = 0`, colOb.Cols().ExcelInfoId)
  81. defaultCols, e := colOb.GetItemsByCondition(cond, make([]interface{}, 0), []string{}, fmt.Sprintf("%s ASC", colOb.Cols().Sort))
  82. if e != nil {
  83. br.Msg = "操作失败"
  84. br.ErrMsg = fmt.Sprintf("获取默认表头设置失败, %v", e)
  85. return
  86. }
  87. for _, v := range defaultCols {
  88. resp.TableCols = append(resp.TableCols, v.Format2Item())
  89. }
  90. }
  91. br.Data = resp
  92. br.Ret = 200
  93. br.Success = true
  94. br.Msg = "操作成功"
  95. }
  96. // Save
  97. // @Title 保存表格
  98. // @Description 保存表格
  99. // @Param request body request.TableSaveReq true "type json string"
  100. // @Success 200 string "操作成功"
  101. // @router /table/save [post]
  102. func (this *TradeAnalysisTableController) Save() {
  103. br := new(models.BaseResponse).Init()
  104. defer func() {
  105. if br.ErrMsg == "" {
  106. br.IsSendEmail = false
  107. }
  108. this.Data["json"] = br
  109. this.ServeJSON()
  110. }()
  111. sysUser := this.SysUser
  112. if sysUser == nil {
  113. br.Msg = "请登录"
  114. br.ErrMsg = "请登录,SysUser Is Empty"
  115. br.Ret = 408
  116. return
  117. }
  118. var req tradeAnalysisRequest.TableSaveReq
  119. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  120. br.Msg = "参数解析异常"
  121. br.ErrMsg = fmt.Sprintf("参数解析异常: %v", e)
  122. return
  123. }
  124. // 校验配置
  125. pass, tips := tradeAnalysisService.CheckAnalysisTableExtraConfig(req.TableConfig)
  126. if !pass {
  127. br.Msg = tips
  128. return
  129. }
  130. req.TableName = strings.TrimSpace(req.TableName)
  131. if req.TableName == "" {
  132. br.Msg = "请输入表格名称"
  133. return
  134. }
  135. if req.ClassifyId <= 0 {
  136. br.Msg = "请选择分类"
  137. return
  138. }
  139. configByte, e := json.Marshal(req.TableConfig)
  140. if e != nil {
  141. br.Msg = "操作失败"
  142. br.ErrMsg = fmt.Sprintf("表格配置JSON格式化失败, %v", e)
  143. return
  144. }
  145. extraConfig := string(configByte)
  146. // 表格数据
  147. tableRows, e := tradeAnalysisService.GetTableRowsDataByConfig(req.TableConfig)
  148. if e != nil {
  149. br.Msg = "操作失败"
  150. br.ErrMsg = fmt.Sprintf("获取表格行数据失败, %v", e)
  151. return
  152. }
  153. contentByte, e := json.Marshal(tableRows)
  154. if e != nil {
  155. br.Msg = "操作失败"
  156. br.ErrMsg = fmt.Sprintf("表格数据JSON格式化失败, %v", e)
  157. return
  158. }
  159. excelContent := string(contentByte)
  160. // 新增表格
  161. var excelId int
  162. if req.ExcelInfoId <= 0 {
  163. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10) + "_" + utils.GetRandString(10)
  164. newExcel := &excel.ExcelInfo{
  165. ExcelName: req.TableName,
  166. Source: utils.TRADE_ANALYSIS_TABLE,
  167. ExcelType: 1,
  168. UniqueCode: utils.MD5(utils.EXCEL_DATA_PREFIX + "_" + timestamp),
  169. ExcelClassifyId: req.ClassifyId,
  170. SysUserId: sysUser.AdminId,
  171. SysUserRealName: sysUser.RealName,
  172. Content: excelContent,
  173. ExtraConfig: extraConfig,
  174. UpdateUserId: sysUser.AdminId,
  175. UpdateUserRealName: sysUser.RealName,
  176. ModifyTime: time.Now(),
  177. CreateTime: time.Now(),
  178. }
  179. if e := newExcel.Create(); e != nil {
  180. br.Msg = "操作失败"
  181. br.ErrMsg = fmt.Sprintf("新增表格失败, %v", e)
  182. return
  183. }
  184. excelId = newExcel.ExcelInfoId
  185. }
  186. // 更新表格
  187. if req.ExcelInfoId > 0 {
  188. excelItem, e := excel.GetExcelInfoById(req.ExcelInfoId)
  189. if e != nil {
  190. if e.Error() == utils.ErrNoRow() {
  191. br.Msg = "表格不存在, 请刷新页面"
  192. return
  193. }
  194. br.Msg = "操作失败"
  195. br.ErrMsg = fmt.Sprintf("获取表格信息失败, %v", e)
  196. return
  197. }
  198. excelItem.ExcelName = req.TableName
  199. excelItem.ExcelClassifyId = req.ClassifyId
  200. excelItem.Content = excelContent
  201. excelItem.ExtraConfig = extraConfig
  202. excelItem.UpdateUserId = sysUser.AdminId
  203. excelItem.UpdateUserRealName = sysUser.RealName
  204. excelItem.ModifyTime = time.Now()
  205. updateCols := []string{"ExcelName", "ExcelClassifyId", "ExtraConfig", "Content", "UpdateUserId", "UpdateUserRealName", "ModifyTime"}
  206. if e = excelItem.Update(updateCols); e != nil {
  207. br.Msg = "操作失败"
  208. br.ErrMsg = fmt.Sprintf("更新表格信息失败, %v", e)
  209. return
  210. }
  211. excelId = excelItem.ExcelInfoId
  212. }
  213. if excelId <= 0 {
  214. br.Msg = "操作失败"
  215. br.ErrMsg = fmt.Sprintf("表格ID异常: %d", excelId)
  216. return
  217. }
  218. // 表头信息
  219. columnOb := new(tradeAnalysisModel.TradeAnalysisTableColumn)
  220. configColumns := make([]*tradeAnalysisModel.TradeAnalysisTableColumn, 0)
  221. if len(req.TableCols) > 0 {
  222. for _, v := range req.TableCols {
  223. t := new(tradeAnalysisModel.TradeAnalysisTableColumn)
  224. t.ExcelInfoId = excelId
  225. t.ColumnKey = v.ColumnKey
  226. t.ColumnName = v.ColumnName
  227. t.ColumnNameEn = v.ColumnNameEn
  228. t.Sort = v.Sort
  229. t.IsMust = v.IsMust
  230. t.IsShow = v.IsShow
  231. t.IsSort = v.IsSort
  232. t.CreateTime = time.Now()
  233. t.ModifyTime = time.Now()
  234. configColumns = append(configColumns, t)
  235. }
  236. } else {
  237. // 获取默认的表头信息
  238. cond := fmt.Sprintf(` AND %s = 0`, columnOb.Cols().ExcelInfoId)
  239. defaultCols, e := columnOb.GetItemsByCondition(cond, make([]interface{}, 0), []string{}, fmt.Sprintf("%s ASC", columnOb.Cols().Sort))
  240. if e != nil {
  241. br.Msg = "操作失败"
  242. br.ErrMsg = fmt.Sprintf("获取默认表头设置失败, %v", e)
  243. return
  244. }
  245. for _, v := range defaultCols {
  246. v.Id = 0
  247. v.ExcelInfoId = excelId
  248. v.CreateTime = time.Now()
  249. v.ModifyTime = time.Now()
  250. configColumns = append(configColumns, v)
  251. }
  252. }
  253. // 删除并新增表头
  254. {
  255. cond := fmt.Sprintf(`%s = ?`, columnOb.Cols().ExcelInfoId)
  256. pars := make([]interface{}, 0)
  257. pars = append(pars, excelId)
  258. if e = columnOb.RemoveByCondition(cond, pars); e != nil {
  259. br.Msg = "操作失败"
  260. br.ErrMsg = fmt.Sprintf("删除原表头配置失败, %v", e)
  261. return
  262. }
  263. if e = columnOb.CreateMulti(configColumns); e != nil {
  264. br.Msg = "操作失败"
  265. br.ErrMsg = fmt.Sprintf("批量新增表头配置失败, %v", e)
  266. return
  267. }
  268. }
  269. br.Data = excelId
  270. br.Ret = 200
  271. br.Success = true
  272. br.Msg = "操作成功"
  273. }
  274. // Detail
  275. // @Title 表格详情
  276. // @Description 表格详情
  277. // @Param ExcelInfoId query int true "表格ID"
  278. // @Success 200 {object} response.TableResp
  279. // @router /table/detail [get]
  280. func (this *TradeAnalysisTableController) Detail() {
  281. br := new(models.BaseResponse).Init()
  282. defer func() {
  283. if br.ErrMsg == "" {
  284. br.IsSendEmail = false
  285. }
  286. this.Data["json"] = br
  287. this.ServeJSON()
  288. }()
  289. sysUser := this.SysUser
  290. if sysUser == nil {
  291. br.Msg = "请登录"
  292. br.ErrMsg = "请登录,SysUser Is Empty"
  293. br.Ret = 408
  294. return
  295. }
  296. excelId, _ := this.GetInt("ExcelInfoId", 0)
  297. if excelId <= 0 {
  298. br.Msg = "参数有误"
  299. br.ErrMsg = fmt.Sprintf("表格ID不可为空, ID: %d", excelId)
  300. return
  301. }
  302. // 获取表格信息
  303. excelItem, e := excel.GetExcelInfoById(excelId)
  304. if e != nil {
  305. if e.Error() == utils.ErrNoRow() {
  306. br.Msg = "表格不存在, 请刷新页面"
  307. return
  308. }
  309. br.Msg = "获取失败"
  310. br.ErrMsg = fmt.Sprintf("获取表格信息失败, %v", e)
  311. return
  312. }
  313. if excelItem.Source != utils.TRADE_ANALYSIS_TABLE {
  314. br.Msg = "获取失败"
  315. br.ErrMsg = fmt.Sprintf("表格类型异常, Source: %d", excelItem.Source)
  316. return
  317. }
  318. var tableConfig tradeAnalysisModel.TableExtraConfig
  319. if e = json.Unmarshal([]byte(excelItem.ExtraConfig), &tableConfig); e != nil {
  320. br.Msg = "获取失败"
  321. br.ErrMsg = fmt.Sprintf("解析表格配置失败, err: %v, config: %s", e, excelItem.ExtraConfig)
  322. return
  323. }
  324. // 表格数据
  325. tableRows := make([]*tradeAnalysisModel.TableRowData, 0)
  326. if excelItem.Content != "" {
  327. if e = json.Unmarshal([]byte(excelItem.Content), &tableRows); e != nil {
  328. br.Msg = "获取失败"
  329. br.ErrMsg = fmt.Sprintf("解析表格配置失败, err: %v, config: %s", e, excelItem.ExtraConfig)
  330. return
  331. }
  332. } else {
  333. // 根据配置获取表格行数据
  334. tableRows, e = tradeAnalysisService.GetTableRowsDataByConfig(tableConfig)
  335. if e != nil {
  336. br.Msg = "获取失败"
  337. br.ErrMsg = fmt.Sprintf("获取表格行数据失败, %v", e)
  338. return
  339. }
  340. }
  341. // 响应
  342. resp := new(tradeAnalysisResponse.TableResp)
  343. resp.ExcelInfoId = excelItem.ExcelInfoId
  344. resp.TableData = tableRows
  345. resp.TableName = strings.TrimSpace(excelItem.ExcelName)
  346. resp.ClassifyId = excelItem.ExcelClassifyId
  347. resp.ModifyTime = utils.TimeTransferString(utils.FormatDateTime, excelItem.ModifyTime)
  348. resp.TableConfig = tableConfig
  349. resp.TableCols = make([]*tradeAnalysisModel.TradeAnalysisTableColumnItem, 0)
  350. // 获取表头信息
  351. {
  352. colOb := new(tradeAnalysisModel.TradeAnalysisTableColumn)
  353. cond := fmt.Sprintf(" AND %s = ?", colOb.Cols().ExcelInfoId)
  354. pars := make([]interface{}, 0)
  355. pars = append(pars, excelId)
  356. configCols, e := colOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC", colOb.Cols().Sort))
  357. if e != nil {
  358. br.Msg = "获取失败"
  359. br.ErrMsg = fmt.Sprintf("获取表格表头设置失败, %v", e)
  360. return
  361. }
  362. // 表头信息为空
  363. if len(configCols) == 0 {
  364. defaultCols, e := colOb.GetItemsByCondition(``, make([]interface{}, 0), []string{}, fmt.Sprintf("%s ASC", colOb.Cols().Sort))
  365. if e != nil {
  366. br.Msg = "操作失败"
  367. br.ErrMsg = fmt.Sprintf("获取默认表头设置失败, %v", e)
  368. return
  369. }
  370. configCols = defaultCols
  371. }
  372. for _, v := range configCols {
  373. resp.TableCols = append(resp.TableCols, v.Format2Item())
  374. }
  375. }
  376. resp.Button = services.GetTradeAnalysisTableOpButton(excelItem.SysUserId, sysUser.AdminId, sysUser.RoleTypeCode, false)
  377. br.Data = resp
  378. br.Ret = 200
  379. br.Success = true
  380. br.Msg = "获取成功"
  381. }
  382. // SaveAs
  383. // @Title 表格另存为
  384. // @Description 表格另存为
  385. // @Param request body request.TableSaveAsReq true "type json string"
  386. // @Success 200 string "操作成功"
  387. // @router /table/save_as [post]
  388. func (this *TradeAnalysisTableController) SaveAs() {
  389. br := new(models.BaseResponse).Init()
  390. defer func() {
  391. if br.ErrMsg == "" {
  392. br.IsSendEmail = false
  393. }
  394. this.Data["json"] = br
  395. this.ServeJSON()
  396. }()
  397. sysUser := this.SysUser
  398. if sysUser == nil {
  399. br.Msg = "请登录"
  400. br.ErrMsg = "请登录,SysUser Is Empty"
  401. br.Ret = 408
  402. return
  403. }
  404. var req tradeAnalysisRequest.TableSaveAsReq
  405. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  406. br.Msg = "参数解析异常"
  407. br.ErrMsg = fmt.Sprintf("参数解析异常: %v", e)
  408. return
  409. }
  410. req.TableName = strings.TrimSpace(req.TableName)
  411. if req.TableName == "" {
  412. br.Msg = "请输入表格名称"
  413. return
  414. }
  415. if req.ClassifyId <= 0 {
  416. br.Msg = "请选择分类"
  417. return
  418. }
  419. // 原表格
  420. excelItem, e := excel.GetExcelInfoById(req.ExcelInfoId)
  421. if e != nil {
  422. if e.Error() == utils.ErrNoRow() {
  423. br.Msg = "原表格不存在, 请刷新页面"
  424. return
  425. }
  426. br.Msg = "操作失败"
  427. br.ErrMsg = fmt.Sprintf("获取表格信息失败, %v", e)
  428. return
  429. }
  430. // 新表格
  431. timestamp := strconv.FormatInt(time.Now().UnixNano(), 10) + "_" + utils.GetRandString(10)
  432. newExcel := &excel.ExcelInfo{
  433. ExcelName: req.TableName,
  434. Source: utils.TRADE_ANALYSIS_TABLE,
  435. ExcelType: 1,
  436. UniqueCode: utils.MD5(utils.EXCEL_DATA_PREFIX + "_" + timestamp),
  437. ExcelClassifyId: req.ClassifyId,
  438. SysUserId: sysUser.AdminId,
  439. SysUserRealName: sysUser.RealName,
  440. Content: excelItem.Content,
  441. ExtraConfig: excelItem.ExtraConfig,
  442. UpdateUserId: sysUser.AdminId,
  443. UpdateUserRealName: sysUser.RealName,
  444. ModifyTime: time.Now(),
  445. CreateTime: time.Now(),
  446. }
  447. if e := newExcel.Create(); e != nil {
  448. br.Msg = "操作失败"
  449. br.ErrMsg = fmt.Sprintf("新增表格失败, %v", e)
  450. return
  451. }
  452. // 表头信息
  453. columnOb := new(tradeAnalysisModel.TradeAnalysisTableColumn)
  454. configColumns := make([]*tradeAnalysisModel.TradeAnalysisTableColumn, 0)
  455. {
  456. cond := fmt.Sprintf(` AND %s = ?`, columnOb.Cols().ExcelInfoId)
  457. pars := make([]interface{}, 0)
  458. pars = append(pars, req.ExcelInfoId)
  459. originColumns, e := columnOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC", columnOb.Cols().Sort))
  460. if e != nil {
  461. br.Msg = "操作失败"
  462. br.ErrMsg = fmt.Sprintf("获取默认表头设置失败, %v", e)
  463. return
  464. }
  465. for _, v := range originColumns {
  466. v.Id = 0
  467. v.ExcelInfoId = newExcel.ExcelInfoId
  468. v.CreateTime = time.Now()
  469. v.ModifyTime = time.Now()
  470. configColumns = append(configColumns, v)
  471. }
  472. }
  473. // 没找到就获取默认的表头信息
  474. if len(configColumns) == 0 {
  475. cond := fmt.Sprintf(` AND %s = 0`, columnOb.Cols().ExcelInfoId)
  476. defaultCols, e := columnOb.GetItemsByCondition(cond, make([]interface{}, 0), []string{}, fmt.Sprintf("%s ASC", columnOb.Cols().Sort))
  477. if e != nil {
  478. br.Msg = "操作失败"
  479. br.ErrMsg = fmt.Sprintf("获取默认表头设置失败, %v", e)
  480. return
  481. }
  482. for _, v := range defaultCols {
  483. v.Id = 0
  484. v.ExcelInfoId = newExcel.ExcelInfoId
  485. v.CreateTime = time.Now()
  486. v.ModifyTime = time.Now()
  487. configColumns = append(configColumns, v)
  488. }
  489. }
  490. // 新增表头
  491. if e = columnOb.CreateMulti(configColumns); e != nil {
  492. br.Msg = "操作失败"
  493. br.ErrMsg = fmt.Sprintf("批量新增表头配置失败, %v", e)
  494. return
  495. }
  496. br.Data = newExcel.ExcelInfoId
  497. br.Ret = 200
  498. br.Success = true
  499. br.Msg = "操作成功"
  500. }
  501. // Refresh
  502. // @Title 刷新表格
  503. // @Description 刷新表格
  504. // @Param request body request.TableRefreshReq true "type json string"
  505. // @Success 200 string "操作成功"
  506. // @router /table/refresh [post]
  507. func (this *TradeAnalysisTableController) Refresh() {
  508. br := new(models.BaseResponse).Init()
  509. defer func() {
  510. if br.ErrMsg == "" {
  511. br.IsSendEmail = false
  512. }
  513. this.Data["json"] = br
  514. this.ServeJSON()
  515. }()
  516. sysUser := this.SysUser
  517. if sysUser == nil {
  518. br.Msg = "请登录"
  519. br.ErrMsg = "请登录,SysUser Is Empty"
  520. br.Ret = 408
  521. return
  522. }
  523. var req tradeAnalysisRequest.TableRefreshReq
  524. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  525. br.Msg = "参数解析异常"
  526. br.ErrMsg = fmt.Sprintf("参数解析异常: %v", e)
  527. return
  528. }
  529. if req.ExcelInfoId <= 0 {
  530. br.Msg = "参数有误"
  531. br.ErrMsg = fmt.Sprintf("表格ID有误: %d", req.ExcelInfoId)
  532. return
  533. }
  534. cacheKey := fmt.Sprintf("%s_%d", utils.CACHE_EXCEL_REFRESH, req.ExcelInfoId)
  535. if utils.Rc.IsExist(cacheKey) {
  536. br.Ret = 200
  537. br.Success = true
  538. br.Msg = "系统处理中,请勿频繁操作"
  539. return
  540. }
  541. utils.Rc.SetNX(cacheKey, 1, 2*time.Minute)
  542. defer func() {
  543. _ = utils.Rc.Delete(cacheKey)
  544. }()
  545. // 获取表格信息
  546. item, e := excel.GetExcelInfoById(req.ExcelInfoId)
  547. if e != nil {
  548. if e.Error() == utils.ErrNoRow() {
  549. br.Msg = "表格不存在, 请刷新页面"
  550. return
  551. }
  552. br.Msg = "操作失败"
  553. br.ErrMsg = fmt.Sprintf("获取表格信息失败, %v", e)
  554. return
  555. }
  556. // 获取表格内容
  557. var tableConfig tradeAnalysisModel.TableExtraConfig
  558. if item.ExtraConfig == "" {
  559. br.Msg = "操作失败"
  560. br.ErrMsg = fmt.Sprintf("表格配置为空, ExcelId: %d", item.ExcelInfoId)
  561. return
  562. }
  563. if e = json.Unmarshal([]byte(item.ExtraConfig), &tableConfig); e != nil {
  564. br.Msg = "操作失败"
  565. br.ErrMsg = fmt.Sprintf("表格配置解析失败, ExcelId: %d, Err: %v", item.ExcelInfoId, e)
  566. return
  567. }
  568. tableData, e := tradeAnalysisService.GetTableRowsDataByConfig(tableConfig)
  569. if e != nil {
  570. br.Msg = "操作失败"
  571. br.ErrMsg = fmt.Sprintf("获取表格行数据失败, %v", e)
  572. return
  573. }
  574. content, e := json.Marshal(tableData)
  575. if e != nil {
  576. br.Msg = "表格数据JSON格式化失败"
  577. br.ErrMsg = fmt.Sprintf("表格数据JSON格式化失败, %v", e)
  578. return
  579. }
  580. item.Content = string(content)
  581. // 更新内容
  582. updateCols := []string{"Content", "ModifyTime"}
  583. if e = item.Update(updateCols); e != nil {
  584. br.Msg = "操作失败"
  585. br.ErrMsg = fmt.Sprintf("更新表格数据失败, %v", e)
  586. return
  587. }
  588. br.Ret = 200
  589. br.Success = true
  590. br.Msg = "操作成功"
  591. }
  592. // Remove
  593. // @Title 删除表格
  594. // @Description 删除表格
  595. // @Param request body request.TableRemoveReq true "type json string"
  596. // @Success 200 string "操作成功"
  597. // @router /table/remove [post]
  598. func (this *TradeAnalysisTableController) Remove() {
  599. br := new(models.BaseResponse).Init()
  600. defer func() {
  601. if br.ErrMsg == "" {
  602. br.IsSendEmail = false
  603. }
  604. this.Data["json"] = br
  605. this.ServeJSON()
  606. }()
  607. sysUser := this.SysUser
  608. if sysUser == nil {
  609. br.Msg = "请登录"
  610. br.ErrMsg = "请登录,SysUser Is Empty"
  611. br.Ret = 408
  612. return
  613. }
  614. var req tradeAnalysisRequest.TableRemoveReq
  615. if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
  616. br.Msg = "参数解析异常"
  617. br.ErrMsg = fmt.Sprintf("参数解析异常: %v", e)
  618. return
  619. }
  620. if req.ExcelInfoId <= 0 {
  621. br.Msg = "参数有误"
  622. br.ErrMsg = fmt.Sprintf("表格ID有误: %d", req.ExcelInfoId)
  623. return
  624. }
  625. // 获取表格信息
  626. excelItem, e := excel.GetExcelInfoById(req.ExcelInfoId)
  627. if e != nil {
  628. if e.Error() == utils.ErrNoRow() {
  629. br.Ret = 200
  630. br.Success = true
  631. br.Msg = "操作成功"
  632. return
  633. }
  634. br.Msg = "获取失败"
  635. br.ErrMsg = fmt.Sprintf("获取表格信息失败, %v", e)
  636. return
  637. }
  638. // 软删
  639. excelItem.IsDelete = 1
  640. excelItem.UpdateUserId = sysUser.AdminId
  641. excelItem.UpdateUserRealName = sysUser.RealName
  642. excelItem.ModifyTime = time.Now()
  643. updateCols := []string{"IsDelete", "UpdateUserId", "UpdateUserRealName", "ModifyTime"}
  644. if e = excelItem.Update(updateCols); e != nil {
  645. br.Msg = "操作失败"
  646. br.ErrMsg = fmt.Sprintf("删除表格信息失败, %v", e)
  647. return
  648. }
  649. br.Ret = 200
  650. br.Success = true
  651. br.Msg = "操作成功"
  652. }
  653. // Download
  654. // @Title 下载表格
  655. // @Description 下载表格
  656. // @Param ExcelInfoId query int true "表格ID"
  657. // @Success 200 下载成功
  658. // @router /table/download [get]
  659. func (this *TradeAnalysisTableController) Download() {
  660. br := new(models.BaseResponse).Init()
  661. defer func() {
  662. if br.ErrMsg == "" {
  663. br.IsSendEmail = false
  664. utils.FileLog.Info(fmt.Sprintf("多空分析表格-下载失败, ErrMsg: %s", br.ErrMsg))
  665. }
  666. this.Data["json"] = br
  667. this.ServeJSON()
  668. }()
  669. sysUser := this.SysUser
  670. if sysUser == nil {
  671. br.Msg = "请登录"
  672. br.ErrMsg = "请登录,SysUser Is Empty"
  673. br.Ret = 408
  674. return
  675. }
  676. excelId, _ := this.GetInt("ExcelInfoId")
  677. if excelId <= 0 {
  678. br.Msg = "参数有误"
  679. br.ErrMsg = fmt.Sprintf("表格ID有误: %d", excelId)
  680. return
  681. }
  682. // 获取表格信息
  683. excelItem, e := excel.GetExcelInfoById(excelId)
  684. if e != nil {
  685. br.Msg = "下载失败"
  686. br.ErrMsg = fmt.Sprintf("获取表格信息失败, %v", e)
  687. return
  688. }
  689. // 获取表头信息
  690. configCols := make([]*tradeAnalysisModel.TradeAnalysisTableColumn, 0)
  691. {
  692. colOb := new(tradeAnalysisModel.TradeAnalysisTableColumn)
  693. cond := fmt.Sprintf(" AND %s = ?", colOb.Cols().ExcelInfoId)
  694. pars := make([]interface{}, 0)
  695. pars = append(pars, excelId)
  696. items, e := colOb.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s ASC", colOb.Cols().Sort))
  697. if e != nil {
  698. br.Msg = "下载失败"
  699. br.ErrMsg = fmt.Sprintf("获取表格表头设置失败, %v", e)
  700. return
  701. }
  702. configCols = items
  703. // 表头信息为空
  704. if len(configCols) == 0 {
  705. defaultCols, e := colOb.GetItemsByCondition(``, make([]interface{}, 0), []string{}, fmt.Sprintf("%s ASC", colOb.Cols().Sort))
  706. if e != nil {
  707. br.Msg = "下载失败"
  708. br.ErrMsg = fmt.Sprintf("获取默认表头设置失败, %v", e)
  709. return
  710. }
  711. configCols = defaultCols
  712. }
  713. }
  714. if len(configCols) == 0 {
  715. br.Msg = "下载失败"
  716. br.ErrMsg = fmt.Sprintf("表头信息异常")
  717. return
  718. }
  719. // 表格数据
  720. tableRows := make([]*tradeAnalysisModel.TableRowData, 0)
  721. var tableConfig tradeAnalysisModel.TableExtraConfig
  722. if excelItem.Content != "" {
  723. if e = json.Unmarshal([]byte(excelItem.Content), &tableRows); e != nil {
  724. br.Msg = "获取失败"
  725. br.ErrMsg = fmt.Sprintf("解析表格配置失败, err: %v, config: %s", e, excelItem.ExtraConfig)
  726. return
  727. }
  728. } else {
  729. // 根据配置获取表格行数据
  730. if e = json.Unmarshal([]byte(excelItem.ExtraConfig), &tableConfig); e != nil {
  731. br.Msg = "获取失败"
  732. br.ErrMsg = fmt.Sprintf("解析表格配置失败, err: %v, config: %s", e, excelItem.ExtraConfig)
  733. return
  734. }
  735. tableRows, e = tradeAnalysisService.GetTableRowsDataByConfig(tableConfig)
  736. if e != nil {
  737. br.Msg = "获取失败"
  738. br.ErrMsg = fmt.Sprintf("获取表格行数据失败, %v", e)
  739. return
  740. }
  741. }
  742. // 生成excel
  743. dir, _ := os.Executable()
  744. exPath := filepath.Dir(dir)
  745. downFile := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  746. xlsxFile := xlsx.NewFile()
  747. sheetNew := new(xlsx.Sheet)
  748. sheetNew, e = xlsxFile.AddSheet("Sheet1")
  749. if e != nil {
  750. br.Msg = "下载失败"
  751. br.ErrMsg = fmt.Sprintf("生成Sheet失败, %v", e)
  752. return
  753. }
  754. // 第一行-表头
  755. headerRow := sheetNew.AddRow()
  756. var headerKeys []string
  757. for _, v := range configCols {
  758. if v.IsShow == 0 {
  759. continue
  760. }
  761. headerRow.AddCell().SetValue(v.ColumnName)
  762. headerKeys = append(headerKeys, v.ColumnKey)
  763. }
  764. // 数据行
  765. for _, v := range tableRows {
  766. row := sheetNew.AddRow()
  767. rv := reflect.ValueOf(v)
  768. // v为指针先解引用
  769. if rv.Kind() == reflect.Ptr {
  770. rv = rv.Elem()
  771. }
  772. // 获取对应key的值
  773. for _, h := range headerKeys {
  774. fieldVal := rv.FieldByName(h)
  775. if fieldVal.IsValid() {
  776. cell := row.AddCell()
  777. cell.SetString(fmt.Sprint(fieldVal.Interface()))
  778. continue
  779. }
  780. row.AddCell().SetString("")
  781. }
  782. }
  783. // 保存文件
  784. if e = xlsxFile.Save(downFile); e != nil {
  785. br.Msg = "下载失败"
  786. br.ErrMsg = fmt.Sprintf("保存文件内容失败, %v", e)
  787. return
  788. }
  789. fileName := fmt.Sprintf(`%s%s.xlsx`, excelItem.ExcelName, time.Now().Format("06.01.02"))
  790. this.Ctx.Output.Download(downFile, fileName)
  791. defer func() {
  792. _ = os.Remove(downFile)
  793. }()
  794. br.Ret = 200
  795. br.Success = true
  796. br.Msg = "下载成功"
  797. }