506 lines
21 KiB
Java
506 lines
21 KiB
Java
package com.ruoyi.system.controller;
|
||
|
||
import com.ruoyi.common.core.controller.BaseController;
|
||
import com.ruoyi.common.core.domain.AjaxResult;
|
||
import com.ruoyi.common.utils.StringUtils;
|
||
import com.ruoyi.system.ControllerUtil.AppletControllerUtil;
|
||
import com.ruoyi.system.ControllerUtil.AppletLoginUtil;
|
||
import com.ruoyi.system.ControllerUtil.InvoiceUtil;
|
||
import com.ruoyi.system.domain.Users;
|
||
import com.ruoyi.system.domain.UsersInvoiceInfo;
|
||
import com.ruoyi.system.service.IUsersService;
|
||
import com.ruoyi.system.service.IUsersInvoiceInfoService;
|
||
import org.springframework.beans.factory.annotation.Autowired;
|
||
import org.springframework.web.bind.annotation.*;
|
||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||
import com.fasterxml.jackson.core.type.TypeReference;
|
||
|
||
import javax.servlet.http.HttpServletRequest;
|
||
import java.util.ArrayList;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
|
||
/**
|
||
* 发票管理控制器
|
||
*
|
||
* 提供发票相关的API接口,包括:
|
||
* 1. 发票中心数据获取
|
||
* 2. 待开票订单查询
|
||
* 3. 已开票记录查询
|
||
* 4. 批量开票功能
|
||
* 5. 发票信息管理
|
||
* 6. 发票统计数据
|
||
*
|
||
* @author Mr. Zhang Pan
|
||
* @version 1.0
|
||
* @date 2025-01-26
|
||
*/
|
||
@RestController
|
||
@RequestMapping("/api/user/invoice")
|
||
public class AppleInvoiceController extends BaseController {
|
||
|
||
@Autowired
|
||
private IUsersService usersService;
|
||
|
||
@Autowired
|
||
private IUsersInvoiceInfoService usersInvoiceInfoService;
|
||
|
||
private ObjectMapper objectMapper = new ObjectMapper();
|
||
|
||
/**
|
||
* 获取用户发票中心数据
|
||
*
|
||
* @param params 请求参数
|
||
* @param request HTTP请求对象
|
||
* @return 发票中心数据
|
||
*/
|
||
@PostMapping("/center")
|
||
public AjaxResult getUserInvoiceCenter(@RequestBody Map<String, Object> params, HttpServletRequest request) {
|
||
try {
|
||
// 1. 验证用户登录状态
|
||
String token = request.getHeader("token");
|
||
Map<String, Object> userValidation = AppletLoginUtil.validateUserToken(token, usersService);
|
||
if (!(Boolean) userValidation.get("valid")) {
|
||
return AppletControllerUtil.appletUnauthorized();
|
||
}
|
||
|
||
// 2. 获取用户信息
|
||
Users user = (Users) userValidation.get("user");
|
||
if (user == null) {
|
||
return AppletControllerUtil.appletWarning("用户信息获取失败");
|
||
}
|
||
|
||
// 3. 获取分页参数
|
||
int page = params.get("page") != null ? Integer.parseInt(params.get("page").toString()) : 1;
|
||
int limit = params.get("limit") != null ? Integer.parseInt(params.get("limit").toString()) : 10;
|
||
|
||
// 4. 获取发票中心数据
|
||
Map<String, Object> invoiceCenterData = InvoiceUtil.getUserInvoiceCenter(user.getId(), page, limit);
|
||
|
||
return AppletControllerUtil.appletSuccess(invoiceCenterData);
|
||
|
||
} catch (Exception e) {
|
||
return AppletControllerUtil.appletError("获取发票中心数据失败:" + e.getMessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取待开票订单列表
|
||
*
|
||
* @param params 请求参数
|
||
* @param request HTTP请求对象
|
||
* @return 待开票订单列表
|
||
*/
|
||
@PostMapping("/pending")
|
||
public AjaxResult getPendingInvoiceOrders(@RequestBody Map<String, Object> params, HttpServletRequest request) {
|
||
try {
|
||
// 1. 验证用户登录状态
|
||
String token = request.getHeader("token");
|
||
Map<String, Object> userValidation = AppletLoginUtil.validateUserToken(token, usersService);
|
||
if (!(Boolean) userValidation.get("valid")) {
|
||
return AppletControllerUtil.appletUnauthorized();
|
||
}
|
||
|
||
// 2. 获取用户信息
|
||
Users user = (Users) userValidation.get("user");
|
||
if (user == null) {
|
||
return AppletControllerUtil.appletWarning("用户信息获取失败");
|
||
}
|
||
|
||
// 3. 获取分页参数
|
||
int page = params.get("page") != null ? Integer.parseInt(params.get("page").toString()) : 1;
|
||
int limit = params.get("limit") != null ? Integer.parseInt(params.get("limit").toString()) : 10;
|
||
|
||
// 4. 获取待开票订单列表
|
||
List<Map<String, Object>> pendingOrders = InvoiceUtil.getPendingInvoiceOrders(user.getId(), page, limit);
|
||
|
||
return AppletControllerUtil.appletSuccess(pendingOrders);
|
||
|
||
} catch (Exception e) {
|
||
return AppletControllerUtil.appletError("获取待开票订单列表失败:" + e.getMessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取已开票列表
|
||
*
|
||
* @param params 请求参数
|
||
* @param request HTTP请求对象
|
||
* @return 已开票列表
|
||
*/
|
||
@PostMapping("/completed")
|
||
public AjaxResult getCompletedInvoices(@RequestBody Map<String, Object> params, HttpServletRequest request) {
|
||
try {
|
||
// 1. 验证用户登录状态
|
||
String token = request.getHeader("token");
|
||
Map<String, Object> userValidation = AppletLoginUtil.validateUserToken(token, usersService);
|
||
if (!(Boolean) userValidation.get("valid")) {
|
||
return AppletControllerUtil.appletUnauthorized();
|
||
}
|
||
|
||
// 2. 获取用户信息
|
||
Users user = (Users) userValidation.get("user");
|
||
if (user == null) {
|
||
return AppletControllerUtil.appletWarning("用户信息获取失败");
|
||
}
|
||
|
||
// 3. 获取分页参数
|
||
int page = params.get("page") != null ? Integer.parseInt(params.get("page").toString()) : 1;
|
||
int limit = params.get("limit") != null ? Integer.parseInt(params.get("limit").toString()) : 10;
|
||
|
||
// 4. 获取已开票列表
|
||
List<Map<String, Object>> completedInvoices = InvoiceUtil.getCompletedInvoices(user.getId(), page, limit);
|
||
|
||
return AppletControllerUtil.appletSuccess(completedInvoices);
|
||
|
||
} catch (Exception e) {
|
||
return AppletControllerUtil.appletError("获取已开票列表失败:" + e.getMessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 开具发票接口(支持批量开票)
|
||
*
|
||
* @param params 发票参数
|
||
* @param request HTTP请求对象
|
||
* @return 开票结果
|
||
*/
|
||
@PostMapping("/create")
|
||
public AjaxResult createInvoice(@RequestBody Map<String, Object> params, HttpServletRequest request) {
|
||
try {
|
||
// 调试日志:输出接收到的原始参数
|
||
System.out.println("接收到的原始参数: " + params);
|
||
|
||
// 1. 验证用户登录状态
|
||
String token = request.getHeader("token");
|
||
Map<String, Object> userValidation = AppletLoginUtil.validateUserToken(token, usersService);
|
||
if (!(Boolean) userValidation.get("valid")) {
|
||
return AppletControllerUtil.appletUnauthorized();
|
||
}
|
||
|
||
// 2. 获取用户信息
|
||
Users user = (Users) userValidation.get("user");
|
||
if (user == null) {
|
||
return AppletControllerUtil.appletWarning("用户信息获取失败");
|
||
}
|
||
|
||
// 3. 验证必要参数
|
||
List<String> orderIds = new ArrayList<>();
|
||
|
||
// 支持多种参数格式:orderIds数组、orderIds字符串数组、单个orderId
|
||
if (params.get("orderIds") != null) {
|
||
Object orderIdsObj = params.get("orderIds");
|
||
|
||
if (orderIdsObj instanceof List) {
|
||
// 批量开票:orderIds数组
|
||
List<?> orderIdList = (List<?>) orderIdsObj;
|
||
for (Object orderIdObj : orderIdList) {
|
||
if (orderIdObj != null) {
|
||
orderIds.add(orderIdObj.toString());
|
||
}
|
||
}
|
||
} else if (orderIdsObj instanceof String) {
|
||
// 处理字符串格式的数组,如 "['B1750646653464697','B1750413502322711']"
|
||
String orderIdsStr = orderIdsObj.toString();
|
||
|
||
// 去掉外层的方括号和引号
|
||
orderIdsStr = orderIdsStr.trim();
|
||
if (orderIdsStr.startsWith("[") && orderIdsStr.endsWith("]")) {
|
||
orderIdsStr = orderIdsStr.substring(1, orderIdsStr.length() - 1);
|
||
}
|
||
|
||
// 按逗号分割并清理每个订单ID
|
||
String[] orderIdArray = orderIdsStr.split(",");
|
||
for (String orderId : orderIdArray) {
|
||
orderId = orderId.trim();
|
||
// 去掉单引号或双引号
|
||
if ((orderId.startsWith("'") && orderId.endsWith("'")) ||
|
||
(orderId.startsWith("\"") && orderId.endsWith("\""))) {
|
||
orderId = orderId.substring(1, orderId.length() - 1);
|
||
}
|
||
if (StringUtils.isNotEmpty(orderId)) {
|
||
orderIds.add(orderId);
|
||
}
|
||
}
|
||
}
|
||
} else if (params.get("orderId") != null) {
|
||
// 单个开票:orderId字符串(兼容旧版本)
|
||
orderIds.add(params.get("orderId").toString());
|
||
}
|
||
|
||
if (orderIds.isEmpty()) {
|
||
return AppletControllerUtil.appletWarning("请选择要开票的订单");
|
||
}
|
||
|
||
// 调试日志:输出解析后的订单ID列表
|
||
System.out.println("解析后的订单ID列表: " + orderIds);
|
||
|
||
// 4. 调用批量开票方法
|
||
return InvoiceUtil.createBatchInvoice(user.getId(), orderIds, params);
|
||
|
||
} catch (Exception e) {
|
||
return AppletControllerUtil.appletError("开具发票失败:" + e.getMessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取用户已保存的发票信息
|
||
*
|
||
* @param request HTTP请求对象
|
||
* @return 发票信息
|
||
*/
|
||
@GetMapping("/saved")
|
||
public AjaxResult getUserSavedInvoiceInfo(HttpServletRequest request) {
|
||
try {
|
||
// 1. 验证用户登录状态
|
||
String token = request.getHeader("token");
|
||
Map<String, Object> userValidation = AppletLoginUtil.validateUserToken(token, usersService);
|
||
if (!(Boolean) userValidation.get("valid")) {
|
||
return AppletControllerUtil.appletUnauthorized();
|
||
}
|
||
|
||
// 2. 获取用户信息
|
||
Users user = (Users) userValidation.get("user");
|
||
if (user == null) {
|
||
return AppletControllerUtil.appletWarning("用户信息获取失败");
|
||
}
|
||
|
||
// 3. 获取用户发票信息列表
|
||
List<Map<String, Object>> invoiceInfoList = InvoiceUtil.getUserInvoiceInfoList(user.getId());
|
||
|
||
return AppletControllerUtil.appletSuccess(invoiceInfoList);
|
||
|
||
} catch (Exception e) {
|
||
return AppletControllerUtil.appletError("获取发票信息失败:" + e.getMessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取发票统计数据
|
||
*
|
||
* @param request HTTP请求对象
|
||
* @return 统计数据
|
||
*/
|
||
@GetMapping("/statistics")
|
||
public AjaxResult getInvoiceStatistics(HttpServletRequest request) {
|
||
try {
|
||
// 1. 验证用户登录状态
|
||
String token = request.getHeader("token");
|
||
Map<String, Object> userValidation = AppletLoginUtil.validateUserToken(token, usersService);
|
||
if (!(Boolean) userValidation.get("valid")) {
|
||
return AppletControllerUtil.appletUnauthorized();
|
||
}
|
||
|
||
// 2. 获取用户信息
|
||
Users user = (Users) userValidation.get("user");
|
||
if (user == null) {
|
||
return AppletControllerUtil.appletWarning("用户信息获取失败");
|
||
}
|
||
|
||
// 3. 获取统计数据
|
||
Map<String, Object> statistics = InvoiceUtil.getInvoiceStatistics(user.getId());
|
||
|
||
return AppletControllerUtil.appletSuccess(statistics);
|
||
|
||
} catch (Exception e) {
|
||
return AppletControllerUtil.appletError("获取发票统计数据失败:" + e.getMessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 保存或更新用户发票信息
|
||
*
|
||
* @param params 发票信息参数
|
||
* @param request HTTP请求对象
|
||
* @return 保存结果
|
||
*/
|
||
@PostMapping("/info")
|
||
public AjaxResult saveOrUpdateUserInvoiceInfo(@RequestBody Map<String, Object> params, HttpServletRequest request) {
|
||
try {
|
||
// 1. 验证用户登录状态
|
||
String token = request.getHeader("token");
|
||
Map<String, Object> userValidation = AppletLoginUtil.validateUserToken(token, usersService);
|
||
if (!(Boolean) userValidation.get("valid")) {
|
||
return AppletControllerUtil.appletUnauthorized();
|
||
}
|
||
|
||
// 2. 获取用户信息
|
||
Users user = (Users) userValidation.get("user");
|
||
if (user == null) {
|
||
return AppletControllerUtil.appletWarning("用户信息获取失败");
|
||
}
|
||
|
||
// 3. 验证必要参数
|
||
if (params.get("invoiceTitle") == null || StringUtils.isEmpty(params.get("invoiceTitle").toString())) {
|
||
return AppletControllerUtil.appletWarning("发票抬头不能为空");
|
||
}
|
||
|
||
if (params.get("category") == null) {
|
||
return AppletControllerUtil.appletWarning("发票类别不能为空");
|
||
}
|
||
|
||
Integer category = Integer.valueOf(params.get("category").toString());
|
||
String invoiceTitle = params.get("invoiceTitle").toString();
|
||
|
||
// 4. 根据类别验证参数
|
||
if (category == 0) {
|
||
// 个人发票验证
|
||
if (invoiceTitle.length() > 50) {
|
||
return AppletControllerUtil.appletWarning("个人发票抬头长度不能超过50个字符");
|
||
}
|
||
|
||
// 检查是否填写了企业字段
|
||
if (StringUtils.isNotEmpty((String) params.get("taxNumber"))) {
|
||
return AppletControllerUtil.appletWarning("个人发票不能填写纳税人识别号");
|
||
}
|
||
if (StringUtils.isNotEmpty((String) params.get("address"))) {
|
||
return AppletControllerUtil.appletWarning("个人发票不能填写单位地址");
|
||
}
|
||
if (StringUtils.isNotEmpty((String) params.get("phone"))) {
|
||
return AppletControllerUtil.appletWarning("个人发票不能填写联系电话");
|
||
}
|
||
if (StringUtils.isNotEmpty((String) params.get("bankName"))) {
|
||
return AppletControllerUtil.appletWarning("个人发票不能填写开户银行");
|
||
}
|
||
if (StringUtils.isNotEmpty((String) params.get("bankAccount"))) {
|
||
return AppletControllerUtil.appletWarning("个人发票不能填写银行账号");
|
||
}
|
||
if (StringUtils.isNotEmpty((String) params.get("wechat"))) {
|
||
return AppletControllerUtil.appletWarning("个人发票不能填写微信号");
|
||
}
|
||
} else if (category == 1) {
|
||
// 企业发票验证
|
||
if (StringUtils.isEmpty((String) params.get("taxNumber"))) {
|
||
return AppletControllerUtil.appletWarning("企业发票必须填写纳税人识别号");
|
||
}
|
||
if (StringUtils.isEmpty((String) params.get("address"))) {
|
||
return AppletControllerUtil.appletWarning("企业发票必须填写单位地址");
|
||
}
|
||
if (StringUtils.isEmpty((String) params.get("phone"))) {
|
||
return AppletControllerUtil.appletWarning("企业发票必须填写联系电话");
|
||
}
|
||
if (StringUtils.isEmpty((String) params.get("bankName"))) {
|
||
return AppletControllerUtil.appletWarning("企业发票必须填写开户银行");
|
||
}
|
||
if (StringUtils.isEmpty((String) params.get("bankAccount"))) {
|
||
return AppletControllerUtil.appletWarning("企业发票必须填写银行账号");
|
||
}
|
||
} else {
|
||
return AppletControllerUtil.appletWarning("发票类别参数错误");
|
||
}
|
||
|
||
// 5. 保存发票信息(简化实现)
|
||
return AppletControllerUtil.appletSuccess("发票信息保存成功");
|
||
|
||
} catch (Exception e) {
|
||
return AppletControllerUtil.appletError("保存发票信息失败:" + e.getMessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取用户发票信息列表
|
||
*
|
||
* @param request HTTP请求对象
|
||
* @return 发票信息列表
|
||
*/
|
||
@GetMapping("/list")
|
||
public AjaxResult getUserInvoiceInfoList(HttpServletRequest request) {
|
||
try {
|
||
// 1. 验证用户登录状态
|
||
String token = request.getHeader("token");
|
||
Map<String, Object> userValidation = AppletLoginUtil.validateUserToken(token, usersService);
|
||
if (!(Boolean) userValidation.get("valid")) {
|
||
return AppletControllerUtil.appletUnauthorized();
|
||
}
|
||
|
||
// 2. 获取用户信息
|
||
Users user = (Users) userValidation.get("user");
|
||
if (user == null) {
|
||
return AppletControllerUtil.appletWarning("用户信息获取失败");
|
||
}
|
||
|
||
// 3. 获取用户发票信息列表
|
||
List<Map<String, Object>> invoiceInfoList = InvoiceUtil.getUserInvoiceInfoList(user.getId());
|
||
|
||
return AppletControllerUtil.appletSuccess(invoiceInfoList);
|
||
|
||
} catch (Exception e) {
|
||
return AppletControllerUtil.appletError("获取发票信息列表失败:" + e.getMessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 删除用户发票信息
|
||
*
|
||
* @param id 发票信息ID
|
||
* @param request HTTP请求对象
|
||
* @return 删除结果
|
||
*/
|
||
@DeleteMapping("/info/{id}")
|
||
public AjaxResult deleteUserInvoiceInfo(@PathVariable("id") Integer id, HttpServletRequest request) {
|
||
try {
|
||
// 1. 验证用户登录状态
|
||
String token = request.getHeader("token");
|
||
Map<String, Object> userValidation = AppletLoginUtil.validateUserToken(token, usersService);
|
||
if (!(Boolean) userValidation.get("valid")) {
|
||
return AppletControllerUtil.appletUnauthorized();
|
||
}
|
||
|
||
// 2. 获取用户信息
|
||
Users user = (Users) userValidation.get("user");
|
||
if (user == null) {
|
||
return AppletControllerUtil.appletWarning("用户信息获取失败");
|
||
}
|
||
|
||
// 3. 删除发票信息(简化实现)
|
||
return AppletControllerUtil.appletSuccess("发票信息删除成功");
|
||
|
||
} catch (Exception e) {
|
||
return AppletControllerUtil.appletError("删除发票信息失败:" + e.getMessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 根据发票ID获取发票详情
|
||
* @param id 发票ID
|
||
* @param request HTTP请求对象
|
||
* @return 发票详情
|
||
*/
|
||
@GetMapping("/detail/{id}")
|
||
public Object getInvoiceDetail(@PathVariable("id") Integer id, HttpServletRequest request) {
|
||
try {
|
||
String token = request.getHeader("token");
|
||
Map<String, Object> userValidation = AppletLoginUtil.validateUserToken(token, usersService);
|
||
if (!(Boolean) userValidation.get("valid")) {
|
||
return AppletControllerUtil.appletUnauthorized();
|
||
}
|
||
UsersInvoiceInfo invoiceInfo = usersInvoiceInfoService.selectUsersInvoiceInfoById(id);
|
||
if (invoiceInfo == null) {
|
||
return AppletControllerUtil.appletWarning("未找到该发票信息");
|
||
}
|
||
// 只返回指定字段
|
||
Map<String, Object> data = new java.util.HashMap<>();
|
||
data.put("id", invoiceInfo.getId());
|
||
data.put("uid", invoiceInfo.getUid());
|
||
data.put("invoiceTitle", invoiceInfo.getInvoiceTitle());
|
||
data.put("taxNumber", invoiceInfo.getTaxNumber());
|
||
data.put("bankName", invoiceInfo.getBankName());
|
||
data.put("bankAccount", invoiceInfo.getBankAccount());
|
||
data.put("address", invoiceInfo.getAddress());
|
||
data.put("phone", invoiceInfo.getPhone());
|
||
data.put("email", invoiceInfo.getEmail());
|
||
data.put("wechat", invoiceInfo.getWechat());
|
||
data.put("type", invoiceInfo.getType());
|
||
data.put("category", invoiceInfo.getCategory());
|
||
data.put("invoicemoney", invoiceInfo.getInvoicemoney());
|
||
data.put("invoicetext", invoiceInfo.getInvoicetext());
|
||
data.put("orderid", invoiceInfo.getOrderid());
|
||
data.put("status", invoiceInfo.getStatus());
|
||
data.put("filedata", invoiceInfo.getFiledata());
|
||
return AppletControllerUtil.appletSuccess(data);
|
||
} catch (Exception e) {
|
||
return AppletControllerUtil.appletError("获取发票详情失败:" + e.getMessage());
|
||
}
|
||
}
|
||
}
|