package com.ruoyi.web.controller.api; import com.ruoyi.common.annotation.Log; import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.enums.BusinessType; import com.ruoyi.system.dto.SysCardDeleteRequest; import com.ruoyi.system.dto.SysCardDetailRequest; import com.ruoyi.system.dto.SysCardEmployeeRequest; import com.ruoyi.system.dto.SysCardPageRequest; import com.ruoyi.system.dto.SysCardSaveRequest; import com.ruoyi.system.service.ISysCardService; import com.ruoyi.system.vo.SysCardVO; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.List; /** * 卡片管理接口。卡片属于本地设备侧数据,不受中台版本管控。 */ @RestController @RequestMapping("/system/card") public class SysCardController extends BaseController { @Autowired private ISysCardService cardService; @Log(title = "卡片管理", businessType = BusinessType.SELECT) @PreAuthorize("@ss.hasPermi('visitor:card:view')") @PostMapping("/page") public AjaxResult page(@RequestBody(required = false) SysCardPageRequest request) { return success(cardService.pageList(request)); } @Log(title = "卡片管理", businessType = BusinessType.SELECT) @PreAuthorize("@ss.hasPermi('visitor:card:view')") @PostMapping("/detail") public AjaxResult detail(@RequestBody SysCardDetailRequest request) { return success(cardService.detail(request)); } @Log(title = "卡片管理", businessType = BusinessType.SELECT) @PreAuthorize("@ss.hasPermi('visitor:card:view')") @PostMapping("/employee") public AjaxResult employee(@RequestBody SysCardEmployeeRequest request) { return success(cardService.findEmployee(request)); } @Log(title = "卡片管理", businessType = BusinessType.INSERT) @PreAuthorize("@ss.hasPermi('visitor:card:add')") @PostMapping("/add") public AjaxResult add(@RequestBody SysCardSaveRequest request) { return toAjax(cardService.save(request, getUsername())); } @Log(title = "卡片管理", businessType = BusinessType.UPDATE) @PreAuthorize("@ss.hasPermi('visitor:card:edit')") @PostMapping("/edit") public AjaxResult edit(@RequestBody SysCardSaveRequest request) { return toAjax(cardService.save(request, getUsername())); } @Log(title = "卡片管理", businessType = BusinessType.UPDATE) @PreAuthorize("@ss.hasPermi('visitor:card:bind')") @PostMapping("/bind") public AjaxResult bind(@RequestBody SysCardSaveRequest request) { return toAjax(cardService.save(request, getUsername())); } @Log(title = "卡片管理", businessType = BusinessType.UPDATE) @PreAuthorize("@ss.hasPermi('visitor:card:unbind')") @PostMapping("/unbind") public AjaxResult unbind(@RequestBody SysCardSaveRequest request) { request.setEmployeeId(null); request.setEmployeeName(null); return toAjax(cardService.save(request, getUsername())); } @Log(title = "卡片管理", businessType = BusinessType.DELETE) @PreAuthorize("@ss.hasPermi('visitor:card:remove')") @PostMapping("/delete") public AjaxResult delete(@RequestBody SysCardDeleteRequest request) { return toAjax(cardService.delete(request, getUsername())); } @Log(title = "卡片管理", businessType = BusinessType.IMPORT) @PreAuthorize("@ss.hasPermi('visitor:card:import')") @PostMapping("/importData") public AjaxResult importData(@RequestParam("file") MultipartFile file) throws IOException { if (file == null || file.isEmpty()) { return error("请选择导入文件"); } return success(cardService.importData(file.getInputStream(), getUsername())); } @Log(title = "卡片管理", businessType = BusinessType.EXPORT) @PreAuthorize("@ss.hasPermi('visitor:card:export')") @PostMapping("/export") public void export(@RequestBody(required = false) SysCardPageRequest request, HttpServletResponse response) throws IOException { response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setCharacterEncoding("utf-8"); String fileName = URLEncoder.encode("卡片列表", "UTF-8").replaceAll("\\+", "%20"); response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); List cards = cardService.list(request); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); try (Workbook workbook = new XSSFWorkbook()) { Sheet sheet = workbook.createSheet("卡片列表"); String[] headers = {"卡号", "员工姓名", "联系方式", "所属部门", "绑定时间", "卡片状态"}; Row headerRow = sheet.createRow(0); for (int index = 0; index < headers.length; index++) { Cell cell = headerRow.createCell(index); cell.setCellValue(headers[index]); sheet.setColumnWidth(index, 22 * 256); } for (int index = 0; index < cards.size(); index++) { SysCardVO card = cards.get(index); Row row = sheet.createRow(index + 1); row.createCell(0).setCellValue(value(card.getCardNo())); row.createCell(1).setCellValue(value(card.getEmployeeName())); row.createCell(2).setCellValue(value(card.getContactPhone())); row.createCell(3).setCellValue(value(card.getDeptName())); row.createCell(4).setCellValue(card.getBindTime() == null ? "" : dateFormat.format(card.getBindTime())); row.createCell(5).setCellValue(Boolean.TRUE.equals(card.getBound()) ? "已绑定" : "未绑定"); } workbook.write(response.getOutputStream()); } } @Log(title = "卡片管理", businessType = BusinessType.EXPORT) @PreAuthorize("@ss.hasPermi('visitor:card:import')") @PostMapping("/importTemplate") public void importTemplate(HttpServletResponse response) throws IOException { response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); response.setCharacterEncoding("utf-8"); String fileName = URLEncoder.encode("卡片导入模板", "UTF-8").replaceAll("\\+", "%20"); response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx"); try (Workbook workbook = new XSSFWorkbook()) { Sheet sheet = workbook.createSheet("卡片导入"); String[] headers = {"卡号", "员工姓名"}; Row headerRow = sheet.createRow(0); for (int index = 0; index < headers.length; index++) { headerRow.createCell(index).setCellValue(headers[index]); sheet.setColumnWidth(index, 24 * 256); } workbook.write(response.getOutputStream()); } } private String value(String value) { return value == null ? "" : value; } }