gaoluyang
6 天以前 006020bc9a87dc40c2e73bc50f244b348b831de1
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
package cn.iocoder.yudao.module.erp.controller.admin.purchase;
 
import cn.hutool.core.collection.CollUtil;
import cn.idev.excel.annotation.ExcelIgnoreUnannotated;
import cn.idev.excel.annotation.ExcelProperty;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
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.module.erp.controller.admin.purchase.vo.demand.ErpPurchaseDemandPageReqVO;
import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.demand.ErpPurchaseDemandPurchaseRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.demand.ErpPurchaseDemandRespVO;
import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.demand.ErpPurchaseDemandSaveReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchaseDemandDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchaseDemandItemDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchaseOrderDO;
import cn.iocoder.yudao.module.erp.service.purchase.ErpPurchaseDemandService;
import cn.iocoder.yudao.module.mdm.api.item.MdmItemApi;
import cn.iocoder.yudao.module.mdm.api.item.dto.MdmItemRespDTO;
import cn.iocoder.yudao.module.mdm.api.unit.MdmUnitMeasureApi;
import cn.iocoder.yudao.module.mdm.api.unit.dto.MdmUnitMeasureRespDTO;
import cn.iocoder.yudao.module.srm.api.supplier.SrmSupplierApi;
import cn.iocoder.yudao.module.srm.api.supplier.dto.SrmSupplierRespDTO;
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 lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
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.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
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;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMultiMap;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
 
@Tag(name = "管理后台 - ERP 申请采购单(需求单)")
@RestController
@RequestMapping("/erp/purchase-demand")
@Validated
public class ErpPurchaseDemandController {
 
    @Resource
    private ErpPurchaseDemandService demandService;
    @Resource
    private SrmSupplierApi srmSupplierApi;
    @Resource
    private MdmItemApi mdmItemApi;
    @Resource
    private MdmUnitMeasureApi mdmUnitMeasureApi;
 
    @PostMapping("/create")
    @Operation(summary = "创建申请采购单")
    @PreAuthorize("@ss.hasPermission('erp:purchase-demand:create')")
    public CommonResult<Long> createDemand(@Valid @RequestBody ErpPurchaseDemandSaveReqVO createReqVO) {
        return success(demandService.createDemand(createReqVO));
    }
 
    @PutMapping("/update")
    @Operation(summary = "更新申请采购单")
    @PreAuthorize("@ss.hasPermission('erp:purchase-demand:update')")
    public CommonResult<Boolean> updateDemand(@Valid @RequestBody ErpPurchaseDemandSaveReqVO updateReqVO) {
        demandService.updateDemand(updateReqVO);
        return success(true);
    }
 
    @DeleteMapping("/delete")
    @Operation(summary = "删除申请采购单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:purchase-demand:delete')")
    public CommonResult<Boolean> deleteDemand(@RequestParam("id") Long id) {
        demandService.deleteDemand(id);
        return success(true);
    }
 
    @GetMapping("/get")
    @Operation(summary = "获得申请采购单")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<ErpPurchaseDemandRespVO> getDemand(@RequestParam("id") Long id) {
        ErpPurchaseDemandDO demand = demandService.getDemand(id);
        if (demand == null) {
            return success(null);
        }
        return success(buildRespList(Collections.singletonList(demand)).get(0));
    }
 
    @GetMapping("/page")
    @Operation(summary = "获得申请采购单分页")
    public CommonResult<PageResult<ErpPurchaseDemandRespVO>> getDemandPage(@Valid ErpPurchaseDemandPageReqVO pageReqVO) {
        PageResult<ErpPurchaseDemandDO> pageResult = demandService.getDemandPage(pageReqVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(PageResult.empty(pageResult.getTotal()));
        }
        return success(new PageResult<>(buildRespList(pageResult.getList()), pageResult.getTotal()));
    }
 
    @PostMapping("/purchase-order")
    @Operation(summary = "需求单采购下单(支持批量,所选需求单需属于同一供货商)")
    @Parameter(name = "ids", description = "需求单编号列表", required = true)
    @PreAuthorize("@ss.hasPermission('erp:purchase-demand:purchase')")
    public CommonResult<ErpPurchaseDemandPurchaseRespVO> generatePurchaseOrder(@RequestBody List<Long> ids) {
        ErpPurchaseOrderDO order = demandService.generatePurchaseOrder(ids);
        return success(new ErpPurchaseDemandPurchaseRespVO(order.getId(), order.getNo()));
    }
 
