package com.nanometer.smartlab.controller; import com.nanometer.smartlab.entity.HazardousWaste; import com.nanometer.smartlab.entity.SysLaboratory; import com.nanometer.smartlab.entity.SysUser; import com.nanometer.smartlab.entity.dto.HazardousWasteUser; import com.nanometer.smartlab.entity.enumtype.Waster; import com.nanometer.smartlab.service.HazardousWasteService; import com.nanometer.smartlab.service.SysUserService; import com.nanometer.smartlab.util.Constants; import com.nanometer.smartlab.util.FacesUtils; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.primefaces.context.RequestContext; import org.primefaces.model.LazyDataModel; import org.primefaces.model.SortOrder; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import javax.annotation.Resource; import javax.faces.event.ActionListener; import java.text.SimpleDateFormat; import java.util.*; @Controller @Scope("session") public class HazardousWasteController extends BaseController{ private static Logger logger = Logger.getLogger(HazardousWasteController.class); private LazyDataModel dataModel; private Date startTime; private Date endTime; private String status; private String applyPerson; private Long projectId; private String department; private String tid; private List statusList = new ArrayList<> (); private boolean isSearch = false; private boolean isAnalysSearch = false; private LazyDataModel analysisDataModel; private HazardousWaste selectedOne; /** * 选中的list */ private List selectedList; private HazardousWaste hazardousWaste=new HazardousWaste(); private int action; @Autowired private SysUserService sysUserService; private List userSelectList; @Resource private HazardousWasteService hazardousWasteService; public List getUserSelectList() { if (userSelectList == null) { this.initUserSelectList(); } return userSelectList; } private void initUserSelectList() { this.setUserSelectList(this.sysUserService.getSysUserList(null, null, null, null, null,null,null)); } public SysUser getSelectedUserById(Long userId) { SysUser sysUser=this.sysUserService.getSysUser(userId); if(sysUser==null){ return new SysUser(); } return sysUser; } public void addHazardousWaste(){ this.hazardousWaste = new HazardousWaste(); this.action = Constants.ACTION_ADD; RequestContext.getCurrentInstance().execute("PF('wastedialog').show()"); } public void editHazardousWaste(){ if (this.selectedList == null || this.selectedList.size() == 0) { FacesUtils.warn("请选择数据。"); return; } if (this.selectedList.size() > 1) { FacesUtils.warn("只能选择一个数据进行修改。"); return; } this.hazardousWaste=this.hazardousWasteService.selectById(this.selectedList.get(0).getId()); this.action = Constants.ACTION_EDIT; RequestContext.getCurrentInstance().execute("PF('wastedialog').show()"); } public void onSaveBtnClick(){ try { Long userId = this.hazardousWaste.getApplyPerson(); SysUser sysUser = sysUserService.getSysUser(userId); this.hazardousWaste.setUnit(sysUser.getCompany()); String userName = getUserName(); if (this.action == Constants.ACTION_ADD) { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS"); this.hazardousWaste.setCreator(userName); this.hazardousWaste.setTid(sdf.format(new Date())); this.hazardousWaste.setStatus("待确认"); hazardousWasteService.insertInfo(this.hazardousWaste); FacesUtils.info("新建成功。"); RequestContext.getCurrentInstance().execute("PF('wastedialog').hide()"); } else { this.hazardousWaste.setUpdator(userName); hazardousWasteService.updateWaste(this.hazardousWaste); FacesUtils.info("修改成功。"); RequestContext.getCurrentInstance().execute("PF('wastedialog').hide()"); } }catch (Exception e){ logger.error("操作失败。", e); FacesUtils.warn("操作失败。"); } } public LazyDataModel getDataModel() { if (this.dataModel == null) { this.dataModel = new LazyDataModel() { public List load (int first, int pageSize, String sortField, SortOrder sortOrder, Map filters) { List list = null; try { if (getUser().getWaster() != null){ if (getUser().getWaster().equals(Waster.MANAGE)){ int pageCount = hazardousWasteService.countAll(startTime,endTime,status,applyPerson,projectId,department,tid); this.setRowCount(pageCount); if (pageCount > 0) list = hazardousWasteService.selectAll(startTime,endTime,status,applyPerson,projectId,department,tid,first, pageSize); }else if (getUser().getProjectId()!=null){ int pageCount = hazardousWasteService.countAll(startTime,endTime,status,applyPerson,getUser().getProjectId(),department,tid); this.setRowCount(pageCount); if (pageCount > 0) list = hazardousWasteService.selectAll(startTime,endTime,status,applyPerson,getUser().getProjectId(),department,tid,first, pageSize); } }else { this.setRowCount(0); } //查询更换模式 if (isSearch) { isSearch = false; } selectedOne = null; } catch (Exception e) { logger.error("error occured.", e); } return list; } @Override public HazardousWaste getRowData(String rowKey) { // Iterator iterator = this.iterator(); // HazardousWaste su = null; // while (iterator.hasNext()) { // su = iterator.next(); // if ( su.getId().equals(Long.parseLong(rowKey))) { // return su; // } // } return hazardousWasteService.selectById(Long.parseLong(rowKey)); } }; } return dataModel; } public void modelChange() { this.isSearch = true; } public void setDataModel(LazyDataModel dataModel) { this.dataModel = dataModel; } public LazyDataModel getAnalysisDataModel() { if (this.analysisDataModel == null) { this.analysisDataModel = new LazyDataModel() { public List load (int first, int pageSize, String sortField, SortOrder sortOrder, Map filters) { List list = null; try { if (getUser().getWaster() != null){ if (getUser().getWaster().equals(Waster.MANAGE)){ int pageCount = hazardousWasteService.countStatistics(startTime,endTime,status,applyPerson,projectId,department); this.setRowCount(pageCount); if (pageCount > 0) list = hazardousWasteService.selectStatistics(startTime,endTime,status,applyPerson,projectId,department,first, pageSize); }else if (getUser().getProjectId()!=null){ int pageCount = hazardousWasteService.countStatistics(startTime,endTime,status,applyPerson,getUser().getProjectId(),department); this.setRowCount(pageCount); if (pageCount > 0) list = hazardousWasteService.selectStatistics(startTime,endTime,status,applyPerson,getUser().getProjectId(),department,first, pageSize); } }else { this.setRowCount(0); } //查询更换模式 if (isAnalysSearch) { isAnalysSearch = false; } } catch (Exception e) { logger.error("error occured.", e); } return list; } @Override public HazardousWaste getRowData(String rowKey) { // Iterator iterator = this.iterator(); // HazardousWaste su = null; // while (iterator.hasNext()) { // su = iterator.next(); // if ( su.getId().equals(Long.parseLong(rowKey))) { // return su; // } // } return null; } }; } return analysisDataModel; } public void modelAnalysisChange() { this.isAnalysSearch = true; } public void setAnalysisDataModel(LazyDataModel dataModel) { this.analysisDataModel = dataModel; } public Date getStartTime() { return startTime; } public void setStartTime(Date startTime) { this.startTime = startTime; } public Date getEndTime() { return endTime; } public void setEndTime(Date endTime) { this.endTime = endTime; } public boolean isSearch() { return isSearch; } public void setSearch(boolean search) { isSearch = search; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public String getApplyPerson() { return applyPerson; } public void setApplyPerson(String applyPerson) { this.applyPerson = applyPerson; } public Long getProjectId() { return projectId; } public void setProject(Long projectId) { this.projectId = projectId; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } public List getStatusList() { statusList = new ArrayList<> (); statusList.add("全部"); statusList.add("待确认"); statusList.add("已确认"); statusList.add("已拒绝"); statusList.add("已取消"); return statusList; } public void export2Excel() { List list = new ArrayList<>(); if (getUser().getWaster() != null){ if (getUser().getWaster().equals(Waster.MANAGE)){ list = hazardousWasteService.exportList(startTime,endTime,status,applyPerson,projectId,department,tid); }else if (getUser().getProjectId()!=null){ list = hazardousWasteService.exportList(startTime,endTime,status,applyPerson,getUser().getProjectId(),department,tid); } } try{ hazardousWasteService.export2Excel(list); }catch (Exception e){ e.printStackTrace(); FacesUtils.warn("导出失败"); } } public void exportStatistics2Excel(){ List list = new ArrayList<>(); if (getUser().getWaster() != null){ if (getUser().getWaster().equals(Waster.MANAGE)){ list = hazardousWasteService.exportStatisticsList(startTime,endTime,status,applyPerson,projectId,department); }else if (getUser().getProjectId()!=null){ list = hazardousWasteService.exportStatisticsList(startTime,endTime,status,applyPerson,getUser().getProjectId(),department); } } try{ hazardousWasteService.exportStatistics2Excel(list); }catch (Exception e){ e.printStackTrace(); FacesUtils.warn("导出失败"); } } public void cancelOne(){ //待确认状态的才能取消 if (this.selectedList == null || this.selectedList.size() == 0) { FacesUtils.warn("请选择数据。"); return; } if (this.selectedList.size() > 1) { FacesUtils.warn("只能选择一个数据进行修改。"); return; } HazardousWaste hwaste=this.selectedList.get(0); if (!"待确认".equals(hwaste.getStatus())) { FacesUtils.warn("非待确认不能取消"); return; } try { hwaste.setStatus("已取消"); hazardousWasteService.updateWaste(hwaste); FacesUtils.warn("操作成功"); } catch (Exception e) { e.printStackTrace(); FacesUtils.warn("操作失败"); } } public void recoverOne(){ if (this.selectedList == null || this.selectedList.size() == 0) { FacesUtils.warn("请选择数据。"); return; } if (this.selectedList.size() > 1) { FacesUtils.warn("只能选择一个数据进行修改。"); return; } HazardousWaste hwaste=this.selectedList.get(0); if (!"已取消".equals(hwaste.getStatus())) { FacesUtils.warn("非已取消不能恢复"); return; } try { hwaste.setStatus("待确认"); hazardousWasteService.updateWaste(hwaste); FacesUtils.warn("操作成功"); } catch (Exception e) { e.printStackTrace(); FacesUtils.warn("操作失败"); } } public String getTid() { return tid; } public void setTid(String tid) { this.tid = tid; } public HazardousWaste getSelectedOne() { return selectedOne; } public void setSelectedOne(HazardousWaste selectedOne) { this.selectedOne = selectedOne; } public List getSelectedList() { return selectedList; } public void setSelectedList(List selectedList) { this.selectedList = selectedList; } public HazardousWaste getHazardousWaste() { return hazardousWaste; } public void setHazardousWaste(HazardousWaste hazardousWaste) { this.hazardousWaste = hazardousWaste; } public int getAction() { return action; } public void setAction(int action) { this.action = action; } public void setUserSelectList(List userSelectList) { this.userSelectList = userSelectList; } }