package com.nanometer.smartlab.controller; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.annotation.Resource; import com.nanometer.smartlab.dao.SysLaboratoryDao; import com.nanometer.smartlab.entity.SysLaboratory; import com.nanometer.smartlab.entity.SysLaboratoryContainer; import com.nanometer.smartlab.entity.SysWarehouseContainer; import com.nanometer.smartlab.exception.BusinessException; import com.nanometer.smartlab.exception.ExceptionEnumCode; import com.nanometer.smartlab.service.SysLaboratoryContainerService; import com.nanometer.smartlab.service.SysLaboratoryService; import com.nanometer.smartlab.service.SysWarehouseContainerService; import org.apache.log4j.Logger; import org.primefaces.context.RequestContext; import org.primefaces.model.LazyDataModel; import org.primefaces.model.SortOrder; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import com.nanometer.smartlab.entity.SysWarehouse; import com.nanometer.smartlab.service.SysWarehouseService; import com.nanometer.smartlab.util.Constants; import com.nanometer.smartlab.util.FacesUtils; /** * Created by cmower on 17/11/20. */ @Controller @Scope("session") public class WarehouseMngController extends BaseController { private static final long serialVersionUID = 101543880938627455L; private static Logger logger = Logger.getLogger(WarehouseMngController.class); @Resource private SysWarehouseService sysWarehouseService; @Resource private SysLaboratoryDao sysLaboratoryDao; @Resource private SysLaboratoryContainerService sysLaboratoryContainerService; @Resource private SysWarehouseContainerService sysWarehouseContainerService; /** * 数据源 */ private LazyDataModel dataModel; /** * 数据模型 */ private SysWarehouse sysWarehouse; /** * 选中的list */ private List selectedList; private String type; private String name; private int action; public void onNewBtnClick() { this.sysWarehouse = new SysWarehouse(); 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.sysWarehouse = this.sysWarehouseService.getSysWarehouse(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.sysWarehouse == null) { FacesUtils.warn("新建对象为空。"); return; } if (this.sysWarehouseService.isSysWarehouseExist(this.sysWarehouse.getBarCode(), null)) { FacesUtils.warn("地址条码已存在。"); return; } this.sysWarehouseService.insertSysWarehouse(sysWarehouse); FacesUtils.info("新建成功。"); RequestContext.getCurrentInstance().execute("PF('dialog').hide()"); // 修改 } else if (this.action == Constants.ACTION_EDIT) { if (this.sysWarehouse == null) { FacesUtils.warn("修改对象为空。"); return; } if (this.sysWarehouseService.isSysWarehouseExist(this.sysWarehouse.getBarCode(), this.sysWarehouse.getId())) { FacesUtils.warn("地址条码已存在。"); return; } SysLaboratory sysLaboratory = sysLaboratoryDao.getSysLaboratoryByName(sysWarehouse.getName()); if (sysLaboratory != null) { this.sysWarehouseService.updateSysWarehouse2(sysWarehouse,sysLaboratory); } else { this.sysWarehouseService.updateSysWarehouse(sysWarehouse); } 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; } //判断下面是否有柜子,有就拒绝删除 for (SysWarehouse sysWarehouse : selectedList) { List list = sysWarehouseContainerService.getSysWarehouseContainerList(sysWarehouse.getId()); if (list.size() > 0) { throw new BusinessException(ExceptionEnumCode.SYS_ERR, "该仓库下还有货柜,请清除货柜后删除仓库"); } } this.sysWarehouseService.deleteSysWarehouse(this.selectedList); FacesUtils.info("删除成功。"); } catch (BusinessException e) { logger.error(e.getMessage()); FacesUtils.warn(e.getMessage()); } catch (Exception e) { logger.error("操作失败。", e); FacesUtils.warn("操作失败。"); } } @SuppressWarnings("serial") 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 = sysWarehouseService.getSysWarehouseTotalCount(type, name); this.setRowCount(count); if (count > 0) { list = sysWarehouseService.getSysWarehouseList(type, name, first, pageSize); } selectedList = new ArrayList<>(); } catch (Exception e) { logger.error(e); } return list; } @Override public SysWarehouse getRowData(String rowKey) { // Iterator iterator = this.iterator(); // if (iterator != null) { // SysWarehouse su = null; // while (iterator.hasNext()) { // su = iterator.next(); // if (rowKey.equals(su.getId())) { // return su; // } // } // } return sysWarehouseService.getSysWarehouse(rowKey); } }; } return dataModel; } public SysWarehouse getSysWarehouse() { return sysWarehouse; } public void setSysWarehouse(SysWarehouse sysWarehouse) { this.sysWarehouse = sysWarehouse; } public List getSelectedList() { return selectedList; } public void setSelectedList(List selectedList) { this.selectedList = selectedList; } public int getAction() { return action; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getType() { return type; } public void setType(String type) { this.type = type; } }