package com.blueland.user.role;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.blueland.annotation.ApiVersion;
import com.blueland.core.util.UserUtils;
import com.blueland.enums.Version;
import com.blueland.model.TPfApplication;
import com.blueland.service.IApplicationService;
import com.blueland.service.IRoleService;
import com.blueland.service.ITucStaffService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.blueland.bo.ApplicationEntity;
/**
 * @author 代码自动生成工具
 * @version 1.0
 * @since  
 * @author cg
 */
import com.blueland.bo.RoleEntity;
import com.blueland.bo.TucStaffEntity;

import io.swagger.annotations.Api;

import static com.blueland.constant.ResponseConstant.*;


@Api(value = "userRole")
@RestController
@RequestMapping(value = "/userRole")
public class UserRoleRest {
	@Autowired
	private IRoleService roleService;
	
	@Autowired
	private ITucStaffService pubUserService;
	@Autowired
	private IApplicationService appService;
	
	/**
	 * 获取用户下面所有角色
	 * @param appId
	 * @param userId
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/{appId}/user/{userId}/roles",produces = "application/json;charset=UTF-8",method = RequestMethod.GET)
	@ResponseBody
	@CrossOrigin
	public Map<String, Object> listUserRole(@PathVariable("appId") String appId,@PathVariable("userId")String userId,
			HttpServletRequest request,HttpServletResponse response)  {
		Map < String , Object > result = new HashMap< String , Object>(); 
		try {
			if(StringUtils.isBlank(appId) || StringUtils.isBlank(userId)){
				result.put("resultCode", "401");
				result.put("resultMsg", "输入参数异常");
			}else{
				TPfApplication app = appService.getById(appId);
				TucStaffEntity user = pubUserService.getStaffByStaffId(userId);
			    if(app==null){
			    	result.put("resultCode", "401");
					result.put("resultMsg", "应用ID错误");
				}else if(user==null){
					result.put("resultCode", RESULT_CODE_ERROR_CODE);
					result.put("resultMsg", "用户ID错误");
				}else{
					List<RoleEntity> roleEntryList = roleService.findRolesByUser(appId, userId);
					result.put("resultCode", RESULT_CODE_SUCCESS_CODE);
					result.put("resultMsg", "处理成功");
					result.put("result", roleEntryList);
				}
			}
		} catch (Exception e) {
			result.put("resultCode", RESULT_CODE_ERROR_CODE);
			result.put("resultMsg", "系统内部异常");
			e.printStackTrace();
		}
		return result;
	}


	@ApiVersion(groups = Version.SIX)
	@ApiOperation("获取用户下面所有角色")
	@GetMapping(value = "/user/roles")
	@ResponseBody
	@CrossOrigin
	public Map<String, Object> listUserByRole()  {
		Map < String , Object > result = new HashMap< String , Object>();
		try {
			String appId = UserUtils.getTenantId();
			String postCode = UserUtils.getPostCode();
			if(StringUtils.isBlank(appId) || StringUtils.isBlank(postCode)){
				result.put("resultCode", "401");
				result.put("resultMsg", "请求头参数异常");
			}else{
				TPfApplication app = appService.getById(appId);
				Long userCount = pubUserService.selectUserCount(postCode);
				if(app==null){
					result.put("resultCode", "401");
					result.put("resultMsg", "应用ID错误");
				}else if(userCount <= 0){
					result.put("resultCode", RESULT_CODE_ERROR_CODE);
					result.put("resultMsg", "用户ID错误");
				}else{
					List<RoleEntity> roleEntryList = roleService.findRolesByPostCode(appId, postCode);
					result.put("resultCode", RESULT_CODE_SUCCESS_CODE);
					result.put("resultMsg", "处理成功");
					result.put("result", roleEntryList);
				}
			}
		} catch (Exception e) {
			result.put("resultCode", RESULT_CODE_ERROR_CODE);
			result.put("resultMsg", "系统内部异常");
			e.printStackTrace();
		}
		return result;
	}
	
}



