李林
2023-10-07 658d4927d468c47208fd012d9128b09249c07eff
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
 *    Copyright (c) 2018-2025, ztt All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: ztt
 */
 
package com.chinaztt.mes.production.controller;
 
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinaztt.mes.basic.entity.Workstation;
import com.chinaztt.mes.basic.service.WorkstationService;
import com.chinaztt.mes.common.wrapper.QueryWrapperUtil;
import com.chinaztt.mes.production.dto.DutyRecordDTO;
import com.chinaztt.mes.production.entity.DutyRecord;
import com.chinaztt.mes.production.excel.*;
import com.chinaztt.mes.production.service.DutyRecordService;
import com.chinaztt.mes.production.util.BackUtils;
import com.chinaztt.ztt.common.core.util.R;
import com.chinaztt.ztt.common.log.annotation.SysLog;
import com.chinaztt.ztt.common.security.annotation.Inner;
import com.google.common.collect.Lists;
import com.xxl.job.core.handler.annotation.XxlJob;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
 
 
/**
 * 上班记录
 *
 * @author cxf
 * @date 2021-01-20 09:11:25
 */
@RestController
@AllArgsConstructor
@RequestMapping("/dutyRecord")
@Api(value = "dutyRecord", tags = "上班记录管理")
public class DutyRecordController {
 
    private final DutyRecordService dutyRecordService;
    private final BackUtils backUtils;
 
