初始化项目

This commit is contained in:
wanglongjie 2026-01-23 17:20:52 +08:00
commit 60c416e553
79 changed files with 5609 additions and 0 deletions

7
.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
/.idea/
/tmpclaude-6f5d-cwd
/tmpclaude-006c-cwd
/tmpclaude-27a4-cwd
/tmpclaude-2246-cwd
/tmpclaude-a7d6-cwd
/tmpclaude-b78e-cwd

124
CLAUDE.md Normal file
View File

@ -0,0 +1,124 @@
- # CLAUDE.md
此文件为 Claude Code (claude.ai/code) 在此仓库中工作时提供指导。
## 项目名称
经销商管理系统
## 项目概述
经销商管理系统是一款面向企业渠道管理的CRM系统核心解决“客户报备冲突”与“保护期归属”问题。系统通过**唯一有效报备机制**与**自动到期释放机制**,确保同一客户在同一时间只能被一个经销商跟进,并在保护期结束后自动回归可报备池,避免长期占坑,提升渠道效率与公平性。
## 技术栈
- 前端Vue 3 + TypeScript + Vite + Element Plus
- 后端Spring Boot 2.7 + MyBatis + MySQL 8
- 架构模式前后端分离MVC 分层
- 构建工具Maven后端、pnpm前端
- 代码规范ESLint + Prettier前端、SpotBugs + Checkstyle后端
## 项目结构
```
by-crm/
├── backend/ # Spring Boot 工程
│ ├── src/main/com/bycrm/
│ │ ├── controller/ # Web 层RESTful API
│ │ ├── service/ # 业务层
│ │ ├── mapper/ # MyBatis DAO
│ │ ├── entity/ # PO / DTO / VO
│ │ ├── config/ # 跨域、MyBatis、Swagger 等配置
│ │ └── ByCrmApplication.java
│ ├── src/main/resources/
│ │ ├── mapper/*.xml # SQL 映射
│ │ └── application.yml # 数据源、MyBatis、JWT、有效期等配置
│ └── pom.xml
├── frontend/ # Vue 3 工程
│ ├── src/
│ │ ├── api/ # 接口封装axios
│ │ ├── views/ # 页面级组件
│ │ ├── components/ # 通用组件
│ │ ├── router/ # Vue Router
│ │ ├── stores/ # Pinia 状态
│ │ ├── types/ # TypeScript 类型
│ │ └── utils/ # 权限、请求拦截、日期格式化
│ ├── vite.config.ts
│ └── package.json
├── sql/
│ └── init.sql # 客户表、报备表、经销商表、字典表
└── docs/
└── api.md # Swagger 导出文档
```
## 开发命令
### 后端
```bash
cd backend
mvn spring-boot:run # 本地启动(端口 8080
mvn test # 单元测试
mvn spotbugs:check # 静态检查
```
### 前端
```bash
cd frontend
pnpm install
pnpm dev # 本地启动(端口 5173
pnpm build # 生成 dist
pnpm lint # ESLint 检查
```
## 核心业务流程(供编码时参考)
1. 客户唯一性校验:新建客户时先模糊匹配名称,命中则禁止重复创建。
2. 报备申请:经销商选择客户 → 填写简要说明 → 提交。
3. 防撞单:提交瞬间校验该客户是否已存在“有效”报备,存在则直接提示冲突。
4. 审核:管理员在后台查看待审核列表 → 通过(生成保护期)/ 驳回(填写原因)。
5. 保护期:通过后自动计算到期日(默认 90 天,可配置),到期凌晨定时任务将状态置为“已失效”,客户重新变为“可报备”。
6. 数据可见性:经销商仅看“自己的客户 + 自己的报备”;管理员可看全部。
## 关键配置项
- `application.yml` 中的 `crm.report.ttl-days` 控制保护期天数。
- `crm.report.allow-overlap` 仅用于测试开关,生产必须保持 false。
- Vue 全局拦截器统一注入 JWT登录后返回权限粒度到按钮级。
## 数据库命名规范
- 表名:全小写,下划线分割,如 `customer`, `report`, `dealer`,以 `crm_` 开头。
- 字段:全小写,下划线,主键 `id`,不需要外键,外键由程序控制,时间 `created_at`, `updated_at`
- 字典:使用 `tinyint` 并配套枚举类,如 `customer_status` 0-可报备 1-保护中。
## 后续迭代方向
- 微信小程序端提交报备
- 客户公海池与主动分配
- 报备延期申请流程
- 报表:经销商活跃度、客户转化率
## API 接口说明
### 认证接口
- POST `/api/auth/login` - 用户登录
- GET `/api/auth/user/info` - 获取当前用户信息
- POST `/api/auth/logout` - 用户退出
### 客户接口
- GET `/api/customer/page` - 分页查询客户
- GET `/api/customer/{id}` - 获取客户详情
- POST `/api/customer` - 创建客户
- PUT `/api/customer/{id}` - 更新客户
- DELETE `/api/customer/{id}` - 删除客户
- GET `/api/customer/search` - 搜索客户(用于客户唯一性校验)
### 报备接口
- GET `/api/report/page` - 分页查询报备
- GET `/api/report/{id}` - 获取报备详情
- POST `/api/report` - 创建报备
- PUT `/api/report/{id}/audit` - 审核报备
- DELETE `/api/report/{id}` - 撤回报备
### 经销商接口(仅管理员)
- GET `/api/dealer/list` - 查询所有经销商
- GET `/api/dealer/{id}` - 获取经销商详情
- POST `/api/dealer` - 创建经销商
- PUT `/api/dealer/{id}` - 更新经销商
- DELETE `/api/dealer/{id}` - 删除经销商
## 默认测试账号
- 管理员:`admin` / `admin123`
- 经销商用户1`user001` / `admin123`
- 经销商用户2`user002` / `admin123`
- 经销商用户3`user003` / `admin123`

170
backend/pom.xml Normal file
View File

@ -0,0 +1,170 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.18</version>
<relativePath/>
</parent>
<groupId>com.bycrm</groupId>
<artifactId>by-crm-backend</artifactId>
<version>1.0.0</version>
<name>BY-CRM Backend</name>
<description>经销商管理系统后端服务</description>
<properties>
<java.version>1.8</java.version>
<mybatis-spring-boot.version>2.3.1</mybatis-spring-boot.version>
<druid.version>1.2.20</druid.version>
<jwt.version>0.11.5</jwt.version>
<swagger.version>3.0.0</swagger.version>
<commons-lang3.version>3.12.0</commons-lang3.version>
<hutool.version>5.8.23</hutool.version>
</properties>
<dependencies>
<!-- Spring Boot Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Validation -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<!-- MyBatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring-boot.version}</version>
</dependency>
<!-- MySQL Driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<!-- Druid 数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- JWT -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>${jwt.version}</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>${jwt.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>${jwt.version}</version>
<scope>runtime</scope>
</dependency>
<!-- Swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>${swagger.version}</version>
</dependency>
<!-- Apache Commons Lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!-- Hutool 工具类 -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- Spring Boot Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- SpotBugs (静态代码检查) -->
<dependency>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-annotations</artifactId>
<version>4.7.3</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
<!-- SpotBugs 插件 -->
<plugin>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-maven-plugin</artifactId>
<version>4.7.3.6</version>
<dependencies>
<dependency>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs</artifactId>
<version>4.7.3</version>
</dependency>
</dependencies>
</plugin>
<!-- Checkstyle 插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<configLocation>checkstyle.xml</configLocation>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<failsOnError>false</failsOnError>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,21 @@
package com.bycrm;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* 经销商管理系统主启动类
*/
@SpringBootApplication
@EnableScheduling
public class ByCrmApplication {
public static void main(String[] args) {
SpringApplication.run(ByCrmApplication.class, args);
System.out.println("\n========================================");
System.out.println("经销商管理系统启动成功!");
System.out.println("Swagger文档地址: http://localhost:8080/api/swagger-ui/index.html");
System.out.println("========================================\n");
}
}

View File

@ -0,0 +1,14 @@
package com.bycrm.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 需要认证的注解
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface AuthRequired {
}

View File

@ -0,0 +1,92 @@
package com.bycrm.common;
/**
* 系统常量
*/
public class Constants {
/**
* JWT Token 前缀
*/
public static final String TOKEN_PREFIX = "Bearer ";
/**
* JWT Token Header
*/
public static final String TOKEN_HEADER = "Authorization";
/**
* 用户信息在 Redis 中的前缀
*/
public static final String USER_TOKEN_PREFIX = "user:token:";
/**
* 客户状态 - 可报备
*/
public static final Integer CUSTOMER_STATUS_AVAILABLE = 0;
/**
* 客户状态 - 保护中
*/
public static final Integer CUSTOMER_STATUS_PROTECTED = 1;
/**
* 客户状态 - 已失效
*/
public static final Integer CUSTOMER_STATUS_EXPIRED = 2;
/**
* 报备状态 - 待审核
*/
public static final Integer REPORT_STATUS_PENDING = 0;
/**
* 报备状态 - 已通过
*/
public static final Integer REPORT_STATUS_APPROVED = 1;
/**
* 报备状态 - 已驳回
*/
public static final Integer REPORT_STATUS_REJECTED = 2;
/**
* 报备状态 - 已失效
*/
public static final Integer REPORT_STATUS_EXPIRED = 3;
/**
* 用户角色 - 管理员
*/
public static final Integer USER_ROLE_ADMIN = 0;
/**
* 用户角色 - 经销商用户
*/
public static final Integer USER_ROLE_DEALER = 1;
/**
* 用户状态 - 禁用
*/
public static final Integer USER_STATUS_DISABLED = 0;
/**
* 用户状态 - 启用
*/
public static final Integer USER_STATUS_ENABLED = 1;
/**
* 经销商状态 - 禁用
*/
public static final Integer DEALER_STATUS_DISABLED = 0;
/**
* 经销商状态 - 启用
*/
public static final Integer DEALER_STATUS_ENABLED = 1;
/**
* 默认保护期天数
*/
public static final Integer DEFAULT_PROTECT_DAYS = 90;
}

View File

@ -0,0 +1,55 @@
package com.bycrm.common;
import lombok.Data;
import java.io.Serializable;
import java.util.List;
/**
* 分页结果
*/
@Data
public class PageResult<T> implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 总记录数
*/
private Long total;
/**
* 当前页数据
*/
private List<T> records;
/**
* 当前页
*/
private Long current;
/**
* 每页大小
*/
private Long size;
/**
* 总页数
*/
private Long pages;
public PageResult() {
}
public PageResult(Long total, List<T> records, Long current, Long size) {
this.total = total;
this.records = records;
this.current = current;
this.size = size;
this.pages = (total + size - 1) / size;
}
public static <T> PageResult<T> of(Long total, List<T> records, Long current, Long size) {
return new PageResult<>(total, records, current, size);
}
}

View File

@ -0,0 +1,94 @@
package com.bycrm.common;
import lombok.Data;
import java.io.Serializable;
/**
* 统一响应结果
*/
@Data
public class Result<T> implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 状态码
*/
private Integer code;
/**
* 响应消息
*/
private String message;
/**
* 响应数据
*/
private T data;
/**
* 时间戳
*/
private Long timestamp;
public Result() {
this.timestamp = System.currentTimeMillis();
}
public Result(Integer code, String message, T data) {
this.code = code;
this.message = message;
this.data = data;
this.timestamp = System.currentTimeMillis();
}
/**
* 成功返回无数据
*/
public static <T> Result<T> success() {
return new Result<>(200, "操作成功", null);
}
/**
* 成功返回有数据
*/
public static <T> Result<T> success(T data) {
return new Result<>(200, "操作成功", data);
}
/**
* 成功返回自定义消息
*/
public static <T> Result<T> success(String message, T data) {
return new Result<>(200, message, data);
}
/**
* 失败返回
*/
public static <T> Result<T> error(String message) {
return new Result<>(500, message, null);
}
/**
* 失败返回自定义状态码
*/
public static <T> Result<T> error(Integer code, String message) {
return new Result<>(code, message, null);
}
/**
* 未授权返回
*/
public static <T> Result<T> unauthorized(String message) {
return new Result<>(401, message, null);
}
/**
* 禁止访问返回
*/
public static <T> Result<T> forbidden(String message) {
return new Result<>(403, message, null);
}
}

View File

@ -0,0 +1,54 @@
package com.bycrm.config;
import com.bycrm.common.Constants;
import com.bycrm.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 认证拦截器
*/
@Slf4j
@Component
public class AuthInterceptor implements HandlerInterceptor {
private final JwtUtil jwtUtil;
public AuthInterceptor(JwtUtil jwtUtil) {
this.jwtUtil = jwtUtil;
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 设置当前用户ID到请求属性中
String token = getTokenFromRequest(request);
if (token != null && jwtUtil.validateToken(token)) {
Long userId = jwtUtil.getUserIdFromToken(token);
request.setAttribute("currentUserId", userId);
request.setAttribute("currentUserRole", jwtUtil.getRoleFromToken(token));
request.setAttribute("currentUserDealerId", jwtUtil.getDealerIdFromToken(token));
return true;
}
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
response.setContentType("application/json;charset=UTF-8");
response.getWriter().write("{\"code\":401,\"message\":\"未授权,请先登录\"}");
return false;
}
/**
* 从请求中获取 Token
*/
private String getTokenFromRequest(HttpServletRequest request) {
String bearerToken = request.getHeader(Constants.TOKEN_HEADER);
if (bearerToken != null && bearerToken.startsWith(Constants.TOKEN_PREFIX)) {
return bearerToken.substring(Constants.TOKEN_PREFIX.length());
}
return null;
}
}

View File

@ -0,0 +1,22 @@
package com.bycrm.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* 跨域配置
*/
@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOriginPatterns("*")
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.allowedHeaders("*")
.allowCredentials(true)
.maxAge(3600);
}
}

View File

@ -0,0 +1,12 @@
package com.bycrm.config;
import org.springframework.context.annotation.Configuration;
/**
* MyBatis 配置
*/
@Configuration
public class MyBatisConfig {
// MyBatis 配置主要通过 application.yml 配置
}

View File

@ -0,0 +1,39 @@
package com.bycrm.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
/**
* Swagger 配置
*/
@Configuration
@EnableOpenApi
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.bycrm.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("经销商管理系统 API 文档")
.description("经销商管理系统 RESTful API 接口文档")
.contact(new Contact("BY CRM", "", ""))
.version("1.0.0")
.build();
}
}

View File

@ -0,0 +1,35 @@
package com.bycrm.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.Arrays;
/**
* Web MVC 配置
*/
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
private final AuthInterceptor authInterceptor;
public WebMvcConfig(AuthInterceptor authInterceptor) {
this.authInterceptor = authInterceptor;
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(authInterceptor)
.addPathPatterns("/**")
.excludePathPatterns(Arrays.asList(
"/auth/login",
"/swagger-ui/**",
"/swagger-resources/**",
"/v3/api-docs/**",
"/webjars/**",
"/error",
"/druid/**"
));
}
}

View File

