package com.fuzamei.web;

import java.util.LinkedHashMap;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.fuzamei.constant.HintMSG;
import com.fuzamei.constant.Role;
import com.fuzamei.constant.Roles;
import com.fuzamei.entity.User;
import com.fuzamei.service.CarrierService;
import com.fuzamei.service.UserAuthoricationService;
import com.fuzamei.util.JSONUtil;
import com.fuzamei.util.ValidationUtil;

@RestController
@RequestMapping(path="/carrier")
public class CarriersAction {

	@Autowired
	private CarrierService carrierService;
	
	@Autowired
	private UserAuthoricationService userAuthoricationService;
	@Autowired
	private HttpServletRequest req;
	/**
	 * 
	* @Title: queryCarriers
	* @Description: TODO(这里用一句话描述这个方法的作用)
	{
		"user_id":"",
		"username":""
	}
	* @param @return    设定文件
	* @return Map<String,Object>    返回类型
	* @author ylx
	* @date 2018年1月29日 下午7:55:37
	* @throws
	 */
	@PostMapping(value="queryCarriers")
	public Map<String, Object> queryCarriers(@RequestBody String data){
		try {
			@SuppressWarnings("unchecked")
			Map<String, Object> map = JSON.parseObject(data,Map.class);
			//Integer user_id = ValidationUtil.checkAndAssignInt(map.get("user_id"));
			//String tokenId= (String) map.get("tokenId");
			Integer user_id = ValidationUtil.checkAndAssignInt(req.getHeader("Authorization").split("&")[1]);
			userAuthoricationService.queryUserAuthority(user_id, Roles.ADMIN);
			String username=ValidationUtil.checkBlankStringAndAssignEmptyIfIsBlank(map.get("username"));
			Map<String, Object> mapToService = new LinkedHashMap<String, Object>();
			mapToService.put("user_id",user_id);
			mapToService.put("username", username);
			List<User> list=carrierService.queryCarriers(mapToService);
			return JSONUtil.getJsonMap(200, true, HintMSG.QUERY_SUCCESS, list);
		} catch (Exception e) {
			return JSONUtil.getJsonMap(500, false, HintMSG.QUERY_FAIL+":"+e.getMessage(), null);
		}
	}
	
}
