Blame view

src/main/java/com/phxl/modules/goods/service/billsettlement/BillSettlementService.java 6.73 KB
Quilan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
package com.phxl.modules.goods.service.billsettlement;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.Now;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.phxl.common.util.DateUtils;
import com.phxl.common.utils.BaseException;
import com.phxl.common.utils.IdUtil;
import com.phxl.modules.goods.dao.billsettlement.BillSettlementDao;
import com.phxl.modules.goods.dao.billsettlement.BillSettlementDetailDao;
import com.phxl.modules.goods.entity.billsettlement.BillSettlement;
import com.phxl.modules.goods.entity.billsettlement.BillSettlementDetail;
import com.phxl.modules.goods.entity.billsettlement.BillSettlementWithDetail;


@Service
public class BillSettlementService{

	@Autowired
	private BillSettlementDao billSettlementDao;

	@Autowired
	private BillSettlementDetailDao billSettlementDetailDao;
	
	/**
	 * 日志对象
	 */
	protected Logger logger = LoggerFactory.getLogger(getClass());

	public boolean batchSave(List<BillSettlementWithDetail> settlementWithDetails) {
		boolean result = true;
		try {
			checkBillSettlement(settlementWithDetails);
		} catch (Exception e) {
			throw new BaseException(e.getMessage());
		}
		for (BillSettlementWithDetail settlement : settlementWithDetails) {
			BillSettlement billSettlement = settlement.getTRSettle();
			List<BillSettlementDetail> details = new ArrayList<BillSettlementDetail>();
			details = settlement.getTRSettle().getTRSettleDetail();
			String id = IdUtil.uuid();
			Integer rowno = 1;
			billSettlement.setId(id);
			if(details != null && details.size() != 0){
				for (BillSettlementDetail billSettlementDetail : details) {
					billSettlementDetail.setSettleBillNo(billSettlement.getSettleBillNo());
					billSettlementDetail.setId(id);
					billSettlementDetail.setRowNo(rowno);
					billSettlementDetail.setCreate_time(new Date());
					billSettlementDetail.setUpdate_time(new Date());
					rowno ++;
				}
				billSettlementDao.insert(billSettlement);
				billSettlementDetailDao.batchInsert(details);
			}
		}
		return result;
	}


	private void checkBillSettlement(List<BillSettlementWithDetail> settlementWithDetails) {
		if(settlementWithDetails == null || settlementWithDetails.size() == 0){
			logger.error("无相应结算单据信息");
			throw new BaseException("无相应结算单据信息");
		}
		for (BillSettlementWithDetail billSettlementWithDetail : settlementWithDetails) {
			BillSettlement billSettlement = billSettlementWithDetail.getTRSettle();
			String settleBillNo = billSettlement.getSettleBillNo();
			if (StringUtils.isEmpty(settleBillNo)) {
				logger.error("结算单结算单号不能为空");
				throw new BaseException("结算单结算单号不能为空");
			}
			Date settleDate = billSettlement.getSettleDate();
			if (settleDate == null) {
				logger.error("结算单结算单号:"+settleBillNo+"结算单结算单日期不能为空");
				throw new BaseException("结算单结算单号:"+settleBillNo+"结算单结算单日期不能为空");
			}
			List<BillSettlementDetail> details = billSettlement.getTRSettleDetail();
			if(details == null || details.size() == 0){
				logger.error("结算单结算单号:"+settleBillNo+"无相应结算单据明细信息");
				throw new BaseException("结算单结算单号:"+settleBillNo+"无相应结算单据明细信息");
			}
			String useBillNo = billSettlement.getUseBillNo();
			if (StringUtils.isEmpty(useBillNo)) {
				logger.error("结算单结算单号:"+settleBillNo+","+"结算单发药单据编号不能为空");
				throw new BaseException("结算单结算单号:"+settleBillNo+", "+"结算单明细发药单据编号不能为空");
			}
			for (BillSettlementDetail billSettlementDetail : details) {
				String detailSettleBillNo = billSettlementDetail.getSettleBillNo();
				if (StringUtils.isEmpty(detailSettleBillNo)) {
					logger.error("结算单结算单号:"+settleBillNo+"结算单明细结算单号不能为空");
					throw new BaseException("结算单结算单号:"+settleBillNo+"结算单明细结算单号不能为空");
				}
				int rowNo = billSettlementDetail.getRowNo();
				if (rowNo == 0) {
					logger.error("结算单结算单号:"+settleBillNo+"结算单明细行号不能为空");
					throw new BaseException("结算单结算单号:"+settleBillNo+"结算单明细行号不能为空");
				}
				String purchaser_GoodsCode = billSettlementDetail.getGoodsCode();
				if (StringUtils.isEmpty(purchaser_GoodsCode)) {
					logger.error("结算单结算单号:"+settleBillNo+",行号:"+rowNo+"结算单明细采购方_商品编码不能为空");
					throw new BaseException("结算单结算单号:"+settleBillNo+",行号:"+rowNo+"结算单明细采购方_商品编码不能为空");
				}
				String purchase_GoodsName = billSettlementDetail.getGoodsName();
				if (StringUtils.isEmpty(purchase_GoodsName)) {
					logger.error("结算单结算单号:"+settleBillNo+",行号:"+rowNo+"结算单明细采购方_商品名称不能为空");
					throw new BaseException("结算单结算单号:"+settleBillNo+",行号:"+rowNo+"结算单明细采购方_商品名称不能为空");
				}
				BigDecimal purchase_SettleQty = billSettlementDetail.getSettleQty();
				if (purchase_SettleQty == null) {
					logger.error("结算单结算单号:"+settleBillNo+",行号:"+rowNo+"结算单明细采购方_结算数量不能为空");
					throw new BaseException("结算单结算单号:"+settleBillNo+",行号:"+rowNo+"结算单明细采购方_结算数量不能为空");
				}
				String purchase_Unit = billSettlementDetail.getUnit();
				if (StringUtils.isEmpty(purchase_Unit)) {
					logger.error("结算单结算单号:"+settleBillNo+",行号:"+rowNo+"结算单明细采购方_包装单位不能为空");
					throw new BaseException("结算单结算单号:"+settleBillNo+",行号:"+rowNo+"结算单明细采购方_包装单位不能为空");
				}
				Date productionDate = billSettlementDetail.getProductionDate();
				if (productionDate == null) {
					logger.error("结算单结算单号:"+settleBillNo+",行号:"+rowNo+"结算单明细生产日期前不能为空");
					throw new BaseException("结算单结算单号:"+settleBillNo+",行号:"+rowNo+"结算单明细生产日期前不能为空");
				}
				Date expireDate = billSettlementDetail.getExpireDate();
				if (expireDate == null) {
					logger.error("结算单结算单号:"+settleBillNo+",行号:"+rowNo+"结算单明细有效日期不能为空");
					throw new BaseException("结算单结算单号:"+settleBillNo+",行号:"+rowNo+"结算单明细有效日期不能为空");
				}
			}
		}
		
	}

	
	
}