package com.nanometer.smartlab.controller; import com.nanometer.smartlab.entity.*; import com.nanometer.smartlab.entity.enumtype.ApplyStatus; import com.nanometer.smartlab.exception.BusinessException; import com.nanometer.smartlab.service.*; 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.context.annotation.Scope; import org.springframework.stereotype.Controller; import javax.annotation.Resource; import java.math.BigDecimal; import java.sql.Timestamp; import java.util.*; /** * Created by johnny on 17/12/15. */ @Controller @Scope("session") public class OrderMngController extends BaseController { private static Logger logger = Logger.getLogger(OrderMngController.class); @Resource private OpeOrderService opeOrderService; @Resource private MenuController menuController; @Resource private SysWarehouseService sysWarehouseService; @Resource private SysWarehouseContainerService sysWarehouseContainerService; @Resource private BaseMetaService baseMetaService; @Resource private SysUserService sysUserService; private LazyDataModel dataModel; private OpeOrder opeOrder; private List selectedList; private String orderCode; private String orderName; private Timestamp startTime; private Timestamp endTime; private String orderUserName; private Integer orderStatus; private List orderStatusSelectList; private List userSelectList; private List opeOrderApplyList; private List warehouseList; private List warehouseContainerList; private List warehouseContainerPlaceList; private Map warehouseNameMap; private Map warehouseContainerMap; private Map> warehouseIdContainerMap; public void initRukuPage() { this.userSelectList = this.sysUserService.getSysUserList(null, null, null, null, null,null,null); //将admin置于首位 for (int i = 0; i < userSelectList.size(); i++) { if ("admin".equals(userSelectList.get(i).getAccount())){ userSelectList.add(0, userSelectList.remove(i)); } } this.warehouseList = this.sysWarehouseService.getSysWarehouseList(null, null, null, null); this.warehouseNameMap = new HashMap(); if (this.warehouseList != null && this.warehouseList.size() > 0) { for (SysWarehouse house : this.warehouseList) { this.warehouseNameMap.put(house.getId(), house.getName()); } } this.warehouseContainerMap = new HashMap(); this.warehouseIdContainerMap = new HashMap>(); List warehouseContainerList = this.sysWarehouseContainerService.getSysWarehouseContainerList(null, null, null, null, null); if (warehouseContainerList != null && warehouseContainerList.size() > 0) { for (SysWarehouseContainer container : warehouseContainerList) { warehouseContainerMap.put(container.getId(), container); if (!warehouseIdContainerMap.containsKey(container.getWarehouseId()) || warehouseIdContainerMap.get(container.getWarehouseId()) == null) { warehouseIdContainerMap.put(container.getWarehouseId(), new ArrayList()); } warehouseIdContainerMap.get(container.getWarehouseId()).add(container); } } this.onWarehouseSelectChange(null); } public void initChangeCodePage() { initRukuPage(); } public void onRukuBtnClick() { if (this.selectedList == null || this.selectedList.size() == 0) { FacesUtils.warn("请选择数据。"); return; } if (this.selectedList.size() > 1) { FacesUtils.warn("只能选择一个数据进行入库。"); return; } this.opeOrder = this.opeOrderService.getOpeOrder(this.selectedList.get(0).getId()); if (this.opeOrder.getStatus().getKey() != ApplyStatus.PENDING_PURCHASE.getKey() && this.opeOrder.getStatus().getKey() != ApplyStatus.SUPPLIER_CONFIRM.getKey() && this.opeOrder.getStatus().getKey() != ApplyStatus.UNCOMPLETED.getKey()) { FacesUtils.warn("只有待采购,已确认和未完成状态的订单才能进行入库。"); return; } this.opeOrderApplyList = this.opeOrderService.getOpeApplyListByOrder(this.selectedList.get(0).getId()); this.menuController.goToPage(Constants.PAGE_ORDER_MNG_RUKU, Constants.PAGE_ORDER_MNG); } // public void onEditBtnClick() { // if (this.selectedList == null // || this.selectedList.size() == 0) { // FacesUtils.warn("请选择数据。"); // return; // } // if (this.selectedList.size() > 1) { // FacesUtils.warn("只能选择一个数据进行修改。"); // return; // } // // this.opeOrder = this.opeOrderService.getOpeOrder(this.selectedList.get(0).getId()); // /*if (this.opeOrder.getStatus().getKey() != ApplyStatus.PENDING_PURCHASE.getKey()) { // FacesUtils.warn("只有待采购状态的订单才能进行入库。"); // return; // }*/ // /*this.opeApply = this.opeApplyService.getOpeApply(this.selectedList.get(0).getId()); // this.opeApply = this.opeApplyService.getOpeApply(this.selectedList.get(0).getId()); // this.action = Constants.ACTION_EDIT;*/ // this.menuController.goToPage(Constants.PAGE_ORDER_MNG_EDIT, Constants.PAGE_APPLY_MNG); // } public void onViewBtnClick() { if (this.selectedList == null || this.selectedList.size() == 0) { FacesUtils.warn("请选择数据。"); return; } if (this.selectedList.size() > 1) { FacesUtils.warn("只能选择一个数据进行查看。"); return; } this.opeOrder = this.opeOrderService.getOpeOrder(this.selectedList.get(0).getId()); this.opeOrderApplyList = this.opeOrderService.getOpeApplyListByOrder(this.selectedList.get(0).getId()); RequestContext.getCurrentInstance().execute("PF('dialog').show()"); } public void onDeleteBtnClick() { try { if (this.selectedList == null || this.selectedList.size() == 0) { FacesUtils.warn("请选择数据。"); return; } for (OpeOrder oo : this.selectedList) { if (!this.opeOrderService.isOrderPendingPurchase(oo)) { FacesUtils.warn("只有待采购状态的数据可以删除。"); return; } } this.opeOrderService.deleteOpeOrder(this.selectedList); FacesUtils.info("删除成功。"); } catch (Exception e) { logger.error("操作失败。", e); FacesUtils.warn("操作失败。"); } } public void onSaveBtnClick() { try { if (this.opeOrder == null) { FacesUtils.warn("订单数据为空。"); return; } if (this.opeOrderApplyList == null || this.opeOrderApplyList.size() == 0) { FacesUtils.warn("入库产品数据为空。"); return; } BigDecimal startReagentCode = null; BigDecimal endReagentCode = null; for (OpeApply opeApply : this.opeOrderApplyList) { // 检查到货数量 if (opeApply.getArrivalNum() == null || opeApply.getArrivalNum() <= 0) { FacesUtils.warn("申购编号:" + opeApply.getApplyCode() + "的到货数量不能为空,且要大于0。"); return; } if (StringUtils.isBlank(opeApply.getStartReagentCode()) || StringUtils.isBlank(opeApply.getEndReagentCode())) { FacesUtils.warn("申购编号:" + opeApply.getApplyCode() + "的条形码未自动生成"); return; } // 检查仓库名 if (opeApply.getHouseId()==null) { FacesUtils.warn("申购编号:" + opeApply.getApplyCode() + "的仓库名不能为空。"); return; } // 检查货柜名 if (opeApply.getContainerId()==null) { FacesUtils.warn("申购编号:" + opeApply.getApplyCode() + "的货柜名不能为空。"); return; } // 检查货柜位置 if (StringUtils.isBlank(opeApply.getPlace())) { FacesUtils.warn("申购编号:" + opeApply.getApplyCode() + "的货柜位置不能为空。"); return; } } this.opeOrderService.rukuOpeOrder(this.opeOrder, this.opeOrderApplyList); // 待发邮件保存 this.opeOrderService.emailStatusSave(this.opeOrder, this.opeOrderApplyList); FacesUtils.info("入库成功。"); this.onCancelBtnClick(); } catch (BusinessException e) { logger.warn("操作失败。", e); FacesUtils.warn(e.getMessage()); } catch (Exception e) { logger.error("操作失败。", e); FacesUtils.warn("操作失败。"); } } public void onCancelBtnClick() { this.menuController.backToPage(); } 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 = opeOrderService.getOpeOrderTotalCount(orderCode, orderName, startTime, endTime, orderStatus, orderUserName); this.setRowCount(count); if (count > 0) { list = opeOrderService.getOpeOrderList(orderCode, orderName, startTime, endTime, orderStatus, orderUserName, first, pageSize); } } catch (Exception e) { logger.error(e); } selectedList = null; return list; } @Override public OpeOrder getRowData(String rowKey) { // Iterator iterator = this.iterator(); // if (iterator != null) { // OpeOrder oo = null; // while (iterator.hasNext()) { // oo = iterator.next(); // if (rowKey.equals(oo.getId())) { // return oo; // } // } // } Long id=Long.valueOf(rowKey); return opeOrderService.getOpeOrder(id); } }; } return dataModel; } public void onChangeCodePageSaveBtnClick() { try { if (this.opeOrder == null) { FacesUtils.warn("订单数据为空。"); return; } if (this.opeOrderApplyList == null || this.opeOrderApplyList.size() == 0) { FacesUtils.warn("变更条形码产品数据为空。"); return; } BigDecimal startReagentCode = null; BigDecimal endReagentCode = null; for (OpeApply opeApply : this.opeOrderApplyList) { // 检查到货数量 if (opeApply.getArrivalNum() == null || opeApply.getArrivalNum() <= 0) { FacesUtils.warn("申购编号:" + opeApply.getApplyCode() + "的到货数量不能为空,且要大于0。"); return; } // 检查货号 if (StringUtils.isBlank(opeApply.getArticleNumber())) { FacesUtils.warn("申购编号:" + opeApply.getApplyCode() + "的货号不能为空。"); return; } // 检查条形码开始 if (StringUtils.isBlank(opeApply.getStartReagentCode())) { FacesUtils.warn("申购编号:" + opeApply.getApplyCode() + "的条形码开始不能为空。"); return; } // 检查条形码结束 if (StringUtils.isBlank(opeApply.getEndReagentCode())) { FacesUtils.warn("申购编号:" + opeApply.getApplyCode() + "的条形码结束不能为空。"); return; } startReagentCode = new BigDecimal(opeApply.getStartReagentCode()); endReagentCode = new BigDecimal(opeApply.getEndReagentCode()); // 检查条形码结束要大于条形码开始 if (endReagentCode.compareTo(startReagentCode) == -1) { FacesUtils.warn("申购编号:" + opeApply.getApplyCode() + "的条形码开始必须小于条形码结束。"); return; } // 检查条形码和到货数量是否匹配 if (endReagentCode.subtract(startReagentCode).intValue() != (opeApply.getArrivalNum() - 1)) { FacesUtils.warn("申购编号:" + opeApply.getApplyCode() + "的条形码数量和到货数量不一致。"); return; } // // 检查仓库名 // if (StringUtils.isBlank(opeApply.getHouseId())) { // FacesUtils.warn("申购编号:" + opeApply.getApplyCode() + "的仓库名不能为空。"); // return; // } // // 检查货柜名 // if (StringUtils.isBlank(opeApply.getContainerId())) { // FacesUtils.warn("申购编号:" + opeApply.getApplyCode() + "的货柜名不能为空。"); // return; // } // // 检查货柜位置 // if (StringUtils.isBlank(opeApply.getPlaceId())) { // FacesUtils.warn("申购编号:" + opeApply.getApplyCode() + "的货柜位置不能为空。"); // return; // } } //由于修改条形码重新入库 this.opeOrderService.reRukuInfo(this.opeOrder, this.opeOrderApplyList); // 待发邮件保存 this.opeOrderService.emailStatusSave(this.opeOrder, this.opeOrderApplyList); FacesUtils.info("条形码修改成功。"); this.onCancelBtnClick(); } catch (BusinessException e) { logger.warn("操作失败。", e); FacesUtils.warn(e.getMessage()); } catch (Exception e) { logger.error("操作失败。", e); FacesUtils.warn("操作失败。"); } } public OpeOrder getOpeOrder() { return opeOrder; } public void setOpeOrder(OpeOrder opeOrder) { this.opeOrder = opeOrder; } public List getSelectedList() { return selectedList; } public void setSelectedList(List selectedList) { this.selectedList = selectedList; } public String getOrderCode() { return orderCode; } public void setOrderCode(String orderCode) { this.orderCode = orderCode; } public String getOrderName() { return orderName; } public void setOrderName(String orderName) { this.orderName = orderName; } public Timestamp getStartTime() { return startTime; } public void setStartTime(Timestamp startTime) { this.startTime = startTime; } public Timestamp getEndTime() { return endTime; } public void setEndTime(Timestamp endTime) { this.endTime = endTime; } public String getOrderUserName() { return orderUserName; } public void setOrderUserName(String orderUserName) { this.orderUserName = orderUserName; } public Integer getOrderStatus() { return orderStatus; } public void setOrderStatus(Integer orderStatus) { this.orderStatus = orderStatus; } public List getOrderStatusSelectList() { if (this.orderStatusSelectList == null) { this.orderStatusSelectList = new ArrayList(); for(ApplyStatus d : ApplyStatus.values()){ orderStatusSelectList.add(d); } } return orderStatusSelectList; } public List getOpeOrderApplyList() { return opeOrderApplyList; } public List getWarehouseList() { return warehouseList; } public List getWarehouseContainerList() { return warehouseContainerList; } public List getWarehouseContainerPlaceList() { return warehouseContainerPlaceList; } public List getUserSelectList() { return userSelectList; } public String formatWarehouseName(Long id) { if(id==null){ return ""; } String name=warehouseNameMap.get(id); if(StringUtils.isBlank(name)){ SysWarehouse warehouse=this.sysWarehouseService.getSysWarehouse(id); if(warehouse!=null){ warehouseNameMap.put(warehouse.getId(),warehouse.getName()); name=warehouse.getName(); } } return name; } public String formatWarehouseContainerName(Long id) { if(id==null){ return ""; } String containerCode=this.warehouseContainerMap.get(id).getContainerCode(); if(StringUtils.isBlank(containerCode)){ SysWarehouseContainer sysWarehouseContainer = this.sysWarehouseContainerService.getSysWarehouseContainer(id); if(sysWarehouseContainer!=null){ this.warehouseContainerMap.put(sysWarehouseContainer.getId(),sysWarehouseContainer); containerCode=sysWarehouseContainer.getContainerCode(); } } return containerCode; } public String formatWarehouseContainerPlaceName(Long id) { if (id!=null) { return this.baseMetaService.getBaseMetaValue(id); } return ""; } public void onWarehouseSelectChange(Long warehouseId) { this.warehouseContainerList = null; this.warehouseContainerPlaceList = null; this.changeWarehouseContainerList(warehouseId); this.changeWarehouseContainerPlaceList(null); } public void onWarehouseContainerSelectChange(Long warehouseContainerId) { this.warehouseContainerPlaceList = null; this.changeWarehouseContainerPlaceList(warehouseContainerId); } private void changeWarehouseContainerList(Long warehouseId) { if (warehouseId==null) { if (this.warehouseList != null && this.warehouseList.size() > 0) { warehouseId = this.warehouseList.get(0).getId(); } } if (warehouseId!=null) { this.warehouseContainerList = warehouseIdContainerMap.get(warehouseId); } } private void changeWarehouseContainerPlaceList(Long warehouseContainerId) { if (warehouseContainerId==null) { if (this.warehouseContainerList != null && this.warehouseContainerList.size() > 0) { warehouseContainerId = this.warehouseContainerList.get(0).getId(); } } if (warehouseContainerId==null) { SysWarehouseContainer container = this.warehouseContainerMap.get(warehouseContainerId); BaseMeta baseMeta = null; if (container != null && (baseMeta = this.baseMetaService.getBaseMeta(container.getStructure())) != null) { this.warehouseContainerPlaceList = this.baseMetaService.getBaseMetaList(baseMeta.getMetaKey()); } } } }