@ -0,0 +1,79 @@
package com.bycrm.controller;
import com.bycrm.common.Result;
import com.bycrm.dto.LoginDTO;
import com.bycrm.service.UserService;
import com.bycrm.vo.UserInfoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
/**
* 认证控制器
*/
@Api(tags = "认证管理")
@RestController
@RequestMapping("/auth")
public class AuthController {
private final UserService userService;
public AuthController(UserService userService) {
this.userService = userService;
}
/**
* 用户登录
*/
@ApiOperation("用户登录")
@PostMapping("/login")
public Result<LoginResponse> login(@RequestBody LoginDTO loginDTO) {
String token = userService.login(loginDTO);
return Result.success(new LoginResponse(token));
}
/**
* 获取当前用户信息
*/
@ApiOperation("获取当前用户信息")
@GetMapping("/user/info")
public Result<UserInfoVO> getCurrentUser(HttpServletRequest request) {
String token = getTokenFromRequest(request);
UserInfoVO user = userService.getCurrentUser(token);
return Result.success(user);
}
/**
* 用户退出
*/
@ApiOperation("用户退出")
@PostMapping("/logout")
public Result<Void> logout() {
return Result.success();
}
/**
* 从请求中获取 Token
*/
private String getTokenFromRequest(HttpServletRequest request) {
String authorization = request.getHeader("Authorization");
if (authorization != null && authorization.startsWith("Bearer ")) {
return authorization.substring(7);
}
return null;
}
/**
* 登录响应
*/
@lombok.Data
static class LoginResponse {
private String token;
public LoginResponse(String token) {
this.token = token;
}
}
}

View File

@ -0,0 +1,106 @@
package com.bycrm.controller;
import com.bycrm.common.PageResult;
import com.bycrm.common.Result;
import com.bycrm.dto.CustomerDTO;
import com.bycrm.dto.PageQuery;
import com.bycrm.entity.Customer;
import com.bycrm.service.CustomerService;
import com.bycrm.vo.CustomerVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
/**
* 客户控制器
*/
@Api(tags = "客户管理")
@RestController
@RequestMapping("/customer")
public class CustomerController {
private final CustomerService customerService;
public CustomerController(CustomerService customerService) {
this.customerService = customerService;
}
/**
* 分页查询客户
*/
@ApiOperation("分页查询客户")
@GetMapping("/page")
public Result<PageResult<CustomerVO>> getCustomerPage(
@ApiParam("当前页") @RequestParam(defaultValue = "1") Long current,
@ApiParam("每页大小") @RequestParam(defaultValue = "10") Long size,
@ApiParam("客户名称") @RequestParam(required = false) String name,
@ApiParam("所属行业") @RequestParam(required = false) String industry,
@ApiParam("状态") @RequestParam(required = false) Integer status) {
PageQuery query = new PageQuery();
query.setCurrent(current);
query.setSize(size);
PageResult<CustomerVO> result = customerService.getCustomerPage(query, name, industry, status);
return Result.success(result);
}
/**
* 根据ID获取客户详情
*/
@ApiOperation("根据ID获取客户详情")
@GetMapping("/{id}")
public Result<CustomerVO> getCustomerById(@ApiParam("客户ID") @PathVariable Long id) {
CustomerVO customer = customerService.getCustomerById(id);
return Result.success(customer);
}
/**
* 创建客户
*/
@ApiOperation("创建客户")
@PostMapping
public Result<Customer> createCustomer(@RequestBody CustomerDTO customerDTO, HttpServletRequest request) {
Long currentUserId = (Long) request.getAttribute("currentUserId");
Customer customer = customerService.createCustomer(customerDTO, currentUserId);
return Result.success(customer);
}
/**
* 更新客户
*/
@ApiOperation("更新客户")
@PutMapping("/{id}")
public Result<Void> updateCustomer(
@ApiParam("客户ID") @PathVariable Long id,
@RequestBody CustomerDTO customerDTO,
HttpServletRequest request) {
Long currentUserId = (Long) request.getAttribute("currentUserId");
customerService.updateCustomer(id, customerDTO, currentUserId);
return Result.success();
}
/**
* 删除客户
*/
@ApiOperation("删除客户")
@DeleteMapping("/{id}")
public Result<Void> deleteCustomer(@ApiParam("客户ID") @PathVariable Long id, HttpServletRequest request) {
Long currentUserId = (Long) request.getAttribute("currentUserId");
customerService.deleteCustomer(id, currentUserId);
return Result.success();
}
/**
* 根据名称搜索客户用于客户唯一性校验
*/
@ApiOperation("根据名称搜索客户")
@GetMapping("/search")
public Result<List<Customer>> searchByName(@ApiParam("客户名称") @RequestParam String name) {
List<Customer> customers = customerService.searchByName(name);
return Result.success(customers);
}
}

View File

@ -0,0 +1,82 @@
package com.bycrm.controller;
import com.bycrm.common.Result;
import com.bycrm.dto.DealerDTO;
import com.bycrm.entity.Dealer;
import com.bycrm.service.DealerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
* 经销商控制器
*/
@Api(tags = "经销商管理")
@RestController
@RequestMapping("/dealer")
public class DealerController {
private final DealerService dealerService;
public DealerController(DealerService dealerService) {
this.dealerService = dealerService;
}
/**
* 查询所有经销商
*/
@ApiOperation("查询所有经销商")
@GetMapping("/list")
public Result<List<Dealer>> getDealerList(
@ApiParam("经销商名称") @RequestParam(required = false) String name,
@ApiParam("经销商编码") @RequestParam(required = false) String code,
@ApiParam("状态") @RequestParam(required = false) Integer status) {
List<Dealer> dealers = dealerService.getDealerList(name, code, status);
return Result.success(dealers);
}
/**
* 根据ID获取经销商
*/
@ApiOperation("根据ID获取经销商")
@GetMapping("/{id}")
public Result<Dealer> getDealerById(@ApiParam("经销商ID") @PathVariable Long id) {
Dealer dealer = dealerService.getDealerById(id);
return Result.success(dealer);
}
/**
* 创建经销商
*/
@ApiOperation("创建经销商")
@PostMapping
public Result<Void> createDealer(@RequestBody DealerDTO dealerDTO) {
dealerService.createDealer(dealerDTO);
return Result.success();
}
/**
* 更新经销商
*/
@ApiOperation("更新经销商")
@PutMapping("/{id}")
public Result<Void> updateDealer(
@ApiParam("经销商ID") @PathVariable Long id,
@RequestBody DealerDTO dealerDTO) {
dealerService.updateDealer(id, dealerDTO);
return Result.success();
}
/**
* 删除经销商
*/
@ApiOperation("删除经销商")
@DeleteMapping("/{id}")
public Result<Void> deleteDealer(@ApiParam("经销商ID") @PathVariable Long id) {
dealerService.deleteDealer(id);
return Result.success();
}
}

View File

@ -0,0 +1,99 @@
package com.bycrm.controller;
import com.bycrm.common.PageResult;
import com.bycrm.common.Result;
import com.bycrm.dto.PageQuery;
import com.bycrm.dto.ReportAuditDTO;
import com.bycrm.dto.ReportDTO;
import com.bycrm.service.ReportService;
import com.bycrm.vo.ReportVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
/**
* 报备控制器
*/
@Api(tags = "报备管理")
@RestController
@RequestMapping("/report")
public class ReportController {
private final ReportService reportService;
public ReportController(ReportService reportService) {
this.reportService = reportService;
}
/**
* 分页查询报备
*/
@ApiOperation("分页查询报备")
@GetMapping("/page")
public Result<PageResult<ReportVO>> getReportPage(
@ApiParam("当前页") @RequestParam(defaultValue = "1") Long current,
@ApiParam("每页大小") @RequestParam(defaultValue = "10") Long size,
@ApiParam("经销商ID") @RequestParam(required = false) Long dealerId,
@ApiParam("经销商名称") @RequestParam(required = false) String dealerName,
@ApiParam("客户名称") @RequestParam(required = false) String customerName,
@ApiParam("状态") @RequestParam(required = false) Integer status,
HttpServletRequest request) {
Long currentUserId = (Long) request.getAttribute("currentUserId");
PageQuery query = new PageQuery();
query.setCurrent(current);
query.setSize(size);
PageResult<ReportVO> result = reportService.getReportPage(query, dealerId, dealerName, customerName, status, currentUserId);
return Result.success(result);
}
/**
* 根据ID获取报备详情
*/
@ApiOperation("根据ID获取报备详情")
@GetMapping("/{id}")
public Result<ReportVO> getReportById(@ApiParam("报备ID") @PathVariable Long id) {
ReportVO report = reportService.getReportById(id);
return Result.success(report);
}
/**
* 创建报备
*/
@ApiOperation("创建报备")
@PostMapping
public Result<Void> createReport(@RequestBody ReportDTO reportDTO, HttpServletRequest request) {
Long currentUserId = (Long) request.getAttribute("currentUserId");
reportService.createReport(reportDTO, currentUserId);
return Result.success();
}
/**
* 审核报备
*/
@ApiOperation("审核报备")
@PutMapping("/{id}/audit")
public Result<Void> auditReport(
@ApiParam("报备ID") @PathVariable Long id,
@RequestBody ReportAuditDTO auditDTO,
HttpServletRequest request) {
Long currentUserId = (Long) request.getAttribute("currentUserId");
reportService.auditReport(id, auditDTO, currentUserId);
return Result.success();
}
/**
* 撤回报备
*/
@ApiOperation("撤回报备")
@DeleteMapping("/{id}")
public Result<Void> withdrawReport(@ApiParam("报备ID") @PathVariable Long id, HttpServletRequest request) {
Long currentUserId = (Long) request.getAttribute("currentUserId");
reportService.withdrawReport(id, currentUserId);
return Result.success();
}
}

View File

@ -0,0 +1,36 @@
package com.bycrm.dto;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import java.io.Serializable;
/**
* 客户请求 DTO
*/
@Data
public class CustomerDTO implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 客户名称
*/
@NotBlank(message = "客户名称不能为空")
private String name;
/**
* 联系电话
*/
private String phone;
/**
* 地址
*/
private String address;
/**
* 所属行业
*/
private String industry;
}

View File

@ -0,0 +1,51 @@
package com.bycrm.dto;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
/**
* 经销商请求 DTO
*/
@Data
public class DealerDTO implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 经销商名称
*/
@NotBlank(message = "经销商名称不能为空")
private String name;
/**
* 经销商编码
*/
@NotBlank(message = "经销商编码不能为空")
private String code;
/**
* 联系人
*/
@NotBlank(message = "联系人不能为空")
private String contactPerson;
/**
* 联系电话
*/
@NotBlank(message = "联系电话不能为空")
private String contactPhone;
/**
* 邮箱
*/
private String email;
/**
* 状态
*/
@NotNull(message = "状态不能为空")
private Integer status;
}

View File

@ -0,0 +1,27 @@
package com.bycrm.dto;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import java.io.Serializable;
/**
* 登录请求 DTO
*/
@Data
public class LoginDTO implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 用户名
*/
@NotBlank(message = "用户名不能为空")
private String username;
/**
* 密码
*/
@NotBlank(message = "密码不能为空")
private String password;
}

View File

@ -0,0 +1,34 @@
package com.bycrm.dto;
import lombok.Data;
import java.io.Serializable;
/**
* 分页查询基类
*/
@Data
public class PageQuery implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 当前页
*/
private Long current = 1L;
/**
* 每页大小
*/
private Long size = 10L;
/**
* 排序字段
*/
private String orderBy;
/**
* 排序方式asc/desc
*/
private String orderDirection = "desc";
}

View File

@ -0,0 +1,32 @@
package com.bycrm.dto;
import lombok.Data;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
/**
* 报备审核 DTO
*/
@Data
public class ReportAuditDTO implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 报备ID
*/
@NotNull(message = "报备ID不能为空")
private Long reportId;
/**
* 是否通过
*/
@NotNull(message = "审核结果不能为空")
private Boolean approved;
/**
* 驳回原因
*/
private String rejectReason;
}

View File

@ -0,0 +1,26 @@
package com.bycrm.dto;
import lombok.Data;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
/**
* 报备请求 DTO
*/
@Data
public class ReportDTO implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 客户ID
*/
@NotNull(message = "客户ID不能为空")
private Long customerId;
/**
* 报备说明
*/
private String description;
}

View File

@ -0,0 +1,58 @@
package com.bycrm.entity;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* 客户实体
*/
@Data
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 客户ID
*/
private Long id;
/**
* 客户名称
*/
private String name;
/**
* 联系电话
*/
private String phone;
/**
* 地址
*/
private String address;
/**
* 所属行业
*/
private String industry;
/**
* 状态0-可报备 1-保护中 2-已失效
*/
private Integer status;
/**
* 创建时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createdAt;
/**
* 更新时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime updatedAt;
}

View File

@ -0,0 +1,63 @@
package com.bycrm.entity;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* 经销商实体
*/
@Data
public class Dealer implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 经销商ID
*/
private Long id;
/**
* 经销商名称
*/
private String name;
/**
* 经销商编码
*/
private String code;
/**
* 联系人
*/
private String contactPerson;
/**
* 联系电话
*/
private String contactPhone;
/**
* 邮箱
*/
private String email;
/**
* 状态0-禁用 1-启用
*/
private Integer status;
/**
* 创建时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createdAt;
/**
* 更新时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime updatedAt;
}

View File

@ -0,0 +1,54 @@
package com.bycrm.entity;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.List;
/**
* 数据字典实体
*/
@Data
public class Dict implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 字典ID
*/
private Long id;
/**
* 字典编码
*/
private String dictCode;
/**
* 字典名称
*/
private String dictName;
/**
* 描述
*/
private String description;
/**
* 创建时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createdAt;
/**
* 更新时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime updatedAt;
/**
* 字典项列表不存储在数据库中
*/
private transient List<DictItem> items;
}

View File

@ -0,0 +1,53 @@
package com.bycrm.entity;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* 数据字典项实体
*/
@Data
public class DictItem implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 字典项ID
*/
private Long id;
/**
* 字典ID
*/
private Long dictId;
/**
* 字典项标签
*/
private String itemLabel;
/**
* 字典项值
*/
private String itemValue;
/**
* 排序
*/
private Integer sortOrder;
/**
* 创建时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createdAt;
/**
* 更新时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime updatedAt;
}

View File

@ -0,0 +1,57 @@
package com.bycrm.entity;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* 操作日志实体
*/
@Data
public class OperationLog implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 日志ID
*/
private Long id;
/**
* 用户ID
*/
private Long userId;
/**
* 用户名不存储在数据库中
*/
private transient String username;
/**
* 模块名称
*/
private String module;
/**
* 操作类型
*/
private String operation;
/**
* 操作描述
*/
private String description;
/**
* IP地址
*/
private String ip;
/**
* 创建时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createdAt;
}

View File

@ -0,0 +1,85 @@
package com.bycrm.entity;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* 报备实体
*/
@Data
public class Report implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 报备ID
*/
private Long id;
/**
* 经销商ID
*/
private Long dealerId;
/**
* 客户ID
*/
private Long customerId;
/**
* 报备说明
*/
private String description;
/**
* 状态0-待审核 1-已通过 2-已驳回 3-已失效
*/
private Integer status;
/**
* 驳回原因
*/
private String rejectReason;
/**
* 保护期开始时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime protectStartDate;
/**
* 保护期结束时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime protectEndDate;
/**
* 创建时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createdAt;
/**
* 更新时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime updatedAt;
/**
* 关联查询字段 - 经销商名称
*/
private String dealerName;
/**
* 关联查询字段 - 客户名称
*/
private String customerName;
/**
* 关联查询字段 - 客户电话
*/
private String customerPhone;
}

View File

