package market.Slm.DeliveryManagement.impl;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import market.Com.pb.DoubleUtil;
import market.Com.vo.CommonPage;
import market.Com.vo.ResultModel;
import market.Slm.DeliveryManagement.mapper.SlmDeliveryPlanLineMapper;
import market.Slm.DeliveryManagement.service.SlmDeliveryPlanLineService;
import market.Slm.DeliveryManagement.vo.SlmDeliveryPlanHeat;
import market.Slm.DeliveryManagement.vo.SlmDeliveryPlanLine;
import market.Slm.DeliveryManagement.vo.SlmLadingBillLine;
import market.Slm.OrderManagement.impl.SlmOrderInfoServiceImpl;
import market.Slm.OrderManagement.vo.SlmOrderInfo;

import org.springframework.transaction.annotation.Transactional;

import com.hnshituo.basic.service.impl.BaseServiceImpl;
import com.hnshituo.basic.service.support.PageModel;
import com.hnshituo.basic.spring.remoting.annotation.RemoteService;
import com.hnshituo.core.auth.service.HomeService;

@RemoteService(path="SlmDeliveryPlanLineService")
public class SlmDeliveryPlanLineServiceImpl extends BaseServiceImpl<SlmDeliveryPlanLine,Object> implements SlmDeliveryPlanLineService {

	/**
	 * 查询行
	 * */
	public ResultModel doQueryWeb(CommonPage<SlmDeliveryPlanLine> sum){
		ResultModel rm=new ResultModel();
		SlmDeliveryPlanLine line=sum.getObject();
		int pageIndex=sum.getPageIndex();
		int pageSize=sum.getPageSize();
		PageModel<SlmDeliveryPlanLine> pm=new PageModel<SlmDeliveryPlanLine>(pageIndex, pageSize);
		try{
			List<SlmDeliveryPlanLine> list=this.getMapper(SlmDeliveryPlanLineMapper.class).findWithCondition(line,null, "create_time desc,valid_flag desc", (pageIndex-1)*pageSize,pageSize);
			int count=this.getMapper(SlmDeliveryPlanLineMapper.class).countWithCondition(line, null);
			list=countWeight(list);
			pm.setRows(list);
			pm.setTotal(count);
			rm.setState("200");
			rm.setData(pm);
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo(ex.getMessage());
			ex.printStackTrace();
		}
		return rm;
	}

	/**
	 * 发运计划行新增
	 * @throws Exception 
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doAddWeb(CommonPage<SlmDeliveryPlanLine> sum) throws Exception{
		ResultModel rm=new ResultModel();
		String userId = (String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName = (String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		SlmDeliveryPlanLine line=sum.getObject();
		try{
			int count=this.getMapper(SlmDeliveryPlanLineMapper.class).countWithCondition(null," PLAN_NO like concat('"+line.getPlan_no()+"','%')")+1;
			line.setLine_no(line.getPlan_no()+String.format("%03d",count));
			line.setCreate_man_no(userId);
			line.setCreate_man_name(userName);
			line.setCreate_time(new Date());
			line.setValid_flag("1");
			this.getMapper(SlmDeliveryPlanLineMapper.class).insert(line);
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("新增失败"+ex.getMessage());
			ex.printStackTrace();
			//--19-10-15添加异常回滚
			throw new Exception(rm.getMsgInfo());
		}
		return rm;
	}

	/**
	 * 发运单编辑
	 * @throws Exception 
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doUpdateWeb(CommonPage<SlmDeliveryPlanLine> sum) throws Exception{
		ResultModel rm=new ResultModel();
		String userId=(String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		SlmDeliveryPlanLine line=sum.getObject();
		try{
			//判断何种状态可以进行编辑
			SlmDeliveryPlanHeat heat=this.getBean(SlmDeliveryPlanHeatServiceImpl.class).doQueryOne(line.getPlan_no());
			if(!"1".equals(heat.getPlan_status())){		//只有编辑状态才可修改
				rm.setMsgInfo("当前状态无法编辑");
				rm.setState("500");
				return rm;
			}
			line.setUpdate_man_no(userId);
			line.setUpdate_man_name(userName);
			line.setUpdate_time(new Date());
			this.getMapper(SlmDeliveryPlanLineMapper.class).update(line);
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo("编辑失败"+ex.getMessage());
			ex.printStackTrace();
			//--19-10-15添加异常回滚
			throw new Exception(rm.getMsgInfo());
		}
		return rm;
	}

	/**
	 * 发运单启用停用
	 * @throws Exception 
	 * */
	@Transactional(value="tm", rollbackFor=Exception.class)
	public ResultModel doSwitchWeb(CommonPage<SlmDeliveryPlanLine> sum) throws Exception{
		ResultModel rm=new ResultModel();
		String userId=(String) this.getSession().getAttribute(HomeService.SESSION_USER_ID);
		String userName=(String) this.getSession().getAttribute(HomeService.SESSION_USER_NAME);
		SlmDeliveryPlanLine line=sum.getObject();
		String state="1".equals(line.getValid_flag())?"启用":"停用";
		try{
			//判断何种状态可以进行启用停用
			SlmDeliveryPlanHeat heat=this.getBean(SlmDeliveryPlanHeatServiceImpl.class).doQueryOne(line.getPlan_no());
			if(!"1".equals(heat.getPlan_status())){		//只有编辑状态才可修改
				rm.setMsgInfo("当前状态无法"+state);
				rm.setState("500");
				return rm;
			}
			if("0".equals(line.getValid_flag())){		//停用
				line.setDelete_man_no(userId);
				line.setDelete_man_name(userName);
				line.setDelete_time(new Date());
				this.getMapper(SlmDeliveryPlanLineMapper.class).update(line);
			}else{			//启用
				line.setUpdate_man_no(userId);
				line.setUpdate_man_name(userName);
				line.setUpdate_time(new Date());
				this.getMapper(SlmDeliveryPlanLineMapper.class).doEnable(line);
			}
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo(state+"失败"+ex.getMessage());
			ex.printStackTrace();
			//--19-10-15添加异常回滚
			throw new Exception(rm.getMsgInfo());
		}
		return rm;
	}

