package com.zy.asrs.service.impl; 
 | 
  
 | 
import com.baomidou.mybatisplus.mapper.EntityWrapper; 
 | 
import com.baomidou.mybatisplus.service.impl.ServiceImpl; 
 | 
import com.core.common.Cools; 
 | 
import com.core.common.R; 
 | 
import com.core.common.SnowflakeIdWorker; 
 | 
import com.core.exception.CoolException; 
 | 
import com.zy.asrs.entity.ManLocDetl; 
 | 
import com.zy.asrs.entity.Mat; 
 | 
import com.zy.asrs.entity.Node; 
 | 
import com.zy.asrs.entity.param.InitPakoutParam; 
 | 
import com.zy.asrs.entity.param.MatnrDto; 
 | 
import com.zy.asrs.entity.param.PakinParam; 
 | 
import com.zy.asrs.mapper.NodeMapper; 
 | 
import com.zy.asrs.service.ManLocDetlService; 
 | 
import com.zy.asrs.service.MatService; 
 | 
import com.zy.asrs.service.NodeService; 
 | 
import com.zy.asrs.utils.SaasUtils; 
 | 
import org.springframework.beans.factory.annotation.Autowired; 
 | 
import org.springframework.stereotype.Service; 
 | 
import org.springframework.transaction.annotation.Transactional; 
 | 
  
 | 
import java.util.Date; 
 | 
import java.util.List; 
 | 
  
 | 
@Service("nodeService") 
 | 
public class NodeServiceImpl extends ServiceImpl<NodeMapper, Node> implements NodeService { 
 | 
    @Autowired 
 | 
    private NodeService nodeService; 
 | 
    @Autowired 
 | 
    private MatService matService; 
 | 
    @Autowired 
 | 
    private SnowflakeIdWorker snowflakeIdWorker; 
 | 
    @Autowired 
 | 
    private ManLocDetlService manLocDetlService; 
 | 
  
