@ -29,67 +29,81 @@ import java.util.UUID;
* @Date : 2020 / 2 / 18 16 : 26
* @Date : 2020 / 2 / 18 16 : 26
* @Author : PeiChen
* @Author : PeiChen
* /
* /
// 声明这是一个控制器
@Controller
@Controller
// 映射访问路径为/visitor
@RequestMapping ( "/visitor" )
@RequestMapping ( "/visitor" )
public class VisitorController {
public class VisitorController {
// 自动注入VisitorService服务类
private VisitorService visitorService ;
private VisitorService visitorService ;
// 通过Autowired注解自动注入VisitorService
@Autowired
@Autowired
public void setVisitorService ( VisitorService visitorService ) {
public void setVisitorService ( VisitorService visitorService ) {
this . visitorService = visitorService ;
this . visitorService = visitorService ;
}
}
// 访问路径为/visitor/login的请求处理方法, 返回注册页面
@RequestMapping ( "/login" )
@RequestMapping ( "/login" )
public String register ( ) {
public String register ( ) {
return "regist_visitor" ;
return "regist_visitor" ;
}
} // 返回regist_visitor视图
/ * *
/ * *
* 来 访 登 记 实 现 ( C 操 作 )
* 来 访 登 记 实 现 ( C 操 作 )
* @param visitor
* @param visitor 访 客 对 象
* @return
* @return ModelAndView 视 图 模 型 对 象
* @throws Exception
* @throws Exception 异 常
* /
* /
@RequestMapping ( "/addLogin" )
@RequestMapping ( "/addLogin" )
public ModelAndView addVisitor ( Visitor visitor ) throws Exception {
public ModelAndView addVisitor ( Visitor visitor ) throws Exception {
ModelAndView mv = new ModelAndView ( ) ;
ModelAndView mv = new ModelAndView ( ) ;
// 参数校验,如果访客信息不完整,则返回错误信息
if ( visitor = = null | | visitor . getName ( ) = = null | | visitor . getSno ( ) = = null | | visitor . getPhone ( ) = = null | | visitor . getPlace ( ) = = null ) {
if ( visitor = = null | | visitor . getName ( ) = = null | | visitor . getSno ( ) = = null | | visitor . getPhone ( ) = = null | | visitor . getPlace ( ) = = null ) {
mv . addObject ( "error_msg" , "来访登记失败,请重新登记!" ) ;
mv . addObject ( "error_msg" , "来访登记失败,请重新登记!" ) ;
mv . setViewName ( "regist_visitor" ) ;
mv . setViewName ( "regist_visitor" ) ;
return mv ;
return mv ;
}
}
// 生成访客ID
if ( visitor . getId ( ) = = null | | "" . trim ( ) . equals ( visitor . getId ( ) ) ) {
if ( visitor . getId ( ) = = null | | "" . trim ( ) . equals ( visitor . getId ( ) ) ) {
String uuid = UUID . randomUUID ( ) . toString ( ) . replace ( "-" , "" ) ;
String uuid = UUID . randomUUID ( ) . toString ( ) . replace ( "-" , "" ) ;
visitor . setId ( uuid ) ;
visitor . setId ( uuid ) ;
}
}
// 设置来访时间为当前系统时间
String date = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) . format ( new Date ( ) ) ;
String date = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) . format ( new Date ( ) ) ;
visitor . setBegin_date ( date ) ; //设置来访时间为提交来访登记时间
visitor . setBegin_date ( date ) ; //设置来访时间为提交来访登记时间
//先设置离开时间为空串,后续注销时再修改为注销时系统时间
//先设置离开时间为空串,后续注销时再修改为注销时系统时间
if ( visitor . getEnd_date ( ) = = null | | "" . trim ( ) . equals ( visitor . getEnd_date ( ) ) ) {
if ( visitor . getEnd_date ( ) = = null | | "" . trim ( ) . equals ( visitor . getEnd_date ( ) ) ) {
visitor . setEnd_date ( "" ) ;
visitor . setEnd_date ( "" ) ;
}
}
// 添加访客信息到数据库
visitorService . add ( visitor ) ;
visitorService . add ( visitor ) ;
// 添加访客ID到模型, 用于页面显示
mv . addObject ( "id" , visitor . getId ( ) ) ;
mv . addObject ( "id" , visitor . getId ( ) ) ;
mv . setViewName ( "visitor-success" ) ;
mv . setViewName ( "visitor-success" ) ; // 设置成功页面视图
return mv ;
return mv ;
}
}
/ * *
/ * *
* 访 客 记 录 注 销
* 访 客 记 录 注 销
* @param request
* @param request HttpServletRequest 对 象
* @return
* @return ModelAndView 视 图 模 型 对 象
* @throws Exception
* @throws Exception 异 常
* /
* /
@RequestMapping ( "/login_out" )
@RequestMapping ( "/login_out" )
public ModelAndView logout ( HttpServletRequest request ) throws Exception {
public ModelAndView logout ( HttpServletRequest request ) throws Exception {
ModelAndView mv = new ModelAndView ( ) ;
ModelAndView mv = new ModelAndView ( ) ;
// 获取访客ID
String id = request . getParameter ( "id" ) ;
String id = request . getParameter ( "id" ) ;
// 如果ID为空, 则返回错误信息
if ( id = = null | | "" . trim ( ) . equals ( id ) ) {
if ( id = = null | | "" . trim ( ) . equals ( id ) ) {
mv . addObject ( "logout_msg" , "系统繁忙,请稍后再试!" ) ;
mv . addObject ( "logout_msg" , "系统繁忙,请稍后再试!" ) ;
mv . setViewName ( "regist_visitor" ) ;
mv . setViewName ( "regist_visitor" ) ;
}
}
// 获取当前系统时间
String date = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) . format ( new Date ( ) ) ;
String date = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) . format ( new Date ( ) ) ;
// 注销访客记录
visitorService . logout ( id , date ) ;
visitorService . logout ( id , date ) ;
mv . addObject ( "logout_msg" , "注销成功" ) ;
mv . addObject ( "logout_msg" , "注销成功" ) ;
mv . setViewName ( "regist_visitor" ) ;
mv . setViewName ( "regist_visitor" ) ;
@ -98,79 +112,106 @@ public class VisitorController {
/ * *
/ * *
* 管 理 员 手 动 注 销 来 访 状 态
* 管 理 员 手 动 注 销 来 访 状 态
* @param request
* @param request HttpServletRequest 对 象
* @param response
* @param response HttpServletResponse 对 象
* @throws Exception
* @throws Exception 异 常
* /
* /
@RequestMapping ( "/updateStatus" )
@RequestMapping ( "/updateStatus" )
public void updateStatus ( HttpServletRequest request , HttpServletResponse response ) throws Exception {
public void updateStatus ( HttpServletRequest request , HttpServletResponse response ) throws Exception {
request . setCharacterEncoding ( "utf-8" ) ;
request . setCharacterEncoding ( "utf-8" ) ;
response . setCharacterEncoding ( "utf-8" ) ;
response . setCharacterEncoding ( "utf-8" ) ;
PrintWriter writer = response . getWriter ( ) ;
PrintWriter writer = response . getWriter ( ) ;
// 获取访客ID
String id = request . getParameter ( "id" ) ;
String id = request . getParameter ( "id" ) ;
// 如果ID为空, 则返回错误信息
if ( id = = null | | "" . trim ( ) . equals ( id ) ) {
if ( id = = null | | "" . trim ( ) . equals ( id ) ) {
writer . write ( "false" ) ;
writer . write ( "false" ) ;
return ;
return ;
}
}
// 获取当前系统时间
String date = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) . format ( new Date ( ) ) ;
String date = new SimpleDateFormat ( "yyyy-MM-dd HH:mm:ss" ) . format ( new Date ( ) ) ;
// 注销访客记录
visitorService . logout ( id , date ) ;
visitorService . logout ( id , date ) ;
writer . write ( "true" ) ;
writer . write ( "true" ) ;
}
}
/ * *
/ * *
* 查 询 所 有 访 客 记 录
* 查 询 所 有 访 客 记 录
* @param page
* @param page 当 前 页 码
* @param size
* @param size 每 页 显 示 数 量
* @param request
* @param request HttpServletRequest 对 象
* @param response
* @param response HttpServletResponse 对 象
* @return
* @return ModelAndView 视 图 模 型 对 象
* @throws Exception
* @throws Exception 异 常
* /
* /
@RequestMapping ( "/findAll" )
@RequestMapping ( "/findAll" )
public ModelAndView findAll ( @RequestParam ( name = "page" , required = true , defaultValue = "1" ) int page , @RequestParam ( name = "size" , required = true , defaultValue = "5" ) int size , HttpServletRequest request , HttpServletResponse response ) throws Exception {
public ModelAndView findAll ( @RequestParam ( name = "page" , required = true , defaultValue = "1" ) int page , @RequestParam ( name = "size" , required = true , defaultValue = "5" ) int size , HttpServletRequest request , HttpServletResponse response ) throws Exception {
// 设置请求和响应的字符编码为UTF-8
request . setCharacterEncoding ( "utf-8" ) ;
request . setCharacterEncoding ( "utf-8" ) ;
response . setCharacterEncoding ( "utf-8" ) ;
response . setCharacterEncoding ( "utf-8" ) ;
// 创建ModelAndView对象用于返回视图和数据
ModelAndView mv = new ModelAndView ( ) ;
ModelAndView mv = new ModelAndView ( ) ;
List < Visitor > visitors = null ;
List < Visitor > visitors = null ;
// 获取请求中的keyword参数
String keyword = request . getParameter ( "keyword" ) ;
String keyword = request . getParameter ( "keyword" ) ;
// 如果keyword为空或为空字符串, 则查询所有访客记录
if ( keyword = = null | | "" . trim ( ) . equals ( keyword ) | | keyword . length ( ) = = 0 ) {
if ( keyword = = null | | "" . trim ( ) . equals ( keyword ) | | keyword . length ( ) = = 0 ) {
visitors = visitorService . findAll ( page , size ) ;
visitors = visitorService . findAll ( page , size ) ;
} else {
} else {
// 否则根据keyword进行搜索
visitors = visitorService . search ( page , size , keyword ) ;
visitors = visitorService . search ( page , size , keyword ) ;
}
}
// 创建分页信息对象
PageInfo pageInfo = new PageInfo ( visitors ) ;
PageInfo pageInfo = new PageInfo ( visitors ) ;
// 将分页信息添加到ModelAndView对象中
mv . addObject ( "pageInfo" , pageInfo ) ;
mv . addObject ( "pageInfo" , pageInfo ) ;
// 设置视图名称为"visitor-list"
mv . setViewName ( "visitor-list" ) ;
mv . setViewName ( "visitor-list" ) ;
// 返回ModelAndView对象
return mv ;
return mv ;
}
}
/ * *
/ * *
* 访 客 日 志 `
* 访 客 日 志 `
* @return
* @return ModelAndView 视 图 模 型 对 象 , 包 含 访 客 日 志 列 表 和 分 页 信 息
* @throws Exception
* @throws Exception 抛 出 可 能 的 异 常
* /
* /
@RequestMapping ( "/log" )
@RequestMapping ( "/log" )
public ModelAndView log ( @RequestParam ( name = "page" , required = true , defaultValue = "1" ) int page , @RequestParam ( name = "size" , required = true , defaultValue = "10" ) int size ) throws Exception {
public ModelAndView log ( @RequestParam ( name = "page" , required = true , defaultValue = "1" ) int page , @RequestParam ( name = "size" , required = true , defaultValue = "10" ) int size ) throws Exception {
// 创建ModelAndView对象用于返回视图和数据
ModelAndView mv = new ModelAndView ( ) ;
ModelAndView mv = new ModelAndView ( ) ;
// 获取访客日志列表
List < Visitor > logs = visitorService . log ( page , size ) ;
List < Visitor > logs = visitorService . log ( page , size ) ;
// 创建分页信息对象
PageInfo pageInfo = new PageInfo ( logs ) ;
PageInfo pageInfo = new PageInfo ( logs ) ;
// 将分页信息添加到ModelAndView对象中
mv . addObject ( "pageInfo" , pageInfo ) ;
mv . addObject ( "pageInfo" , pageInfo ) ;
// 设置视图名称为"visitor-log"
mv . setViewName ( "visitor-log" ) ;
mv . setViewName ( "visitor-log" ) ;
// 返回ModelAndView对象
return mv ;
return mv ;
}
}
/ * *
/ * *
* 导 出 访 客 信 息
* 导 出 访 客 信 息
* @param response
* @param response HttpServletResponse 对 象 , 用 于 发 送 Excel 文 件 作 为 响 应
* @throws Exception
* @throws Exception 抛 出 可 能 的 异 常
* /
* /
@RequestMapping ( "/visitorInfo" )
@RequestMapping ( "/visitorInfo" )
public void export ( HttpServletResponse response ) throws Exception {
public void export ( HttpServletResponse response ) throws Exception {
// 获取Excel文件的输入流
InputStream is = visitorService . getInputStream ( ) ;
InputStream is = visitorService . getInputStream ( ) ;
// 设置响应的内容类型为Excel文件
response . setContentType ( "application/vnd.ms-excel" ) ;
response . setContentType ( "application/vnd.ms-excel" ) ;
// 设置响应头, 指示浏览器以附件形式下载文件, 并设置文件名为visitorInfo.xls
response . setHeader ( "contentDisposition" , "attachment;filename=visitorInfo.xls" ) ;
response . setHeader ( "contentDisposition" , "attachment;filename=visitorInfo.xls" ) ;
// 获取响应的输出流
ServletOutputStream outputStream = response . getOutputStream ( ) ;
ServletOutputStream outputStream = response . getOutputStream ( ) ;
// 使用IOUtils工具类将输入流的内容复制到输出流
IOUtils . copy ( is , outputStream ) ;
IOUtils . copy ( is , outputStream ) ;
}
}