DataController.java 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. package com.xunmei.deploy.controller;
  2. import cn.hutool.core.date.DateUtil;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.baomidou.mybatisplus.core.metadata.IPage;
  6. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  7. import com.github.zafarkhaja.semver.Version;
  8. import com.xunmei.common.core.domain.R;
  9. import com.xunmei.common.core.utils.DateUtils;
  10. import com.xunmei.common.core.utils.StringUtils;
  11. import com.xunmei.common.core.web.domain.AjaxResult;
  12. import com.xunmei.common.core.web.page.TableDataInfo;
  13. import com.xunmei.deploy.domain.*;
  14. import com.xunmei.deploy.service.*;
  15. import com.xunmei.deploy.util.CommonConstraint;
  16. import com.xunmei.deploy.util.CommonUtils;
  17. import com.xunmei.deploy.util.Message;
  18. import com.xunmei.deploy.vo.HostInfoVo;
  19. import jxl.Workbook;
  20. import jxl.WorkbookSettings;
  21. import jxl.write.*;
  22. import jxl.write.biff.RowsExceededException;
  23. import org.slf4j.Logger;
  24. import org.slf4j.LoggerFactory;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.transaction.annotation.Transactional;
  27. import org.springframework.web.bind.annotation.*;
  28. import org.springframework.web.multipart.MultipartFile;
  29. import javax.servlet.http.HttpServletRequest;
  30. import javax.servlet.http.HttpServletResponse;
  31. import java.io.*;
  32. import java.text.SimpleDateFormat;
  33. import java.util.*;
  34. @RestController
  35. @RequestMapping("/deployData")
  36. public class DataController extends BaseController{
  37. private Logger log = LoggerFactory.getLogger(getClass());
  38. @Autowired
  39. private HostInfoService hostInfoService;
  40. @Autowired
  41. private AppRunInfoService appRunInfoService;
  42. @Autowired
  43. private FrontTaskService frontTaskService;
  44. @Autowired
  45. private HostZipInfoService hostZipInfoService;
  46. @Autowired
  47. private PackageInfoService packageInfoService;
  48. @Autowired
  49. private ZipPackInfoService zipPackInfoService;
  50. @Autowired
  51. private UploadAppInfoService uploadAppInfoService;
  52. @Autowired
  53. private UpgradeBatchInfoService upgradeBatchInfoService;
  54. @Autowired
  55. private UpgradeTaskService upgradeTaskService;
  56. @Autowired
  57. private OperationLogService logService;
  58. /**
  59. * 主机管理-列表
  60. * @param page
  61. * @param hostInfoVo
  62. * @return
  63. */
  64. @PostMapping("/hostInfoList")
  65. public TableDataInfo hostInfoList(Page<HostInfo> page, HostInfoVo hostInfoVo){
  66. IPage<HostInfoVo> result = hostInfoService.selectHostInfoPage(page, hostInfoVo);
  67. return getDataTable(result);
  68. }
  69. /**
  70. * 主机管理--详情页(应用列表)
  71. * @param hostId
  72. * @return
  73. */
  74. @PostMapping("/getRunApps")
  75. public List<AppRunInfo> getRunApps(String hostId){
  76. List<AppRunInfo> appRunningInfos = appRunInfoService.getByHostId(hostId);
  77. return appRunningInfos;
  78. }
  79. /**
  80. * 主机管理--批量升级
  81. */
  82. @PostMapping("/addHostBatch")
  83. public AjaxResult addHostBatch(String zipId,@RequestParam("hostIds[]") String[] hostIds){
  84. try {
  85. String msg = upgradeBatchInfoService.addHostUpgradeBatch(zipId, hostIds);
  86. return AjaxResult.success(msg);
  87. } catch (Exception e) {
  88. log.error("主机管理-批量升级出现异常:{}",e);
  89. return AjaxResult.error("升级批次创建失败,"+e.getMessage());
  90. }
  91. }
  92. /**
  93. * 主机管理--批量升级-先获取已上传的升级包下拉框
  94. * @return
  95. */
  96. @RequestMapping("/getZipInfos")
  97. public Message getZipInfos(){
  98. List<ZipPackInfo> list = zipPackInfoService.getList();
  99. Collections.sort(list, new Comparator<ZipPackInfo>() {
  100. @Override
  101. public int compare(ZipPackInfo o1, ZipPackInfo o2) {
  102. String o1ZipVersion = o1.getZipVersion();
  103. String o2ZipVersion = o2.getZipVersion();
  104. Version oldVersion,newVersion;
  105. try {
  106. oldVersion = Version.valueOf(o1ZipVersion);
  107. } catch (Exception e) {
  108. oldVersion = Version.valueOf("1.0.0");
  109. }
  110. try {
  111. newVersion = Version.valueOf(o2ZipVersion);
  112. } catch (Exception e) {
  113. newVersion = Version.valueOf("1.0.0");
  114. }
  115. if (oldVersion.compareTo(newVersion) > 0){
  116. return -1;
  117. } else if (oldVersion.compareTo(newVersion) < 0) {
  118. return 1;
  119. } else {
  120. return 0;
  121. }
  122. }
  123. });
  124. JSONArray array = new JSONArray();
  125. JSONObject json;
  126. for (ZipPackInfo zipPackInfo : list) {
  127. json = new JSONObject();
  128. json.put("id",zipPackInfo.getId());
  129. if (StringUtils.isEmpty(zipPackInfo.getHostTypeName())){
  130. json.put("text",zipPackInfo.getZipVersion());
  131. }else {
  132. json.put("text",zipPackInfo.getZipVersion() + "-" + zipPackInfo.getHostTypeName());
  133. }
  134. array.add(json);
  135. }
  136. return Message.success(null,array);
  137. }
  138. /**
  139. * 主机管理--批量升级-先根据页面勾选的主机筛选出可升级的主机hostIds,返回给页面操作升级
  140. */
  141. @PostMapping("/validateHostBatch")
  142. public AjaxResult validateHostBatch(String upgradeZipId,@RequestParam("hostIds[]") String[] hostIds){
  143. if(StringUtils.isEmpty(upgradeZipId)){
  144. return AjaxResult.error("请选择升级包!");
  145. }
  146. if(hostIds == null || hostIds.length == 0){
  147. return AjaxResult.error("请选择需要升级的主机!");
  148. }
  149. try {
  150. Map<String, Object> data = upgradeBatchInfoService.validateHostBatch(upgradeZipId, hostIds);
  151. return AjaxResult.success(data);
  152. } catch (Exception e) {
  153. log.error("主机管理--批量升级-根据页面勾选的主机筛选出可升级的主机出现异常:{}",e);
  154. return AjaxResult.error(e.getMessage());
  155. }
  156. }
  157. /**
  158. * 主机管理--重启服务(创建任务)
  159. * @param hostId
  160. **/
  161. @PostMapping("/createTask")
  162. public Message createTask(String hostId){
  163. try {
  164. frontTaskService.createRestartAppTask(hostId);
  165. return Message.success(null,null);
  166. } catch (Exception e) {
  167. log.error("主机管理-重启服务(创建任务)出现异常:{}",e);
  168. return Message.error(e.getMessage());
  169. }
  170. }
  171. /**
  172. * 主机管理--删除主机
  173. **/
  174. @PostMapping("/deleteByHostId")
  175. @Transactional(rollbackFor = Exception.class)
  176. public R deleteByHostId(String id,String userId,HttpServletRequest request){
  177. if(StringUtils.isEmpty(userId)){
  178. return R.fail("删除失败,操作用户为空或会话过期,请尝试重新登录!");
  179. }
  180. return hostInfoService.deleteByHostId(id);
  181. }
  182. /**
  183. * 主机管理--删除主机-先验证主机是否存在升级任务
  184. * @param hostId
  185. * @return
  186. */
  187. @RequestMapping("/getUpgradeTaskByHostId")
  188. @ResponseBody
  189. public Message getUpgradeTaskByHostId(String hostId){
  190. try {
  191. List<UpgradeTask> list = upgradeTaskService.getByHostId(hostId);
  192. return Message.success("获取获取任务成功!",list);
  193. } catch (Exception e) {
  194. log.error("主机管理--删除主机-查询主机当前的升级任务出现异常:{}",e);
  195. return Message.error("获取任务失败,"+e.getMessage());
  196. }
  197. }
  198. /**
  199. * 主机管理--当前版本号下拉框
  200. **/
  201. @RequestMapping("/getZipInfos3")
  202. public Message getZipInfos3(){
  203. List<String> list = hostZipInfoService.selectOldList();
  204. return Message.success(null,CommonUtils.sortVersion(list));
  205. }
  206. /**
  207. * 主机管理--目标版本号下拉框
  208. **/
  209. @RequestMapping("/getZipInfos4")
  210. public Message getZipInfos4(){
  211. List<String> list = hostZipInfoService.selectTargetList();
  212. return Message.success(null,CommonUtils.sortVersion(list));
  213. }
  214. /**
  215. * 主机管理--白令海版本号下拉框
  216. */
  217. @RequestMapping("/getAgentVersion")
  218. public Message getAgentVersion(){
  219. List<String> list = packageInfoService.selectAgentVersion();
  220. return Message.success(null,CommonUtils.sortVersion(list));
  221. }
  222. /**
  223. * 主机管理--导出
  224. */
  225. @RequestMapping(value = "/hostInfoListExport")
  226. public void hostInfoListExport(
  227. HostInfoVo hostInfoVo,
  228. HttpServletResponse response) throws Exception {
  229. Page<HostInfo> page = new Page<>();
  230. page.setSize(5000);
  231. page.setCurrent(1);
  232. IPage<HostInfoVo> result = hostInfoService.selectHostInfoPage(page, hostInfoVo);
  233. List<HostInfoVo> list = result.getRecords();
  234. Label label = null;
  235. WritableSheet sheet = null;
  236. WritableWorkbook workBook = null;
  237. WorkbookSettings settings = new WorkbookSettings();
  238. settings.setWriteAccess(null);
  239. ByteArrayOutputStream os = new ByteArrayOutputStream();
  240. InputStream is = null;
  241. OutputStream out = null;
  242. try {
  243. workBook = Workbook.createWorkbook(os, settings);
  244. sheet = workBook.createSheet("sheet1", 0);
  245. sheet.setColumnView(0, 30);//设置Excel的宽度
  246. sheet.setColumnView(1, 30);
  247. sheet.setColumnView(2, 30);
  248. sheet.setColumnView(3, 30);
  249. sheet.setColumnView(4, 30);
  250. sheet.setColumnView(5, 30);
  251. sheet.setColumnView(6, 30);
  252. sheet.setColumnView(7, 30);
  253. sheet.setColumnView(8, 30);
  254. sheet.setColumnView(9, 30);
  255. sheet.setColumnView(10, 30);
  256. sheet.setColumnView(11, 30);
  257. this.addHostInfoToLabel(label, sheet);//向Excel添加标题
  258. this.addHostContentInfoToLabel(label, sheet, list);//向Excel添加表格数据
  259. workBook.write();
  260. } catch (Exception e) {
  261. log.error("主机管理excel导出处理数据出现异常:{}", e);
  262. } finally {
  263. workBook.close();
  264. }
  265. try {
  266. is = new ByteArrayInputStream(os.toByteArray());
  267. out = response.getOutputStream();
  268. // 设置输出文件信息
  269. response.setContentType("application/octet-stream;charset=UTF-8");
  270. response.addHeader("Content-Disposition", "attachment;filename=" + new String(("主机管理列表" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xls").getBytes("gb2312"), "ISO8859-1"));
  271. // 写文件流
  272. byte[] buffer = new byte[6 * 1024];
  273. int len = 0;
  274. while ((len = is.read(buffer, 0, buffer.length)) != -1) {
  275. out.write(buffer, 0, len);
  276. }
  277. } catch (IOException e) {
  278. log.error("主机管理excel导出处理流出现异常:{}", e);
  279. } finally {
  280. is.close();
  281. out.close();
  282. }
  283. }
  284. /**
  285. * 升级包管理-列表
  286. */
  287. @PostMapping("/zipList")
  288. public TableDataInfo zipList(Page<ZipPackInfo> page, ZipPackInfo info){
  289. IPage<ZipPackInfo> result = zipPackInfoService.selectPage(page,info);
  290. return getDataTable(result);
  291. }
  292. /**
  293. * 升级包管理-适用主机下拉框
  294. */
  295. @PostMapping("/zipHostType")
  296. public Message zipHostType(){
  297. List<String> list = zipPackInfoService.getZipHostType();
  298. JSONArray array = new JSONArray();
  299. JSONObject json;
  300. for(String name:list){
  301. json = new JSONObject();
  302. json.put("id",name);
  303. json.put("text",name);
  304. array.add(json);
  305. }
  306. return Message.success(null,array);
  307. }
  308. /**
  309. * 升级包管理-详情页(升级包内应用列表)
  310. */
  311. @RequestMapping("/uploadList")
  312. public List<UploadAppInfo> uploadList(String id){
  313. List<UploadAppInfo> list = uploadAppInfoService.getListByZipId(id);
  314. return list;
  315. }
  316. /**
  317. * 升级包管理-上传升级包
  318. */
  319. @PostMapping("/zipUpload")
  320. public String fileUpload(MultipartFile file, HttpServletResponse response){
  321. try {
  322. uploadAppInfoService.uploadZip(file);
  323. return "压缩包解压完成!";
  324. } catch (Exception e) {
  325. log.error("上传升级包出现异常:{}",e);
  326. response.setStatus(500);
  327. return "解析包错误:"+e.getMessage();
  328. }
  329. }
  330. /**
  331. * 升级包管理-删除
  332. */
  333. @RequestMapping("/delZip")
  334. public AjaxResult delZip(String ids){
  335. return zipPackInfoService.delZip(ids);
  336. }
  337. /**
  338. * 升级状态查询-列表
  339. */
  340. @PostMapping("/batchList")
  341. public TableDataInfo batchList(Page<UpgradeBatchInfo> page, UpgradeBatchInfo info){
  342. IPage<UpgradeBatchInfo> result = upgradeBatchInfoService.selectPage(page,info);
  343. return getDataTable(result);
  344. }
  345. /**
  346. * 升级状态查询-详情
  347. */
  348. @PostMapping("/batchHostDetail")
  349. public List<BatchHostInfo> batchHostDetail(String id,String orgName,Integer status){
  350. List<BatchHostInfo> batchHosts = upgradeBatchInfoService.getBatchHosts(id,orgName,status);
  351. return batchHosts;
  352. }
  353. /**
  354. * 升级状态查询-详情下的二级详情
  355. */
  356. @PostMapping("/batchTask")
  357. public List<UpgradeTask> batchTask(String hostId,String batchNumber){
  358. List<UpgradeTask> upgradeTasks = upgradeTaskService.batchTask(hostId, batchNumber);
  359. for (UpgradeTask upgradeTask : upgradeTasks) {
  360. String deployDescription = upgradeTask.getDeployDescription();
  361. if (StringUtils.isNotEmpty(deployDescription)) {
  362. try {
  363. String[] split = deployDescription.split(CommonConstraint.UpgradeDescriptionConstant.SPLIT);
  364. String desc1 = CommonConstraint.UpgradeDescriptionConstant.DESCRIPTION.get(split[0]);
  365. String desc2 = CommonConstraint.UpgradeDescriptionConstant.DESCRIPTION.get(split[1]);
  366. if (null == desc1 || "null".equals(desc1)) {
  367. desc1 = "";
  368. }
  369. if (null == desc2 || "null".equals(desc2)) {
  370. desc2 = "";
  371. }
  372. upgradeTask.setDeployDescription(desc1 + CommonConstraint.UpgradeDescriptionConstant.SPLIT + desc2);
  373. } catch (Exception e) {
  374. log.error("查看升级应用详情出现异常:{}",e);
  375. }
  376. }
  377. }
  378. return upgradeTasks;
  379. }
  380. /**
  381. * 升级状态查询-详情导出
  382. */
  383. @RequestMapping(value = "/batchHostDetailExport")
  384. public void batchHostDetailExport(
  385. String id,String orgName,Integer status,
  386. HttpServletResponse response) throws Exception {
  387. Page<HostInfo> page = new Page<>();
  388. page.setSize(5000);
  389. page.setCurrent(1);
  390. List<BatchHostInfo> list = upgradeBatchInfoService.getBatchHosts(id,orgName,status);
  391. Label label = null;
  392. WritableSheet sheet = null;
  393. WritableWorkbook workBook = null;
  394. WorkbookSettings settings = new WorkbookSettings();
  395. settings.setWriteAccess(null);
  396. ByteArrayOutputStream os = new ByteArrayOutputStream();
  397. InputStream is = null;
  398. OutputStream out = null;
  399. try {
  400. workBook = Workbook.createWorkbook(os, settings);
  401. sheet = workBook.createSheet("sheet1", 0);
  402. sheet.setColumnView(0, 30);//设置Excel的宽度
  403. sheet.setColumnView(1, 30);
  404. sheet.setColumnView(2, 30);
  405. sheet.setColumnView(3, 30);
  406. sheet.setColumnView(4, 30);
  407. sheet.setColumnView(5, 30);
  408. sheet.setColumnView(6, 30);
  409. sheet.setColumnView(7, 30);
  410. sheet.setColumnView(8, 30);
  411. this.addBatchHostDetailToLabel(label, sheet);//向Excel添加标题
  412. this.addBatchHostContentToLabel(label, sheet, list);//向Excel添加表格数据
  413. workBook.write();
  414. } catch (Exception e) {
  415. log.error("excel导出异常:{}", e);
  416. } finally {
  417. workBook.close();
  418. }
  419. // 根据批次Id 获取批次code
  420. UpgradeBatchInfo byId = upgradeBatchInfoService.getById(id);
  421. String batchCode = byId.getBatchCode();
  422. try {
  423. is = new ByteArrayInputStream(os.toByteArray());
  424. out = response.getOutputStream();
  425. // 设置输出文件信息
  426. response.setContentType("application/octet-stream;charset=UTF-8");
  427. response.addHeader("Content-Disposition", "attachment;filename=" + new String(("升级主机详情" + batchCode + ".xls").getBytes("gb2312"), "ISO8859-1"));
  428. // 写文件流
  429. byte[] buffer = new byte[6 * 1024];
  430. int len = 0;
  431. while ((len = is.read(buffer, 0, buffer.length)) != -1) {
  432. out.write(buffer, 0, len);
  433. }
  434. } catch (IOException e) {
  435. log.error("excel导出异常:{}", e);
  436. } finally {
  437. is.close();
  438. out.close();
  439. }
  440. }
  441. /**
  442. * 主机管理-列表导出(给Excel导出增加标题)
  443. */
  444. private WritableSheet addHostInfoToLabel(Label label, WritableSheet sheet) throws RowsExceededException, WriteException {
  445. WritableFont font1 = new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD);
  446. WritableCellFormat cellFormat1 = new WritableCellFormat(font1);
  447. label = new Label(0, 0, "序号", cellFormat1);
  448. sheet.addCell(label);
  449. label = new Label(1, 0, "所属机构", cellFormat1);
  450. sheet.addCell(label);
  451. label = new Label(2, 0, "主机名称", cellFormat1);
  452. sheet.addCell(label);
  453. label = new Label(3, 0, "主机IP", cellFormat1);
  454. sheet.addCell(label);
  455. label = new Label(4, 0, "当前版本号", cellFormat1);
  456. sheet.addCell(label);
  457. label = new Label(5, 0, "目标版本号", cellFormat1);
  458. sheet.addCell(label);
  459. label = new Label(6, 0, "升级状态", cellFormat1);
  460. sheet.addCell(label);
  461. label = new Label(7, 0, "服务状态", cellFormat1);
  462. sheet.addCell(label);
  463. label = new Label(8, 0, "白令海状态", cellFormat1);
  464. sheet.addCell(label);
  465. label = new Label(9, 0, "传输加密", cellFormat1);
  466. sheet.addCell(label);
  467. label = new Label(10, 0, "白令海版本号", cellFormat1);
  468. sheet.addCell(label);
  469. label = new Label(11, 0, "注册时间", cellFormat1);
  470. sheet.addCell(label);
  471. return sheet;
  472. }
  473. /**
  474. * 升级状态查询-详情导出(给Excel导出增加标题)
  475. */
  476. private WritableSheet addBatchHostDetailToLabel(Label label, WritableSheet sheet) throws RowsExceededException, WriteException {
  477. //设置字体;
  478. WritableFont font1 = new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD);
  479. WritableCellFormat cellFormat1 = new WritableCellFormat(font1);
  480. label = new Label(0, 0, "序号", cellFormat1);
  481. sheet.addCell(label);
  482. label = new Label(1, 0, "批次号", cellFormat1);
  483. sheet.addCell(label);
  484. label = new Label(2, 0, "所属机构", cellFormat1);
  485. sheet.addCell(label);
  486. label = new Label(3, 0, "版本号", cellFormat1);
  487. sheet.addCell(label);
  488. label = new Label(4, 0, "状态", cellFormat1);
  489. sheet.addCell(label);
  490. label = new Label(5, 0, "主机名称", cellFormat1);
  491. sheet.addCell(label);
  492. label = new Label(6, 0, "主机IP", cellFormat1);
  493. sheet.addCell(label);
  494. label = new Label(7, 0, "创建时间", cellFormat1);
  495. sheet.addCell(label);
  496. label = new Label(8, 0, "结束时间", cellFormat1);
  497. sheet.addCell(label);
  498. return sheet;
  499. }
  500. /**
  501. * 主机管理-列表导出(往Excel中添加内容)
  502. */
  503. private void addHostContentInfoToLabel(Label label, WritableSheet sheet, List<HostInfoVo> list) throws Exception {
  504. WritableFont writableFont = new WritableFont(WritableFont.createFont("宋体"), 11, WritableFont.NO_BOLD, false);
  505. WritableCellFormat writableCellFormat = new WritableCellFormat(writableFont);
  506. writableCellFormat.setWrap(true);
  507. for (int i = 0; i < list.size(); i++) {
  508. HostInfoVo vdso = list.get(i);
  509. label = new Label(0, i + 1, i + 1 + "", writableCellFormat);
  510. sheet.addCell(label);
  511. label = new Label(1, i + 1, StringUtils.isNotEmpty(vdso.getHostOrgName()) ? vdso.getHostOrgName() : "-", writableCellFormat);
  512. sheet.addCell(label);
  513. label = new Label(2, i + 1, StringUtils.isNotEmpty(vdso.getHostName()) ? vdso.getHostName() : "-", writableCellFormat);
  514. sheet.addCell(label);
  515. label = new Label(3, i + 1, StringUtils.isNotEmpty(vdso.getHostIp()) ? vdso.getHostIp() : "-", writableCellFormat);
  516. sheet.addCell(label);
  517. label = new Label(4, i + 1, StringUtils.isNotEmpty(vdso.getZipVersion()) ? vdso.getZipVersion() : "-", writableCellFormat);
  518. sheet.addCell(label);
  519. label = new Label(5, i + 1, StringUtils.isNotEmpty(vdso.getTargetVersion()) ? vdso.getTargetVersion() : "-", writableCellFormat);
  520. sheet.addCell(label);
  521. String updateStatusStr = "-";
  522. if (vdso.getUpdateStatus() == null) {
  523. updateStatusStr = "-";
  524. } else if (vdso.getUpdateStatus() == 0) {
  525. updateStatusStr = "进行中";
  526. } else if (vdso.getUpdateStatus() == 1) {
  527. updateStatusStr = "成功";
  528. } else if (vdso.getUpdateStatus() == 2) {
  529. updateStatusStr = "失败";
  530. } else if (vdso.getUpdateStatus() == 3) {
  531. updateStatusStr = "重试中";
  532. }
  533. label = new Label(6, i + 1, updateStatusStr, writableCellFormat);
  534. sheet.addCell(label);
  535. label = new Label(7, i + 1, vdso.getServerStatus() == 1 ? "正常" : "异常", writableCellFormat);
  536. sheet.addCell(label);
  537. label = new Label(8, i + 1, vdso.getHostStatus() == 1 ? "在线" : "离线", writableCellFormat);
  538. sheet.addCell(label);
  539. label = new Label(9, i + 1, vdso.getEncryption() == 1 ? "是" : "否", writableCellFormat);
  540. sheet.addCell(label);
  541. label = new Label(10, i + 1, StringUtils.isNotEmpty(vdso.getAgentVersion()) ? vdso.getAgentVersion() : "-", writableCellFormat);
  542. sheet.addCell(label);
  543. label = new Label(11, i + 1, StringUtils.isNotEmpty(vdso.getRegisterDateTime()) ? vdso.getRegisterDateTime() : "-", writableCellFormat);
  544. sheet.addCell(label);
  545. }
  546. }
  547. /**
  548. * 升级状态查询-详情导出(往Excel中添加内容)
  549. */
  550. private void addBatchHostContentToLabel(Label label, WritableSheet sheet, List<BatchHostInfo> list) throws Exception {
  551. WritableFont writableFont = new WritableFont(WritableFont.createFont("宋体"), 11, WritableFont.NO_BOLD, false);
  552. WritableCellFormat writableCellFormat = new WritableCellFormat(writableFont);
  553. writableCellFormat.setWrap(true);
  554. for (int i = 0; i < list.size(); i++) {
  555. BatchHostInfo vdso = list.get(i);
  556. label = new Label(0, i + 1, i + 1 + "", writableCellFormat);
  557. sheet.addCell(label);
  558. label = new Label(1, i + 1, StringUtils.isNotEmpty(vdso.getBatchCode()) ? vdso.getBatchCode() : "-", writableCellFormat);
  559. sheet.addCell(label);
  560. label = new Label(2, i + 1, StringUtils.isNotEmpty(vdso.getOrgName()) ? vdso.getOrgName() : "-", writableCellFormat);
  561. sheet.addCell(label);
  562. label = new Label(3, i + 1, StringUtils.isNotEmpty(vdso.getZipVersion()) ? vdso.getZipVersion() : "-", writableCellFormat);
  563. sheet.addCell(label);
  564. String status = "-";
  565. if (vdso.getStatus() == 0) {
  566. status = "进行中";
  567. } else if (vdso.getStatus() == 1) {
  568. status = "成功";
  569. } else if (vdso.getStatus() == 2) {
  570. status = "失败";
  571. } else if (vdso.getStatus() == 3) {
  572. status = "重试中";
  573. }
  574. label = new Label(4, i + 1, status, writableCellFormat);
  575. sheet.addCell(label);
  576. label = new Label(5, i + 1, StringUtils.isNotEmpty(vdso.getHostName()) ? vdso.getHostName() : "-", writableCellFormat);
  577. sheet.addCell(label);
  578. label = new Label(6, i + 1, StringUtils.isNotEmpty(vdso.getHostIp()) ? vdso.getHostIp() : "-", writableCellFormat);
  579. sheet.addCell(label);
  580. label = new Label(7, i + 1, null != vdso.getCreateTime() ? DateUtil.format(vdso.getCreateTime(), DateUtils.YYYY_MM_DD_HH_MM_SS) : "-", writableCellFormat);
  581. sheet.addCell(label);
  582. label = new Label(8, i + 1, null != vdso.getFinishTime() ? DateUtil.format(vdso.getFinishTime(), DateUtils.YYYY_MM_DD_HH_MM_SS) : "-", writableCellFormat);
  583. sheet.addCell(label);
  584. }
  585. }
  586. /**
  587. * 日志-列表
  588. */
  589. @PostMapping("/logInfoList")
  590. public TableDataInfo logInfoList(Page<OperationLog> page, OperationLog aperationLog){
  591. IPage<OperationLog> result = logService.selectPage(page, aperationLog);
  592. return getDataTable(result);
  593. }
  594. /**
  595. * 日志-列表导出
  596. */
  597. @RequestMapping(value = "/logListExport")
  598. public void logListExport(OperationLog operationLog,HttpServletResponse response) throws Exception {
  599. Page<OperationLog> page = new Page<>();
  600. page.setSize(5000);
  601. page.setCurrent(1);
  602. IPage<OperationLog> result = logService.selectLogPage(page, operationLog);
  603. List<OperationLog> list = result.getRecords();
  604. Label label = null;
  605. WritableSheet sheet = null;
  606. WritableWorkbook workBook = null;
  607. WorkbookSettings settings = new WorkbookSettings();
  608. settings.setWriteAccess(null);
  609. ByteArrayOutputStream os = new ByteArrayOutputStream();
  610. InputStream is = null;
  611. OutputStream out = null;
  612. try {
  613. workBook = Workbook.createWorkbook(os, settings);
  614. sheet = workBook.createSheet("sheet1", 0);
  615. sheet.setColumnView(0, 30);//设置Excel的宽度
  616. sheet.setColumnView(1, 30);
  617. sheet.setColumnView(2, 30);
  618. sheet.setColumnView(3, 30);
  619. sheet.setColumnView(4, 30);
  620. sheet.setColumnView(5, 30);
  621. sheet.setColumnView(6, 30);
  622. sheet.setColumnView(7, 30);
  623. sheet.setColumnView(8, 30);
  624. sheet.setColumnView(9, 30);
  625. this.addLogToLabel(label, sheet);//向Excel添加标题
  626. this.addLogContentInfoToLabel(label, sheet, list);//向Excel添加表格数据
  627. workBook.write();
  628. } catch (Exception e) {
  629. log.error("excel导出异常", e);
  630. } finally {
  631. workBook.close();
  632. }
  633. try {
  634. is = new ByteArrayInputStream(os.toByteArray());
  635. out = response.getOutputStream();
  636. // 设置输出文件信息
  637. response.setContentType("application/octet-stream;charset=UTF-8");
  638. response.addHeader("Content-Disposition", "attachment;filename=" + new String(("日志管理列表" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xls").getBytes("gb2312"), "ISO8859-1"));
  639. // 写文件流
  640. byte[] buffer = new byte[6 * 1024];
  641. int len = 0;
  642. while ((len = is.read(buffer, 0, buffer.length)) != -1) {
  643. out.write(buffer, 0, len);
  644. }
  645. } catch (IOException e) {
  646. log.error("logListExport日志模块excel导出异常:{}", e);
  647. } finally {
  648. is.close();
  649. out.close();
  650. }
  651. }
  652. /**
  653. * 日志-列表导出(给Excel导出增加标题)
  654. */
  655. private WritableSheet addLogToLabel(Label label, WritableSheet sheet) throws RowsExceededException, WriteException {
  656. //设置字体;
  657. WritableFont font1 = new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD);
  658. WritableCellFormat cellFormat1 = new WritableCellFormat(font1);
  659. label = new Label(0, 0, "序号", cellFormat1);
  660. sheet.addCell(label);
  661. label = new Label(1, 0, "操作人", cellFormat1);
  662. sheet.addCell(label);
  663. label = new Label(2, 0, "所属机构", cellFormat1);
  664. sheet.addCell(label);
  665. label = new Label(3, 0, "主机名称", cellFormat1);
  666. sheet.addCell(label);
  667. label = new Label(4, 0, "主机IP", cellFormat1);
  668. sheet.addCell(label);
  669. label = new Label(5, 0, "当前版本号", cellFormat1);
  670. sheet.addCell(label);
  671. label = new Label(6, 0, "目标版本号", cellFormat1);
  672. sheet.addCell(label);
  673. label = new Label(7, 0, "操作描述", cellFormat1);
  674. sheet.addCell(label);
  675. label = new Label(8, 0, "操作时间", cellFormat1);
  676. sheet.addCell(label);
  677. label = new Label(9, 0, "操作结果", cellFormat1);
  678. sheet.addCell(label);
  679. return sheet;
  680. }
  681. /**
  682. * 日志-列表导出(往Excel中添加内容)
  683. */
  684. private void addLogContentInfoToLabel(Label label, WritableSheet sheet, List<OperationLog> list) throws Exception {
  685. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  686. WritableFont writableFont = new WritableFont(WritableFont.createFont("宋体"), 11, WritableFont.NO_BOLD, false);
  687. WritableCellFormat writableCellFormat = new WritableCellFormat(writableFont);
  688. writableCellFormat.setWrap(true);
  689. for (int i = 0; i < list.size(); i++) {
  690. OperationLog log = list.get(i);
  691. label = new Label(0, i + 1, i + 1 + "", writableCellFormat);
  692. sheet.addCell(label);
  693. label = new Label(1, i + 1, StringUtils.isNotEmpty(log.getUserName()) ? log.getUserName() : "-", writableCellFormat);
  694. sheet.addCell(label);
  695. label = new Label(2, i + 1, StringUtils.isNotEmpty(log.getOrganizeName()) ? log.getOrganizeName() : "-", writableCellFormat);
  696. sheet.addCell(label);
  697. label = new Label(3, i + 1, StringUtils.isNotEmpty(log.getHostName()) ? log.getHostName() : "-", writableCellFormat);
  698. sheet.addCell(label);
  699. label = new Label(4, i + 1, StringUtils.isNotEmpty(log.getHostIp()) ? log.getHostIp() : "-", writableCellFormat);
  700. sheet.addCell(label);
  701. label = new Label(5, i + 1, StringUtils.isNotEmpty(log.getTargetVersion()) ? log.getTargetVersion() : "-", writableCellFormat);
  702. sheet.addCell(label);
  703. label = new Label(6, i + 1, StringUtils.isNotEmpty(log.getMirrorVersion()) ? log.getMirrorVersion() : "-", writableCellFormat);
  704. sheet.addCell(label);
  705. label = new Label(7, i + 1, StringUtils.isNotEmpty(log.getRemark()) ? log.getRemark() : "-", writableCellFormat);
  706. sheet.addCell(label);
  707. label = new Label(8, i + 1, DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",log.getOperaTime()), writableCellFormat);
  708. sheet.addCell(label);
  709. label = new Label(9, i + 1, StringUtils.isNotEmpty(log.getOperaResult()) ? log.getOperaResult() : "-", writableCellFormat);
  710. sheet.addCell(label);
  711. }
  712. }
  713. }