package com.nanometer.smartlab.controller; import com.nanometer.smartlab.entity.OpeApply; import com.nanometer.smartlab.entity.OpeOrder; import com.nanometer.smartlab.entity.enumtype.ApplyStatus; import com.nanometer.smartlab.service.OpeApplyService; import com.nanometer.smartlab.service.OpeOrderService; 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.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/14. */ @Controller @Scope("session") public class RequireMngController extends BaseController { private static Logger logger = Logger.getLogger(RequireMngController.class); @Resource private OpeApplyService opeApplyService; @Resource private MenuController menuController; @Resource private OpeOrderService opeOrderService; private LazyDataModel dataModel; private OpeOrder opeOrder; private List selectedList; private String reagentName; private Timestamp startDeadline; private Timestamp endDeadline; private OpeApply opeApply; public void onNewBtnClick() { if (selectedList == null || selectedList.size() == 0) { FacesUtils.warn("请选择数据。"); return; } Set results = new HashSet<>(); for(OpeApply item : selectedList) { results.add(item.getReagent().getSupplierId()); } if(results.size() > 1) { FacesUtils.warn("请选择试剂供应商一样的数据。"); return; } this.opeOrder = new OpeOrder(); this.menuController.goToPage(Constants.PAGE_REQUIRE_MNG_NEW, Constants.PAGE_REQUIRE_MNG); } public void onCancelApplyClick(){ if (this.selectedList == null || this.selectedList.size() == 0) { FacesUtils.warn("请选择数据。"); return; } try { for (OpeApply ope:this.selectedList) { this.opeApplyService.cancelApply(ApplyStatus.CANCEL,ope.getId()); } FacesUtils.info("取消成功。"); }catch(Exception e){ logger.error("操作失败。", e); FacesUtils.warn("操作失败。"); } } public void onViewBtnClick() { if (this.selectedList == null || this.selectedList.size() == 0) { FacesUtils.warn("请选择数据。"); return; } if (this.selectedList.size() > 1) { FacesUtils.warn("只能选择一个数据进行查看。"); return; } this.opeApply = this.opeApplyService.getOpeApply(this.selectedList.get(0).getId()); RequestContext.getCurrentInstance().execute("PF('dialog').show()"); } public void onCancelBtnClick() { this.menuController.backToPage(); } public void onSaveBtnClick() { try { if (this.opeOrder == null) { FacesUtils.warn("订单数据为空。"); return; } if (this.selectedList == null || this.selectedList.size() == 0) { FacesUtils.warn("申购数据为空。"); return; } //判断所有供应商是不是同一个 //Set supplierIdCheckSet = new HashSet<>(); //for (OpeApply opeApply : this.selectedList) { // supplierIdCheckSet.add(opeApply.getReagent().getSupplierId()); //} //if (supplierIdCheckSet.size() > 1) { // FacesUtils.warn("选择的供应商不为同一个供应商,订单生产失败!"); //return; //} this.opeOrderService.createOpeOrder(this.opeOrder, this.selectedList, this.getUser()); FacesUtils.info("生成订单成功。"); this.menuController.backToPage(); } 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 = opeApplyService.getOpeApplyTotalCount(reagentName, startDeadline, endDeadline, ApplyStatus.APPROVED.getKey(), null, null, null,null,null); this.setRowCount(count); if (count > 0) { list = opeApplyService.getOpeApplyList(reagentName, startDeadline, endDeadline, ApplyStatus.APPROVED.getKey(), null, null, null, first, pageSize,null,null); } } catch (Exception e) { logger.error(e); } selectedList = null; return list; } @Override public OpeApply getRowData(String rowKey) { Iterator iterator = this.iterator(); // if (iterator != null) { // OpeApply oa = null; // while (iterator.hasNext()) { // oa = iterator.next(); // if (rowKey.equals(oa.getId())) { // return oa; // } // } // } return opeApplyService.getOpeApply(rowKey); } }; } return dataModel; } 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 getReagentName() { return reagentName; } public void setReagentName(String reagentName) { this.reagentName = reagentName; } public Timestamp getStartDeadline() { return startDeadline; } public void setStartDeadline(Timestamp startDeadline) { this.startDeadline = startDeadline; } public Timestamp getEndDeadline() { return endDeadline; } public void setEndDeadline(Timestamp endDeadline) { this.endDeadline = endDeadline; } public OpeApply getOpeApply() { return opeApply; } public void setOpeApply(OpeApply opeApply) { this.opeApply = opeApply; } public BigDecimal calTotal(OpeApply opeApply) { if (opeApply != null && opeApply.getReagent() != null) { BigDecimal count = new BigDecimal(0); BigDecimal price = new BigDecimal(0); if (opeApply.getNum() != null && opeApply.getNum() != 0) { count = new BigDecimal(opeApply.getNum()); } if (opeApply.getReagent().getPrice() != null) { price = opeApply.getReagent().getPrice(); } return count.multiply(price); } return new BigDecimal(0); } public BigDecimal calTotalList(List opeApplyList) { BigDecimal total = new BigDecimal(0); if (opeApplyList != null && opeApplyList.size() > 0) { for (OpeApply apply : opeApplyList) { total = total.add(this.calTotal(apply)); } } return total; } }