package com.ifish.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.ifish.dto.JsonResult;
import com.ifish.entity.User;
import com.ifish.enums.ResultEnum;
import com.ifish.exception.IfishException;
import com.ifish.service.UserService;
import com.ifish.validator.LoginParam;
import com.ifish.validator.RegisterParam;

/** 
 * @ClassName:UsersAction 
 * @author ggw 
 */

@RestController
@RequestMapping("/users")
public class UsersAction {
	
	@Autowired
	private UserService userService;
	
	/**
	 * 获取验证码
	 * @param sendType
	 * @param phoneNumber
	 * @return
	 */
	@RequestMapping(value="/v3/getVerificateCode.do",method=RequestMethod.POST)
	public JsonResult<?> getVerificateCode(String sendType,String phoneNumber){
		return userService.getVerificateCode(phoneNumber, sendType);
	}
	
	/**
	 * 用户注册
	 * @param version
	 * @param userParam
	 * @param result
	 * @return
	 */
	@RequestMapping(value={"/v3/register.do","register.do"},method=RequestMethod.POST)
	public JsonResult<?> register(@Validated RegisterParam registerParam,BindingResult result) {
		//参数校验
		if(result.hasErrors()){
			throw new IfishException(ResultEnum.error401);
		}
		return userService.register(registerParam);
	}
	
	/**
	 * 用户登录
	 * @param version
	 * @param userParam
	 * @param result
	 * @return
	 */
	@RequestMapping(value={"/v3/login.do","/login.do"},method=RequestMethod.POST)
	public JsonResult<?> login(@Validated LoginParam loginParam,BindingResult result) {
		//参数校验
		if(result.hasErrors()){
			throw new IfishException(ResultEnum.error401);
		}
		return userService.login(loginParam);
	}
	
	/**
	 * 登陆验证
	 * @param loginParam
	 * @param result
	 * @return
	 */
	@RequestMapping(value="/loginValidation",method=RequestMethod.POST)
	public JsonResult<?> loginValidate(@Validated LoginParam loginParam,BindingResult result) {
		//参数校验
		if(result.hasErrors()){
			throw new IfishException(ResultEnum.error401);
		}
		return userService.loginValidate(loginParam);
	}
	
	/**
	 * 更多用户数据
	 * @param userId
	 * @return
	 */
	@RequestMapping(value="/moreUserData/{userId}",method=RequestMethod.GET)
	public JsonResult<?> moreUserData(@PathVariable("userId") Integer userId) {
		return userService.moreUserData(userId);
	}
	
	/**
	 * 绑定设备
	 * @param userId
	 * @param macAddress
	 * @return
	 */
	@RequestMapping(value="/bindingDevice/{userId}",method=RequestMethod.POST)
	public JsonResult<?> bindDevice(@PathVariable("userId") Integer userId,@RequestParam("macAddress") String macAddress) {
		return userService.bindDevice(userId, macAddress);
	}
	
	/**
	 * 扫一扫分享设备
	 * @param userId
	 * @param macAddress
	 * @return
	 */
	@RequestMapping(value="/sharingDevice/{userId}",method=RequestMethod.POST)
	public JsonResult<?> shareDevice(@PathVariable("userId") Integer userId,@RequestParam("deviceId") Integer deviceId) {
		return userService.shareDevice(userId, deviceId);
	}
	
	/**
	 * 修改用户信息
	 * @param user
	 * @return
	 */
	@RequestMapping(value="/v3/updateInfo.do",method=RequestMethod.POST)
	public JsonResult<?> updateInfo(User user) {
		return userService.updateInfo(user);
	}
	
	/**
	 * 上传用户头像
	 * @param userId
	 * @param fileUpload
	 * @return
	 */
	@RequestMapping(value="/v3/uploadUserImg.do",method=RequestMethod.POST)
	public JsonResult<?> uploadUserImg(@RequestParam("userId")Integer userId,MultipartFile fileUpload) {
		return userService.uploadUserImg(userId,fileUpload);
	}
	
}
