package com.xt.jygl.gkjcxx.ax.ctl; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaBuilder.In; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import cache.CacheSearchCondition; import com.fasterxml.jackson.core.JsonProcessingException; import com.xt.jygl.common.BaseWebCtl; import com.xt.jygl.common.Constants; import com.xt.jygl.common.GlobalData; import com.xt.jygl.common.SearchCondition; import com.xt.jygl.common.Utils; import com.xt.jygl.gkjcxx.ax.client.AxWebService; import com.xt.jygl.gkjcxx.ax.entity.AxEntity; import com.xt.jygl.gkjcxx.ax.service.AxService; import com.xt.jygl.gkjcxx.ax.vo.AxVo; import com.xt.jygl.gkjcxx.gk.entity.GkEntity; import com.xt.jygl.gkjcxx.gk.service.GkService; import com.xt.jygl.gkjcxx.gq.entity.GqEntity; import com.xt.jygl.gkjcxx.gq.service.GqService; import com.xt.jygl.gkjygl.gkjyr.jyrjbxx.entity.GkjyrEntity; import com.xt.jygl.gkjygl.gkjyr.jyrjbxx.service.GkjyrService; import com.xtframe.core.anon.auth.RequiresPermissions; import com.xtframe.core.base.ctl.WebJsonResult; import com.xtframe.core.support.SecurityMgr; import com.xtframe.sec.code.entity.CodeEntity; import com.xtframe.sec.common.BaseEntity; import com.xtframe.sec.common.QueryService; import com.xtframe.sec.common.SimplePageRequest; import com.xtframe.sec.utils.SecUtils; import com.xtframe.util.StringUtils; @Controller @RequestMapping("/ax") public class AxCtl extends BaseWebCtl { @Autowired private SecurityMgr securityMgr; @Autowired private QueryService query; @Autowired private AxService axService; @Autowired private GkService gkService; @Autowired private GqService gqService; @Autowired private GkjyrService gkjyrService; /** * 一览页面 * @param page 分页 * @param model * @param request * @param axmc 岸线名称 * @param szdid 所在地ID * @param szgkid 所在港口ID * @param szgqid 所在港区ID * @param ssjyr 经营人 * @param menuid * @param lyztid * @return * @throws JsonProcessingException */ @RequestMapping(value = "/main") @RequiresPermissions("ax:main") public String main(SimplePageRequest page, Model model, final HttpServletRequest request, final String axmc, final String szdid, final String szgkid, final String szgqid, final String ssjyr, String menuid,final String lyztid) throws JsonProcessingException { final Map cacheSCmap = CacheSearchCondition.cacheSCmap(request,"ax"); Specification spec = new Specification() { // 查询条件 @Override public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) { List list = new ArrayList(); if (StringUtils.isNotEmpty(axmc)) { list.add(cb.like(root.get("axmc").as(String.class), "%" + axmc + "%")); } else if (isGET(request)) { CacheSearchCondition.predicateList(list, cacheSCmap, root, cb, null,"axmc"); } if (StringUtils.isNotEmpty(szdid)) { list.add(cb.like(root.get("szd").get("id").as(String.class), "%" + szdid + "%")); } else if (isGET(request)) { CacheSearchCondition.predicateList(list, cacheSCmap, root, cb, "id","szd","szdid"); } if (StringUtils.isNotEmpty(szgkid)) { list.add(cb.like(root.get("szgk").get("id").as(String.class), "%" + szgkid + "%")); } else if (isGET(request)) { CacheSearchCondition.predicateList(list, cacheSCmap, root, cb, "id","szgk","szgkid"); } if (StringUtils.isNotEmpty(szgqid)) { list.add(cb.like(root.get("szgq").get("id").as(String.class), "%" + szgqid + "%")); } else if (isGET(request)) { CacheSearchCondition.predicateList(list, cacheSCmap, root, cb, "id","szgq","szgqid"); } if (StringUtils.isNotEmpty(ssjyr)) { list.add(cb.like(root.get("ssjyr").get("gkjyr").as(String.class), "%" + ssjyr + "%")); } else if (isGET(request)) { CacheSearchCondition.predicateList(list, cacheSCmap, root, cb, "gkjyr","ssjyr"); } if (StringUtils.isNotEmpty(lyztid)) { list.add(cb.like(root.get("lyzt").get("id").as(String.class), "%" + lyztid + "%")); } else if (isGET(request)) { CacheSearchCondition.predicateList(list, cacheSCmap, root, cb, "id","lyzt","lyztid"); } // 经营人只能查看自己企业下的数据,非经营人只能查看自己所在地的数据 if (isJyr()) { list.add(cb.like(root.get("ssjyr").get("id").as(String.class), "%" + getSsjyr().getId() + "%")); } else { In in = cb.in(root.get("szgq").get("id").as(String.class)); @SuppressWarnings("unchecked") List gqlist = (List) Utils.getSession() .getAttribute("gqList"); if (gqlist != null && gqlist.size() > 0) { for (int i = 0; i < gqlist.size(); i++) { in.value(gqlist.get(i)); } list.add(in); } } list.add(cb.notEqual(root.get("recordStatus").as(Integer.class), BaseEntity.RECORD_STATE_DELETE)); list.add(cb.notEqual(root.get("recordStatus").as(Integer.class), BaseEntity.RECORE_STATE_COPY)); return cb.and(list.toArray(new Predicate[] {})); } }; if(page.getPage() <1){ page.setPage(SearchCondition.axpage); } page.setSort("id"); page.setOrder("desc"); Page list = query.findAll(spec, toPageRequest(page), AxEntity.class); List voList = new ArrayList(); for (AxEntity entity : list) { AxVo vo = new AxVo(); if (null != entity) { BeanUtils.copyProperties(entity, vo); vo.setSzdzw(entity.getSzd() != null ? entity.getSzd().getText() : ""); vo.setSzgqzw(entity.getSzgq() != null ? entity.getSzgq().getGqmc() : ""); vo.setSzgkzw(entity.getSzgk() != null ? entity.getSzgk().getGkmc() : ""); vo.setSsjyrzw(entity.getSsjyr() != null ? entity.getSsjyr().getGkjyr() : ""); vo.setAxlxzw(entity.getAxlx() != null ? entity.getAxlx().getText() : ""); vo.setAxgnzw(entity.getAxgn() != null ? entity.getAxgn().getText() : ""); vo.setLyztzw(entity.getLyzt() != null ? entity.getLyzt().getText() : ""); vo.setNbazw(entity.getNba() != null?entity.getNba().getText() : ""); vo.setSzd(entity.getSzd() != null ? entity.getSzd().getId() : ""); vo.setSzgq(entity.getSzgq() != null ? entity.getSzgq().getId() : ""); vo.setSzgk(entity.getSzgk() != null ? entity.getSzgk().getId() : ""); vo.setSsjyr(entity.getSsjyr() != null ? entity.getSsjyr().getId() : ""); vo.setAxlx(entity.getAxlx() != null ? entity.getAxlx().getId() : ""); vo.setAxgn(entity.getAxgn() != null ? entity.getAxgn().getId() : ""); vo.setLyzt(entity.getLyzt() != null ? entity.getLyzt().getId() : ""); vo.setNba(entity.getNba() != null?entity.getNba().getId() : ""); voList.add(vo); } } if (isPOST(request)) { SearchCondition.axpage=page.getPage(); //cacheSCmap.put("axpage", page.getPage()); CacheSearchCondition.putCacheSCmap(request.getParameterMap(), cacheSCmap); } model.addAttribute("list", voList); model.addAttribute("axmc", CacheSearchCondition.cacheSCmapVal(cacheSCmap, "axmc")); model.addAttribute("szd", CacheSearchCondition.cacheSCmapVal(cacheSCmap, "szdid")); model.addAttribute("szgk", CacheSearchCondition.cacheSCmapVal(cacheSCmap, "szgkid")); model.addAttribute("szgq", CacheSearchCondition.cacheSCmapVal(cacheSCmap, "szgqid")); model.addAttribute("ssjyr", CacheSearchCondition.cacheSCmapVal(cacheSCmap, "ssjyr")); model.addAttribute("lyzt",CacheSearchCondition.cacheSCmapVal(cacheSCmap, "lyztid")); List lyztlist = securityMgr.codeService().findValidCodesByGroupCode("AXLYZT"); model.addAttribute("lyztlist", JSONArray.fromObject(lyztlist, config).toString()); model.addAttribute("menuid", menuid); // 翻页共通 putPageInfo(model, page, list); return "gkjcxx/ax/axmain"; } //删除 @RequestMapping(value = "/delete") @RequiresPermissions("ax:delete") @ResponseBody public WebJsonResult logicdelete(Model model, String id) { WebJsonResult wr = success(); try { // 逻辑删除 axService.logicDelete(id); // 如果是市级删除,省级也删除 AxEntity ax = axService.findOne(id); try { if (GlobalData.DEPLOY_MODE.equals(Constants.DEPLOY_MODE_CITY) && needWS()) { AxWebService webService = axService.getWebService(null); webService.delete(id); ax.setTbzt(Constants.NO); axService.save(ax); } } catch (Exception e) { ax.setTbzt(Constants.NO); axService.save(ax); } } catch (Exception e) { wr.setSuccess(false); wr.setMessage("操作失败!"); e.printStackTrace(); } return wr; } //保存 @RequestMapping(value = "/save") @ResponseBody @RequiresPermissions("ax:add") public WebJsonResult save(Model model, AxEntity code, String szdid, String szgkid, String szgqid, String ssjyrid, String axlxid, String axgnid, String lyztid,String nbaid) { // how:如果是新增直接保存,如果更新,先取出数据库中记录,然后更新页面传过来的字段 // why:这样的话一些在前台无需显示的字段保证不会丢失,同时一个个字段设置有助于bug排查 WebJsonResult wr = success(); // 必填字段的后台验证 if (StringUtils.isEmpty(szdid)) { wr.setSuccess(false); wr.setMessage("请选择所在地"); return wr; } if (StringUtils.isEmpty(szgkid)) { wr.setSuccess(false); wr.setMessage("请选择所在港口"); return wr; } if (StringUtils.isEmpty(szgqid)) { wr.setSuccess(false); wr.setMessage("请选择所在港区"); return wr; } if (StringUtils.isEmpty(ssjyrid)) { wr.setSuccess(false); wr.setMessage("请选择所属经营人"); return wr; } AxEntity axEntity = new AxEntity(); try { if (!StringUtils.isEmpty(code.getId())) { // 修改 axEntity = axService.findOne(code.getId()); } axEntity.setAxcd(code.getAxcd()); axEntity.setAxmc(code.getAxmc()); GkEntity szgk = new GkEntity(); szgk.setId(szgkid); axEntity.setSzgk(szgk); GqEntity szgq = new GqEntity(); szgq.setId(szgqid); axEntity.setSzgq(szgq); GkjyrEntity ssjyr = new GkjyrEntity(); ssjyr.setId(ssjyrid); axEntity.setSsjyr(ssjyr); // 非必填字段的验证,如为空则不验证,不为空则验证 if (!StringUtils.isEmpty(axlxid)) { CodeEntity axlx = new CodeEntity(axlxid); axEntity.setAxlx(axlx); } if (!StringUtils.isEmpty(axgnid)) { CodeEntity axgn = new CodeEntity(axgnid); axEntity.setAxgn(axgn); } if (!StringUtils.isEmpty(lyztid)) { CodeEntity lyzt = new CodeEntity(lyztid); axEntity.setLyzt(lyzt); } if (!StringUtils.isEmpty(nbaid)) { CodeEntity nba = new CodeEntity(nbaid); axEntity.setNba(nba); } axEntity.setZyid(code.getZyid()); axEntity.setJzrq(code.getJzrq()); axEntity.setAxcd(code.getAxcd()); axEntity.setQdzb(code.getQdzb()); axEntity.setZdzb(code.getZdzb()); axEntity.setAxpb(code.getAxpb()); axEntity.setBz(code.getBz()); axEntity.setWaterarea(code.getWaterarea()); axEntity.setLandarea(code.getLandarea()); axEntity.setLinelength(code.getLinelength()); axEntity.setQdzby(code.getQdzby()); axEntity.setZdzby(code.getZdzby()); axEntity.setAxwzms(code.getAxwzms()); GqEntity gq = gqService.findOne(szgqid); axEntity.setSsgkglbm(gq != null ? gq.getSsgkglbm() : null); CodeEntity szd = new CodeEntity(szdid); axEntity.setSzd(szd); axService.save(axEntity); } catch (Exception e) { wr.setSuccess(false); wr.setMessage("操作失败!"); e.printStackTrace(); } return wr; } //编辑 @RequestMapping(value = "/edit") @RequiresPermissions("ax:edit") public String edit(Model model, String id) { List axlxlist = securityMgr.codeService().findValidCodesByGroupCode("AXLX"); model.addAttribute("axlxlist", JSONArray.fromObject(axlxlist, config).toString()); List axgnlist = securityMgr.codeService().findValidCodesByGroupCode("AXGN"); model.addAttribute("axgnlist", JSONArray.fromObject(axgnlist, config).toString()); List lyztlist = securityMgr.codeService().findValidCodesByGroupCode("AXLYZT"); model.addAttribute("lyztlist", JSONArray.fromObject(lyztlist, config).toString()); List nbalist = securityMgr.codeService().findValidCodesByGroupCode("NBA"); model.addAttribute("nbalist",JSONArray.fromObject(nbalist,config).toString()); AxEntity entity = axService.findOne(id); AxVo vo = new AxVo(); BeanUtils.copyProperties(entity, vo); vo.setSzdzw(entity.getSzd() != null ? entity.getSzd().getText() : ""); vo.setSzgqzw(entity.getSzgq() != null ? entity.getSzgq().getGqmc() : ""); vo.setSzgkzw(entity.getSzgk() != null ? entity.getSzgk().getGkmc() : ""); vo.setSsjyrzw(entity.getSsjyr() != null ? entity.getSsjyr().getGkjyr() : ""); vo.setAxlxzw(entity.getAxlx() != null ? entity.getAxlx().getText() : ""); vo.setAxgnzw(entity.getAxgn() != null ? entity.getAxgn().getText() : ""); vo.setLyztzw(entity.getLyzt() != null ? entity.getLyzt().getText() : ""); vo.setNbazw(entity.getNba() != null?entity.getNba().getText() : ""); vo.setSzd(entity.getSzd() != null ? entity.getSzd().getId() : ""); vo.setSzgq(entity.getSzgq() != null ? entity.getSzgq().getId() : ""); vo.setSzgk(entity.getSzgk() != null ? entity.getSzgk().getId() : ""); vo.setSsjyr(entity.getSsjyr() != null ? entity.getSsjyr().getId() : ""); vo.setAxlx(entity.getAxlx() != null ? entity.getAxlx().getId() : ""); vo.setAxgn(entity.getAxgn() != null ? entity.getAxgn().getId() : ""); vo.setLyzt(entity.getLyzt() != null ? entity.getLyzt().getId() : ""); vo.setNba(entity.getNba() != null?entity.getNba().getId() : ""); if(StringUtils.isEmpty(vo.getZyid())){ SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyyMMddHHmmss"); String zyid = dateTimeFormat.format(new Date()); vo.setZyid(zyid); } model.addAttribute("record", vo); model.addAttribute("jyr", getSsjyr() != null ? JSONObject.fromObject(getSsjyr()).toString() : null); return "gkjcxx/ax/axedit"; } //查看 @RequestMapping(value = "/view") @RequiresPermissions("ax:main") public String view(Model model, String id) { AxVo vo = new AxVo(); AxEntity entity = axService.findOne(id); if (entity != null) { BeanUtils.copyProperties(entity, vo); vo.setSzdzw(entity.getSzd() != null ? entity.getSzd().getText() : ""); vo.setSzgqzw(entity.getSzgq() != null ? entity.getSzgq().getGqmc() : ""); vo.setSzgkzw(entity.getSzgk() != null ? entity.getSzgk().getGkmc() : ""); vo.setSsjyrzw(entity.getSsjyr() != null ? entity.getSsjyr().getGkjyr() : ""); vo.setAxlxzw(entity.getAxlx() != null ? entity.getAxlx().getText() : ""); vo.setAxgnzw(entity.getAxgn() != null ? entity.getAxgn().getText() : ""); vo.setLyztzw(entity.getLyzt() != null ? entity.getLyzt().getText() : ""); vo.setNbazw(entity.getNba() != null?entity.getNba().getText() : ""); vo.setSzd(entity.getSzd() != null ? entity.getSzd().getId() : ""); vo.setSzgq(entity.getSzgq() != null ? entity.getSzgq().getId() : ""); vo.setSzgk(entity.getSzgk() != null ? entity.getSzgk().getId() : ""); vo.setSsjyr(entity.getSsjyr() != null ? entity.getSsjyr().getId() : ""); vo.setAxlx(entity.getAxlx() != null ? entity.getAxlx().getId() : ""); vo.setAxgn(entity.getAxgn() != null ? entity.getAxgn().getId() : ""); vo.setLyzt(entity.getLyzt() != null ? entity.getLyzt().getId() : ""); vo.setNba(entity.getNba() != null?entity.getNba().getId() : ""); if (null != entity.getJzrq()) { vo.setJzrqfmt(new SimpleDateFormat("yyyy-MM-dd").format(entity.getJzrq())); } } model.addAttribute("record", vo); return "gkjcxx/ax/axview"; } //新增 @RequestMapping(value = "/add") @RequiresPermissions("ax:add") public String add(Model model) { List axlxlist = securityMgr.codeService().findValidCodesByGroupCode("AXLX"); model.addAttribute("axlxlist", JSONArray.fromObject(axlxlist, config).toString()); List axgnlist = securityMgr.codeService().findValidCodesByGroupCode("AXGN"); model.addAttribute("axgnlist", JSONArray.fromObject(axgnlist, config).toString()); List lyztlist = securityMgr.codeService().findValidCodesByGroupCode("AXLYZT"); model.addAttribute("lyztlist", JSONArray.fromObject(lyztlist, config).toString()); List nbalist = securityMgr.codeService().findValidCodesByGroupCode("NBA"); model.addAttribute("nbalist",JSONArray.fromObject(nbalist, config).toString()); model.addAttribute("jyr", getSsjyr() != null ? JSONObject.fromObject(getSsjyr()).toString() : null); if(SecUtils.getCurrUser() != null){ List list = securityMgr.codeService().findValidCodesByGroupCodeAndCity(Constants.GROUP_CODE_SZD, GlobalData.CITY_CODE); model.addAttribute("sz",list.size()==1?list.get(0).getId():""); if(list.size()==1){ List gklist = gkService.findBySzd(list.get(0).getId()); model.addAttribute("gk",gklist.size()==1?gklist.get(0).getId():""); } } SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyyMMddHHmmss"); String zyid = dateTimeFormat.format(new Date()); model.addAttribute("zyid", zyid); return "gkjcxx/ax/axadd"; } /** * 导出全部数据到excel * * @param response * @return */ @RequestMapping(value = "/exportExcelAll") public String exportExcelAll(HttpServletResponse response, final String axmc, final String szdid, final String szgkid, final String szgqid, final String ssjyr, final String lyztid) { Specification spec = new Specification() { // 查询条件 @Override public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) { List list = new ArrayList(); if (StringUtils.isNotEmpty(axmc)) { list.add(cb.like(root.get("axmc").as(String.class), "%" + axmc + "%")); } if (StringUtils.isNotEmpty(szdid)) { list.add(cb.like(root.get("szd").get("id").as(String.class), "%" + szdid + "%")); } if (StringUtils.isNotEmpty(szgkid)) { list.add(cb.like(root.get("szgk").get("id").as(String.class), "%" + szgkid + "%")); } if (StringUtils.isNotEmpty(szgqid)) { list.add(cb.like(root.get("szgq").get("id").as(String.class), "%" + szgqid + "%")); } if (StringUtils.isNotEmpty(ssjyr)) { list.add(cb.like(root.get("ssjyr").get("gkjyr").as(String.class), "%" + ssjyr + "%")); } if (StringUtils.isNotEmpty(lyztid)) { list.add(cb.like(root.get("lyzt").get("id").as(String.class), "%" + lyztid + "%")); } // 经营人只能查看自己企业下的数据,非经营人只能查看自己所在地的数据 if (isJyr()) { list.add(cb.like(root.get("ssjyr").get("id").as(String.class), "%" + getSsjyr().getId() + "%")); } else { In in = cb.in(root.get("szgq").get("id").as(String.class)); @SuppressWarnings("unchecked") List gqlist = (List) Utils.getSession() .getAttribute("gqList"); if (gqlist != null && gqlist.size() > 0) { for (int i = 0; i < gqlist.size(); i++) { in.value(gqlist.get(i)); } list.add(in); } } list.add(cb.notEqual(root.get("recordStatus").as(Integer.class), BaseEntity.RECORD_STATE_DELETE)); list.add(cb.notEqual(root.get("recordStatus").as(Integer.class), BaseEntity.RECORE_STATE_COPY)); return cb.and(list.toArray(new Predicate[] {})); } }; List list = query.findAll(spec, AxEntity.class); List voList = new ArrayList(); for (AxEntity entity : list) { AxVo vo = new AxVo(); if (null != entity) { BeanUtils.copyProperties(entity, vo); vo.setSzdzw(entity.getSzd() != null ? entity.getSzd().getText() : ""); vo.setSzgqzw(entity.getSzgq() != null ? entity.getSzgq().getGqmc() : ""); vo.setSzgkzw(entity.getSzgk() != null ? entity.getSzgk().getGkmc() : ""); vo.setSsjyrzw(entity.getSsjyr() != null ? entity.getSsjyr().getGkjyr() : ""); vo.setAxlxzw(entity.getAxlx() != null ? entity.getAxlx().getText() : ""); vo.setAxgnzw(entity.getAxgn() != null ? entity.getAxgn().getText() : ""); vo.setLyztzw(entity.getLyzt() != null ? entity.getLyzt().getText() : ""); vo.setNbazw(entity.getNba() != null?entity.getNba().getText() : ""); vo.setSzd(entity.getSzd() != null ? entity.getSzd().getId() : ""); vo.setSzgq(entity.getSzgq() != null ? entity.getSzgq().getId() : ""); vo.setSzgk(entity.getSzgk() != null ? entity.getSzgk().getId() : ""); vo.setSsjyr(entity.getSsjyr() != null ? entity.getSsjyr().getId() : ""); vo.setAxlx(entity.getAxlx() != null ? entity.getAxlx().getId() : ""); vo.setAxgn(entity.getAxgn() != null ? entity.getAxgn().getId() : ""); vo.setLyzt(entity.getLyzt() != null ? entity.getLyzt().getId() : ""); vo.setNba(entity.getNba() != null?entity.getNba().getId() : ""); voList.add(vo); } } response.setContentType("application/binary;charset=iso-8859-1"); try { ServletOutputStream outputStream = response.getOutputStream(); String fileName = new String(("岸线信息全部").getBytes(), "iso-8859-1"); response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xlsx");// 组装附件名称和格式 String[] titles = { "序号", "岸线名称", "所在港口", "所在港区", "所属经营人", "利用状态" }; axService.exportExcelAll(voList, titles, outputStream); } catch (IOException e) { e.printStackTrace(); } return null; } /** * 导出当前页数据到excel * * @param response * @param axmc * @param szdid * @param szgkid * @param szgqid * @param ssjyr * @return */ @RequestMapping(value = "/exportExcelthisPage") public String exportExcelthisPage(SimplePageRequest page, HttpServletResponse response, final String axmc, final String szdid, final String szgkid, final String szgqid, final String ssjyr, int pageNum, int pageSize, final String lyztid) { Specification spec = new Specification() { @Override public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) { List list = new ArrayList(); if (StringUtils.isNotEmpty(axmc)) { list.add(cb.like(root.get("axmc").as(String.class), "%" + axmc + "%")); } if (StringUtils.isNotEmpty(szdid)) { list.add(cb.like(root.get("szd").get("id").as(String.class), "%" + szdid + "%")); } if (StringUtils.isNotEmpty(szgkid)) { list.add(cb.like(root.get("szgk").get("id").as(String.class), "%" + szgkid + "%")); } if (StringUtils.isNotEmpty(szgqid)) { list.add(cb.like(root.get("szgq").get("id").as(String.class), "%" + szgqid + "%")); } if (StringUtils.isNotEmpty(ssjyr)) { list.add(cb.like(root.get("ssjyr").get("gkjyr").as(String.class), "%" + ssjyr + "%")); } if (StringUtils.isNotEmpty(lyztid)) { list.add(cb.like(root.get("lyzt").get("id").as(String.class), "%" + lyztid + "%")); } // 经营人只能查看自己企业下的数据,非经营人只能查看自己所在地的数据 if (isJyr()) { list.add(cb.like(root.get("ssjyr").get("id").as(String.class), "%" + getSsjyr().getId() + "%")); } else { In in = cb.in(root.get("szgq").get("id").as(String.class)); @SuppressWarnings("unchecked") List gqlist = (List) Utils.getSession() .getAttribute("gqList"); if (gqlist != null && gqlist.size() > 0) { for (int i = 0; i < gqlist.size(); i++) { in.value(gqlist.get(i)); } list.add(in); } } list.add(cb.notEqual(root.get("recordStatus").as(Integer.class), BaseEntity.RECORD_STATE_DELETE)); list.add(cb.notEqual(root.get("recordStatus").as(Integer.class), BaseEntity.RECORE_STATE_COPY)); return cb.and(list.toArray(new Predicate[] {})); } }; page.setPage(pageNum); page.setRows(pageSize); page.setSort("id"); page.setOrder("desc"); Page list = query.findAll(spec, toPageRequest(page), AxEntity.class); List voList = new ArrayList(); for (AxEntity entity : list) { AxVo vo = new AxVo(); if (null != entity) { BeanUtils.copyProperties(entity, vo); vo.setSzdzw(entity.getSzd() != null ? entity.getSzd().getText() : ""); vo.setSzgqzw(entity.getSzgq() != null ? entity.getSzgq().getGqmc() : ""); vo.setSzgkzw(entity.getSzgk() != null ? entity.getSzgk().getGkmc() : ""); vo.setSsjyrzw(entity.getSsjyr() != null ? entity.getSsjyr().getGkjyr() : ""); vo.setAxlxzw(entity.getAxlx() != null ? entity.getAxlx().getText() : ""); vo.setAxgnzw(entity.getAxgn() != null ? entity.getAxgn().getText() : ""); vo.setLyztzw(entity.getLyzt() != null ? entity.getLyzt().getText() : ""); vo.setNbazw(entity.getNba() != null?entity.getNba().getText() : ""); vo.setSzd(entity.getSzd() != null ? entity.getSzd().getId() : ""); vo.setSzgq(entity.getSzgq() != null ? entity.getSzgq().getId() : ""); vo.setSzgk(entity.getSzgk() != null ? entity.getSzgk().getId() : ""); vo.setSsjyr(entity.getSsjyr() != null ? entity.getSsjyr().getId() : ""); vo.setAxlx(entity.getAxlx() != null ? entity.getAxlx().getId() : ""); vo.setAxgn(entity.getAxgn() != null ? entity.getAxgn().getId() : ""); vo.setLyzt(entity.getLyzt() != null ? entity.getLyzt().getId() : ""); vo.setNba(entity.getNba() != null?entity.getNba().getId() : ""); voList.add(vo); } } response.setContentType("application/binary;charset=iso-8859-1"); try { ServletOutputStream outputStream = response.getOutputStream(); String fileName = new String(("岸线信息单页").getBytes(), "iso-8859-1"); response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xlsx");// 组装附件名称和格式 String[] titles = { "序号", "岸线名称", "所在港口", "所在港区", "所属经营人", "利用状态" }; axService.exportExcelthisPage(voList, titles, outputStream); } catch (IOException e) { e.printStackTrace(); } return null; } /** * 打开批量导入页面 * * @param model * @return */ @RequestMapping(value = "/inputAll") public String inputAll(Model model) { return "gkjcxx/ax/inputAll"; } /** * 批量导入excel数据 * * @param model * @return */ @SuppressWarnings("unchecked") @RequestMapping(value = "/report") @ResponseBody public WebJsonResult report(@RequestParam MultipartFile file) throws IOException { WebJsonResult wr = success(); try { Map map = axService.readReport(file.getInputStream()); if (((List) map.get("error")).size() > 0) { List errorList = (List) map.get("error"); for (int j = 0; j < errorList.size(); j++) { File file1 = new File("D:/bug.txt"); if (!file1.exists()) { file1.createNewFile(); } FileOutputStream fos = new FileOutputStream("D:/bug.txt", true); fos.write(errorList.get(j).getBytes()); fos.flush(); fos.close(); } wr.setMessage("导入文件有错误,请根据操作日志修改正确后重新导入!"); } else { List axList = (List) map.get("axList"); for (int i = 0; i < axList.size(); i++) { axService.save(axList.get(i)); } wr.setMessage("数据导入成功!"); } } catch (Exception e) { wr.setSuccess(false); wr.setMessage("操作失败!"); e.printStackTrace(); } return wr; } /** * 下载错误日志 * * @param request * @param response * @throws UnsupportedEncodingException */ @RequestMapping(value = "/downLoadTxt") public void DownLoadTxt(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException { response.setCharacterEncoding("utf-8"); response.setContentType("multipart/form-data"); String fileName = "错误日志.txt"; if ("FF".equals(getBrowser(request))) { // 针对火狐浏览器处理方式不一样了 fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1"); } else { fileName = URLEncoder.encode(fileName, "UTF-8"); } response.setHeader("Content-Disposition", "attachment;fileName=" + fileName); File file = new File("D:/bug.txt"); try { FileReader reader = new FileReader(file); BufferedReader br = new BufferedReader(reader); String line = br.readLine(); while (line != null) { response.getOutputStream().write(line.getBytes()); line = br.readLine(); } br.close(); file.delete(); return; } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 服务器端判断客户端浏览器类型(IE/FF/SF) * * @param request * @return */ public static String getBrowser(HttpServletRequest request) { String UserAgent = request.getHeader("USER-AGENT").toLowerCase(); if (UserAgent != null) { if (UserAgent.indexOf("msie") >= 0) return "IE"; if (UserAgent.indexOf("firefox") >= 0) return "FF"; if (UserAgent.indexOf("safari") >= 0) return "SF"; } return null; } /*** * 解决js传入参数乱码 * * @param str * @return */ static String enCodeStr(String str) { if (str == null) { str = ""; } try { return new String(str.getBytes("iso-8859-1"), "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); return null; } } }