6dd9d3b6ea6fe46d0b5113d5e60f1885477178b7.svn-base 6.73 KB
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+"结算单明细有效日期不能为空");
				}
			}
		}
		
	}

	
	
}