	/**
	 * 详情
	 * */
	public ResultModel doQueryDetailWeb(CommonPage<SlmDeliveryPlanLine> sum){
		ResultModel rm=new ResultModel();
		SlmDeliveryPlanLine line=sum.getObject();
		try{
			line=this.getMapper(SlmDeliveryPlanLineMapper.class).findById(line.getLine_no());
			List<SlmDeliveryPlanLine> list=new ArrayList<SlmDeliveryPlanLine>();
			list.add(line);
			list=countWeight(list);
			line=list.get(0);			
			rm.setData(line);
			rm.setState("200");
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo(ex.getMessage());
			ex.printStackTrace();
		}
		return rm;
	}

	/**
	 * 通过订单查询计划行
	 * */
	public ResultModel doQueryByOrderWeb(CommonPage<SlmDeliveryPlanLine> sum){
		ResultModel rm=new ResultModel();
		SlmDeliveryPlanLine line=sum.getObject();
		int pageIndex=sum.getPageIndex();
		int pageSize=sum.getPageSize();
		PageModel<SlmDeliveryPlanLine> pm=new PageModel<SlmDeliveryPlanLine>(pageIndex, pageSize);
		try{
			String order_no=line.getOrder_no();
			if(order_no!=null&&order_no.length()>6){
				String contract_no=order_no.substring(0,order_no.length()-6);
				String line_seq=order_no.substring(order_no.length()-6,order_no.length()-3);
				String order_seq=order_no.substring(order_no.length()-3);
				line.setContract_no(contract_no);
				line.setContract_line_seq(line_seq);
				line.setOrder_seq(order_seq);
				line.setOrder_no(null);
			}
			List<SlmDeliveryPlanLine> list=this.getMapper(SlmDeliveryPlanLineMapper.class).doQueryByOrder(line, (pageIndex-1)*pageSize,pageIndex*pageSize);
			int count=this.getMapper(SlmDeliveryPlanLineMapper.class).doQueryByOrderCount(line);
			for(SlmDeliveryPlanLine lines:list){
				String orderNo=lines.getContract_no()+lines.getContract_line_seq()+lines.getOrder_seq();
				lines.setOrder_no(orderNo);
			}
			pm.setRows(list);
			pm.setTotal(count);
			rm.setState("200");
			rm.setData(pm);
		}catch(Exception ex){
			rm.setState("500");
			rm.setMsgInfo(ex.getMessage());
			ex.printStackTrace();
		}
		return rm;
	}

