package com.huatek.frame.base.user.controller;

import java.util.Map;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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.dubbo.config.annotation.Reference;
import com.huatek.frame.base.dto.UserDTO;
import com.huatek.frame.base.entity.User;
import com.huatek.frame.base.service.UserService;
import com.huatek.frame.base.vo.UserVO;
import com.huatek.torch.frame.log.KerLog;
import com.huatek.torch.frame.permission.KerPermission;
import com.huatek.torch.frame.validate.KerValid;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 用户管理
 * 
 * @author andy_yan
 *
 */
@Api(tags={"用户管理接口"})
@RestController
@RequestMapping("/api/user")
public class UserController {
 

	@Reference(check = false)
	private UserService userService;
 
	/**
	 * 用户列表
	 * 
	 * @param page
	 * @param limit
	 * @param user
	 * @return
	 */
	@ApiOperation(value = "用户列表",httpMethod="POST", notes= "用户列表", produces = "application/json;charset=utf-8",response=UserVO.class)
	@KerLog(projectName = "PROJECTNAME", modelName = "USER.MODELNAME", methodName = "USER.METHODNAME.LISTUSERS")
	@PostMapping(value="/users", produces = { "application/json;charset=utf-8"})
	public  @KerPermission("10028") Map<String, Object> users(@RequestBody UserDTO user) {
		Map<String, Object> map = userService.findUserPage(user);
		return map;
	}

	/**
	 * 新增用户
	 * 
	 * @param user
	 * @return
	 */
	@ApiOperation(value = "新增用户",httpMethod="POST", notes= "新增用户", produces = "application/json;charset=utf-8",response=Map.class)
	@KerLog(projectName = "PROJECTNAME", modelName = "USER.MODELNAME", methodName = "USER.METHODNAME.SAVEUSERS")
	@PostMapping(value = "/addUser", produces = { "application/json;charset=utf-8"})
	public  @KerPermission("10030") @KerValid Map<String, Object> addUser(@RequestBody User user) {
		Map<String, Object> map = userService.addUser(user);
		return map;
	}

	/**
	 *  查询
	 * 
	 * @param uid
	 * @return
	 */
	@ApiOperation(value = "查询用户",httpMethod="GET", notes= "查询用户", produces = "application/json;charset=utf-8",response=UserVO.class)
	@KerLog(projectName = "PROJECTNAME", modelName = "USER.MODELNAME", methodName = "USER.METHODNAME.MODIFYQUERY")
	@GetMapping(value = "/findUser/{id}", produces = { "application/json;charset=utf-8"})
	public  @KerPermission("10029") Map<String, Object> findUser(@PathVariable(value="id")String id) {
		Map<String, Object> map = userService.findUser(id);
		return map;
	} 

	/**
	 * 修改保存
	 * 
	 * @param user
	 * @return
	 */
	@ApiOperation(value = "修改用户",httpMethod="POST", notes= "修改用户", produces = "application/json;charset=utf-8",response=Map.class)
	@KerLog(projectName = "PROJECTNAME", modelName = "USER.MODELNAME", methodName = "USER.METHODNAME.MODIFSAVE")
	@PostMapping(value = "/modifyUser", produces = { "application/json;charset=utf-8"})
	public  @KerPermission("10031") @KerValid Map<String, Object> modifyUser(@RequestBody User user) {
		Map<String, Object> map = userService.modifyUser(user);
		return map;
	}

	/**
	 * 删除用户
	 *  
	 * @param ids
	 * @return
	 */
	@ApiOperation(value = "删除用户",httpMethod="POST", notes= "删除用户", produces = "application/json;charset=utf-8",response=Map.class)
	@KerLog(projectName = "PROJECTNAME", modelName = "USER.MODELNAME", methodName = "USER.METHODNAME.MODIFSTATES")
	@PostMapping(value = "/deleteUser", produces = { "application/json;charset=utf-8"})
	public  @KerPermission("10032") Map<String, Object> deleteUser(@ApiParam(value="id集合") @RequestBody String[] ids) {
		Map<String, Object> map = userService.deleteUser(ids);
		return map;
	} 
	
	/**
	 * 更新用户角色关系
	 * 
	 * @param uid
	 * @param rIds
	 * @return
	 */
	@ApiOperation(value = "更新用户角色关系",httpMethod="POST", notes= "更新用户角色关系", produces = "application/json;charset=utf-8",response=Map.class)
	@KerLog(projectName = "PROJECTNAME", modelName = "USER.MODELNAME", methodName = "USER.METHODNAME.UPDATEROLE")
	@PostMapping(value = "/updateUserRole/{uid}", produces = { "application/json;charset=utf-8"})
	public  @KerPermission("10033") Map<String, Object> updateUserRole(@ApiParam(value="用户id") @PathVariable("uid")String uid, @ApiParam(value="角色id集合") @RequestBody String[] rids) {
		Map<String, Object> map = userService.updateUserRole(uid, rids);
		return map;
	} 
}
