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.SjqnForm; import com.gxwebsoft.gxmu.param.SjqnFormParam; import com.gxwebsoft.gxmu.service.ReviewFlowStarterService; import com.gxwebsoft.gxmu.service.SjqnFormService; import com.gxwebsoft.gxmu.util.GxmuQueryHelper; import com.gxwebsoft.gxmu.util.SjqnDocxExportUtil; 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.List; import java.util.LinkedHashMap; import java.util.Map; import java.util.zip.ZipOutputStream; /** * 十佳青年申报表控制器 */ @Api(tags = "十佳青年申报表管理") @RestController @RequestMapping("/api/gxmu/sjqn-form") public class SjqnFormController extends BaseController { @Resource private SjqnFormService sjqnFormService; @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> page(SjqnFormParam param) { applyBackendUserScope(param, param::setUserId, param::setUserIds, true); PageParam page = new PageParam<>(param); page.setDefaultOrder("id desc"); return success(new PageResult<>(sjqnFormService.page(page, GxmuQueryHelper.applyKeywords(page.getWrapper("keywords"), param.getKeywords(), "name", "unit_name", "apply_type")).getRecords(), page.getTotal())); } @ApiOperation("分页查询当前用户十佳青年申报表") @GetMapping("/userPage") public ApiResult> userPage(SjqnFormParam param) { applyBackendUserScope(param, param::setUserId, param::setUserIds, true); if (param.getUserId() == null && (param.getUserIds() == null || param.getUserIds().isEmpty())) { param.setUserId(getLoginUserId()); } PageParam page = new PageParam<>(param); page.setDefaultOrder("id desc"); return success(new PageResult<>(sjqnFormService.page(page, GxmuQueryHelper.applyKeywords(page.getWrapper("keywords"), param.getKeywords(), "name", "unit_name", "apply_type")).getRecords(), page.getTotal())); } @ApiOperation("查询全部十佳青年申报表") @GetMapping() public ApiResult> list(SjqnFormParam param) { PageParam page = new PageParam<>(param); page.setDefaultOrder("id desc"); return success(sjqnFormService.list(page.getOrderWrapper( GxmuQueryHelper.applyKeywords(page.getWrapper("keywords"), param.getKeywords(), "name", "unit_name", "apply_type")))); } @OperationLog @ApiOperation("导出十佳青年岗位能手申报材料") @PostMapping("/export") public ApiResult export(@RequestBody SjqnFormParam 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 page = new PageParam<>(param); page.setDefaultOrder("id desc"); List records = sjqnFormService.list(page.getOrderWrapper( GxmuQueryHelper.applyKeywords(page.getWrapper("keywords"), param.getKeywords(), "name", "unit_name", "apply_type"))); if (records == null || records.isEmpty()) { return fail("暂无可导出的数据"); } User loginUser = getLoginUser(); Map contactMap = new LinkedHashMap<>(); for (SjqnForm form : records) { contactMap.put(form.getId(), loginUser == null ? "" : valueOf(loginUser.getPhone())); } SjqnDocxExportUtil.SummaryMeta summaryMeta = new SjqnDocxExportUtil.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)) { SjqnDocxExportUtil.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 get(@PathVariable("id") Integer id) { return success(sjqnFormService.getById(id)); } @OperationLog @ApiOperation("添加十佳青年申报表") @PostMapping() public ApiResult save(@RequestBody SjqnForm sjqnForm) { User loginUser = getLoginUser(); if (loginUser != null) { sjqnForm.setUserId(loginUser.getUserId()); } if (sjqnFormService.save(sjqnForm)) { reviewFlowStarterService.startReview("gxmu_sjqn", sjqnForm.getId()); return success("添加成功"); } return fail("添加失败"); } @OperationLog @ApiOperation("修改十佳青年申报表") @PutMapping() public ApiResult update(@RequestBody SjqnForm sjqnForm) { if (sjqnFormService.updateById(sjqnForm)) { reviewFlowStarterService.restartReviewWhenLastRejected("gxmu_sjqn", sjqnForm.getId()); return success("修改成功"); } return fail("修改失败"); } @OperationLog @ApiOperation("删除十佳青年申报表") @DeleteMapping("/{id}") public ApiResult remove(@PathVariable("id") Integer id) { return sjqnFormService.removeById(id) ? success("删除成功") : fail("删除失败"); } @OperationLog @ApiOperation("批量修改十佳青年申报表") @PutMapping("/batch") public ApiResult updateBatch(@RequestBody BatchParam batchParam) { return batchParam.update(sjqnFormService, "id") ? success("修改成功") : fail("修改失败"); } @OperationLog @ApiOperation("批量删除十佳青年申报表") @DeleteMapping("/batch") public ApiResult removeBatch(@RequestBody List ids) { return sjqnFormService.removeByIds(ids) ? success("删除成功") : fail("删除失败"); } private String resolveYearText(SjqnFormParam param, List 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(); } }