/***文档注释***********************************************
 * 作者               :                   龙文航
 * 创建日期      :                   2017.12.8
 * 描述               :                   物流管理-装车管理
 * 注意事项      :                   无
 * 遗留BUG :                   无
 * 修改日期      :                   
 * 修改人员      :                   
 * 修改内容      :                   
***********************************************************/
package market.Lms.Logistics.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import market.Com.vo.CommonPage;
import market.Com.vo.PassportInfo;
import market.Com.vo.ResultModel;
import market.Lms.Logistics.mapper.LmsPassportinfoMapper;
import market.Lms.Logistics.mapper.SpmDistrbuteMainMapper;
import market.Lms.Logistics.mapper.SpmTransportDetailMapper;
import market.Lms.Logistics.service.SpmTransportDetailService;
import market.Lms.Logistics.vo.SpmDistrbuteMain;
import market.Lms.Logistics.vo.SpmTransportDetail;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;

@SuppressWarnings("rawtypes")
@RemoteService(path="SpmTransportDetailService")
public class SpmTransportDetailServiceImpl extends BaseServiceImpl<SpmTransportDetail, Object> implements SpmTransportDetailService {

	/**
	 * 生成主键
	 * @return
	 * @throws Exception 
	 */
	public String createOutBillNo() throws Exception
	{
		try 
		{
			String outbillno="";
			List<HashMap> list = this.getMapper(SpmTransportDetailMapper.class).getNo("BL"+(new SimpleDateFormat("yyyyMMdd").format(new Date())));
			outbillno = (String) list.get(0).get("MAXNO");
			return outbillno;
		} 
		catch (Exception ex) 
		{
			throw (ex);
		}
	}
	
	/**
	 * 打印出厂证，统计边线件数
	 */
	public ResultModel doCoutBSqty(CommonPage<SpmTransportDetail> cp) {
		ResultModel rm = new ResultModel();
		try {
			List<SpmTransportDetail> objectList = cp.getObjectList();

			if (objectList == null || objectList.isEmpty()) {
				rm.setMsgInfo("请选择数据！");
				rm.setErrorInfo("请选择数据！");
				return rm;
			}

			SpmTransportDetail _spm = new SpmTransportDetail();
			Integer quantitysum = 0;
			
			for (SpmTransportDetail obj : objectList) {
				List<SpmTransportDetail> spm = this.getMapper(SpmTransportDetailMapper.class).doCoutBSqty(obj.getDistrbuteno());
				quantitysum += spm.get(0).getQuantity() == null ? 0 : spm.get(0).getQuantity();
			}

			_spm.setSum_quantity(quantitysum);

			rm.setState("200");
			rm.setData(_spm);
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败");
			e.printStackTrace();
		}
		return rm;
	}
	
	
	/**
	 * 装车验证
	 */
	
	public ResultModel doFind(CommonPage<SpmTransportDetail> cp) {
		ResultModel rm =new ResultModel();
		try {
			List<SpmTransportDetail> spm= this.getMapper(SpmTransportDetailMapper.class).doFindSpm(cp.getObject().getBillNo(), cp.getObject().getObjectno());
			if (spm.size()==0) {
				rm.setMsgInfo("该物料号不能装车");
				rm.setState("500");
				return rm;
			}
			rm.setState("200");
			rm.setData(spm);
		} catch (Exception e) {
			// TODO: handle exception
		}
		return rm;
	}
	
	
	/**
	 * 装车打印
	 */
	
	public ResultModel doSelect(CommonPage<SpmTransportDetail> cp) {
		ResultModel rm =new ResultModel();
		try {
			List<HashMap> spm= this.getMapper(SpmTransportDetailMapper.class).doSelect(cp.getObject().getOutbillno());
			
			rm.setState("200");
			rm.setData(spm);
		} catch (Exception e) {
			rm.setState("500");
		rm.setMsgInfo("查询失败");
		}
		return rm;
	}
	

