package com.cidic.sdx.controllers;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.jgroups.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
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.ResponseBody;

import com.cidic.sdx.exception.SdxException;
import com.cidic.sdx.model.ListResultModel;
import com.cidic.sdx.model.ResultModel;
import com.cidic.sdx.model.Style;
import com.cidic.sdx.model.User;
import com.cidic.sdx.model.UserStyle;
import com.cidic.sdx.services.UserService;

@Controller
@RequestMapping("/user")
public class UserController {
	
	private static final Logger logger = LoggerFactory.getLogger(StyleController.class);
	
	@Autowired
	@Qualifier(value="userServiceImpl")
	private UserService userServiceImpl;
	
	private ResultModel resultModel = null;
	
	@ExceptionHandler(SdxException.class)
	public @ResponseBody ResultModel handleCustomException(SdxException ex) {
		ResultModel resultModel = new ResultModel();
		resultModel.setResultCode(ex.getErrCode());
		resultModel.setMessage(ex.getErrMsg());
		return resultModel;
	}
	
	@RequestMapping(value = "/userMgr", method = RequestMethod.GET)
	public String userMgr(Locale locale, Model model) {
		return "userMgr";
	}
	
	@RequestMapping(value = "/showUserChoose", method = RequestMethod.GET)
	public String showUserChoose(Locale locale, Model model) {
		return "showUserChoose";
	}
	
	@RequestMapping(value = "/insert", method = RequestMethod.POST)  
	@ResponseBody
	public ResultModel insert(@RequestParam String userId,@RequestParam String styleIds){
		
		
		try{
			User user = new User();
			user.setUserId(userId);
			user.setCreateTime(new Date());
			
			String[] styles = styleIds.split(",");
			
			List<UserStyle> userStyleList = new ArrayList<UserStyle>();
			for (String s : styles){
				Style style1 = new Style();
				style1.setId(Integer.parseInt(s));
				UserStyle userStyle1 = new UserStyle();
				userStyle1.setUser(user);
				userStyle1.setStyle(style1);
				userStyle1.setCreateTime(new Date());
				userStyleList.add(userStyle1);
			}
			
			user.setUserStyleList(userStyleList);
			userServiceImpl.insertUser(user);
			resultModel = new ResultModel();
			resultModel.setResultCode(200);
		}
		catch(Exception e){
			throw new SdxException(500, "дݳ");
		}
		return resultModel;
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.GET, produces="application/json")  
	@ResponseBody
	public ListResultModel listUser(@RequestParam int iDisplayLength, @RequestParam int iDisplayStart,@RequestParam String sEcho){
		ListResultModel listResultModel = new ListResultModel();
		try{
			List<User> list = userServiceImpl.getDataByPage(iDisplayLength, iDisplayStart, sEcho);
			listResultModel.setAaData(list);
			listResultModel.setsEcho(sEcho);
			listResultModel.setiTotalRecords(list.size());
			listResultModel.setiTotalDisplayRecords(iDisplayStart + list.size());
			listResultModel.setSuccess(true);
		}
		catch(Exception e){
			listResultModel.setSuccess(false);
		}
		return listResultModel;
	}
	
	@RequestMapping(value = "/detailResult/{userId}", method = RequestMethod.GET, produces="application/json")  
	@ResponseBody
	public ResultModel getUserResultDetailById(@PathVariable String userId){
		
		try{
			List<Style> list = userServiceImpl.getStyleListByUser(userId);
			resultModel = new ResultModel();
			resultModel.setResultCode(200);
			resultModel.setObject(list);
		}
		catch(Exception e){
			throw new SdxException(500, "дݳ");
		}
		return resultModel;
	}
}

@@ -29,16 +29,49 @@
 llers;%0A%0A
+import java.text.ParseException;%0A
 import j
@@ -162,16 +162,110 @@
 ocale;%0A%0A
+import javax.servlet.http.HttpServletRequest;%0Aimport javax.servlet.http.HttpServletResponse;%0A%0A
 import o
@@ -1957,16 +1957,25 @@
 erChoose
+/%7BuserId%7D
 %22, metho
@@ -2045,35 +2045,246 @@
 ale, Model model
-) %7B
+,@PathVariable String userId) %7B%0A%09%09List%3CStyle%3E list;%0A%09%09try %7B%0A%09%09%09list = userServiceImpl.getStyleListByUser(userId);%0A%09%09%09model.addAttribute(%22list%22, list );%0A%09%09%7D catch (ParseException e) %7B%0A%09%09%09%0A%09%09%09e.printStackTrace();%0A%09%09%7D
 %0A%09%09return %22showU
@@ -2409,16 +2409,72 @@
  insert(
+HttpServletRequest request,HttpServletResponse response,
 @Request
@@ -2530,16 +2530,244 @@
 )%7B%0A%09%09%0A%09%09
+response.setContentType(%22text/html;charset=UTF-8%22);%0A%09%09response.addHeader(%22Access-Control-Allow-Origin%22,%22*%22);%0A%09    if(%22IE%22.equals(request.getParameter(%22type%22)))%7B%0A%09    %09response.addHeader(%22XDomainRequestAllowed%22,%221%22);%0A%09    %7D%0A%09    
 %0A%09%09try%7B%0A

fix bugs