package com.changhong.epc.form.rest.Assets; import com.changhong.epc.bean.admin.AssetDepreciation; import com.changhong.epc.bean.form.Accrued; import com.changhong.epc.bean.form.Asset; import com.changhong.epc.bean.form.QueryParam; import com.changhong.epc.bean.form.SectoralAssets; import com.changhong.epc.constter.form.utf.FormUrlConst; import com.changhong.epc.form.service.assets.AssetService; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.OutputStream; import java.net.URLEncoder; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; @RestController public class AssetsRest implements FormUrlConst { @Resource(name="assetService") private AssetService assetService; /** * 页面初始化查询资产 * @throws Exception */ @RequestMapping(value = SELECT_ASSETS, method={RequestMethod.OPTIONS, RequestMethod.POST}) public List selectAssets(@RequestBody Map data){ return assetService.getSectoralAsset(data.get("date")); } /** * 同过一级部门进行查找异常数据 * */ @RequestMapping(value = SELECT_ERRASSETS, method={RequestMethod.OPTIONS, RequestMethod.POST}) public List selectErrAssets(@RequestBody QueryParam queryParam) throws Exception{ return assetService.getErrAsset(queryParam); } /** *通过子部门查询异常资产 * */ @RequestMapping(value = SELECT_ERRASSETSBYONE, method={RequestMethod.OPTIONS, RequestMethod.POST}) public List selectErrAsset(@RequestBody QueryParam queryParam) throws Exception{ return assetService.getErrAssetOne(queryParam); } /** * 分页 * 通过条件查询 * 查询详细 * 2018-1-18 */ @RequestMapping(value = SELECT_ACCRUED, method={RequestMethod.OPTIONS, RequestMethod.POST}) public List selectAccrued(@RequestBody QueryParam queryParam){ try { return assetService.getAccrueds(queryParam); } catch (ParseException e) { return null; } } /** * 保存前验证资产 * @param assetDepreciation * @return */ @PostMapping(VALIDATE_ASSET) public Boolean validateAsset(@RequestBody AssetDepreciation assetDepreciation){ return assetService.validateAsset(assetDepreciation); } //下载exl @GetMapping(SELECT_DOEXL) public boolean doExl(String date,String useDepCode,String useDepName,String state,String assetName,String assetCode,HttpServletRequest request, HttpServletResponse response){ QueryParam qp = new QueryParam(); if(date != null && (!date.equals("null")) && (!date.equals(""))){ qp.setDate(date); } if(state != null && (!state.equals("null")) && (!state.equals(""))){ qp.setState(state); } if(assetCode != null && (!assetCode.equals("null")) && (!assetCode.equals(""))){ qp.setAssetCode(assetCode); } if(assetName != null && (!assetName.equals("null")) && (!assetName.equals(""))){ qp.setAssetName(assetName); } if(useDepCode != null && (!useDepCode.equals("null")) && (!useDepCode.equals(""))){ qp.setUseDepCode(useDepCode); } if(useDepName != null && (!useDepName.equals("null")) && (!useDepName.equals(""))){ qp.setUseDepName(useDepName); } List acc = new ArrayList(); try { acc=assetService.getAccrueds(qp); } catch (ParseException e1) { } // 第一步,创建一个webbook,对应一个Excel文件 HSSFWorkbook workbook = new HSSFWorkbook(); // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet 页 HSSFSheet sheet = workbook.createSheet(new SimpleDateFormat("yyyy-MM-dd").format(new Date())); // 第三步,在sheet中添加表头第0行 HSSFRow row = sheet.createRow(0); // 第四步,创建单元格,并设置值表头 设置表头居中 HSSFCellStyle style = workbook.createCellStyle(); // 创建一个居中格 style.setAlignment(HSSFCellStyle.ALIGN_CENTER); row.createCell(0).setCellValue("序号"); row.createCell(1).setCellValue("部门"); row.createCell(2).setCellValue("资产编号"); row.createCell(3).setCellValue("资产名称"); row.createCell(4).setCellValue("规格型号"); row.createCell(5).setCellValue("资产化日期"); row.createCell(6).setCellValue("预计使用年限(月)"); row.createCell(7).setCellValue("已使用年限(月)"); row.createCell(8).setCellValue("原值"); row.createCell(9).setCellValue("折旧方法"); row.createCell(10).setCellValue("累计折旧"); row.createCell(11).setCellValue("本月折旧"); row.createCell(12).setCellValue("月折旧率(%)"); row.createCell(13).setCellValue("折后净值"); for(int i = 0;i < 14;i++){ //设置表头居中 row.getCell(i).setCellStyle(style); //设置列宽 sheet.setColumnWidth(i, 3500); } sheet.setColumnWidth(0, 1500); // 第五步,写入实体数据 for (int i = 0; i < acc.size(); i++) { row = sheet.createRow(i + 1); // 第六步,创建单元格,并设置值 if(acc.get(i).getDepreciationMethod().equals("1")){ acc.get(i).setDepreciationMethod("双倍余额递减法"); }else if(acc.get(i).getDepreciationMethod().equals("2")){ acc.get(i).setDepreciationMethod("平均年限法"); }else if(acc.get(i).getDepreciationMethod().equals("3")){ acc.get(i).setDepreciationMethod("年限总和法"); } row.createCell(0).setCellValue(i+1); row.createCell(1).setCellValue(acc.get(i).getUseDepartmentName() == null ? "" : acc.get(i).getUseDepartmentName()); row.createCell(2).setCellValue(acc.get(i).getAssetsCode() == null ? "" : acc.get(i).getAssetsCode()); row.createCell(3).setCellValue(acc.get(i).getAssetsName() == null ? "" : acc.get(i).getAssetsName()); row.createCell(4).setCellValue(acc.get(i).getSpecifications() == null ? "" : acc.get(i).getSpecifications()); row.createCell(5).setCellValue(acc.get(i).getCapitalizationData() == null ? "" : acc.get(i).getCapitalizationData()); row.createCell(6).setCellValue(acc.get(i).getEstimateTime() == null ? "" : acc.get(i).getEstimateTime()); row.createCell(7).setCellValue(acc.get(i).getAlreadyTime() == null ? "" : acc.get(i).getAlreadyTime()); row.createCell(8).setCellValue(acc.get(i).getOriginalMoney() == null ? "" : acc.get(i).getOriginalMoney()); row.createCell(9).setCellValue(acc.get(i).getDepreciationMethod() == null ? "" : acc.get(i).getDepreciationMethod()); row.createCell(10).setCellValue(acc.get(i).getCumulativeDepreciation() == null ? 0 : acc.get(i).getCumulativeDepreciation()); row.createCell(11).setCellValue(acc.get(i).getCurrentDepreciation() == null ? 0 : acc.get(i).getCurrentDepreciation()); row.createCell(12).setCellValue(acc.get(i).getDepreciationRate() == null ? 0 : acc.get(i).getDepreciationRate()); row.createCell(13).setCellValue(acc.get(i).getNetMoney() == null ? 0 : acc.get(i).getNetMoney()); } String downFileName = new String("资产折旧信息"+new SimpleDateFormat("yyyy-MM").format(new Date())+".xls"); try { //若不进行编码在IE下会乱码 downFileName = URLEncoder.encode(downFileName, "UTF-8"); } catch (Exception e) { } try (OutputStream os = response.getOutputStream()){ // 清空response response.reset(); response.setContentType("application/msexcel");//设置生成的文件类型 response.setCharacterEncoding("UTF-8");//设置文件头编码方式和文件名 response.setHeader("Content-Disposition", "attachment; filename=" + new String(downFileName.getBytes("utf-8"), "ISO8859-1")); workbook.write(os); os.flush(); } catch (IOException e) { // System.err.println(e.getMessage()); return false; } return true; } @PostMapping(ASSET_UPDATESTATUS) public int updateAssetsByState(@RequestBody Map map){ return assetService.updateAssetsByState(map); } }