package com.nanometer.smartlab.controller; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.*; import javax.annotation.Resource; import org.apache.log4j.Logger; import org.primefaces.context.RequestContext; import org.primefaces.event.FileUploadEvent; import org.primefaces.model.LazyDataModel; import org.primefaces.model.SortOrder; import org.primefaces.model.UploadedFile; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import com.nanometer.smartlab.entity.SysController; import com.nanometer.smartlab.service.SysControllerService; import com.nanometer.smartlab.util.Constants; import com.nanometer.smartlab.util.FacesUtils; /** * Created by johnny on 17/11/20. */ @Controller @Scope("session") public class SysControllerMngController extends BaseController { private static Logger logger = Logger.getLogger(SysControllerMngController.class); @Resource private SysControllerService sysControllerService; @Value("${filePath}") private String filePath; //配置文件配置的物理保存地址 private LazyDataModel dataModel; private SysController controller; private List selectedList; private String name; private String code; private int action; public void onNewBtnClick() { this.controller = new SysController(); this.action = Constants.ACTION_ADD; } public void onEditBtnClick() { if (this.selectedList == null || this.selectedList.size() == 0) { FacesUtils.warn("请选择数据。"); return; } if (this.selectedList.size() > 1) { FacesUtils.warn("只能选择一个数据进行修改。"); return; } this.controller = this.sysControllerService.getSysController(this.selectedList.get(0).getId()); this.action = Constants.ACTION_EDIT; RequestContext.getCurrentInstance().execute("PF('dialog').show()"); } public void onSaveBtnClick() { try { if (this.controller == null) { FacesUtils.warn("新建对象为空。"); return; } // 新建 if (this.action == Constants.ACTION_ADD) { if (this.sysControllerService.getSysControllerByCode(controller.getControllerCode())) { FacesUtils.warn("设备代码已存在。"); return; } if (this.sysControllerService.getSysControllerByName(controller.getControllerName())) { FacesUtils.warn("设备名称已存在。"); return; } this.sysControllerService.insertSysController(controller); FacesUtils.info("新建成功。"); RequestContext.getCurrentInstance().execute("PF('dialog').hide()"); // 修改 } else if (this.action == Constants.ACTION_EDIT) { this.sysControllerService.updateSysController(controller); FacesUtils.info("修改成功。"); RequestContext.getCurrentInstance().execute("PF('dialog').hide()"); } } catch (Exception e) { logger.error("操作失败。", e); FacesUtils.warn("操作失败。"); } } public void onDeleteBtnClick() { try { if (this.selectedList == null || this.selectedList.size() == 0) { FacesUtils.warn("请选择数据。"); return; } List ids = new ArrayList<>(); for(SysController controller : this.selectedList) { ids.add(controller.getId()); } this.sysControllerService.deleteSysControllers(ids); FacesUtils.info("删除成功。"); } catch (Exception e) { logger.error("操作失败。", e); FacesUtils.warn("操作失败。"); } } public void uploadFile(FileUploadEvent event){ try { sysControllerService.uploadFile(event); } catch (Exception e) { e.printStackTrace(); FacesUtils.warn("导入失败"); } } public void export2Excel(){ List list = sysControllerService.exportExcelList(name, code); try{ sysControllerService.export2Excel(list); }catch (Exception e){ e.printStackTrace(); FacesUtils.warn("导出失败"); } } public LazyDataModel getDataModel() { if (this.dataModel == null) { this.dataModel = new LazyDataModel() { @Override public List load(int first, int pageSize, String sortField, SortOrder sortOrder, Map filters) { List list = null; try { int count = sysControllerService.getSysControllerListCount(code, name); this.setRowCount(count); if (count > 0) { list = sysControllerService.getSysControllerList(code, name, first, pageSize); } selectedList = new ArrayList<>(); } catch (Exception e) { logger.error(e); } return list; } @Override public SysController getRowData(String rowKey) { // Iterator iterator = this.iterator(); // if (iterator != null) { // SysController br = null; // while (iterator.hasNext()) { // br = iterator.next(); // if (Integer.valueOf(rowKey) == br.getId()) { // return br; // } // } // } return sysControllerService.getSysController(Long.valueOf(rowKey)); } }; } return dataModel; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public SysController getController() { return controller; } public void setController(SysController controller) { this.controller = controller; } public List getSelectedList() { return selectedList; } public void setSelectedList(List selectedList) { this.selectedList = selectedList; } public int getAction() { return action; } }