@ -0,0 +1,73 @@
package com.bycrm.entity;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* 用户实体
*/
@Data
public class User implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 用户ID
*/
private Long id;
/**
* 用户名
*/
private String username;
/**
* 密码BCrypt加密
*/
private String password;
/**
* 真实姓名
*/
private String realName;
/**
* 关联经销商ID管理员为NULL
*/
private Long dealerId;
/**
* 角色0-管理员 1-经销商用户
*/
private Integer role;
/**
* 状态0-禁用 1-启用
*/
private Integer status;
/**
* 创建时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createdAt;
/**
* 更新时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime updatedAt;
/**
* 关联查询字段 - 经销商名称
*/
private String dealerName;
/**
* Token不存储在数据库中仅用于返回
*/
private transient String token;
}

View File

@ -0,0 +1,27 @@
package com.bycrm.exception;
import lombok.Getter;
/**
* 业务异常
*/
@Getter
public class BusinessException extends RuntimeException {
private final Integer code;
public BusinessException(String message) {
super(message);
this.code = 500;
}
public BusinessException(Integer code, String message) {
super(message);
this.code = code;
}
public BusinessException(String message, Throwable cause) {
super(message, cause);
this.code = 500;
}
}

View File

@ -0,0 +1,62 @@
package com.bycrm.exception;
import com.bycrm.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.stream.Collectors;
/**
* 全局异常处理器
*/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
/**
* 业务异常
*/
@ExceptionHandler(BusinessException.class)
public Result<Void> handleBusinessException(BusinessException e) {
log.error("业务异常:{}", e.getMessage());
return Result.error(e.getCode(), e.getMessage());
}
/**
* 参数校验异常
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
public Result<Void> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
String message = e.getBindingResult().getFieldErrors().stream()
.map(FieldError::getDefaultMessage)
.collect(Collectors.joining(", "));
log.error("参数校验异常:{}", message);
return Result.error(400, message);
}
/**
* 约束违反异常
*/
@ExceptionHandler(ConstraintViolationException.class)
public Result<Void> handleConstraintViolationException(ConstraintViolationException e) {
String message = e.getConstraintViolations().stream()
.map(ConstraintViolation::getMessage)
.collect(Collectors.joining(", "));
log.error("约束违反异常:{}", message);
return Result.error(400, message);
}
/**
* 系统异常
*/
@ExceptionHandler(Exception.class)
public Result<Void> handleException(Exception e) {
log.error("系统异常", e);
return Result.error("系统异常,请联系管理员");
}
}

View File

@ -0,0 +1,51 @@
package com.bycrm.mapper;
import com.bycrm.entity.Customer;
import com.bycrm.dto.PageQuery;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* 客户 Mapper
*/
@Mapper
public interface CustomerMapper {
/**
* 根据ID查询客户
*/
Customer selectById(@Param("id") Long id);
/**
* 根据名称模糊查询客户用于客户唯一性校验
*/
List<Customer> selectByNameLike(@Param("name") String name);
/**
* 分页查询客户
*/
List<Customer> selectPage(@Param("query") PageQuery query, @Param("name") String name,
@Param("industry") String industry, @Param("status") Integer status);
/**
* 查询客户总数
*/
Long countPage(@Param("name") String name, @Param("industry") String industry, @Param("status") Integer status);
/**
* 插入客户
*/
int insert(Customer customer);
/**
* 更新客户
*/
int update(Customer customer);
/**
* 删除客户
*/
int deleteById(@Param("id") Long id);
}

View File

@ -0,0 +1,44 @@
package com.bycrm.mapper;
import com.bycrm.entity.Dealer;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* 经销商 Mapper
*/
@Mapper
public interface DealerMapper {
/**
* 根据ID查询经销商
*/
Dealer selectById(@Param("id") Long id);
/**
* 根据编码查询经销商
*/
Dealer selectByCode(@Param("code") String code);
/**
* 查询所有经销商
*/
List<Dealer> selectList(@Param("name") String name, @Param("code") String code, @Param("status") Integer status);
/**
* 插入经销商
*/
int insert(Dealer dealer);
/**
* 更新经销商
*/
int update(Dealer dealer);
/**
* 删除经销商
*/
int deleteById(@Param("id") Long id);
}

View File

@ -0,0 +1,63 @@
package com.bycrm.mapper;
import com.bycrm.entity.Report;
import com.bycrm.dto.PageQuery;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* 报备 Mapper
*/
@Mapper
public interface ReportMapper {
/**
* 根据ID查询报备含关联信息
*/
Report selectById(@Param("id") Long id);
/**
* 查询客户的有效报备
*/
Report selectValidByCustomerId(@Param("customerId") Long customerId);
/**
* 分页查询报备
*/
List<Report> selectPage(@Param("query") PageQuery query, @Param("dealerId") Long dealerId,
@Param("dealerName") String dealerName, @Param("customerName") String customerName,
@Param("status") Integer status);
/**
* 查询报备总数
*/
Long countPage(@Param("dealerId") Long dealerId, @Param("dealerName") String dealerName,
@Param("customerName") String customerName, @Param("status") Integer status);
/**
* 插入报备
*/
int insert(Report report);
/**
* 更新报备
*/
int update(Report report);
/**
* 删除报备
*/
int deleteById(@Param("id") Long id);
/**
* 查询即将过期的报备定时任务使用
*/
List<Report> selectExpiringReports(@Param("endDate") String endDate);
/**
* 批量更新报备状态为已失效
*/
int batchUpdateExpired(@Param("ids") List<Long> ids);
}

View File

@ -0,0 +1,44 @@
package com.bycrm.mapper;
import com.bycrm.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
* 用户 Mapper
*/
@Mapper
public interface UserMapper {
/**
* 根据用户名查询用户
*/
User selectByUsername(@Param("username") String username);
/**
* 根据ID查询用户
*/
User selectById(@Param("id") Long id);
/**
* 查询所有用户
*/
List<User> selectList();
/**
* 插入用户
*/
int insert(User user);
/**
* 更新用户
*/
int update(User user);
/**
* 删除用户
*/
int deleteById(@Param("id") Long id);
}

View File

@ -0,0 +1,50 @@
package com.bycrm.service;
import com.bycrm.common.PageResult;
import com.bycrm.dto.CustomerDTO;
import com.bycrm.dto.PageQuery;
import com.bycrm.entity.Customer;
import com.bycrm.vo.CustomerVO;
import java.util.List;
/**
* 客户服务接口
*/
public interface CustomerService {
/**
* 分页查询客户
*/
PageResult<CustomerVO> getCustomerPage(PageQuery query, String name, String industry, Integer status);
/**
* 根据 ID 获取客户详情
*/
CustomerVO getCustomerById(Long id);
/**
* 创建客户
*/
Customer createCustomer(CustomerDTO customerDTO, Long currentUserId);
/**
* 更新客户
*/
void updateCustomer(Long id, CustomerDTO customerDTO, Long currentUserId);
/**
* 删除客户
*/
void deleteCustomer(Long id, Long currentUserId);
/**
* 根据名称模糊查询客户用于客户唯一性校验
*/
List<Customer> searchByName(String name);
/**
* 校验客户名称是否重复
*/
void checkCustomerNameDuplicate(String name);
}

View File

@ -0,0 +1,37 @@
package com.bycrm.service;
import com.bycrm.dto.DealerDTO;
import com.bycrm.entity.Dealer;
import java.util.List;
/**
* 经销商服务接口
*/
public interface DealerService {
/**
* 查询所有经销商
*/
List<Dealer> getDealerList(String name, String code, Integer status);
/**
* 根据 ID 获取经销商
*/
Dealer getDealerById(Long id);
/**
* 创建经销商
*/
void createDealer(DealerDTO dealerDTO);
/**
* 更新经销商
*/
void updateDealer(Long id, DealerDTO dealerDTO);
/**
* 删除经销商
*/
void deleteDealer(Long id);
}

View File

@ -0,0 +1,44 @@
package com.bycrm.service;
import com.bycrm.common.PageResult;
import com.bycrm.dto.PageQuery;
import com.bycrm.dto.ReportAuditDTO;
import com.bycrm.dto.ReportDTO;
import com.bycrm.vo.ReportVO;
/**
* 报备服务接口
*/
public interface ReportService {
/**
* 分页查询报备
*/
PageResult<ReportVO> getReportPage(PageQuery query, Long dealerId, String dealerName,
String customerName, Integer status, Long currentUserId);
/**
* 根据 ID 获取报备详情
*/
ReportVO getReportById(Long id);
/**
* 创建报备
*/
void createReport(ReportDTO reportDTO, Long currentUserId);
/**
* 审核报备
*/
void auditReport(Long id, ReportAuditDTO auditDTO, Long currentUserId);
/**
* 撤回报备
*/
void withdrawReport(Long id, Long currentUserId);
/**
* 处理过期报备定时任务调用
*/
void handleExpiredReports();
}

View File

@ -0,0 +1,31 @@
package com.bycrm.service;
import com.bycrm.dto.LoginDTO;
import com.bycrm.entity.User;
import com.bycrm.vo.UserInfoVO;
/**
* 用户服务接口
*/
public interface UserService {
/**
* 用户登录
*/
String login(LoginDTO loginDTO);
/**
* 根据 ID 获取用户信息
*/
User getUserById(Long id);
/**
* 根据用户名获取用户信息
*/
User getUserByUsername(String username);
/**
* 获取当前登录用户信息
*/
UserInfoVO getCurrentUser(String token);
}

View File

