LocationServiceImpl.java 4.77 KB
package com.huaheng.pc.config.location.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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.LocationTypeService;
import com.huaheng.pc.config.zone.domain.Zone;
import com.huaheng.pc.config.zone.service.ZoneService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.pc.config.location.domain.Location;
import com.huaheng.pc.config.location.mapper.LocationMapper;

@Service("LocationService")
public class LocationServiceImpl extends ServiceImpl<LocationMapper, Location> implements LocationService{

    @Resource
    private LocationService locationService;
    @Resource
    private LocationTypeService locationTypeService;
    @Resource
    private ZoneService zoneService;
    @Resource
    private LocationMapper locationMapper;

    @Override
    public boolean insertLocation(Location location) {
        /* 判断库位类型编码是否存在*/
        LambdaQueryWrapper<LocationType> typelambda = Wrappers.lambdaQuery();
        typelambda.eq(LocationType::getCode,location.getLocationType())
                        .select(LocationType::getCode);
        List<Map<String, Object>> list = locationTypeService.listMaps(typelambda);
        if (list.size() < 1){
            throw new ServiceException("库位类型编码不存在");
        }

        /* 判断库区编码是否存在*/
        LambdaQueryWrapper<Zone> zoneLambda= Wrappers.lambdaQuery();
        zoneLambda.eq(Zone::getCode, location.getZoneCode())
                .select(Zone::getCode);

        list = zoneService.listMaps(zoneLambda);
        if (list.size() < 1){
            throw new ServiceException("库区编码不存在");
        }

        String prefix = location.getLocationType().substring(1);
        List<Location> locations = new ArrayList<>();
        for (int i=1; i<=location.getIRow().intValue(); i++)  {
            for (int j=1; j<=location.getIColumn().intValue(); j++)  {
                for (int k=1; k<=location.getILayer().intValue(); k++)  {
                    for (int m=1; m<=location.getIGrid().intValue(); m++)  {
                        //Integer roadway = (j / 2) + 1;
                        Location param = new Location();
                        param.setIRow(i);
                        param.setIColumn(j);
                        param.setILayer(k);
                        param.setIGrid(m);
                        param.setZoneCode(location.getZoneCode());
                        param.setLocationType(location.getLocationType());
                        param.setStatus(location.getStatus());
                        param.setWarehouseCode(ShiroUtils.getWarehouseCode());
                        param.setCreatedBy(ShiroUtils.getLoginName());
                        param.setLastUpdatedBy(ShiroUtils.getLoginName());
                        String code = MessageFormat.format("{0}{1}-{2}-{3}-{4}",
                                prefix,
                                String.format("%02d", i),
                                String.format("%02d", j),
                                String.format("%02d", k),
                                String.format("%02d", m));
                        QueryWrapper<Location> queryWrapper = new QueryWrapper<>();
                        List<Location> locationList = locationService.list(queryWrapper.eq("code", code));
                        if (locationList.size() != 0){
                            return true;
                        } else {
                            param.setCode(code);
                            location.setWarehouseCode(ShiroUtils.getWarehouseCode());
                            location.setCreatedBy(ShiroUtils.getLoginName());
                            location.setLastUpdatedBy(ShiroUtils.getLoginName());
                        }
                        param.setCode(code);
                        locations.add(param);
                    }
                }
            }
        }
        Boolean result = locationService.saveBatch(locations);
        return result;
    }

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