package com.nanometer.smartlab.controller; import com.nanometer.smartlab.entity.SysSupplier; import com.nanometer.smartlab.service.SysSupplierService; import com.nanometer.smartlab.util.Constants; import com.nanometer.smartlab.util.FacesUtils; 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.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import javax.annotation.Resource; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; /** * Created by johnny on 17/11/29. */ @Controller @Scope("session") public class SupplierInfoMngController extends BaseController { private static Logger logger = Logger.getLogger(SupplierInfoMngController.class); @Resource private SysSupplierService sysSupplierService; private LazyDataModel dataModel; private SysSupplier sysSupplier; private List selectedList; private String name; private int action; public void onNewBtnClick() { this.sysSupplier = new SysSupplier(); 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.sysSupplier = this.sysSupplierService.getSysSupplier(this.selectedList.get(0).getId()); this.action = Constants.ACTION_EDIT; RequestContext.getCurrentInstance().execute("PF('dialog').show()"); } public void onSaveBtnClick() { try { // 新建 if (this.action == Constants.ACTION_ADD) { if (this.sysSupplier == null) { FacesUtils.warn("新建对象为空。"); return; } this.sysSupplierService.insertSysSupplier(this.sysSupplier); FacesUtils.info("新建成功。"); RequestContext.getCurrentInstance().execute("PF('dialog').hide()"); // 修改 } else if (this.action == Constants.ACTION_EDIT) { if (this.sysSupplier == null) { FacesUtils.warn("修改对象为空。"); return; } this.sysSupplierService.updateSysSupplier(this.sysSupplier); 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; } this.sysSupplierService.deleteSysSupplier(this.selectedList); FacesUtils.info("删除成功。"); } catch (Exception e) { logger.error("操作失败。", e); 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 = sysSupplierService.getSysSupplierTotalCount(name); this.setRowCount(count); if (count > 0) { list = sysSupplierService.getSysSupplierList(name, first, pageSize); } selectedList = new ArrayList<>(); } catch (Exception e) { logger.error(e); } return list; } @Override public SysSupplier getRowData(String rowKey) { // Iterator iterator = this.iterator(); // if (iterator != null) { // SysSupplier ss = null; // while (iterator.hasNext()) { // ss = iterator.next(); // if (rowKey.equals(ss.getId())) { // return ss; // } // } // } Long id=Long.valueOf(rowKey); return sysSupplierService.getSysSupplier(id); } }; } return dataModel; } public void export2Excel(){ List list = sysSupplierService.exportExcelList(name); try{ sysSupplierService.export2Excel(list); }catch (Exception e){ e.printStackTrace(); FacesUtils.warn("导出失败"); } } public void uploadSupplier(FileUploadEvent event){ try { sysSupplierService.importSupplier(event); FacesUtils.info("导入成功"); } catch (Exception e) { e.printStackTrace(); FacesUtils.warn("导入失败"); } } public SysSupplier getSysSupplier() { return sysSupplier; } public void setSysSupplier(SysSupplier sysSupplier) { this.sysSupplier = sysSupplier; } public List getSelectedList() { return selectedList; } public void setSelectedList(List selectedList) { this.selectedList = selectedList; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAction() { return action; } }