    @GetMapping("/export-excel")
    @Operation(summary = "导出申请采购单 Excel")
    @PreAuthorize("@ss.hasPermission('erp:purchase-demand:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDemandExcel(@Valid ErpPurchaseDemandPageReqVO pageReqVO,
                                  HttpServletResponse response) throws IOException {
        // 复用分页过滤条件,导出内容与列表一致
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ErpPurchaseDemandDO> list = demandService.getDemandPage(pageReqVO).getList();
        List<ErpPurchaseDemandExcelRow> rows = buildRespList(list).stream()
                .map(vo -> new ErpPurchaseDemandExcelRow(vo.getNo(), vo.getSupplierName(),
                        toPurchaseCategoryLabel(vo.getPurchaseCategory()), toSourceTypeLabel(vo.getSourceType()),
                        vo.getWorkOrderNo(), vo.getDemandTime(), vo.getTotalCount(), toStatusLabel(vo.getStatus()),
                        vo.getOrderNo(), vo.getPurpose(), vo.getRemark(), vo.getCreateTime()))
                .collect(Collectors.toList());
        ExcelUtils.write(response, "申请采购单.xlsx", "申请采购单", ErpPurchaseDemandExcelRow.class, rows);
    }
 
    private List<ErpPurchaseDemandRespVO> buildRespList(List<ErpPurchaseDemandDO> list) {
        // 1.1 需求明细
        List<ErpPurchaseDemandItemDO> itemList = demandService.getDemandItemListByDemandIds(
                convertSet(list, ErpPurchaseDemandDO::getId));
        Map<Long, List<ErpPurchaseDemandItemDO>> itemMap = convertMultiMap(itemList, ErpPurchaseDemandItemDO::getDemandId);
        // 1.2 物料 / 单位 / 供货商
        Map<Long, MdmItemRespDTO> mdmItemMap = mdmItemApi.getItemMap(convertSet(itemList, ErpPurchaseDemandItemDO::getProductId));
        Map<Long, MdmUnitMeasureRespDTO> unitMap = mdmUnitMeasureApi.getUnitMeasureMap(
                convertSet(itemList, ErpPurchaseDemandItemDO::getProductUnitId));
        Map<Long, SrmSupplierRespDTO> supplierMap = srmSupplierApi.getSupplierList(
                        convertSet(list, ErpPurchaseDemandDO::getSupplierId)).getCheckedData().stream()
                .collect(Collectors.toMap(SrmSupplierRespDTO::getId, v -> v, (a, b) -> a));
        // 2. 拼接
        return BeanUtils.toBean(list, ErpPurchaseDemandRespVO.class, demand -> {
            demand.setItems(BeanUtils.toBean(itemMap.get(demand.getId()), ErpPurchaseDemandRespVO.Item.class, item -> {
                MapUtils.findAndThen(mdmItemMap, item.getProductId(), mdmItem ->
                        item.setProductName(mdmItem.getName()).setProductBarCode(mdmItem.getBarCode()));
                MapUtils.findAndThen(unitMap, item.getProductUnitId(), unit -> item.setProductUnitName(unit.getName()));
            }));
            demand.setProductNames(CollUtil.join(demand.getItems(), ",", ErpPurchaseDemandRespVO.Item::getProductName));
            MapUtils.findAndThen(supplierMap, demand.getSupplierId(), supplier -> demand.setSupplierName(supplier.getName()));
        });
    }
 
    private static String toPurchaseCategoryLabel(Integer purchaseCategory) {
        if (purchaseCategory == null) {
            return "";
        }
        return purchaseCategory == 2 ? "原辅料" : "代理商品";
    }
 
    private static String toSourceTypeLabel(Integer sourceType) {
        if (sourceType == null) {
            return "";
        }
        return sourceType == 1 ? "生产计划" : "手工创建";
    }
 
    private static String toStatusLabel(Integer status) {
        if (status == null) {
            return "";
        }
        return status == 2 ? "已采购" : "未采购";
    }
 
    /**
     * 导出用行结构(Excel 列)
     */
    @ExcelIgnoreUnannotated
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class ErpPurchaseDemandExcelRow {
 
        @ExcelProperty("需求单号")
        private String no;
 
        @ExcelProperty("供货商")
        private String supplierName;
 
        @ExcelProperty("采购品类")
        private String purchaseCategoryLabel;
 
        @ExcelProperty("来源")
        private String sourceTypeLabel;
 
        @ExcelProperty("关联生产任务")
        private String workOrderNo;
 
        @ExcelProperty("需求日期")
        private LocalDate demandTime;
 
        @ExcelProperty("合计数量")
        private BigDecimal totalCount;
 
        @ExcelProperty("状态")
        private String statusLabel;
 
        @ExcelProperty("采购订单号")
        private String orderNo;
 
        @ExcelProperty("用途说明")
        private String purpose;
 
        @ExcelProperty("备注")
        private String remark;
 
        @ExcelProperty("创建时间")
        private LocalDateTime createTime;
 
    }
 
}