gaoluyang
7 天以前 e1590017b716d77f88f075364c55cfbdb480286f
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
package cn.iocoder.yudao.module.mes.controller.admin.dv.borrow;
 
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.module.mes.controller.admin.dv.borrow.vo.MesDvBorrowPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.dv.borrow.vo.MesDvBorrowRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.dv.borrow.vo.MesDvBorrowSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.dv.borrow.MesDvBorrowDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.dv.machinery.MesDvMachineryDO;
import cn.iocoder.yudao.module.mes.service.dv.borrow.MesDvBorrowService;
import cn.iocoder.yudao.module.mes.service.dv.machinery.MesDvMachineryService;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
 
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
 
@Tag(name = "管理后台 - MES 设备借用归还")
@RestController
@RequestMapping("/mes/dv/borrow")
@Validated
public class MesDvBorrowController {
 
    @Resource
    private MesDvBorrowService borrowService;
    @Resource
    private MesDvMachineryService machineryService;
    @Resource
    private AdminUserApi adminUserApi;
 
    @PostMapping("/create")
    @Operation(summary = "创建设备借用")
    @PreAuthorize("@ss.hasPermission('mes:dv-borrow:create')")
    public CommonResult<Long> createBorrow(@Valid @RequestBody MesDvBorrowSaveReqVO createReqVO) {
        return success(borrowService.createBorrow(createReqVO));
    }
 
    @PutMapping("/update")
    @Operation(summary = "更新设备借用")
    @PreAuthorize("@ss.hasPermission('mes:dv-borrow:update')")
    public CommonResult<Boolean> updateBorrow(@Valid @RequestBody MesDvBorrowSaveReqVO updateReqVO) {
        borrowService.updateBorrow(updateReqVO);
        return success(true);
    }
 
    @DeleteMapping("/delete")
    @Operation(summary = "删除设备借用")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:dv-borrow:delete')")
    public CommonResult<Boolean> deleteBorrow(@RequestParam("id") Long id) {
        borrowService.deleteBorrow(id);
        return success(true);
    }
 
    @PutMapping("/return")
    @Operation(summary = "设备归还", description = "归还后记录实际归还时间与归还状况,状态变为已归还")
    @Parameters({
            @Parameter(name = "id", description = "编号", required = true),
            @Parameter(name = "returnCondition", description = "归还状况,如:完好、轻微磨损、需维修")
    })
    @PreAuthorize("@ss.hasPermission('mes:dv-borrow:update')")
    public CommonResult<Boolean> returnBorrow(@RequestParam("id") Long id,
                                             @RequestParam(value = "returnCondition", required = false) String returnCondition) {
        borrowService.returnBorrow(id, returnCondition);
        return success(true);
    }
 
    @GetMapping("/get")
    @Operation(summary = "获得设备借用")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:dv-borrow:query')")
    public CommonResult<MesDvBorrowRespVO> getBorrow(@RequestParam("id") Long id) {
        MesDvBorrowDO borrow = borrowService.getBorrow(id);
        if (borrow == null) {
            return success(null);
        }
        return success(buildBorrowRespVOList(Collections.singletonList(borrow)).get(0));
    }
 
    @GetMapping("/page")
    @Operation(summary = "获得设备借用分页")
    @PreAuthorize("@ss.hasPermission('mes:dv-borrow:query')")
    public CommonResult<PageResult<MesDvBorrowRespVO>> getBorrowPage(@Valid MesDvBorrowPageReqVO pageReqVO) {
        PageResult<MesDvBorrowDO> pageResult = borrowService.getBorrowPage(pageReqVO);
        return success(new PageResult<>(buildBorrowRespVOList(pageResult.getList()), pageResult.getTotal()));
    }
 
    @GetMapping("/export-excel")
    @Operation(summary = "导出设备借用 Excel")
    @PreAuthorize("@ss.hasPermission('mes:dv-borrow:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBorrowExcel(@Valid MesDvBorrowPageReqVO pageReqVO,
                                  HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MesDvBorrowDO> list = borrowService.getBorrowPage(pageReqVO).getList();
        ExcelUtils.write(response, "设备借用.xls", "数据", MesDvBorrowRespVO.class, buildBorrowRespVOList(list));
    }
 
    private List<MesDvBorrowRespVO> buildBorrowRespVOList(List<MesDvBorrowDO> list) {
        if (CollUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
        Map<Long, MesDvMachineryDO> machineryMap = machineryService.getMachineryMap(
                convertSet(list, MesDvBorrowDO::getMachineryId));
        Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(
                convertSet(list, MesDvBorrowDO::getBorrowerUserId));
        return BeanUtils.toBean(list, MesDvBorrowRespVO.class, vo -> {
            MapUtils.findAndThen(machineryMap, vo.getMachineryId(), machinery -> {
                vo.setMachineryCode(machinery.getCode());
                vo.setMachineryName(machinery.getName());
            });
            MapUtils.findAndThen(userMap, vo.getBorrowerUserId(),
                    user -> vo.setBorrowerUserName(user.getNickname()));
        });
    }
 
}