	/**
	 * 装车打印
	 */
	public ResultModel doSelectB(CommonPage<SpmTransportDetail> cp) {
		ResultModel rm =new ResultModel();
		try {
			List<SpmTransportDetail> objectList = cp.getObjectList();
			
			if (objectList==null||objectList.isEmpty()) {
				rm.setMsgInfo("请选择数据！");
				return rm;
			}
			
			//检验是否为同一辆车
			int i=1;
			StringBuffer sb = new StringBuffer();
			List<String> outbillnoList= new ArrayList<>();
			for (SpmTransportDetail sp : objectList) {
				sb.append(sp.getOutbillno());
				if(i!=objectList.size()) {
					sb.append(",");
				}
				outbillnoList.add(sp.getOutbillno());
				++i;
			}
//			System.out.println(sb.toString());
			boolean b = this.getBean(SpmDistrbuteMainServiceImpl.class).doQueryCarno(sb.toString());
			if (!b) {
				rm.setMsgInfo("请您选择同一辆车的数据。");
				return rm;
			}
			//20210507 判断是否下发门禁系统
			for (SpmTransportDetail std : objectList) {
				SpmDistrbuteMain sdm = this.getMapper(SpmDistrbuteMainMapper.class).findById(std.getDistrbuteno());
				if (sdm!=null) {
					PassportInfo ppi = this.getMapper(LmsPassportinfoMapper.class).findById("Y"+sdm.getDeliveryno());
					if (ppi ==null || "0".equals(ppi.getIs_send())) {
						rm.setState("500");
						rm.setMsgInfo("数据还未下发门禁系统，请先下发");
						return rm;
					}
				}
			}
			
			
			List<SpmTransportDetail> _spm = new ArrayList<SpmTransportDetail>();
			String orderno=null;
			String ordernoHao=null;
			String reciver_address=null;
			String bill_no=null;
			String customerNm=null;
			
			//计算总的过磅扣重
			Double deduction = this.getMapper(SpmTransportDetailMapper.class).doQuerySumWgt_KZ(outbillnoList);
			deduction = new BigDecimal(deduction).setScale(3,BigDecimal.ROUND_HALF_UP).doubleValue();
			for (SpmTransportDetail obj : objectList) {
				
				List<SpmTransportDetail> spm2= this.getMapper(SpmTransportDetailMapper.class).doSelectB(obj.getOutbillno());
				
				//20200814 
				System.out.println(spm2);
				for(SpmTransportDetail s : spm2){
					if(s.getOrderno()!=null && !"".equals(s.getOrderno())){
						orderno=s.getOrderno();
						ordernoHao=s.getOrderno().substring(0, s.getOrderno().length()-6);
					}
					if(s.getReciver_address()!=null && !"".equals(s.getReciver_address())){
						reciver_address=s.getReciver_address();
					}
					if(s.getBillNo()!=null && !"".equals(s.getBillNo())){
						bill_no=s.getBillNo();
					}
					if(s.getCustomerNm()!=null && !"".equals(s.getCustomerNm())){
						customerNm=s.getCustomerNm();
					}
				}
				List<SpmTransportDetail> spm=this.getMapper(SpmTransportDetailMapper.class).doQueryWebBySQL1(obj.getDistrbuteno());
				Integer quantitysum=0;
				Double theoryweightsum=0d;
				for(SpmTransportDetail std:spm){
					quantitysum+=std.getQuantity()==null?0:std.getQuantity();
					theoryweightsum+=std.getTheoryweight()==null?0:std.getTheoryweight();
				}
				for(SpmTransportDetail std:spm){
					std.setTheoryweightsum(theoryweightsum);
					std.setSum_quantity(quantitysum);
					std.setOrderno(orderno);
					std.setOrdernoHao(ordernoHao);
					std.setReciver_address(reciver_address);
					std.setBillNo(bill_no);
					std.setCustomerNm(customerNm);
					std.setDeduction(deduction+"");//过磅扣重
					_spm.add(std);
				}			
			}
			rm.setState("200");
			rm.setData(_spm);
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败");
			e.printStackTrace();
		}
		return rm;
	}
	
