Files
mp-java/src/main/java/com/gxwebsoft/credit/controller/CreditDeliveryNoticeController.java
赵忠林 eaea99a1e9 feat(controller): 添加硬删除功能并替换现有删除方法
- 在 BatchImportSupport 中新增 hardRemoveById 和 hardRemoveByIds 方法实现物理删除
- 替换所有控制器中的删除方法调用为新的硬删除方法
- 硬删除方法支持通过实体类和ID进行单个或批量物理删除
- 实现了分块处理大量ID的批量删除功能,避免数据库限制
- 保持 MyBatis-Plus 拦截器兼容性以
2026-02-14 18:28:47 +08:00

389 lines
16 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.gxwebsoft.credit.controller;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.credit.entity.CreditDeliveryNotice;
import com.gxwebsoft.credit.param.CreditDeliveryNoticeImportParam;
import com.gxwebsoft.credit.param.CreditDeliveryNoticeParam;
import com.gxwebsoft.credit.service.CreditCompanyService;
import com.gxwebsoft.credit.service.CreditCompanyRecordCountService;
import com.gxwebsoft.credit.service.CreditDeliveryNoticeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* 送达公告司法大数据控制器
*
* @author 科技小王子
* @since 2025-12-19 19:49:52
*/
@Tag(name = "送达公告司法大数据管理")
@RestController
@RequestMapping("/api/credit/credit-delivery-notice")
public class CreditDeliveryNoticeController extends BaseController {
@Resource
private CreditDeliveryNoticeService creditDeliveryNoticeService;
@Resource
private BatchImportSupport batchImportSupport;
@Resource
private CreditCompanyService creditCompanyService;
@Resource
private CreditCompanyRecordCountService creditCompanyRecordCountService;
@Operation(summary = "分页查询送达公告司法大数据")
@GetMapping("/page")
public ApiResult<PageResult<CreditDeliveryNotice>> page(CreditDeliveryNoticeParam param) {
// 使用关联查询
return success(creditDeliveryNoticeService.pageRel(param));
}
@Operation(summary = "查询全部送达公告司法大数据")
@GetMapping()
public ApiResult<List<CreditDeliveryNotice>> list(CreditDeliveryNoticeParam param) {
// 使用关联查询
return success(creditDeliveryNoticeService.listRel(param));
}
@Operation(summary = "根据id查询送达公告司法大数据")
@GetMapping("/{id}")
public ApiResult<CreditDeliveryNotice> get(@PathVariable("id") Integer id) {
// 使用关联查询
return success(creditDeliveryNoticeService.getByIdRel(id));
}
@PreAuthorize("hasAuthority('credit:creditDeliveryNotice:save')")
@OperationLog
@Operation(summary = "添加送达公告司法大数据")
@PostMapping()
public ApiResult<?> save(@RequestBody CreditDeliveryNotice creditDeliveryNotice) {
// 记录当前登录用户id
// User loginUser = getLoginUser();
// if (loginUser != null) {
// creditDeliveryNotice.setUserId(loginUser.getUserId());
// }
if (creditDeliveryNoticeService.save(creditDeliveryNotice)) {
return success("添加成功");
}
return fail("添加失败");
}
@PreAuthorize("hasAuthority('credit:creditDeliveryNotice:update')")
@OperationLog
@Operation(summary = "修改送达公告司法大数据")
@PutMapping()
public ApiResult<?> update(@RequestBody CreditDeliveryNotice creditDeliveryNotice) {
if (creditDeliveryNoticeService.updateById(creditDeliveryNotice)) {
return success("修改成功");
}
return fail("修改失败");
}
@PreAuthorize("hasAuthority('credit:creditDeliveryNotice:remove')")
@OperationLog
@Operation(summary = "删除送达公告司法大数据")
@DeleteMapping("/{id}")
public ApiResult<?> remove(@PathVariable("id") Integer id) {
if (batchImportSupport.hardRemoveById(CreditDeliveryNotice.class, id)) {
return success("删除成功");
}
return fail("删除失败");
}
@PreAuthorize("hasAuthority('credit:creditDeliveryNotice:save')")
@OperationLog
@Operation(summary = "批量添加送达公告司法大数据")
@PostMapping("/batch")
public ApiResult<?> saveBatch(@RequestBody List<CreditDeliveryNotice> list) {
if (creditDeliveryNoticeService.saveBatch(list)) {
return success("添加成功");
}
return fail("添加失败");
}
@PreAuthorize("hasAuthority('credit:creditDeliveryNotice:update')")
@OperationLog
@Operation(summary = "批量修改送达公告司法大数据")
@PutMapping("/batch")
public ApiResult<?> removeBatch(@RequestBody BatchParam<CreditDeliveryNotice> batchParam) {
if (batchParam.update(creditDeliveryNoticeService, "id")) {
return success("修改成功");
}
return fail("修改失败");
}
@PreAuthorize("hasAuthority('credit:creditDeliveryNotice:remove')")
@OperationLog
@Operation(summary = "批量删除送达公告司法大数据")
@DeleteMapping("/batch")
public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
if (batchImportSupport.hardRemoveByIds(CreditDeliveryNotice.class, ids)) {
return success("删除成功");
}
return fail("删除失败");
}
/**
* 根据企业名称匹配企业并更新 companyId匹配 CreditCompany.name / CreditCompany.matchName
*
* <p>默认仅更新 companyId=0 的记录;如需覆盖更新,传 onlyNull=false。</p>
*/
@PreAuthorize("hasAuthority('credit:creditDeliveryNotice:update')")
@OperationLog
@Operation(summary = "根据企业名称匹配并更新companyId")
@PostMapping("/company-id/refresh")
public ApiResult<Map<String, Object>> refreshCompanyIdByCompanyName(
@RequestParam(value = "onlyNull", required = false, defaultValue = "true") Boolean onlyNull,
@RequestParam(value = "limit", required = false) Integer limit
) {
User loginUser = getLoginUser();
Integer currentTenantId = loginUser != null ? loginUser.getTenantId() : null;
// Party columns may contain multiple roles/names; match if any company name is contained in the text.
// Priority: 原告/上诉人 > 被告/被上诉人 > 其他当事人/第三人
BatchImportSupport.CompanyIdRefreshStats stats = batchImportSupport.refreshCompanyIdByCompanyNameContainedInText(
creditDeliveryNoticeService,
creditCompanyService,
currentTenantId,
onlyNull,
limit,
CreditDeliveryNotice::getId,
CreditDeliveryNotice::setId,
CreditDeliveryNotice::getCompanyId,
CreditDeliveryNotice::setCompanyId,
CreditDeliveryNotice::getHasData,
CreditDeliveryNotice::setHasData,
CreditDeliveryNotice::getTenantId,
CreditDeliveryNotice::new,
CreditDeliveryNotice::getPlaintiffAppellant,
CreditDeliveryNotice::getAppellee,
CreditDeliveryNotice::getOtherPartiesThirdParty
);
if (!stats.anyDataRead) {
return success("无可更新数据", stats.toMap());
}
return success("更新完成,更新" + stats.updated + "", stats.toMap());
}
/**
* 批量导入送达公告司法大数据
*/
@PreAuthorize("hasAuthority('credit:creditDeliveryNotice:save')")
@Operation(summary = "批量导入送达公告司法大数据")
@PostMapping("/import")
public ApiResult<List<String>> importBatch(@RequestParam("file") MultipartFile file,
@RequestParam(value = "companyId", required = false) Integer companyId) {
List<String> errorMessages = new ArrayList<>();
int successCount = 0;
Set<Integer> touchedCompanyIds = new HashSet<>();
try {
int sheetIndex = ExcelImportSupport.findSheetIndex(file, "送达公告", 0);
ExcelImportSupport.ImportResult<CreditDeliveryNoticeImportParam> importResult = ExcelImportSupport.read(
file, CreditDeliveryNoticeImportParam.class, this::isEmptyImportRow, sheetIndex);
List<CreditDeliveryNoticeImportParam> list = importResult.getData();
int usedTitleRows = importResult.getTitleRows();
int usedHeadRows = importResult.getHeadRows();
int usedSheetIndex = importResult.getSheetIndex();
if (CollectionUtils.isEmpty(list)) {
return fail("未读取到数据,请确认模板表头与示例格式一致", null);
}
User loginUser = getLoginUser();
Integer currentUserId = loginUser != null ? loginUser.getUserId() : null;
Integer currentTenantId = loginUser != null ? loginUser.getTenantId() : null;
// URL 通常以超链接形式存在于“案号”列里
Map<String, String> urlByCaseNumber = ExcelImportSupport.readHyperlinksByHeaderKey(
file, usedSheetIndex, usedTitleRows, usedHeadRows, "案号");
final int chunkSize = 500;
final int mpBatchSize = 500;
List<CreditDeliveryNotice> chunkItems = new ArrayList<>(chunkSize);
List<Integer> chunkRowNumbers = new ArrayList<>(chunkSize);
for (int i = 0; i < list.size(); i++) {
CreditDeliveryNoticeImportParam param = list.get(i);
try {
CreditDeliveryNotice item = convertImportParamToEntity(param);
if (!ImportHelper.isBlank(item.getCaseNumber())) {
String link = urlByCaseNumber.get(item.getCaseNumber().trim());
if (link != null && !link.isEmpty()) {
item.setUrl(link);
}
}
if (item.getCompanyId() == null && companyId != null) {
item.setCompanyId(companyId);
}
if (item.getUserId() == null && currentUserId != null) {
item.setUserId(currentUserId);
}
if (item.getTenantId() == null && currentTenantId != null) {
item.setTenantId(currentTenantId);
}
if (item.getStatus() == null) {
item.setStatus(0);
}
if (item.getRecommend() == null) {
item.setRecommend(0);
}
if (item.getDeleted() == null) {
item.setDeleted(0);
}
int excelRowNumber = i + 1 + usedTitleRows + usedHeadRows;
if (ImportHelper.isBlank(item.getCaseNumber())) {
errorMessages.add("" + excelRowNumber + "行:案号不能为空");
continue;
}
if (item.getCompanyId() != null && item.getCompanyId() > 0) {
touchedCompanyIds.add(item.getCompanyId());
}
chunkItems.add(item);
chunkRowNumbers.add(excelRowNumber);
if (chunkItems.size() >= chunkSize) {
successCount += batchImportSupport.persistInsertOnlyChunk(
creditDeliveryNoticeService,
chunkItems,
chunkRowNumbers,
mpBatchSize,
CreditDeliveryNotice::getCaseNumber,
"",
errorMessages
);
chunkItems.clear();
chunkRowNumbers.clear();
}
} catch (Exception e) {
int excelRowNumber = i + 1 + usedTitleRows + usedHeadRows;
errorMessages.add("" + excelRowNumber + "行:" + e.getMessage());
e.printStackTrace();
}
}
if (!chunkItems.isEmpty()) {
successCount += batchImportSupport.persistInsertOnlyChunk(
creditDeliveryNoticeService,
chunkItems,
chunkRowNumbers,
mpBatchSize,
CreditDeliveryNotice::getCaseNumber,
"",
errorMessages
);
}
creditCompanyRecordCountService.refresh(CreditCompanyRecordCountService.CountType.DELIVERY_NOTICE, touchedCompanyIds);
if (errorMessages.isEmpty()) {
return success("成功导入" + successCount + "条数据", null);
} else {
return success("导入完成,成功" + successCount + "条,失败" + errorMessages.size() + "", errorMessages);
}
} catch (Exception e) {
e.printStackTrace();
return fail("导入失败:" + e.getMessage(), null);
}
}
/**
* 下载送达公告导入模板
*/
@Operation(summary = "下载送达公告导入模板")
@GetMapping("/import/template")
public void downloadTemplate(HttpServletResponse response) throws IOException {
List<CreditDeliveryNoticeImportParam> templateList = new ArrayList<>();
CreditDeliveryNoticeImportParam example = new CreditDeliveryNoticeImportParam();
example.setDataType("送达公告");
example.setPlaintiffAppellant("原告示例");
example.setAppellee("被告示例");
example.setOtherPartiesThirdParty2("第三人示例");
example.setInvolvedAmount("100000");
example.setDataStatus("正常");
example.setOccurrenceTime("2024-01-01");
example.setCaseNumber("2024示例案号");
example.setCauseOfAction("案由示例");
example.setCourtName("示例法院");
example.setComments("备注信息");
templateList.add(example);
Workbook workbook = ExcelImportSupport.buildTemplate("送达公告导入模板", "送达公告", CreditDeliveryNoticeImportParam.class, templateList);
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setHeader("Content-Disposition", "attachment; filename=credit_delivery_notice_import_template.xlsx");
workbook.write(response.getOutputStream());
workbook.close();
}
private boolean isEmptyImportRow(CreditDeliveryNoticeImportParam param) {
if (param == null) {
return true;
}
return ImportHelper.isBlank(param.getCaseNumber())
&& ImportHelper.isBlank(param.getCauseOfAction())
&& ImportHelper.isBlank(param.getOtherPartiesThirdParty())
&& ImportHelper.isBlank(param.getOtherPartiesThirdParty2())
&& ImportHelper.isBlank(param.getPlaintiffAppellant())
&& ImportHelper.isBlank(param.getAppellee());
}
private CreditDeliveryNotice convertImportParamToEntity(CreditDeliveryNoticeImportParam param) {
CreditDeliveryNotice entity = new CreditDeliveryNotice();
String occurrenceTime = !ImportHelper.isBlank(param.getOccurrenceTime2())
? param.getOccurrenceTime2()
: param.getOccurrenceTime();
String otherPartiesThirdParty = !ImportHelper.isBlank(param.getOtherPartiesThirdParty2())
? param.getOtherPartiesThirdParty2()
: param.getOtherPartiesThirdParty();
String courtName = !ImportHelper.isBlank(param.getCourtName2())
? param.getCourtName2()
: param.getCourtName();
String involvedAmount = !ImportHelper.isBlank(param.getInvolvedAmount2())
? param.getInvolvedAmount2()
: param.getInvolvedAmount();
entity.setDataType(param.getDataType());
entity.setPlaintiffAppellant(param.getPlaintiffAppellant());
entity.setAppellee(param.getAppellee());
entity.setInvolvedAmount(involvedAmount);
entity.setDataStatus(param.getDataStatus());
entity.setOtherPartiesThirdParty(otherPartiesThirdParty);
entity.setOccurrenceTime(occurrenceTime);
entity.setCaseNumber(param.getCaseNumber());
entity.setCauseOfAction(param.getCauseOfAction());
entity.setCourtName(courtName);
entity.setComments(param.getComments());
return entity;
}
}