Fancy
2024-07-15 2db191d2c36c490f27a9e985005ef8cf4df261ee
pms-parent/pms-web-station/src/main/java/com/dy/pmsStation/workOrder/WorkOrderSv.java
@@ -1,58 +1,405 @@
package com.dy.pmsStation.workOrder;
import com.alibaba.excel.util.StringUtils;
import com.dy.common.webUtil.BaseResponseUtils;
import com.dy.pmsGlobal.daoBa.BaUserMapper;
import com.dy.pmsGlobal.daoPlt.PltProductMapper;
import com.dy.pmsGlobal.daoPlt.PltStationMapper;
import com.dy.pmsGlobal.daoPr.PrAssemblyPlanMapper;
import com.dy.pmsGlobal.daoPr.PrProductionNodeMapper;
import com.dy.pmsGlobal.daoSta.StaAssemblyWorkHistoryMapper;
import com.dy.pmsGlobal.daoSta.StaAssemblyWorkLastMapper;
import com.dy.pmsGlobal.pojoBa.BaUser;
import com.dy.pmsGlobal.pojoPlt.PltProduct;
import com.dy.pmsGlobal.pojoPlt.PltStation;
import com.dy.pmsGlobal.pojoPr.PrAssemblyPlan;
import com.dy.pmsGlobal.pojoPr.PrProductionNode;
import com.dy.pmsGlobal.pojoSta.StaAssemblyWorkHistory;
import com.dy.pmsGlobal.pojoSta.StaAssemblyWorkLast;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
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;
import java.util.stream.Collectors;
@Slf4j
@Service
public class WorkOrderSv {
    private static final int STATUS_NORMAL = 1;
    private static final int STATUS_WORK_LAST_LOGOUT = 2;
    private static final int STATUS_REPAIR = 3;
    private static final int STATUS_WASTE = 4;
    private static final String USER_PREFIX = "101";
    private static final String STATION_PREFIX = "103";
    private PrAssemblyPlanMapper assemblyDao;
    private BaUserMapper baUserDao;
    private PltStationMapper pltStationDao;
    private StaAssemblyWorkLastMapper assemblyWorkLastDao;
    private StaAssemblyWorkHistoryMapper assemblyWorkHistoryDao;
    private PrProductionNodeMapper nodeDao;
    private PltProductMapper productDao;
    @Autowired
    public void setAssemblyDao(PrAssemblyPlanMapper assemblyDao) {
        this.assemblyDao = assemblyDao;
    }
    public String isStationUsed(StaAssemblyWorkLast last){
        String stationId = "";
        String userId = "";
        //判断哪个是人员, 哪个是工站信息
       /* if(last.getStationId() != null && last.getStationId().toString().startsWith("103")){
        }
        if(last.getUserId() != null && last.getStationId().toString().startsWith("103")){
        }
        PrAssemblyPlan params = new PrAssemblyPlan();
        params.setStatus(1);
        List<PrAssemblyPlan> list = sv.selectList(params);
        return BaseResponseUtils.buildSuccess(list);
        log.info("获取工站编号");
        String maxCode = dao.selectMaxCode();
        if (StringUtils.isBlank(maxCode)) {
            return DEFAULT_CODE;
        } else {
            int nextCode = Integer.parseInt(maxCode) + 1;
            // 检查溢出
            if (nextCode > 9999) {
                throw new RuntimeException("工站编号溢出");
            }
            return String.format(CODE_FORMAT, nextCode);
        }*/
        return "OK";
    @Autowired
    private void setBaUserDao(BaUserMapper baUserDao) {
        this.baUserDao = baUserDao;
    }
    public List<PrAssemblyPlan> selectList(PrAssemblyPlan params){
        List<PrAssemblyPlan> planList = assemblyDao.selectList(params);
    @Autowired
    private void setPltStationDao(PltStationMapper pltStationDao) {
        this.pltStationDao = pltStationDao;
    }
    @Autowired
    private void setStaAssemblyWorkHistoryDao(StaAssemblyWorkHistoryMapper assemblyWorkHistoryDao) {
        this.assemblyWorkHistoryDao = assemblyWorkHistoryDao;
    }
    @Autowired
    private void setStaAssemblyWorkLastDao(StaAssemblyWorkLastMapper assemblyWorkLastDao) {
        this.assemblyWorkLastDao = assemblyWorkLastDao;
    }
    @Autowired
    public void setNodeDao(PrProductionNodeMapper nodeDao) {
        this.nodeDao = nodeDao;
    }
    @Autowired
    public void setPltProductDao(PltProductMapper productDao) {
        this.productDao = productDao;
    }
    public BaUser getUserInfo(String id) {
        Long userId = null;
        if (StringUtils.isBlank(id)) {
            throw new RuntimeException("员工编码不能为空");
        }
        //用户101
        if (id.startsWith(USER_PREFIX)) {
            userId = Long.parseLong(id.toString().substring(3));
        } else {
            userId = Long.parseLong(id.toString());
        }
        BaUser userInfo = baUserDao.selectByPrimaryKey(userId);
        if (userInfo == null) {
            throw new RuntimeException("员工编码:" + userId + "不在系统中,请先维护员工信息");
        } else if (userInfo.getDisabled()) {
            throw new RuntimeException("员工(" + userInfo.getName() + ")编码:" + userId + "在系统已禁用,请先启用员工");
        }
        return userInfo;
    }
    public PltStation getStationInfo(String id) {
        //仅检查工站信息以及工站占用情况
        Long stationId = null;
        if (StringUtils.isBlank(id)) {
            throw new RuntimeException("工站编码不能为空");
        }
        //工站103
        if (id.startsWith(STATION_PREFIX)) {
            stationId = Long.parseLong(id.toString().substring(3));
        } else {
            stationId = Long.parseLong(id.toString());
        }
        PltStation stationInfo = pltStationDao.selectByPrimaryKey(stationId);
        if (stationInfo == null) {
            throw new RuntimeException("工站编码:" + stationId + "不在系统中,请先维护工站信息");
        } else if (stationInfo.getDisabled()) {
            throw new RuntimeException("工站(" + stationInfo.getName() + ")编码:" + stationId + "在系统已禁用,请先启用工站");
        }
        return stationInfo;
    }
    public StaAssemblyWorkLast checkUserAndStationUsed(QueryVo vo) {
        StaAssemblyWorkLast result = new StaAssemblyWorkLast();
        PltStation stationInfo = null;
        BaUser userInfo = null;
        if (StringUtils.isBlank(vo.getStationId()) || StringUtils.isBlank(vo.getUserId())) {
            throw new RuntimeException("工站编码或员工编码均不能为空,请检查");
        }
        //工站103
        if (vo.getStationId().startsWith(USER_PREFIX)) {
            userInfo = getUserInfo(vo.getStationId());
        } else {
            stationInfo = getStationInfo(vo.getStationId());
        }
        if (vo.getUserId().startsWith(STATION_PREFIX)) {
            stationInfo = getStationInfo(vo.getUserId());
        } else {
            userInfo = getUserInfo(vo.getUserId());
        }
        if (userInfo == null && stationInfo == null) {
            throw new RuntimeException("员工编码:" + vo.getUserId() + "和工站编码:" + vo.getStationId() + "不正确,请检查");
        } else if (userInfo == null) {
            throw new RuntimeException("员工编码:" + vo.getUserId() + "不正确,请检查");
        } else if (stationInfo == null) {
            throw new RuntimeException("工站编码:" + vo.getStationId() + "不正确,请检查");
        }
        //检查表中stationId是否已经存在
        StaAssemblyWorkLast param = new StaAssemblyWorkLast();
        param.setStationId(stationInfo.getId());
        param.setStatus(STATUS_NORMAL);
        List<StaAssemblyWorkLast> workLasts = assemblyWorkLastDao.selectList(param);
        long userId = userInfo.getId();
        if (workLasts.size() > 0) {
            List<StaAssemblyWorkLast> userStationList = workLasts.stream().filter(item -> userId == item.getUserId()).collect(Collectors.toList());
            if (userStationList.size() == 0) {
                throw new RuntimeException("员工(" + workLasts.get(0).getUserName() + ")编码:" + workLasts.get(0).getUserId() + "已经绑定该工站(" + workLasts.get(0).getStationName() + ")编码:" + workLasts.get(0).getStationId() + ".请先让其解绑");
            } else {
                BeanUtils.copyProperties(userStationList.get(0), result);
            }
        } else {
            result.setUserId(userInfo.getId());
            result.setUserName(userInfo.getName());
            result.setStationId(stationInfo.getId());
            result.setStationName(stationInfo.getName());
            result.setLineId(stationInfo.getLineId());
        }
        return result;
    }
    public StaAssemblyWorkLast getWorkLast(String workId) {
        if(StringUtils.isBlank(workId)){
            throw new RuntimeException("登录ID为空,请联系管理员查看并解决问题.");
        }
        StaAssemblyWorkLast result = assemblyWorkLastDao.selectByPrimaryKey(Long.parseLong(workId));
        if (result == null) {
            throw new RuntimeException("系统中没有该条登录信息");
            //return null;
        }
        BaUser userInfo = getUserInfo(result.getUserId().toString());
        PltStation stationInfo = getStationInfo(result.getStationId().toString());
        //校验planId processId nodeId
        PrAssemblyPlan assyPlan = assemblyDao.selectByPrimaryKey(result.getPlanId());
        boolean hasNodeId = assyPlan.getProcess().getNodes().stream()
                .anyMatch(node -> node.id.equals(result.getNodeId()));
        if (userInfo == null || stationInfo == null || assyPlan == null || (!hasNodeId)) {
            //直接删除记录
            logout(result.getId().toString());
            result.setId(null);
            result.setStatus(STATUS_WORK_LAST_LOGOUT);
            if(userInfo == null){
                result.setUserId(null);
                result.setUserName("");
            }
            if(stationInfo == null){
                result.setStationId(null);
                result.setStationName("");
            }
            if(assyPlan == null){
                result.setPlanId(null);
                result.setPlanName("");
            }
            if(!hasNodeId){
                result.setNodeId(null);
                result.setNodeName("");
            }
            return result;
        }else {
            result.setPlanName(assyPlan.getName());
            result.setProcessName(assyPlan.getProcessName());
            result.setNodeName(assyPlan.getProcess().getNodes().get(0).getContent());
            //找到对应的产品信息
            PltProduct  product = productDao.selectByPrimaryKey(assyPlan.getProId());
            if (product != null) {
                result.setProId(product.getId());
                result.setProName(product.getName());
                String devicePrefix = "110" + product.getCode() + assyPlan.getBatchNo();
                result.setDevicePrefix(devicePrefix);
            }
        }
        result.setStationId(stationInfo.getId());
        result.setStationName(stationInfo.getName());
        result.setLineId(stationInfo.getLineId());
        result.setLineName(stationInfo.getLineName());
        return result;
    }
    public StaAssemblyWorkLast checkStationUsed(String stationId) {
        StaAssemblyWorkLast result = new StaAssemblyWorkLast();
        PltStation stationInfo = null;
        if (StringUtils.isBlank(stationId)) {
            throw new RuntimeException("工站编码不能为空,请检查");
        }
        stationInfo = getStationInfo(stationId);
        //检查表中stationId是否已经存在
        StaAssemblyWorkLast param = new StaAssemblyWorkLast();
        param.setStationId(stationInfo.getId());
        param.setStatus(STATUS_NORMAL);
        List<StaAssemblyWorkLast> workLasts = assemblyWorkLastDao.selectList(param);
        if (workLasts.size() > 0) {
            result =  getWorkLast(workLasts.get(0).getId().toString());
        }
        result.setStationId(stationInfo.getId());
        result.setStationName(stationInfo.getName());
        result.setLineId(stationInfo.getLineId());
        result.setLineName(stationInfo.getLineName());
        return result;
    }
    @Transactional
    public StaAssemblyWorkLast save(QueryVo vo) {
        //先check
        StaAssemblyWorkLast checkResult = checkUserAndStationUsed(vo);
        //校验planId processId nodeId
        PrAssemblyPlan assyPlan = assemblyDao.selectByPrimaryKey(vo.getPlanId());
        if (assyPlan == null) {
            throw new RuntimeException("组装任务已经在系统中删除请检查,刷新并重新选择任务");
        }
        boolean hasNodeId = assyPlan.getProcess().getNodes().stream()
                .anyMatch(node -> node.id.equals(vo.getNodeId()));
        if (!hasNodeId) {
            throw new RuntimeException("节点信息有误或被其他人员修改,请重新选择节点信息");
        }
        //数据逐个封装返回
        vo.setLineId(checkResult.getLineId());
        StaAssemblyWorkLast staLast = new StaAssemblyWorkLast();
        BeanUtils.copyProperties(vo, staLast);
        staLast.setUserId(checkResult.getUserId());
        staLast.setStationId(checkResult.getStationId());
        int count = 0;
        if (checkResult.getId() == null) {
            BeanUtils.copyProperties(vo, staLast);//vo拷贝到staLast
            staLast.setUserId(checkResult.getUserId());
            staLast.setStationId(checkResult.getStationId());
            staLast.setStatus(STATUS_NORMAL);
            staLast.setStartTime(new Date());
            count = assemblyWorkLastDao.insertSelective(staLast);
        } else {
            //保存一条history?
            BeanUtils.copyProperties(checkResult, staLast);
            staLast.setPlanId(assyPlan.getId());
            staLast.setProcessId(assyPlan.getProcess().getId());
            staLast.setNodeId(vo.getNodeId());
            staLast.setStatus(STATUS_NORMAL);
            staLast.setStartTime(new Date());
            count = assemblyWorkLastDao.updateByPrimaryKeySelective(staLast);
        }
        if (count <= 0) {
            throw new RuntimeException("数据库存储失败");
        }
        StaAssemblyWorkLast result = assemblyWorkLastDao.selectByPrimaryKey(staLast.getId());
        List<PrProductionNode> nodeInfo = assyPlan.getProcess().getNodes().stream()
                .filter(node -> node.id.equals(vo.getNodeId())).collect(Collectors.toList());
        result.setPlanName(assyPlan.getName());
        result.setProcessName(assyPlan.getProcessName());
        result.setNodeName(assyPlan.getProcess().getNodes().get(0).getContent());
        //找到对应的产品信息
        PltProduct  product = productDao.selectByPrimaryKey(assyPlan.getProId());
        if (product == null) {
            throw new RuntimeException("产品信息有误或被其他人员修改,请登录管理系统查看任务关联产品ID为:" + assyPlan.getProId());
        }
        result.setProId(product.getId());
        result.setProName(product.getName());
        String devicePrefix = "110" + product.getCode() + assyPlan.getBatchNo();
        result.setDevicePrefix(devicePrefix);
        return result;
    }
    @Transactional
    public int logout(String id) {
        //找到登录记录
        StaAssemblyWorkLast workLast = assemblyWorkLastDao.selectByPrimaryKey(Long.parseLong(id));
        if (workLast == null) {
            throw new RuntimeException("系统中没有该条登录信息");
        }
        //更新登录记录
        workLast.setStatus(STATUS_WORK_LAST_LOGOUT);
        workLast.setEndTime(new Date());
        // 保存history
        StaAssemblyWorkHistory history = new StaAssemblyWorkHistory();
        BeanUtils.copyProperties(workLast, history);
        //history.setId(null);
        int count = assemblyWorkHistoryDao.insertSelective(history);
        //删除last
        assemblyWorkLastDao.deleteByPrimaryKey(Long.parseLong(id));
        return count;
    }
    public List<PrAssemblyPlan> selectAssyPlanList(PrAssemblyPlan params) {
        List<PrAssemblyPlan> planList = assemblyDao.selectAssyPlanSimplify(params);
        /* planList.stream().forEach(process -> {
            process.getProcess().getNodes().forEach(node -> {
                if(node.instruction != null){
                    addUrl(node.instruction);
                }
            });
        });*/
        return planList;
    }
}
    public int addAssistant(String workId, String assistant) {
        //找到workId
        StaAssemblyWorkLast workLast = assemblyWorkLastDao.selectByPrimaryKey(Long.parseLong(workId));
        if (workLast == null) {
            throw new RuntimeException("系统中没有当前登录信息,请检查或重新登录");
        }
        BaUser userInfo = getUserInfo(assistant);
        //当前登录人不能是辅助人员
        if (workLast.getUserId() == userInfo.getId()) {
            throw new RuntimeException(userInfo.getName() + "是当前登录人员,不必添加为辅助人员.");
        }
        String userId = userInfo.getId().toString();
        if (StringUtils.isBlank(workLast.getAssistants())) {
            workLast.setAssistants(userId);
        } else {
            //已经添加的辅助人员 不必重复添加
            String assistants = workLast.getAssistants();
            String[] assistantArr = assistants.split(",");
            for (String item : assistantArr) {
                if (userId.equals(item)) {
                    throw new RuntimeException(userInfo.getName() + "已经添加为辅助人员,不必重复添加.");
                }
            }
            workLast.setAssistants(assistants + "," + userId);
        }
        int count = assemblyWorkLastDao.updateByPrimaryKey(workLast);
        return count;
    }
    public int removeAssistant(String workId, String assistant) {
        //找到workId
        StaAssemblyWorkLast workLast = assemblyWorkLastDao.selectByPrimaryKey(Long.parseLong(workId));
        if (workLast == null) {
            throw new RuntimeException("系统中没有当前登录信息,请检查或重新登录");
        }
        if ((!StringUtils.isBlank(assistant)) && assistant.startsWith(USER_PREFIX)) {
            assistant = assistant.substring(3);
        }
        if (!StringUtils.isBlank(workLast.getAssistants())) {
            StringBuilder sb = new StringBuilder();
            String[] assistantArr = workLast.getAssistants().split(",");
            for (String item : assistantArr) {
                if ((!StringUtils.isBlank(assistant)) && (!assistant.equals(item))) {
                    sb.append(item).append(",");
                }
            }
            String assistantStr = sb.length() > 0 ? sb.deleteCharAt(sb.length() - 1).toString() : "";
            workLast.setAssistants(assistantStr);
        }
        int count = assemblyWorkLastDao.updateByPrimaryKey(workLast);
        return count;
    }
    public List<BaUser> getAssistantList(String workId) {
        //找到workId
        StaAssemblyWorkLast workLast = assemblyWorkLastDao.selectByPrimaryKey(Long.parseLong(workId));
        if (workLast == null) {
            throw new RuntimeException("系统中没有当前登录信息,请检查或重新登录");
        }
        List<BaUser> assistantList = baUserDao.selectByAssistants(workLast.getAssistants());
        return assistantList;
    }
}