	/**
	 * 通过提单行查询计划行
	 * */
	//	public ResultModel doQueryByBillLineWeb(CommonPage<SlmDeliveryPlanLine> sum){
	//		ResultModel rm=new ResultModel();
	//		String bill_no=sum.getMemo1();		//提单号
	//		try{
	//			SlmLadingBillHeat bill=this.getBean(SlmLadingBillHeatServiceImpl.class).doQueryOne(bill_no);
	//			SlmDeliveryPlanLine line=new SlmDeliveryPlanLine();
	//			line.setPlan_no(bill.getPlan_no());
	//			List<SlmDeliveryPlanLine> list=this.getMapper(SlmDeliveryPlanLineMapper.class).find(line,0,0);
	//			rm.setState("200");
	//			rm.setData(list);
	//		}catch(Exception ex){
	//			rm.setState("500");
	//			rm.setMsgInfo(ex.getMessage());
	//			ex.printStackTrace();
	//		}
	//		return rm;
	//	}

	/**
	 * 查询可填写的重量上限
	 * */
	public ResultModel doQueryWeightMax(CommonPage<SlmDeliveryPlanLine> sum){
		ResultModel rm=new ResultModel();
		SlmDeliveryPlanLine line=sum.getObject();
		SlmOrderInfo order=this.getBean(SlmOrderInfoServiceImpl.class).doQueryOne(line.getOrder_no());
		List<SlmDeliveryPlanLine> lineList=this.getMapper(SlmDeliveryPlanLineMapper.class).doQueryByOrder(line,0,0);
		Double sum_weight=0d;
		Double sum_count=0d;
		for(SlmDeliveryPlanLine lines:lineList){
			SlmLadingBillLine billLine=new SlmLadingBillLine();
			billLine.setContract_no(lines.getContract_no());
			billLine.setContract_line_seq(lines.getContract_line_seq());
			billLine.setOrder_seq(lines.getOrder_seq());
			List<SlmLadingBillLine> billList=this.getBean(SlmLadingBillLineServiceImpl.class).doQuery(billLine);
			for(SlmLadingBillLine bills:billList){
				sum_weight+=bills.getActual_weight();
				sum_count+=bills.getActual_quantity();
			}
		}
		if(order.getEstimate_weight()!=null)
			line.setSend_weight(DoubleUtil.round(order.getEstimate_weight()-sum_weight,DoubleUtil.DEF_DIV_SCALE));
		if(order.getOrder_quantity()!=null)
			line.setSend_count(DoubleUtil.round(order.getOrder_quantity()-sum_count,DoubleUtil.DEF_DIV_SCALE));
		rm.setState("200");
		rm.setData(line);
		return rm;
	}

	public List<SlmDeliveryPlanLine> doQuery(SlmDeliveryPlanLine line) {
		return this.getMapper(SlmDeliveryPlanLineMapper.class).find(line,0,0);
	}

	public void doUpdate(SlmDeliveryPlanLine line) {
		this.getMapper(SlmDeliveryPlanLineMapper.class).update(line);
	}

	//计算该子计划下的所有提单的已发量，已发数量之和
	private List<SlmDeliveryPlanLine> countWeight(List<SlmDeliveryPlanLine> list){
		for(SlmDeliveryPlanLine lines:list){
			SlmLadingBillLine billLine=new SlmLadingBillLine();
			billLine.setContract_no(lines.getContract_no());
			billLine.setContract_line_seq(lines.getContract_line_seq());
			billLine.setOrder_seq(lines.getOrder_seq());
			List<SlmLadingBillLine> billList=this.getBean(SlmLadingBillLineServiceImpl.class).doQuery(billLine);
			Double total_weight=0d;
			Double total_sum=0d;
			for(SlmLadingBillLine bills:billList){
				total_weight+=bills.getActual_weight()==null?0:bills.getActual_weight();
				total_sum+=bills.getActual_quantity()==null?0:bills.getActual_quantity();
			}
			lines.setSend_weight(DoubleUtil.round(total_weight,DoubleUtil.DEF_DIV_SCALE));
			lines.setSend_count(DoubleUtil.round(total_sum,DoubleUtil.DEF_DIV_SCALE));
			//订单号修改
			String order_no=lines.getContract_no()+lines.getContract_line_seq()+lines.getOrder_seq();
			lines.setOrder_no(order_no);
		}
		return list;
	}
	
	public Double doubleTest(Double a,Double b){
		Double total=a-b;
//		total=DoubleUtil.round(total, 4);
		System.out.println(total);
		return total;
	}
}
