LocationServiceImpl.java 16.6 KB
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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
package com.huaheng.pc.general.location.service;

import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.pc.config.locationType.domain.LocationType;
import com.huaheng.pc.config.locationType.service.ILocationTypeService;
import com.huaheng.pc.config.points.domain.Points;
import com.huaheng.pc.config.points.service.PointsService;
import com.huaheng.pc.config.zone.domain.Zone;
import com.huaheng.pc.config.zone.service.IZoneService;
import com.huaheng.pc.general.container.domain.Container;
import com.huaheng.pc.general.location.domain.Location;
import com.huaheng.pc.general.location.domain.LocationInfo;
import com.huaheng.pc.general.location.domain.LocationPosition;
import com.huaheng.pc.general.location.domain.LocationStatus;
import com.huaheng.pc.general.location.mapper.LocationMapper;
import com.huaheng.pc.general.location.mapper.LocationMapperAuto;
import com.huaheng.pc.general.material.domain.Material;
import com.huaheng.pc.general.material.service.IMaterialService;
import com.huaheng.pc.inventory.inventory.domain.Inventory;
import com.huaheng.pc.inventory.inventory.service.IInventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


/**
 * 库位 服务层实现
 * 
 * @author huaheng
 * @date 2018-08-19
 */
@Service
public class LocationServiceImpl implements ILocationService {

    @Resource
    private IZoneService zoneService;
    @Resource
    private IMaterialService materialService;
    @Resource
    private LocationMapperAuto aotuMapper;
    @Resource
    private LocationMapper locationMapper;
    @Resource
    private ILocationTypeService locationTypeService;
    @Resource
    private IInventoryService inventoryService;
    @Autowired
    private PointsService pointsService;


    public List<Location> selectListEntityByLike(Location condition) {
        return aotuMapper.selectListEntityByLike(condition);
    }

    public List<Location> selectListEntityByEqual(Location condition) {
        return aotuMapper.selectListEntityByEqual(condition);
    }

    public Location selectFirstEntity(Location condition) {
        Location item = aotuMapper.selectFirstEntity(condition);
        return item;
    }

    public Location selectEntityById(Integer id) {
        return aotuMapper.selectEntityById(id);
    }

    public List<Map<String, Object>> selectListMapByEqual(String columns, Location condition) {
        return aotuMapper.selectListMapByEqual(columns, condition);
    }

    public Map<String, Object> selectFirstMap(String columns, Location condition) {
        Map<String, Object> item = aotuMapper.selectFirstMap(columns, condition);
        return item;
    }

    public int insert(Location record) {
        return aotuMapper.insert(record);
    }

    public int updateByModel(Location record) {
        return aotuMapper.updateByModel(record);
    }

    public int updateByCondition(Location record, Location condition) {
        return aotuMapper.updateByCondition(record, condition);
    }

    public int deleteById(Integer id) {
        return aotuMapper.deleteById(id);
    }

    public int deleteByCondition(Location condition) {
        return aotuMapper.deleteByCondition(condition);
    }


    @Override
    public boolean insertLocation(Location location) {
        LocationType locationType = new LocationType();
        locationType.setPrefix(location.getType());
        List<Map<String, Object>> list = locationTypeService.selectListMapByEqual("prefix", locationType);
        if (list.size() < 1)
            throw new ServiceException("库位类型编码不存在");
        Zone zone = new Zone();
        zone.setCode(location.getZoneCode());
        list = zoneService.selectListMapByEqual("code", zone);
        if (list.size() < 1)
            throw new ServiceException("库区编码不存在");

        String code = MessageFormat.format("{0}{1}-{2}-{3}-{4}",
                                            location.getType(),
                                            String.format("%02d", location.getRow()),
                                            String.format("%02d", location.getLine()),
                                            String.format("%02d", location.getLayer()),
                                            String.format("%02d", location.getGrid()));
        Location locationCondition = new Location();
        locationCondition.setCode(code);
        if (this.selectFirstEntity(locationCondition) != null)   {
            return  true;
        }
        else
        {
            location.setCode(code);
            location.setWarehouseId(ShiroUtils.getWarehouseId());
            location.setWarehouseCode(ShiroUtils.getWarehouseCode());
            location.setStatus("empty");
            location.setCreatedBy(ShiroUtils.getLoginName());
            location.setLastUpdatedBy(ShiroUtils.getLoginName());
            Integer result = this.insert(location);
            return  result > 0;
        }
    }

    @Override
    public boolean updateLocation(Location location) {
        Inventory inventory = new Inventory();
        inventory.setLocationCode(location.getCode());
        if (inventoryService.selectFirstEntity(inventory) != null)
        {
            throw new ServiceException("货位号(" + location.getCode() + ")已经存在物料,不能删除");
        }
        else
        {
            Integer result = this.updateByModel(location);
            return  result > 0;
        }
    }

