package com.fuzamei.web;

import java.util.List;
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.UserDetail;
import com.fuzamei.service.CargoConsignService;
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="/cargoConsign")
public class CargoConsignAction {

	@Autowired
	private CargoConsignService cargoConsignService;
	
	@Autowired
	private UserAuthoricationService userAuthoricationService;
	
	@Autowired
	private OrderService orderService;
	
	private static final Integer ROWNUM=Integer.parseInt(ReadConfUtil.getProperty("rowNum"));
	/**
	 * 
	* @Title: queryOrdersBySupplier
	* @Description: TODO(供应商查询订单信息)
	{
		"userId":"xx",
		"page":"1",
		"orderId":"",
		"boxNo":"",
		"statusId":"",
		"startTime":"",
		"endTime":""
	}
	* @param @return    设定文件
	* @return Map<String,Object>    返回类型
	* @author ylx
	* @date 2018年1月26日 上午11:35:13
	* @throws
	 */
	@RequestMapping(value="/queryOrdersBySupplier",method=RequestMethod.POST)
	public Map<String, Object> queryOrdersBySupplier(@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 = cargoConsignService.queryOrdersBySupplier(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: queryOrdersByCarrier
	* @Description: TODO(承运商查看订单信息)
	{
		"userId":"",
		"page":"1",
		"orderId":"",
		"boxNo":"",
		"statusId":"",
		"startTime":"",
		"endTime":""
	}
	* @param @return    设定文件
	* @return Map<String,Object>    返回类型
	* @author ylx
	* @date 2018年1月26日 下午1:03:33
	* @throws
	 */
	@RequestMapping(value="/queryOrdersByCarrier",method=RequestMethod.POST)
	public Map<String, Object> queryOrdersByCarrier(@RequestBody Param param){
		try {
			userAuthoricationService.queryUserAuthority(ValidationUtil.checkAndAssignInt(param.getUserId()), Role.CARRIER);
			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 = cargoConsignService.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: doCarry
	* @Description: TODO(承运商点击承运按钮后对订单表进行更新)
	{
		"userId":"",
		"orderId":"",
		"carNo":""
	}
	* @param @return    设定文件
	* @return Map<String,Object>    返回类型
	* @author ylx
	* @date 2018年1月26日 下午1:14:26
	* @throws
	 */
	@RequestMapping(value="/doCarry",method=RequestMethod.POST)
	public Map<String, Object> doCarry(@RequestBody Param param){
		try {
			userAuthoricationService.queryUserAuthority(ValidationUtil.checkAndAssignInt(param.getUserId()), Role.CARRIER);
			Orders order = orderService.queryFullOrderByOrderId(ValidationUtil.checkAndAssignInt(param.getOrderId()));
			if(order==null) throw new RuntimeException("订单不存在");
			if(!Status.UNDELIVER.equals(order.getStatusId())) throw new RuntimeException("非法操作");
			if(!param.getUserId().equals(order.getCarrierId())) throw new RuntimeException("无权操作");
			cargoConsignService.doCarry(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: showAllCarriersUnderSupplier
	* @Description: TODO(当供应商点击发货时候，跳出的弹框中承运商下拉框中显示所有承运商)
	{
		"userId":"xxx"
	}
	* @param @return    设定文件
	* @return Map<String,Object>    返回类型
	* @author ylx
	* @date 2018年1月26日 下午12:03:48
	* @throws
	 */
	@RequestMapping(value="/showAllCarriersUnderSupplier",method=RequestMethod.POST)
	public Map<String, Object> showAllCarriersUnderSupplier(@RequestBody Param param){
		try {
			userAuthoricationService.queryUserDetail(ValidationUtil.checkAndAssignInt(param.getUserId()), Role.SUPPLIER);
			param.setRoleName(Role.CARRIER);
			List<UserDetail> carrierList = userAuthoricationService.showAllCarriersUnderSupplier(param);
			return JSONUtil.getJsonMap(200, true, HintMSG.QUERY_SUCCESS, carrierList);
		} catch (Exception e) {
			return JSONUtil.getJsonMap(500, false, HintMSG.QUERY_FAIL+":"+e.getMessage(), null);
		}
	}
	
	
	/**
	 * 
	* @Title: orderTracking
	* @Description: TODO(供应商和承运商的订单查询)
	{
		"userId":"xxx",
		"orderId":""
	}
	* @param @return    设定文件
	* @return Map<String,Object>    返回类型
	* @author ylx
	* @date 2018年1月26日 下午1:47:45
	* @throws
	 */
	@RequestMapping(value="/orderTracking",method=RequestMethod.POST)
	public Map<String, Object> orderTracking(@RequestBody Param param){
		try {
			UserDetail userDetail = userAuthoricationService.queryUserAuthority(ValidationUtil.checkAndAssignInt(param.getUserId()),Role.CARRIER,Role.SUPPLIER);//检测当前操作用户权限
			Orders order = orderService.queryFullOrderByOrderId(ValidationUtil.checkAndAssignInt(param.getOrderId()));
			if(Role.SUPPLIER.equals(userDetail.getRoleName())){
				if(!param.getUserId().equals(order.getSupplierId())) throw new RuntimeException("无权查看");
			}
			if(Role.CARRIER.equals(userDetail.getRoleName())){
				if(!param.getUserId().equals(order.getCarrierId())) throw new RuntimeException("无权查看");
			}
			param.setOrder(order);
			Map<String, Object> map = cargoConsignService.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);
		}
	}
	
}
