package cfw.controller;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cfw.model.Sign;
import cfw.model.User;
import cfw.service.SignService;
import cfw.service.UserService;
@Controller
@RequestMapping("/user/")
public class UserController {
@Autowired
@Qualifier("userService")
private UserService userService;
@Autowired
@Qualifier("signService")
private SignService signService;
//登录简单的验证
@ResponseBody
@RequestMapping(value="/login/")
public String login(HttpServletRequest request , ModelMap model){
String username=request.getParameter("username");
String password=request.getParameter("password");
Map param=new HashMap();
param.put("username", username);
param.put("password", password);
User user=this.userService.getUserByParam(param);
if(user==null){
return "0";
}
request.getSession().setAttribute("user", user);
return "1";
}
@RequestMapping(value="/index/")
public String index(HttpServletRequest request , ModelMap model){
User user=(User) request.getSession().getAttribute("user");
if(user==null){
//未登录转到登录页面 WEB-INF/index.jsp
return "redirect:/";
}
//判断用户是否签到
int is_sign=this.signService.isSign(user);
model.addAttribute("user", user);
model.addAttribute("is_sign", is_sign);
return "/user";
}
//签到
@ResponseBody
@RequestMapping(value="/sign/")
public String sign(HttpServletRequest request , ModelMap model){
User user=(User) request.getSession().getAttribute("user");
this.signService.sign(user);
return "";
}
//获取签到历史
@ResponseBody
@RequestMapping(value="/getSign/")
public String getSign(HttpServletRequest request , ModelMap model){
User user=(User) request.getSession().getAttribute("user");
String year=request.getParameter("year");
String month=request.getParameter("month");
Sign sign=this.signService.getSign(user,year,month);
JSONObject jsonObject=JSONObject.fromObject(sign);
return jsonObject.toString();
}
}