    @Override
    public Integer deleteLocation(Integer id)  {
        Location location = this.selectEntityById(id);
        if (location == null)
        {
            throw new ServiceException("库位不存在!");
        }
        if (location.getStatus() == "lock" || location.getStatus() == "occupy")
        {
            throw new ServiceException("库位(" + location.getCode() + ")状态是预定或者占用,不能删除!");
        }
        Inventory inventory = new Inventory();
        inventory.setLocationCode(location.getCode());
        if (inventoryService.selectFirstEntity(inventory) != null)
        {
            throw new ServiceException("货位号(" + location.getCode() + ")已经存在物料,不能删除");
        }
        return  this.deleteById(id);
    }

    @Override
    public boolean checkLocation(String code) {
        Location condition = new Location();
        condition.setWarehouseId(ShiroUtils.getWarehouseId());
        condition.setEnable(true);
        condition.setDeleted(false);
        condition.setCode(code);
        List<Location> locations=selectListEntityByEqual(condition);
        if(locations.size() >= 1){
            Location location = locations.get(0);
            String containerCode = location.getContainerCode();
            if(containerCode != null && containerCode.length() > 0) {
                throw new ServiceException("货架上已有容器");
            }
            String status = location.getStatus();
            if(!status.equals("empty")) {
                throw new ServiceException("库位状态不为空");
            }
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void updateStatus(String locationCode, String status) {
        if (StringUtils.isNotEmpty(locationCode)) {
//            System.out.println("ShiroUtils.getWarehouseCode():" + ShiroUtils.getWarehouseCode()+ "  locationCode:" + locationCode + " status:" + status);
            locationMapper.updateStatus(ShiroUtils.getWarehouseCode(), locationCode, status);
        }
    }

    @Override
    public void updateContainerCodeAndStatus(String locationCode, String containerCode, String status) {
        if (StringUtils.isNotEmpty(locationCode))
            locationMapper.updateContainerCodeAndStatus(ShiroUtils.getWarehouseCode(), locationCode, containerCode, status);
    }

    @Override
    public void updateContainerCode(String locationCode, String containerCode) {
        if (StringUtils.isNotEmpty(locationCode))
            locationMapper.updateContainerCode(ShiroUtils.getWarehouseCode(), locationCode, containerCode);
    }

    @Override
    public LocationInfo getAllLocation(String type) {
        if (StringUtils.isNotEmpty(type)) {
            Location location = locationMapper.getAllLocation(ShiroUtils.getWarehouseCode(), type);
            LocationInfo locationInfo = new LocationInfo();
            locationInfo.setMaxRow(location.getRow());
            locationInfo.setMaxLine(location.getLine());
            locationInfo.setMaxLayer(location.getLayer());
            locationInfo.setMaxGrid(location.getGrid());
            return locationInfo;
        }
        return null;
    }

    @Override
    public LocationInfo getAllLocations(String type,String warehouseCode) {
        if (StringUtils.isNotEmpty(type)) {
            if(StringUtils.isEmpty(warehouseCode)){
                warehouseCode="CS0001";
            }
            Location location = locationMapper.getAllLocation(warehouseCode, type);
            LocationInfo locationInfo = new LocationInfo();
            locationInfo.setMaxRow(location.getRow());
            locationInfo.setMaxLine(location.getLine());
            locationInfo.setMaxLayer(location.getLayer());
            locationInfo.setMaxGrid(location.getGrid());
            return locationInfo;
        }
        return null;
    }

    @Override
    public List<Location> getLocationInfo(String type, String row, String line, String layer, String grid,String warehouseCode) {
        Location location = new Location();
        location.setType(type);
        if(StringUtils.isEmpty(warehouseCode)) {
            location.setWarehouseCode(ShiroUtils.getWarehouseCode());
        }else {
            location.setWarehouseCode(warehouseCode);
        }
        if (StringUtils.isNotEmpty(row)) {
            location.setRow(Integer.parseInt(row));
        }
        if (StringUtils.isNotEmpty(line)) {
            location.setLine(Integer.parseInt(line));
        }
        if (StringUtils.isNotEmpty(layer)) {
            location.setLayer(Integer.parseInt(layer));
        }
        if (StringUtils.isNotEmpty(grid)) {
            location.setGrid(Integer.parseInt(grid));
        }
        List<Location> locations = selectListEntityByEqual(location);
        List<Location> locationList = new ArrayList<>();
        Inventory condition = new Inventory();
        condition.setDeleted(false);
        if(StringUtils.isEmpty(warehouseCode)) {
            condition.setWarehouseCode(ShiroUtils.getWarehouseCode());
        }else {
            condition.setWarehouseCode(warehouseCode);
        }
        List<Inventory> inventoryList = inventoryService.selectListEntityByEqual(condition);
        List<String> materials= new ArrayList<>();
        for(Location location1 : locations) {
            for(Inventory inventory1 :inventoryList)  {
                if(location1.getCode().equals(inventory1.getLocationCode())) {
                    materials.add(inventory1.getMaterialCode());
                }
            }
        }
        String[] str = materials.toArray(new String[materials.size()]);
        List<Material>  materialList = null;
        if(str != null && str.length > 0) {
            materialList = materialService.selectNameByListcode(str);
        }

        if(str != null && str.length > 0) {
            List<Material>  materials1 = materialService.selectNameByListUserdef1(str);
            materialList.addAll(materials1);
        }

        for(Location location1 : locations) {
            List<Inventory> inventorys = null;
            List<String> materialCodes = new ArrayList<>();
            List<String> materialNames = new ArrayList<>();
            List<String> materialBatch = new ArrayList<>();
            List<String> materialQty = new ArrayList<>();
            for(Inventory inventory1 :inventoryList)  {
                if(location1.getCode().equals(inventory1.getLocationCode())) {
                    if(inventorys == null) {
                        inventorys = new ArrayList<>();
                    }
                    inventorys .add(inventory1);
                    materialCodes.add(inventory1.getMaterialCode());
                    for (Material material1 : materialList) {
                        if (material1.getCode().equals(inventory1.getMaterialCode()) || material1.getUserDef1().equals(inventory1.getMaterialCode())){
                            materialNames.add(material1.getName());
                            break;
                        }
                    }
                    materialBatch.add(inventory1.getBatch());
                    materialQty.add(String.valueOf(inventory1.getQty()));
                }
            }
            int userDef3 = 0;
            String status = location1.getStatus();
            String containerCode = location1.getContainerCode();
            boolean enable = location1.getEnable();
            if(status.equals("empty")) {
                if(containerCode.equals("")) {
                    userDef3 = LocationStatus.IDLE_EMPTY_LOCATION;
                } else {
                    if(inventorys == null) {
                        userDef3 = LocationStatus.IDLE_EMPTY_CONTAINER;
                    } else {
                        location1.setMaterialName(materialNames);
                        location1.setMaterialCode(materialCodes);
                        location1.setBatch(materialBatch);
                        location1.setQty(materialQty);
                        userDef3 = LocationStatus.IDLE_FULL_CONTAINER;
                    }
                }
            } else if(status.equals("lock")) {
                if(containerCode.equals("")) {
                    userDef3 = LocationStatus.LOCK_EMPTY_LOCATION;
                } else {
                    if(inventorys == null) {
                        userDef3 = LocationStatus.LOCK_EMPTY_CONTAINER;
                    } else {
                        location1.setMaterialName(materialNames);
                        location1.setMaterialCode(materialCodes);
                        location1.setBatch(materialBatch);
                        location1.setQty(materialQty);
                        userDef3 = LocationStatus.IDLE_FULL_CONTAINER;
                    }
                }
            }
            if(!enable) {
                if(containerCode.equals("")) {
                    userDef3 = LocationStatus.DISABLE_EMPTY_LOCATION;
                } else {
                    if(inventorys == null) {
                        userDef3 = LocationStatus.DISABLE_EMPTY_CONTAINER;
                    } else {
                        location1.setMaterialName(materialNames);
                        location1.setMaterialCode(materialCodes);
                        location1.setBatch(materialBatch);
                        location1.setQty(materialQty);
                        userDef3 = LocationStatus.DISABLE_FULL_CONTAINER;
                    }
                }
            }
            location1.setUserDef3(String.valueOf(userDef3));
            locationList.add(location1);
        }
        return locationList;
    }

    @Override
    public Location selectSSPLocation() {
        return locationMapper.selectSSPLocation();
    }

    @Override
    public List<String> getAllLocationCode(Location location) {
        List<String> list = locationMapper.getAllLocationCode(location);
        return list;
    }

    @Override
    public Location selectAddress(Location location) {
        return locationMapper.selectAddress(location);

    }

//    @Override
//    public List<Map<String, Object>> selectIntY() {
//        return locationMapper.selectIntX();
//    }

    @Override
    public LocationPosition selectLocationPosition(LocationPosition locationPosition) {
        return locationMapper.selectLocationPosition(locationPosition);
    }

    @Override
    public int updateContainer(Container container, LocationPosition locationPosition) {
        return locationMapper.updateContainer(container,locationPosition);
    }

    @Override
    public List<Location> selectEmptyAddress() {
        Points points=new Points();
        points.setWarehouseCode(ShiroUtils.getWarehouseCode());
        points.setIsLocked(2);
        return locationMapper.selectEmptyAddress(points);
    }

    @Override
    public List<LinkedHashMap<String, Object>> getLocationProp() {
        return locationMapper.getLocationProp();
    }

//    @Override
//    public List<LocationPosition> selectList(LocationPosition locationPosition) {
//        return locationMapper.selectList(locationPosition);
//    }


}