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.User; import com.gxwebsoft.common.system.service.FileRecordService; import com.gxwebsoft.gxmu.entity.QmgcForm; import com.gxwebsoft.gxmu.param.QmgcFormParam; import com.gxwebsoft.gxmu.service.QmgcFormService; import com.gxwebsoft.gxmu.service.ReviewFlowStarterService; import com.gxwebsoft.gxmu.model.DeclareCheckResult; import com.gxwebsoft.gxmu.service.DeclareGuardService; import org.springframework.transaction.annotation.Transactional; import com.gxwebsoft.gxmu.util.GxmuQueryHelper; import com.gxwebsoft.gxmu.util.QmgcDocxExportUtil; 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.zip.ZipOutputStream; /** * 青马工程培训班学员登记表控制器 */ @Api(tags = "青马工程培训班学员登记表管理") @RestController @RequestMapping("/api/gxmu/qmgc-form") public class QmgcFormController extends BaseController { @Resource private QmgcFormService qmgcFormService; @Resource private ReviewFlowStarterService reviewFlowStarterService; @Resource private DeclareGuardService declareGuardService; @Resource private FileRecordService fileRecordService; @Value("${config.upload-path}") private String uploadPath; @Value("${config.server-url}") private String requestURL; @ApiOperation("分页查询青马工程培训班学员登记表") @GetMapping("/page") public ApiResult> page(QmgcFormParam param) { applyBackendUserScope(param, param::setUserId, param::setUserIds, true); PageParam page = new PageParam<>(param); page.setDefaultOrder("id desc"); return success(new PageResult<>(qmgcFormService.page(page, GxmuQueryHelper.applyKeywords(page.getWrapper("keywords"), param.getKeywords(), "name", "phone", "school_info")).getRecords(), page.getTotal())); } @ApiOperation("分页查询当前用户青马工程培训班学员登记表") @GetMapping("/userPage") public ApiResult> userPage(QmgcFormParam 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<>(qmgcFormService.page(page, GxmuQueryHelper.applyKeywords(page.getWrapper("keywords"), param.getKeywords(), "name", "phone", "school_info")).getRecords(), page.getTotal())); } @ApiOperation("查询全部青马工程培训班学员登记表") @GetMapping() public ApiResult> list(QmgcFormParam param) { PageParam page = new PageParam<>(param); page.setDefaultOrder("id desc"); return success(qmgcFormService.list(page.getOrderWrapper( GxmuQueryHelper.applyKeywords(page.getWrapper("keywords"), param.getKeywords(), "name", "phone", "school_info")))); } @OperationLog @ApiOperation("导出青马工程培训班学员登记表") @PostMapping("/export") public ApiResult export(@RequestBody QmgcFormParam 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 = qmgcFormService.list(page.getOrderWrapper( GxmuQueryHelper.applyKeywords(page.getWrapper("keywords"), param.getKeywords(), "name", "phone", "school_info"))); if (records == null || records.isEmpty()) { return fail("暂无可导出的数据"); } String yearText = resolveYearText(param, records); String relativePath = "file/docx/" + yearText + "年青马工程培训班学员登记表_" + 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)) { QmgcDocxExportUtil.writeExportZip(zipOutputStream, records); 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(qmgcFormService.getById(id)); } @OperationLog @ApiOperation("添加青马工程培训班学员登记表") @PostMapping() @Transactional(rollbackFor = Exception.class) public ApiResult save(@RequestBody QmgcForm qmgcForm) { User loginUser = getLoginUser(); if (loginUser != null) { qmgcForm.setUserId(loginUser.getUserId()); } DeclareCheckResult declareCheck = declareGuardService.checkBeforeCreate("gxmu_qmgc_form", qmgcForm.getDeclareId(), qmgcForm.getYear(), qmgcForm.getUserId()); if (declareCheck.isRejected()) { return fail(declareCheck.getError()); } if (declareCheck.getDeclareId() != null) { qmgcForm.setDeclareId(declareCheck.getDeclareId()); } qmgcForm.setDeclareStatus(1); if (qmgcFormService.save(qmgcForm)) { reviewFlowStarterService.startReview("gxmu_qmgc_form", qmgcForm.getId()); return success("添加成功"); } return fail("添加失败"); } @OperationLog @ApiOperation("修改青马工程培训班学员登记表") @PutMapping() public ApiResult update(@RequestBody QmgcForm qmgcForm) { if (qmgcFormService.updateById(qmgcForm)) { reviewFlowStarterService.restartReviewWhenLastRejected("gxmu_qmgc_form", qmgcForm.getId()); return success("修改成功"); } return fail("修改失败"); } @OperationLog @ApiOperation("删除青马工程培训班学员登记表") @DeleteMapping("/{id}") public ApiResult remove(@PathVariable("id") Integer id) { return qmgcFormService.removeById(id) ? success("删除成功") : fail("删除失败"); } @OperationLog @ApiOperation("批量修改青马工程培训班学员登记表") @PutMapping("/batch") public ApiResult updateBatch(@RequestBody BatchParam batchParam) { return batchParam.update(qmgcFormService, "id") ? success("修改成功") : fail("修改失败"); } @OperationLog @ApiOperation("批量删除青马工程培训班学员登记表") @DeleteMapping("/batch") public ApiResult removeBatch(@RequestBody List ids) { return qmgcFormService.removeByIds(ids) ? success("删除成功") : fail("删除失败"); } private String resolveYearText(QmgcFormParam 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()); } }