ContainerServiceImpl.java 11.9 KB
package com.huaheng.pc.config.container.service;

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.BusinessException;
import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.config.container.domain.Container;
import com.huaheng.pc.config.container.domain.ContainerStatus;
import com.huaheng.pc.config.container.mapper.ContainerMapper;
import com.huaheng.pc.config.containerType.service.ContainerTypeService;
import com.huaheng.pc.config.location.domain.Location;
import com.huaheng.pc.config.location.service.LocationService;
import com.huaheng.pc.receipt.receiptContainerHeader.domain.ReceiptContainerView;
import com.huaheng.pc.task.taskHeader.domain.TaskHeader;
import com.huaheng.pc.task.taskHeader.service.TaskHeaderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ContainerServiceImpl extends ServiceImpl<ContainerMapper, Container> implements ContainerService{

    @Resource
    private ContainerTypeService containerTypeService;
    @Resource
    private ContainerMapper containerMapper;
    @Resource
    private LocationService locationService;
    @Resource
    private TaskHeaderService taskHeaderService;

    @Override
    public AjaxResult<List<Container>> insertContainer(String type, Integer quantity) {
        List<Container>  containerList = new ArrayList<>();
        List<Container> containers =new ArrayList<>();
        Integer number = getNumber(type);
        for(int i=0; i<quantity; i++) {
            number++;
            Container container = new Container();
            container.setContainerType(type);
            container.setCode(String.format("%s%05d", type, number));
            container.setStatus(ContainerStatus.empty.name());
            container.setCreated(new Date());
            container.setCreatedBy(ShiroUtils.getLoginName());
            container.setLastUpdated(null);
            container.setLastUpdatedBy(null);
            container.setEnable(true);
            //container.setCompanyCode(ShiroUtils.getCompanyCodeList().get(0));
            container.setWarehouseCode(ShiroUtils.getWarehouseCode());
            containers.add(container);
            if( i>0 && (i%1000==0 || i == quantity-1)){
                containerMapper.addList(containers);
                containers = new ArrayList<>();
            }
        }
        return AjaxResult.success(containers);
    }

    private Integer getNumber(String type) {
        if (!containerTypeService.checkConfig(type)) {
            throw new ServiceException("容器类型编码不存在");
        }
        LambdaQueryWrapper<Container> lambda = Wrappers.lambdaQuery();
        lambda.select(Container::getCode).eq(Container::getContainerType, type)
                .eq(Container::getWarehouseCode, ShiroUtils.getWarehouseCode())
                .orderByDesc(Container::getId).last("Limit 1");
        Container container = containerMapper.selectOne(lambda);

        //如果指定类型的最后的code存在,那么 code = 容器类型 + (排序号 + 1)
        if (container != null && container.getCode() != null) {
            Integer number = Integer.valueOf(container.getCode().substring(container.getCode().length() - 5, container.getCode().length()));
            return  number;
        } else {
            return  0;
        }
    }

    @Override
    public String importContainer(List<Container> containerList, Boolean updateSupport, String operName){
        if (StringUtils.isNull(containerList) || containerList.size() == 0) {
            throw new  BusinessException("导入数据不能为空!");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Container container : containerList) {
            try {
                LambdaQueryWrapper<Container> lambdaQueryWrapper = Wrappers.lambdaQuery();
                lambdaQueryWrapper.eq(Container::getCode, container.getCode());
                // 验证是否存在这个容器
                Container m = this.getOne(lambdaQueryWrapper);

                if (StringUtils.isNull(m)) {
//                    System.out.println(ShiroUtils.getLoginName());
                    container.setCreatedBy(ShiroUtils.getUser().getLoginName());
                    container.setLastUpdatedBy(ShiroUtils.getLoginName());
                    this.save(container);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、编码 " + container.getCode() + " 导入成功");
                } else {
                    String msg = "<br/>" + failureNum + "、编码" + container.getCode() + " 已存在:";
                    failureMsg.append(msg);
                    log.error(msg);
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、编码" + container.getCode() + " 导入失败:";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
            throw new BusinessException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
        }
        return successMsg.toString();
    }

    /**
     * 已入空容器展示
     * */
    @Override
    public List<Location> getEmptyContainerInLocation(String zoneCode, String containerCode, String locationCode,String locationType, String warehouseCode) {
        return containerMapper.getEmptyContainerInLocation(zoneCode, containerCode,locationCode,locationType,warehouseCode);
    }

    /**
     * 修改容器库位和状态
     * */
    @Override
    public void updateLocationCodeAndStatus(String containerCode, String locationCode, String status) {
        if (StringUtils.isNotEmpty(containerCode) || StringUtils.isNotEmpty(locationCode)) {
            containerMapper.updateLocationCodeAndStatus(ShiroUtils.getWarehouseCode(), containerCode, locationCode, status);
        }
    }

    @Override
    public void updateLocationCodeAndStatus(String containerCode, String locationCode, String status, String warehouseCode) {
        if (StringUtils.isNotEmpty(containerCode) || StringUtils.isNotEmpty(locationCode)) {
            containerMapper.updateLocationCodeAndStatus(warehouseCode, containerCode, locationCode, status);
        }
    }

    @Override
    public boolean updateStatus(String containerCode, String status, String warehouseCode) {
        LambdaQueryWrapper<Container> containerLambdaQueryWrapper = Wrappers.lambdaQuery();
        containerLambdaQueryWrapper.eq(Container::getCode, containerCode)
                                    .eq(Container::getWarehouseCode, warehouseCode);
        Container container = getOne(containerLambdaQueryWrapper);
        container.setStatus(status);
        boolean result =  update(container, containerLambdaQueryWrapper);
        return result;
    }

    /**
     * 如果为临时容器,在取消组盘和出库任务完成时删除容器
     * @param containerType 容器类型
     * @param containerCode 容器编码
     * @return
     */
    @Override
    @Transactional
    public void removeContainer(String containerType, String containerCode) {
        if ("LS".equals(containerType)) {
            LambdaQueryWrapper<Container> lambdaQueryWrapper = Wrappers.lambdaQuery();
            lambdaQueryWrapper.eq(Container::getCode,containerCode);
            if (!this.remove(lambdaQueryWrapper)){
                throw new ServiceException("删除临时容器失败");
            }
        }
    }

    @Override
    public Container getContainerByCode(String code) {
        return containerMapper.findAllByCode(code, ShiroUtils.getWarehouseCode());
    }

    @Override
    public Container getContainerByCode(String containCode, String warehouseCode) {
        return containerMapper.findAllByCode(containCode, warehouseCode);
    }

    @Override
    public Container findContainerByCodeType(String containerCode, String containerType, String warehouseCode) {
        Container one = this.getOne(new LambdaQueryWrapper<Container>()
                .eq(Container::getCode, containerCode)
                .eq(Container::getWarehouseCode, warehouseCode)
                .eq(Container::getContainerType, containerType));
        return one;
    }


    @Override
    public List<Container> selectListShelf(String warehouseCode ) {
        return containerMapper.selectListShelf(warehouseCode);
    }


    @Override
    public Container emptyContainer(String warehouseCode ) {
        return containerMapper.emptyContainer(warehouseCode);
    }

    @Override
    public List<Container> selectEmptyList(Container container) {
        return containerMapper.selectEmptyList(container);
    }
    @Override
    public List<Container> selectListEntityByEqual(Container condition) {
        LambdaQueryWrapper<Container> lambd = Wrappers.lambdaQuery();
        lambd.eq(StringUtils.isNotEmpty(condition.getCode()),Container::getCode, condition.getCode())
                .eq(StringUtils.isNotEmpty(condition.getWarehouseCode()),Container::getWarehouseCode, condition.getWarehouseCode())
                .eq(StringUtils.isNotEmpty(condition.getGoodsShelfNo()),Container::getGoodsShelfNo, condition.getGoodsShelfNo())
                .eq(StringUtils.isNotEmpty(condition.getContainerType()),Container::getContainerType, condition.getContainerType());
        return this.list(lambd);
    }

    @Override
    public Container selectFirstEntity(Container condition) {
        LambdaQueryWrapper<Container> lambd = Wrappers.lambdaQuery();
        lambd.eq(StringUtils.isNotEmpty(condition.getCode()),Container::getCode, condition.getCode())
                .eq(StringUtils.isNotEmpty(condition.getWarehouseCode()),Container::getWarehouseCode, condition.getWarehouseCode())
                .eq(StringUtils.isNotEmpty(condition.getContainerType()),Container::getContainerType, condition.getContainerType());
        lambd.orderByDesc(Container::getCode);
        lambd.last("limit 1");
        return this.getOne(lambd);
    }

    @Override
    public ReceiptContainerView check(ReceiptContainerView receiptContainerView, Container container) {
        if(container.getContainerType().equals("M")) {
            if(StringUtils.isEmpty(container.getLocationCode())) {
                receiptContainerView.setTaskType((short) 100);
            }else {
                receiptContainerView.setTaskType((short) 200);
            }
            receiptContainerView.setZoneCode("LK");
        }
        if(container.getContainerType().equals("A")){
            receiptContainerView.setZoneCode("AGV");
            receiptContainerView.setTaskType((short) 200);
        }
        if(container.getContainerType().equals("D")){
            receiptContainerView.setZoneCode("PK");
            receiptContainerView.setTaskType((short) 200);
        }
        if(container.getContainerType().equals("Q")){
            receiptContainerView.setZoneCode("QG");
            receiptContainerView.setTaskType((short) 200);
        }
        if(container.getContainerType().equals("X")){
            receiptContainerView.setZoneCode("XN");
            receiptContainerView.setTaskType((short) 200);
        }
        return receiptContainerView;
    }

}