|  |  | 
 |  |  | package com.vincent.rsf.server.manager.service.impl; | 
 |  |  |  | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; | 
 |  |  | import com.baomidou.mybatisplus.core.metadata.IPage; | 
 |  |  | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | 
 |  |  | import com.vincent.rsf.framework.common.Cools; | 
 |  |  | import com.vincent.rsf.framework.common.R; | 
 |  |  | import com.vincent.rsf.framework.exception.CoolException; | 
 |  |  | import com.vincent.rsf.server.manager.controller.dto.LocStockDto; | 
 |  |  | import com.vincent.rsf.server.manager.controller.dto.LocUsageDto; | 
 |  |  | import com.vincent.rsf.server.manager.controller.params.LocMastInitParam; | 
 |  |  | import com.vincent.rsf.server.manager.controller.params.LocModifyParams; | 
 |  |  | import com.vincent.rsf.server.manager.entity.WarehouseAreas; | 
 |  |  | import com.vincent.rsf.server.manager.entity.*; | 
 |  |  | import com.vincent.rsf.server.manager.mapper.LocMapper; | 
 |  |  | import com.vincent.rsf.server.manager.entity.Loc; | 
 |  |  | import com.vincent.rsf.server.manager.service.LocService; | 
 |  |  | import com.vincent.rsf.server.manager.mapper.LocTypeRelaMapper; | 
 |  |  | import com.vincent.rsf.server.manager.service.*; | 
 |  |  | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 
 |  |  | import com.vincent.rsf.server.manager.service.WarehouseAreasService; | 
 |  |  | import com.vincent.rsf.server.manager.utils.Shelves; | 
 |  |  | import org.apache.commons.lang3.StringUtils; | 
 |  |  | 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.Date; | 
 |  |  | import java.util.Arrays; | 
 |  |  | import java.util.List; | 
 |  |  | import java.util.Objects; | 
 |  |  | import java.util.stream.Collectors; | 
 |  |  |  | 
 |  |  | @Service("locService") | 
 |  |  | public class LocServiceImpl extends ServiceImpl<LocMapper, Loc> implements LocService { | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private WarehouseAreasService warehouseAreasService; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private LocTypeRelaService locTypeRelaService; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private LocAreaRelaService locAreaRelaService; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private LocAreaService locAreaService; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private LocTypeRelaMapper locTypeRelaMapper; | 
 |  |  |     @Autowired | 
 |  |  |     private LocService locService; | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public R modifyLocs(LocModifyParams locs) { | 
 |  |  | 
 |  |  |             throw new CoolException("修改库位信息不能为空!!"); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         String join = loc.getType(); | 
 |  |  |         if (!Objects.isNull(loc.getTypeIds()) && !loc.getTypeIds().isEmpty()) { | 
 |  |  |              join = StringUtils.join(loc.getTypeIds(), ","); | 
 |  |  |         } | 
 |  |  |         boolean update = this.update(new LambdaUpdateWrapper<Loc>() | 
 |  |  |                 .in(Loc::getId, locs.getId()) | 
 |  |  |                 .eq(Loc::getStatus, 1) | 
 |  |  |                 .set(!Objects.isNull(loc.getAreaId()), Loc::getAreaId, loc.getAreaId()) | 
 |  |  |                 .set(!Objects.isNull(loc.getWarehouseId()), Loc::getWarehouseId, loc.getWarehouseId()) | 
 |  |  |                 .set(!Objects.isNull(loc.getUseStatus()), Loc::getUseStatus, loc.getUseStatus()) | 
 |  |  |                 .set(!Objects.isNull(loc.getType()), Loc::getType, loc.getType()) | 
 |  |  |                 .set(!Objects.isNull(loc.getTypeIds()) && !loc.getTypeIds().isEmpty(), Loc::getType, join) | 
 |  |  |                 .set(!Objects.isNull(loc.getLength()), Loc::getLength, loc.getLength()) | 
 |  |  |                 .set(!Objects.isNull(loc.getWidth()), Loc::getWidth, loc.getWidth()) | 
 |  |  |                 .set(!Objects.isNull(loc.getHeight()), Loc::getHeight, loc.getHeight()) | 
 |  |  | 
 |  |  |         if (!update) { | 
 |  |  |             throw new CoolException("库位信息修改失败!!"); | 
 |  |  |         } | 
 |  |  |         return R.ok("操作成功!!"); | 
 |  |  |  | 
 |  |  |         return R.ok(loc); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     @Transactional(rollbackFor = Exception.class) | 
 |  |  |     public R initLocs(LocMastInitParam param) { | 
 |  |  |     public R initLocs(LocMastInitParam param, Long loginUserId) { | 
 |  |  |         //清空表 | 
 |  |  |         if (this.baseMapper.delete(new LambdaUpdateWrapper<>()) < 0) { | 
 |  |  |             throw new CoolException("数据库初始化失败:老数据删除失败!!"); | 
 |  |  | //        this.baseMapper.deleteAll(); | 
 |  |  |         //清空当前库区库位 | 
 |  |  |         List<Loc> locs = this.list(new LambdaQueryWrapper<Loc>().eq(Loc::getWarehouseId, param.getWarehouseId()).eq(Loc::getAreaId, param.getAreaId())); | 
 |  |  |         if (!locs.isEmpty()) { | 
 |  |  |             if (!this.remove(new LambdaQueryWrapper<Loc>().eq(Loc::getWarehouseId, param.getWarehouseId()).eq(Loc::getAreaId, param.getAreaId()))) { | 
 |  |  |                 throw new CoolException("初始化失败:数据删除失败!!"); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         WarehouseAreas warehouseAreas = warehouseAreasService.getById(param.getAreaId()); | 
 |  |  |         WarehouseAreas warehouseAreas = warehouseAreasService.getOne(new LambdaQueryWrapper<WarehouseAreas>() | 
 |  |  |                 .eq(WarehouseAreas::getWarehouseId, param.getWarehouseId()) | 
 |  |  |                 .eq(WarehouseAreas::getId, param.getAreaId())); | 
 |  |  |         if (Objects.isNull(warehouseAreas)) { | 
 |  |  |             throw new CoolException("库区不存在!!"); | 
 |  |  |         } | 
 |  |  |         String join = StringUtils.join(param.getTypeIds(), ","); | 
 |  |  |  | 
 |  |  |         String[] split = warehouseAreas.getCode().split("\\."); | 
 |  |  |         if (split.length == 0){ | 
 |  |  |             throw new CoolException("库区编码错误!!"); | 
 |  |  |         } | 
 |  |  |         StringBuilder locStar = new StringBuilder(); | 
 |  |  |         for (int i = 0; i < split.length; i++) { | 
 |  |  |             locStar.append(split[i]).append("-"); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         List<Loc> list = new ArrayList<>(); | 
 |  |  |         Integer chanl = 0; //默认第一巷道 | 
 |  |  |         for (int r = param.getStartRow(); r <= param.getEndRow(); r++) { | 
 |  |  |             try{ | 
 |  |  |                 Shelves shelves = new Shelves(param.getEndRow() - param.getStartRow() + 1, param.getChannel(),param.getStartRow()); | 
 |  |  |                 for (List<Integer> node : shelves.nodes){ | 
 |  |  |                     if (node.contains(r)) { | 
 |  |  |                         if (!Cools.isEmpty(param.getStartChannel()) && param.getStartChannel() > 0){ | 
 |  |  |                             chanl = shelves.nodes.indexOf(node) + param.getStartChannel(); | 
 |  |  |                         }else { | 
 |  |  |                             chanl = shelves.nodes.indexOf(node) + 1; | 
 |  |  |                         } | 
 |  |  |                         break; | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |             }catch (Exception e){ | 
 |  |  |                 throw new CoolException("解析巷道号失败"); | 
 |  |  |             } | 
 |  |  |             for (int b = param.getStartBay(); b <= param.getEndBay(); b++) { | 
 |  |  |                 for (int l = param.getStartLev(); l <= param.getEndLev(); l++) { | 
 |  |  |                     // 获取库位号 | 
 |  |  |                     String locNo = String.format("%02d", r) + String.format("%03d", b) + String.format("%02d", l); | 
 |  |  |                     String locNo = locStar +  String.format("%d", r) + String.format("-%d", b) + String.format("-%d", l); | 
 |  |  |                     Loc loc = new Loc(); | 
 |  |  |                     loc.setCode(locNo) | 
 |  |  |                             .setUseStatus("O") | 
 |  |  |                             .setRow(r) | 
 |  |  |                             .setCol(b) | 
 |  |  |                             .setLev(l) | 
 |  |  |                             .setChannel(chanl) | 
 |  |  |                             .setType(join) | 
 |  |  |                             .setAreaId(param.getAreaId()) | 
 |  |  |                             .setWarehouseId(warehouseAreas.getWareId()) | 
 |  |  |                             .setType(param.getType()); | 
 |  |  |  | 
 |  |  |                             .setUpdateBy(loginUserId) | 
 |  |  |                             .setCreateBy(loginUserId) | 
 |  |  |                             .setWarehouseId(warehouseAreas.getWarehouseId()); | 
 |  |  |                     list.add(loc); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         if (!this.saveBatch(list)) { | 
 |  |  |             throw new CoolException("库位初始化失败!!"); | 
 |  |  |         } | 
 |  |  |         List<LocTypeRela> locTypeRelas = new ArrayList<>(); | 
 |  |  |         for (Long typeId : param.getTypeIds()) { | 
 |  |  |             for (Loc loc : list) { | 
 |  |  |                 LocTypeRela typeRela = new LocTypeRela(); | 
 |  |  |                 typeRela.setLocId(loc.getId()).setTypeId(typeId); | 
 |  |  |                 locTypeRelas.add(typeRela); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         if (!locTypeRelaService.saveBatch(locTypeRelas)) { | 
 |  |  |             throw new CoolException("库位类型关联失败!!"); | 
 |  |  |         } | 
 |  |  |         return R.ok("初始化成功!!"); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public boolean saveType(Loc loc) { | 
 |  |  |         List<LocTypeRela> relas =  new ArrayList<>(); | 
 |  |  |         loc.getTypeIds().forEach(id -> { | 
 |  |  |             LocTypeRela typeRela = new LocTypeRela(); | 
 |  |  |             typeRela.setTypeId(id) | 
 |  |  |                     .setLocId(loc.getId()); | 
 |  |  |             relas.add(typeRela); | 
 |  |  |         }); | 
 |  |  |         if (!locTypeRelaService.saveOrUpdateBatch(relas)) { | 
 |  |  |             throw new CoolException("库位关联关系保存失败!!"); | 
 |  |  |         } | 
 |  |  |         return true; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public boolean updateLoc(Loc loc) { | 
 |  |  |         locTypeRelaMapper.delete(new LambdaQueryWrapper<LocTypeRela>().eq(LocTypeRela::getLocId, loc.getId())); | 
 |  |  |         List<LocTypeRela> relas =  new ArrayList<>(); | 
 |  |  |         loc.getTypeIds().forEach(id -> { | 
 |  |  |             LocTypeRela typeRela = new LocTypeRela(); | 
 |  |  |             typeRela.setTypeId(id) | 
 |  |  |                     .setLocId(loc.getId()); | 
 |  |  |             relas.add(typeRela); | 
 |  |  |         }); | 
 |  |  |         if (!locTypeRelaService.saveOrUpdateBatch(relas)) { | 
 |  |  |             throw new CoolException("库位关联关系保存失败!!"); | 
 |  |  |         } | 
 |  |  |         return true; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public R batchBindLocs(LocModifyParams locs) { | 
 |  |  |         if (locs.getId().isEmpty()) { | 
 |  |  |             throw new CoolException("库位ID不能为空!!"); | 
 |  |  |         } | 
 |  |  |         if (Objects.isNull(locs.getLocAreaId())) { | 
 |  |  |             throw new CoolException("修改库位信息不能为空!!"); | 
 |  |  |         } | 
 |  |  |         List<LocArea> locAreas = locAreaService.list(new LambdaQueryWrapper<LocArea>().eq(LocArea::getId, locs.getLocAreaId())); | 
 |  |  |         if (locAreas.isEmpty()) { | 
 |  |  |             throw new CoolException("库位分区不存在!!"); | 
 |  |  |         } | 
 |  |  |         List<LocAreaRela> relas = new ArrayList<>(); | 
 |  |  |         locs.getId().forEach(id -> { | 
 |  |  |             LocAreaRela locAreaRela = new LocAreaRela(); | 
 |  |  |             locAreaRela.setLocId(id) | 
 |  |  |                     .setLocAreaId(locs.getLocAreaId()); | 
 |  |  |             relas.add(locAreaRela); | 
 |  |  |         }); | 
 |  |  |         if (!locAreaRelaService.saveBatch(relas)) { | 
 |  |  |             throw new CoolException("库位分区失败!!"); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         return R.ok(); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 获取库位使用情况 | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     @Override | 
 |  |  |     public R getLocPies() { | 
 |  |  |         List<LocUsageDto> locPies = this.baseMapper.getLocPies(); | 
 |  |  |         return R.ok().add(locPies); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * @author Ryan | 
 |  |  |      * @date 2025/8/16 | 
 |  |  |      * @description: 获取库位明细 | 
 |  |  |      * @version 1.0 | 
 |  |  |      */ | 
 |  |  |     @Override | 
 |  |  |     public IPage<LocStockDto> getLocDetls(Page<Object> page) { | 
 |  |  |         return this.baseMapper.getLocDetls(page); | 
 |  |  |     } | 
 |  |  | } |