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.SupplierService;
import com.fuzamei.service.UserAuthoricationService;
import com.fuzamei.util.JSONUtil;
import com.fuzamei.util.ValidationUtil;

@RestController
@RequestMapping(path="/supplier")
public class SupplierAction {
	@Autowired
	private SupplierService supplierService;
	
	@Autowired
	private UserAuthoricationService userAuthoricationService;
	@Autowired
	private HttpServletRequest req;
	/**
	 * 
	* @Title: querySupplier
	* @Description: TODO(查询供应商信息列表)
	{
         "user_id":"1001",
         "username":"小张"
    }
	* @param @return    设定文件
	* @return Map<String,Object>    返回类型
	* @author ylx
	* @date 2018年1月29日 下午8:18:52
	* @throws  
	 */
	@PostMapping(value="querySupplier")
	public Map<String, Object> querySupplier(@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=supplierService.querySupplier(mapToService);
			return JSONUtil.getJsonMap(200, true, HintMSG.QUERY_SUCCESS, list);
		} catch (Exception e) {
			return JSONUtil.getJsonMap(500, false, HintMSG.QUERY_FAIL+":"+e.getMessage(), null);
		}
	}
	
	/**
	 * 
	* @Title: queryPlannerBySupplierId
	* @Description: TODO(查询该供应商对应的计划员有哪几个)
	{
	 	"user_id":"",
	 	"supplierId":"1005"
     }
	* @param @return    设定文件
	* @return Map<String,Object>    返回类型
	* @author ylx
	* @date 2018年1月29日 下午8:19:35
	* @throws
	 */
	@PostMapping(value="queryPlannerBySupplierId")
	public Map<String, Object> queryPlannerBySupplierId(@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);
			Integer supplierId = ValidationUtil.checkAndAssignInt(map.get("supplierId"));
			userAuthoricationService.queryUserAuthority(supplierId);//校验传来得有没有这个id
		    List<User> plannerList=supplierService.queryPlannerBySupplierId(supplierId);
		    return JSONUtil.getJsonMap(200, true, HintMSG.QUERY_SUCCESS, plannerList);
		} catch (Exception e) {
			return JSONUtil.getJsonMap(500, false, HintMSG.QUERY_FAIL+":"+e.getMessage(), null);
		}
	}
	
}
