package com.ifish.action;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ifish.entity.User;
import com.ifish.enums.ResultEnum;
import com.ifish.service.BaseService;
import com.ifish.util.IfishUtil;

/** 
 * @ClassName: UpdateUserAction 
 * @Description: TODO 
 * @author ggw 
 */

@Controller
@RequestMapping("/updateUser")
public class UpdateUserAction {
	
	@Autowired
	private BaseService baseService;
	
	private static Logger log = LoggerFactory.getLogger(UpdateUserAction.class);

	@ModelAttribute("user")  
	public User getUser(@RequestParam Integer userId) {
		return baseService.findById(userId);  
	}
	/**
	 * 更新用户信息
	 * @param user
	 * @return
	 */
	@RequestMapping("/updateUser.do")
	@ResponseBody
	public Object update(@ModelAttribute("user") User user){
		try {
			return baseService.updateInfo(user);
		} catch (Exception e) {
			log.error("update userinfo:userId:{},error message:{}",user.getUserId(),e.toString());
		}
		return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
	}
	
	/**
	 * 上传头像
	 * @param user
	 * @param fileUpload
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/uploadFile.do")
	@ResponseBody
	public Object upload(@ModelAttribute("user") User user,@RequestParam MultipartFile fileUpload){
		try {
			if(fileUpload.getSize()>1048576){
				return IfishUtil.returnJson(ResultEnum.warn206.getKey(), "");
			}
			else{
				return baseService.uploadFile(user,fileUpload);
			}
		} catch (Exception e) {
			log.error("upload images:userId:{},error message:{}",user.getUserId(),e.toString());
		}
		return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
	}
	
}
