package com.fuzamei.service.serviceImpl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fuzamei.constant.OperationType;
import com.fuzamei.constant.Status;
import com.fuzamei.entity.IssueOrder;
import com.fuzamei.entity.Orders;
import com.fuzamei.entity.Param;
import com.fuzamei.entity.SendOrder;
import com.fuzamei.mapperInterface.CargoConsignMapper;
import com.fuzamei.mapperInterface.OperationHistoryMapper;
import com.fuzamei.service.CargoConsignService;
import com.fuzamei.util.PageDTO;

@Service
public class CargoConsignServiceImpl implements CargoConsignService {

	@Autowired
	private CargoConsignMapper cargoConsignMapper;
	
	@Autowired
	private OperationHistoryMapper operationHistoryMapper;

	@Override
	public PageDTO queryOrdersBySupplier(Param param) {
		PageDTO pageDto=new PageDTO();
		List<SendOrder> orderList = cargoConsignMapper.queryOrdersBySupplier(param);
		int count = cargoConsignMapper.findQueryCountBySupplier(param);
		pageDto.setRows(orderList);
		pageDto.setTotal(count);
		return pageDto;
	}

	@Override
	public PageDTO queryOrdersByCarrier(Param param) {
		PageDTO pageDto=new PageDTO();
		List<SendOrder> orderList = cargoConsignMapper.queryOrdersByCarrier(param);
		int count = cargoConsignMapper.findQueryCountByCarrier(param);
		pageDto.setRows(orderList);
		pageDto.setTotal(count);
		return pageDto;
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public void doCarry(Param param) {
		Long currentTime = System.currentTimeMillis();
		//更新【订单表】
		param.setTransportTime(currentTime);
		param.setUpdateTime(currentTime);
		param.setStatusId(Status.DELIVERING);
		cargoConsignMapper.doCarry(param);
		//插入【操作记录】
		param.setOperationTypeId(OperationType.DELIVER);
		param.setOperatorId(param.getUserId());
		param.setOperationTime(currentTime);
		operationHistoryMapper.addToHistory(param);
	}

	@Override
	public Map<String, Object> orderTracking(Param param) {
		Orders order = param.getOrder();
		return OrdersIssueServiceImpl.dispatchOrderInfo(order);
	}
	
	
}
