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.constant.Statuses;
import com.fuzamei.entity.Orders;
import com.fuzamei.entity.Params;
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 HttpServletRequest req;
	
	@Autowired
	private OrderService orderService;
	
	/**
	 * 
	* @Title: queryOrdersByReceiver
	* @Description: TODO(收货员查询订单信息)
	{
		"tokenId":"xxx",
		"page":"1",
		"orderId":"",
		"carNo":"",
		"startTime":"",
		"endTime":""
	}
	* @params @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 Params params){
		try {
			String userId = req.getHeader("Authorization").split("&")[1];
			userAuthoricationService.queryUserAuthority(ValidationUtil.checkAndAssignInt(userId), Role.STOCKER);
			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 = cargoTallyService.queryOrdersByReceiver(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: searchNewOrderByOrderId
	* @Description: TODO(售货员根据订单号点击下一步查询最新的点货订单)
	{
		"tokenId":"",
		"orderId":""
	}
	* @params @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 Params params){
		try {
			String userId = req.getHeader("Authorization").split("&")[1];
			userAuthoricationService.queryUserAuthority(ValidationUtil.checkAndAssignInt(userId), Role.STOCKER);
			params.setUserId(Integer.parseInt(userId));
			Orders order = orderService.queryFullOrderByOrderId(ValidationUtil.checkAndAssignInt(params.getOrderId()));
			if(order==null) throw new RuntimeException("订单不存在");
			if(!Statuses.DELIVERING.equals(order.getStatusId())) throw new RuntimeException("非法操作");
			params.setOrder(order);
			TallyOrder tallyOrder=cargoTallyService.searchNewOrderByOrderId(params);
			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(收货员点击确认点货或拒收操作)
	{
		"tokenId":"",
		"orderId":"",
		"confirmId":""
	}
	* @params @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 Params params){
		try {
			String userId = req.getHeader("Authorization").split("&")[1];
			ValidationUtil.checkBlankAndAssignString(params.getSign());//前端签名非空校验
			userAuthoricationService.queryUserAuthority(ValidationUtil.checkAndAssignInt(userId), Role.STOCKER);
			params.setUserId(Integer.parseInt(userId));
			ValidationUtil.checkAndAssignLong(params.getTallyTime());
			ValidationUtil.checkRangeAndAssignInt(params.getConfirmId(), 0, 1);//确认id只能是0和1
			Orders order = orderService.queryFullOrderByOrderId(ValidationUtil.checkAndAssignInt(params.getOrderId()));
			if(order==null) throw new RuntimeException("订单不存在");
			if(!Statuses.DELIVERING.equals(order.getStatusId())) throw new RuntimeException("非法操作");
			cargoTallyService.confirmOrReject(params);
			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(收货员查询订单跟踪信息)
	{
		"tokenId":"",
		"orderId":""
	}
	* @params @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 Params params){
		try {
			String userId = req.getHeader("Authorization").split("&")[1];
			userAuthoricationService.queryUserAuthority(ValidationUtil.checkAndAssignInt(userId),Role.STOCKER);//检测当前操作用户权限
			params.setUserId(Integer.parseInt(userId));
			Orders order = orderService.queryFullOrderByOrderId(ValidationUtil.checkAndAssignInt(params.getOrderId()));
			if(!params.getUserId().equals(order.getReceiverId())) throw new RuntimeException("无权查看");
			params.setOrder(order);
			List<Object> list = cargoTallyService.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);
		}
	}
	
}
