package com.fuzamei.web;

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

@RestController
@RequestMapping(value="/receiving_clerk")
public class ReceivingClerkAction {
	@Autowired
	private ReceivingClerkService receivingClerkService;
	
	@Autowired
	private UserAuthoricationService userAuthoricationService;
	@Autowired
	private HttpServletRequest req;
	/**
	   {
        "user_id":"1001",
        "username":""
       } 
	 * @param data
	 * @return
	 */
	@PostMapping(value="queryReceivingClerk")
	public Map<String, Object> queryReceivingClerk(@RequestBody String data){
		try {
			@SuppressWarnings("unchecked")
			Map<String, Object> map = JSON.parseObject(data,Map.class);
			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=receivingClerkService.queryReceivingClerk(mapToService);
			return JSONUtil.getJsonMap(200, true, HintMSG.QUERY_SUCCESS, list);
		} catch (Exception e) {
			e.printStackTrace();
			return JSONUtil.getJsonMap(500, false, HintMSG.QUERY_FAIL+":"+e.getMessage(), null);
		}
	 }
	
	
	//根据用户id 查询角色     test   此方法不用
	/**
	 * "user_id":"1001"
	 */
	@PostMapping(value="selectRoleByUserId")
	public Map<String, Object> selectRoleByUserId(@RequestBody String data){
		try {
			@SuppressWarnings("unchecked")
			Map<String, Object> map = JSON.parseObject(data,Map.class);//将前端传过来的json数据转化为map对象，并将map对象传入service层进行数据校验
			Integer userId = ValidationUtil.checkAndAssignInt(map.get("user_id"));  
			
			String ss=receivingClerkService.selectRoleByUserId(userId);
			System.out.println(ss+"fffffffffff");
		    return JSONUtil.getJsonMap(200, true, HintMSG.QUERY_SUCCESS, ss);
		} catch (Exception e) {
			return JSONUtil.getJsonMap(500, false, HintMSG.QUERY_FAIL+":"+e.getMessage(), null);
		}
	}
	
	
}
