package com.th.demo.controller.system;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.th.demo.model.system.SysUserRole;
import com.th.demo.model.system.Type;
import com.th.demo.service.impl.system.SysUserRoleServiceImpl;
import com.th.demo.service.system.SysUserRoleService;
import com.th.demo.tools.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

@Controller
@RequestMapping(value = "/SysUserRole")
public class SysUserRoleController {
	
	@Autowired
	public SysUserRoleServiceImpl sysUserRoleServiceImpl;
	@Autowired
	public SysUserRoleService sysUserRoleService;


	Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
	String result = Type.FAIL;

	@RequestMapping(value = "/remove.do", method = RequestMethod.POST)
	public void remove(String userId,
						String roleId,
			HttpServletResponse response) throws IOException {

		result = Tools.moreThanZero(sysUserRoleServiceImpl.remove(userId, roleId));

		response.setCharacterEncoding("utf-8");
		response.setHeader("Content-type", "text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.print(result);
		out.flush();
		out.close();
	}

	@RequestMapping(value = "/save.do", method = RequestMethod.POST)//Post方法
	public void save(String userId,
						  String roleId,
						  String addId,
						  HttpServletResponse response) throws IOException {
//调用service业务逻辑
		result = sysUserRoleService.save(userId,roleId,addId);
		response.setCharacterEncoding("utf-8");
		response.setHeader("Content-type", "text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.print(result);
		out.flush();
		out.close();
	}
	@RequestMapping(value = "/show.do")
	public ModelAndView show() throws IOException {
		return new ModelAndView("system/sysuserrole");
	}
}
