liujie
2026-09-21 20051550f01227ecebbe0bf064e188dd242f58f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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<SysCardVO> 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;
    }
}