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.constant.Status;
import com.fuzamei.entity.Orders;
import com.fuzamei.entity.Param;
import com.fuzamei.entity.TallyOrder;
import com.fuzamei.service.CargoTallyService;
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="/cargoTally")
public class CargoTallyAction {

	@Autowired
	private CargoTallyService cargoTallyService;
	
	@Autowired
	private UserAuthoricationService userAuthoricationService;
	
	@Autowired
	private OrderService orderService;
	
	private static final Integer ROWNUM=Integer.parseInt(ReadConfUtil.getProperty("rowNum"));
	
	/**
	 * 
	* @Title: queryOrdersByReceiver
	* @Description: TODO(收货员查询订单信息)
	{
		"userId":"xxx",
		"page":"1",
		"orderId":"",
		"carNo":"",
		"startTime":"",
		"endTime":""
	}
	* @param @return    设定文件
	* @return Map<String,Object>    返回类型
	* @author ylx
	* @date 2018年1月26日 下午2:12:02
	* @throws
	 */
	@RequestMapping(value="/queryOrdersByReceiver",method=RequestMethod.POST)
	public Map<String, Object> queryOrdersByReceiver(@RequestBody Param param){
		try {
			userAuthoricationService.queryUserAuthority(ValidationUtil.checkAndAssignInt(param.getUserId()), Role.STOCKER);
			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 = cargoTallyService.queryOrdersByReceiver(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: searchNewOrderByOrderId
	* @Description: TODO(售货员根据订单号点击下一步查询最新的点货订单)
	{
		"userId":"",
		"orderId":""
	}
	* @param @return    设定文件
	* @return Map<String,Object>    返回类型
	* @author ylx
	* @date 2018年1月26日 下午2:24:02
	* @throws
	 */
	@RequestMapping(value="/searchNewOrderByOrderId",method=RequestMethod.POST)
	public Map<String, Object> searchNewOrderByOrderId(@RequestBody Param param){
		try {
			userAuthoricationService.queryUserAuthority(ValidationUtil.checkAndAssignInt(param.getUserId()), Role.STOCKER);
			Orders order = orderService.queryFullOrderByOrderId(ValidationUtil.checkAndAssignInt(param.getOrderId()));
			if(order==null) throw new RuntimeException("订单不存在");
			if(!Status.DELIVERING.equals(order.getStatusId())) throw new RuntimeException("非法操作");
			param.setOrder(order);
			TallyOrder tallyOrder=cargoTallyService.searchNewOrderByOrderId(param);
			return JSONUtil.getJsonMap(200, true, HintMSG.QUERY_SUCCESS, tallyOrder);
		} catch (Exception e) {
			return JSONUtil.getJsonMap(500, false, HintMSG.QUERY_FAIL+":"+e.getMessage(), null);
		}
	}
	
	/**
	 * 
	* @Title: confirmOrReject
	* @Description: TODO(收货员点击确认点货或拒收操作)
	{
		"userId":"",
		"orderId":"",
		"confirmId":""
	}
	* @param @return    设定文件
	* @return Map<String,Object>    返回类型
	* @author ylx
	* @date 2018年1月26日 下午2:35:06
	* @throws
	 */
	@RequestMapping(value="/confirmOrReject",method=RequestMethod.POST)
	public Map<String, Object> confirmOrReject(@RequestBody Param param){
		try {
			userAuthoricationService.queryUserAuthority(ValidationUtil.checkAndAssignInt(param.getUserId()), Role.STOCKER);
			ValidationUtil.checkRangeAndAssignInt(param.getConfirmId(), 0, 1);//确认id只能是0和1
			Orders order = orderService.queryFullOrderByOrderId(ValidationUtil.checkAndAssignInt(param.getOrderId()));
			if(order==null) throw new RuntimeException("订单不存在");
			if(!Status.DELIVERING.equals(order.getStatusId())) throw new RuntimeException("非法操作");
			cargoTallyService.confirmOrReject(param);
			return JSONUtil.getJsonMap(200, true, HintMSG.OPERATION_SUCCESS, null);
		} catch (Exception e) {
			return JSONUtil.getJsonMap(500, false, HintMSG.OPERATION_FAIL+":"+e.getMessage(), null);
		}
	}
	
	/**
	 * 
	* @Title: orderTracking
	* @Description: TODO(收货员查询订单跟踪信息)
	{
		"userId":"",
		"orderId":""
	}
	* @param @return    设定文件
	* @return Map<String,Object>    返回类型
	* @author ylx
	* @date 2018年1月26日 下午2:48:12
	* @throws
	 */
	@RequestMapping(value="/orderTracking",method=RequestMethod.POST)
	public Map<String, Object> orderTracking(@RequestBody Param param){
		try {
			userAuthoricationService.queryUserAuthority(ValidationUtil.checkAndAssignInt(param.getUserId()),Role.STOCKER);//检测当前操作用户权限
			Orders order = orderService.queryFullOrderByOrderId(ValidationUtil.checkAndAssignInt(param.getOrderId()));
			if(!param.getUserId().equals(order.getReceiverId())) throw new RuntimeException("无权查看");
			param.setOrder(order);
			Map<String, Object> map = cargoTallyService.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);
		}
	}
	
}
