| package com.vincent.rsf.server.manager.service.impl; | 
|   | 
| import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 
| import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
| import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; | 
| import com.baomidou.mybatisplus.core.metadata.IPage; | 
| import com.vincent.rsf.framework.common.R; | 
| import com.vincent.rsf.framework.exception.CoolException; | 
| import com.vincent.rsf.server.manager.controller.params.IsptOrderParam; | 
| import com.vincent.rsf.server.manager.controller.params.QlyInspectAndItem; | 
| import com.vincent.rsf.server.manager.entity.*; | 
| import com.vincent.rsf.server.manager.enums.QlyIsptResult; | 
| import com.vincent.rsf.server.manager.enums.QlyIsptStatus; | 
| import com.vincent.rsf.server.manager.mapper.QlyInspectMapper; | 
| import com.vincent.rsf.server.manager.service.*; | 
| import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 
| import com.vincent.rsf.server.system.constant.SerialRuleCode; | 
| import com.vincent.rsf.server.system.utils.SerialRuleUtils; | 
| import org.apache.commons.lang3.StringUtils; | 
| import org.springframework.beans.BeanUtils; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.stereotype.Service; | 
| import org.springframework.transaction.annotation.Transactional; | 
|   | 
| import java.util.ArrayList; | 
| import java.util.List; | 
| import java.util.Map; | 
| import java.util.Objects; | 
| import java.util.stream.Collectors; | 
|   | 
| @Service("qlyInspectService") | 
| public class QlyInspectServiceImpl extends ServiceImpl<QlyInspectMapper, QlyInspect> implements QlyInspectService { | 
|   | 
|     @Autowired | 
|     private AsnOrderItemService asnOrderItemService; | 
|   | 
|     @Autowired | 
|     private AsnOrderService asnOrderService; | 
|   | 
|     @Autowired | 
|     private QlyIsptItemService qlyIsptItemService; | 
|   | 
|     @Autowired | 
|     private CompanysService companysService; | 
|   | 
|     @Autowired | 
|     private WarehouseAreasItemService warehouseAreasItemService; | 
|   | 
|     @Override | 
|     public List<AsnOrderItem> listByAsn(Map<String, Object> map) { | 
|         if (Objects.isNull(map.get("asnCode"))) { | 
|             throw new CoolException("收货单据明细编码不能为空!!"); | 
|         } | 
|         AsnOrder asnOrder = asnOrderService.getOne(new LambdaQueryWrapper<AsnOrder>() | 
|                 .eq(AsnOrder::getCode, map.get("asnCode")) | 
|                 .ne(AsnOrder::getNtyStatus, 0)); | 
|         if (Objects.isNull(asnOrder)) { | 
|             throw new CoolException("单据不存在!!"); | 
|         } | 
|         List<AsnOrderItem> asnOrderItems = asnOrderItemService.list(new LambdaQueryWrapper<AsnOrderItem>() | 
|                 .eq(AsnOrderItem::getAsnCode, map.get("asnCode")) | 
|                 .eq(AsnOrderItem::getNtyStatus, 1)); | 
|         if (asnOrderItems.isEmpty()) { | 
|             return new ArrayList<>(); | 
|         } | 
|         return asnOrderItems; | 
|     } | 
|   | 
|     @Override | 
|     public R allSave(QlyInspectAndItem params) { | 
|         if (Objects.isNull(params.getQlyInspect())) { | 
|             throw new CoolException("质检单据不能为空!!"); | 
|         } | 
|         QlyInspect inspect = params.getQlyInspect(); | 
|         if (Objects.isNull(inspect.getWkType())) { | 
|             throw new CoolException("业务类型不能为空!!"); | 
|         } | 
|         String code = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_INSPECT_CODE, inspect); | 
|         inspect.setCode(code); | 
|         if (!this.saveOrUpdate(inspect)) { | 
|             throw new CoolException("质检单保存失败!!"); | 
|         } | 
|         List<QlyIsptItem> isptItems = params.getQlyIsptItems(); | 
|         if (isptItems.isEmpty()) { | 
|             return R.ok("保存成功!!"); | 
|         } | 
|         List<QlyIsptItem> items = new ArrayList<>(); | 
|         for (QlyIsptItem isptItem : isptItems) { | 
|             if (Objects.isNull(isptItem.getMatnrCode())) { | 
|                 continue; | 
|             } | 
|             isptItem.setIspectId(inspect.getId()); | 
|             items.add(isptItem); | 
|         } | 
|         if (!qlyIsptItemService.saveOrUpdateBatch(items)) { | 
|             throw new CoolException("质检明细保存失败!!"); | 
|         } | 
|         return R.ok("保存成功!!"); | 
|     } | 
|   | 
|     /** | 
|     * @author Ryan | 
|     * @description 获取未质检单据 | 
|     * @param | 
|     * @return | 
|     * @time 2025/3/31 10:12 | 
|     */ | 
|     @Override | 
|     public List<WarehouseAreasItem> getUnInspect(Map<String, String> params) { | 
|         String code = params.get("asnCode"); | 
|         List<WarehouseAreasItem> warehouseAreasItems = warehouseAreasItemService | 
|                 .list(new QueryWrapper<WarehouseAreasItem>() | 
|                         .select("asn_id as id, SUM(anfme) anfme, SUM(qty) qty, area_id, area_name, asn_code, asn_id, splr_id, splr_batch") | 
|                         .eq(StringUtils.isNotBlank(code), "asn_code", code) | 
|                         .lambda() | 
| //                        .isNull(WarehouseAreasItem::getIsptResult) | 
|                         .lt(WarehouseAreasItem::getWorkQty, 1) | 
|                         .lt(WarehouseAreasItem::getQty, 1) | 
|                 .groupBy(WarehouseAreasItem::getAsnCode)); | 
|         return warehouseAreasItems; | 
|     } | 
|   | 
|     /** | 
|     * @author Ryan | 
|     * @description 保存 | 
|     * @param | 
|     * @return | 
|     * @time 2025/3/31 14:54 | 
|     */ | 
|   | 
|     @Override | 
|     @Transactional(rollbackFor = Exception.class) | 
|     public R saveSelected(IsptOrderParam param, Long loginUserId) { | 
|         if (Objects.isNull(param.getIds()) || param.getIds().isEmpty()) { | 
|             throw new CoolException("单据ID不能为空!!"); | 
|         } | 
|         List<AsnOrder> asnOrders = asnOrderService.list(new LambdaQueryWrapper<AsnOrder>().in(AsnOrder::getId, param.getIds())); | 
|         if (asnOrders.isEmpty()) { | 
|             throw new CoolException("单据不存在!!"); | 
|         } | 
| //        List<AsnOrderItem> orderItems = asnOrderItemService.list(new LambdaQueryWrapper<AsnOrderItem>().in(AsnOrderItem::getAsnId, param.getIds())); | 
| //        if (orderItems.isEmpty()) { | 
| //            throw new CoolException("单据明细不存在!!"); | 
| //        } | 
|   | 
|         List<WarehouseAreasItem> orderItems = warehouseAreasItemService.list(new LambdaQueryWrapper<WarehouseAreasItem>() | 
|                 .in(WarehouseAreasItem::getAsnId, param.getIds())); | 
|         Map<Long, List<WarehouseAreasItem>> listMap = orderItems.stream().collect(Collectors.groupingBy(WarehouseAreasItem::getAsnId)); | 
|         for (AsnOrder asnOrder : asnOrders) { | 
|             QlyInspect inspect = new QlyInspect(); | 
|             String ruleCode = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_INSPECT_CODE, null); | 
|             if (StringUtils.isBlank(ruleCode)) { | 
|                 throw new CoolException("策略错误:请检查策略「" + SerialRuleCode.SYS_INSPECT_CODE + "」是否设置正确!!"); | 
|             } | 
|             inspect.setCode(ruleCode) | 
|                     .setWkType(asnOrder.getWkType()) | 
|                     .setIsptStatus(QlyIsptStatus.QLY_ISPT_STAS_ING.val) | 
|                     .setCreateBy(loginUserId) | 
|                     .setAsnId(asnOrder.getId()) | 
|                     .setAsnCode(asnOrder.getCode()); | 
|             /**获取单据明细*/ | 
|             List<WarehouseAreasItem> asnOrderItems = listMap.get(asnOrder.getId()); | 
|             if (Objects.isNull(asnOrderItems) || asnOrderItems.isEmpty()) { | 
|                 continue; | 
|             } | 
|             double anfme = asnOrderItems.stream().mapToDouble(WarehouseAreasItem::getAnfme).sum(); | 
|             inspect.setRcptQty(anfme); | 
|             if (!this.save(inspect)) { | 
|                 throw new CoolException("单据" + asnOrder.getCode() + "保存失败!!"); | 
|             } | 
|             List<QlyIsptItem> items = new ArrayList<>(); | 
|             for (WarehouseAreasItem orderItem : asnOrderItems) { | 
|                 QlyIsptItem isptItem = new QlyIsptItem(); | 
|                 BeanUtils.copyProperties(orderItem, isptItem); | 
|                 isptItem.setAsnItemId(orderItem.getId()) | 
|                         .setIspectId(inspect.getId()) | 
|                         .setRcptQty(orderItem.getAnfme()) | 
|                         .setRcptId(orderItem.getId()) | 
|                         .setPlatItemId(orderItem.getPlatItemId()) | 
|                         .setPlatOrderCode(orderItem.getPlatOrderCode()) | 
|                         .setPlatWorkCode(orderItem.getPlatWorkCode()) | 
|                         .setProjectCode(orderItem.getProjectCode()) | 
|                         .setAnfme(0.0) | 
|                         .setFieldsIndex(orderItem.getFieldsIndex()) | 
|                         .setSplrBatch(orderItem.getSplrBatch()) | 
|                         .setSplrName(orderItem.getSplrId$()) | 
|                         .setCreateBy(loginUserId) | 
|                         .setUpdateBy(loginUserId) | 
|                         .setDlyQty(orderItem.getAnfme()); | 
|                 items.add(isptItem); | 
|             } | 
|             if (!qlyIsptItemService.saveBatch(items)) { | 
|                 throw new CoolException("明细保存失败!!"); | 
|             } | 
|         } | 
| //        if (!asnOrderService.update(new LambdaUpdateWrapper<AsnOrder>().in(AsnOrder::getId, param.getIds()))) { | 
| //            throw new CoolException("报检状态修改失败!!"); | 
| //        } | 
|         return R.ok("保存成功!!"); | 
|     } | 
|   | 
|     @Override | 
|     public boolean qlySave(QlyInspect qlyInspect) { | 
|         String code = SerialRuleUtils.generateRuleCode(SerialRuleCode.SYS_INSPECT_CODE, qlyInspect); | 
|         if (Objects.isNull(code) || StringUtils.isBlank(code)) { | 
|             throw new CoolException("编码规则错误:" + "请检查编码:" + "「SYS_INSPECT_CODE」" + "是否设置成功!!"); | 
|         } | 
|         qlyInspect.setCode(code); | 
|         if (Objects.isNull(qlyInspect.getAsnCode()) || StringUtils.isBlank(qlyInspect.getAsnCode())) { | 
|             throw new CoolException("收货单号不能为空!!"); | 
|         } | 
|         AsnOrder asnOrder = asnOrderService.getOne(new LambdaQueryWrapper<AsnOrder>().eq(AsnOrder::getId, qlyInspect.getAsnCode())); | 
|         if (Objects.isNull(asnOrder)) { | 
|             throw new CoolException("收货单不存在!!"); | 
|         } | 
|         qlyInspect.setAsnCode(asnOrder.getCode()).setAsnId(asnOrder.getId()); | 
|   | 
|         return this.save(qlyInspect); | 
|     } | 
|   | 
|     /** | 
|      * @author Ryan | 
|      * @date 2025/5/14 | 
|      * @description: 质检审核确认 | 
|      * @version 1.0 | 
|      */ | 
|     @Override | 
|     @Transactional(rollbackFor = Exception.class) | 
|     public R verifyComplate(Long id) { | 
|         if (Objects.isNull(id)) { | 
|             throw new CoolException("参数不能为空!!"); | 
|         } | 
|         QlyInspect inspect = this.getById(id); | 
|         if (Objects.isNull(inspect)) { | 
|             throw new CoolException("质检单据不存在!!"); | 
|         } | 
|         inspect.setIsptStatus(QlyIsptStatus.QLY_ISPT_STAS_DONE.val); | 
|         if (!this.saveOrUpdate(inspect)) { | 
|             throw new CoolException("单据修改失败!!"); | 
|         } | 
|         List<QlyIsptItem> items = qlyIsptItemService.selectIsptResult(new LambdaQueryWrapper<QlyIsptItem>().eq(QlyIsptItem::getIspectId, inspect.getId())); | 
|         if (items.isEmpty()) { | 
|             throw new CoolException("质检单据明细不存在!!"); | 
|         } | 
|   | 
|         for (QlyIsptItem isptItem : items) { | 
|             if (isptItem.getSafeQty().compareTo(0.0) <= 0 && isptItem.getDisQty().compareTo(0.0) <= 0 ) { | 
|                 continue; | 
|             } | 
|             WarehouseAreasItem item = warehouseAreasItemService.getById(isptItem.getRcptId()); | 
|             if (Objects.isNull(item)) { | 
|                 throw new CoolException("库存明细不存在!!"); | 
|             } | 
|             qlyIsptItemService.confirmReceipt(isptItem, item); | 
|         } | 
|   | 
|         return R.ok("审核完成!!"); | 
|     } | 
| } |