@ -0,0 +1,152 @@
package com.bycrm.service.impl;
import cn.hutool.core.util.StrUtil;
import com.bycrm.common.Constants;
import com.bycrm.common.PageResult;
import com.bycrm.dto.CustomerDTO;
import com.bycrm.dto.PageQuery;
import com.bycrm.entity.Customer;
import com.bycrm.entity.User;
import com.bycrm.exception.BusinessException;
import com.bycrm.mapper.CustomerMapper;
import com.bycrm.mapper.UserMapper;
import com.bycrm.service.CustomerService;
import com.bycrm.vo.CustomerVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.stream.Collectors;
/**
* 客户服务实现
*/
@Service
public class CustomerServiceImpl implements CustomerService {
private final CustomerMapper customerMapper;
private final UserMapper userMapper;
public CustomerServiceImpl(CustomerMapper customerMapper, UserMapper userMapper) {
this.customerMapper = customerMapper;
this.userMapper = userMapper;
}
@Override
public PageResult<CustomerVO> getCustomerPage(PageQuery query, String name, String industry, Integer status) {
// 计算偏移量
query.setOffset((query.getCurrent() - 1) * query.getSize());
List<Customer> customers = customerMapper.selectPage(query, name, industry, status);
Long total = customerMapper.countPage(name, industry, status);
List<CustomerVO> voList = customers.stream().map(customer -> {
CustomerVO vo = convertToVO(customer);
return vo;
}).collect(Collectors.toList());
return PageResult.of(total, voList, query.getCurrent(), query.getSize());
}
@Override
public CustomerVO getCustomerById(Long id) {
Customer customer = customerMapper.selectById(id);
if (customer == null) {
throw new BusinessException("客户不存在");
}
return convertToVO(customer);
}
@Override
@Transactional(rollbackFor = Exception.class)
public Customer createCustomer(CustomerDTO customerDTO, Long currentUserId) {
// 校验客户名称是否重复
checkCustomerNameDuplicate(customerDTO.getName());
Customer customer = new Customer();
BeanUtils.copyProperties(customerDTO, customer);
customer.setStatus(Constants.CUSTOMER_STATUS_AVAILABLE);
customer.setCreatedAt(LocalDateTime.now());
customer.setUpdatedAt(LocalDateTime.now());
customerMapper.insert(customer);
return customer;
}
@Override
@Transactional(rollbackFor = Exception.class)
public void updateCustomer(Long id, CustomerDTO customerDTO, Long currentUserId) {
Customer existingCustomer = customerMapper.selectById(id);
if (existingCustomer == null) {
throw new BusinessException("客户不存在");
}
// 如果修改了名称需要检查新名称是否与其他客户重复
if (!existingCustomer.getName().equals(customerDTO.getName())) {
checkCustomerNameDuplicate(customerDTO.getName());
}
Customer customer = new Customer();
BeanUtils.copyProperties(customerDTO, customer);
customer.setId(id);
customer.setUpdatedAt(LocalDateTime.now());
customerMapper.update(customer);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void deleteCustomer(Long id, Long currentUserId) {
Customer customer = customerMapper.selectById(id);
if (customer == null) {
throw new BusinessException("客户不存在");
}
if (customer.getStatus() == Constants.CUSTOMER_STATUS_PROTECTED) {
throw new BusinessException("客户处于保护期中,无法删除");
}
customerMapper.deleteById(id);
}
@Override
public List<Customer> searchByName(String name) {
return customerMapper.selectByNameLike(name);
}
@Override
public void checkCustomerNameDuplicate(String name) {
List<Customer> existingCustomers = customerMapper.selectByNameLike(name);
if (existingCustomers.stream().anyMatch(c -> c.getName().equals(name))) {
throw new BusinessException("客户名称已存在");
}
}
/**
* 转换为 VO
*/
private CustomerVO convertToVO(Customer customer) {
CustomerVO vo = new CustomerVO();
BeanUtils.copyProperties(customer, vo);
// 设置状态描述
switch (customer.getStatus()) {
case Constants.CUSTOMER_STATUS_AVAILABLE:
vo.setStatusDesc("可报备");
break;
case Constants.CUSTOMER_STATUS_PROTECTED:
vo.setStatusDesc("保护中");
break;
case Constants.CUSTOMER_STATUS_EXPIRED:
vo.setStatusDesc("已失效");
break;
default:
vo.setStatusDesc("未知");
}
return vo;
}
}

View File

@ -0,0 +1,92 @@
package com.bycrm.service.impl;
import com.bycrm.dto.DealerDTO;
import com.bycrm.entity.Dealer;
import com.bycrm.exception.BusinessException;
import com.bycrm.mapper.DealerMapper;
import com.bycrm.service.DealerService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;
/**
* 经销商服务实现
*/
@Service
public class DealerServiceImpl implements DealerService {
private final DealerMapper dealerMapper;
public DealerServiceImpl(DealerMapper dealerMapper) {
this.dealerMapper = dealerMapper;
}
@Override
public List<Dealer> getDealerList(String name, String code, Integer status) {
return dealerMapper.selectList(name, code, status);
}
@Override
public Dealer getDealerById(Long id) {
Dealer dealer = dealerMapper.selectById(id);
if (dealer == null) {
throw new BusinessException("经销商不存在");
}
return dealer;
}
@Override
@Transactional(rollbackFor = Exception.class)
public void createDealer(DealerDTO dealerDTO) {
// 检查编码是否重复
Dealer existingDealer = dealerMapper.selectByCode(dealerDTO.getCode());
if (existingDealer != null) {
throw new BusinessException("经销商编码已存在");
}
Dealer dealer = new Dealer();
BeanUtils.copyProperties(dealerDTO, dealer);
dealer.setCreatedAt(LocalDateTime.now());
dealer.setUpdatedAt(LocalDateTime.now());
dealerMapper.insert(dealer);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void updateDealer(Long id, DealerDTO dealerDTO) {
Dealer existingDealer = dealerMapper.selectById(id);
if (existingDealer == null) {
throw new BusinessException("经销商不存在");
}
// 如果修改了编码需要检查新编码是否与其他经销商重复
if (!existingDealer.getCode().equals(dealerDTO.getCode())) {
Dealer codeDealer = dealerMapper.selectByCode(dealerDTO.getCode());
if (codeDealer != null && !codeDealer.getId().equals(id)) {
throw new BusinessException("经销商编码已存在");
}
}
Dealer dealer = new Dealer();
BeanUtils.copyProperties(dealerDTO, dealer);
dealer.setId(id);
dealer.setUpdatedAt(LocalDateTime.now());
dealerMapper.update(dealer);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void deleteDealer(Long id) {
Dealer dealer = dealerMapper.selectById(id);
if (dealer == null) {
throw new BusinessException("经销商不存在");
}
dealerMapper.deleteById(id);
}
}

View File

@ -0,0 +1,233 @@
package com.bycrm.service.impl;
import com.bycrm.common.Constants;
import com.bycrm.common.PageResult;
import com.bycrm.dto.PageQuery;
import com.bycrm.dto.ReportAuditDTO;
import com.bycrm.dto.ReportDTO;
import com.bycrm.entity.Report;
import com.bycrm.entity.Customer;
import com.bycrm.entity.User;
import com.bycrm.exception.BusinessException;
import com.bycrm.mapper.CustomerMapper;
import com.bycrm.mapper.ReportMapper;
import com.bycrm.mapper.UserMapper;
import com.bycrm.service.ReportService;
import com.bycrm.vo.ReportVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.stream.Collectors;
/**
* 报备服务实现
*/
@Service
public class ReportServiceImpl implements ReportService {
private final ReportMapper reportMapper;
private final CustomerMapper customerMapper;
private final UserMapper userMapper;
@Value("${crm.report.ttl-days:90}")
private Integer protectDays;
@Value("${crm.report.allow-overlap:false}")
private Boolean allowOverlap;
public ReportServiceImpl(ReportMapper reportMapper, CustomerMapper customerMapper, UserMapper userMapper) {
this.reportMapper = reportMapper;
this.customerMapper = customerMapper;
this.userMapper = userMapper;
}
@Override
public PageResult<ReportVO> getReportPage(PageQuery query, Long dealerId, String dealerName,
String customerName, Integer status, Long currentUserId) {
// 获取当前用户
User currentUser = userMapper.selectById(currentUserId);
// 如果是经销商用户只能查看自己的报备
if (currentUser.getRole() == Constants.USER_ROLE_DEALER) {
dealerId = currentUser.getDealerId();
}
// 计算偏移量
query.setOffset((query.getCurrent() - 1) * query.getSize());
List<Report> reports = reportMapper.selectPage(query, dealerId, dealerName, customerName, status);
Long total = reportMapper.countPage(dealerId, dealerName, customerName, status);
List<ReportVO> voList = reports.stream().map(this::convertToVO).collect(Collectors.toList());
return PageResult.of(total, voList, query.getCurrent(), query.getSize());
}
@Override
public ReportVO getReportById(Long id) {
Report report = reportMapper.selectById(id);
if (report == null) {
throw new BusinessException("报备不存在");
}
return convertToVO(report);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void createReport(ReportDTO reportDTO, Long currentUserId) {
// 获取当前用户
User currentUser = userMapper.selectById(currentUserId);
if (currentUser.getDealerId() == null) {
throw new BusinessException("您未关联经销商,无法提交报备");
}
// 检查客户是否存在
Customer customer = customerMapper.selectById(reportDTO.getCustomerId());
if (customer == null) {
throw new BusinessException("客户不存在");
}
// 防撞单校验检查该客户是否已存在有效报备
Report existingReport = reportMapper.selectValidByCustomerId(reportDTO.getCustomerId());
if (existingReport != null && !allowOverlap) {
throw new BusinessException("该客户已被其他经销商报备,无法重复报备");
}
// 创建报备
Report report = new Report();
report.setDealerId(currentUser.getDealerId());
report.setCustomerId(reportDTO.getCustomerId());
report.setDescription(reportDTO.getDescription());
report.setStatus(Constants.REPORT_STATUS_PENDING);
report.setCreatedAt(LocalDateTime.now());
report.setUpdatedAt(LocalDateTime.now());
reportMapper.insert(report);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void auditReport(Long id, ReportAuditDTO auditDTO, Long currentUserId) {
// 获取当前用户
User currentUser = userMapper.selectById(currentUserId);
if (currentUser.getRole() != Constants.USER_ROLE_ADMIN) {
throw new BusinessException("只有管理员才能审核报备");
}
Report report = reportMapper.selectById(id);
if (report == null) {
throw new BusinessException("报备不存在");
}
if (report.getStatus() != Constants.REPORT_STATUS_PENDING) {
throw new BusinessException("该报备已被审核,无需重复操作");
}
LocalDateTime now = LocalDateTime.now();
if (auditDTO.getApproved()) {
// 审核通过
report.setStatus(Constants.REPORT_STATUS_APPROVED);
report.setProtectStartDate(now);
report.setProtectEndDate(now.plusDays(protectDays));
// 更新客户状态为保护中
Customer customer = customerMapper.selectById(report.getCustomerId());
customer.setStatus(Constants.CUSTOMER_STATUS_PROTECTED);
customerMapper.update(customer);
} else {
// 审核驳回
report.setStatus(Constants.REPORT_STATUS_REJECTED);
report.setRejectReason(auditDTO.getRejectReason());
}
report.setUpdatedAt(now);
reportMapper.update(report);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void withdrawReport(Long id, Long currentUserId) {
User currentUser = userMapper.selectById(currentUserId);
Report report = reportMapper.selectById(id);
if (report == null) {
throw new BusinessException("报备不存在");
}
// 只有报备提交者才能撤回
if (!report.getDealerId().equals(currentUser.getDealerId())) {
throw new BusinessException("您只能撤回自己的报备");
}
if (report.getStatus() != Constants.REPORT_STATUS_PENDING) {
throw new BusinessException("只能撤回待审核的报备");
}
reportMapper.deleteById(id);
}
@Override
@Transactional(rollbackFor = Exception.class)
public void handleExpiredReports() {
// 查询所有保护期已到的报备
String endDate = LocalDateTime.now().toString();
List<Report> expiringReports = reportMapper.selectExpiringReports(endDate);
if (expiringReports.isEmpty()) {
return;
}
// 批量更新报备状态
List<Long> ids = expiringReports.stream().map(Report::getId).collect(Collectors.toList());
reportMapper.batchUpdateExpired(ids);
// 批量更新客户状态为可报备
expiringReports.forEach(report -> {
Customer customer = customerMapper.selectById(report.getCustomerId());
if (customer != null && customer.getStatus() == Constants.CUSTOMER_STATUS_PROTECTED) {
customer.setStatus(Constants.CUSTOMER_STATUS_AVAILABLE);
customerMapper.update(customer);
}
});
}
/**
* 转换为 VO
*/
private ReportVO convertToVO(Report report) {
ReportVO vo = new ReportVO();
BeanUtils.copyProperties(report, vo);
// 设置状态描述
switch (report.getStatus()) {
case Constants.REPORT_STATUS_PENDING:
vo.setStatusDesc("待审核");
break;
case Constants.REPORT_STATUS_APPROVED:
vo.setStatusDesc("已通过");
// 计算剩余保护天数
if (report.getProtectEndDate() != null) {
long days = ChronoUnit.DAYS.between(LocalDateTime.now(), report.getProtectEndDate());
vo.setRemainDays((int) Math.max(0, days));
}
break;
case Constants.REPORT_STATUS_REJECTED:
vo.setStatusDesc("已驳回");
break;
case Constants.REPORT_STATUS_EXPIRED:
vo.setStatusDesc("已失效");
break;
default:
vo.setStatusDesc("未知");
}
return vo;
}
}

View File

@ -0,0 +1,74 @@
package com.bycrm.service.impl;
import com.bycrm.common.Constants;
import com.bycrm.entity.User;
import com.bycrm.exception.BusinessException;
import com.bycrm.mapper.UserMapper;
import com.bycrm.service.UserService;
import com.bycrm.util.JwtUtil;
import com.bycrm.vo.UserInfoVO;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
/**
* 用户服务实现
*/
@Service
public class UserServiceImpl implements UserService {
private final UserMapper userMapper;
private final JwtUtil jwtUtil;
private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
public UserServiceImpl(UserMapper userMapper, JwtUtil jwtUtil) {
this.userMapper = userMapper;
this.jwtUtil = jwtUtil;
}
@Override
public String login(com.bycrm.dto.LoginDTO loginDTO) {
User user = userMapper.selectByUsername(loginDTO.getUsername());
if (user == null) {
throw new BusinessException("用户名或密码错误");
}
if (user.getStatus() == Constants.USER_STATUS_DISABLED) {
throw new BusinessException("用户已被禁用");
}
if (!passwordEncoder.matches(loginDTO.getPassword(), user.getPassword())) {
throw new BusinessException("用户名或密码错误");
}
return jwtUtil.generateToken(user.getId(), user.getUsername(), user.getRole(), user.getDealerId());
}
@Override
public User getUserById(Long id) {
return userMapper.selectById(id);
}
@Override
public User getUserByUsername(String username) {
return userMapper.selectByUsername(username);
}
@Override
public UserInfoVO getCurrentUser(String token) {
Long userId = jwtUtil.getUserIdFromToken(token);
User user = userMapper.selectById(userId);
if (user == null) {
throw new BusinessException("用户不存在");
}
UserInfoVO vo = new UserInfoVO();
vo.setUserId(user.getId());
vo.setUsername(user.getUsername());
vo.setRealName(user.getRealName());
vo.setDealerId(user.getDealerId());
vo.setDealerName(user.getDealerName());
vo.setRole(user.getRole());
vo.setRoleDesc(user.getRole() == Constants.USER_ROLE_ADMIN ? "管理员" : "经销商用户");
return vo;
}
}

View File

@ -0,0 +1,34 @@
package com.bycrm.task;
import com.bycrm.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* 报备过期处理定时任务
*/
@Slf4j
@Component
public class ReportExpireTask {
private final ReportService reportService;
public ReportExpireTask(ReportService reportService) {
this.reportService = reportService;
}
/**
* 每天凌晨1点执行处理过期的报备
*/
@Scheduled(cron = "0 0 1 * * ?")
public void handleExpiredReports() {
log.info("开始处理过期报备...");
try {
reportService.handleExpiredReports();
log.info("过期报备处理完成");
} catch (Exception e) {
log.error("处理过期报备失败", e);
}
}
}

View File

@ -0,0 +1,128 @@
package com.bycrm.util;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.security.Keys;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.crypto.SecretKey;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* JWT 工具类
*/
@Component
public class JwtUtil {
@Value("${jwt.secret}")
private String secret;
@Value("${jwt.expiration}")
private Long expiration;
/**
* 生成 Token
*/
public String generateToken(Long userId, String username, Integer role, Long dealerId) {
Map<String, Object> claims = new HashMap<>();
claims.put("userId", userId);
claims.put("username", username);
claims.put("role", role);
claims.put("dealerId", dealerId);
return generateToken(claims);
}
/**
* 生成 Token
*/
public String generateToken(Map<String, Object> claims) {
Date now = new Date();
Date expiryDate = new Date(now.getTime() + expiration);
return Jwts.builder()
.setClaims(claims)
.setIssuedAt(now)
.setExpiration(expiryDate)
.signWith(getSigningKey(), SignatureAlgorithm.HS512)
.compact();
}
/**
* Token 中获取 Claims
*/
public Claims getClaimsFromToken(String token) {
return Jwts.parserBuilder()
.setSigningKey(getSigningKey())
.build()
.parseClaimsJws(token)
.getBody();
}
/**
* Token 中获取用户ID
*/
public Long getUserIdFromToken(String token) {
Claims claims = getClaimsFromToken(token);
return claims.get("userId", Long.class);
}
/**
* Token 中获取用户名
*/
public String getUsernameFromToken(String token) {
Claims claims = getClaimsFromToken(token);
return claims.get("username", String.class);
}
/**
* Token 中获取角色
*/
public Integer getRoleFromToken(String token) {
Claims claims = getClaimsFromToken(token);
return claims.get("role", Integer.class);
}
/**
* Token 中获取经销商ID
*/
public Long getDealerIdFromToken(String token) {
Claims claims = getClaimsFromToken(token);
return claims.get("dealerId", Long.class);
}
/**
* 验证 Token 是否过期
*/
public boolean isTokenExpired(String token) {
try {
Date expiration = getClaimsFromToken(token).getExpiration();
return expiration.before(new Date());
} catch (Exception e) {
return true;
}
}
/**
* 验证 Token
*/
public boolean validateToken(String token) {
try {
return !isTokenExpired(token);
} catch (Exception e) {
return false;
}
}
/**
* 获取签名密钥
*/
private SecretKey getSigningKey() {
byte[] keyBytes = secret.getBytes(StandardCharsets.UTF_8);
return Keys.hmacShaKeyFor(keyBytes);
}
}

View File

@ -0,0 +1,79 @@
package com.bycrm.vo;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* 客户详情 VO
*/
@Data
public class CustomerVO implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 客户ID
*/
private Long id;
/**
* 客户名称
*/
private String name;
/**
* 联系电话
*/
private String phone;
/**
* 地址
*/
private String address;
/**
* 所属行业
*/
private String industry;
/**
* 状态0-可报备 1-保护中 2-已失效
*/
private Integer status;
/**
* 状态描述
*/
private String statusDesc;
/**
* 当前报备经销商ID
*/
private Long currentDealerId;
/**
* 当前报备经销商名称
*/
private String currentDealerName;
/**
* 保护期结束时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime protectEndDate;
/**
* 创建时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createdAt;
/**
* 更新时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime updatedAt;
}

View File

@ -0,0 +1,95 @@
package com.bycrm.vo;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* 报备详情 VO
*/
@Data
public class ReportVO implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 报备ID
*/
private Long id;
/**
* 经销商ID
*/
private Long dealerId;
/**
* 经销商名称
*/
private String dealerName;
/**
* 客户ID
*/
private Long customerId;
/**
* 客户名称
*/
private String customerName;
/**
* 客户电话
*/
private String customerPhone;
/**
* 报备说明
*/
private String description;
/**
* 状态0-待审核 1-已通过 2-已驳回 3-已失效
*/
private Integer status;
/**
* 状态描述
*/
private String statusDesc;
/**
* 驳回原因
*/
private String rejectReason;
/**
* 保护期开始时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime protectStartDate;
/**
* 保护期结束时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime protectEndDate;
/**
* 剩余保护天数
*/
private Integer remainDays;
/**
* 创建时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createdAt;
/**
* 更新时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime updatedAt;
}

View File

@ -0,0 +1,49 @@
package com.bycrm.vo;
import lombok.Data;
import java.io.Serializable;
/**
* 用户信息 VO
*/
@Data
public class UserInfoVO implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 用户ID
*/
private Long userId;
/**
* 用户名
*/
private String username;
/**
* 真实姓名
*/
private String realName;
/**
* 经销商ID
*/
private Long dealerId;
/**
* 经销商名称
*/
private String dealerName;
/**
* 角色0-管理员 1-经销商用户
*/
private Integer role;
/**
* 角色描述
*/
private String roleDesc;
}

View File

@ -0,0 +1,75 @@
server:
port: 8080
servlet:
context-path: /api
spring:
application:
name: by-crm
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/by_crm?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: root
druid:
initial-size: 5
min-idle: 5
max-active: 20
max-wait: 60000
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
validation-query: SELECT 1
test-while-idle: true
test-on-borrow: false
test-on-return: false
filters: stat,wall
web-stat-filter:
enabled: true
url-pattern: /*
stat-view-servlet:
enabled: true
url-pattern: /druid/*
reset-enable: false
login-username: admin
login-password: admin123
jackson:
time-zone: GMT+8
date-format: yyyy-MM-dd HH:mm:ss
default-property-inclusion: non_null
# MyBatis 配置
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.bycrm.entity
configuration:
map-underscore-to-camel-case: true
cache-enabled: false
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# JWT 配置
jwt:
secret: by-crm-secret-key-2024-please-change-in-production
expiration: 86400000 # 24小时单位毫秒
# CRM 业务配置
crm:
report:
ttl-days: 90 # 保护期天数
allow-overlap: false # 是否允许重叠报备(生产环境必须为 false
# Swagger 配置
springfox:
documentation:
swagger-ui:
enabled: true
enabled: true
# 日志配置
logging:
level:
com.bycrm.mapper: debug
org.springframework.web: info
pattern:
console: '%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{50} - %msg%n'

View File

@ -0,0 +1,80 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bycrm.mapper.CustomerMapper">
<resultMap id="BaseResultMap" type="com.bycrm.entity.Customer">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="phone" property="phone"/>
<result column="address" property="address"/>
<result column="industry" property="industry"/>
<result column="status" property="status"/>
<result column="created_at" property="createdAt"/>
<result column="updated_at" property="updatedAt"/>
</resultMap>
<select id="selectById" resultMap="BaseResultMap">
SELECT * FROM crm_customer WHERE id = #{id}
</select>
<select id="selectByNameLike" resultMap="BaseResultMap">
SELECT * FROM crm_customer
WHERE name LIKE CONCAT('%', #{name}, '%')
LIMIT 10
</select>
<select id="selectPage" resultMap="BaseResultMap">
SELECT * FROM crm_customer
<where>
<if test="name != null and name != ''">
AND name LIKE CONCAT('%', #{name}, '%')
</if>
<if test="industry != null and industry != ''">
AND industry = #{industry}
</if>
<if test="status != null">
AND status = #{status}
</if>
</where>
ORDER BY created_at DESC
LIMIT #{query.size} OFFSET #{query.offset}
</select>
<select id="countPage" resultType="java.lang.Long">
SELECT COUNT(*) FROM crm_customer
<where>
<if test="name != null and name != ''">
AND name LIKE CONCAT('%', #{name}, '%')
</if>
<if test="industry != null and industry != ''">
AND industry = #{industry}
</if>
<if test="status != null">
AND status = #{status}
</if>
</where>
</select>
<insert id="insert" parameterType="com.bycrm.entity.Customer" useGeneratedKeys="true" keyProperty="id">
INSERT INTO crm_customer (name, phone, address, industry, status)
VALUES (#{name}, #{phone}, #{address}, #{industry}, #{status})
</insert>
<update id="update" parameterType="com.bycrm.entity.Customer">
UPDATE crm_customer
<set>
<if test="name != null and name != ''">name = #{name},</if>
<if test="phone != null">phone = #{phone},</if>
<if test="address != null">address = #{address},</if>
<if test="industry != null">industry = #{industry},</if>
<if test="status != null">status = #{status},</if>
</set>
WHERE id = #{id}
</update>
<delete id="deleteById">
DELETE FROM crm_customer WHERE id = #{id}
</delete>
</mapper>

View File

@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bycrm.mapper.DealerMapper">
<resultMap id="BaseResultMap" type="com.bycrm.entity.Dealer">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="code" property="code"/>
<result column="contact_person" property="contactPerson"/>
<result column="contact_phone" property="contactPhone"/>
<result column="email" property="email"/>
<result column="status" property="status"/>
<result column="created_at" property="createdAt"/>
<result column="updated_at" property="updatedAt"/>
</resultMap>
<select id="selectById" resultMap="BaseResultMap">
SELECT * FROM crm_dealer WHERE id = #{id}
</select>
<select id="selectByCode" resultMap="BaseResultMap">
SELECT * FROM crm_dealer WHERE code = #{code}
</select>
<select id="selectList" resultMap="BaseResultMap">
SELECT * FROM crm_dealer
<where>
<if test="name != null and name != ''">
AND name LIKE CONCAT('%', #{name}, '%')
</if>
<if test="code != null and code != ''">
AND code LIKE CONCAT('%', #{code}, '%')
</if>
<if test="status != null">
AND status = #{status}
</if>
</where>
ORDER BY created_at DESC
</select>
<insert id="insert" parameterType="com.bycrm.entity.Dealer" useGeneratedKeys="true" keyProperty="id">
INSERT INTO crm_dealer (name, code, contact_person, contact_phone, email, status)
VALUES (#{name}, #{code}, #{contactPerson}, #{contactPhone}, #{email}, #{status})
</insert>
<update id="update" parameterType="com.bycrm.entity.Dealer">
UPDATE crm_dealer
<set>
<if test="name != null and name != ''">name = #{name},</if>
<if test="code != null and code != ''">code = #{code},</if>
<if test="contactPerson != null and contactPerson != ''">contact_person = #{contactPerson},</if>
<if test="contactPhone != null and contactPhone != ''">contact_phone = #{contactPhone},</if>
<if test="email != null">email = #{email},</if>
<if test="status != null">status = #{status},</if>
</set>
WHERE id = #{id}
</update>
<delete id="deleteById">
DELETE FROM crm_dealer WHERE id = #{id}
</delete>
</mapper>

View File

@ -0,0 +1,122 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bycrm.mapper.ReportMapper">
<resultMap id="BaseResultMap" type="com.bycrm.entity.Report">
<id column="id" property="id"/>
<result column="dealer_id" property="dealerId"/>
<result column="customer_id" property="customerId"/>
<result column="description" property="description"/>
<result column="status" property="status"/>
<result column="reject_reason" property="rejectReason"/>
<result column="protect_start_date" property="protectStartDate"/>
<result column="protect_end_date" property="protectEndDate"/>
<result column="created_at" property="createdAt"/>
<result column="updated_at" property="updatedAt"/>
<result column="dealer_name" property="dealerName"/>
<result column="customer_name" property="customerName"/>
<result column="customer_phone" property="customerPhone"/>
</resultMap>
<select id="selectById" resultMap="BaseResultMap">
SELECT r.*,
d.name AS dealer_name,
c.name AS customer_name,
c.phone AS customer_phone
FROM crm_report r
LEFT JOIN crm_dealer d ON r.dealer_id = d.id
LEFT JOIN crm_customer c ON r.customer_id = c.id
WHERE r.id = #{id}
</select>
<select id="selectValidByCustomerId" resultMap="BaseResultMap">
SELECT * FROM crm_report
WHERE customer_id = #{customerId}
AND status IN (0, 1)
LIMIT 1
</select>
<select id="selectPage" resultMap="BaseResultMap">
SELECT r.*,
d.name AS dealer_name,
c.name AS customer_name,
c.phone AS customer_phone
FROM crm_report r
LEFT JOIN crm_dealer d ON r.dealer_id = d.id
LEFT JOIN crm_customer c ON r.customer_id = c.id
<where>
<if test="dealerId != null">
AND r.dealer_id = #{dealerId}
</if>
<if test="dealerName != null and dealerName != ''">
AND d.name LIKE CONCAT('%', #{dealerName}, '%')
</if>
<if test="customerName != null and customerName != ''">
AND c.name LIKE CONCAT('%', #{customerName}, '%')
</if>
<if test="status != null">
AND r.status = #{status}
</if>
</where>
ORDER BY r.created_at DESC
LIMIT #{query.size} OFFSET #{query.offset}
</select>
<select id="countPage" resultType="java.lang.Long">
SELECT COUNT(*)
FROM crm_report r
LEFT JOIN crm_dealer d ON r.dealer_id = d.id
LEFT JOIN crm_customer c ON r.customer_id = c.id
<where>
<if test="dealerId != null">
AND r.dealer_id = #{dealerId}
</if>
<if test="dealerName != null and dealerName != ''">
AND d.name LIKE CONCAT('%', #{dealerName}, '%')
</if>
<if test="customerName != null and customerName != ''">
AND c.name LIKE CONCAT('%', #{customerName}, '%')
</if>
<if test="status != null">
AND r.status = #{status}
</if>
</where>
</select>
<insert id="insert" parameterType="com.bycrm.entity.Report" useGeneratedKeys="true" keyProperty="id">
INSERT INTO crm_report (dealer_id, customer_id, description, status, protect_start_date, protect_end_date)
VALUES (#{dealerId}, #{customerId}, #{description}, #{status}, #{protectStartDate}, #{protectEndDate})
</insert>
<update id="update" parameterType="com.bycrm.entity.Report">
UPDATE crm_report
<set>
<if test="status != null">status = #{status},</if>
<if test="rejectReason != null">reject_reason = #{rejectReason},</if>
<if test="protectStartDate != null">protect_start_date = #{protectStartDate},</if>
<if test="protectEndDate != null">protect_end_date = #{protectEndDate},</if>
</set>
WHERE id = #{id}
</update>
<delete id="deleteById">
DELETE FROM crm_report WHERE id = #{id}
</delete>
<select id="selectExpiringReports" resultMap="BaseResultMap">
SELECT * FROM crm_report
WHERE status = 1
AND protect_end_date &lt;= #{endDate}
</select>
<update id="batchUpdateExpired">
UPDATE crm_report
SET status = 3
WHERE id IN
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</update>
</mapper>

View File

@ -0,0 +1,64 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bycrm.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.bycrm.entity.User">
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
<result column="real_name" property="realName"/>
<result column="dealer_id" property="dealerId"/>
<result column="role" property="role"/>
<result column="status" property="status"/>
<result column="created_at" property="createdAt"/>
<result column="updated_at" property="updatedAt"/>
<result column="dealer_name" property="dealerName"/>
</resultMap>
<select id="selectByUsername" resultMap="BaseResultMap">
SELECT u.*,
d.name AS dealer_name
FROM crm_user u
LEFT JOIN crm_dealer d ON u.dealer_id = d.id
WHERE u.username = #{username}
</select>
<select id="selectById" resultMap="BaseResultMap">
SELECT u.*,
d.name AS dealer_name
FROM crm_user u
LEFT JOIN crm_dealer d ON u.dealer_id = d.id
WHERE u.id = #{id}
</select>
<select id="selectList" resultMap="BaseResultMap">
SELECT u.*,
d.name AS dealer_name
FROM crm_user u
LEFT JOIN crm_dealer d ON u.dealer_id = d.id
ORDER BY u.created_at DESC
</select>
<insert id="insert" parameterType="com.bycrm.entity.User" useGeneratedKeys="true" keyProperty="id">
INSERT INTO crm_user (username, password, real_name, dealer_id, role, status)
VALUES (#{username}, #{password}, #{realName}, #{dealerId}, #{role}, #{status})
</insert>
<update id="update" parameterType="com.bycrm.entity.User">
UPDATE crm_user
<set>
<if test="password != null and password != ''">password = #{password},</if>
<if test="realName != null and realName != ''">real_name = #{realName},</if>
<if test="dealerId != null">dealer_id = #{dealerId},</if>
<if test="role != null">role = #{role},</if>
<if test="status != null">status = #{status},</if>
</set>
WHERE id = #{id}
</update>
<delete id="deleteById">
DELETE FROM crm_user WHERE id = #{id}
</delete>
</mapper>

25
frontend/.eslintrc.cjs Normal file
View File

@ -0,0 +1,25 @@
module.exports = {
root: true,
env: {
node: true,
browser: true,
es2021: true
},
extends: [
'eslint:recommended',
'plugin:vue/vue3-recommended',
'plugin:@typescript-eslint/recommended'
],
parser: 'vue-eslint-parser',
parserOptions: {
ecmaVersion: 2021,
parser: '@typescript-eslint/parser',
sourceType: 'module'
},
plugins: ['vue', '@typescript-eslint'],
rules: {
'vue/multi-word-component-names': 'off',
'@typescript-eslint/no-explicit-any': 'warn',
'vue/no-v-html': 'off'
}
}

8
frontend/.prettierrc Normal file
View File

@ -0,0 +1,8 @@
{
"semi": false,
"singleQuote": true,
"printWidth": 100,
"trailingComma": "none",
"arrowParens": "avoid",
"endOfLine": "lf"
}

13
frontend/index.html Normal file
View File

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>经销商管理系统</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>

39
frontend/package.json Normal file
View File

@ -0,0 +1,39 @@
{
"name": "by-crm-frontend",
"version": "1.0.0",
"description": "经销商管理系统前端",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vue-tsc && vite build",
"preview": "vite preview",
"lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore"
},
"dependencies": {
"vue": "^3.4.0",
"vue-router": "^4.2.0",
"pinia": "^2.1.0",
"pinia-plugin-persistedstate": "^3.2.0",
"axios": "^1.6.0",
"element-plus": "^2.5.0",
"@element-plus/icons-vue": "^2.3.0",
"dayjs": "^1.11.0"
},
"devDependencies": {
"@vitejs/plugin-vue": "^5.0.0",
"vite": "^5.0.0",
"vue-tsc": "^1.8.0",
"typescript": "^5.3.0",
"@types/node": "^20.10.0",
"eslint": "^8.55.0",
"eslint-plugin-vue": "^9.19.0",
"@typescript-eslint/parser": "^6.15.0",
"@typescript-eslint/eslint-plugin": "^6.15.0",
"prettier": "^3.1.0",
"sass": "^1.69.0"
},
"engines": {
"node": ">=16.0.0",
"pnpm": ">=8.0.0"
}
}

25
frontend/src/App.vue Normal file
View File

@ -0,0 +1,25 @@
<template>
<router-view />
</template>
<script setup lang="ts">
// App
</script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html,
body,
#app {
width: 100%;
height: 100%;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial,
'Noto Sans', sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol',
'Noto Color Emoji';
}
</style>

17
frontend/src/api/auth.ts Normal file
View File

@ -0,0 +1,17 @@
import { http } from '@/utils/request'
import type { LoginRequest, LoginResponse, User } from '@/types'
// 用户登录
export const login = (data: LoginRequest) => {
return http.post<LoginResponse>('/auth/login', data)
}
// 获取当前用户信息
export const getUserInfo = () => {
return http.get<User>('/auth/user/info')
}
// 用户退出
export const logout = () => {
return http.post('/auth/logout')
}

View File

@ -0,0 +1,32 @@
import { http } from '@/utils/request'
import type { Customer, CustomerForm, PageQuery, PageResult } from '@/types'
// 分页查询客户
export const getCustomerPage = (params: PageQuery & { name?: string; industry?: string; status?: number }) => {
return http.get<PageResult<Customer>>('/customer/page', { params })
}
// 根据ID获取客户详情
export const getCustomerById = (id: number) => {
return http.get<Customer>(`/customer/${id}`)
}
// 创建客户
export const createCustomer = (data: CustomerForm) => {
return http.post('/customer', data)
}
// 更新客户
export const updateCustomer = (id: number, data: CustomerForm) => {
return http.put(`/customer/${id}`, data)
}
// 删除客户
export const deleteCustomer = (id: number) => {
return http.delete(`/customer/${id}`)
}
// 根据名称搜索客户
export const searchCustomerByName = (name: string) => {
return http.get<Customer[]>('/customer/search', { params: { name } })
}

View File

@ -0,0 +1,27 @@
import { http } from '@/utils/request'
import type { Dealer } from '@/types'
// 查询所有经销商
export const getDealerList = (params?: { name?: string; code?: string; status?: number }) => {
return http.get<Dealer[]>('/dealer/list', { params })
}
// 根据ID获取经销商
export const getDealerById = (id: number) => {
return http.get<Dealer>(`/dealer/${id}`)
}
// 创建经销商
export const createDealer = (data: Partial<Dealer>) => {
return http.post('/dealer', data)
}
// 更新经销商
export const updateDealer = (id: number, data: Partial<Dealer>) => {
return http.put(`/dealer/${id}`, data)
}
// 删除经销商
export const deleteDealer = (id: number) => {
return http.delete(`/dealer/${id}`)
}

View File

@ -0,0 +1,32 @@
import { http } from '@/utils/request'
import type { Report, ReportForm, ReportAuditForm, PageQuery, PageResult } from '@/types'
// 分页查询报备
export const getReportPage = (params: PageQuery & {
dealerId?: number
dealerName?: string
customerName?: string
status?: number
}) => {
return http.get<PageResult<Report>>('/report/page', { params })
}
// 根据ID获取报备详情
export const getReportById = (id: number) => {
return http.get<Report>(`/report/${id}`)
}
// 创建报备
export const createReport = (data: ReportForm) => {
return http.post('/report', data)
}
// 审核报备
export const auditReport = (id: number, data: ReportAuditForm) => {
return http.put(`/report/${id}/audit`, data)
}
// 撤回报备
export const withdrawReport = (id: number) => {
return http.delete(`/report/${id}`)
}

24
frontend/src/main.ts Normal file
View File

@ -0,0 +1,24 @@
import { createApp } from 'vue'
import { createPinia } from 'pinia'
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import App from './App.vue'
import router from './router'
const app = createApp(App)
const pinia = createPinia()
pinia.use(piniaPluginPersistedstate)
app.use(pinia)
app.use(router)
app.use(ElementPlus)
// 注册所有图标
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
app.component(key, component)
}
app.mount('#app')

View File

@ -0,0 +1,82 @@
import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import { useUserStore } from '@/stores/user'
const routes: RouteRecordRaw[] = [
{
path: '/login',
name: 'Login',
component: () => import('@/views/Login.vue'),
meta: { title: '登录' }
},
{
path: '/',
component: () => import('@/views/Layout.vue'),
redirect: '/dashboard',
children: [
{
path: 'dashboard',
name: 'Dashboard',
component: () => import('@/views/Dashboard.vue'),
meta: { title: '首页' }
},
{
path: 'customer',
name: 'Customer',
component: () => import('@/views/Customer.vue'),
meta: { title: '客户管理' }
},
{
path: 'report',
name: 'Report',
component: () => import('@/views/Report.vue'),
meta: { title: '报备管理' }
},
{
path: 'dealer',
name: 'Dealer',
component: () => import('@/views/Dealer.vue'),
meta: { title: '经销商管理', requiresAdmin: true }
}
]
}
]
const router = createRouter({
history: createWebHistory(),
routes
})
// 路由守卫
router.beforeEach((to, from, next) => {
const userStore = useUserStore()
// 设置页面标题
document.title = `${to.meta.title || ''} - 经销商管理系统`
// 如果访问登录页,已登录则跳转到首页
if (to.path === '/login') {
if (userStore.isLoggedIn) {
next('/')
} else {
next()
}
return
}
// 其他页面需要登录
if (!userStore.isLoggedIn) {
next('/login')
return
}
// 管理员权限校验
if (to.meta.requiresAdmin && !userStore.isAdmin) {
next('/')
return
}
next()
})
export default router

View File

@ -0,0 +1,57 @@
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import { login as loginApi, getUserInfo, logout as logoutApi } from '@/api/auth'
import type { LoginRequest, User } from '@/types'
export const useUserStore = defineStore(
'user',
() => {
const token = ref<string>(localStorage.getItem('token') || '')
const userInfo = ref<User | null>(null)
const isLoggedIn = computed(() => !!token.value)
const isAdmin = computed(() => userInfo.value?.role === 0)
const isDealer = computed(() => userInfo.value?.role === 1)
// 登录
const login = async (loginForm: LoginRequest) => {
const res = await loginApi(loginForm)
token.value = res.token
localStorage.setItem('token', res.token)
await fetchUserInfo()
}
// 获取用户信息
const fetchUserInfo = async () => {
if (token.value) {
try {
userInfo.value = await getUserInfo()
} catch (error) {
console.error('获取用户信息失败', error)
logout()
}
}
}
// 退出登录
const logout = () => {
token.value = ''
userInfo.value = null
localStorage.removeItem('token')
}
return {
token,
userInfo,
isLoggedIn,
isAdmin,
isDealer,
login,
fetchUserInfo,
logout
}
},
{
persist: true
}
)

101
frontend/src/types/index.ts Normal file
View File

@ -0,0 +1,101 @@
// 用户相关类型
export interface User {
userId: number
username: string
realName: string
dealerId?: number
dealerName?: string
role: number
roleDesc: string
}
export interface LoginRequest {
username: string
password: string
}
export interface LoginResponse {
token: string
}
// 客户相关类型
export interface Customer {
id: number
name: string
phone?: string
address?: string
industry?: string
status: number
statusDesc: string
createdAt: string
updatedAt: string
}
export interface CustomerForm {
name: string
phone?: string
address?: string
industry?: string
}
// 经销商相关类型
export interface Dealer {
id: number
name: string
code: string
contactPerson: string
contactPhone: string
email?: string
status: number
createdAt: string
updatedAt: string
}
// 报备相关类型
export interface Report {
id: number
dealerId: number
dealerName: string
customerId: number
customerName: string
customerPhone?: string
description?: string
status: number
statusDesc: string
rejectReason?: string
protectStartDate?: string
protectEndDate?: string
remainDays?: number
createdAt: string
updatedAt: string
}
export interface ReportForm {
customerId: number
description?: string
}
export interface ReportAuditForm {
approved: boolean
rejectReason?: string
}
// 分页相关类型
export interface PageQuery {
current: number
size: number
}
export interface PageResult<T> {
total: number
records: T[]
current: number
size: number
pages: number
}
// 字典相关类型
export interface DictItem {
label: string
value: string | number
}

View File

@ -0,0 +1,78 @@
import axios, { type AxiosInstance, type AxiosRequestConfig, type AxiosResponse } from 'axios'
import { ElMessage } from 'element-plus'
import { useUserStore } from '@/stores/user'
// 响应数据接口
export interface ApiResponse<T = any> {
code: number
message: string
data: T
timestamp: number
}
// 创建 axios 实例
const service: AxiosInstance = axios.create({
baseURL: '/api',
timeout: 30000,
headers: {
'Content-Type': 'application/json;charset=UTF-8'
}
})
// 请求拦截器
service.interceptors.request.use(
(config) => {
const userStore = useUserStore()
if (userStore.token) {
config.headers.Authorization = `Bearer ${userStore.token}`
}
return config
},
(error) => {
return Promise.reject(error)
}
)
// 响应拦截器
service.interceptors.response.use(
(response: AxiosResponse<ApiResponse>) => {
const { data } = response
if (data.code === 200) {
return data.data
} else if (data.code === 401) {
ElMessage.error('登录已过期,请重新登录')
const userStore = useUserStore()
userStore.logout()
window.location.href = '/login'
return Promise.reject(new Error(data.message || '未授权'))
} else {
ElMessage.error(data.message || '请求失败')
return Promise.reject(new Error(data.message || '请求失败'))
}
},
(error) => {
ElMessage.error(error.message || '网络请求失败')
return Promise.reject(error)
}
)
// 封装请求方法
export const http = {
get<T = any>(url: string, config?: AxiosRequestConfig): Promise<T> {
return service.get(url, config)
},
post<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T> {
return service.post(url, data, config)
},
put<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T> {
return service.put(url, data, config)
},
delete<T = any>(url: string, config?: AxiosRequestConfig): Promise<T> {
return service.delete(url, config)
}
}
export default service

View File

@ -0,0 +1,273 @@
<template>
<div class="customer-page">
<el-card>
<template #header>
<div class="card-header">
<span>客户管理</span>
<el-button type="primary" @click="handleAdd">
<el-icon><Plus /></el-icon>
新增客户
</el-button>
</div>
</template>
<!-- 查询表单 -->
<el-form :inline="true" :model="queryForm" class="query-form">
<el-form-item label="客户名称">
<el-input v-model="queryForm.name" placeholder="请输入客户名称" clearable />
</el-form-item>
<el-form-item label="所属行业">
<el-select v-model="queryForm.industry" placeholder="请选择行业" clearable>
<el-option label="制造业" value="manufacturing" />
<el-option label="互联网" value="internet" />
<el-option label="金融" value="finance" />
<el-option label="零售" value="retail" />
<el-option label="教育" value="education" />
<el-option label="医疗" value="healthcare" />
<el-option label="其他" value="other" />
</el-select>
</el-form-item>
<el-form-item label="状态">
<el-select v-model="queryForm.status" placeholder="请选择状态" clearable>
<el-option label="可报备" :value="0" />
<el-option label="保护中" :value="1" />
<el-option label="已失效" :value="2" />
</el-select>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="handleQuery">
<el-icon><Search /></el-icon>
查询
</el-button>
<el-button @click="handleReset">重置</el-button>
</el-form-item>
</el-form>
<!-- 数据表格 -->
<el-table :data="tableData" border stripe v-loading="loading">
<el-table-column prop="name" label="客户名称" />
<el-table-column prop="phone" label="联系电话" />
<el-table-column prop="address" label="地址" />
<el-table-column prop="industry" label="所属行业">
<template #default="{ row }">
{{ getIndustryLabel(row.industry) }}
</template>
</el-table-column>
<el-table-column prop="status" label="状态">
<template #default="{ row }">
<el-tag v-if="row.status === 0" type="success">可报备</el-tag>
<el-tag v-else-if="row.status === 1" type="warning">保护中</el-tag>
<el-tag v-else type="info">已失效</el-tag>
</template>
</el-table-column>
<el-table-column prop="createdAt" label="创建时间" width="180" />
<el-table-column label="操作" width="180" fixed="right">
<template #default="{ row }">
<el-button link type="primary" @click="handleEdit(row)">编辑</el-button>
<el-button link type="danger" @click="handleDelete(row)">删除</el-button>
</template>
</el-table-column>
</el-table>
<!-- 分页 -->
<el-pagination
v-model:current-page="queryForm.current"
v-model:page-size="queryForm.size"
:total="total"
:page-sizes="[10, 20, 50, 100]"
layout="total, sizes, prev, pager, next, jumper"
@size-change="fetchData"
@current-change="fetchData"
style="margin-top: 20px; justify-content: flex-end"
/>
</el-card>
<!-- 新增/编辑对话框 -->
<el-dialog
v-model="dialogVisible"
:title="dialogTitle"
width="600px"
@close="handleDialogClose"
>
<el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
<el-form-item label="客户名称" prop="name">
<el-input v-model="formData.name" placeholder="请输入客户名称" />
</el-form-item>
<el-form-item label="联系电话" prop="phone">
<el-input v-model="formData.phone" placeholder="请输入联系电话" />
</el-form-item>
<el-form-item label="地址" prop="address">
<el-input v-model="formData.address" placeholder="请输入地址" />
</el-form-item>
<el-form-item label="所属行业" prop="industry">
<el-select v-model="formData.industry" placeholder="请选择行业" style="width: 100%">
<el-option label="制造业" value="manufacturing" />
<el-option label="互联网" value="internet" />
<el-option label="金融" value="finance" />
<el-option label="零售" value="retail" />
<el-option label="教育" value="education" />
<el-option label="医疗" value="healthcare" />
<el-option label="其他" value="other" />
</el-select>
</el-form-item>
</el-form>
<template #footer>
<el-button @click="dialogVisible = false">取消</el-button>
<el-button type="primary" @click="handleSubmit">确定</el-button>
</template>
</el-dialog>
</div>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted, computed } from 'vue'
import { ElMessage, ElMessageBox, type FormInstance, type FormRules } from 'element-plus'
import { getCustomerPage, createCustomer, updateCustomer, deleteCustomer } from '@/api/customer'
import type { Customer, CustomerForm } from '@/types'
const loading = ref(false)
const tableData = ref<Customer[]>([])
const total = ref(0)
const dialogVisible = ref(false)
const dialogTitle = computed(() => (formData.id ? '编辑客户' : '新增客户'))
const formRef = ref<FormInstance>()
const queryForm = reactive({
current: 1,
size: 10,
name: '',
industry: '',
status: undefined as number | undefined
})
const formData = reactive<CustomerForm & { id?: number }>({
name: '',
phone: '',
address: '',
industry: ''
})
const rules: FormRules = {
name: [{ required: true, message: '请输入客户名称', trigger: 'blur' }]
}
//
const getIndustryLabel = (industry: string) => {
const map: Record<string, string> = {
manufacturing: '制造业',
internet: '互联网',
finance: '金融',
retail: '零售',
education: '教育',
healthcare: '医疗',
other: '其他'
}
return map[industry] || industry
}
//
const fetchData = async () => {
loading.value = true
try {
const res = await getCustomerPage(queryForm)
tableData.value = res.records
total.value = res.total
} catch (error) {
console.error('查询失败', error)
} finally {
loading.value = false
}
}
//
const handleQuery = () => {
queryForm.current = 1
fetchData()
}
//
const handleReset = () => {
queryForm.name = ''
queryForm.industry = ''
queryForm.status = undefined
queryForm.current = 1
fetchData()
}
//
const handleAdd = () => {
dialogVisible.value = true
}
//
const handleEdit = (row: Customer) => {
Object.assign(formData, row)
dialogVisible.value = true
}
//
const handleDelete = async (row: Customer) => {
try {
await ElMessageBox.confirm('确定要删除该客户吗?', '提示', { type: 'warning' })
await deleteCustomer(row.id)
ElMessage.success('删除成功')
fetchData()
} catch (error) {
//
}
}
//
const handleSubmit = async () => {
if (!formRef.value) return
await formRef.value.validate(async (valid) => {
if (valid) {
try {
if (formData.id) {
await updateCustomer(formData.id, formData)
ElMessage.success('更新成功')
} else {
await createCustomer(formData)
ElMessage.success('创建成功')
}
dialogVisible.value = false
fetchData()
} catch (error) {
console.error('提交失败', error)
}
}
})
}
//
const handleDialogClose = () => {
formRef.value?.resetFields()
Object.assign(formData, {
name: '',
phone: '',
address: '',
industry: ''
})
delete formData.id
}
onMounted(() => {
fetchData()
})
</script>
<style scoped>
.customer-page {
width: 100%;
}
.card-header {
display: flex;
justify-content: space-between;
align-items: center;
}
.query-form {
margin-bottom: 20px;
}
</style>

View File

@ -0,0 +1,152 @@
<template>
<div class="dashboard">
<el-row :gutter="20">
<el-col :span="6">
<el-card class="stat-card">
<div class="stat-content">
<div class="stat-icon" style="background-color: #409eff">
<el-icon :size="30"><User /></el-icon>
</div>
<div class="stat-info">
<div class="stat-label">客户总数</div>
<div class="stat-value">{{ stats.customerCount }}</div>
</div>
</div>
</el-card>
</el-col>
<el-col :span="6">
<el-card class="stat-card">
<div class="stat-content">
<div class="stat-icon" style="background-color: #67c23a">
<el-icon :size="30"><Document /></el-icon>
</div>
<div class="stat-info">
<div class="stat-label">报备总数</div>
<div class="stat-value">{{ stats.reportCount }}</div>
</div>
</div>
</el-card>
</el-col>
<el-col :span="6">
<el-card class="stat-card">
<div class="stat-content">
<div class="stat-icon" style="background-color: #e6a23c">
<el-icon :size="30"><Clock /></el-icon>
</div>
<div class="stat-info">
<div class="stat-label">待审核</div>
<div class="stat-value">{{ stats.pendingCount }}</div>
</div>
</div>
</el-card>
</el-col>
<el-col :span="6">
<el-card class="stat-card">
<div class="stat-content">
<div class="stat-icon" style="background-color: #f56c6c">
<el-icon :size="30"><Shop /></el-icon>
</div>
<div class="stat-info">
<div class="stat-label">经销商数</div>
<div class="stat-value">{{ stats.dealerCount }}</div>
</div>
</div>
</el-card>
</el-col>
</el-row>
<el-card class="welcome-card" style="margin-top: 20px">
<h3>欢迎使用经销商管理系统</h3>
<p v-if="userStore.isAdmin">您是管理员可以管理所有客户报备和经销商信息</p>
<p v-else>您是经销商用户可以管理客户和提交报备申请</p>
</el-card>
</div>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useUserStore } from '@/stores/user'
import * as statisticsApi from '@/api/customer'
const userStore = useUserStore()
const stats = ref({
customerCount: 0,
reportCount: 0,
pendingCount: 0,
dealerCount: 0
})
//
onMounted(() => {
stats.value = {
customerCount: 5,
reportCount: 0,
pendingCount: 0,
dealerCount: 3
}
})
</script>
<style scoped>
.dashboard {
width: 100%;
}
.stat-card {
cursor: pointer;
transition: all 0.3s;
}
.stat-card:hover {
transform: translateY(-5px);
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}
.stat-content {
display: flex;
align-items: center;
}
.stat-icon {
width: 60px;
height: 60px;
border-radius: 8px;
display: flex;
align-items: center;
justify-content: center;
color: #fff;
margin-right: 15px;
}
.stat-info {
flex: 1;
}
.stat-label {
font-size: 14px;
color: #909399;
margin-bottom: 8px;
}
.stat-value {
font-size: 24px;
font-weight: bold;
color: #303133;
}
.welcome-card {
text-align: center;
padding: 40px;
}
.welcome-card h3 {
margin-bottom: 15px;
color: #303133;
}
.welcome-card p {
color: #606266;
line-height: 1.8;
}
</style>

View File

@ -0,0 +1,237 @@
<template>
<div class="dealer-page">
<el-card>
<template #header>
<div class="card-header">
<span>经销商管理</span>
<el-button type="primary" @click="handleAdd">
<el-icon><Plus /></el-icon>
新增经销商
</el-button>
</div>
</template>
<!-- 查询表单 -->
<el-form :inline="true" :model="queryForm" class="query-form">
<el-form-item label="经销商名称">
<el-input v-model="queryForm.name" placeholder="请输入经销商名称" clearable />
</el-form-item>
<el-form-item label="经销商编码">
<el-input v-model="queryForm.code" placeholder="请输入经销商编码" clearable />
</el-form-item>
<el-form-item label="状态">
<el-select v-model="queryForm.status" placeholder="请选择状态" clearable>
<el-option label="启用" :value="1" />
<el-option label="禁用" :value="0" />
</el-select>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="handleQuery">
<el-icon><Search /></el-icon>
查询
</el-button>
<el-button @click="handleReset">重置</el-button>
</el-form-item>
</el-form>
<!-- 数据表格 -->
<el-table :data="tableData" border stripe v-loading="loading">
<el-table-column prop="name" label="经销商名称" />
<el-table-column prop="code" label="经销商编码" />
<el-table-column prop="contactPerson" label="联系人" />
<el-table-column prop="contactPhone" label="联系电话" />
<el-table-column prop="email" label="邮箱" />
<el-table-column prop="status" label="状态">
<template #default="{ row }">
<el-tag :type="row.status === 1 ? 'success' : 'danger'">
{{ row.status === 1 ? '启用' : '禁用' }}
</el-tag>
</template>
</el-table-column>
<el-table-column prop="createdAt" label="创建时间" width="180" />
<el-table-column label="操作" width="180" fixed="right">
<template #default="{ row }">
<el-button link type="primary" @click="handleEdit(row)">编辑</el-button>
<el-button link type="danger" @click="handleDelete(row)">删除</el-button>
</template>
</el-table-column>
</el-table>
</el-card>
<!-- 新增/编辑对话框 -->
<el-dialog
v-model="dialogVisible"
:title="dialogTitle"
width="600px"
@close="handleDialogClose"
>
<el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
<el-form-item label="经销商名称" prop="name">
<el-input v-model="formData.name" placeholder="请输入经销商名称" />
</el-form-item>
<el-form-item label="经销商编码" prop="code">
<el-input v-model="formData.code" placeholder="请输入经销商编码" />
</el-form-item>
<el-form-item label="联系人" prop="contactPerson">
<el-input v-model="formData.contactPerson" placeholder="请输入联系人" />
</el-form-item>
<el-form-item label="联系电话" prop="contactPhone">
<el-input v-model="formData.contactPhone" placeholder="请输入联系电话" />
</el-form-item>
<el-form-item label="邮箱" prop="email">
<el-input v-model="formData.email" placeholder="请输入邮箱" />
</el-form-item>
<el-form-item label="状态" prop="status">
<el-radio-group v-model="formData.status">
<el-radio :label="1">启用</el-radio>
<el-radio :label="0">禁用</el-radio>
</el-radio-group>
</el-form-item>
</el-form>
<template #footer>
<el-button @click="dialogVisible = false">取消</el-button>
<el-button type="primary" @click="handleSubmit">确定</el-button>
</template>
</el-dialog>
</div>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted, computed } from 'vue'
import { ElMessage, ElMessageBox, type FormInstance, type FormRules } from 'element-plus'
import { getDealerList, createDealer, updateDealer, deleteDealer } from '@/api/dealer'
import type { Dealer } from '@/types'
const loading = ref(false)
const tableData = ref<Dealer[]>([])
const dialogVisible = ref(false)
const dialogTitle = computed(() => (formData.id ? '编辑经销商' : '新增经销商'))
const formRef = ref<FormInstance>()
const queryForm = reactive({
name: '',
code: '',
status: undefined as number | undefined
})
const formData = reactive<Partial<Dealer> & { id?: number }>({
name: '',
code: '',
contactPerson: '',
contactPhone: '',
email: '',
status: 1
})
const rules: FormRules = {
name: [{ required: true, message: '请输入经销商名称', trigger: 'blur' }],
code: [{ required: true, message: '请输入经销商编码', trigger: 'blur' }],
contactPerson: [{ required: true, message: '请输入联系人', trigger: 'blur' }],
contactPhone: [{ required: true, message: '请输入联系电话', trigger: 'blur' }]
}
//
const fetchData = async () => {
loading.value = true
try {
const res = await getDealerList(queryForm)
tableData.value = res
} catch (error) {
console.error('查询失败', error)
} finally {
loading.value = false
}
}
//
const handleQuery = () => {
fetchData()
}
//
const handleReset = () => {
queryForm.name = ''
queryForm.code = ''
queryForm.status = undefined
fetchData()
}
//
const handleAdd = () => {
dialogVisible.value = true
}
//
const handleEdit = (row: Dealer) => {
Object.assign(formData, row)
dialogVisible.value = true
}
//
const handleDelete = async (row: Dealer) => {
try {
await ElMessageBox.confirm('确定要删除该经销商吗?', '提示', { type: 'warning' })
await deleteDealer(row.id)
ElMessage.success('删除成功')
fetchData()
} catch (error) {
//
}
}
//
const handleSubmit = async () => {
if (!formRef.value) return
await formRef.value.validate(async (valid) => {
if (valid) {
try {
if (formData.id) {
await updateDealer(formData.id, formData)
ElMessage.success('更新成功')
} else {
await createDealer(formData)
ElMessage.success('创建成功')
}
dialogVisible.value = false
fetchData()
} catch (error) {
console.error('提交失败', error)
}
}
})
}
//
const handleDialogClose = () => {
formRef.value?.resetFields()
Object.assign(formData, {
name: '',
code: '',
contactPerson: '',
contactPhone: '',
email: '',
status: 1
})
delete formData.id
}
onMounted(() => {
fetchData()
})
</script>
<style scoped>
.dealer-page {
width: 100%;
}
.card-header {
display: flex;
justify-content: space-between;
align-items: center;
}
.query-form {
margin-bottom: 20px;
}
</style>

View File

@ -0,0 +1,142 @@
<template>
<el-container class="layout-container">
<el-aside width="200px">
<div class="logo">经销商管理系统</div>
<el-menu
:default-active="activeMenu"
router
background-color="#304156"
text-color="#bfcbd9"
active-text-color="#409eff"
>
<el-menu-item index="/dashboard">
<el-icon><HomeFilled /></el-icon>
<span>首页</span>
</el-menu-item>
<el-menu-item index="/customer">
<el-icon><User /></el-icon>
<span>客户管理</span>
</el-menu-item>
<el-menu-item index="/report">
<el-icon><Document /></el-icon>
<span>报备管理</span>
</el-menu-item>
<el-menu-item v-if="userStore.isAdmin" index="/dealer">
<el-icon><Shop /></el-icon>
<span>经销商管理</span>
</el-menu-item>
</el-menu>
</el-aside>
<el-container>
<el-header>
<div class="header-content">
<div class="breadcrumb">
<el-breadcrumb separator="/">
<el-breadcrumb-item>{{ currentPageTitle }}</el-breadcrumb-item>
</el-breadcrumb>
</div>
<div class="user-info">
<el-dropdown>
<span class="user-name">
<el-icon><Avatar /></el-icon>
{{ userStore.userInfo?.realName }}
<el-tag v-if="userStore.isAdmin" type="danger" size="small" style="margin-left: 8px">
管理员
</el-tag>
<el-tag v-else type="success" size="small" style="margin-left: 8px">经销商</el-tag>
</span>
<template #dropdown>
<el-dropdown-menu>
<el-dropdown-item @click="handleLogout">
<el-icon><SwitchButton /></el-icon>
退出登录
</el-dropdown-item>
</el-dropdown-menu>
</template>
</el-dropdown>
</div>
</div>
</el-header>
<el-main>
<router-view />
</el-main>
</el-container>
</el-container>
</template>
<script setup lang="ts">
import { computed } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessageBox } from 'element-plus'
import { useUserStore } from '@/stores/user'
const route = useRoute()
const router = useRouter()
const userStore = useUserStore()
const activeMenu = computed(() => route.path)
const currentPageTitle = computed(() => route.meta.title as string || '首页')
const handleLogout = async () => {
try {
await ElMessageBox.confirm('确定要退出登录吗?', '提示', {
type: 'warning'
})
userStore.logout()
router.push('/login')
} catch {
// 退
}
}
</script>
<style scoped>
.layout-container {
height: 100%;
}
.el-aside {
background-color: #304156;
color: #fff;
}
.logo {
height: 60px;
line-height: 60px;
text-align: center;
font-size: 18px;
font-weight: bold;
color: #fff;
background-color: #2b3a4b;
}
.el-header {
background-color: #fff;
border-bottom: 1px solid #e6e6e6;
display: flex;
align-items: center;
padding: 0 20px;
}
.header-content {
width: 100%;
display: flex;
justify-content: space-between;
align-items: center;
}
.user-name {
display: flex;
align-items: center;
cursor: pointer;
}
.user-name .el-icon {
margin-right: 5px;
}
.el-main {
background-color: #f0f2f5;
padding: 20px;
}
</style>

View File

@ -0,0 +1,106 @@
<template>
<div class="login-container">
<div class="login-box">
<h2 class="login-title">经销商管理系统</h2>
<el-form ref="loginFormRef" :model="loginForm" :rules="rules" class="login-form">
<el-form-item prop="username">
<el-input
v-model="loginForm.username"
placeholder="请输入用户名"
prefix-icon="User"
size="large"
/>
</el-form-item>
<el-form-item prop="password">
<el-input
v-model="loginForm.password"
type="password"
placeholder="请输入密码"
prefix-icon="Lock"
size="large"
@keyup.enter="handleLogin"
/>
</el-form-item>
<el-form-item>
<el-button type="primary" size="large" :loading="loading" @click="handleLogin" class="login-btn">
登录
</el-button>
</el-form-item>
</el-form>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import { useUserStore } from '@/stores/user'
const router = useRouter()
const userStore = useUserStore()
const loginFormRef = ref<FormInstance>()
const loading = ref(false)
const loginForm = reactive({
username: '',
password: ''
})
const rules: FormRules = {
username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
}
const handleLogin = async () => {
if (!loginFormRef.value) return
await loginFormRef.value.validate(async (valid) => {
if (valid) {
loading.value = true
try {
await userStore.login(loginForm)
ElMessage.success('登录成功')
router.push('/')
} catch (error) {
console.error('登录失败', error)
} finally {
loading.value = false
}
}
})
}
</script>
<style scoped>
.login-container {
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
.login-box {
width: 400px;
padding: 40px;
background: white;
border-radius: 10px;
box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}
.login-title {
text-align: center;
margin-bottom: 30px;
color: #333;
}
.login-form {
margin-top: 20px;
}
.login-btn {
width: 100%;
}
</style>

View File

@ -0,0 +1,348 @@
<template>
<div class="report-page">
<el-card>
<template #header>
<div class="card-header">
<span>报备管理</span>
<el-button type="primary" @click="handleAdd" v-if="!userStore.isAdmin">
<el-icon><Plus /></el-icon>
提交报备
</el-button>
</div>
</template>
<!-- 查询表单 -->
<el-form :inline="true" :model="queryForm" class="query-form">
<el-form-item label="客户名称">
<el-input v-model="queryForm.customerName" placeholder="请输入客户名称" clearable />
</el-form-item>
<el-form-item label="状态">
<el-select v-model="queryForm.status" placeholder="请选择状态" clearable>
<el-option label="待审核" :value="0" />
<el-option label="已通过" :value="1" />
<el-option label="已驳回" :value="2" />
<el-option label="已失效" :value="3" />
</el-select>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="handleQuery">
<el-icon><Search /></el-icon>
查询
</el-button>
<el-button @click="handleReset">重置</el-button>
</el-form-item>
</el-form>
<!-- 数据表格 -->
<el-table :data="tableData" border stripe v-loading="loading">
<el-table-column prop="dealerName" label="经销商" />
<el-table-column prop="customerName" label="客户名称" />
<el-table-column prop="customerPhone" label="联系电话" />
<el-table-column prop="description" label="报备说明" show-overflow-tooltip />
<el-table-column prop="status" label="状态">
<template #default="{ row }">
<el-tag v-if="row.status === 0" type="warning">待审核</el-tag>
<el-tag v-else-if="row.status === 1" type="success">已通过</el-tag>
<el-tag v-else-if="row.status === 2" type="danger">已驳回</el-tag>
<el-tag v-else type="info">已失效</el-tag>
</template>
</el-table-column>
<el-table-column prop="protectEndDate" label="保护期截止" width="180">
<template #default="{ row }">
{{ row.protectEndDate || '-' }}
</template>
</el-table-column>
<el-table-column prop="createdAt" label="创建时间" width="180" />
<el-table-column label="操作" width="200" fixed="right">
<template #default="{ row }">
<el-button link type="primary" @click="handleView(row)">查看</el-button>
<el-button
v-if="userStore.isAdmin && row.status === 0"
link
type="success"
@click="handleAudit(row)"
>
审核
</el-button>
<el-button
v-if="!userStore.isAdmin && row.status === 0"
link
type="danger"
@click="handleWithdraw(row)"
>
撤回
</el-button>
</template>
</el-table-column>
</el-table>
<!-- 分页 -->
<el-pagination
v-model:current-page="queryForm.current"
v-model:page-size="queryForm.size"
:total="total"
:page-sizes="[10, 20, 50, 100]"
layout="total, sizes, prev, pager, next, jumper"
@size-change="fetchData"
@current-change="fetchData"
style="margin-top: 20px; justify-content: flex-end"
/>
</el-card>
<!-- 提交报备对话框 -->
<el-dialog v-model="dialogVisible" title="提交报备" width="600px" @close="handleDialogClose">
<el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
<el-form-item label="客户" prop="customerId">
<el-select
v-model="formData.customerId"
placeholder="请选择客户"
filterable
remote
:remote-method="searchCustomer"
:loading="searchLoading"
style="width: 100%"
>
<el-option
v-for="item in customerOptions"
:key="item.id"
:label="item.name"
:value="item.id"
/>
</el-select>
</el-form-item>
<el-form-item label="报备说明" prop="description">
<el-input
v-model="formData.description"
type="textarea"
:rows="4"
placeholder="请输入报备说明"
/>
</el-form-item>
</el-form>
<template #footer>
<el-button @click="dialogVisible = false">取消</el-button>
<el-button type="primary" @click="handleSubmit">提交</el-button>
</template>
</el-dialog>
<!-- 审核对话框 -->
<el-dialog v-model="auditDialogVisible" title="审核报备" width="600px">
<el-form ref="auditFormRef" :model="auditForm" label-width="100px">
<el-form-item label="审核结果">
<el-radio-group v-model="auditForm.approved">
<el-radio :label="true">通过</el-radio>
<el-radio :label="false">驳回</el-radio>
</el-radio-group>
</el-form-item>
<el-form-item label="驳回原因" prop="rejectReason" v-if="!auditForm.approved">
<el-input
v-model="auditForm.rejectReason"
type="textarea"
:rows="4"
placeholder="请输入驳回原因"
/>
</el-form-item>
</el-form>
<template #footer>
<el-button @click="auditDialogVisible = false">取消</el-button>
<el-button type="primary" @click="handleAuditSubmit">确定</el-button>
</template>
</el-dialog>
</div>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox, type FormInstance, type FormRules } from 'element-plus'
import { useUserStore } from '@/stores/user'
import { getReportPage, createReport, auditReport, withdrawReport } from '@/api/report'
import { searchCustomerByName } from '@/api/customer'
import type { Report, ReportForm } from '@/types'
const userStore = useUserStore()
const loading = ref(false)
const tableData = ref<Report[]>([])
const total = ref(0)
const dialogVisible = ref(false)
const auditDialogVisible = ref(false)
const auditReportId = ref<number>()
const formRef = ref<FormInstance>()
const auditFormRef = ref<FormInstance>()
const searchLoading = ref(false)
const customerOptions = ref<Array<{ id: number; name: string }>>([])
const queryForm = reactive({
current: 1,
size: 10,
customerName: '',
status: undefined as number | undefined
})
const formData = reactive<ReportForm>({
customerId: 0,
description: ''
})
const auditForm = reactive({
approved: true,
rejectReason: ''
})
const rules: FormRules = {
customerId: [{ required: true, message: '请选择客户', trigger: 'change' }]
}
//
const fetchData = async () => {
loading.value = true
try {
const res = await getReportPage(queryForm)
tableData.value = res.records
total.value = res.total
} catch (error) {
console.error('查询失败', error)
} finally {
loading.value = false
}
}
//
const searchCustomer = async (query: string) => {
if (!query) return
searchLoading.value = true
try {
const res = await searchCustomerByName(query)
customerOptions.value = res
} catch (error) {
console.error('搜索客户失败', error)
} finally {
searchLoading.value = false
}
}
//
const handleQuery = () => {
queryForm.current = 1
fetchData()
}
//
const handleReset = () => {
queryForm.customerName = ''
queryForm.status = undefined
queryForm.current = 1
fetchData()
}
//
const handleAdd = () => {
dialogVisible.value = true
}
//
const handleView = (row: Report) => {
ElMessageBox.alert(
`
<div style="line-height: 2;">
<p><strong>经销商</strong>${row.dealerName}</p>
<p><strong>客户名称</strong>${row.customerName}</p>
<p><strong>联系电话</strong>${row.customerPhone || '-'}</p>
<p><strong>报备说明</strong>${row.description || '-'}</p>
<p><strong>状态</strong>${row.statusDesc}</p>
<p><strong>驳回原因</strong>${row.rejectReason || '-'}</p>
<p><strong>保护期</strong>${row.protectStartDate || '-'} ~ ${row.protectEndDate || '-'}</p>
</div>
`,
'报备详情',
{
dangerouslyUseHTMLString: true,
confirmButtonText: '关闭'
}
)
}
//
const handleAudit = (row: Report) => {
auditReportId.value = row.id
auditForm.approved = true
auditForm.rejectReason = ''
auditDialogVisible.value = true
}
//
const handleWithdraw = async (row: Report) => {
try {
await ElMessageBox.confirm('确定要撤回该报备吗?', '提示', { type: 'warning' })
await withdrawReport(row.id)
ElMessage.success('撤回成功')
fetchData()
} catch (error) {
//
}
}
//
const handleSubmit = async () => {
if (!formRef.value) return
await formRef.value.validate(async (valid) => {
if (valid) {
try {
await createReport(formData)
ElMessage.success('提交成功')
dialogVisible.value = false
fetchData()
} catch (error) {
console.error('提交失败', error)
}
}
})
}
//
const handleAuditSubmit = async () => {
if (!auditReportId.value) return
if (!auditForm.approved && !auditForm.rejectReason) {
ElMessage.warning('请输入驳回原因')
return
}
try {
await auditReport(auditReportId.value, auditForm)
ElMessage.success('审核成功')
auditDialogVisible.value = false
fetchData()
} catch (error) {
console.error('审核失败', error)
}
}
//
const handleDialogClose = () => {
formRef.value?.resetFields()
Object.assign(formData, {
customerId: 0,
description: ''
})
customerOptions.value = []
}
onMounted(() => {
fetchData()
})
</script>
<style scoped>
.report-page {
width: 100%;
}
.card-header {
display: flex;
justify-content: space-between;
align-items: center;
}
.query-form {
margin-bottom: 20px;
}
</style>

7
frontend/src/vite-env.d.ts vendored Normal file
View File

@ -0,0 +1,7 @@
/// <reference types="vite/client" />
declare module '*.vue' {
import type { DefineComponent } from 'vue'
const component: DefineComponent<{}, {}, any>
export default component
}

31
frontend/tsconfig.json Normal file
View File

@ -0,0 +1,31 @@
{
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"module": "ESNext",
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"skipLibCheck": true,
/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "preserve",
/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
/* Path alias */
"baseUrl": ".",
"paths": {
"@/*": ["src/*"]
}
},
"include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],
"references": [{ "path": "./tsconfig.node.json" }]
}

View File

@ -0,0 +1,10 @@
{
"compilerOptions": {
"composite": true,
"skipLibCheck": true,
"module": "ESNext",
"moduleResolution": "bundler",
"allowSyntheticDefaultImports": true
},
"include": ["vite.config.ts"]
}

34
frontend/vite.config.ts Normal file
View File

@ -0,0 +1,34 @@
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
resolve: {
alias: {
'@': resolve(__dirname, 'src')
}
},
server: {
port: 5173,
proxy: {
'/api': {
target: 'http://localhost:8080',
changeOrigin: true
}
}
},
build: {
outDir: 'dist',
sourcemap: false,
rollupOptions: {
output: {
manualChunks: {
'element-plus': ['element-plus'],
'vue-vendor': ['vue', 'vue-router', 'pinia']
}
}
}
}
})

161
sql/init.sql Normal file
View File

@ -0,0 +1,161 @@
-- 经销商管理系统数据库初始化脚本
-- 数据库版本MySQL 8.0
CREATE DATABASE IF NOT EXISTS by_crm DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE by_crm;
-- 经销商表
CREATE TABLE IF NOT EXISTS crm_dealer (
id BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '经销商ID',
name VARCHAR(100) NOT NULL COMMENT '经销商名称',
code VARCHAR(50) NOT NULL UNIQUE COMMENT '经销商编码',
contact_person VARCHAR(50) NOT NULL COMMENT '联系人',
contact_phone VARCHAR(20) NOT NULL COMMENT '联系电话',
email VARCHAR(100) COMMENT '邮箱',
status TINYINT NOT NULL DEFAULT 1 COMMENT '状态0-禁用 1-启用',
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
INDEX idx_code (code),
INDEX idx_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='经销商表';
-- 客户表
CREATE TABLE IF NOT EXISTS crm_customer (
id BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '客户ID',
name VARCHAR(100) NOT NULL COMMENT '客户名称',
phone VARCHAR(20) COMMENT '联系电话',
address VARCHAR(255) COMMENT '地址',
industry VARCHAR(100) COMMENT '所属行业',
status TINYINT NOT NULL DEFAULT 0 COMMENT '状态0-可报备 1-保护中 2-已失效',
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
INDEX idx_name (name),
INDEX idx_status (status),
INDEX idx_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='客户表';
-- 报备表
CREATE TABLE IF NOT EXISTS crm_report (
id BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '报备ID',
dealer_id BIGINT NOT NULL COMMENT '经销商ID',
customer_id BIGINT NOT NULL COMMENT '客户ID',
description VARCHAR(500) COMMENT '报备说明',
status TINYINT NOT NULL DEFAULT 0 COMMENT '状态0-待审核 1-已通过 2-已驳回 3-已失效',
reject_reason VARCHAR(255) COMMENT '驳回原因',
protect_start_date DATETIME COMMENT '保护期开始时间',
protect_end_date DATETIME COMMENT '保护期结束时间',
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
INDEX idx_dealer_id (dealer_id),
INDEX idx_customer_id (customer_id),
INDEX idx_status (status),
INDEX idx_protect_end_date (protect_end_date),
UNIQUE KEY uk_customer_valid (customer_id, status) COMMENT '同一客户只能有一个有效报备'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='报备表';
-- 用户表(管理员和经销商用户)
CREATE TABLE IF NOT EXISTS crm_user (
id BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '用户ID',
username VARCHAR(50) NOT NULL UNIQUE COMMENT '用户名',
password VARCHAR(255) NOT NULL COMMENT '密码BCrypt加密',
real_name VARCHAR(50) NOT NULL COMMENT '真实姓名',
dealer_id BIGINT COMMENT '关联经销商ID管理员为NULL',
role TINYINT NOT NULL COMMENT '角色0-管理员 1-经销商用户',
status TINYINT NOT NULL DEFAULT 1 COMMENT '状态0-禁用 1-启用',
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
INDEX idx_username (username),
INDEX idx_dealer_id (dealer_id),
INDEX idx_role (role)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户表';
-- 数据字典表
CREATE TABLE IF NOT EXISTS crm_dict (
id BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '字典ID',
dict_code VARCHAR(50) NOT NULL UNIQUE COMMENT '字典编码',
dict_name VARCHAR(100) NOT NULL COMMENT '字典名称',
description VARCHAR(255) COMMENT '描述',
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='数据字典表';
-- 数据字典项表
CREATE TABLE IF NOT EXISTS crm_dict_item (
id BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '字典项ID',
dict_id BIGINT NOT NULL COMMENT '字典ID',
item_label VARCHAR(100) NOT NULL COMMENT '字典项标签',
item_value VARCHAR(50) NOT NULL COMMENT '字典项值',
sort_order INT NOT NULL DEFAULT 0 COMMENT '排序',
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
INDEX idx_dict_id (dict_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='数据字典项表';
-- 操作日志表
CREATE TABLE IF NOT EXISTS crm_operation_log (
id BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '日志ID',
user_id BIGINT NOT NULL COMMENT '用户ID',
module VARCHAR(50) NOT NULL COMMENT '模块名称',
operation VARCHAR(50) NOT NULL COMMENT '操作类型',
description VARCHAR(500) COMMENT '操作描述',
ip VARCHAR(50) COMMENT 'IP地址',
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
INDEX idx_user_id (user_id),
INDEX idx_module (module),
INDEX idx_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='操作日志表';
-- 插入初始数据
-- 插入默认管理员用户名admin密码admin123BCrypt加密后的值
INSERT INTO crm_user (username, password, real_name, dealer_id, role, status) VALUES
('admin', '$2a$10$ZK5LlFqFZ5.LpFJj/YqxJ.X5JD4JlSfHz.FGG8XnP/YjV6LvVJz0q', '系统管理员', NULL, 0, 1);
-- 插入测试经销商
INSERT INTO crm_dealer (name, code, contact_person, contact_phone, email, status) VALUES
('北京科技有限公司', 'DLR001', '张三', '13800138001', 'zhangsan@example.com', 1),
('上海贸易有限公司', 'DLR002', '李四', '13800138002', 'lisi@example.com', 1),
('深圳实业有限公司', 'DLR003', '王五', '13800138003', 'wangwu@example.com', 1);
-- 插入测试经销商用户
INSERT INTO crm_user (username, password, real_name, dealer_id, role, status) VALUES
('user001', '$2a$10$ZK5LlFqFZ5.LpFJj/YqxJ.X5JD4JlSfHz.FGG8XnP/YjV6LvVJz0q', '张三', 1, 1, 1),
('user002', '$2a$10$ZK5LlFqFZ5.LpFJj/YqxJ.X5JD4JlSfHz.FGG8XnP/YjV6LvVJz0q', '李四', 2, 1, 1),
('user003', '$2a$10$ZK5LlFqFZ5.LpFJj/YqxJ.X5JD4JlSfHz.FGG8XnP/YjV6LvVJz0q', '王五', 3, 1, 1);
-- 插入数据字典
INSERT INTO crm_dict (dict_code, dict_name, description) VALUES
('customer_status', '客户状态', '客户的状态信息'),
('report_status', '报备状态', '报备单的状态信息'),
('customer_industry', '客户行业', '客户所属行业分类');
-- 插入客户状态字典项
INSERT INTO crm_dict_item (dict_id, item_label, item_value, sort_order) VALUES
(1, '可报备', '0', 0),
(1, '保护中', '1', 1),
(1, '已失效', '2', 2);
-- 插入报备状态字典项
INSERT INTO crm_dict_item (dict_id, item_label, item_value, sort_order) VALUES
(2, '待审核', '0', 0),
(2, '已通过', '1', 1),
(2, '已驳回', '2', 2),
(2, '已失效', '3', 3);
-- 插入客户行业字典项
INSERT INTO crm_dict_item (dict_id, item_label, item_value, sort_order) VALUES
(3, '制造业', 'manufacturing', 0),
(3, '互联网', 'internet', 1),
(3, '金融', 'finance', 2),
(3, '零售', 'retail', 3),
(3, '教育', 'education', 4),
(3, '医疗', 'healthcare', 5),
(3, '其他', 'other', 6);
-- 插入测试客户
INSERT INTO crm_customer (name, phone, address, industry, status) VALUES
('阿里巴巴集团', '0571-12345678', '浙江省杭州市余杭区', 'internet', 0),
('腾讯科技', '0755-87654321', '广东省深圳市南山区', 'internet', 0),
('华为技术', '0755-12345678', '广东省深圳市龙岗区', 'manufacturing', 0),
('京东集团', '010-66666666', '北京市大兴区', 'retail', 0),
('平安保险', '0755-55555555', '广东省深圳市福田区', 'finance', 0);