Files
tuanwei-java/src/main/java/com/gxwebsoft/gxmu/controller/SjtbzbsjFormController.java
2026-07-20 11:48:06 +08:00

234 lines
10 KiB
Java

package com.gxwebsoft.gxmu.controller;
import cn.hutool.core.util.StrUtil;
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.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.system.entity.FileRecord;
import com.gxwebsoft.common.system.entity.Organization;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.common.system.service.FileRecordService;
import com.gxwebsoft.common.system.service.OrganizationService;
import com.gxwebsoft.gxmu.entity.SjtbzbsjForm;
import com.gxwebsoft.gxmu.param.SjtbzbsjFormParam;
import com.gxwebsoft.gxmu.service.ReviewFlowStarterService;
import com.gxwebsoft.gxmu.service.SjtbzbsjFormService;
import com.gxwebsoft.gxmu.util.GxmuQueryHelper;
import com.gxwebsoft.gxmu.util.SjtbzbsjDocxExportUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;
/**
* 十佳团支部书记申报表控制器
*/
@Api(tags = "十佳团支部书记申报表管理")
@RestController
@RequestMapping("/api/gxmu/sjtbzbsj-form")
public class SjtbzbsjFormController extends BaseController {
@Resource
private SjtbzbsjFormService sjtbzbsjFormService;
@Resource
private ReviewFlowStarterService reviewFlowStarterService;
@Resource
private FileRecordService fileRecordService;
@Resource
private OrganizationService organizationService;
@Value("${config.upload-path}")
private String uploadPath;
@Value("${config.server-url}")
private String requestURL;
@ApiOperation("分页查询十佳团支部书记申报表")
@GetMapping("/page")
public ApiResult<PageResult<SjtbzbsjForm>> page(SjtbzbsjFormParam param) {
applyBackendUserScope(param, param::setUserId, param::setUserIds, true);
PageParam<SjtbzbsjForm, SjtbzbsjFormParam> page = new PageParam<>(param);
page.setDefaultOrder("id desc");
return success(new PageResult<>(sjtbzbsjFormService.page(page,
GxmuQueryHelper.applyKeywords(page.getWrapper("keywords"), param.getKeywords(),
"name", "college_class", "branch_name")).getRecords(), page.getTotal()));
}
@ApiOperation("分页查询当前用户十佳团支部书记申报表")
@GetMapping("/userPage")
public ApiResult<PageResult<SjtbzbsjForm>> userPage(SjtbzbsjFormParam param) {
applyBackendUserScope(param, param::setUserId, param::setUserIds, true);
if (param.getUserId() == null && (param.getUserIds() == null || param.getUserIds().isEmpty())) {
param.setUserId(getLoginUserId());
}
PageParam<SjtbzbsjForm, SjtbzbsjFormParam> page = new PageParam<>(param);
page.setDefaultOrder("id desc");
return success(new PageResult<>(sjtbzbsjFormService.page(page,
GxmuQueryHelper.applyKeywords(page.getWrapper("keywords"), param.getKeywords(),
"name", "college_class", "branch_name")).getRecords(), page.getTotal()));
}
@ApiOperation("查询全部十佳团支部书记申报表")
@GetMapping()
public ApiResult<List<SjtbzbsjForm>> list(SjtbzbsjFormParam param) {
PageParam<SjtbzbsjForm, SjtbzbsjFormParam> page = new PageParam<>(param);
page.setDefaultOrder("id desc");
return success(sjtbzbsjFormService.list(page.getOrderWrapper(
GxmuQueryHelper.applyKeywords(page.getWrapper("keywords"), param.getKeywords(),
"name", "college_class", "branch_name"))));
}
@OperationLog
@ApiOperation("导出十佳团支部书记申报材料")
@PostMapping("/export")
public ApiResult<?> export(@RequestBody SjtbzbsjFormParam param, HttpServletRequest request) throws IOException {
applyBackendUserScope(param, param::setUserId, param::setUserIds, true);
if (param.getUserId() == null && (param.getUserIds() == null || param.getUserIds().isEmpty())) {
param.setUserId(getLoginUserId());
}
PageParam<SjtbzbsjForm, SjtbzbsjFormParam> page = new PageParam<>(param);
page.setDefaultOrder("id desc");
List<SjtbzbsjForm> records = sjtbzbsjFormService.list(page.getOrderWrapper(
GxmuQueryHelper.applyKeywords(page.getWrapper("keywords"), param.getKeywords(),
"name", "college_class", "branch_name")));
if (records == null || records.isEmpty()) {
return fail("暂无可导出的数据");
}
User loginUser = getLoginUser();
Map<Integer, String> contactMap = new LinkedHashMap<>();
for (SjtbzbsjForm form : records) {
contactMap.put(form.getId(), loginUser == null ? "" : valueOf(loginUser.getPhone()));
}
SjtbzbsjDocxExportUtil.SummaryMeta summaryMeta = new SjtbzbsjDocxExportUtil.SummaryMeta();
summaryMeta.setYearText(resolveYearText(param, records));
summaryMeta.setReporter(loginUser == null ? "" : resolveReporter(loginUser));
summaryMeta.setContact(loginUser == null ? "" : valueOf(loginUser.getPhone()));
summaryMeta.setOrganizationPartyOpinion(resolveOrganizationPartyOpinion(loginUser));
String relativePath = "file/docx/十佳团支部书记申报材料_" + System.currentTimeMillis() + ".zip";
File targetFile = new File(uploadPath + relativePath);
File parentFile = targetFile.getParentFile();
if (parentFile != null && !parentFile.exists()) {
parentFile.mkdirs();
}
try (FileOutputStream outputStream = new FileOutputStream(targetFile);
ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream)) {
SjtbzbsjDocxExportUtil.writeExportZip(zipOutputStream, records, summaryMeta, contactMap);
zipOutputStream.finish();
}
FileRecord result = new FileRecord();
result.setCreateUserId(getLoginUserId());
result.setName(targetFile.getName());
result.setPath(targetFile.getAbsolutePath());
result.setContentType("application/zip");
result.setUrl(requestURL + "/" + relativePath);
fileRecordService.save(result);
return success(result);
}
@ApiOperation("根据id查询十佳团支部书记申报表")
@GetMapping("/{id}")
public ApiResult<SjtbzbsjForm> get(@PathVariable("id") Integer id) {
return success(sjtbzbsjFormService.getById(id));
}
@OperationLog
@ApiOperation("添加十佳团支部书记申报表")
@PostMapping()
public ApiResult<?> save(@RequestBody SjtbzbsjForm sjtbzbsjForm) {
User loginUser = getLoginUser();
if (loginUser != null) {
sjtbzbsjForm.setUserId(loginUser.getUserId());
}
if (sjtbzbsjFormService.save(sjtbzbsjForm)) {
reviewFlowStarterService.startReview("gxmu_sjtbzbsj", sjtbzbsjForm.getId());
return success("添加成功");
}
return fail("添加失败");
}
@OperationLog
@ApiOperation("修改十佳团支部书记申报表")
@PutMapping()
public ApiResult<?> update(@RequestBody SjtbzbsjForm sjtbzbsjForm) {
if (sjtbzbsjFormService.updateById(sjtbzbsjForm)) {
reviewFlowStarterService.restartReviewWhenLastRejected("gxmu_sjtbzbsj", sjtbzbsjForm.getId());
return success("修改成功");
}
return fail("修改失败");
}
@OperationLog
@ApiOperation("删除十佳团支部书记申报表")
@DeleteMapping("/{id}")
public ApiResult<?> remove(@PathVariable("id") Integer id) {
return sjtbzbsjFormService.removeById(id) ? success("删除成功") : fail("删除失败");
}
@OperationLog
@ApiOperation("批量修改十佳团支部书记申报表")
@PutMapping("/batch")
public ApiResult<?> updateBatch(@RequestBody BatchParam<SjtbzbsjForm> batchParam) {
return batchParam.update(sjtbzbsjFormService, "id") ? success("修改成功") : fail("修改失败");
}
@OperationLog
@ApiOperation("批量删除十佳团支部书记申报表")
@DeleteMapping("/batch")
public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
return sjtbzbsjFormService.removeByIds(ids) ? success("删除成功") : fail("删除失败");
}
private String resolveYearText(SjtbzbsjFormParam param, List<SjtbzbsjForm> records) {
if (param.getYear() != null) {
return String.valueOf(param.getYear());
}
if (records != null && !records.isEmpty() && records.get(0).getYear() != null) {
return String.valueOf(records.get(0).getYear());
}
return String.valueOf(java.time.LocalDate.now().getYear());
}
private String resolveReporter(User loginUser) {
if (loginUser == null) {
return "";
}
if (StrUtil.isNotBlank(loginUser.getRealName())) {
return loginUser.getRealName().trim();
}
if (StrUtil.isNotBlank(loginUser.getNickname())) {
return loginUser.getNickname().trim();
}
return valueOf(loginUser.getUsername());
}
private String resolveOrganizationPartyOpinion(User loginUser) {
if (loginUser == null || loginUser.getOrganizationId() == null) {
return "(盖章)";
}
Organization organization = organizationService.getByIdRel(loginUser.getOrganizationId());
String organizationName = organization == null ? "" : valueOf(organization.getOrganizationName());
return organizationName.isEmpty() ? "(盖章)" : organizationName + "(盖章)";
}
private String valueOf(String value) {
return value == null ? "" : value.trim();
}
}