mirror of
https://github.com/saysky/Hotel
synced 2025-05-06 19:49:25 +08:00
update
This commit is contained in:
parent
c24d3024ae
commit
efd2ce9e10
@ -1,58 +0,0 @@
|
||||
package com.example.hotel.config;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.web.servlet.LocaleResolver;
|
||||
import org.springframework.web.servlet.config.annotation.*;
|
||||
import org.springframework.web.servlet.i18n.SessionLocaleResolver;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* 拦截器,资源路径配置
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
@EnableWebMvc
|
||||
@ComponentScan(basePackages = "com.example.hotel.controller")
|
||||
@PropertySource(value = "classpath:application.yaml", ignoreResourceNotFound = true, encoding = "UTF-8")
|
||||
public class MvcConfig implements WebMvcConfigurer {
|
||||
|
||||
|
||||
/**
|
||||
* 配置静态资源路径
|
||||
*
|
||||
* @param registry registry
|
||||
*/
|
||||
@Override
|
||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
registry.addResourceHandler("/static/**")
|
||||
.addResourceLocations("classpath:/static/");
|
||||
registry.addResourceHandler("/**")
|
||||
.addResourceLocations("classpath:/templates/themes/")
|
||||
.addResourceLocations("classpath:/robots.txt");
|
||||
registry.addResourceHandler("/upload/**")
|
||||
.addResourceLocations("file:///" + System.getProperties().getProperty("user.home") + "/sens/upload/");
|
||||
registry.addResourceHandler("/favicon.ico")
|
||||
.addResourceLocations("classpath:/static/images/favicon.ico");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addCorsMappings(CorsRegistry registry) {
|
||||
registry.addMapping("/**")
|
||||
.allowCredentials(true)
|
||||
.allowedHeaders("*")
|
||||
.allowedOrigins("*")
|
||||
.allowedMethods("*");
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LocaleResolver localeResolver() {
|
||||
SessionLocaleResolver slr = new SessionLocaleResolver();
|
||||
slr.setDefaultLocale(Locale.CHINA);
|
||||
return slr;
|
||||
}
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
package com.example.hotel.config.mybatisplus;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
|
||||
/**
|
||||
* @author 言曌
|
||||
* @date 2018/12/22 下午1:49
|
||||
*/
|
||||
|
||||
@Configuration
|
||||
public class MybatisPlusConfig {
|
||||
|
||||
/***
|
||||
* plus 的性能优化
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public PerformanceInterceptor performanceInterceptor() {
|
||||
PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
|
||||
/*<!-- SQL 执行性能分析,开发环境使用,线上不推荐。 maxTime 指的是 sql 最大执行时长 -->*/
|
||||
performanceInterceptor.setMaxTime(1000);
|
||||
/*<!--SQL是否格式化 默认false-->*/
|
||||
performanceInterceptor.setFormat(true);
|
||||
return performanceInterceptor;
|
||||
}
|
||||
|
||||
/**
|
||||
* mybatis-plus分页插件
|
||||
*/
|
||||
@Bean
|
||||
public PaginationInterceptor paginationInterceptor() {
|
||||
return new PaginationInterceptor();
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
package com.example.hotel.config.properties;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author example
|
||||
*/
|
||||
@Data
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "ignored")
|
||||
public class IgnoredUrlsProperties {
|
||||
|
||||
private List<String> urls = new ArrayList<>();
|
||||
}
|
@ -1,108 +0,0 @@
|
||||
package com.example.hotel.config.shiro;
|
||||
|
||||
import cn.hutool.core.date.DateUnit;
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.lang.Validator;
|
||||
import com.example.hotel.common.constant.CommonConstant;
|
||||
import com.example.hotel.entity.Permission;
|
||||
import com.example.hotel.entity.Role;
|
||||
import com.example.hotel.service.PermissionService;
|
||||
import com.example.hotel.service.RoleService;
|
||||
import com.example.hotel.service.UserService;
|
||||
import com.example.hotel.entity.User;
|
||||
import com.example.hotel.enums.CommonParamsEnum;
|
||||
import com.example.hotel.enums.TrueFalseEnum;
|
||||
import com.example.hotel.enums.UserStatusEnum;
|
||||
import com.example.hotel.util.RegexUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.shiro.authc.*;
|
||||
import org.apache.shiro.authz.AuthorizationInfo;
|
||||
import org.apache.shiro.authz.SimpleAuthorizationInfo;
|
||||
import org.apache.shiro.realm.AuthorizingRealm;
|
||||
import org.apache.shiro.subject.PrincipalCollection;
|
||||
import org.apache.shiro.util.ByteSource;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Slf4j
|
||||
public class MyRealm extends AuthorizingRealm {
|
||||
|
||||
@Autowired
|
||||
@Lazy
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
@Lazy
|
||||
private RoleService roleService;
|
||||
|
||||
@Autowired
|
||||
@Lazy
|
||||
private PermissionService permissionService;
|
||||
|
||||
|
||||
/**
|
||||
* 认证信息(身份验证) Authentication 是用来验证用户身份
|
||||
*/
|
||||
@Override
|
||||
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
|
||||
log.info("认证-->MyShiroRealm.doGetAuthenticationInfo()");
|
||||
//1.验证手机号
|
||||
User user;
|
||||
String account = (String) token.getPrincipal();
|
||||
if (RegexUtil.isIdCard(account)) {
|
||||
user = userService.findByIdCard(account);
|
||||
} else {
|
||||
user = userService.findByUserName(account);
|
||||
}
|
||||
if (user == null) {
|
||||
//用户不存在
|
||||
log.info("用户不存在! 登录名:{}, 密码:{}", account, token.getCredentials());
|
||||
return null;
|
||||
}
|
||||
Role role = roleService.findByUserId(user.getId());
|
||||
if (role != null) {
|
||||
user.setRole(role.getRole());
|
||||
}
|
||||
|
||||
|
||||
//2.判断账号是否被封号
|
||||
if (!Objects.equals(user.getStatus(), UserStatusEnum.NORMAL.getCode())) {
|
||||
throw new LockedAccountException("账号被封禁");
|
||||
}
|
||||
|
||||
//3.封装authenticationInfo,准备验证密码
|
||||
SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
|
||||
user, // 手机号
|
||||
user.getUserPass(), // 密码
|
||||
ByteSource.Util.bytes(CommonConstant.PASSWORD_SALT), // 盐
|
||||
getName() // realm name
|
||||
);
|
||||
System.out.println("realName:" + getName());
|
||||
return authenticationInfo;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
|
||||
|
||||
SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
|
||||
User user = (User) principals.getPrimaryPrincipal();
|
||||
|
||||
Role role = roleService.findByRoleId(user.getId());
|
||||
|
||||
authorizationInfo.addRole(role.getRole());
|
||||
List<Permission> permissions = permissionService.listPermissionsByRoleId(role.getId());
|
||||
//把权限的URL全部放到authorizationInfo中去
|
||||
Set<String> urls = permissions.stream().map(p -> p.getUrl()).collect(Collectors.toSet());
|
||||
authorizationInfo.addStringPermissions(urls);
|
||||
|
||||
return authorizationInfo;
|
||||
}
|
||||
}
|
@ -1,111 +0,0 @@
|
||||
package com.example.hotel.config.shiro;
|
||||
|
||||
import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
|
||||
import com.example.hotel.config.properties.IgnoredUrlsProperties;
|
||||
import org.apache.shiro.authc.credential.AllowAllCredentialsMatcher;
|
||||
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
|
||||
import org.apache.shiro.mgt.SecurityManager;
|
||||
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
|
||||
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.servlet.Filter;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
@Configuration
|
||||
public class ShiroConfig {
|
||||
|
||||
@Bean
|
||||
public ShiroDialect shiroDialect() {
|
||||
return new ShiroDialect();
|
||||
}
|
||||
|
||||
@Bean
|
||||
IgnoredUrlsProperties getIgnoredUrlsProperties() {
|
||||
return new IgnoredUrlsProperties();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
|
||||
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
|
||||
shiroFilterFactoryBean.setSecurityManager(securityManager);
|
||||
//自定义拦截器
|
||||
Map<String, Filter> filtersMap = new LinkedHashMap<String, Filter>();
|
||||
//访问权限配置
|
||||
filtersMap.put("requestURL", getURLPathMatchingFilter());
|
||||
shiroFilterFactoryBean.setFilters(filtersMap);
|
||||
|
||||
//拦截器.
|
||||
Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
|
||||
// 配置不会被拦截的链接 顺序判断
|
||||
List<String> urls = getIgnoredUrlsProperties().getUrls();
|
||||
for (String url : urls) {
|
||||
filterChainDefinitionMap.put(url, "anon");
|
||||
}
|
||||
filterChainDefinitionMap.put("/admin", "authc");
|
||||
filterChainDefinitionMap.put("/admin/**", "requestURL");
|
||||
filterChainDefinitionMap.put("/**", "anon");
|
||||
|
||||
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
|
||||
|
||||
|
||||
// 如果不设置默认会自动寻找Web工程根目录下的"/login"页面
|
||||
shiroFilterFactoryBean.setLoginUrl("/");
|
||||
// 登录成功后要跳转的链接
|
||||
shiroFilterFactoryBean.setSuccessUrl("/");
|
||||
//未授权界面;
|
||||
shiroFilterFactoryBean.setUnauthorizedUrl("/403");
|
||||
|
||||
return shiroFilterFactoryBean;
|
||||
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SecurityManager securityManager() {
|
||||
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
|
||||
securityManager.setRealm(myRealm());
|
||||
return securityManager;
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
public MyRealm myRealm() {
|
||||
MyRealm normalRealm = new MyRealm();
|
||||
normalRealm.setCredentialsMatcher(hashedCredentialsMatcher());
|
||||
return normalRealm;
|
||||
}
|
||||
|
||||
/**
|
||||
* 访问 权限 拦截器
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public URLPathMatchingFilter getURLPathMatchingFilter() {
|
||||
return new URLPathMatchingFilter();
|
||||
}
|
||||
|
||||
/**
|
||||
* MD5加盐加密十次
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public HashedCredentialsMatcher hashedCredentialsMatcher() {
|
||||
HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
|
||||
//散列算法:这里使用MD5算法;
|
||||
hashedCredentialsMatcher.setHashAlgorithmName("md5");
|
||||
//散列的次数,md5("")
|
||||
hashedCredentialsMatcher.setHashIterations(10);
|
||||
return hashedCredentialsMatcher;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public AllowAllCredentialsMatcher allowAllCredentialsMatcher() {
|
||||
return new AllowAllCredentialsMatcher();
|
||||
}
|
||||
|
||||
}
|
@ -1,74 +0,0 @@
|
||||
package com.example.hotel.config.shiro;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.example.hotel.service.PermissionService;
|
||||
import com.example.hotel.util.SpringUtil;
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.apache.shiro.subject.Subject;
|
||||
import org.apache.shiro.web.filter.PathMatchingFilter;
|
||||
import org.apache.shiro.web.util.WebUtils;
|
||||
|
||||
import javax.servlet.ServletRequest;
|
||||
import javax.servlet.ServletResponse;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* URL拦截器
|
||||
*/
|
||||
public class URLPathMatchingFilter extends PathMatchingFilter {
|
||||
|
||||
|
||||
PermissionService permissionService = null;
|
||||
private PermissionService permissionService() {
|
||||
if (permissionService == null) {
|
||||
permissionService = (PermissionService) SpringUtil.getBean("permissionServiceImpl");
|
||||
}
|
||||
return permissionService;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean onPreHandle(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
|
||||
//请求的url
|
||||
String requestURL = getPathWithinApplication(request);
|
||||
System.out.println("请求的url :" + requestURL);
|
||||
Subject subject = SecurityUtils.getSubject();
|
||||
if (!subject.isAuthenticated()) {
|
||||
// 如果没有登录, 进入登录流程
|
||||
WebUtils.issueRedirect(request, response, "/");
|
||||
return false;
|
||||
}
|
||||
|
||||
//从session里读取当前用户的权限URL列表
|
||||
Set<String> urls = (Set<String>) subject.getSession().getAttribute("permissionUrls");
|
||||
if (urls.contains(requestURL)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
//没有权限
|
||||
if (isAjax((HttpServletRequest) request)) {
|
||||
response.setCharacterEncoding("utf-8");
|
||||
response.setContentType("application/json; charset=utf-8");
|
||||
PrintWriter writer = response.getWriter();
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("code", 0);
|
||||
map.put("msg", "没有权限访问");
|
||||
writer.write(JSONObject.toJSONString(map));
|
||||
} else {
|
||||
WebUtils.issueRedirect(request, response, "/403");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public static boolean isAjax(HttpServletRequest httpRequest) {
|
||||
return (httpRequest.getHeader("X-Requested-With") != null
|
||||
&& "XMLHttpRequest"
|
||||
.equals(httpRequest.getHeader("X-Requested-With").toString()));
|
||||
}
|
||||
|
||||
}
|
@ -1,86 +0,0 @@
|
||||
package com.example.hotel.controller.admin;
|
||||
|
||||
import com.example.hotel.entity.Permission;
|
||||
import com.example.hotel.entity.Role;
|
||||
import com.example.hotel.entity.User;
|
||||
import com.example.hotel.service.PermissionService;
|
||||
import com.example.hotel.controller.common.BaseController;
|
||||
import com.example.hotel.dto.JsonResult;
|
||||
import com.example.hotel.service.RoleService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 后台首页控制器
|
||||
* </pre>
|
||||
*/
|
||||
@Slf4j
|
||||
@Controller
|
||||
@RequestMapping(value = "/admin")
|
||||
public class AdminController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
private PermissionService permissionService;
|
||||
|
||||
@Autowired
|
||||
private RoleService roleService;
|
||||
|
||||
/**
|
||||
* 请求后台页面
|
||||
*
|
||||
* @param model model
|
||||
* @return 模板路径admin/admin_index
|
||||
*/
|
||||
@GetMapping
|
||||
public String index(Model model) {
|
||||
// return "admin/admin_index";
|
||||
return "redirect:/admin/order";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获得当前用户的菜单
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
@GetMapping(value = "/currentMenus")
|
||||
@ResponseBody
|
||||
public JsonResult getMenu() {
|
||||
Long userId = getLoginUserId();
|
||||
List<Permission> permissions = permissionService.findPermissionTreeByUserIdAndResourceType(userId, "menu");
|
||||
return JsonResult.success("", permissions);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得当前登录用户
|
||||
*/
|
||||
@GetMapping(value = "/currentUser")
|
||||
@ResponseBody
|
||||
public JsonResult currentUser() {
|
||||
User user = getLoginUser();
|
||||
if (user != null) {
|
||||
return JsonResult.success("", user);
|
||||
}
|
||||
return JsonResult.error("用户未登录");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得当前用户角色编码
|
||||
*/
|
||||
@GetMapping(value = "/currentRole")
|
||||
@ResponseBody
|
||||
public JsonResult currentRole() {
|
||||
Role role = roleService.findByUserId(getLoginUserId());
|
||||
if (role == null) {
|
||||
return JsonResult.error("用户未登录或无角色");
|
||||
}
|
||||
return JsonResult.success("", role.getRole());
|
||||
}
|
||||
|
||||
}
|
@ -1,44 +0,0 @@
|
||||
package com.example.hotel.controller.admin;
|
||||
|
||||
import com.example.hotel.controller.common.BaseController;
|
||||
import com.example.hotel.util.FileUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 后台附件控制器
|
||||
* </pre>
|
||||
*
|
||||
* @author : saysky
|
||||
* @date : 2017/12/19
|
||||
*/
|
||||
@Slf4j
|
||||
@Controller
|
||||
@RequestMapping(value = "/admin/file")
|
||||
public class AttachmentController extends BaseController {
|
||||
|
||||
|
||||
/**
|
||||
* 上传文件
|
||||
*
|
||||
* @param file file
|
||||
* @return Map
|
||||
*/
|
||||
@PostMapping(value = "/upload", produces = {"application/json;charset=UTF-8"})
|
||||
@ResponseBody
|
||||
public Map<String, Object> uploadFile(@RequestParam("file") MultipartFile file) {
|
||||
Map<String, Object> map = new HashMap<>(1);
|
||||
String path = FileUtil.upload(file);
|
||||
map.put("link", path);
|
||||
return map;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,108 +0,0 @@
|
||||
package com.example.hotel.controller.admin;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.example.hotel.controller.common.BaseController;
|
||||
import com.example.hotel.entity.Category;
|
||||
import com.example.hotel.dto.JsonResult;
|
||||
import com.example.hotel.service.CategoryService;
|
||||
import com.example.hotel.util.PageUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 后台分类管理控制器
|
||||
* </pre>
|
||||
*
|
||||
*/
|
||||
@Slf4j
|
||||
@Controller
|
||||
@RequestMapping(value = "/admin/category")
|
||||
public class CategoryController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
private CategoryService categoryService;
|
||||
|
||||
|
||||
/**
|
||||
* 查询所有分类并渲染category页面
|
||||
*
|
||||
* @return 模板路径admin/admin_category
|
||||
*/
|
||||
@GetMapping
|
||||
public String categories(@RequestParam(value = "page", defaultValue = "0") Integer pageNumber,
|
||||
@RequestParam(value = "size", defaultValue = "10") Integer pageSize,
|
||||
@RequestParam(value = "sort", defaultValue = "cateSort") String sort,
|
||||
@RequestParam(value = "order", defaultValue = "desc") String order, Model model) {
|
||||
Page page = PageUtil.initMpPage(pageNumber, pageSize, sort, order);
|
||||
Page<Category> categoryPage = categoryService.findAll(page);
|
||||
model.addAttribute("categories", categoryPage.getRecords());
|
||||
model.addAttribute("pageInfo", PageUtil.convertPageVo(page));
|
||||
return "admin/admin_category";
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增/修改分类目录
|
||||
*
|
||||
* @param category category对象
|
||||
* @return 重定向到/admin/category
|
||||
*/
|
||||
@PostMapping(value = "/save")
|
||||
@ResponseBody
|
||||
public JsonResult saveCategory(@ModelAttribute Category category) {
|
||||
categoryService.insertOrUpdate(category);
|
||||
return JsonResult.success("保存成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除分类
|
||||
*
|
||||
* @param cateId 分类Id
|
||||
* @return JsonResult
|
||||
*/
|
||||
@DeleteMapping(value = "/delete")
|
||||
@ResponseBody
|
||||
public JsonResult checkDelete(@RequestParam("id") Long cateId) {
|
||||
//1.判断这个分类有客房
|
||||
Integer count = categoryService.countPostByCateId(cateId);
|
||||
if (count != 0) {
|
||||
return JsonResult.error("该分类已经有了客房,无法删除");
|
||||
}
|
||||
categoryService.delete(cateId);
|
||||
return JsonResult.success("删除成功");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 跳转到修改页面
|
||||
*
|
||||
* @param cateId cateId
|
||||
* @param model model
|
||||
* @return 模板路径admin/admin_category
|
||||
*/
|
||||
@GetMapping(value = "/edit")
|
||||
public String toEditCategory(Model model,
|
||||
@RequestParam(value = "page", defaultValue = "0") Integer pageNumber,
|
||||
@RequestParam(value = "size", defaultValue = "10") Integer pageSize,
|
||||
@RequestParam(value = "sort", defaultValue = "cateSort") String sort,
|
||||
@RequestParam(value = "order", defaultValue = "desc") String order,
|
||||
@RequestParam("id") Long cateId) {
|
||||
Page page = PageUtil.initMpPage(pageNumber, pageSize, sort, order);
|
||||
|
||||
//更新的分类
|
||||
Category category = categoryService.get(cateId);
|
||||
if (category == null) {
|
||||
return this.renderNotFound();
|
||||
}
|
||||
model.addAttribute("updateCategory", category);
|
||||
|
||||
// 所有分类
|
||||
Page<Category> categoryPage = categoryService.findAll(page);
|
||||
model.addAttribute("categories", categoryPage.getRecords());
|
||||
model.addAttribute("pageInfo", PageUtil.convertPageVo(page));
|
||||
return "admin/admin_category";
|
||||
}
|
||||
}
|
@ -1,170 +0,0 @@
|
||||
package com.example.hotel.controller.admin;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.example.hotel.controller.common.BaseController;
|
||||
import com.example.hotel.dto.JsonResult;
|
||||
import com.example.hotel.dto.QueryCondition;
|
||||
import com.example.hotel.entity.Order;
|
||||
import com.example.hotel.enums.OrderStatusEnum;
|
||||
import com.example.hotel.service.OrderService;
|
||||
import com.example.hotel.service.RecordService;
|
||||
import com.example.hotel.util.DateUtil;
|
||||
import com.example.hotel.util.PageUtil;
|
||||
import com.example.hotel.util.RegexUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.weaver.ast.Or;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 订单管理控制器
|
||||
* </pre>
|
||||
*/
|
||||
@Slf4j
|
||||
@Controller
|
||||
@RequestMapping(value = "/admin/order")
|
||||
public class OrderController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
private OrderService orderService;
|
||||
|
||||
@Autowired
|
||||
private RecordService recordService;
|
||||
|
||||
/**
|
||||
* 查询所有订单并渲染order页面
|
||||
*
|
||||
* @return 模板路径admin/admin_order
|
||||
*/
|
||||
@GetMapping
|
||||
public String orders(@RequestParam(value = "page", defaultValue = "0") Integer pageNumber,
|
||||
@RequestParam(value = "size", defaultValue = "10") Integer pageSize,
|
||||
@RequestParam(value = "sort", defaultValue = "id") String sort,
|
||||
@RequestParam(value = "order", defaultValue = "desc") String order, Model model) {
|
||||
Page page = PageUtil.initMpPage(pageNumber, pageSize, sort, order);
|
||||
Page<Order> orderPage = null;
|
||||
Boolean isCustomer = loginUserIsCustomer();
|
||||
if (isCustomer) {
|
||||
Order orderCondition = new Order();
|
||||
orderCondition.setUserId(getLoginUserId());
|
||||
QueryCondition queryCondition = new QueryCondition();
|
||||
queryCondition.setData(orderCondition);
|
||||
orderPage = orderService.findAll(page, queryCondition);
|
||||
} else {
|
||||
orderPage = orderService.findAll(page);
|
||||
}
|
||||
model.addAttribute("orders", orderPage.getRecords());
|
||||
model.addAttribute("pageInfo", PageUtil.convertPageVo(page));
|
||||
return "admin/admin_order";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除订单
|
||||
*
|
||||
* @param id 订单Id
|
||||
* @return JsonResult
|
||||
*/
|
||||
@DeleteMapping(value = "/delete")
|
||||
@ResponseBody
|
||||
public JsonResult delete(@RequestParam("id") Long id) {
|
||||
Order order = orderService.get(id);
|
||||
if (order == null) {
|
||||
return JsonResult.error("订单不存在");
|
||||
}
|
||||
|
||||
orderService.delete(id);
|
||||
|
||||
|
||||
Long postId = order.getPostId();
|
||||
Long userId = order.getUserId();
|
||||
List<String> dateList = DateUtil.getBetweenDates(order.getStartDate(), order.getQuantity());
|
||||
|
||||
// 释放预定
|
||||
recordService.delete(postId, userId, dateList);
|
||||
return JsonResult.success("删除成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 完结订单
|
||||
*
|
||||
* @param id 订单Id
|
||||
* @return JsonResult
|
||||
*/
|
||||
@PostMapping(value = "/finish")
|
||||
@ResponseBody
|
||||
public JsonResult finish(@RequestParam("id") Long id) {
|
||||
Order order = orderService.get(id);
|
||||
if (order == null) {
|
||||
return JsonResult.error("订单不存在");
|
||||
}
|
||||
|
||||
order.setStatus(OrderStatusEnum.FINISHED.getCode());
|
||||
orderService.update(order);
|
||||
return JsonResult.success("完结成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭订单
|
||||
*
|
||||
* @param id 订单Id
|
||||
* @return JsonResult
|
||||
*/
|
||||
@PostMapping(value = "/close")
|
||||
@ResponseBody
|
||||
@Transactional
|
||||
public JsonResult close(@RequestParam("id") Long id) {
|
||||
// 修改订单状态
|
||||
Order order = orderService.get(id);
|
||||
if (order == null) {
|
||||
return JsonResult.error("订单不存在");
|
||||
}
|
||||
|
||||
order.setStatus(OrderStatusEnum.CLOSED.getCode());
|
||||
orderService.update(order);
|
||||
|
||||
Long postId = order.getPostId();
|
||||
Long userId = order.getUserId();
|
||||
List<String> dateList = DateUtil.getBetweenDates(order.getStartDate(), order.getQuantity());
|
||||
|
||||
// 释放预定
|
||||
recordService.delete(postId, userId, dateList);
|
||||
return JsonResult.success("关闭成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 财务页面
|
||||
*
|
||||
* @param model
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/finance")
|
||||
public String finance(@RequestParam(value = "startDate", required = false) String startDate,
|
||||
@RequestParam(value = "endDate", required = false) String endDate,
|
||||
@RequestParam(value = "page", defaultValue = "0") Integer pageNumber,
|
||||
@RequestParam(value = "size", defaultValue = "10") Integer pageSize,
|
||||
@RequestParam(value = "sort", defaultValue = "id") String sort,
|
||||
@RequestParam(value = "order", defaultValue = "desc") String order,
|
||||
Model model) {
|
||||
|
||||
Page page = PageUtil.initMpPage(pageNumber, pageSize, sort, order);
|
||||
Page<Order> orderPage = orderService.findAll(startDate, endDate, page);
|
||||
model.addAttribute("orders", orderPage.getRecords());
|
||||
model.addAttribute("pageInfo", PageUtil.convertPageVo(page));
|
||||
|
||||
model.addAttribute("startDate", startDate);
|
||||
model.addAttribute("endDate", endDate);
|
||||
|
||||
Integer totalPrice = orderService.getTotalPriceSum(startDate, endDate);
|
||||
model.addAttribute("totalPrice", totalPrice == null ? 0 : totalPrice);
|
||||
return "admin/admin_finance";
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,141 +0,0 @@
|
||||
package com.example.hotel.controller.admin;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.example.hotel.entity.Permission;
|
||||
import com.example.hotel.dto.JsonResult;
|
||||
import com.example.hotel.enums.ResourceTypeEnum;
|
||||
import com.example.hotel.service.PermissionService;
|
||||
import com.example.hotel.util.PageUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 后台权限管理控制器
|
||||
*/
|
||||
@Slf4j
|
||||
@Controller
|
||||
@RequestMapping(value = "/admin/permission")
|
||||
public class PermissionController {
|
||||
|
||||
|
||||
@Autowired
|
||||
private PermissionService permissionService;
|
||||
|
||||
/**
|
||||
* 查询所有权限并渲染permission页面
|
||||
*
|
||||
* @return 模板路径admin/admin_permission
|
||||
*/
|
||||
@GetMapping
|
||||
public String permissions(@RequestParam(value = "page", defaultValue = "1") Integer pageNumber,
|
||||
@RequestParam(value = "size", defaultValue = "10") Integer pageSize,
|
||||
@RequestParam(value = "sort", defaultValue = "id") String sort,
|
||||
@RequestParam(value = "order", defaultValue = "asc") String order, Model model) {
|
||||
//权限列表
|
||||
Page page = PageUtil.initMpPage(pageNumber, pageSize, sort, order);
|
||||
|
||||
Page<Permission> permissions = permissionService.findAll(page);
|
||||
model.addAttribute("permissionList", permissions.getRecords());
|
||||
model.addAttribute("pageInfo", PageUtil.convertPageVo(page));
|
||||
|
||||
// 所有权限
|
||||
model.addAttribute("permissions", getPermissionList());
|
||||
return "admin/admin_permission";
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增/修改权限
|
||||
*
|
||||
* @param permission permission对象
|
||||
* @return 重定向到/admin/permission
|
||||
*/
|
||||
@PostMapping(value = "/save")
|
||||
public String savePermission(@ModelAttribute Permission permission) {
|
||||
permissionService.insertOrUpdate(permission);
|
||||
return "redirect:/admin/permission";
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除权限
|
||||
*
|
||||
* @param permissionId 权限Id
|
||||
* @return JsonResult
|
||||
*/
|
||||
@DeleteMapping(value = "/delete")
|
||||
@ResponseBody
|
||||
public JsonResult checkDelete(@RequestParam("id") Long permissionId) {
|
||||
// // 请先删除子权限
|
||||
Integer childCount = permissionService.countChildPermission(permissionId);
|
||||
if (childCount > 0) {
|
||||
return JsonResult.error("请先删除子节点");
|
||||
}
|
||||
permissionService.delete(permissionId);
|
||||
return JsonResult.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 跳转到新增页面
|
||||
*
|
||||
* @param model model
|
||||
* @return 模板路径admin/admin_permission
|
||||
*/
|
||||
@GetMapping(value = "/new")
|
||||
public String toAddPermission(Model model) {
|
||||
// 带有等级的权限列表
|
||||
model.addAttribute("permissionList", permissionService.findPermissionListWithLevel());
|
||||
// 权限列表
|
||||
model.addAttribute("permissions", getPermissionList());
|
||||
return "admin/admin_permission_new";
|
||||
}
|
||||
|
||||
/**
|
||||
* 跳转到修改页面
|
||||
*
|
||||
* @param permissionId permissionId
|
||||
* @param model model
|
||||
* @return 模板路径admin/admin_permission
|
||||
*/
|
||||
@GetMapping(value = "/edit")
|
||||
public String toEditPermission(Model model, @RequestParam("id") Long permissionId) {
|
||||
//更新的权限
|
||||
Permission permission = permissionService.get(permissionId);
|
||||
model.addAttribute("updatePermission", permission);
|
||||
|
||||
// 带有等级的权限列表
|
||||
model.addAttribute("permissionList", permissionService.findPermissionListWithLevel());
|
||||
// 权限列表
|
||||
model.addAttribute("permissions", getPermissionList());
|
||||
// 设置URL为编辑的URL
|
||||
return "admin/admin_permission_edit";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 所有权限
|
||||
* @return
|
||||
*/
|
||||
public List<Permission> getPermissionList() {
|
||||
//权限列表
|
||||
QueryWrapper queryWrapper = new QueryWrapper();
|
||||
queryWrapper.orderByAsc("sort");
|
||||
List<Permission> permissions = permissionService.findAll(queryWrapper);
|
||||
// 设置URL为编辑的URL
|
||||
for (Permission permission : permissions) {
|
||||
permission.setUrl("/admin/permission/edit?id=" + permission.getId());
|
||||
if (ResourceTypeEnum.MENU.getCode().equals(permission.getResourceType())) {
|
||||
permission.setName(permission.getName() + "[" + ResourceTypeEnum.MENU.getDescription() + "]");
|
||||
} else if (ResourceTypeEnum.BUTTON.getCode().equals(permission.getResourceType())) {
|
||||
permission.setName(permission.getName() + "[" + ResourceTypeEnum.BUTTON.getDescription() + "]");
|
||||
} else if (ResourceTypeEnum.PAGE.getCode().equals(permission.getResourceType())) {
|
||||
permission.setName(permission.getName() + "[" + ResourceTypeEnum.PAGE.getDescription() + "]");
|
||||
}
|
||||
}
|
||||
return permissions;
|
||||
}
|
||||
}
|
@ -1,263 +0,0 @@
|
||||
package com.example.hotel.controller.admin;
|
||||
|
||||
import cn.hutool.http.HtmlUtil;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.example.hotel.controller.common.BaseController;
|
||||
import com.example.hotel.dto.JsonResult;
|
||||
import com.example.hotel.dto.QueryCondition;
|
||||
import com.example.hotel.exception.MyBusinessException;
|
||||
import com.example.hotel.entity.*;
|
||||
import com.example.hotel.enums.*;
|
||||
import com.example.hotel.service.*;
|
||||
import com.example.hotel.util.PageUtil;
|
||||
import com.example.hotel.util.RegexUtil;
|
||||
import com.example.hotel.util.SensUtils;
|
||||
import com.example.hotel.vo.SearchVo;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 后台客房管理控制器
|
||||
* </pre>
|
||||
*
|
||||
* @author : saysky
|
||||
* @date : 2017/12/10
|
||||
*/
|
||||
@Slf4j
|
||||
@Controller
|
||||
@RequestMapping(value = "/admin/post")
|
||||
public class PostController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
private PostService postService;
|
||||
|
||||
@Autowired
|
||||
private CategoryService categoryService;
|
||||
|
||||
public static final String TITLE = "title";
|
||||
|
||||
public static final String CONTENT = "content";
|
||||
|
||||
|
||||
/**
|
||||
* 处理后台获取客房列表的请求
|
||||
*
|
||||
* @param model model
|
||||
* @return 模板路径admin/admin_post
|
||||
*/
|
||||
@GetMapping
|
||||
public String posts(Model model,
|
||||
@RequestParam(value = "status", defaultValue = "0") Integer status,
|
||||
@RequestParam(value = "keywords", defaultValue = "") String keywords,
|
||||
@RequestParam(value = "searchType", defaultValue = "") String searchType,
|
||||
@RequestParam(value = "postSource", defaultValue = "-1") Integer postSource,
|
||||
@RequestParam(value = "page", defaultValue = "1") Integer pageNumber,
|
||||
@RequestParam(value = "size", defaultValue = "10") Integer pageSize,
|
||||
@RequestParam(value = "sort", defaultValue = "createTime") String sort,
|
||||
@RequestParam(value = "order", defaultValue = "desc") String order,
|
||||
@ModelAttribute SearchVo searchVo) {
|
||||
|
||||
Post condition = new Post();
|
||||
if (!StringUtils.isBlank(keywords)) {
|
||||
if (TITLE.equals(searchType)) {
|
||||
condition.setPostTitle(keywords);
|
||||
} else {
|
||||
condition.setPostContent(keywords);
|
||||
}
|
||||
}
|
||||
condition.setPostStatus(status);
|
||||
|
||||
Page page = PageUtil.initMpPage(pageNumber, pageSize, sort, order);
|
||||
Page<Post> posts = postService.findAll(
|
||||
page,
|
||||
new QueryCondition<>(condition, searchVo));
|
||||
|
||||
List<Post> postList = posts.getRecords();
|
||||
for(Post post : postList) {
|
||||
post.setCategory(categoryService.get(post.getCateId()));
|
||||
}
|
||||
//封装分类和标签
|
||||
model.addAttribute("posts", postList);
|
||||
model.addAttribute("pageInfo", PageUtil.convertPageVo(page));
|
||||
model.addAttribute("status", status);
|
||||
model.addAttribute("keywords", keywords);
|
||||
model.addAttribute("searchType", searchType);
|
||||
model.addAttribute("postSource", postSource);
|
||||
model.addAttribute("order", order);
|
||||
model.addAttribute("sort", sort);
|
||||
return "admin/admin_post";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 处理跳转到新建客房页面
|
||||
*
|
||||
* @return 模板路径admin/admin_editor
|
||||
*/
|
||||
@GetMapping(value = "/new")
|
||||
public String newPost(Model model) {
|
||||
//所有分类
|
||||
List<Category> allCategories = categoryService.findAll();
|
||||
model.addAttribute("categories", allCategories);
|
||||
return "admin/admin_post_new";
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 添加/更新客房
|
||||
*
|
||||
* @param post Post实体
|
||||
*/
|
||||
@PostMapping(value = "/save")
|
||||
@ResponseBody
|
||||
public JsonResult pushPost(@ModelAttribute Post post) {
|
||||
// 1、提取摘要
|
||||
int postSummary = 100;
|
||||
//客房摘要
|
||||
String summaryText = HtmlUtil.cleanHtmlTag(post.getPostContent());
|
||||
if (summaryText.length() > postSummary) {
|
||||
String summary = summaryText.substring(0, postSummary);
|
||||
post.setPostSummary(summary);
|
||||
} else {
|
||||
post.setPostSummary(summaryText);
|
||||
}
|
||||
|
||||
// 2.处理imgUrl
|
||||
String postEditor = post.getPostEditor();
|
||||
if(StringUtils.isNotEmpty(postEditor)) {
|
||||
List<String> urlList = RegexUtil.getImgSrc(postEditor);
|
||||
String imgUrl = SensUtils.listToStr(urlList);
|
||||
post.setImgUrl(imgUrl);
|
||||
}
|
||||
|
||||
// 2.添加/更新入库
|
||||
postService.insertOrUpdate(post);
|
||||
return JsonResult.success("发布成功");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 处理移至回收站的请求
|
||||
*
|
||||
* @param postId 客房编号
|
||||
* @return 重定向到/admin/post
|
||||
*/
|
||||
@PostMapping(value = "/throw")
|
||||
@ResponseBody
|
||||
public JsonResult moveToTrash(@RequestParam("id") Long postId) {
|
||||
Post post = postService.get(postId);
|
||||
if (post == null) {
|
||||
throw new MyBusinessException("客房不存在");
|
||||
}
|
||||
post.setPostStatus(PostStatusEnum.RECYCLE.getCode());
|
||||
postService.update(post);
|
||||
return JsonResult.success("操作成功");
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理客房为发布的状态
|
||||
*
|
||||
* @param postId 客房编号
|
||||
* @return 重定向到/admin/post
|
||||
*/
|
||||
@PostMapping(value = "/revert")
|
||||
@ResponseBody
|
||||
public JsonResult moveToPublish(@RequestParam("id") Long postId) {
|
||||
Post post = postService.get(postId);
|
||||
if (post == null) {
|
||||
throw new MyBusinessException("客房不存在");
|
||||
}
|
||||
post.setPostStatus(PostStatusEnum.PUBLISHED.getCode());
|
||||
postService.update(post);
|
||||
return JsonResult.success("操作成功");
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 处理删除客房的请求
|
||||
*
|
||||
* @param postId 客房编号
|
||||
* @return 重定向到/admin/post
|
||||
*/
|
||||
@DeleteMapping(value = "/delete")
|
||||
@ResponseBody
|
||||
public JsonResult removePost(@RequestParam("id") Long postId) {
|
||||
Post post = postService.get(postId);
|
||||
if (post == null) {
|
||||
throw new MyBusinessException("客房不存在");
|
||||
}
|
||||
postService.delete(postId);
|
||||
return JsonResult.success("删除成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除
|
||||
*
|
||||
* @param ids 客房ID列表
|
||||
* @return 重定向到/admin/post
|
||||
*/
|
||||
@DeleteMapping(value = "/batchDelete")
|
||||
@ResponseBody
|
||||
public JsonResult batchDelete(@RequestParam("ids") List<Long> ids) {
|
||||
//批量操作
|
||||
//1、防止恶意操作
|
||||
if (ids == null || ids.size() == 0 || ids.size() >= 100) {
|
||||
return new JsonResult(ResultCodeEnum.FAIL.getCode(), "参数不合法!");
|
||||
}
|
||||
//2、检查用户权限
|
||||
//客房作者才可以删除
|
||||
List<Post> postList = postService.findByBatchIds(ids);
|
||||
//3、如果当前状态为回收站,则删除;否则,移到回收站
|
||||
for (Post post : postList) {
|
||||
if (Objects.equals(post.getPostStatus(), PostStatusEnum.RECYCLE.getCode())) {
|
||||
postService.delete(post.getId());
|
||||
} else {
|
||||
post.setPostStatus(PostStatusEnum.RECYCLE.getCode());
|
||||
postService.update(post);
|
||||
}
|
||||
}
|
||||
return JsonResult.success("删除成功");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 跳转到编辑客房页面
|
||||
*
|
||||
* @param postId 客房编号
|
||||
* @param model model
|
||||
* @return 模板路径admin/admin_editor
|
||||
*/
|
||||
@GetMapping(value = "/edit")
|
||||
public String editPost(@RequestParam("id") Long postId, Model model) {
|
||||
Post post = postService.get(postId);
|
||||
if (post == null) {
|
||||
throw new MyBusinessException("客房不存在");
|
||||
}
|
||||
|
||||
//当前客房分类
|
||||
Category category = categoryService.get(post.getCateId());
|
||||
post.setCategory(category);
|
||||
model.addAttribute("post", post);
|
||||
|
||||
|
||||
//所有分类
|
||||
List<Category> allCategories = categoryService.findAll();
|
||||
model.addAttribute("categories", allCategories);
|
||||
return "admin/admin_post_edit";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
@ -1,95 +0,0 @@
|
||||
package com.example.hotel.controller.admin;
|
||||
|
||||
import com.example.hotel.common.constant.CommonConstant;
|
||||
import com.example.hotel.controller.common.BaseController;
|
||||
import com.example.hotel.entity.User;
|
||||
import com.example.hotel.dto.JsonResult;
|
||||
import com.example.hotel.service.UserService;
|
||||
import com.example.hotel.util.Md5Util;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 后台用户管理控制器
|
||||
*/
|
||||
@Slf4j
|
||||
@Controller
|
||||
@RequestMapping(value = "/admin/user")
|
||||
public class ProfileController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
/**
|
||||
* 获取用户信息并跳转
|
||||
*
|
||||
* @return 模板路径admin/admin_profile
|
||||
*/
|
||||
@GetMapping("/profile")
|
||||
public String profile(Model model) {
|
||||
//1.用户信息
|
||||
User user = getLoginUser();
|
||||
model.addAttribute("user", user);
|
||||
return "admin/admin_profile";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 处理修改用户资料的请求
|
||||
*
|
||||
* @param user user
|
||||
* @return JsonResult
|
||||
*/
|
||||
@PostMapping(value = "/profile/save")
|
||||
@ResponseBody
|
||||
public JsonResult saveProfile(@ModelAttribute User user) {
|
||||
User loginUser = getLoginUser();
|
||||
|
||||
User saveUser = userService.get(loginUser.getId());
|
||||
saveUser.setUserPass(null);
|
||||
saveUser.setId(loginUser.getId());
|
||||
saveUser.setUserName(user.getUserName());
|
||||
saveUser.setUserDisplayName(user.getUserDisplayName());
|
||||
saveUser.setUserAvatar(user.getUserAvatar());
|
||||
saveUser.setUserDesc(user.getUserDesc());
|
||||
saveUser.setIdCard(user.getIdCard());
|
||||
userService.insertOrUpdate(saveUser);
|
||||
return JsonResult.success("资料修改成功,请重新登录");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 处理修改密码的请求
|
||||
*
|
||||
* @param beforePass 旧密码
|
||||
* @param newPass 新密码
|
||||
* @return JsonResult
|
||||
*/
|
||||
@PostMapping(value = "/changePass")
|
||||
@ResponseBody
|
||||
public JsonResult changePass(@ModelAttribute("beforePass") String beforePass,
|
||||
@ModelAttribute("newPass") String newPass) {
|
||||
|
||||
// 1.密码长度是否合法
|
||||
if (newPass.length() > 20 || newPass.length() < 6) {
|
||||
return JsonResult.error("用户密码长度为6-20位!");
|
||||
}
|
||||
|
||||
// 2.比较密码
|
||||
User loginUser = getLoginUser();
|
||||
User user = userService.get(loginUser.getId());
|
||||
if (user != null && Objects.equals(user.getUserPass(), Md5Util.toMd5(beforePass, CommonConstant.PASSWORD_SALT, 10))) {
|
||||
userService.updatePassword(user.getId(), newPass);
|
||||
} else {
|
||||
return JsonResult.error("旧密码错误");
|
||||
}
|
||||
return JsonResult.success("密码重置成功");
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,159 +0,0 @@
|
||||
package com.example.hotel.controller.admin;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.example.hotel.entity.Permission;
|
||||
import com.example.hotel.entity.Role;
|
||||
import com.example.hotel.dto.JsonResult;
|
||||
import com.example.hotel.enums.ResourceTypeEnum;
|
||||
import com.example.hotel.service.PermissionService;
|
||||
import com.example.hotel.service.RoleService;
|
||||
import com.example.hotel.util.PageUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.logging.log4j.util.Strings;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 后台角色管理控制器
|
||||
*/
|
||||
@Slf4j
|
||||
@Controller
|
||||
@RequestMapping(value = "/admin/role")
|
||||
public class RoleController {
|
||||
|
||||
@Autowired
|
||||
private RoleService roleService;
|
||||
|
||||
@Autowired
|
||||
private PermissionService permissionService;
|
||||
|
||||
/**
|
||||
* 查询所有角色并渲染role页面
|
||||
*
|
||||
* @return 模板路径admin/admin_role
|
||||
*/
|
||||
@GetMapping
|
||||
public String roles(@RequestParam(value = "page", defaultValue = "1") Integer pageNumber,
|
||||
@RequestParam(value = "size", defaultValue = "10") Integer pageSize,
|
||||
@RequestParam(value = "sort", defaultValue = "level") String sort,
|
||||
@RequestParam(value = "order", defaultValue = "desc") String order, Model model) {
|
||||
//角色列表
|
||||
Page page = PageUtil.initMpPage(pageNumber, pageSize, sort, order);
|
||||
|
||||
Page<Role> roles = roleService.findAll(page);
|
||||
model.addAttribute("roles", roles.getRecords());
|
||||
model.addAttribute("pageInfo", PageUtil.convertPageVo(page));
|
||||
|
||||
return "admin/admin_role";
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增/修改角色
|
||||
*
|
||||
* @param role role对象
|
||||
* @return 重定向到/admin/role
|
||||
*/
|
||||
@PostMapping(value = "/save")
|
||||
@ResponseBody
|
||||
public JsonResult saveRole(@ModelAttribute Role role,
|
||||
@RequestParam(value = "permissionIds") String permissionIds) {
|
||||
|
||||
if (Strings.isNotEmpty(permissionIds)) {
|
||||
String[] arr = permissionIds.split(",");
|
||||
List<Permission> permissions = new ArrayList<>();
|
||||
for (String permissionId : arr) {
|
||||
Permission permission = new Permission();
|
||||
permission.setId(Long.valueOf(permissionId));
|
||||
permissions.add(permission);
|
||||
}
|
||||
role.setPermissions(permissions);
|
||||
}
|
||||
roleService.insertOrUpdate(role);
|
||||
return JsonResult.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除角色
|
||||
*
|
||||
* @param roleId 角色Id
|
||||
* @return JsonResult
|
||||
*/
|
||||
@DeleteMapping(value = "/delete")
|
||||
@ResponseBody
|
||||
public JsonResult checkDelete(@RequestParam("id") Long roleId) {
|
||||
//判断这个角色有没有用户
|
||||
Integer userCount = roleService.countUserByRoleId(roleId);
|
||||
if (userCount != 0) {
|
||||
return JsonResult.error("当前角色已关联用户,无法删除");
|
||||
}
|
||||
roleService.delete(roleId);
|
||||
return JsonResult.success("删除角色成功");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 添加用户页面
|
||||
*
|
||||
* @return 模板路径admin/admin_edit
|
||||
*/
|
||||
@GetMapping("/new")
|
||||
public String addRole(Model model) {
|
||||
// 所有权限
|
||||
model.addAttribute("permissions", getPermissionList());
|
||||
return "admin/admin_role_add";
|
||||
}
|
||||
|
||||
/**
|
||||
* 跳转到修改页面
|
||||
*
|
||||
* @param roleId roleId
|
||||
* @param model model
|
||||
* @return 模板路径admin/admin_role
|
||||
*/
|
||||
@GetMapping(value = "/edit")
|
||||
public String toEditRole(Model model, @RequestParam("id") Long roleId) {
|
||||
//更新的角色
|
||||
Role role = roleService.findByRoleId(roleId);
|
||||
//当前角色的权限列表
|
||||
role.setPermissions(permissionService.listPermissionsByRoleId(roleId));
|
||||
model.addAttribute("updateRole", role);
|
||||
|
||||
// 所有权限
|
||||
model.addAttribute("permissions", getPermissionList());
|
||||
|
||||
// 当前角色的权限列表
|
||||
List<Long> currentPermissionIds = permissionService.findPermissionByRoleId(roleId).stream().map(p -> p.getId()).collect(Collectors.toList());
|
||||
model.addAttribute("currentPermissionIds", currentPermissionIds);
|
||||
return "admin/admin_role_edit";
|
||||
}
|
||||
|
||||
/**
|
||||
* 所有权限
|
||||
* @return
|
||||
*/
|
||||
public List<Permission> getPermissionList() {
|
||||
//权限列表
|
||||
QueryWrapper queryWrapper = new QueryWrapper();
|
||||
queryWrapper.orderByAsc("sort");
|
||||
List<Permission> permissions = permissionService.findAll(queryWrapper);
|
||||
// 设置URL为编辑的URL
|
||||
for (Permission permission : permissions) {
|
||||
permission.setUrl("/admin/permission/edit?id=" + permission.getId());
|
||||
if (ResourceTypeEnum.MENU.getCode().equals(permission.getResourceType())) {
|
||||
permission.setName(permission.getName() + "[" + ResourceTypeEnum.MENU.getDescription() + "]");
|
||||
} else if (ResourceTypeEnum.BUTTON.getCode().equals(permission.getResourceType())) {
|
||||
permission.setName(permission.getName() + "[" + ResourceTypeEnum.BUTTON.getDescription() + "]");
|
||||
} else if (ResourceTypeEnum.PAGE.getCode().equals(permission.getResourceType())) {
|
||||
permission.setName(permission.getName() + "[" + ResourceTypeEnum.PAGE.getDescription() + "]");
|
||||
}
|
||||
}
|
||||
return permissions;
|
||||
}
|
||||
}
|
@ -1,228 +0,0 @@
|
||||
package com.example.hotel.controller.admin;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.example.hotel.entity.*;
|
||||
import com.example.hotel.enums.RoleEnum;
|
||||
import com.example.hotel.service.*;
|
||||
import com.example.hotel.controller.common.BaseController;
|
||||
import com.example.hotel.dto.JsonResult;
|
||||
import com.example.hotel.util.PageUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 后台用户管理控制器
|
||||
*/
|
||||
@Slf4j
|
||||
@Controller
|
||||
@RequestMapping(value = "/admin/user")
|
||||
public class UserController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private RoleService roleService;
|
||||
|
||||
|
||||
@Autowired
|
||||
private UserRoleRefService userRoleRefService;
|
||||
|
||||
|
||||
public static final String USER_NAME = "userName";
|
||||
public static final String USER_DISPLAY_NAME = "userDisplayName";
|
||||
public static final String EMAIL = "email";
|
||||
|
||||
/**
|
||||
* 查询所有分类并渲染user页面
|
||||
*
|
||||
* @return 模板路径admin/admin_user
|
||||
*/
|
||||
@GetMapping("/customer")
|
||||
public String customers(
|
||||
@RequestParam(value = "status", defaultValue = "0") Integer status,
|
||||
@RequestParam(value = "keywords", defaultValue = "") String keywords,
|
||||
@RequestParam(value = "searchType", defaultValue = "") String searchType,
|
||||
@RequestParam(value = "page", defaultValue = "1") Integer pageNumber,
|
||||
@RequestParam(value = "size", defaultValue = "10") Integer pageSize,
|
||||
@RequestParam(value = "sort", defaultValue = "createTime") String sort,
|
||||
@RequestParam(value = "order", defaultValue = "desc") String order, Model model) {
|
||||
//用户列表
|
||||
Page page = PageUtil.initMpPage(pageNumber, pageSize, sort, order);
|
||||
User condition = new User();
|
||||
condition.setStatus(status);
|
||||
if (!StringUtils.isBlank(keywords)) {
|
||||
if (USER_NAME.equals(searchType)) {
|
||||
condition.setUserName(keywords);
|
||||
} else if (USER_DISPLAY_NAME.equals(searchType)) {
|
||||
condition.setUserDisplayName(keywords);
|
||||
} else if (EMAIL.equals(searchType)) {
|
||||
condition.setIdCard(keywords);
|
||||
}
|
||||
}
|
||||
String role = RoleEnum.CUSTOMER.getValue();
|
||||
Page<User> users = userService.findByRoleAndCondition(role, condition, page);
|
||||
|
||||
//角色列表
|
||||
Integer maxLevel = roleService.findMaxLevelByUserId(getLoginUserId());
|
||||
List<Role> roles = roleService.findByLessThanLevel(maxLevel);
|
||||
model.addAttribute("roles", roles);
|
||||
model.addAttribute("users", users.getRecords());
|
||||
model.addAttribute("pageInfo", PageUtil.convertPageVo(page));
|
||||
model.addAttribute("status", status);
|
||||
model.addAttribute("keywords", keywords);
|
||||
model.addAttribute("searchType", searchType);
|
||||
model.addAttribute("sort", sort);
|
||||
model.addAttribute("order", order);
|
||||
model.addAttribute("currentRole", role);
|
||||
return "admin/admin_user";
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询所有分类并渲染user页面
|
||||
*
|
||||
* @return 模板路径admin/admin_user
|
||||
*/
|
||||
@GetMapping("/worker")
|
||||
public String works(
|
||||
@RequestParam(value = "status", defaultValue = "0") Integer status,
|
||||
@RequestParam(value = "keywords", defaultValue = "") String keywords,
|
||||
@RequestParam(value = "searchType", defaultValue = "") String searchType,
|
||||
@RequestParam(value = "page", defaultValue = "1") Integer pageNumber,
|
||||
@RequestParam(value = "size", defaultValue = "10") Integer pageSize,
|
||||
@RequestParam(value = "sort", defaultValue = "createTime") String sort,
|
||||
@RequestParam(value = "order", defaultValue = "desc") String order, Model model) {
|
||||
//用户列表
|
||||
Page page = PageUtil.initMpPage(pageNumber, pageSize, sort, order);
|
||||
User condition = new User();
|
||||
condition.setStatus(status);
|
||||
if (!StringUtils.isBlank(keywords)) {
|
||||
if (USER_NAME.equals(searchType)) {
|
||||
condition.setUserName(keywords);
|
||||
} else if (USER_DISPLAY_NAME.equals(searchType)) {
|
||||
condition.setUserDisplayName(keywords);
|
||||
} else if (EMAIL.equals(searchType)) {
|
||||
condition.setIdCard(keywords);
|
||||
}
|
||||
}
|
||||
String role = RoleEnum.WORKER.getValue();
|
||||
Page<User> users = userService.findByRoleAndCondition(role, condition, page);
|
||||
|
||||
//角色列表
|
||||
Integer maxLevel = roleService.findMaxLevelByUserId(getLoginUserId());
|
||||
List<Role> roles = roleService.findByLessThanLevel(maxLevel);
|
||||
model.addAttribute("roles", roles);
|
||||
model.addAttribute("users", users.getRecords());
|
||||
model.addAttribute("pageInfo", PageUtil.convertPageVo(page));
|
||||
model.addAttribute("status", status);
|
||||
model.addAttribute("keywords", keywords);
|
||||
model.addAttribute("searchType", searchType);
|
||||
model.addAttribute("sort", sort);
|
||||
model.addAttribute("order", order);
|
||||
model.addAttribute("currentRole", role);
|
||||
return "admin/admin_user";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除用户
|
||||
*
|
||||
* @param userId 用户Id
|
||||
* @return JsonResult
|
||||
*/
|
||||
@DeleteMapping(value = "/delete")
|
||||
@ResponseBody
|
||||
public JsonResult removeUser(@RequestParam("id") Long userId) {
|
||||
userService.delete(userId);
|
||||
return JsonResult.success("删除成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加用户页面
|
||||
*
|
||||
* @return 模板路径admin/admin_edit
|
||||
*/
|
||||
@GetMapping("/new")
|
||||
public String addUser(Model model) {
|
||||
|
||||
//角色列表
|
||||
List<Role> roles = roleService.findAll();
|
||||
model.addAttribute("roles", roles);
|
||||
|
||||
return "admin/admin_user_add";
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑用户页面
|
||||
*
|
||||
* @return 模板路径admin/admin_edit
|
||||
*/
|
||||
@GetMapping("/edit")
|
||||
public String edit(@RequestParam("id") Long userId, Model model) {
|
||||
User user = userService.get(userId);
|
||||
if (user != null) {
|
||||
model.addAttribute("user", user);
|
||||
//该用户的角色
|
||||
Role currentRole = roleService.findByUserId(userId);
|
||||
model.addAttribute("currentRole", currentRole);
|
||||
|
||||
//角色列表
|
||||
List<Role> roles = roleService.findAll();
|
||||
model.addAttribute("roles", roles);
|
||||
|
||||
|
||||
return "admin/admin_user_edit";
|
||||
}
|
||||
return this.renderNotFound();
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除
|
||||
*
|
||||
* @param ids 用户ID列表
|
||||
* @return
|
||||
*/
|
||||
@DeleteMapping(value = "/batchDelete")
|
||||
@ResponseBody
|
||||
public JsonResult batchDelete(@RequestParam("ids") List<Long> ids) {
|
||||
//批量操作
|
||||
if (ids == null || ids.size() == 0 || ids.size() >= 100) {
|
||||
return JsonResult.error("参数不合法!");
|
||||
}
|
||||
List<User> userList = userService.findByBatchIds(ids);
|
||||
for (User user : userList) {
|
||||
userService.delete(user.getId());
|
||||
}
|
||||
return JsonResult.success("删除成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增/修改用户
|
||||
*
|
||||
* @param user user
|
||||
* @return 重定向到/admin/user
|
||||
*/
|
||||
@PostMapping(value = "/save")
|
||||
@ResponseBody
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public JsonResult saveUser(@ModelAttribute User user,
|
||||
@RequestParam(value = "roleId", required = false) Long roleId) {
|
||||
// 1.添加用户
|
||||
userService.insertOrUpdate(user);
|
||||
if(roleId != null) {
|
||||
// 2.先删除该用户的角色关联
|
||||
userRoleRefService.deleteByUserId(user.getId());
|
||||
// 3.添加角色关联
|
||||
userRoleRefService.insert(new UserRoleRef(user.getId(), roleId));
|
||||
}
|
||||
return JsonResult.success("保存成功");
|
||||
}
|
||||
|
||||
}
|
@ -1,95 +0,0 @@
|
||||
package com.example.hotel.controller.common;
|
||||
|
||||
import com.example.hotel.entity.User;
|
||||
import com.example.hotel.enums.RoleEnum;
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.apache.shiro.subject.Subject;
|
||||
|
||||
/**
|
||||
* Controller抽象类
|
||||
*/
|
||||
public abstract class BaseController {
|
||||
|
||||
/**
|
||||
* 渲染404页面
|
||||
*
|
||||
* @return redirect:/404
|
||||
*/
|
||||
public String renderNotFound() {
|
||||
return "forward:/404";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 渲染404页面
|
||||
*
|
||||
* @return redirect:/404
|
||||
*/
|
||||
public String renderNotAllowAccess() {
|
||||
return "redirect:/403";
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前登录用户
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public User getLoginUser() {
|
||||
Subject subject = SecurityUtils.getSubject();
|
||||
if (subject.isAuthenticated()) {
|
||||
return (User) subject.getPrincipal();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前用户ID
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Long getLoginUserId() {
|
||||
return getLoginUser().getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前用户是管理员
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Boolean loginUserIsAdmin() {
|
||||
User loginUser = getLoginUser();
|
||||
if (loginUser != null) {
|
||||
return RoleEnum.ADMIN.getValue().equalsIgnoreCase(loginUser.getRole());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前用户是工作人员
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Boolean loginUserIsWorker() {
|
||||
User loginUser = getLoginUser();
|
||||
if (loginUser != null) {
|
||||
return RoleEnum.WORKER.getValue().equalsIgnoreCase(loginUser.getRole());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 当前用户是消费者
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Boolean loginUserIsCustomer() {
|
||||
User loginUser = getLoginUser();
|
||||
if (loginUser != null) {
|
||||
return RoleEnum.CUSTOMER.getValue().equalsIgnoreCase(loginUser.getRole());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,76 +0,0 @@
|
||||
package com.example.hotel.controller.common;
|
||||
|
||||
import com.example.hotel.enums.CommonParamsEnum;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.web.servlet.error.ErrorController;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* 错误页面控制器
|
||||
*/
|
||||
@Slf4j
|
||||
@Controller
|
||||
public class CommonController implements ErrorController {
|
||||
|
||||
|
||||
/**
|
||||
* 渲染404,500
|
||||
*
|
||||
* @param request request
|
||||
* @return String
|
||||
*/
|
||||
@GetMapping(value = "/error")
|
||||
public String handleError(HttpServletRequest request) {
|
||||
Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
|
||||
if (statusCode.equals(CommonParamsEnum.NOT_FOUND.getValue())) {
|
||||
return "redirect:/404";
|
||||
} else {
|
||||
return "redirect:/500";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 渲染403页面
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
@GetMapping(value = "/403")
|
||||
public String fourZeroThree() {
|
||||
return "common/error/403";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 渲染404页面
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
@GetMapping(value = "/404")
|
||||
public String fourZeroFour() {
|
||||
return "common/error/404";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 渲染500页面
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
@GetMapping(value = "/500")
|
||||
public String fiveZeroZero() {
|
||||
return "common/error/500";
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the path of the error page.
|
||||
*
|
||||
* @return the error path
|
||||
*/
|
||||
@Override
|
||||
public String getErrorPath() {
|
||||
return "/error";
|
||||
}
|
||||
}
|
@ -1,84 +0,0 @@
|
||||
package com.example.hotel.controller.home;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.example.hotel.controller.common.BaseController;
|
||||
import com.example.hotel.dto.PostQueryCondition;
|
||||
import com.example.hotel.entity.Category;
|
||||
import com.example.hotel.entity.Post;
|
||||
import com.example.hotel.service.CategoryService;
|
||||
import com.example.hotel.service.PostService;
|
||||
import com.example.hotel.util.PageUtil;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author 言曌
|
||||
* @date 2020/3/11 4:59 下午
|
||||
*/
|
||||
@Controller
|
||||
public class FrontCategoryController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
private PostService postService;
|
||||
|
||||
@Autowired
|
||||
private CategoryService categoryService;
|
||||
|
||||
/**
|
||||
* 分类列表
|
||||
*
|
||||
* @param model
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/category")
|
||||
public String category(@RequestParam(value = "keywords", required = false) String keywords,
|
||||
Model model) {
|
||||
QueryWrapper queryWrapper = new QueryWrapper();
|
||||
queryWrapper.orderByDesc("cate_sort");
|
||||
if (StringUtils.isNotEmpty(keywords)) {
|
||||
queryWrapper.like("cate_name", keywords);
|
||||
}
|
||||
List<Category> categories = categoryService.findAll(queryWrapper);
|
||||
model.addAttribute("categories", categories);
|
||||
return "home/category";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 分类对应的帖子列表
|
||||
*
|
||||
* @param model
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/category/{id}")
|
||||
public String index(@PathVariable("id") Long cateId,
|
||||
@RequestParam(value = "page", defaultValue = "1") Integer pageNumber,
|
||||
@RequestParam(value = "size", defaultValue = "10") Integer pageSize,
|
||||
@RequestParam(value = "sort", defaultValue = "createTime") String sort,
|
||||
@RequestParam(value = "order", defaultValue = "desc") String order,
|
||||
Model model) {
|
||||
|
||||
Category category = categoryService.get(cateId);
|
||||
if(category == null) {
|
||||
return renderNotFound();
|
||||
}
|
||||
|
||||
Page page = PageUtil.initMpPage(pageNumber, pageSize, sort, order);
|
||||
PostQueryCondition condition = new PostQueryCondition();
|
||||
condition.setCateId(cateId);
|
||||
Page<Post> postPage = postService.findPostByCondition(condition, page);
|
||||
model.addAttribute("posts", postPage);
|
||||
model.addAttribute("category", category);
|
||||
return "home/category_post";
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,342 +0,0 @@
|
||||
package com.example.hotel.controller.home;
|
||||
|
||||
import com.example.hotel.controller.common.BaseController;
|
||||
import com.example.hotel.dto.JsonResult;
|
||||
import com.example.hotel.entity.*;
|
||||
import com.example.hotel.enums.OrderStatusEnum;
|
||||
import com.example.hotel.service.*;
|
||||
import com.example.hotel.util.DateUtil;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author 言曌
|
||||
* @date 2020/3/11 4:59 下午
|
||||
*/
|
||||
@Controller
|
||||
public class FrontPostController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
private CategoryService categoryService;
|
||||
|
||||
@Autowired
|
||||
private PostService postService;
|
||||
|
||||
@Autowired
|
||||
private OrderService orderService;
|
||||
|
||||
@Autowired
|
||||
private RecordService recordService;
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
/**
|
||||
* 帖子详情
|
||||
*
|
||||
* @param id
|
||||
* @param model
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/post/{id}")
|
||||
public String postDetails(@PathVariable("id") Long id,
|
||||
@RequestParam(value = "startDate", required = false) String start,
|
||||
@RequestParam(value = "quantity", defaultValue = "1") Integer quantity,
|
||||
Model model) {
|
||||
DateFormat dateFormat = new SimpleDateFormat(DateUtil.FORMAT);
|
||||
|
||||
if (quantity == null || quantity < 1) {
|
||||
quantity = 1;
|
||||
}
|
||||
|
||||
Date today = new Date();
|
||||
|
||||
// 判断入住日期是否合法
|
||||
if (StringUtils.isEmpty(start)) {
|
||||
start = dateFormat.format(today);
|
||||
} else {
|
||||
try {
|
||||
Date startDate = dateFormat.parse(start);
|
||||
if (startDate.before(today)) {
|
||||
start = dateFormat.format(today);
|
||||
}
|
||||
} catch (ParseException e) {
|
||||
start = dateFormat.format(today);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// 客房
|
||||
Post post = postService.get(id);
|
||||
if (post == null) {
|
||||
return renderNotFound();
|
||||
}
|
||||
// 分类
|
||||
Category category = categoryService.get(post.getCateId());
|
||||
post.setCategory(category);
|
||||
model.addAttribute("post", post);
|
||||
|
||||
String[] imgUrlList = post.getImgUrl().split(",");
|
||||
model.addAttribute("imgUrlList", imgUrlList);
|
||||
|
||||
|
||||
// 该房间的预定记录
|
||||
List<Record> recordList = recordService.findByPostId(id);
|
||||
model.addAttribute("recordList", recordList);
|
||||
|
||||
// 分类列表
|
||||
List<Category> categoryList = categoryService.findAll();
|
||||
model.addAttribute("categoryList", categoryList);
|
||||
|
||||
model.addAttribute("startDate", start);
|
||||
model.addAttribute("quantity", quantity);
|
||||
return "home/post";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 结算页面
|
||||
*
|
||||
* @param postId
|
||||
* @param start
|
||||
* @param quantity
|
||||
* @param model
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/checkout")
|
||||
public String checkout(@RequestParam("postId") Long postId,
|
||||
@RequestParam(value = "startDate", required = false) String start,
|
||||
@RequestParam(value = "quantity", defaultValue = "1") Integer quantity,
|
||||
Model model) {
|
||||
DateFormat dateFormat = new SimpleDateFormat(DateUtil.FORMAT);
|
||||
|
||||
if (quantity == null || quantity < 1) {
|
||||
quantity = 1;
|
||||
}
|
||||
|
||||
Date today = new Date();
|
||||
|
||||
// 判断入住日期是否合法
|
||||
if (StringUtils.isEmpty(start)) {
|
||||
start = dateFormat.format(today);
|
||||
} else {
|
||||
try {
|
||||
Date startDate = dateFormat.parse(start);
|
||||
if (startDate.before(today)) {
|
||||
start = dateFormat.format(today);
|
||||
}
|
||||
} catch (ParseException e) {
|
||||
start = dateFormat.format(today);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
Post post = postService.get(postId);
|
||||
if (post == null) {
|
||||
return this.renderNotFound();
|
||||
}
|
||||
|
||||
User user = getLoginUser();
|
||||
if (user == null) {
|
||||
return "redirect:/";
|
||||
}
|
||||
|
||||
// 分类列表
|
||||
List<Category> categoryList = categoryService.findAll();
|
||||
model.addAttribute("categoryList", categoryList);
|
||||
|
||||
model.addAttribute("post", post);
|
||||
model.addAttribute("startDate", start);
|
||||
model.addAttribute("quantity", quantity);
|
||||
model.addAttribute("user", user);
|
||||
return "home/checkout";
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建订单
|
||||
*
|
||||
* @param postId
|
||||
* @param start
|
||||
* @param quantity
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/checkOrder")
|
||||
@ResponseBody
|
||||
public JsonResult checkOrder(@RequestParam(value = "postId") Long postId,
|
||||
@RequestParam(value = "startDate") String start,
|
||||
@RequestParam(value = "quantity") Integer quantity) {
|
||||
User user = getLoginUser();
|
||||
if (user == null) {
|
||||
return JsonResult.error("请先登录");
|
||||
}
|
||||
|
||||
Post post = postService.get(postId);
|
||||
if (post == null) {
|
||||
return JsonResult.error("客房不存在");
|
||||
}
|
||||
|
||||
DateFormat dateFormat = new SimpleDateFormat(DateUtil.FORMAT);
|
||||
|
||||
if (quantity == null || quantity < 1 || quantity > 7) {
|
||||
return JsonResult.error("天数不合法");
|
||||
}
|
||||
|
||||
Date today = new Date();
|
||||
|
||||
// 判断入住日期是否合法
|
||||
if (StringUtils.isEmpty(start)) {
|
||||
start = dateFormat.format(today);
|
||||
} else {
|
||||
try {
|
||||
Date startDate = dateFormat.parse(start);
|
||||
if (startDate.before(today) && !Objects.equals(start, dateFormat.format(today))) {
|
||||
return JsonResult.error("不能预定过去的日期");
|
||||
}
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
return JsonResult.error("预定日期格式不合法");
|
||||
}
|
||||
}
|
||||
|
||||
// 查询日期列表
|
||||
List<String> dateList = DateUtil.getBetweenDates(start, quantity);
|
||||
// 判断客房是否可以预定
|
||||
List<Record> recordList = recordService.findByPostIdAndRecordDate(postId, dateList);
|
||||
if (recordList.size() > 0) {
|
||||
return JsonResult.error("房间已被人预定,请重新选择房间和日期");
|
||||
}
|
||||
return JsonResult.success("可以预定");
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建订单
|
||||
*
|
||||
* @param postId
|
||||
* @param start
|
||||
* @param quantity
|
||||
* @param userName
|
||||
* @param userDisplayName
|
||||
* @param idCard
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/order")
|
||||
@Transactional
|
||||
@ResponseBody
|
||||
public JsonResult addOrder(@RequestParam(value = "postId") Long postId,
|
||||
@RequestParam(value = "startDate") String start,
|
||||
@RequestParam(value = "quantity") Integer quantity,
|
||||
@RequestParam(value = "userName") String userName,
|
||||
@RequestParam(value = "userDisplayName") String userDisplayName,
|
||||
@RequestParam(value = "idCard") String idCard) {
|
||||
User user = getLoginUser();
|
||||
if (user == null) {
|
||||
return JsonResult.error("请先登录");
|
||||
}
|
||||
|
||||
Post post = postService.get(postId);
|
||||
if (post == null) {
|
||||
return JsonResult.error("客房不存在");
|
||||
}
|
||||
|
||||
DateFormat dateFormat = new SimpleDateFormat(DateUtil.FORMAT);
|
||||
|
||||
if (quantity == null || quantity < 1 || quantity > 7) {
|
||||
return JsonResult.error("天数不合法");
|
||||
}
|
||||
|
||||
Date today = new Date();
|
||||
|
||||
// 判断入住日期是否合法
|
||||
if (StringUtils.isEmpty(start)) {
|
||||
start = dateFormat.format(today);
|
||||
} else {
|
||||
try {
|
||||
Date startDate = dateFormat.parse(start);
|
||||
if (startDate.before(today) && !Objects.equals(start, dateFormat.format(today))) {
|
||||
return JsonResult.error("不能预定过去的日期");
|
||||
}
|
||||
} catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
return JsonResult.error("预定日期格式不合法");
|
||||
}
|
||||
}
|
||||
|
||||
// 查询日期列表
|
||||
List<String> dateList = DateUtil.getBetweenDates(start, quantity);
|
||||
// 判断客房是否可以预定
|
||||
List<Record> recordList = recordService.findByPostIdAndRecordDate(postId, dateList);
|
||||
if (recordList.size() > 0) {
|
||||
return JsonResult.error("房间已被人预定,请重新选择房间和日期");
|
||||
}
|
||||
|
||||
// 支付省略
|
||||
// 添加订单
|
||||
Order order = new Order();
|
||||
order.setPostId(postId);
|
||||
order.setQuantity(quantity);
|
||||
order.setStartDate(start);
|
||||
order.setName(userDisplayName);
|
||||
order.setPhone(userName);
|
||||
order.setIdCard(idCard);
|
||||
order.setUserId(user.getId());
|
||||
order.setStatus(OrderStatusEnum.HAS_PAY.getCode());
|
||||
order.setPostTitle(post.getPostTitle());
|
||||
order.setPostNumber(post.getNumber());
|
||||
order.setPrice(post.getPrice());
|
||||
order.setTotalPrice(post.getPrice() * quantity);
|
||||
order.setCreateTime(new Date());
|
||||
order.setUpdateTime(new Date());
|
||||
orderService.insert(order);
|
||||
|
||||
// 添加预定记录
|
||||
for (String recordDate : dateList) {
|
||||
Record record = new Record();
|
||||
record.setPostId(postId);
|
||||
record.setUserId(user.getId());
|
||||
record.setRecordDate(recordDate);
|
||||
recordService.insert(record);
|
||||
}
|
||||
return JsonResult.success("预定成功", order.getId());
|
||||
}
|
||||
|
||||
@GetMapping("/order/{id}")
|
||||
public String order(@PathVariable("id") Long id, Model model) {
|
||||
Order order = orderService.get(id);
|
||||
if (order == null) {
|
||||
return this.renderNotFound();
|
||||
}
|
||||
|
||||
User user = getLoginUser();
|
||||
if (user == null) {
|
||||
return "redirect:/";
|
||||
}
|
||||
|
||||
Boolean isCustomer = loginUserIsCustomer();
|
||||
if (!Objects.equals(order.getUserId(), user.getId()) && isCustomer) {
|
||||
return this.renderNotAllowAccess();
|
||||
}
|
||||
model.addAttribute("order", order);
|
||||
|
||||
|
||||
// 分类列表
|
||||
List<Category> categoryList = categoryService.findAll();
|
||||
model.addAttribute("categoryList", categoryList);
|
||||
|
||||
model.addAttribute("user", userService.get(order.getUserId()));
|
||||
return "home/order";
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -1,92 +0,0 @@
|
||||
package com.example.hotel.controller.home;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.example.hotel.controller.common.BaseController;
|
||||
import com.example.hotel.dto.PostQueryCondition;
|
||||
import com.example.hotel.entity.*;
|
||||
import com.example.hotel.exception.MyBusinessException;
|
||||
import com.example.hotel.service.*;
|
||||
import com.example.hotel.util.DateUtil;
|
||||
import com.example.hotel.util.PageUtil;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author 言曌
|
||||
* @date 2020/3/9 11:00 上午
|
||||
*/
|
||||
|
||||
@Controller
|
||||
public class IndexController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
private PostService postService;
|
||||
|
||||
@Autowired
|
||||
private CategoryService categoryService;
|
||||
|
||||
|
||||
@GetMapping("/")
|
||||
public String index(@RequestParam(value = "page", defaultValue = "1") Integer pageNumber,
|
||||
@RequestParam(value = "size", defaultValue = "9") Integer pageSize,
|
||||
@RequestParam(value = "sort", defaultValue = "createTime") String sort,
|
||||
@RequestParam(value = "order", defaultValue = "desc") String order,
|
||||
@RequestParam(value = "startDate", required = false) String start,
|
||||
@RequestParam(value = "quantity", defaultValue = "1") Integer quantity,
|
||||
@RequestParam(value = "cateId", defaultValue = "0") Long cateId,
|
||||
Model model) {
|
||||
DateFormat dateFormat = new SimpleDateFormat(DateUtil.FORMAT);
|
||||
|
||||
if (quantity == null || quantity < 1) {
|
||||
quantity = 1;
|
||||
}
|
||||
|
||||
Date today = new Date();
|
||||
|
||||
// 判断入住日期是否合法
|
||||
if (StringUtils.isEmpty(start)) {
|
||||
start = dateFormat.format(today);
|
||||
} else {
|
||||
try {
|
||||
Date startDate = dateFormat.parse(start);
|
||||
if (startDate.before(today)) {
|
||||
start = dateFormat.format(today);
|
||||
}
|
||||
} catch (ParseException e) {
|
||||
start = dateFormat.format(today);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
PostQueryCondition condition = new PostQueryCondition();
|
||||
// 查询日期列表
|
||||
List<String> dateList = DateUtil.getBetweenDates(start, quantity);
|
||||
condition.setDateList(dateList);
|
||||
condition.setCateId(cateId);
|
||||
Page page = PageUtil.initMpPage(pageNumber, pageSize, sort, order);
|
||||
Page<Post> postPage = postService.findPostByCondition(condition, page);
|
||||
model.addAttribute("posts", postPage);
|
||||
|
||||
// 分类列表
|
||||
List<Category> categoryList = categoryService.findAll();
|
||||
model.addAttribute("categoryList", categoryList);
|
||||
model.addAttribute("quantity", quantity);
|
||||
model.addAttribute("startDate", start);
|
||||
model.addAttribute("cateId", cateId);
|
||||
return "home/index";
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,194 +0,0 @@
|
||||
package com.example.hotel.controller.home;
|
||||
|
||||
import cn.hutool.core.lang.Validator;
|
||||
import com.example.hotel.common.constant.CommonConstant;
|
||||
import com.example.hotel.controller.common.BaseController;
|
||||
import com.example.hotel.entity.Role;
|
||||
import com.example.hotel.entity.User;
|
||||
import com.example.hotel.entity.UserRoleRef;
|
||||
import com.example.hotel.dto.JsonResult;
|
||||
import com.example.hotel.enums.CommonParamsEnum;
|
||||
import com.example.hotel.enums.TrueFalseEnum;
|
||||
import com.example.hotel.enums.UserStatusEnum;
|
||||
import com.example.hotel.service.*;
|
||||
import com.example.hotel.util.Md5Util;
|
||||
import com.example.hotel.util.RegexUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.RandomStringUtils;
|
||||
import org.apache.commons.lang3.RandomUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.shiro.SecurityUtils;
|
||||
import org.apache.shiro.authc.IncorrectCredentialsException;
|
||||
import org.apache.shiro.authc.LockedAccountException;
|
||||
import org.apache.shiro.authc.UnknownAccountException;
|
||||
import org.apache.shiro.authc.UsernamePasswordToken;
|
||||
import org.apache.shiro.subject.Subject;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.mail.MessagingException;
|
||||
import java.util.Set;
|
||||
|
||||
@Controller
|
||||
@Slf4j
|
||||
public class LoginController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
private PermissionService permissionService;
|
||||
|
||||
@Autowired
|
||||
private UserService userService;
|
||||
|
||||
@Autowired
|
||||
private RoleService roleService;
|
||||
|
||||
@Autowired
|
||||
private UserRoleRefService userRoleRefService;
|
||||
|
||||
/**
|
||||
* 验证登录信息
|
||||
*
|
||||
* @param userName 登录名:身份证号码/手机号
|
||||
* @param userPass password 密码
|
||||
* @return JsonResult JsonResult
|
||||
*/
|
||||
@PostMapping(value = "/login")
|
||||
@ResponseBody
|
||||
public JsonResult getLogin(@RequestParam("userName") String userName,
|
||||
@RequestParam("userPass") String userPass) {
|
||||
Subject subject = SecurityUtils.getSubject();
|
||||
UsernamePasswordToken token = new UsernamePasswordToken(userName, userPass);
|
||||
try {
|
||||
subject.login(token);
|
||||
if (subject.isAuthenticated()) {
|
||||
//登录成功,修改登录错误次数为0
|
||||
User user = (User) subject.getPrincipal();
|
||||
Set<String> permissionUrls = permissionService.findPermissionUrlsByUserId(user.getId());
|
||||
subject.getSession().setAttribute("permissionUrls", permissionUrls);
|
||||
return JsonResult.success("登录成功");
|
||||
}
|
||||
} catch (UnknownAccountException e) {
|
||||
return JsonResult.error("账号不存在");
|
||||
} catch (IncorrectCredentialsException e) {
|
||||
e.printStackTrace();
|
||||
return JsonResult.error("账号或密码错误");
|
||||
} catch (LockedAccountException e) {
|
||||
return JsonResult.error(e.getMessage());
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
log.info(e.getMessage());
|
||||
}
|
||||
return JsonResult.error("服务器内部错误");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 退出登录
|
||||
*
|
||||
* @return 重定向到/login
|
||||
*/
|
||||
@GetMapping(value = "/logout")
|
||||
public String logOut() {
|
||||
Subject subject = SecurityUtils.getSubject();
|
||||
subject.logout();
|
||||
return "redirect:/";
|
||||
}
|
||||
|
||||
/**
|
||||
* 退出登录
|
||||
*
|
||||
* @return 重定向到/login
|
||||
*/
|
||||
@PostMapping(value = "/logout")
|
||||
@ResponseBody
|
||||
public JsonResult ajaxLogOut() {
|
||||
Subject subject = SecurityUtils.getSubject();
|
||||
subject.logout();
|
||||
return JsonResult.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证注册信息
|
||||
*
|
||||
* @param userName 手机号
|
||||
* @param idCard 身份证号码
|
||||
* @return JsonResult JsonResult
|
||||
*/
|
||||
@PostMapping(value = "/register")
|
||||
@ResponseBody
|
||||
@Transactional
|
||||
public JsonResult getRegister(@RequestParam("userName") String userName,
|
||||
@RequestParam("userPass") String userPass,
|
||||
@RequestParam("idCard") String idCard,
|
||||
@RequestParam("userDisplayName") String userDisplayName) {
|
||||
// 1.校验是否输入完整
|
||||
if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(userPass) || StringUtils.isEmpty(idCard)) {
|
||||
return JsonResult.error("请填写完整信息");
|
||||
}
|
||||
|
||||
// 2.密码长度是否合法
|
||||
if (userPass.length() > 20 || userPass.length() < 6) {
|
||||
return JsonResult.error("用户密码长度为6-20位!");
|
||||
}
|
||||
|
||||
//3.创建用户
|
||||
User user = new User();
|
||||
user.setUserName(userName);
|
||||
user.setUserDisplayName(userDisplayName);
|
||||
user.setIdCard(idCard);
|
||||
user.setUserPass(Md5Util.toMd5(userPass, CommonConstant.PASSWORD_SALT, 10));
|
||||
user.setUserAvatar("/static/images/avatar/" + RandomUtils.nextInt(1, 41) + ".jpeg");
|
||||
user.setStatus(UserStatusEnum.NORMAL.getCode());
|
||||
userService.insert(user);
|
||||
|
||||
//4.关联角色
|
||||
Role defaultRole = roleService.findDefaultRole();
|
||||
if (defaultRole != null) {
|
||||
userRoleRefService.insert(new UserRoleRef(user.getId(), defaultRole.getId()));
|
||||
}
|
||||
return JsonResult.success("注册成功");
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理忘记密码
|
||||
*
|
||||
* @param userName 手机号
|
||||
* @param idCard 身份证号码
|
||||
* @return JsonResult
|
||||
*/
|
||||
@PostMapping(value = "/forget")
|
||||
@ResponseBody
|
||||
public JsonResult getForget(@RequestParam("userName") String userName,
|
||||
@RequestParam("userPass") String userPass,
|
||||
@RequestParam("idCard") String idCard) {
|
||||
|
||||
User user = userService.findByUserName(userName);
|
||||
if (user != null && idCard.equalsIgnoreCase(user.getIdCard())) {
|
||||
//1.修改密码
|
||||
userService.updatePassword(user.getId(), userPass);
|
||||
return JsonResult.success("密码重置成功,新密码为" + userPass);
|
||||
} else {
|
||||
return JsonResult.error("手机号和电子身份证号码不一致");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查用户是否登录
|
||||
*
|
||||
* @return JsonResult
|
||||
*/
|
||||
@GetMapping(value = "/checkLogin")
|
||||
@ResponseBody
|
||||
public JsonResult checkLogin() {
|
||||
User user = getLoginUser();
|
||||
if (user == null) {
|
||||
return JsonResult.error("请先登录");
|
||||
} else {
|
||||
return JsonResult.success();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,50 +0,0 @@
|
||||
package com.example.hotel.service;
|
||||
|
||||
|
||||
import com.example.hotel.common.base.BaseService;
|
||||
import com.example.hotel.entity.Category;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 分类业务逻辑接口
|
||||
* </pre>
|
||||
*
|
||||
* @author : saysky
|
||||
* @date : 2017/11/30
|
||||
*/
|
||||
public interface CategoryService extends BaseService<Category, Long> {
|
||||
|
||||
/**
|
||||
* 查询所有分类目录,带count和根据level封装name
|
||||
*
|
||||
* @return 返回List集合
|
||||
*/
|
||||
List<Category> findByUserId(Long userId);
|
||||
|
||||
/**
|
||||
* 获得某个分类的所有客房数
|
||||
*
|
||||
* @param cateId 分类Id
|
||||
* @return 客房数
|
||||
*/
|
||||
Integer countPostByCateId(Long cateId);
|
||||
|
||||
/**
|
||||
* 根据用户ID删除
|
||||
*
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
Integer deleteByUserId(Long userId);
|
||||
|
||||
/**
|
||||
* 将分类ID列表转成分类
|
||||
*
|
||||
* @param cateIds
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
List<Category> cateIdsToCateList(List<Long> cateIds, Long userId);
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
package com.example.hotel.service;
|
||||
|
||||
|
||||
import javax.mail.MessagingException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 邮件发送业务逻辑接口
|
||||
* </pre>
|
||||
*/
|
||||
public interface MailService {
|
||||
|
||||
/**
|
||||
* 发送邮件
|
||||
*
|
||||
* @param to 接收者
|
||||
* @param title 标题
|
||||
* @param content 内容
|
||||
*/
|
||||
void sendMail(String to, String title, String content) throws MessagingException;
|
||||
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
package com.example.hotel.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.example.hotel.common.base.BaseService;
|
||||
import com.example.hotel.entity.Order;
|
||||
import com.example.hotel.entity.User;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 订单
|
||||
*
|
||||
* @author 言曌
|
||||
* @date 2020/4/6 2:00 下午
|
||||
*/
|
||||
public interface OrderService extends BaseService<Order, Long> {
|
||||
|
||||
/**
|
||||
* 根据时间范围查询总金额
|
||||
*
|
||||
* @param startDate
|
||||
* @param endDate
|
||||
* @return
|
||||
*/
|
||||
Integer getTotalPriceSum(String startDate, String endDate);
|
||||
|
||||
/**
|
||||
* 根据时间范围查询
|
||||
* @param startDate
|
||||
* @param endDate
|
||||
* @param page
|
||||
* @return
|
||||
*/
|
||||
Page<Order> findAll(String startDate, String endDate, Page<Order> page);
|
||||
}
|
@ -1,65 +0,0 @@
|
||||
package com.example.hotel.service;
|
||||
|
||||
import com.example.hotel.entity.Permission;
|
||||
import com.example.hotel.common.base.BaseService;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 权限逻辑接口
|
||||
*/
|
||||
public interface PermissionService extends BaseService<Permission, Long> {
|
||||
|
||||
/**
|
||||
* 根据角色Id获得权限列表
|
||||
*
|
||||
* @param roleId 角色Id
|
||||
* @return 权限列表
|
||||
*/
|
||||
List<Permission> listPermissionsByRoleId(Long roleId);
|
||||
|
||||
/**
|
||||
* 获得某个用户的权限URL列表
|
||||
*
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
Set<String> findPermissionUrlsByUserId(Long userId);
|
||||
|
||||
/**
|
||||
* 获得某个用户的用户ID和资源类型
|
||||
*
|
||||
* @param userId
|
||||
* @param resourceType
|
||||
* @return
|
||||
*/
|
||||
List<Permission> findPermissionTreeByUserIdAndResourceType(Long userId, String resourceType);
|
||||
|
||||
/**
|
||||
* 根据角色ID获得权限列表
|
||||
* @param roleId
|
||||
* @return
|
||||
*/
|
||||
List<Permission> findPermissionByRoleId(Long roleId);
|
||||
|
||||
/**
|
||||
* 获得所有权限,带有等级
|
||||
* @return
|
||||
*/
|
||||
List<Permission> findPermissionListWithLevel();
|
||||
|
||||
/**
|
||||
* 统计子节点数量
|
||||
* @param id
|
||||
* @return
|
||||
*/
|
||||
Integer countChildPermission(Long id);
|
||||
|
||||
/**
|
||||
* 根据URL获得权限
|
||||
* @param url
|
||||
* @return
|
||||
*/
|
||||
Permission findByUrl(String url);
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
package com.example.hotel.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.example.hotel.common.base.BaseService;
|
||||
import com.example.hotel.dto.PostQueryCondition;
|
||||
import com.example.hotel.entity.Post;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 记录/页面业务逻辑接口
|
||||
* </pre>
|
||||
*/
|
||||
public interface PostService extends BaseService<Post, Long> {
|
||||
|
||||
/**
|
||||
* 根据条件获得列表
|
||||
* @param condition
|
||||
* @return
|
||||
*/
|
||||
Page<Post> findPostByCondition(PostQueryCondition condition, Page<Post> page);
|
||||
|
||||
|
||||
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
package com.example.hotel.service;
|
||||
|
||||
import com.example.hotel.common.base.BaseService;
|
||||
import com.example.hotel.entity.Record;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 预定
|
||||
* @author 言曌
|
||||
* @date 2020/4/6 2:00 下午
|
||||
*/
|
||||
public interface RecordService extends BaseService<Record, Long> {
|
||||
|
||||
/**
|
||||
* 根据房间ID和日期列表查询预定
|
||||
*
|
||||
* @param postId
|
||||
* @param dateList
|
||||
* @return
|
||||
*/
|
||||
List<Record> findByPostIdAndRecordDate( Long postId, List<String> dateList);
|
||||
|
||||
/**
|
||||
* 获得某个房间的预定记录
|
||||
* @param postId
|
||||
* @return
|
||||
*/
|
||||
List<Record> findByPostId(Long postId);
|
||||
|
||||
/**
|
||||
* 删除
|
||||
* @param postId
|
||||
* @param userId
|
||||
* @param dateList
|
||||
* @return
|
||||
*/
|
||||
Integer delete(Long postId, Long userId, List<String> dateList);
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
package com.example.hotel.service;
|
||||
|
||||
import com.example.hotel.entity.RolePermissionRef;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
public interface RolePermissionRefService {
|
||||
|
||||
/**
|
||||
* 删除某个角色的所有关联
|
||||
*
|
||||
* @param roleId 角色Id
|
||||
*/
|
||||
void deleteRefByRoleId(Long roleId);
|
||||
|
||||
/**
|
||||
* 添加角色和权限关联
|
||||
*
|
||||
* @param rolePermissionRef RolePermissionRef
|
||||
* @return UserRoleRef
|
||||
*/
|
||||
void saveByRolePermissionRef(RolePermissionRef rolePermissionRef);
|
||||
|
||||
/**
|
||||
* 批量添加
|
||||
*
|
||||
* @param rolePermissionRefs 列表
|
||||
*/
|
||||
void batchSaveByRolePermissionRef(List<RolePermissionRef> rolePermissionRefs);
|
||||
|
||||
}
|
@ -1,77 +0,0 @@
|
||||
package com.example.hotel.service;
|
||||
|
||||
import com.example.hotel.entity.Role;
|
||||
import com.example.hotel.common.base.BaseService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 角色逻辑接口
|
||||
*/
|
||||
public interface RoleService extends BaseService<Role, Long> {
|
||||
|
||||
/**
|
||||
* 删除某个用户的所有关联
|
||||
*
|
||||
* @param userId 用户Id
|
||||
*/
|
||||
void deleteByUserId(Long userId);
|
||||
|
||||
/**
|
||||
* 根据编号查询单个权限
|
||||
*
|
||||
* @param roleId roleId
|
||||
* @return Role
|
||||
*/
|
||||
Role findByRoleId(Long roleId);
|
||||
|
||||
/**
|
||||
* 根据编号查询单个权限
|
||||
*
|
||||
* @param roleName roleName
|
||||
* @return Role
|
||||
*/
|
||||
Role findByRoleName(String roleName);
|
||||
|
||||
/**
|
||||
* 根据用户Id获得角色
|
||||
*
|
||||
* @param userId 用户Id
|
||||
* @return 角色列表
|
||||
*/
|
||||
Role findByUserId(Long userId);
|
||||
|
||||
/**
|
||||
* 统计这个角色的用户数
|
||||
*
|
||||
* @param roleId 角色Id
|
||||
*/
|
||||
Integer countUserByRoleId(Long roleId);
|
||||
|
||||
/**
|
||||
* 查询某个用户最大的角色等级
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
Integer findMaxLevelByUserId(Long userId);
|
||||
|
||||
/**
|
||||
* 查询小于等于该等级的角色
|
||||
* @param level
|
||||
* @return
|
||||
*/
|
||||
List<Role> findByLessThanLevel(Integer level);
|
||||
|
||||
/**
|
||||
* 获得用户注册默认角色
|
||||
* @return
|
||||
*/
|
||||
Role findDefaultRole();
|
||||
|
||||
/**
|
||||
* 获得用户注册默认角色
|
||||
* @return
|
||||
*/
|
||||
Role getMaxRoleByUserId(Long userId);
|
||||
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
package com.example.hotel.service;
|
||||
|
||||
import com.example.hotel.entity.UserRoleRef;
|
||||
import com.example.hotel.common.base.BaseService;
|
||||
|
||||
|
||||
public interface UserRoleRefService extends BaseService<UserRoleRef, Long> {
|
||||
|
||||
/**
|
||||
* 根据用户Id删除
|
||||
*
|
||||
* @param userId 用户Id
|
||||
*/
|
||||
void deleteByUserId(Long userId);
|
||||
|
||||
|
||||
}
|
@ -1,49 +0,0 @@
|
||||
package com.example.hotel.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.example.hotel.common.base.BaseService;
|
||||
import com.example.hotel.entity.User;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 用户业务逻辑接口
|
||||
*/
|
||||
public interface UserService extends BaseService<User, Long> {
|
||||
|
||||
/**
|
||||
* 根据手机号获得用户
|
||||
*
|
||||
* @param userName 手机号
|
||||
* @return 用户
|
||||
*/
|
||||
User findByUserName(String userName);
|
||||
|
||||
|
||||
/**
|
||||
* 根据身份证号码查找用户
|
||||
*
|
||||
* @param idCard 身份证号码
|
||||
* @return User
|
||||
*/
|
||||
User findByIdCard(String idCard);
|
||||
|
||||
/**
|
||||
* 更新密码
|
||||
*
|
||||
* @param userId 用户Id
|
||||
* @param password 密码
|
||||
*/
|
||||
void updatePassword(Long userId, String password);
|
||||
|
||||
/**
|
||||
* 分页获取所有用户
|
||||
*
|
||||
* @param roleName 角色名称
|
||||
* @param condition 查询条件
|
||||
* @param page 分页信息
|
||||
* @return 用户列表
|
||||
*/
|
||||
Page<User> findByRoleAndCondition(String roleName, User condition, Page<User> page);
|
||||
|
||||
}
|
@ -1,112 +0,0 @@
|
||||
package com.example.hotel.service.impl;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.example.hotel.entity.Category;
|
||||
import com.example.hotel.mapper.CategoryMapper;
|
||||
import com.example.hotel.mapper.PostMapper;
|
||||
import com.example.hotel.service.CategoryService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 分类业务逻辑实现类
|
||||
* </pre>
|
||||
*
|
||||
*/
|
||||
@Service
|
||||
public class CategoryServiceImpl implements CategoryService {
|
||||
|
||||
@Autowired
|
||||
private CategoryMapper categoryMapper;
|
||||
|
||||
@Autowired
|
||||
private PostMapper postMapper;
|
||||
|
||||
@Override
|
||||
public BaseMapper<Category> getRepository() {
|
||||
return categoryMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryWrapper<Category> getQueryWrapper(Category category) {
|
||||
//对指定字段查询
|
||||
QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
|
||||
if (category != null) {
|
||||
if (StrUtil.isNotBlank(category.getCateName())) {
|
||||
queryWrapper.like("cate_name", category.getCateName());
|
||||
}
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Category insert(Category category) {
|
||||
categoryMapper.insert(category);
|
||||
return category;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Category update(Category category) {
|
||||
categoryMapper.updateById(category);
|
||||
return category;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete(Long id) {
|
||||
//2.删除分类
|
||||
categoryMapper.deleteById(id);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<Category> findByUserId(Long userId) {
|
||||
QueryWrapper queryWrapper = new QueryWrapper();
|
||||
queryWrapper.eq("user_id", userId);
|
||||
return categoryMapper.selectList(queryWrapper);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Integer countPostByCateId(Long cateId) {
|
||||
return postMapper.countPostByCateId(cateId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Category insertOrUpdate(Category entity) {
|
||||
if (entity.getId() == null) {
|
||||
insert(entity);
|
||||
} else {
|
||||
update(entity);
|
||||
}
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer deleteByUserId(Long userId) {
|
||||
return categoryMapper.deleteByUserId(userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Category> cateIdsToCateList(List<Long> cateIds, Long userId) {
|
||||
List<Category> categoryList = this.findByUserId(userId);
|
||||
List<Long> allCateIds = categoryList.stream().map(Category::getId).collect(Collectors.toList());
|
||||
List<Category> result = new ArrayList<>();
|
||||
for(Long id : cateIds) {
|
||||
if(allCateIds.contains(id)) {
|
||||
Category category = new Category();
|
||||
category.setId(id);
|
||||
result.add(category);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -1,51 +0,0 @@
|
||||
package com.example.hotel.service.impl;
|
||||
|
||||
import com.example.hotel.service.MailService;
|
||||
import com.example.hotel.util.SensUtils;
|
||||
import io.github.biezhi.ome.OhMyEmail;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.mail.MessagingException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 邮件发送业务逻辑实现类
|
||||
* </pre>
|
||||
*
|
||||
* @author : saysky
|
||||
* @date : 2018/1/23
|
||||
*/
|
||||
@Service
|
||||
public class MailServiceImpl implements MailService {
|
||||
|
||||
@Value("${mail.smtp.host}")
|
||||
private String host;
|
||||
|
||||
@Value("${mail.smtp.username}")
|
||||
private String username;
|
||||
|
||||
@Value("${mail.smtp.password}")
|
||||
private String password;
|
||||
|
||||
@Value("${mail.from.name}")
|
||||
private String fromName;
|
||||
|
||||
/**
|
||||
* 发送邮件
|
||||
*
|
||||
* @param to to 接收者
|
||||
* @param title subject 标题
|
||||
* @param content content 内容
|
||||
*/
|
||||
@Override
|
||||
public void sendMail(String to, String title, String content) throws MessagingException {
|
||||
//配置邮件服务器
|
||||
SensUtils.configMail(host, username, password);
|
||||
OhMyEmail.subject(title)
|
||||
.from(fromName)
|
||||
.to(to)
|
||||
.text(content)
|
||||
.send();
|
||||
}
|
||||
}
|
@ -1,62 +0,0 @@
|
||||
package com.example.hotel.service.impl;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.example.hotel.entity.Order;
|
||||
import com.example.hotel.entity.User;
|
||||
import com.example.hotel.mapper.OrderMapper;
|
||||
import com.example.hotel.service.OrderService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* @author 言曌
|
||||
* @date 2020/4/6 2:01 下午
|
||||
*/
|
||||
@Service
|
||||
public class OrderServiceImpl implements OrderService {
|
||||
|
||||
@Autowired
|
||||
private OrderMapper orderMapper;
|
||||
|
||||
@Override
|
||||
public BaseMapper<Order> getRepository() {
|
||||
return orderMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryWrapper<Order> getQueryWrapper(Order order) {
|
||||
//对指定字段查询
|
||||
QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
|
||||
if (order != null) {
|
||||
if (order.getUserId() != null) {
|
||||
queryWrapper.eq("user_id", order.getUserId());
|
||||
}
|
||||
if (order.getPostId() != null) {
|
||||
queryWrapper.eq("post_id", order.getPostId());
|
||||
}
|
||||
if (StrUtil.isNotBlank(order.getPhone())) {
|
||||
queryWrapper.eq("phone", order.getPhone());
|
||||
}
|
||||
if (order.getStatus() != null) {
|
||||
queryWrapper.eq("status", order.getStatus());
|
||||
}
|
||||
if (order.getStartDate() != null) {
|
||||
queryWrapper.eq("start_date", order.getStartDate());
|
||||
}
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getTotalPriceSum(String startDate, String endDate) {
|
||||
return orderMapper.getTotalPriceSum(startDate, endDate);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<Order> findAll(String startDate, String endDate, Page<Order> page) {
|
||||
return page.setRecords(orderMapper.findAll(startDate, endDate, page));
|
||||
}
|
||||
}
|
@ -1,124 +0,0 @@
|
||||
package com.example.hotel.service.impl;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.example.hotel.entity.Permission;
|
||||
import com.example.hotel.mapper.PermissionMapper;
|
||||
import com.example.hotel.mapper.RolePermissionRefMapper;
|
||||
import com.example.hotel.service.PermissionService;
|
||||
import com.example.hotel.util.PermissionUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 角色业务逻辑实现类
|
||||
*/
|
||||
@Service
|
||||
public class PermissionServiceImpl implements PermissionService {
|
||||
|
||||
@Autowired
|
||||
private PermissionMapper permissionMapper;
|
||||
|
||||
@Autowired
|
||||
private RolePermissionRefMapper rolePermissionRefMapper;
|
||||
|
||||
|
||||
@Override
|
||||
public List<Permission> listPermissionsByRoleId(Long roleId) {
|
||||
return permissionMapper.findByRoleId(roleId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> findPermissionUrlsByUserId(Long userId) {
|
||||
List<Permission> permissions = permissionMapper.findPermissionByUserId(userId);
|
||||
Set<String> urls = permissions.stream().map(p -> p.getUrl()).collect(Collectors.toSet());
|
||||
return urls;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Permission> findPermissionTreeByUserIdAndResourceType(Long userId, String resourceType) {
|
||||
List<Permission> permissions = permissionMapper.findPermissionByUserIdAndResourceType(userId, resourceType);
|
||||
return PermissionUtil.getPermissionTree(permissions);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Permission> findPermissionByRoleId(Long roleId) {
|
||||
return permissionMapper.findPermissionByRoleId(roleId);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public BaseMapper<Permission> getRepository() {
|
||||
return permissionMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryWrapper<Permission> getQueryWrapper(Permission permission) {
|
||||
//对指定字段查询
|
||||
QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
|
||||
if (permission != null) {
|
||||
if (StrUtil.isNotBlank(permission.getResourceType())) {
|
||||
queryWrapper.eq("resource_type", permission.getResourceType());
|
||||
}
|
||||
if (StrUtil.isNotBlank(permission.getResourceType())) {
|
||||
queryWrapper.eq("resource_type", permission.getResourceType());
|
||||
}
|
||||
if (StrUtil.isNotBlank(permission.getUrl())) {
|
||||
queryWrapper.eq("url", permission.getUrl());
|
||||
}
|
||||
if (StrUtil.isNotBlank(permission.getName())) {
|
||||
queryWrapper.eq("name", permission.getName());
|
||||
}
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Permission insertOrUpdate(Permission entity) {
|
||||
if (entity.getId() == null) {
|
||||
insert(entity);
|
||||
} else {
|
||||
update(entity);
|
||||
}
|
||||
return entity;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void delete(Long id) {
|
||||
permissionMapper.deleteById(id);
|
||||
rolePermissionRefMapper.deleteByPermissionId(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Permission> findPermissionListWithLevel() {
|
||||
List<Permission> permissionList = permissionMapper.selectList(null);
|
||||
permissionList = PermissionUtil.getPermissionList(permissionList);
|
||||
|
||||
// 加空格以展示等级
|
||||
for (Permission permission : permissionList) {
|
||||
for (int i = 1; i < permission.getLevel(); i++) {
|
||||
permission.setName(" "+permission.getName());
|
||||
}
|
||||
}
|
||||
return permissionList;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer countChildPermission(Long id) {
|
||||
return permissionMapper.countChildPermission(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Permission findByUrl(String url) {
|
||||
return permissionMapper.findByUrl(url);
|
||||
}
|
||||
|
||||
}
|
@ -1,93 +0,0 @@
|
||||
package com.example.hotel.service.impl;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.example.hotel.dto.PostQueryCondition;
|
||||
import com.example.hotel.entity.*;
|
||||
import com.example.hotel.mapper.*;
|
||||
import com.example.hotel.service.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 客房业务逻辑实现类
|
||||
* </pre>
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class PostServiceImpl implements PostService {
|
||||
|
||||
|
||||
@Autowired
|
||||
private PostMapper postMapper;
|
||||
|
||||
@Override
|
||||
public Page<Post> findPostByCondition(PostQueryCondition condition, Page<Post> page) {
|
||||
List<Post> postList = postMapper.findPostByCondition(condition, page);
|
||||
return page.setRecords(postList);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseMapper<Post> getRepository() {
|
||||
return postMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Post insert(Post post) {
|
||||
postMapper.insert(post);
|
||||
return post;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Post update(Post post) {
|
||||
postMapper.updateById(post);
|
||||
return post;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void delete(Long postId) {
|
||||
Post post = this.get(postId);
|
||||
if (post != null) {
|
||||
postMapper.deleteById(post.getId());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryWrapper<Post> getQueryWrapper(Post post) {
|
||||
//对指定字段查询
|
||||
QueryWrapper<Post> queryWrapper = new QueryWrapper<>();
|
||||
if (post != null) {
|
||||
if (StrUtil.isNotBlank(post.getPostTitle())) {
|
||||
queryWrapper.like("post_title", post.getPostTitle());
|
||||
}
|
||||
if (StrUtil.isNotBlank(post.getPostContent())) {
|
||||
queryWrapper.like("post_content", post.getPostContent());
|
||||
}
|
||||
if (post.getPostStatus() != null && post.getPostStatus() != -1) {
|
||||
queryWrapper.eq("post_status", post.getPostStatus());
|
||||
}
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Post insertOrUpdate(Post post) {
|
||||
if (post.getId() == null) {
|
||||
insert(post);
|
||||
} else {
|
||||
update(post);
|
||||
}
|
||||
return post;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,64 +0,0 @@
|
||||
package com.example.hotel.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.example.hotel.entity.Record;
|
||||
import com.example.hotel.mapper.RecordMapper;
|
||||
import com.example.hotel.service.RecordService;
|
||||
import org.apache.commons.collections.ListUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author 言曌
|
||||
* @date 2020/4/6 2:01 下午
|
||||
*/
|
||||
@Service
|
||||
public class RecordServiceImpl implements RecordService {
|
||||
|
||||
@Autowired
|
||||
private RecordMapper recordMapper;
|
||||
|
||||
@Override
|
||||
public BaseMapper<Record> getRepository() {
|
||||
return recordMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryWrapper<Record> getQueryWrapper(Record record) {
|
||||
//对指定字段查询
|
||||
QueryWrapper<Record> queryWrapper = new QueryWrapper<>();
|
||||
if (record != null) {
|
||||
if (record.getUserId() != null) {
|
||||
queryWrapper.eq("user_id", record.getUserId());
|
||||
}
|
||||
if (record.getPostId() != null) {
|
||||
queryWrapper.eq("post_id", record.getPostId());
|
||||
}
|
||||
if (record.getRecordDate() != null) {
|
||||
queryWrapper.eq("record_date", record.getRecordDate());
|
||||
}
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Record> findByPostIdAndRecordDate(Long postId, List<String> dateList) {
|
||||
return recordMapper.findByPostIdAndRecordDate(postId, dateList);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Record> findByPostId(Long postId) {
|
||||
return recordMapper.findByPostId(postId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer delete(Long postId, Long userId, List<String> dateList) {
|
||||
if (dateList != null && dateList.size() > 0) {
|
||||
return recordMapper.delete(postId, userId, dateList);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
@ -1,31 +0,0 @@
|
||||
package com.example.hotel.service.impl;
|
||||
|
||||
import com.example.hotel.entity.RolePermissionRef;
|
||||
import com.example.hotel.mapper.RolePermissionRefMapper;
|
||||
import com.example.hotel.service.RolePermissionRefService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class RolePermissionRefServiceImpl implements RolePermissionRefService {
|
||||
|
||||
@Autowired
|
||||
private RolePermissionRefMapper rolePermissionRefMapper;
|
||||
|
||||
@Override
|
||||
public void deleteRefByRoleId(Long roleId) {
|
||||
rolePermissionRefMapper.deleteByRoleId(roleId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveByRolePermissionRef(RolePermissionRef rolePermissionRef) {
|
||||
rolePermissionRefMapper.insert(rolePermissionRef);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void batchSaveByRolePermissionRef(List<RolePermissionRef> rolePermissionRefs) {
|
||||
rolePermissionRefMapper.batchInsert(rolePermissionRefs);
|
||||
}
|
||||
}
|
@ -1,139 +0,0 @@
|
||||
package com.example.hotel.service.impl;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.example.hotel.entity.Permission;
|
||||
import com.example.hotel.entity.Role;
|
||||
import com.example.hotel.entity.RolePermissionRef;
|
||||
import com.example.hotel.mapper.RoleMapper;
|
||||
import com.example.hotel.service.RolePermissionRefService;
|
||||
import com.example.hotel.service.RoleService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 角色业务逻辑实现类
|
||||
*/
|
||||
@Service
|
||||
public class RoleServiceImpl implements RoleService {
|
||||
|
||||
@Autowired
|
||||
private RoleMapper roleMapper;
|
||||
|
||||
@Autowired
|
||||
private RolePermissionRefService rolePermissionRefService;
|
||||
|
||||
@Override
|
||||
public BaseMapper<Role> getRepository() {
|
||||
return roleMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryWrapper<Role> getQueryWrapper(Role role) {
|
||||
//对指定字段查询
|
||||
QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
|
||||
if (role != null) {
|
||||
if (StrUtil.isNotBlank(role.getRole())) {
|
||||
queryWrapper.eq("role", role.getRole());
|
||||
}
|
||||
if (StrUtil.isNotBlank(role.getDescription())) {
|
||||
queryWrapper.eq("description", role.getDescription());
|
||||
}
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteByUserId(Long userId) {
|
||||
roleMapper.deleteByUserId(userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Role findByRoleId(Long roleId) {
|
||||
return roleMapper.selectById(roleId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Role findByRoleName(String roleName) {
|
||||
QueryWrapper queryWrapper = new QueryWrapper();
|
||||
queryWrapper.eq("role", roleName);
|
||||
return roleMapper.selectOne(queryWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Role findByUserId(Long userId) {
|
||||
return roleMapper.findByUserId(userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer countUserByRoleId(Long roleId) {
|
||||
return roleMapper.countUserByRoleId(roleId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer findMaxLevelByUserId(Long userId) {
|
||||
return roleMapper.findMaxLevelByUserId(userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Role> findByLessThanLevel(Integer level) {
|
||||
return roleMapper.findByLessThanLevel(level);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Role findDefaultRole() {
|
||||
return roleMapper.findDefaultRole();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Role getMaxRoleByUserId(Long userId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Role insert(Role role) {
|
||||
roleMapper.insert(role);
|
||||
if (role.getPermissions() != null && role.getPermissions().size() > 0) {
|
||||
List<RolePermissionRef> rolePermissionRefs = new ArrayList<>(role.getPermissions().size());
|
||||
for (Permission permission : role.getPermissions()) {
|
||||
rolePermissionRefs.add(new RolePermissionRef(role.getId(), permission.getId()));
|
||||
}
|
||||
rolePermissionRefService.batchSaveByRolePermissionRef(rolePermissionRefs);
|
||||
}
|
||||
return role;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public Role update(Role role) {
|
||||
roleMapper.updateById(role);
|
||||
if (role.getPermissions() != null && role.getPermissions().size() > 0) {
|
||||
// 删除关联
|
||||
rolePermissionRefService.deleteRefByRoleId(role.getId());
|
||||
// 添加关联
|
||||
List<RolePermissionRef> rolePermissionRefs = new ArrayList<>(role.getPermissions().size());
|
||||
for (Permission permission : role.getPermissions()) {
|
||||
rolePermissionRefs.add(new RolePermissionRef(role.getId(), permission.getId()));
|
||||
}
|
||||
rolePermissionRefService.batchSaveByRolePermissionRef(rolePermissionRefs);
|
||||
}
|
||||
return role;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Role insertOrUpdate(Role entity) {
|
||||
if (entity.getId() == null) {
|
||||
insert(entity);
|
||||
} else {
|
||||
update(entity);
|
||||
}
|
||||
return entity;
|
||||
}
|
||||
|
||||
}
|
@ -1,44 +0,0 @@
|
||||
package com.example.hotel.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.example.hotel.entity.UserRoleRef;
|
||||
import com.example.hotel.mapper.UserRoleRefMapper;
|
||||
import com.example.hotel.service.UserRoleRefService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
|
||||
@Service
|
||||
public class UserRoleRefServiceImpl implements UserRoleRefService {
|
||||
|
||||
@Autowired
|
||||
private UserRoleRefMapper roleRefMapper;
|
||||
|
||||
|
||||
@Override
|
||||
public void deleteByUserId(Long userId) {
|
||||
roleRefMapper.deleteByUserId(userId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseMapper<UserRoleRef> getRepository() {
|
||||
return roleRefMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryWrapper<UserRoleRef> getQueryWrapper(UserRoleRef userRoleRef) {
|
||||
//对指定字段查询
|
||||
QueryWrapper<UserRoleRef> queryWrapper = new QueryWrapper<>();
|
||||
if (userRoleRef != null) {
|
||||
if (userRoleRef.getUserId() != null) {
|
||||
queryWrapper.eq("user_id", userRoleRef.getUserId());
|
||||
}
|
||||
if (userRoleRef.getRoleId() != null) {
|
||||
queryWrapper.eq("role_id", userRoleRef.getRoleId());
|
||||
}
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
}
|
@ -1,199 +0,0 @@
|
||||
package com.example.hotel.service.impl;
|
||||
|
||||
import cn.hutool.core.lang.Validator;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.example.hotel.dto.JsonResult;
|
||||
import com.example.hotel.exception.MyBusinessException;
|
||||
import com.example.hotel.common.constant.CommonConstant;
|
||||
import com.example.hotel.entity.Role;
|
||||
import com.example.hotel.mapper.OrderMapper;
|
||||
import com.example.hotel.mapper.RecordMapper;
|
||||
import com.example.hotel.mapper.UserMapper;
|
||||
import com.example.hotel.entity.User;
|
||||
import com.example.hotel.enums.TrueFalseEnum;
|
||||
import com.example.hotel.service.PostService;
|
||||
import com.example.hotel.service.RoleService;
|
||||
import com.example.hotel.service.UserService;
|
||||
import com.example.hotel.util.Md5Util;
|
||||
import com.example.hotel.util.RegexUtil;
|
||||
import org.apache.commons.lang3.RandomUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 用户业务逻辑实现类
|
||||
*/
|
||||
@Service
|
||||
public class UserServiceImpl implements UserService {
|
||||
|
||||
@Autowired
|
||||
private UserMapper userMapper;
|
||||
|
||||
@Autowired
|
||||
private RoleService roleService;
|
||||
|
||||
@Autowired
|
||||
private OrderMapper orderMapper;
|
||||
|
||||
@Autowired
|
||||
private RecordMapper recordMapper;
|
||||
|
||||
@Override
|
||||
public User findByUserName(String userName) {
|
||||
QueryWrapper queryWrapper = new QueryWrapper();
|
||||
queryWrapper.eq("user_name", userName);
|
||||
return userMapper.selectOne(queryWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public User findByIdCard(String idCard) {
|
||||
QueryWrapper queryWrapper = new QueryWrapper();
|
||||
queryWrapper.eq("id_card", idCard);
|
||||
return userMapper.selectOne(queryWrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updatePassword(Long userId, String password) {
|
||||
User user = new User();
|
||||
user.setId(userId);
|
||||
user.setUserPass(Md5Util.toMd5(password, CommonConstant.PASSWORD_SALT, 10));
|
||||
userMapper.updateById(user);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<User> findByRoleAndCondition(String roleName, User condition, Page<User> page) {
|
||||
List<User> users = new ArrayList<>();
|
||||
if (Objects.equals(roleName, CommonConstant.NONE)) {
|
||||
users = userMapper.findByCondition(condition, page);
|
||||
} else {
|
||||
Role role = roleService.findByRoleName(roleName);
|
||||
if (role != null) {
|
||||
users = userMapper.findByRoleIdAndCondition(role.getId(), condition, page);
|
||||
}
|
||||
}
|
||||
return page.setRecords(users);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseMapper<User> getRepository() {
|
||||
return userMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public QueryWrapper<User> getQueryWrapper(User user) {
|
||||
//对指定字段查询
|
||||
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
|
||||
if (user != null) {
|
||||
if (StrUtil.isNotBlank(user.getUserName())) {
|
||||
queryWrapper.eq("user_name", user.getUserName());
|
||||
}
|
||||
if (StrUtil.isNotBlank(user.getIdCard())) {
|
||||
queryWrapper.eq("id_card", user.getIdCard());
|
||||
}
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public User insert(User user) {
|
||||
// 1.检查长度
|
||||
basicCheck(user);
|
||||
// 2.验证手机号和身份证号码是否存在
|
||||
checkUserNameAndIdCard(user);
|
||||
// 3.添加
|
||||
userMapper.insert(user);
|
||||
return user;
|
||||
}
|
||||
|
||||
@Override
|
||||
public User update(User user) {
|
||||
// 1.检查长度
|
||||
basicCheck(user);
|
||||
// 2.验证手机号和身份证号码是否存在
|
||||
checkUserNameAndIdCard(user);
|
||||
// 3.更新
|
||||
userMapper.updateById(user);
|
||||
return user;
|
||||
}
|
||||
|
||||
private void checkUserNameAndIdCard(User user) {
|
||||
//验证手机号和身份证号码是否存在
|
||||
if (user.getUserName() != null) {
|
||||
User nameCheck = findByUserName(user.getUserName());
|
||||
Boolean isExist = (user.getId() == null && nameCheck != null) ||
|
||||
(user.getId() != null && nameCheck != null && !Objects.equals(nameCheck.getId(), user.getId()));
|
||||
if (isExist) {
|
||||
throw new MyBusinessException("手机号已经存在");
|
||||
}
|
||||
}
|
||||
if (user.getIdCard() != null) {
|
||||
User idCardCheck = findByIdCard(user.getIdCard());
|
||||
Boolean isExist = (user.getId() == null && idCardCheck != null) ||
|
||||
(user.getId() != null && idCardCheck != null && !Objects.equals(idCardCheck.getId(), user.getId()));
|
||||
if (isExist) {
|
||||
throw new MyBusinessException("身份证号码已经存在");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public void delete(Long userId) {
|
||||
//删除用户
|
||||
User user = get(userId);
|
||||
if (user != null) {
|
||||
// 1.修改用户状态为已删除
|
||||
userMapper.deleteById(userId);
|
||||
// 2.修改用户和角色关联
|
||||
roleService.deleteByUserId(userId);
|
||||
// 3.删除订单
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
map.put("user_id", userId);
|
||||
orderMapper.deleteByMap(map);
|
||||
// 4.删除预定
|
||||
recordMapper.deleteByMap(map);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public User insertOrUpdate(User user) {
|
||||
if (user.getId() == null) {
|
||||
user.setUserAvatar("/static/images/avatar/" + RandomUtils.nextInt(1, 41) + ".jpeg");
|
||||
insert(user);
|
||||
} else {
|
||||
update(user);
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
private void basicCheck(User user) {
|
||||
String userName = user.getUserName();
|
||||
String idCard = user.getIdCard();
|
||||
String userDisplayName = user.getUserDisplayName();
|
||||
// 1.身份证号码是否合法
|
||||
if (StringUtils.isNotEmpty(idCard) && !RegexUtil.isIdCard(idCard)) {
|
||||
throw new MyBusinessException("身份证号码不合法");
|
||||
}
|
||||
// 2.手机号码长度是否合法
|
||||
if (StringUtils.isNotEmpty(userName) && userName.length() != 11) {
|
||||
throw new MyBusinessException("手机号码不合法");
|
||||
}
|
||||
// 3.姓名长度是否合法
|
||||
if (StringUtils.isNotEmpty(userDisplayName) && userDisplayName.length() > 10 || userDisplayName.length() < 2) {
|
||||
throw new MyBusinessException("姓名长度不合法");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public User get(Long id) {
|
||||
User user = userMapper.selectById(id);
|
||||
return user;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user