	/**
	 * 装车打印
	 */
	public ResultModel doSelectBF(CommonPage<SpmTransportDetail> cp) {
		ResultModel rm =new ResultModel();
		try {
			List<SpmTransportDetail> objectList = cp.getObjectList();
			
			if (objectList==null||objectList.isEmpty()) {
				rm.setMsgInfo("请选择数据！");
				return rm;
			}
			
			//检验是否为同一辆车
			int i=1;
			StringBuffer sb = new StringBuffer();
			List<String> outbillnoList= new ArrayList<>();
			for (SpmTransportDetail sp : objectList) {
				sb.append(sp.getOutbillno());
				if(i!=objectList.size()) {
					sb.append(",");
				}
				outbillnoList.add(sp.getOutbillno());
				++i;
			}
//			System.out.println(sb.toString());
			boolean b = this.getBean(SpmDistrbuteMainServiceImpl.class).doQueryCarno(sb.toString());
			if (!b) {
				rm.setMsgInfo("请您选择同一辆车的数据。");
				return rm;
			}
			//20210507 判断是否下发门禁系统
			for (SpmTransportDetail std : objectList) {
				SpmDistrbuteMain sdm = this.getMapper(SpmDistrbuteMainMapper.class).findById(std.getDistrbuteno());
				if (sdm!=null) {
					PassportInfo ppi = this.getMapper(LmsPassportinfoMapper.class).findById("Y"+sdm.getDeliveryno());
					if (ppi ==null || "0".equals(ppi.getIs_send())) {
						rm.setState("500");
						rm.setMsgInfo("数据还未下发门禁系统，请先下发");
						return rm;
					}
				}
			}
			
			
			List<SpmTransportDetail> _spm = new ArrayList<SpmTransportDetail>();
			String orderno=null;
			String ordernoHao=null;
			String reciver_address=null;
			String bill_no=null;
			String customerNm=null;
			
			//计算总的过磅扣重
			Double deduction = this.getMapper(SpmTransportDetailMapper.class).doQuerySumWgt_KZ(outbillnoList);
			deduction = new BigDecimal(deduction).setScale(3,BigDecimal.ROUND_HALF_UP).doubleValue();
			for (SpmTransportDetail obj : objectList) {
				
				List<SpmTransportDetail> spm2= this.getMapper(SpmTransportDetailMapper.class).doSelectBF(obj.getOutbillno());
				
				//20200814 
//				System.out.println(spm2);
				for(SpmTransportDetail s : spm2){
					if(s.getOrderno()!=null && !"".equals(s.getOrderno())){
						orderno=s.getOrderno();
						ordernoHao=s.getOrderno().substring(0, s.getOrderno().length()-6);
					}
					if(s.getReciver_address()!=null && !"".equals(s.getReciver_address())){
						reciver_address=s.getReciver_address();
					}
					if(s.getBillNo()!=null && !"".equals(s.getBillNo())){
						bill_no=s.getBillNo();
					}
					if(s.getCustomerNm()!=null && !"".equals(s.getCustomerNm())){
						customerNm=s.getCustomerNm();
					}
				}
				List<SpmTransportDetail> spm=this.getMapper(SpmTransportDetailMapper.class).doQueryWebBySQL1F(obj.getDistrbuteno());
				Integer quantitysum=0;
				Double theoryweightsum=0d;
				for(SpmTransportDetail std:spm){
					quantitysum+=std.getQuantity()==null?0:std.getQuantity();
					theoryweightsum+=std.getTheoryweight()==null?0:std.getTheoryweight();
				}
				for(SpmTransportDetail std:spm){
					std.setTheoryweightsum(theoryweightsum);
					std.setSum_quantity(quantitysum);
					std.setOrderno(orderno);
					std.setOrdernoHao(ordernoHao);
					std.setReciver_address(reciver_address);
					std.setBillNo(bill_no);
					std.setCustomerNm(customerNm);
					std.setDeduction(deduction+"");//过磅扣重
					_spm.add(std);
				}			
			}
			rm.setState("200");
			rm.setData(_spm);
		} catch (Exception e) {
			rm.setState("500");
			rm.setMsgInfo("查询失败");
			e.printStackTrace();
		}
		return rm;
	}
	
}
