cec59e663bedbf9bc29554d7bfa28de351f41e24.svn-base 6.5 KB
package com.phxl.modules.goods.service.suoutstore;

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

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.phxl.common.utils.BaseException;
import com.phxl.common.utils.IdUtil;
import com.phxl.modules.goods.dao.suoutstore.SuOutStoreDao;
import com.phxl.modules.goods.dao.suoutstore.SuOutStoreDetailDao;
import com.phxl.modules.goods.entity.suoutstore.SuOutStore;
import com.phxl.modules.goods.entity.suoutstore.SuOutStoreDetail;
import com.phxl.modules.goods.entity.suoutstore.SuOutStoreWithDetail;

@Service
public class SuOutStoreService {

	@Autowired
	private SuOutStoreDao suOutStoreDao;
	
	@Autowired
	private SuOutStoreDetailDao suOutStoreDetailDao;
	
	/**
	 * 日志对象
	 */
	protected Logger logger = LoggerFactory.getLogger(getClass());
	
	
	public boolean batchSave(List<SuOutStoreWithDetail> suOutStoreWithDetailList) {
		boolean result = true;
		try {
			checkSuOutStore(suOutStoreWithDetailList);
		} catch (Exception e) {
			throw new BaseException("前置机:"+e.getMessage());
		}
		for (SuOutStoreWithDetail suOutStore : suOutStoreWithDetailList) {
			List<SuOutStoreDetail> details = suOutStore.getBill().getBilldetaillist();
			String id = IdUtil.uuid();
			SuOutStore su = suOutStore.getBill();
			su.setId(id);
			if(details != null && details.size() != 0){
				for (SuOutStoreDetail suOutStoreDetail : details) {
					suOutStoreDetail.setBillNo(suOutStore.getBill().getBillNo());
					suOutStoreDetail.setId(id);
				}
				suOutStoreDao.insert(su);
				suOutStoreDetailDao.batchInsert(details);
			}
		}
		return result;
	}


	private void checkSuOutStore(List<SuOutStoreWithDetail> suOutStoreWithDetailsList) {
		if(suOutStoreWithDetailsList == null || suOutStoreWithDetailsList.size() == 0){
			logger.error("前置机:无相应供应商出库单据信息");
			throw new BaseException("前置机:无相应供应商出库单据信息");
		}
		for (SuOutStoreWithDetail suOutStoreWithDetails : suOutStoreWithDetailsList) {
			String billNo = suOutStoreWithDetails.getBill().getBillNo();
			if (StringUtils.isEmpty(billNo)) {
				logger.error("前置机:供应商出库单单据编号不能为空");
				throw new BaseException("前置机:供应商出库单单据编号不能为空");
			}
			String medGuid = suOutStoreWithDetails.getBill().getMedGuid();
			String medName = suOutStoreWithDetails.getBill().getMedName();
			if (StringUtils.isEmpty(medGuid) || StringUtils.isEmpty(medName)) {
				logger.error("前置机:供应商出库单单据编号:"+billNo+"医疗机构代码以及医疗机构名称不能为空");
				throw new BaseException("前置机:供应商出库单单据编号:"+billNo+"医疗机构代码以及医疗机构名称不能为空");
			}
			List<SuOutStoreDetail> details = suOutStoreWithDetails.getBill().getBilldetaillist();
			if(details == null || details.size() == 0){
				logger.error("前置机:供应商出库单单据编号:"+billNo+"出库单单据明细不能为空");
				throw new BaseException("前置机:供应商出库单单据编号:"+billNo+"出库单单据明细不能为空");
			}
			for (SuOutStoreDetail suOutStoreDetail : details) {
				String detailBillNo = suOutStoreDetail.getBillNo();
				if (StringUtils.isEmpty(detailBillNo)) {
					logger.error("前置机:供应商出库单单据编号:"+billNo+"出库单单据明细单据编号不能为空");
					throw new BaseException("前置机:供应商出库单单据编号:"+billNo+"出库单单据明细单据编号不能为空");
				}
				int rowNo = suOutStoreDetail.getRowNo();
				if (rowNo == 0) {
					logger.error("前置机:供应商出库单单据编号:"+billNo+"出库单单据明细行号不能为空");
					throw new BaseException("前置机:供应商出库单单据编号:"+billNo+"出库单单据明细行号不能为空");
				}
				int medPlanRowNo = suOutStoreDetail.getMedPlanRowNo();
				if (medPlanRowNo == 0) {
					logger.error("前置机:供应商出库单单据编号:"+billNo+"出库单单据明细补货计划单据原行号不能为空");
					throw new BaseException("前置机:供应商出库单单据编号:"+billNo+"出库单单据明细补货计划单据原行号不能为空");
				}
				String goodsGuid = suOutStoreDetail.getGoodsGuid();
				String goodsName = suOutStoreDetail.getGoodsName();
				if (StringUtils.isEmpty(goodsGuid) || StringUtils.isEmpty(goodsName)) {
					logger.error("前置机:供应商出库单单据编号:"+billNo+",行号:"+rowNo+"出库单单据明细商品编码以及商品名称不能为空");
					throw new BaseException("前置机:供应商出库单单据编号:"+billNo+",行号:"+rowNo+"出库单单据明细商品编码以及商品名称不能为空");
				}
				String goodsSpec = suOutStoreDetail.getGoodsSpec();
				if (StringUtils.isEmpty(goodsSpec)) {
					logger.error("前置机:供应商出库单单据编号:"+billNo+",行号:"+rowNo+"出库单单据明细商品规格不能为空");
					throw new BaseException("前置机:供应商出库单单据编号:"+billNo+",行号:"+rowNo+"出库单单据明细商品规格不能为空");
				}
				String lot  = suOutStoreDetail.getLot();
				if (StringUtils.isEmpty(lot)) {
					logger.error("前置机:供应商出库单单据编号:"+billNo+",行号:"+rowNo+"出库单单据明细批号不能为空");
					throw new BaseException("前置机:供应商出库单单据编号:"+billNo+",行号:"+rowNo+"出库单单据明细批号不能为空");
				}
				Date productionDate  = suOutStoreDetail.getProductionDate();
				Date expireDate  = suOutStoreDetail.getExpireDate();
				if (productionDate == null || expireDate == null) {
					logger.error("前置机:供应商出库单单据编号:"+billNo+",行号:"+rowNo+"出库单单据明细生产日期以及有效期不能为空");
					throw new BaseException("前置机:供应商出库单单据编号:"+billNo+",行号:"+rowNo+"出库单单据明细生产日期以及有效期不能为空");
				}
				BigDecimal arrivalqty  = suOutStoreDetail.getArrivalqty();
				if (arrivalqty == null) {
					logger.error("前置机:供应商出库单单据编号:"+billNo+",行号:"+rowNo+"出库单单据明细出库数不能为空");
					throw new BaseException("前置机:供应商出库单单据编号:"+billNo+",行号:"+rowNo+"出库单单据明细出库数不能为空");
				}
			}
			
		}
		
	}
	
}