gaoluyang
9 天以前 cbadfd8e691ab335965202a984db16e55ac8e42c
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
package cn.iocoder.yudao.module.erp.controller.admin.purchase;
 
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.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.erp.controller.admin.purchase.vo.contract.ErpSupplierContractPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.contract.ErpSupplierContractRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.contract.ErpSupplierContractSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchaseOrderDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpSupplierContractDO;
import cn.iocoder.yudao.module.erp.dal.mysql.purchase.ErpPurchaseOrderMapper;
import cn.iocoder.yudao.module.erp.service.purchase.ErpSupplierContractService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
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.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
 
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
 
@Tag(name = "管理后台 - ERP 供应商合同")
@RestController
@RequestMapping("/erp/supplier-contract")
@Validated
public class ErpSupplierContractController {
 
    @Resource
    private ErpSupplierContractService contractService;
    @Resource
    private ErpPurchaseOrderMapper purchaseOrderMapper;
 
    @PostMapping("/create")
    @Operation(summary = "创建供应商合同")
    @PreAuthorize("@ss.hasPermission('erp:supplier-contract:create')")
    public CommonResult<Long> createContract(@Valid @RequestBody ErpSupplierContractSaveReqVO createReqVO) {
        return success(contractService.createContract(createReqVO));
    }
 
    @PutMapping("/update")
    @Operation(summary = "更新供应商合同")
    @PreAuthorize("@ss.hasPermission('erp:supplier-contract:update')")
    public CommonResult<Boolean> updateContract(@Valid @RequestBody ErpSupplierContractSaveReqVO updateReqVO) {
        contractService.updateContract(updateReqVO);
        return success(true);
    }
 
    @DeleteMapping("/delete")
    @Operation(summary = "删除供应商合同")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:supplier-contract:delete')")
    public CommonResult<Boolean> deleteContract(@RequestParam("id") Long id) {
        contractService.deleteContract(id);
        return success(true);
    }
 
    @GetMapping("/get")
    @Operation(summary = "获得供应商合同")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:supplier-contract:query')")
    public CommonResult<ErpSupplierContractRespVO> getContract(@RequestParam("id") Long id) {
        ErpSupplierContractDO contract = contractService.getContract(id);
        return success(contract == null ? null : buildResp(contract));
    }
 
    @GetMapping("/page")
    @Operation(summary = "获得供应商合同分页")
    @PreAuthorize("@ss.hasPermission('erp:supplier-contract:query')")
    public CommonResult<PageResult<ErpSupplierContractRespVO>> getContractPage(
            @Valid ErpSupplierContractPageReqVO pageReqVO) {
        PageResult<ErpSupplierContractDO> pageResult = contractService.getContractPage(pageReqVO);
        return success(new PageResult<>(buildRespList(pageResult.getList()), pageResult.getTotal()));
    }
 
    @GetMapping("/list-by-supplier")
    @Operation(summary = "获得指定供应商的合同列表", description = "采购订单关联合同时使用,返回采购合同与合同模板")
    @Parameter(name = "supplierId", description = "供应商编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('erp:supplier-contract:query')")
    public CommonResult<List<ErpSupplierContractRespVO>> getContractListBySupplier(
            @RequestParam("supplierId") Long supplierId) {
        return success(buildRespList(contractService.getContractListBySupplierId(supplierId)));
    }
 
    @GetMapping("/export-excel")
    @Operation(summary = "导出供应商合同 Excel")
    @PreAuthorize("@ss.hasPermission('erp:supplier-contract:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportContractExcel(@Valid ErpSupplierContractPageReqVO pageReqVO,
                                    HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErpSupplierContractDO> list = contractService.getContractPage(pageReqVO).getList();
        ExcelUtils.write(response, "供应商合同.xls", "数据", ErpSupplierContractRespVO.class, buildRespList(list));
    }
 
    // ==================== 拼接 VO ====================
 
    private List<ErpSupplierContractRespVO> buildRespList(List<ErpSupplierContractDO> list) {
        if (CollUtil.isEmpty(list)) {
            return java.util.Collections.emptyList();
        }
        List<ErpSupplierContractRespVO> result = new java.util.ArrayList<>(list.size());
        for (ErpSupplierContractDO contract : list) {
            result.add(buildResp(contract));
        }
        return result;
    }
 
    private ErpSupplierContractRespVO buildResp(ErpSupplierContractDO contract) {
        ErpSupplierContractRespVO vo = BeanUtils.toBean(contract, ErpSupplierContractRespVO.class);
        // 执行进度:关联的采购订单数量与金额合计
        List<ErpPurchaseOrderDO> orders = purchaseOrderMapper.selectListByContractId(contract.getId());
        vo.setOrderCount(orders.size());
        vo.setOrderAmount(orders.stream()
                .map(ErpPurchaseOrderDO::getTotalPrice)
                .filter(java.util.Objects::nonNull)
                .reduce(BigDecimal.ZERO, BigDecimal::add));
        return vo;
    }
 
}