package com.fuzamei.web;

import java.util.Map;

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.Param;
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 OrderService orderService;
	
	private static final Integer ROWNUM=Integer.parseInt(ReadConfUtil.getProperty("rowNum"));
	
	/**
	 * 
	* @Title: queryOrdersByCarrier
	* @Description: TODO(承运商查看订单信息)
	{
		"userId":"xxx",
		"page":"1",
		"orderId":"",
		"carNo":"",
		"statusId":"",
		"startTime":"",
		"endTime":""
	}
	* @param @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 Param param){
		try {
			userAuthoricationService.queryUserAuthority(ValidationUtil.checkAndAssignInt(param.getUserId()), Role.SUPPLIER);
			int page = ValidationUtil.checkMinAndAssignInt(param.getPage(), 1);
			Long startTime=ValidationUtil.checkAndAssignDefaultLong(param.getStartTime(), 0L);
			Long endTime=ValidationUtil.checkAndAssignDefaultLong(param.getEndTime(), Long.MAX_VALUE);
			param.setStartTime(startTime);
			if(startTime<=endTime) param.setEndTime(endTime);
			else param.setEndTime(Long.MAX_VALUE);
			param.setStartPage((page - 1) * ROWNUM);
			param.setRowNum(ROWNUM);
			PageDTO pageDto = cargoDeliverService.queryOrdersByCarrier(param);
			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(承运商查看订单跟踪表)
	{
		"userId":"",
		"orderId":""
	}
	* @param @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 Param param){
		try {
			userAuthoricationService.queryUserAuthority(ValidationUtil.checkAndAssignInt(param.getUserId()),Role.CARRIER);//检测当前操作用户权限
			Orders order = orderService.queryFullOrderByOrderId(ValidationUtil.checkAndAssignInt(param.getOrderId()));
			if(!param.getUserId().equals(order.getCarrierId())) throw new RuntimeException("无权查看");
			param.setOrder(order);
			Map<String, Object> map = cargoDeliverService.orderTracking(param);
			return JSONUtil.getJsonMap(200, true, HintMSG.QUERY_SUCCESS, map);
		} catch (Exception e) {
			return JSONUtil.getJsonMap(500, false, HintMSG.QUERY_FAIL+":"+e.getMessage(), null);
		}
	}
	
}