 | 
    @Override 
 | 
    public Node getTop() { 
 | 
        Node top = this.selectOne(new EntityWrapper<Node>().eq("type", 0).eq("level", 0)); 
 | 
        if (top == null) { 
 | 
            top = new Node(); 
 | 
            top.setName("全部"); 
 | 
            top.setUuid("全部"); 
 | 
            top.setType(0); 
 | 
            top.setLevel(0); 
 | 
            top.setSort(0); 
 | 
            top.setStatus(1); 
 | 
            top.setCreateTime(new Date()); 
 | 
            top.setUpdateTime(new Date()); 
 | 
            Integer insert = this.baseMapper.insert(top); 
 | 
            if (insert == 0) { 
 | 
                throw new CoolException("服务器异常"); 
 | 
            } 
 | 
        } 
 | 
        return top; 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public Node selectByUuid(String uuid) { 
 | 
        return selectOne(new EntityWrapper<Node>().eq("uuid", uuid)); 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public Node selectByUuid(String uuid, Long hostId) { 
 | 
        return this.baseMapper.selectByUuid(uuid, hostId); 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public Node selectByUuid(String uuid, Long hostId, Integer type) { 
 | 
        return selectOne(new EntityWrapper<Node>().eq("host_id", hostId).eq("uuid", uuid).eq("type", type)); 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public Node selectByUuid(String uuid, Long hostId, Integer type, Long parentId) { 
 | 
        return selectOne(new EntityWrapper<Node>().eq("host_id", hostId).eq("uuid", uuid).eq("type", type).eq("parent_id", parentId)); 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    public R stockPakin(PakinParam param, Long userId, Long hostId) { 
 | 
        Node node = nodeService.selectByUuid(param.getNodeId(), hostId); 
 | 
        if (node == null) { 
 | 
            node = nodeService.selectById(param.getNodeId()); 
 | 
        } 
 | 
        if (node == null) { 
 | 
            return R.error("货位不存在"); 
 | 
        } 
 | 
        if (Cools.isEmpty(param.getMats())) { 
 | 
            return R.error("入库物料不能为空"); 
 | 
        } 
 | 
        Date now = new Date(); 
 | 
        for (MatnrDto dto : param.getMats()) { 
 | 
  
 | 
            Mat mat = matService.selectByMatnr(dto.getMatnr()); 
 | 
            if (mat == null) { 
 | 
                throw new CoolException("物料数据错误,请联系管理员"); 
 | 
            } 
 | 
            ManLocDetl check = manLocDetlService.selectOne(new EntityWrapper<ManLocDetl>() 
 | 
                    .eq("loc_no", node.getUuid()) 
 | 
                    .eq("matnr", dto.getMatnr())); 
 | 
            if (check == null) { 
 | 
                ManLocDetl manLocDetl = new ManLocDetl(); 
 | 
                manLocDetl.setLocNo(node.getUuid()); 
 | 
                manLocDetl.setNodeId(node.getId()); 
 | 
                manLocDetl.setZpallet(mat.getBarcode()); 
 | 
                manLocDetl.setAnfme(dto.getCount()); 
 | 
                manLocDetl.setMatnr(mat.getMatnr()); 
 | 
                manLocDetl.setMaktx(mat.getMaktx()); 
 | 
                manLocDetl.setName(mat.getName()); 
 | 
                manLocDetl.setBatch(null); 
 | 
                manLocDetl.setSpecs(mat.getSpecs()); 
 | 
                manLocDetl.setModel(mat.getModel()); 
 | 
                manLocDetl.setCreateTime(now); 
 | 
                manLocDetl.setModiTime(now); 
 | 
                manLocDetl.setCreateBy(userId); 
 | 
                SaasUtils.insertLog(0, manLocDetl.getLocNo(), manLocDetl.getMatnr(), manLocDetl.getAnfme()); 
 | 
                manLocDetlService.insert(manLocDetl); 
 | 
            } else { 
 | 
                check.setAnfme(dto.getCount() + check.getAnfme()); 
 | 
                manLocDetlService.update(check, new EntityWrapper<ManLocDetl>() 
 | 
                        .eq("loc_no", node.getUuid()) 
 | 
                        .eq("matnr", dto.getMatnr())); 
 | 
            } 
 | 
  
 | 
  
 | 
        } 
 | 
        return R.ok("入库成功"); 
 | 
    } 
 | 
  
 | 
    @Transactional 
 | 
    @Override 
 | 
    public R initPakout(List<InitPakoutParam> params, Long userId, Long hostId) { 
 | 
        if (!Cools.isEmpty(params)) { 
 | 
            Date now = new Date(); 
 | 
            for (InitPakoutParam param : params) { 
 | 
                ManLocDetl manLocDetl = manLocDetlService.selectOne(new EntityWrapper<ManLocDetl>() 
 | 
                        .eq("node_id", param.getNodeId()) 
 | 
                        .eq("matnr", param.getMatnr())); 
 | 
                Node node = nodeService.selectOne(new EntityWrapper<Node>() 
 | 
                        .eq("id", param.getNodeId())); 
 | 
                if (node == null) { 
 | 
                    return R.error("找不到该库位,请联系管理员:" + param.getNodeId()); 
 | 
                } 
 | 
                if (manLocDetl == null) { 
 | 
                    return R.error("物料:" + param.getMatnr() + " 在库位中不存在"); 
 | 
                } 
 | 
                if (manLocDetl.getAnfme() - param.getCount() < 0) { 
 | 
                    return R.error("物料:" + param.getMatnr() + " 在库位中数量不足"); 
 | 
                } else if (manLocDetl.getAnfme() - param.getCount() == 0) { 
 | 
                    SaasUtils.insertLog(1, manLocDetl.getLocNo(), manLocDetl.getMatnr(), param.getCount()); 
 | 
                    manLocDetlService.delete(new EntityWrapper<ManLocDetl>() 
 | 
                            .eq("loc_no", node.getUuid()) 
 | 
                            .eq("matnr", param.getMatnr())); 
 | 
                } else { 
 | 
  
 | 
                    manLocDetl.setAnfme(manLocDetl.getAnfme() - param.getCount()); 
 | 
                    manLocDetlService.update(manLocDetl, new EntityWrapper<ManLocDetl>() 
 | 
                            .eq("loc_no", node.getUuid()) 
 | 
                            .eq("matnr", param.getMatnr())); 
 | 
                    SaasUtils.insertLog(1, manLocDetl.getLocNo(), manLocDetl.getMatnr(), param.getCount()); 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
        return R.ok("出库成功"); 
 | 
    } 
 | 
  
 | 
    @Transactional 
 | 
    @Override 
 | 
    public void locMove(String sourceLocNo, String targetLocNo, Long userId) { 
 | 
        List<ManLocDetl> sourceManDetl = manLocDetlService.selectList(new EntityWrapper<ManLocDetl>() 
 | 
                .like("loc_no", sourceLocNo)); 
 | 
        for (ManLocDetl source : sourceManDetl) { 
 | 
            Node targetNode = nodeService.selectOne(new EntityWrapper<Node>() 
 | 
                    .eq("uuid", targetLocNo)); 
 | 
            Node sourceNode = nodeService.selectOne(new EntityWrapper<Node>() 
 | 
                    .eq("uuid", sourceLocNo)); 
 | 
            if (targetNode == null || targetNode.equals("")) { 
 | 
                throw new RuntimeException("无法查询到移库的目标库位"); 
 | 
            } 
 | 
            ManLocDetl check = manLocDetlService.selectOne(new EntityWrapper<ManLocDetl>() 
 | 
                    .eq("loc_no", targetNode.getUuid()) 
 | 
                    .eq("matnr", source.getMatnr())); 
 | 
            if (check == null) { 
 | 
                manLocDetlService.deleteById(source); 
 | 
                source.setLocNo(targetNode.getUuid()); 
 | 
                source.setNodeId(targetNode.getId()); 
 | 
  
 | 
                SaasUtils.insertLog(2, source.getLocNo(), source.getMatnr(), source.getAnfme()); 
 | 
                manLocDetlService.insert(source); 
 | 
            } else { 
 | 
                check.setAnfme(check.getAnfme() + source.getAnfme()); 
 | 
                manLocDetlService.update(check, new EntityWrapper<ManLocDetl>() 
 | 
                        .eq("loc_no", targetNode.getUuid()) 
 | 
                        .eq("matnr", check.getMatnr())); 
 | 
                manLocDetlService.delete(new EntityWrapper<ManLocDetl>() 
 | 
                        .eq("loc_no", sourceNode.getUuid()) 
 | 
                        .eq("matnr", source.getMatnr())); 
 | 
            } 
 | 
  
 | 
        } 
 | 
    } 
 | 
} 
 |