package cn.iocoder.yudao.module.erp.controller.admin.purchase;
|
|
import cn.hutool.core.collection.CollUtil;
|
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.object.BeanUtils;
|
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
|
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
|
import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.plan.ErpPurchasePlanPageReqVO;
|
import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.plan.ErpPurchasePlanRespVO;
|
import cn.iocoder.yudao.module.erp.controller.admin.purchase.vo.plan.ErpPurchasePlanSaveReqVO;
|
import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchasePlanDO;
|
import cn.iocoder.yudao.module.erp.dal.dataobject.purchase.ErpPurchasePlanItemDO;
|
import cn.iocoder.yudao.module.erp.service.purchase.ErpPurchasePlanService;
|
import cn.iocoder.yudao.module.erp.service.purchase.ErpPurchasePlanServiceImpl;
|
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 cn.iocoder.yudao.module.system.api.dept.DeptApi;
|
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
|
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.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.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;
|
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMultiMap;
|
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
|
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
|
|
@Tag(name = "管理后台 - ERP 采购计划")
|
@RestController
|
@RequestMapping("/erp/purchase-plan")
|
@Validated
|
public class ErpPurchasePlanController {
|
|
@Resource
|
private ErpPurchasePlanService purchasePlanService;
|
@Resource
|
private SrmSupplierApi srmSupplierApi;
|
@Resource
|
private MdmItemApi mdmItemApi;
|
@Resource
|
private MdmUnitMeasureApi mdmUnitMeasureApi;
|
@Resource
|
private AdminUserApi adminUserApi;
|
@Resource
|
private DeptApi deptApi;
|
|
@PostMapping("/create")
|
@Operation(summary = "创建采购计划")
|
@PreAuthorize("@ss.hasPermission('erp:purchase-plan:create')")
|
public CommonResult<Long> createPurchasePlan(@Valid @RequestBody ErpPurchasePlanSaveReqVO createReqVO) {
|
return success(purchasePlanService.createPurchasePlan(createReqVO));
|
}
|
|
@PutMapping("/update")
|
@Operation(summary = "更新采购计划")
|
@PreAuthorize("@ss.hasPermission('erp:purchase-plan:update')")
|
public CommonResult<Boolean> updatePurchasePlan(@Valid @RequestBody ErpPurchasePlanSaveReqVO updateReqVO) {
|
purchasePlanService.updatePurchasePlan(updateReqVO);
|
return success(true);
|
}
|
|
@DeleteMapping("/delete")
|
@Operation(summary = "删除采购计划")
|
@Parameter(name = "ids", description = "编号数组", required = true)
|
@PreAuthorize("@ss.hasPermission('erp:purchase-plan:delete')")
|
public CommonResult<Boolean> deletePurchasePlan(@RequestParam("ids") List<Long> ids) {
|
purchasePlanService.deletePurchasePlan(ids);
|
return success(true);
|
}
|
|
@GetMapping("/get")
|
@Operation(summary = "获得采购计划")
|
@Parameter(name = "id", description = "编号", required = true, example = "1024")
|
@PreAuthorize("@ss.hasPermission('erp:purchase-plan:query')")
|
public CommonResult<ErpPurchasePlanRespVO> getPurchasePlan(@RequestParam("id") Long id) {
|
ErpPurchasePlanDO plan = purchasePlanService.getPurchasePlan(id);
|
if (plan == null) {
|
return success(null);
|
}
|
List<ErpPurchasePlanItemDO> planItemList = purchasePlanService.getPurchasePlanItemListByPlanId(id);
|
Map<Long, MdmItemRespDTO> itemMap = mdmItemApi.getItemMap(
|
convertSet(planItemList, ErpPurchasePlanItemDO::getProductId));
|
Map<Long, MdmUnitMeasureRespDTO> unitMeasureMap = mdmUnitMeasureApi.getUnitMeasureMap(
|
convertSet(planItemList, ErpPurchasePlanItemDO::getProductUnitId));
|
return success(BeanUtils.toBean(plan, ErpPurchasePlanRespVO.class, planVO -> {
|
planVO.setDemandSourceName(convertDemandSourceName(plan.getDemandSource()));
|
planVO.setItems(BeanUtils.toBean(planItemList, ErpPurchasePlanRespVO.Item.class, item -> {
|
MapUtils.findAndThen(itemMap, item.getProductId(), mdmItem -> item.setProductName(mdmItem.getName()));
|
MapUtils.findAndThen(unitMeasureMap, item.getProductUnitId(), unit -> item.setProductUnitName(unit.getName()));
|
}));
|
}));
|
}
|
|
@GetMapping("/page")
|
@Operation(summary = "获得采购计划分页")
|
@PreAuthorize("@ss.hasPermission('erp:purchase-plan:query')")
|
public CommonResult<PageResult<ErpPurchasePlanRespVO>> getPurchasePlanPage(@Valid ErpPurchasePlanPageReqVO pageReqVO) {
|
PageResult<ErpPurchasePlanDO> pageResult = purchasePlanService.getPurchasePlanPage(pageReqVO);
|
return success(buildPurchasePlanVOPageResult(pageResult));
|
}
|
|
@GetMapping("/export-excel")
|
@Operation(summary = "导出采购计划 Excel")
|
@PreAuthorize("@ss.hasPermission('erp:purchase-plan:export')")
|
@ApiAccessLog(operateType = EXPORT)
|
public void exportPurchasePlanExcel(@Valid ErpPurchasePlanPageReqVO pageReqVO,
|
HttpServletResponse response) throws IOException {
|
pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
|
List<ErpPurchasePlanRespVO> list = buildPurchasePlanVOPageResult(purchasePlanService.getPurchasePlanPage(pageReqVO)).getList();
|
ExcelUtils.write(response, "采购计划.xls", "数据", ErpPurchasePlanRespVO.class, list);
|
}
|
|
@PutMapping("/submit")
|
@Operation(summary = "提交采购计划审批")
|
@PreAuthorize("@ss.hasPermission('erp:purchase-plan:submit')")
|
public CommonResult<Boolean> submitPurchasePlan(@RequestParam("id") Long id,
|
@RequestParam("processDefinitionKey") String processDefinitionKey) {
|
Long userId = getLoginUserId();
|
purchasePlanService.submitPurchasePlan(id, processDefinitionKey, userId);
|
return success(true);
|
}
|
|
@GetMapping("/approve-process-list")
|
@Operation(summary = "获取采购计划审批流程列表")
|
@PreAuthorize("@ss.hasPermission('erp:purchase-plan:query')")
|
public CommonResult<List<Map<String, Object>>> getApproveProcessList() {
|
return success(purchasePlanService.getPurchasePlanApproveProcessDefinitionList());
|
}
|
|
@PostMapping("/generate-from-stock-alert")
|
@Operation(summary = "根据库存预警生成采购计划", description = "遍历启用安全库存的物料,按缺量生成草稿采购计划")
|
@PreAuthorize("@ss.hasPermission('erp:purchase-plan:create')")
|
public CommonResult<Long> generatePlanFromStockAlert(@RequestParam(value = "name", required = false) String name,
|
@RequestParam(value = "supplierId", required = false) Long supplierId) {
|
ErpPurchasePlanDO plan = purchasePlanService.generatePlanFromStockAlert(name, supplierId);
|
return success(plan.getId());
|
}
|
|
@PostMapping("/generate-requests")
|
@Operation(summary = "生成采购申请")
|
@PreAuthorize("@ss.hasPermission('erp:purchase-plan:generate-request')")
|
public CommonResult<List<Long>> generatePurchaseRequests(@RequestParam("id") Long id) {
|
return success(purchasePlanService.generatePurchaseRequests(id));
|
}
|
|
private PageResult<ErpPurchasePlanRespVO> buildPurchasePlanVOPageResult(PageResult<ErpPurchasePlanDO> pageResult) {
|
if (CollUtil.isEmpty(pageResult.getList())) {
|
return PageResult.empty(pageResult.getTotal());
|
}
|
// 1.1 计划明细
|
List<ErpPurchasePlanItemDO> planItemList = purchasePlanService.getPurchasePlanItemListByPlanIds(
|
convertSet(pageResult.getList(), ErpPurchasePlanDO::getId));
|
Map<Long, List<ErpPurchasePlanItemDO>> planItemMap = convertMultiMap(planItemList, ErpPurchasePlanItemDO::getPlanId);
|
// 1.2 物料信息
|
Map<Long, MdmItemRespDTO> itemMap = mdmItemApi.getItemMap(
|
convertSet(planItemList, ErpPurchasePlanItemDO::getProductId));
|
// 1.3 供应商信息
|
Map<Long, SrmSupplierRespDTO> supplierMap = srmSupplierApi.getSupplierList(
|
convertSet(pageResult.getList(), ErpPurchasePlanDO::getSupplierId)).getCheckedData().stream()
|
.collect(Collectors.toMap(SrmSupplierRespDTO::getId, v -> v, (a, b) -> a));
|
// 1.4 用户信息
|
Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(
|
convertSet(pageResult.getList(), ErpPurchasePlanDO::getPlanUserId));
|
// 1.5 部门信息
|
Map<Long, DeptRespDTO> deptMap = deptApi.getDeptMap(
|
convertSet(pageResult.getList(), ErpPurchasePlanDO::getPlanDeptId));
|
// 2. 开始拼接
|
return BeanUtils.toBean(pageResult, ErpPurchasePlanRespVO.class, plan -> {
|
plan.setDemandSourceName(convertDemandSourceName(plan.getDemandSource()));
|
plan.setItems(BeanUtils.toBean(planItemMap.get(plan.getId()), ErpPurchasePlanRespVO.Item.class,
|
item -> MapUtils.findAndThen(itemMap, item.getProductId(), mdmItem -> item.setProductName(mdmItem.getName()))));
|
plan.setProductNames(CollUtil.join(plan.getItems(), ",", ErpPurchasePlanRespVO.Item::getProductName));
|
MapUtils.findAndThen(supplierMap, plan.getSupplierId(), supplier -> plan.setSupplierName(supplier.getName()));
|
MapUtils.findAndThen(userMap, plan.getPlanUserId(), user -> plan.setPlanUserName(user.getNickname()));
|
MapUtils.findAndThen(deptMap, plan.getPlanDeptId(), dept -> plan.setPlanDeptName(dept.getName()));
|
});
|
}
|
|
private String convertDemandSourceName(Integer demandSource) {
|
if (ErpPurchasePlanServiceImpl.DEMAND_SOURCE_STOCK_ALERT.equals(demandSource)) {
|
return "库存预警";
|
}
|
if (ErpPurchasePlanServiceImpl.DEMAND_SOURCE_PLAN.equals(demandSource)) {
|
return "运维/生产计划";
|
}
|
return null;
|
}
|
|
}
|