AddressServiceImpl.java 5.18 KB
package com.huaheng.pc.config.address.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.huaheng.common.utils.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaheng.common.constant.QuantityConstant;
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.address.domain.Address;
import com.huaheng.pc.config.address.mapper.AddressMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {

    @Autowired
    private AddressMapper addressMapper;

    @Override
    public String selectAddress(String param, String warehouseCode) {
        return selectAddress(param,warehouseCode,null);
    }

    @Override
    public String selectAddress(String param, String warehouseCode, String area) {
        if(StringUtils.isEmpty(param)){
            throw new ServiceException("参数为空");
        }

        LambdaQueryWrapper<Address> addressLam = Wrappers.lambdaQuery();
        addressLam.eq(Address::getParam,param)
                .eq(Address::getWarehouseCode, warehouseCode)
                .eq(StringUtils.isNotEmpty(area),Address::getNumber, area);
        Address address=this.getOne(addressLam);
        if(address == null){
            throw new ServiceException("参数错误,系统没有此参数对应的地址");
        }
        if(StringUtils.isEmpty(address.getUrl())){
            throw new ServiceException("地址为空");
        }
        return address.getUrl();
    }

    @Override
    public String selectAddress(String param, String warehouseCode, String area, Integer roadway) {
        if(StringUtils.isEmpty(param)){
            throw new ServiceException("参数为空");
        }

        LambdaQueryWrapper<Address> addressLam = Wrappers.lambdaQuery();
        addressLam.eq(Address::getParam,param)
                .eq(Address::getWarehouseCode, warehouseCode)
                .eq(Address::getArea, area)
                .eq(Address::getRoadway, roadway);
        Address address=this.getOne(addressLam);
        if(address == null){
            throw new ServiceException("参数错误,系统没有此参数对应的地址");
        }
        if(StringUtils.isEmpty(address.getUrl())){
            throw new ServiceException("地址为空");
        }
        return address.getUrl();
    }

    @Override
    public String selectAddress(String param) {
        if(StringUtils.isEmpty(param)){
            throw new ServiceException("参数为空");
        }

        LambdaQueryWrapper<Address> addressLam = Wrappers.lambdaQuery();
        addressLam.eq(Address::getParam,param)
                .eq(Address::getWarehouseCode, ShiroUtils.getWarehouseCode());
        Address address=this.getOne(addressLam);
        if(address == null){
            throw new ServiceException("参数错误,系统没有此参数对应的地址");
        }
        if(StringUtils.isEmpty(address.getUrl())){
            throw new ServiceException("地址为空");
        }
        return address.getUrl();
    }

    /**
     * 复制地址表
     * @param warehouseCode 原仓库编码
     * @param newWarehouseCode 新仓库编码
     * @return 是否复制成功
     */
    @Override
    @Transactional
    public Boolean addressCopy(String warehouseCode, String newWarehouseCode) {
        log.trace("开始复制地址表");
        LambdaQueryWrapper<Address> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(Address::getWarehouseCode, newWarehouseCode);
         if (!this.list(lambdaQueryWrapper).isEmpty()){
            log.error("该仓库已存在");
            return false;
        }
        lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(Address::getWarehouseCode, warehouseCode);
        List<Address> addressList = this.list(lambdaQueryWrapper);

        for (Address address : addressList) {
            address.setId(null);
            address.setWarehouseCode(newWarehouseCode);
        }

        if ( this.saveBatch(addressList) ){
            log.trace("复制地址表成功,新仓库编码是:"+newWarehouseCode);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Address getAddressByUrl(String url, String warehouseCode) {
        LambdaQueryWrapper<Address> addressLam = Wrappers.lambdaQuery();
        addressLam.eq(Address::getUrl,url);
        if(StringUtils.isEmpty(warehouseCode)) {
            addressLam.eq(Address::getWarehouseCode, QuantityConstant.ryTask_warehouse_code);
        } else {
            addressLam.eq(Address::getWarehouseCode, warehouseCode);
        }
        Address address=this.getOne(addressLam);
        return address;
    }


    @Override
    public Address selectEntity(Address address) {
        return addressMapper.selectEntity(address);
    }

}