package com.fuzamei.web;

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

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fuzamei.constant.HintMSG;
import com.fuzamei.constant.Role;
import com.fuzamei.entity.Orders;
import com.fuzamei.entity.Params;
import com.fuzamei.service.CargoDeliverService;
import com.fuzamei.service.OrderService;
import com.fuzamei.service.UserAuthoricationService;
import com.fuzamei.util.JSONUtil;
import com.fuzamei.util.PageDTO;
import com.fuzamei.util.ReadConfUtil;
import com.fuzamei.util.ValidationUtil;

@RestController
@RequestMapping(path="/cargoDeliver")
public class CargoDeliverAction {

	@Autowired
	private CargoDeliverService cargoDeliverService;
	
	@Autowired
	private UserAuthoricationService userAuthoricationService;
	
	@Autowired
	private HttpServletRequest req;
	
	@Autowired
	private OrderService orderService;
	
	/**
	 * 
	* @Title: queryOrdersByCarrier
	* @Description: TODO(承运商查看订单信息)
	{
		"tokenId":"xxx",
		"page":"1",
		"orderId":"",
		"carNo":"",
		"statusId":"",
		"startTime":"",
		"endTime":""
	}
	* @Paramss @return    设定文件
	* @return Map<String,Object>    返回类型
	* @author ylx
	* @date 2018年1月26日 下午1:51:37
	* @throws
	 */
	@RequestMapping(value="/queryOrdersByCarrier",method=RequestMethod.POST)
	public Map<String, Object> queryOrdersByCarrier(@RequestBody Params params){
		try {
			String userId = req.getHeader("Authorization").split("&")[1];
			userAuthoricationService.queryUserAuthority(ValidationUtil.checkAndAssignInt(userId), Role.CARRIER);
			params.setUserId(Integer.parseInt(userId));
			int page = ValidationUtil.checkMinAndAssignInt(params.getPage(), 1);
			int rowNum = ValidationUtil.checkMinAndAssignInt(params.getRowNum(), 1);
			Long startTime=ValidationUtil.checkAndAssignDefaultLong(params.getStartTime(), 0L);
			Long endTime=ValidationUtil.checkAndAssignDefaultLong(params.getEndTime(), Long.MAX_VALUE);
			params.setStartTime(startTime);
			if(startTime<=endTime) params.setEndTime(endTime);
			else params.setEndTime(Long.MAX_VALUE);
			params.setStartPage((page - 1) * rowNum);
			params.setRowNum(rowNum);
			PageDTO pageDto = cargoDeliverService.queryOrdersByCarrier(params);
			return JSONUtil.getJsonMap(200, true, HintMSG.QUERY_SUCCESS, pageDto);
		} catch (Exception e) {
			return JSONUtil.getJsonMap(500, false, HintMSG.QUERY_FAIL+":"+e.getMessage(), null);
		}
	}
	
	/**
	 * 
	* @Title: orderTracking
	* @Description: TODO(承运商查看订单跟踪表)
	{
		"tokenId":"",
		"orderId":""
	}
	* @Paramss @return    设定文件
	* @return Map<String,Object>    返回类型
	* @author ylx
	* @date 2018年1月26日 下午2:07:25
	* @throws
	 */
	@RequestMapping(value="/orderTracking",method=RequestMethod.POST)
	public Map<String, Object> orderTracking(@RequestBody Params params){
		try {
			String userId = req.getHeader("Authorization").split("&")[1];
			userAuthoricationService.queryUserAuthority(ValidationUtil.checkAndAssignInt(userId),Role.CARRIER);//检测当前操作用户权限
			params.setUserId(Integer.parseInt(userId));
			Orders order = orderService.queryFullOrderByOrderId(ValidationUtil.checkAndAssignInt(params.getOrderId()));
			if(!params.getUserId().equals(order.getCarrierId())) throw new RuntimeException("无权查看");
			params.setOrder(order);
			List<Object> list = cargoDeliverService.orderTracking(params);
			return JSONUtil.getJsonMap(200, true, HintMSG.QUERY_SUCCESS, list);
		} catch (Exception e) {
			return JSONUtil.getJsonMap(500, false, HintMSG.QUERY_FAIL+":"+e.getMessage(), null);
		}
	}
	
}
