| | |
| | | import cn.iocoder.yudao.module.mes.controller.admin.pro.mps.vo.MesProMpsSaveReqVO; |
| | | import cn.iocoder.yudao.module.crm.api.customer.CrmCustomerApi; |
| | | import cn.iocoder.yudao.module.crm.api.customer.dto.CrmCustomerRespDTO; |
| | | import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; |
| | | import cn.iocoder.yudao.module.erp.api.sale.ErpSaleOrderApi; |
| | | import cn.iocoder.yudao.module.system.api.permission.PermissionApi; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.md.item.MesMdItemDO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.md.unitmeasure.MesMdUnitMeasureDO; |
| | | import cn.iocoder.yudao.module.mes.dal.dataobject.pro.mps.MesProMpsDO; |
| | | import cn.iocoder.yudao.module.mes.service.md.item.MesMdItemService; |
| | | import cn.iocoder.yudao.module.mes.service.md.unitmeasure.MesMdUnitMeasureService; |
| | | import cn.iocoder.yudao.module.mes.service.pro.mps.MesProMpsService; |
| | | import io.swagger.v3.oas.annotations.Operation; |
| | | import io.swagger.v3.oas.annotations.Parameter; |
| | |
| | | import java.util.Collections; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.ArrayList; |
| | | import jakarta.validation.constraints.NotEmpty; |
| | | import jakarta.validation.constraints.NotNull; |
| | | import cn.iocoder.yudao.framework.common.exception.ServiceException; |
| | | import lombok.Data; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | 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; |
| | | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| | | import static cn.iocoder.yudao.module.mes.enums.ErrorCodeConstants.PRO_MPS_MERGE_ISSUE_DISABLED; |
| | | |
| | | @Tag(name = "管理后台 - MES 生产主计划") |
| | | @RestController |
| | | @RequestMapping("/mes/pro-mps") |
| | | @Validated |
| | | @Slf4j |
| | | public class MesProMpsController { |
| | | |
| | | @Resource |
| | |
| | | @Resource |
| | | private MesMdItemService itemService; |
| | | @Resource |
| | | private MesMdUnitMeasureService unitMeasureService; |
| | | @Resource |
| | | private CrmCustomerApi customerApi; |
| | | @Resource |
| | | private ErpSaleOrderApi saleOrderApi; |
| | | @Resource |
| | | private PermissionApi permissionApi; |
| | | |
| | | @PostMapping("/create") |
| | | @Operation(summary = "创建生产主计划") |
| | |
| | | @GetMapping("/get") |
| | | @Operation(summary = "获得生产主计划") |
| | | @Parameter(name = "id", description = "编号", required = true, example = "1024") |
| | | @PreAuthorize("@ss.hasPermission('mes:pro-mps:query')") |
| | | public CommonResult<MesProMpsRespVO> getMps(@RequestParam("id") Long id) { |
| | | MesProMpsDO mps = mpsService.getMps(id); |
| | | if (mps == null) { |
| | |
| | | |
| | | @GetMapping("/page") |
| | | @Operation(summary = "获得生产主计划分页") |
| | | @PreAuthorize("@ss.hasPermission('mes:pro-mps:query')") |
| | | public CommonResult<PageResult<MesProMpsRespVO>> getMpsPage(@Valid MesProMpsPageReqVO pageReqVO) { |
| | | PageResult<MesProMpsDO> pageResult = mpsService.getMpsPage(pageReqVO); |
| | | return success(new PageResult<>(buildMpsRespVOList(pageResult.getList()), pageResult.getTotal())); |
| | |
| | | @Operation(summary = "合并下发生产主计划") |
| | | @PreAuthorize("@ss.hasPermission('mes:pro-mps:merge-issue')") |
| | | public CommonResult<Long> mergeAndIssueMps(@RequestBody List<Long> mpsIds) { |
| | | return success(mpsService.mergeAndIssueMps(mpsIds)); |
| | | // 旧入口保留用于明确提示旧客户端升级,禁止绕过新的逐条下发权限校验。 |
| | | throw exception(PRO_MPS_MERGE_ISSUE_DISABLED); |
| | | } |
| | | |
| | | @PostMapping("/batch-issue") |
| | | @Operation(summary = "批量逐条下发生产主计划") |
| | | @PreAuthorize("@ss.hasPermission('mes:pro-mps:issue')") |
| | | public CommonResult<List<BatchIssueResult>> batchIssueMps(@RequestBody @NotEmpty List<@NotNull Long> ids) { |
| | | List<BatchIssueResult> results = new ArrayList<>(); |
| | | for (Long id : ids) { |
| | | BatchIssueResult result = new BatchIssueResult(); |
| | | result.setMpsId(id); |
| | | try { |
| | | result.setWorkOrderId(mpsService.issueMps(id)); |
| | | } catch (ServiceException ex) { |
| | | result.setError(ex.getMessage()); |
| | | } catch (Exception ex) { |
| | | log.error("[batchIssueMps] 下发失败,mpsId={}", id, ex); |
| | | result.setError("下发失败,请联系管理员查看日志"); |
| | | } |
| | | results.add(result); |
| | | } |
| | | return success(results); |
| | | } |
| | | |
| | | @Data |
| | | public static class BatchIssueResult { |
| | | private Long mpsId; |
| | | private Long workOrderId; |
| | | private String error; |
| | | } |
| | | |
| | | @PostMapping("/cancel") |
| | |
| | | // 1. 批量获取关联数据 |
| | | Map<Long, MesMdItemDO> itemMap = itemService.getItemMap( |
| | | convertSet(list, MesProMpsDO::getProductId)); |
| | | Map<Long, MesMdUnitMeasureDO> unitMap = unitMeasureService.getUnitMeasureMap( |
| | | convertSet(itemMap.values(), MesMdItemDO::getUnitMeasureId)); |
| | | Map<Long, CrmCustomerRespDTO> clientMap = customerApi.getCustomerMap( |
| | | convertSet(list, MesProMpsDO::getClientId)); |
| | | // 2. 拼接 VO |
| | | // 2. 下发权限:与 MesProMpsServiceImpl#validateIssuePermission 保持一致 |
| | | Long loginUserId = SecurityFrameworkUtils.getLoginUserId(); |
| | | boolean superAdmin = loginUserId != null && permissionApi.hasAnyRoles(loginUserId, "super_admin"); |
| | | Map<Long, MesProMpsDO> mpsMap = list.stream() |
| | | .collect(java.util.stream.Collectors.toMap(MesProMpsDO::getId, mps -> mps)); |
| | | Map<Long, String> saleOrderCreatorMap = new java.util.HashMap<>(); |
| | | // 3. 拼接 VO |
| | | return BeanUtils.toBean(list, MesProMpsRespVO.class, vo -> { |
| | | MapUtils.findAndThen(itemMap, vo.getProductId(), item -> { |
| | | vo.setProductName(item.getName()) |
| | | .setProductCode(item.getCode()) |
| | | .setProductSpecification(item.getSpecification()); |
| | | MapUtils.findAndThen(unitMap, item.getUnitMeasureId(), |
| | | unit -> vo.setUnitMeasureName(unit.getName())); |
| | | }); |
| | | MapUtils.findAndThen(clientMap, vo.getClientId(), |
| | | client -> vo.setClientName(client.getName())); |
| | | MesProMpsDO mps = mpsMap.get(vo.getId()); |
| | | if (mps != null) { |
| | | String creator = mps.getCreator(); |
| | | if (mps.getSaleOrderId() != null) { |
| | | creator = saleOrderCreatorMap.computeIfAbsent(mps.getSaleOrderId(), orderId -> { |
| | | var order = saleOrderApi.getSaleOrder(orderId).getCheckedData(); |
| | | return order != null ? order.getCreator() : null; |
| | | }); |
| | | } |
| | | vo.setCreator(creator); |
| | | vo.setCanIssue(superAdmin || (loginUserId != null && String.valueOf(loginUserId).equals(creator))); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | } |
| | | } |