| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756 |
- package com.xunmei.deploy.controller;
- import cn.hutool.core.date.DateUtil;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.baomidou.mybatisplus.core.metadata.IPage;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.github.zafarkhaja.semver.Version;
- import com.xunmei.common.core.domain.R;
- import com.xunmei.common.core.utils.DateUtils;
- import com.xunmei.common.core.utils.StringUtils;
- import com.xunmei.common.core.web.domain.AjaxResult;
- import com.xunmei.common.core.web.page.TableDataInfo;
- import com.xunmei.deploy.domain.*;
- import com.xunmei.deploy.service.*;
- import com.xunmei.deploy.util.CommonConstraint;
- import com.xunmei.deploy.util.CommonUtils;
- import com.xunmei.deploy.util.Message;
- import com.xunmei.deploy.vo.HostInfoVo;
- import jxl.Workbook;
- import jxl.WorkbookSettings;
- import jxl.write.*;
- import jxl.write.biff.RowsExceededException;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.web.bind.annotation.*;
- import org.springframework.web.multipart.MultipartFile;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.*;
- import java.text.SimpleDateFormat;
- import java.util.*;
- @RestController
- @RequestMapping("/deployData")
- public class DataController extends BaseController{
- private Logger log = LoggerFactory.getLogger(getClass());
- @Autowired
- private HostInfoService hostInfoService;
- @Autowired
- private AppRunInfoService appRunInfoService;
- @Autowired
- private FrontTaskService frontTaskService;
- @Autowired
- private HostZipInfoService hostZipInfoService;
- @Autowired
- private PackageInfoService packageInfoService;
- @Autowired
- private ZipPackInfoService zipPackInfoService;
- @Autowired
- private UploadAppInfoService uploadAppInfoService;
- @Autowired
- private UpgradeBatchInfoService upgradeBatchInfoService;
- @Autowired
- private UpgradeTaskService upgradeTaskService;
- @Autowired
- private OperationLogService logService;
- /**
- * 主机管理-列表
- * @param page
- * @param hostInfoVo
- * @return
- */
- @PostMapping("/hostInfoList")
- public TableDataInfo hostInfoList(Page<HostInfo> page, HostInfoVo hostInfoVo){
- IPage<HostInfoVo> result = hostInfoService.selectHostInfoPage(page, hostInfoVo);
- return getDataTable(result);
- }
- /**
- * 主机管理--详情页(应用列表)
- * @param hostId
- * @return
- */
- @PostMapping("/getRunApps")
- public List<AppRunInfo> getRunApps(String hostId){
- List<AppRunInfo> appRunningInfos = appRunInfoService.getByHostId(hostId);
- return appRunningInfos;
- }
- /**
- * 主机管理--批量升级
- */
- @PostMapping("/addHostBatch")
- public AjaxResult addHostBatch(String zipId,@RequestParam("hostIds[]") String[] hostIds){
- try {
- String msg = upgradeBatchInfoService.addHostUpgradeBatch(zipId, hostIds);
- return AjaxResult.success(msg);
- } catch (Exception e) {
- log.error("主机管理-批量升级出现异常:{}",e);
- return AjaxResult.error("升级批次创建失败,"+e.getMessage());
- }
- }
- /**
- * 主机管理--批量升级-先获取已上传的升级包下拉框
- * @return
- */
- @RequestMapping("/getZipInfos")
- public Message getZipInfos(){
- List<ZipPackInfo> list = zipPackInfoService.getList();
- Collections.sort(list, new Comparator<ZipPackInfo>() {
- @Override
- public int compare(ZipPackInfo o1, ZipPackInfo o2) {
- String o1ZipVersion = o1.getZipVersion();
- String o2ZipVersion = o2.getZipVersion();
- Version oldVersion,newVersion;
- try {
- oldVersion = Version.valueOf(o1ZipVersion);
- } catch (Exception e) {
- oldVersion = Version.valueOf("1.0.0");
- }
- try {
- newVersion = Version.valueOf(o2ZipVersion);
- } catch (Exception e) {
- newVersion = Version.valueOf("1.0.0");
- }
- if (oldVersion.compareTo(newVersion) > 0){
- return -1;
- } else if (oldVersion.compareTo(newVersion) < 0) {
- return 1;
- } else {
- return 0;
- }
- }
- });
- JSONArray array = new JSONArray();
- JSONObject json;
- for (ZipPackInfo zipPackInfo : list) {
- json = new JSONObject();
- json.put("id",zipPackInfo.getId());
- if (StringUtils.isEmpty(zipPackInfo.getHostTypeName())){
- json.put("text",zipPackInfo.getZipVersion());
- }else {
- json.put("text",zipPackInfo.getZipVersion() + "-" + zipPackInfo.getHostTypeName());
- }
- array.add(json);
- }
- return Message.success(null,array);
- }
- /**
- * 主机管理--批量升级-先根据页面勾选的主机筛选出可升级的主机hostIds,返回给页面操作升级
- */
- @PostMapping("/validateHostBatch")
- public AjaxResult validateHostBatch(String upgradeZipId,@RequestParam("hostIds[]") String[] hostIds){
- if(StringUtils.isEmpty(upgradeZipId)){
- return AjaxResult.error("请选择升级包!");
- }
- if(hostIds == null || hostIds.length == 0){
- return AjaxResult.error("请选择需要升级的主机!");
- }
- try {
- Map<String, Object> data = upgradeBatchInfoService.validateHostBatch(upgradeZipId, hostIds);
- return AjaxResult.success(data);
- } catch (Exception e) {
- log.error("主机管理--批量升级-根据页面勾选的主机筛选出可升级的主机出现异常:{}",e);
- return AjaxResult.error(e.getMessage());
- }
- }
- /**
- * 主机管理--重启服务(创建任务)
- * @param hostId
- **/
- @PostMapping("/createTask")
- public Message createTask(String hostId){
- try {
- frontTaskService.createRestartAppTask(hostId);
- return Message.success(null,null);
- } catch (Exception e) {
- log.error("主机管理-重启服务(创建任务)出现异常:{}",e);
- return Message.error(e.getMessage());
- }
- }
- /**
- * 主机管理--删除主机
- **/
- @PostMapping("/deleteByHostId")
- @Transactional(rollbackFor = Exception.class)
- public R deleteByHostId(String id,String userId,HttpServletRequest request){
- if(StringUtils.isEmpty(userId)){
- return R.fail("删除失败,操作用户为空或会话过期,请尝试重新登录!");
- }
- return hostInfoService.deleteByHostId(id);
- }
- /**
- * 主机管理--删除主机-先验证主机是否存在升级任务
- * @param hostId
- * @return
- */
- @RequestMapping("/getUpgradeTaskByHostId")
- @ResponseBody
- public Message getUpgradeTaskByHostId(String hostId){
- try {
- List<UpgradeTask> list = upgradeTaskService.getByHostId(hostId);
- return Message.success("获取获取任务成功!",list);
- } catch (Exception e) {
- log.error("主机管理--删除主机-查询主机当前的升级任务出现异常:{}",e);
- return Message.error("获取任务失败,"+e.getMessage());
- }
- }
- /**
- * 主机管理--当前版本号下拉框
- **/
- @RequestMapping("/getZipInfos3")
- public Message getZipInfos3(){
- List<String> list = hostZipInfoService.selectOldList();
- return Message.success(null,CommonUtils.sortVersion(list));
- }
- /**
- * 主机管理--目标版本号下拉框
- **/
- @RequestMapping("/getZipInfos4")
- public Message getZipInfos4(){
- List<String> list = hostZipInfoService.selectTargetList();
- return Message.success(null,CommonUtils.sortVersion(list));
- }
- /**
- * 主机管理--白令海版本号下拉框
- */
- @RequestMapping("/getAgentVersion")
- public Message getAgentVersion(){
- List<String> list = packageInfoService.selectAgentVersion();
- return Message.success(null,CommonUtils.sortVersion(list));
- }
- /**
- * 主机管理--导出
- */
- @RequestMapping(value = "/hostInfoListExport")
- public void hostInfoListExport(
- HostInfoVo hostInfoVo,
- HttpServletResponse response) throws Exception {
- Page<HostInfo> page = new Page<>();
- page.setSize(5000);
- page.setCurrent(1);
- IPage<HostInfoVo> result = hostInfoService.selectHostInfoPage(page, hostInfoVo);
- List<HostInfoVo> list = result.getRecords();
- Label label = null;
- WritableSheet sheet = null;
- WritableWorkbook workBook = null;
- WorkbookSettings settings = new WorkbookSettings();
- settings.setWriteAccess(null);
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- InputStream is = null;
- OutputStream out = null;
- try {
- workBook = Workbook.createWorkbook(os, settings);
- sheet = workBook.createSheet("sheet1", 0);
- sheet.setColumnView(0, 30);//设置Excel的宽度
- sheet.setColumnView(1, 30);
- sheet.setColumnView(2, 30);
- sheet.setColumnView(3, 30);
- sheet.setColumnView(4, 30);
- sheet.setColumnView(5, 30);
- sheet.setColumnView(6, 30);
- sheet.setColumnView(7, 30);
- sheet.setColumnView(8, 30);
- sheet.setColumnView(9, 30);
- sheet.setColumnView(10, 30);
- sheet.setColumnView(11, 30);
- this.addHostInfoToLabel(label, sheet);//向Excel添加标题
- this.addHostContentInfoToLabel(label, sheet, list);//向Excel添加表格数据
- workBook.write();
- } catch (Exception e) {
- log.error("主机管理excel导出处理数据出现异常:{}", e);
- } finally {
- workBook.close();
- }
- try {
- is = new ByteArrayInputStream(os.toByteArray());
- out = response.getOutputStream();
- // 设置输出文件信息
- response.setContentType("application/octet-stream;charset=UTF-8");
- response.addHeader("Content-Disposition", "attachment;filename=" + new String(("主机管理列表" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xls").getBytes("gb2312"), "ISO8859-1"));
- // 写文件流
- byte[] buffer = new byte[6 * 1024];
- int len = 0;
- while ((len = is.read(buffer, 0, buffer.length)) != -1) {
- out.write(buffer, 0, len);
- }
- } catch (IOException e) {
- log.error("主机管理excel导出处理流出现异常:{}", e);
- } finally {
- is.close();
- out.close();
- }
- }
- /**
- * 升级包管理-列表
- */
- @PostMapping("/zipList")
- public TableDataInfo zipList(Page<ZipPackInfo> page, ZipPackInfo info){
- IPage<ZipPackInfo> result = zipPackInfoService.selectPage(page,info);
- return getDataTable(result);
- }
- /**
- * 升级包管理-适用主机下拉框
- */
- @PostMapping("/zipHostType")
- public Message zipHostType(){
- List<String> list = zipPackInfoService.getZipHostType();
- JSONArray array = new JSONArray();
- JSONObject json;
- for(String name:list){
- json = new JSONObject();
- json.put("id",name);
- json.put("text",name);
- array.add(json);
- }
- return Message.success(null,array);
- }
- /**
- * 升级包管理-详情页(升级包内应用列表)
- */
- @RequestMapping("/uploadList")
- public List<UploadAppInfo> uploadList(String id){
- List<UploadAppInfo> list = uploadAppInfoService.getListByZipId(id);
- return list;
- }
- /**
- * 升级包管理-上传升级包
- */
- @PostMapping("/zipUpload")
- public String fileUpload(MultipartFile file, HttpServletResponse response){
- try {
- uploadAppInfoService.uploadZip(file);
- return "压缩包解压完成!";
- } catch (Exception e) {
- log.error("上传升级包出现异常:{}",e);
- response.setStatus(500);
- return "解析包错误:"+e.getMessage();
- }
- }
- /**
- * 升级包管理-删除
- */
- @RequestMapping("/delZip")
- public AjaxResult delZip(String ids){
- return zipPackInfoService.delZip(ids);
- }
- /**
- * 升级状态查询-列表
- */
- @PostMapping("/batchList")
- public TableDataInfo batchList(Page<UpgradeBatchInfo> page, UpgradeBatchInfo info){
- IPage<UpgradeBatchInfo> result = upgradeBatchInfoService.selectPage(page,info);
- return getDataTable(result);
- }
- /**
- * 升级状态查询-详情
- */
- @PostMapping("/batchHostDetail")
- public List<BatchHostInfo> batchHostDetail(String id,String orgName,Integer status){
- List<BatchHostInfo> batchHosts = upgradeBatchInfoService.getBatchHosts(id,orgName,status);
- return batchHosts;
- }
- /**
- * 升级状态查询-详情下的二级详情
- */
- @PostMapping("/batchTask")
- public List<UpgradeTask> batchTask(String hostId,String batchNumber){
- List<UpgradeTask> upgradeTasks = upgradeTaskService.batchTask(hostId, batchNumber);
- for (UpgradeTask upgradeTask : upgradeTasks) {
- String deployDescription = upgradeTask.getDeployDescription();
- if (StringUtils.isNotEmpty(deployDescription)) {
- try {
- String[] split = deployDescription.split(CommonConstraint.UpgradeDescriptionConstant.SPLIT);
- String desc1 = CommonConstraint.UpgradeDescriptionConstant.DESCRIPTION.get(split[0]);
- String desc2 = CommonConstraint.UpgradeDescriptionConstant.DESCRIPTION.get(split[1]);
- if (null == desc1 || "null".equals(desc1)) {
- desc1 = "";
- }
- if (null == desc2 || "null".equals(desc2)) {
- desc2 = "";
- }
- upgradeTask.setDeployDescription(desc1 + CommonConstraint.UpgradeDescriptionConstant.SPLIT + desc2);
- } catch (Exception e) {
- log.error("查看升级应用详情出现异常:{}",e);
- }
- }
- }
- return upgradeTasks;
- }
- /**
- * 升级状态查询-详情导出
- */
- @RequestMapping(value = "/batchHostDetailExport")
- public void batchHostDetailExport(
- String id,String orgName,Integer status,
- HttpServletResponse response) throws Exception {
- Page<HostInfo> page = new Page<>();
- page.setSize(5000);
- page.setCurrent(1);
- List<BatchHostInfo> list = upgradeBatchInfoService.getBatchHosts(id,orgName,status);
- Label label = null;
- WritableSheet sheet = null;
- WritableWorkbook workBook = null;
- WorkbookSettings settings = new WorkbookSettings();
- settings.setWriteAccess(null);
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- InputStream is = null;
- OutputStream out = null;
- try {
- workBook = Workbook.createWorkbook(os, settings);
- sheet = workBook.createSheet("sheet1", 0);
- sheet.setColumnView(0, 30);//设置Excel的宽度
- sheet.setColumnView(1, 30);
- sheet.setColumnView(2, 30);
- sheet.setColumnView(3, 30);
- sheet.setColumnView(4, 30);
- sheet.setColumnView(5, 30);
- sheet.setColumnView(6, 30);
- sheet.setColumnView(7, 30);
- sheet.setColumnView(8, 30);
- this.addBatchHostDetailToLabel(label, sheet);//向Excel添加标题
- this.addBatchHostContentToLabel(label, sheet, list);//向Excel添加表格数据
- workBook.write();
- } catch (Exception e) {
- log.error("excel导出异常:{}", e);
- } finally {
- workBook.close();
- }
- // 根据批次Id 获取批次code
- UpgradeBatchInfo byId = upgradeBatchInfoService.getById(id);
- String batchCode = byId.getBatchCode();
- try {
- is = new ByteArrayInputStream(os.toByteArray());
- out = response.getOutputStream();
- // 设置输出文件信息
- response.setContentType("application/octet-stream;charset=UTF-8");
- response.addHeader("Content-Disposition", "attachment;filename=" + new String(("升级主机详情" + batchCode + ".xls").getBytes("gb2312"), "ISO8859-1"));
- // 写文件流
- byte[] buffer = new byte[6 * 1024];
- int len = 0;
- while ((len = is.read(buffer, 0, buffer.length)) != -1) {
- out.write(buffer, 0, len);
- }
- } catch (IOException e) {
- log.error("excel导出异常:{}", e);
- } finally {
- is.close();
- out.close();
- }
- }
- /**
- * 主机管理-列表导出(给Excel导出增加标题)
- */
- private WritableSheet addHostInfoToLabel(Label label, WritableSheet sheet) throws RowsExceededException, WriteException {
- WritableFont font1 = new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD);
- WritableCellFormat cellFormat1 = new WritableCellFormat(font1);
- label = new Label(0, 0, "序号", cellFormat1);
- sheet.addCell(label);
- label = new Label(1, 0, "所属机构", cellFormat1);
- sheet.addCell(label);
- label = new Label(2, 0, "主机名称", cellFormat1);
- sheet.addCell(label);
- label = new Label(3, 0, "主机IP", cellFormat1);
- sheet.addCell(label);
- label = new Label(4, 0, "当前版本号", cellFormat1);
- sheet.addCell(label);
- label = new Label(5, 0, "目标版本号", cellFormat1);
- sheet.addCell(label);
- label = new Label(6, 0, "升级状态", cellFormat1);
- sheet.addCell(label);
- label = new Label(7, 0, "服务状态", cellFormat1);
- sheet.addCell(label);
- label = new Label(8, 0, "白令海状态", cellFormat1);
- sheet.addCell(label);
- label = new Label(9, 0, "传输加密", cellFormat1);
- sheet.addCell(label);
- label = new Label(10, 0, "白令海版本号", cellFormat1);
- sheet.addCell(label);
- label = new Label(11, 0, "注册时间", cellFormat1);
- sheet.addCell(label);
- return sheet;
- }
- /**
- * 升级状态查询-详情导出(给Excel导出增加标题)
- */
- private WritableSheet addBatchHostDetailToLabel(Label label, WritableSheet sheet) throws RowsExceededException, WriteException {
- //设置字体;
- WritableFont font1 = new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD);
- WritableCellFormat cellFormat1 = new WritableCellFormat(font1);
- label = new Label(0, 0, "序号", cellFormat1);
- sheet.addCell(label);
- label = new Label(1, 0, "批次号", cellFormat1);
- sheet.addCell(label);
- label = new Label(2, 0, "所属机构", cellFormat1);
- sheet.addCell(label);
- label = new Label(3, 0, "版本号", cellFormat1);
- sheet.addCell(label);
- label = new Label(4, 0, "状态", cellFormat1);
- sheet.addCell(label);
- label = new Label(5, 0, "主机名称", cellFormat1);
- sheet.addCell(label);
- label = new Label(6, 0, "主机IP", cellFormat1);
- sheet.addCell(label);
- label = new Label(7, 0, "创建时间", cellFormat1);
- sheet.addCell(label);
- label = new Label(8, 0, "结束时间", cellFormat1);
- sheet.addCell(label);
- return sheet;
- }
- /**
- * 主机管理-列表导出(往Excel中添加内容)
- */
- private void addHostContentInfoToLabel(Label label, WritableSheet sheet, List<HostInfoVo> list) throws Exception {
- WritableFont writableFont = new WritableFont(WritableFont.createFont("宋体"), 11, WritableFont.NO_BOLD, false);
- WritableCellFormat writableCellFormat = new WritableCellFormat(writableFont);
- writableCellFormat.setWrap(true);
- for (int i = 0; i < list.size(); i++) {
- HostInfoVo vdso = list.get(i);
- label = new Label(0, i + 1, i + 1 + "", writableCellFormat);
- sheet.addCell(label);
- label = new Label(1, i + 1, StringUtils.isNotEmpty(vdso.getHostOrgName()) ? vdso.getHostOrgName() : "-", writableCellFormat);
- sheet.addCell(label);
- label = new Label(2, i + 1, StringUtils.isNotEmpty(vdso.getHostName()) ? vdso.getHostName() : "-", writableCellFormat);
- sheet.addCell(label);
- label = new Label(3, i + 1, StringUtils.isNotEmpty(vdso.getHostIp()) ? vdso.getHostIp() : "-", writableCellFormat);
- sheet.addCell(label);
- label = new Label(4, i + 1, StringUtils.isNotEmpty(vdso.getZipVersion()) ? vdso.getZipVersion() : "-", writableCellFormat);
- sheet.addCell(label);
- label = new Label(5, i + 1, StringUtils.isNotEmpty(vdso.getTargetVersion()) ? vdso.getTargetVersion() : "-", writableCellFormat);
- sheet.addCell(label);
- String updateStatusStr = "-";
- if (vdso.getUpdateStatus() == null) {
- updateStatusStr = "-";
- } else if (vdso.getUpdateStatus() == 0) {
- updateStatusStr = "进行中";
- } else if (vdso.getUpdateStatus() == 1) {
- updateStatusStr = "成功";
- } else if (vdso.getUpdateStatus() == 2) {
- updateStatusStr = "失败";
- } else if (vdso.getUpdateStatus() == 3) {
- updateStatusStr = "重试中";
- }
- label = new Label(6, i + 1, updateStatusStr, writableCellFormat);
- sheet.addCell(label);
- label = new Label(7, i + 1, vdso.getServerStatus() == 1 ? "正常" : "异常", writableCellFormat);
- sheet.addCell(label);
- label = new Label(8, i + 1, vdso.getHostStatus() == 1 ? "在线" : "离线", writableCellFormat);
- sheet.addCell(label);
- label = new Label(9, i + 1, vdso.getEncryption() == 1 ? "是" : "否", writableCellFormat);
- sheet.addCell(label);
- label = new Label(10, i + 1, StringUtils.isNotEmpty(vdso.getAgentVersion()) ? vdso.getAgentVersion() : "-", writableCellFormat);
- sheet.addCell(label);
- label = new Label(11, i + 1, StringUtils.isNotEmpty(vdso.getRegisterDateTime()) ? vdso.getRegisterDateTime() : "-", writableCellFormat);
- sheet.addCell(label);
- }
- }
- /**
- * 升级状态查询-详情导出(往Excel中添加内容)
- */
- private void addBatchHostContentToLabel(Label label, WritableSheet sheet, List<BatchHostInfo> list) throws Exception {
- WritableFont writableFont = new WritableFont(WritableFont.createFont("宋体"), 11, WritableFont.NO_BOLD, false);
- WritableCellFormat writableCellFormat = new WritableCellFormat(writableFont);
- writableCellFormat.setWrap(true);
- for (int i = 0; i < list.size(); i++) {
- BatchHostInfo vdso = list.get(i);
- label = new Label(0, i + 1, i + 1 + "", writableCellFormat);
- sheet.addCell(label);
- label = new Label(1, i + 1, StringUtils.isNotEmpty(vdso.getBatchCode()) ? vdso.getBatchCode() : "-", writableCellFormat);
- sheet.addCell(label);
- label = new Label(2, i + 1, StringUtils.isNotEmpty(vdso.getOrgName()) ? vdso.getOrgName() : "-", writableCellFormat);
- sheet.addCell(label);
- label = new Label(3, i + 1, StringUtils.isNotEmpty(vdso.getZipVersion()) ? vdso.getZipVersion() : "-", writableCellFormat);
- sheet.addCell(label);
- String status = "-";
- if (vdso.getStatus() == 0) {
- status = "进行中";
- } else if (vdso.getStatus() == 1) {
- status = "成功";
- } else if (vdso.getStatus() == 2) {
- status = "失败";
- } else if (vdso.getStatus() == 3) {
- status = "重试中";
- }
- label = new Label(4, i + 1, status, writableCellFormat);
- sheet.addCell(label);
- label = new Label(5, i + 1, StringUtils.isNotEmpty(vdso.getHostName()) ? vdso.getHostName() : "-", writableCellFormat);
- sheet.addCell(label);
- label = new Label(6, i + 1, StringUtils.isNotEmpty(vdso.getHostIp()) ? vdso.getHostIp() : "-", writableCellFormat);
- sheet.addCell(label);
- label = new Label(7, i + 1, null != vdso.getCreateTime() ? DateUtil.format(vdso.getCreateTime(), DateUtils.YYYY_MM_DD_HH_MM_SS) : "-", writableCellFormat);
- sheet.addCell(label);
- label = new Label(8, i + 1, null != vdso.getFinishTime() ? DateUtil.format(vdso.getFinishTime(), DateUtils.YYYY_MM_DD_HH_MM_SS) : "-", writableCellFormat);
- sheet.addCell(label);
- }
- }
- /**
- * 日志-列表
- */
- @PostMapping("/logInfoList")
- public TableDataInfo logInfoList(Page<OperationLog> page, OperationLog aperationLog){
- IPage<OperationLog> result = logService.selectPage(page, aperationLog);
- return getDataTable(result);
- }
- /**
- * 日志-列表导出
- */
- @RequestMapping(value = "/logListExport")
- public void logListExport(OperationLog operationLog,HttpServletResponse response) throws Exception {
- Page<OperationLog> page = new Page<>();
- page.setSize(5000);
- page.setCurrent(1);
- IPage<OperationLog> result = logService.selectLogPage(page, operationLog);
- List<OperationLog> list = result.getRecords();
- Label label = null;
- WritableSheet sheet = null;
- WritableWorkbook workBook = null;
- WorkbookSettings settings = new WorkbookSettings();
- settings.setWriteAccess(null);
- ByteArrayOutputStream os = new ByteArrayOutputStream();
- InputStream is = null;
- OutputStream out = null;
- try {
- workBook = Workbook.createWorkbook(os, settings);
- sheet = workBook.createSheet("sheet1", 0);
- sheet.setColumnView(0, 30);//设置Excel的宽度
- sheet.setColumnView(1, 30);
- sheet.setColumnView(2, 30);
- sheet.setColumnView(3, 30);
- sheet.setColumnView(4, 30);
- sheet.setColumnView(5, 30);
- sheet.setColumnView(6, 30);
- sheet.setColumnView(7, 30);
- sheet.setColumnView(8, 30);
- sheet.setColumnView(9, 30);
- this.addLogToLabel(label, sheet);//向Excel添加标题
- this.addLogContentInfoToLabel(label, sheet, list);//向Excel添加表格数据
- workBook.write();
- } catch (Exception e) {
- log.error("excel导出异常", e);
- } finally {
- workBook.close();
- }
- try {
- is = new ByteArrayInputStream(os.toByteArray());
- out = response.getOutputStream();
- // 设置输出文件信息
- response.setContentType("application/octet-stream;charset=UTF-8");
- response.addHeader("Content-Disposition", "attachment;filename=" + new String(("日志管理列表" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".xls").getBytes("gb2312"), "ISO8859-1"));
- // 写文件流
- byte[] buffer = new byte[6 * 1024];
- int len = 0;
- while ((len = is.read(buffer, 0, buffer.length)) != -1) {
- out.write(buffer, 0, len);
- }
- } catch (IOException e) {
- log.error("logListExport日志模块excel导出异常:{}", e);
- } finally {
- is.close();
- out.close();
- }
- }
- /**
- * 日志-列表导出(给Excel导出增加标题)
- */
- private WritableSheet addLogToLabel(Label label, WritableSheet sheet) throws RowsExceededException, WriteException {
- //设置字体;
- WritableFont font1 = new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD);
- WritableCellFormat cellFormat1 = new WritableCellFormat(font1);
- label = new Label(0, 0, "序号", cellFormat1);
- sheet.addCell(label);
- label = new Label(1, 0, "操作人", cellFormat1);
- sheet.addCell(label);
- label = new Label(2, 0, "所属机构", cellFormat1);
- sheet.addCell(label);
- label = new Label(3, 0, "主机名称", cellFormat1);
- sheet.addCell(label);
- label = new Label(4, 0, "主机IP", cellFormat1);
- sheet.addCell(label);
- label = new Label(5, 0, "当前版本号", cellFormat1);
- sheet.addCell(label);
- label = new Label(6, 0, "目标版本号", cellFormat1);
- sheet.addCell(label);
- label = new Label(7, 0, "操作描述", cellFormat1);
- sheet.addCell(label);
- label = new Label(8, 0, "操作时间", cellFormat1);
- sheet.addCell(label);
- label = new Label(9, 0, "操作结果", cellFormat1);
- sheet.addCell(label);
- return sheet;
- }
- /**
- * 日志-列表导出(往Excel中添加内容)
- */
- private void addLogContentInfoToLabel(Label label, WritableSheet sheet, List<OperationLog> list) throws Exception {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- WritableFont writableFont = new WritableFont(WritableFont.createFont("宋体"), 11, WritableFont.NO_BOLD, false);
- WritableCellFormat writableCellFormat = new WritableCellFormat(writableFont);
- writableCellFormat.setWrap(true);
- for (int i = 0; i < list.size(); i++) {
- OperationLog log = list.get(i);
- label = new Label(0, i + 1, i + 1 + "", writableCellFormat);
- sheet.addCell(label);
- label = new Label(1, i + 1, StringUtils.isNotEmpty(log.getUserName()) ? log.getUserName() : "-", writableCellFormat);
- sheet.addCell(label);
- label = new Label(2, i + 1, StringUtils.isNotEmpty(log.getOrganizeName()) ? log.getOrganizeName() : "-", writableCellFormat);
- sheet.addCell(label);
- label = new Label(3, i + 1, StringUtils.isNotEmpty(log.getHostName()) ? log.getHostName() : "-", writableCellFormat);
- sheet.addCell(label);
- label = new Label(4, i + 1, StringUtils.isNotEmpty(log.getHostIp()) ? log.getHostIp() : "-", writableCellFormat);
- sheet.addCell(label);
- label = new Label(5, i + 1, StringUtils.isNotEmpty(log.getTargetVersion()) ? log.getTargetVersion() : "-", writableCellFormat);
- sheet.addCell(label);
- label = new Label(6, i + 1, StringUtils.isNotEmpty(log.getMirrorVersion()) ? log.getMirrorVersion() : "-", writableCellFormat);
- sheet.addCell(label);
- label = new Label(7, i + 1, StringUtils.isNotEmpty(log.getRemark()) ? log.getRemark() : "-", writableCellFormat);
- sheet.addCell(label);
- label = new Label(8, i + 1, DateUtils.parseDateToStr("yyyy-MM-dd HH:mm:ss",log.getOperaTime()), writableCellFormat);
- sheet.addCell(label);
- label = new Label(9, i + 1, StringUtils.isNotEmpty(log.getOperaResult()) ? log.getOperaResult() : "-", writableCellFormat);
- sheet.addCell(label);
- }
- }
- }
|