完成新增员工、员工分页查询功能
This commit is contained in:
parent
cd990bf534
commit
cb5c5c0cd8
@ -8,6 +8,7 @@ public class MessageConstant {
|
||||
public static final String PASSWORD_ERROR = "密码错误";
|
||||
public static final String ACCOUNT_NOT_FOUND = "账号不存在";
|
||||
public static final String ACCOUNT_LOCKED = "账号被锁定";
|
||||
public static final String ALREADY_EXISTS = "已存在";
|
||||
public static final String UNKNOWN_ERROR = "未知错误";
|
||||
public static final String USER_NOT_LOGIN = "用户未登录";
|
||||
public static final String CATEGORY_BE_RELATED_BY_SETMEAL = "当前分类关联了套餐,不能删除";
|
||||
|
@ -1,10 +1,13 @@
|
||||
package com.sky.config;
|
||||
|
||||
import com.sky.interceptor.JwtTokenAdminInterceptor;
|
||||
import com.sky.json.JacksonObjectMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
|
||||
@ -15,6 +18,8 @@ import springfox.documentation.service.ApiInfo;
|
||||
import springfox.documentation.spi.DocumentationType;
|
||||
import springfox.documentation.spring.web.plugins.Docket;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 配置类,注册web层相关组件
|
||||
*/
|
||||
@ -51,6 +56,7 @@ public class WebMvcConfiguration extends WebMvcConfigurationSupport {
|
||||
Docket docket = new Docket(DocumentationType.SWAGGER_2)
|
||||
.apiInfo(apiInfo)
|
||||
.select()
|
||||
//指定生成接口需要扫描的包,包括子包
|
||||
.apis(RequestHandlerSelectors.basePackage("com.sky.controller"))
|
||||
.paths(PathSelectors.any())
|
||||
.build();
|
||||
@ -62,7 +68,19 @@ public class WebMvcConfiguration extends WebMvcConfigurationSupport {
|
||||
* @param registry
|
||||
*/
|
||||
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
log.info("开始设置静态资源映射...");
|
||||
registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
|
||||
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
|
||||
}
|
||||
|
||||
protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
|
||||
log.info("扩展消息转换器...");
|
||||
//创建一个消息转换器对象
|
||||
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
|
||||
//需要为消息转换器设置一个对象转换器,对象转换器可以将java对象转换为json字符串
|
||||
converter.setObjectMapper(new JacksonObjectMapper());
|
||||
//自己将消息转换器添加到消息转换器列表中,索引为0
|
||||
converters.add(0,converter);
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -1,19 +1,21 @@
|
||||
package com.sky.controller.admin;
|
||||
|
||||
import com.sky.constant.JwtClaimsConstant;
|
||||
import com.sky.dto.EmployeeDTO;
|
||||
import com.sky.dto.EmployeeLoginDTO;
|
||||
import com.sky.dto.EmployeePageQueryDTO;
|
||||
import com.sky.entity.Employee;
|
||||
import com.sky.properties.JwtProperties;
|
||||
import com.sky.result.PageResult;
|
||||
import com.sky.result.Result;
|
||||
import com.sky.service.EmployeeService;
|
||||
import com.sky.utils.JwtUtil;
|
||||
import com.sky.vo.EmployeeLoginVO;
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@ -24,6 +26,7 @@ import java.util.Map;
|
||||
@RestController
|
||||
@RequestMapping("/admin/employee")
|
||||
@Slf4j
|
||||
@Api(tags = "员工相关接口")
|
||||
public class EmployeeController {
|
||||
|
||||
@Autowired
|
||||
@ -38,6 +41,7 @@ public class EmployeeController {
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/login")
|
||||
@ApiOperation(value = "员工登录")
|
||||
public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
|
||||
log.info("员工登录:{}", employeeLoginDTO);
|
||||
|
||||
@ -67,8 +71,34 @@ public class EmployeeController {
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/logout")
|
||||
@ApiOperation("员工退出")
|
||||
public Result<String> logout() {
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增员工
|
||||
* @param employeeDTO
|
||||
* @return
|
||||
*/
|
||||
@PostMapping
|
||||
@ApiOperation("新增员工")
|
||||
public Result save(@RequestBody EmployeeDTO employeeDTO) {
|
||||
log.info("新增员工:{}", employeeDTO);
|
||||
employeeService.save(employeeDTO);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页查询员工
|
||||
* @param employeePageQueryDTO
|
||||
* @return
|
||||
*/
|
||||
@GetMapping("/page")
|
||||
@ApiOperation("分页查询员工")
|
||||
public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO) {
|
||||
log.info("分页查询员工:{}", employeePageQueryDTO);
|
||||
PageResult pageResult = employeeService.pageQuery(employeePageQueryDTO);
|
||||
return Result.success(pageResult);
|
||||
}
|
||||
}
|
||||
|
@ -23,5 +23,4 @@ public class GlobalExceptionHandler {
|
||||
log.error("异常信息:{}", ex.getMessage());
|
||||
return Result.error(ex.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.sky.interceptor;
|
||||
|
||||
import com.sky.constant.JwtClaimsConstant;
|
||||
import com.sky.context.BaseContext;
|
||||
import com.sky.properties.JwtProperties;
|
||||
import com.sky.utils.JwtUtil;
|
||||
import io.jsonwebtoken.Claims;
|
||||
@ -47,6 +48,8 @@ public class JwtTokenAdminInterceptor implements HandlerInterceptor {
|
||||
Claims claims = JwtUtil.parseJWT(jwtProperties.getAdminSecretKey(), token);
|
||||
Long empId = Long.valueOf(claims.get(JwtClaimsConstant.EMP_ID).toString());
|
||||
log.info("当前员工id:", empId);
|
||||
//将操作者id放入threadlocal中
|
||||
BaseContext.setCurrentId(empId);
|
||||
//3、通过,放行
|
||||
return true;
|
||||
} catch (Exception ex) {
|
||||
|
@ -1,6 +1,9 @@
|
||||
package com.sky.mapper;
|
||||
|
||||
import com.github.pagehelper.Page;
|
||||
import com.sky.dto.EmployeePageQueryDTO;
|
||||
import com.sky.entity.Employee;
|
||||
import org.apache.ibatis.annotations.Insert;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
@ -15,4 +18,21 @@ public interface EmployeeMapper {
|
||||
@Select("select * from employee where username = #{username}")
|
||||
Employee getByUsername(String username);
|
||||
|
||||
/**
|
||||
* 插入员工
|
||||
* @param employee
|
||||
*/
|
||||
@Insert("insert into employee(name, username, password, phone, sex, id_number, " +
|
||||
"create_time, update_time, create_user, update_user) " +
|
||||
"VALUES (#{name}, #{username}, #{password}, #{phone}, #{sex}, #{idNumber}, " +
|
||||
"#{createTime}, #{updateTime}, #{createUser}, #{updateUser})")
|
||||
void insert(Employee employee);
|
||||
|
||||
|
||||
/**
|
||||
* 分页查询
|
||||
* @param employeePageQueryDTO
|
||||
* @return
|
||||
*/
|
||||
Page<Employee> pageQuery(EmployeePageQueryDTO employeePageQueryDTO);
|
||||
}
|
||||
|
@ -1,7 +1,10 @@
|
||||
package com.sky.service;
|
||||
|
||||
import com.sky.dto.EmployeeDTO;
|
||||
import com.sky.dto.EmployeeLoginDTO;
|
||||
import com.sky.dto.EmployeePageQueryDTO;
|
||||
import com.sky.entity.Employee;
|
||||
import com.sky.result.PageResult;
|
||||
|
||||
public interface EmployeeService {
|
||||
|
||||
@ -12,4 +15,16 @@ public interface EmployeeService {
|
||||
*/
|
||||
Employee login(EmployeeLoginDTO employeeLoginDTO);
|
||||
|
||||
/**
|
||||
* 新增员工信息
|
||||
* @param employeeDTO
|
||||
*/
|
||||
void save(EmployeeDTO employeeDTO);
|
||||
|
||||
/**
|
||||
* 分页查询
|
||||
* @param employeePageQueryDTO
|
||||
* @return
|
||||
*/
|
||||
PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO);
|
||||
}
|
||||
|
@ -1,18 +1,30 @@
|
||||
package com.sky.service.impl;
|
||||
|
||||
import com.github.pagehelper.Page;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import com.sky.constant.MessageConstant;
|
||||
import com.sky.constant.PasswordConstant;
|
||||
import com.sky.constant.StatusConstant;
|
||||
import com.sky.context.BaseContext;
|
||||
import com.sky.dto.EmployeeDTO;
|
||||
import com.sky.dto.EmployeeLoginDTO;
|
||||
import com.sky.dto.EmployeePageQueryDTO;
|
||||
import com.sky.entity.Employee;
|
||||
import com.sky.exception.AccountLockedException;
|
||||
import com.sky.exception.AccountNotFoundException;
|
||||
import com.sky.exception.BaseException;
|
||||
import com.sky.exception.PasswordErrorException;
|
||||
import com.sky.mapper.EmployeeMapper;
|
||||
import com.sky.result.PageResult;
|
||||
import com.sky.service.EmployeeService;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.DigestUtils;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class EmployeeServiceImpl implements EmployeeService {
|
||||
|
||||
@ -39,7 +51,8 @@ public class EmployeeServiceImpl implements EmployeeService {
|
||||
}
|
||||
|
||||
//密码比对
|
||||
// TODO 后期需要进行md5加密,然后再进行比对
|
||||
// 对前端传过来的明文进行md5加密对比
|
||||
password = DigestUtils.md5DigestAsHex(password.getBytes());
|
||||
if (!password.equals(employee.getPassword())) {
|
||||
//密码错误
|
||||
throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
|
||||
@ -54,4 +67,55 @@ public class EmployeeServiceImpl implements EmployeeService {
|
||||
return employee;
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增员工信息
|
||||
* @param employeeDTO
|
||||
*/
|
||||
public void save(EmployeeDTO employeeDTO) {
|
||||
String username = employeeDTO.getUsername();
|
||||
|
||||
//判断用户是否存在
|
||||
if (employeeMapper.getByUsername(username) != null) {
|
||||
throw new BaseException(username + MessageConstant.ALREADY_EXISTS);
|
||||
}
|
||||
|
||||
Employee employee = new Employee();
|
||||
|
||||
//对象属性拷贝
|
||||
BeanUtils.copyProperties(employeeDTO, employee);
|
||||
|
||||
//设置账号状态默认启用
|
||||
employee.setStatus(StatusConstant.ENABLE);
|
||||
|
||||
//设置默认密码
|
||||
employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));
|
||||
|
||||
//设置创建时间和修改时间
|
||||
employee.setCreateTime(LocalDateTime.now());
|
||||
employee.setUpdateTime(LocalDateTime.now());
|
||||
|
||||
//设置当前记录创建人和修改人id
|
||||
employee.setCreateUser(BaseContext.getCurrentId());
|
||||
employee.setUpdateUser(BaseContext.getCurrentId());
|
||||
|
||||
employeeMapper.insert(employee);
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页查询
|
||||
* @param employeePageQueryDTO
|
||||
* @return
|
||||
*/
|
||||
public PageResult pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {
|
||||
//开始分页
|
||||
PageHelper.startPage(employeePageQueryDTO.getPage(), employeePageQueryDTO.getPageSize());
|
||||
|
||||
Page<Employee> page = employeeMapper.pageQuery(employeePageQueryDTO);
|
||||
|
||||
long total = page.getTotal();
|
||||
List<Employee> records = page.getResult();
|
||||
|
||||
return new PageResult(total, records);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,4 +5,4 @@ sky:
|
||||
port: 3306
|
||||
database: sky_take_out
|
||||
username: root
|
||||
password: root
|
||||
password: 123456
|
||||
|
@ -2,4 +2,13 @@
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
|
||||
<mapper namespace="com.sky.mapper.EmployeeMapper">
|
||||
<select id="pageQuery" resultType="com.sky.entity.Employee">
|
||||
select * from employee
|
||||
<where>
|
||||
<if test="name!= null and name!= ''">
|
||||
and name like concat('%', #{name}, '%')
|
||||
</if>
|
||||
</where>
|
||||
order by create_time desc
|
||||
</select>
|
||||
</mapper>
|
||||
|
Loading…
Reference in New Issue
Block a user