    /**
     * 分页查询
     *
     * @param page          分页对象
     * @param dutyRecordDTO 上班记录
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('production_dutyrecord_view','product_workbench')")
    public R getDutyRecordPage(Page page, DutyRecordDTO dutyRecordDTO) {
        return R.ok(dutyRecordService.getDutyRecordPage(page, QueryWrapperUtil.gen(dutyRecordDTO)));
    }
 
    /**
     * 导出
     *
     * @param dutyRecordDTO 上班记录-人工明细
     * @return
     */
    @ApiOperation(value = "导出", notes = "导出")
    @Inner(false)
    @GetMapping("/exportDutyRecord")
    public void exportDutyRecord(HttpServletResponse response, DutyRecordDTO dutyRecordDTO) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("UTF-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("人工记录", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        try {
            //新建ExcelWriter
            ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).registerWriteHandler(new LongestMatchColumnWidthStyleStrategy()).build();
            //获取sheet0对象
            WriteSheet mainSheet = EasyExcel.writerSheet(0, "人工记录").head(DutyRecordData.class).build();
            //向sheet0写入数据 传入空list这样只导出表头
            excelWriter.write(dutyRecordService.exportList(QueryWrapperUtil.gen(dutyRecordDTO)), mainSheet);
            //获取sheet1对象
            WriteSheet detailSheet = EasyExcel.writerSheet(1, "人员出勤").head(PersonBoardData.class).build();
            //向sheet1写入数据 传入空list这样只导出表头
            excelWriter.write(dutyRecordService.exportPersonBoard(QueryWrapperUtil.gen(dutyRecordDTO)), detailSheet);
            //获取sheet2对象
            WriteSheet attendanceSheet = EasyExcel.writerSheet(2, "考勤统计").head(AttendanceData.class).build();
            //向sheet2写入数据 传入空list这样只导出表头
            excelWriter.write(dutyRecordService.exportAttendance(QueryWrapperUtil.gen(dutyRecordDTO)), attendanceSheet);
            //获取sheet3对象
            WriteSheet shiftWageSheet = EasyExcel.writerSheet(3, "班次工资").head(ShiftWageData.class).build();
            //向sheet3写入数据 传入空list这样只导出表头
            excelWriter.write(dutyRecordService.exportShiftWage(QueryWrapperUtil.gen(dutyRecordDTO)), shiftWageSheet);
            //关闭流
            excelWriter.finish();
        } catch (IOException e) {
            throw new RuntimeException("导出失败");
        }
    }
 
    /**
     * 通过id查询上班记录
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('production_dutyrecord_view','product_workbench')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(dutyRecordService.getDtoById(id));
    }
 
    /**
     * 新增上班记录
     *
     * @param dutyRecordDTO 上班记录
     * @return R
     */
    @ApiOperation(value = "新增上班记录", notes = "新增上班记录")
    @SysLog("新增上班记录")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('production_dutyrecord_add','product_workbench')")
    public R save(@RequestBody DutyRecordDTO dutyRecordDTO) {
        DutyRecordDTO dutyRecord = dutyRecordService.saveDto(dutyRecordDTO);
        backUtils.backDutyRecordById(dutyRecordDTO.getId(), "新增");
        return R.ok(dutyRecord);
    }
 
 
    /**
     * 展示该工作站对应的所有的机台
     *
     * @param workCenter
     * @return R
     */
    @ApiOperation(value = "展示该工作站对应的所有的机台", notes = "展示该工作站对应的所有的机台")
    @SysLog("展示该工作站对应的所有的机台")
    @GetMapping("/getWorkstation")
    @PreAuthorize("@pms.hasPermission('production_dutyrecord_add','product_workbench')")
    public R getWorkstation(@RequestParam("workCenter") String workCenter) {
        return R.ok(dutyRecordService.getWorkstationList(workCenter));
    }
 
 
    /**
     * 展示该工作中心对应的未提交班次信息
     *
     * @param workCenter
     * @return R
     */
    @ApiOperation(value = "展示该工作中心对应的未提交班次信息", notes = "展示该工作中心对应的未提交班次信息")
    @SysLog("展示该工作中心对应的未提交班次信息")
    @GetMapping("/getUnsubmitWorkstation")
    public R getUnsubmitWorkstation(@RequestParam("workCenter") String workCenter) {
        return R.ok(dutyRecordService.getUnsubmitWorkstationList(workCenter));
    }
 
 
 
    /**
     * 批量新增上班记录
     * @param dutyRecordDTO
     * @return
     */
    @ApiOperation(value = "批量新增上班记录", notes = "批量新增上班记录")
    @SysLog("批量新增上班记录")
    @PostMapping("/batch")
    @PreAuthorize("@pms.hasPermission('product_add_shift_batch','product_workbench')")
    public R batchSave(@RequestBody DutyRecordDTO dutyRecordDTO) {
        List<DutyRecordDTO> records = dutyRecordService.batchSave(dutyRecordDTO);
        backUtils.batchbackDutyRecordByIds(records.stream().map(DutyRecordDTO::getId).collect(Collectors.toList()), "批量新增");
        Optional<DutyRecordDTO> first = records.stream().filter(p -> p.getWorkstationId().equals(dutyRecordDTO.getWorkstationId())).findFirst();
        if (first.isPresent()) {
            return R.ok(first.get());
        }
        return R.ok();
    }
 
    /**
     * 修改上班记录
     *
     * @param dutyRecordDTO 上班记录
     * @return R
     */
    @ApiOperation(value = "修改上班记录", notes = "修改上班记录")
    @SysLog("修改上班记录")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('production_dutyrecord_edit','product_workbench')")
    public R updateById(@RequestBody DutyRecordDTO dutyRecordDTO) {
        //工作台模块限制直接更新
        if(dutyRecordDTO.getIsLimitUpd()) {
            Boolean isSubmit = dutyRecordService.getById(dutyRecordDTO.getId()).getIsSubmit();
            if (isSubmit) {
                throw new RuntimeException("班次已提交,不可编辑");
            }
        }
        boolean result = dutyRecordService.updateById(dutyRecordDTO);
        backUtils.backDutyRecordById(dutyRecordDTO.getId(), dutyRecordDTO.getOperationType());
        return R.ok(dutyRecordService.getDtoById(dutyRecordDTO.getId()));
    }
 
    /**
     * 通过id删除上班记录
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除上班记录", notes = "通过id删除上班记录")
    @SysLog("通过id删除上班记录")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('production_dutyrecord_del','product_workbench')")
    public R removeById(@PathVariable Long id) {
        backUtils.backDeleteDutyRecordById(Arrays.asList(id));
        return R.ok(dutyRecordService.deleteById(id));
    }
 
    /**
     * 通过上班记录id查询人员List
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过上班记录id查询人员List", notes = "通过上班记录id查询人员List")
    @GetMapping("/getPersonByDutyRecordId/{id}")
    public R getPersonByDutyRecordId(@PathVariable("id") Long id) {
        return R.ok(dutyRecordService.getPersonByDutyRecordId(id));
    }
 
    /**
     * 通过上班记录idList查询人员List
     *
     * @param idList
     * @return R
     */
    @ApiOperation(value = "通过上班记录idList查询人员List", notes = "通过上班记录idList查询人员List")
    @GetMapping("/getPersonByDutyRecordIdList")
    public R getPersonByDutyRecordIdList(@RequestParam("idList") List<Long> idList) {
        return R.ok(dutyRecordService.getPersonByDutyRecordIdList(idList));
    }
 
    /**
     * 通过上班记录id查询产量
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过上班记录id查询产量", notes = "通过上班记录id查询产量")
    @GetMapping("/getOutputByDutyRecordId/{id}")
    public R getOutputByDutyRecordId(@PathVariable("id") Long id) {
        return R.ok(dutyRecordService.getOutputByDutyRecordId(id));
    }
 
 
    /**
     * 通过上班记录idList查询产量
     *
     * @param idList
     * @return R
     */
    @ApiOperation(value = "通过上班记录idList查询产量", notes = "通过上班记录idList查询产量")
    @GetMapping("/getOutputByDutyRecordIdList")
    public R getOutputByDutyRecordIdList(@RequestParam("idList") List<Long> idList) {
        return R.ok(dutyRecordService.getOutputByDutyRecordIdList(idList));
    }
 
 
 
 
    /**
     * 通过工作站id查询班次
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过工作站id查询班次", notes = "通过工作站id查询班次")
    @GetMapping("/getDutyRecordByWorkstationId/{id}")
    public R getDutyRecordByWorkstationId(@PathVariable("id") Long id) {
        return R.ok(dutyRecordService.getDutyRecordByWorkstationId(id));
    }
 
    /**
     * 通过id查询操作记录
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询操作记录", notes = "通过id查询操作记录")
    @GetMapping("/getOperatingRecord/{id}")
    public R getOperatingRecord(@PathVariable("id") Long id) {
        return R.ok(dutyRecordService.getOperatingRecord(id));
    }
 
    /**
     * 分页查询人工记录
     *
     * @param page          分页对象
     * @param dutyRecordDTO 上班记录
     * @return
     */
    @ApiOperation(value = "分页查询人工记录", notes = "分页查询人工记录")
    @GetMapping("/getArtificialRecord")
    public R getArtificialRecord(Page page, DutyRecordDTO dutyRecordDTO) {
        return R.ok(dutyRecordService.getArtificialRecord(page, QueryWrapperUtil.gen(dutyRecordDTO)));
    }
 
    /**
     * 工作台班次未提交提醒用于定时任务
     *
     * @return
     */
    @ApiOperation(value = "工作台班次未提交提醒", notes = "工作台班次未提交提醒")
    @GetMapping("/sendUnsubmitMsgAuto")
    @XxlJob("sendUnsubmitMsgAutoHandler")
    public R sendUnsubmitMsg(){
        return R.ok(dutyRecordService.sendUnsubmitMsg());
    }
 
 
    /**
     * 班次批量提交
     *
     * @param ids
     * @return R
     */
    @ApiOperation(value = "班次批量提交", notes = "班次批量提交")
    @SysLog("班次批量提交")
    @PostMapping("/batchSubmit")
    public R batchSubmit(@RequestBody List<Long> ids) {
        return R.ok(dutyRecordService.batchSubmit(ids));
    }
 
 
 
    /**
     * 批量修改工时计算上班记录
     *
     * @param dutyRecordDTOList 上班记录
     * @return R
     */
    @ApiOperation(value = "批量修改工时计算上班记录", notes = "批量修改工时计算上班记录")
    @SysLog("批量修改工时计算上班记录")
    @PostMapping("updateBatchById")
    @PreAuthorize("@pms.hasPermission('production_dutyrecord_edit','product_workbench')")
    public R updateBatchById(@RequestBody List<DutyRecordDTO> dutyRecordDTOList) {
        dutyRecordService.updateBatch(dutyRecordDTOList);
        for (DutyRecordDTO dutyRecordDTO:dutyRecordDTOList) {
            backUtils.backDutyRecordById(dutyRecordDTO.getId(), dutyRecordDTO.getOperationType());
        }
        return R.ok();
    }
}