23d88654e4083525c0c9867dbe77747121ed6071.svn-base 6.73 KB
package com.phxl.modules.goods.service.medinstore;

import java.math.BigDecimal;
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.medinstore.MedInStoreDao;
import com.phxl.modules.goods.dao.medinstore.MedInStoreDetailDao;
import com.phxl.modules.goods.entity.medinstore.MedInStore;
import com.phxl.modules.goods.entity.medinstore.MedInStoreDetail;
import com.phxl.modules.goods.entity.medinstore.MedInStoreWithDetail;

@Service
public class MedInStoreService {
	@Autowired
	private MedInStoreDao inStoreDao;
	
	@Autowired
	private MedInStoreDetailDao inStoreDetailDao;
	
	/**
	 * 日志对象
	 */
	protected Logger logger = LoggerFactory.getLogger(getClass());

	
	private void checkMedInStore(List<MedInStoreWithDetail> instores) {
		if(instores == null || instores.size() == 0){
			logger.error("无相应入库单单据信息");
			throw new BaseException("无相应入库单单据信息");
		}
		for (MedInStoreWithDetail medinstore : instores) {
			String medCode = medinstore.getBill().getMedGuid();
			String medName = medinstore.getBill().getMedName();
			if (StringUtils.isEmpty(medCode) || StringUtils.isEmpty(medName)) {
				logger.error("入库单医疗机构编码以及医疗机构名称不能为空");
				throw new BaseException("入库单医疗机构编码以及医疗机构名称不能为空");
			}
			String supplierCode = medinstore.getBill().getSupplierGuid();
			String suppliername = medinstore.getBill().getSupplierName();
			if (StringUtils.isEmpty(supplierCode) || StringUtils.isEmpty(suppliername)) {
				logger.error("入库单平台供应商代码以及平台供应商名称不能为空");
				throw new BaseException("入库单平台供应商代码以及平台供应商名称不能为空");
			}
			String billNo = medinstore.getBill().getBillNo();
			if (StringUtils.isEmpty(billNo)) {
				logger.error("入库单单据编号不能为空");
				throw new BaseException("入库单单据编号不能为空");
			}
			String billType = medinstore.getBill().getBillType();
			if (StringUtils.isEmpty(billType)) {
				logger.error("入库单单据类型不能为空");
				throw new BaseException("入库单单据类型不能为空");
			}
			List<MedInStoreDetail> details = medinstore.getBill().getBilldetaillist();
			if(details == null || details.size() == 0){
				logger.error("入库单单据编号:"+billNo+"入库明细不能为空");
				throw new BaseException("入库单单据编号:"+billNo+"入库明细不能为空");
			}
			for (MedInStoreDetail medInStoreDetail : details) {
				String detailBillNo = medInStoreDetail.getBillNo();
				if (StringUtils.isEmpty(detailBillNo)) {
					logger.error("入库单单据编号:"+billNo+"入库明细单据编号不能为空");
					throw new BaseException("入库单单据编号:"+billNo+"入库明细单据编号不能为空");
				}
				String goodsCode = medInStoreDetail.getGoodsCode();
				String goodsName = medInStoreDetail.getGoodsName();
				if (StringUtils.isEmpty(goodsCode) || StringUtils.isEmpty(goodsName)) {
					logger.error("入库单单据编号:"+billNo+"入库明细商品编码以及商品名称不能为空");
					throw new BaseException("入库单单据编号:"+billNo+"入库明细商品编码以及商品名称不能为空");
				}
				BigDecimal mvQty = medInStoreDetail.getMvqty();
				if (mvQty == null) {
					logger.error("入库单单据编号:"+billNo+"入库明细商品入库实收数不能为空");
					throw new BaseException("入库单单据编号:"+billNo+"入库明细商品入库实收数不能为空");
				}
				BigDecimal arrivalQty = medInStoreDetail.getArrivalqty();
				if (arrivalQty == null) {
					logger.error("入库单单据编号:"+billNo+"入库明细商品出库数量不能为空");
					throw new BaseException("补货计划单据编号:"+billNo+"入库明细商品出库数量不能为空");
				}
				BigDecimal amount = medInStoreDetail.getAmount();
				if (amount == null) {
					logger.error("入库单单据编号:"+billNo+"入库明细商品金额不能为空");
					throw new BaseException("入库单单据编号:"+billNo+"入库明细商品金额不能为空");
				}
				String unit = medInStoreDetail.getUnit();
				if (StringUtils.isEmpty(unit)) {
					logger.error("入库单单据编号:"+billNo+"入库明细商品单位不能为空");
					throw new BaseException("入库单单据编号:"+billNo+"入库明细商品单位不能为空");
				}
				BigDecimal purchaserPrice = medInStoreDetail.getPurchaserPrice();
				if (purchaserPrice == null) {
					logger.error("入库单单据编号:"+billNo+"入库明细商品采购价格不能为空");
					throw new BaseException("入库单单据编号:"+billNo+"入库明细商品采购价格不能为空");
				}
				String medPlanRowNo = medInStoreDetail.getMedPlanRowNo();
				if (StringUtils.isEmpty(medPlanRowNo)) {
					logger.error("入库单单据编号:"+billNo+"入库明细补货计划单据原行号不能为空");
					throw new BaseException("入库单单据编号:"+billNo+"入库明细补货计划单据原行号不能为空");
				}
				String supplierDoNo = medInStoreDetail.getSupplierDoNo();
				if (StringUtils.isEmpty(supplierDoNo)) {
					logger.error("入库单单据编号:"+billNo+"入库明细供应商出库单号不能为空");
					throw new BaseException("入库单单据编号:"+billNo+"入库明细供应商出库单号不能为空");
				}
				String supplierDoRowNo = medInStoreDetail.getSupplierDoRowNo();
				if (StringUtils.isEmpty(supplierDoRowNo)) {
					logger.error("入库单单据编号:"+billNo+"入库明细供应商出库单行号不能为空");
					throw new BaseException("入库单单据编号:"+billNo+"入库明细供应商出库单行号不能为空");
				}
			}
		}
		
		
	}


	public boolean batchSave(List<MedInStoreWithDetail> instores) {
		
		boolean result = true;
		try {
			checkMedInStore(instores);
		} catch (Exception e) {
			throw new BaseException(e.getMessage());
		}
		for (MedInStoreWithDetail medInStoreWithDetail : instores) {
			MedInStore medinstore = medInStoreWithDetail.getBill();
			List<MedInStoreDetail> details = medinstore.getBilldetaillist();
			String id = IdUtil.uuid();
			medInStoreWithDetail.getBill().setId(id);
			if(details != null && details.size() != 0){
				for (MedInStoreDetail medinstoredetail : details) {
					medinstoredetail.setBillNo(medInStoreWithDetail.getBill().getBillNo());
					medinstoredetail.setId(id);
				}
				inStoreDao.insert(medinstore);
				inStoreDetailDao.batchInsert(details);
			}
		}
		return result;
	}
	


}