yangys
2025-06-13 4308b53ee6f9028905a333d86861ab2735ad2166
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package org.springblade.mdm.basesetting.machine;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BizServiceImpl;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.mdm.basesetting.machine.entity.Machine;
import org.springblade.mdm.basesetting.machine.entity.MachineSpec;
import org.springblade.mdm.basesetting.machine.mapper.MachineMapper;
import org.springblade.mdm.basesetting.machine.vo.MachineQueryVO;
import org.springblade.mdm.basesetting.machine.vo.MachineSaveVO;
import org.springblade.mdm.basesetting.machine.vo.MachineVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.FileSystemUtils;
 
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
 
@Service
public class MachineService extends BizServiceImpl<MachineMapper, Machine> {
 
    @Transactional
    public void saveMachine(MachineSaveVO vo){
        //TODO
        Machine machine = new Machine();
        BeanUtil.copyProperties(vo, machine);
        this.save(machine);
    }
 
    public boolean updateMachine(MachineSaveVO vo) {
        Machine machine = this.getById(vo.getId());
 
        machine.setMachineSpec(vo.getMachineSpec());
        machine.setName(vo.getName());
        machine.setMachineGroupCode(vo.getMachineGroupCode());
        machine.setManufacturer(vo.getManufacturer());
        machine.setOperator(vo.getOperator());
        machine.setControlSystem(vo.getControlSystem());
        machine.setOwnerDept(vo.getOwnerDept());
        machine.setPollingHours(vo.getPollingHours());
        machine.setProgSendDir(vo.getProgSendDir());
        machine.setProgReceiveDir(vo.getProgReceiveDir());
        machine.setRemark(vo.getRemark());
 
        return this.updateById(machine);
    }
 
 
 
 
    /**
     * 查询分页
     * @param query 查询参数
     * @return 分页数据
     */
    public IPage<MachineVO> pageQuery(MachineQueryVO query) {
 
        LambdaQueryWrapper<Machine> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(query.getMachineGroupCode()!=null, Machine::getMachineGroupCode, query.getMachineGroupCode());
        IPage<MachineVO> page = this.getBaseMapper().pageQuery(Condition.getPage(query),queryWrapper);
 
        //MachineSpec.valueOf()
        for (MachineVO record : page.getRecords()) {
            if(record.getMachineSpec() != null){
                MachineSpec spec = MachineSpec.valueOf(record.getMachineSpec());
                record.setMachineSpecName(spec.getText());
            }
 
        }
        return page;
    }
 
    /**
     * 查询详情
     * @param id 机床id
     * @return
     */
    public MachineVO detail(long id) {
        Machine machine = this.getById(id);
 
        MachineVO vo = new MachineVO();
        BeanUtil.copyProperties(machine, vo);
 
        return vo;
    }
 
    /**
     * 产生机床回传结构数据
     * @param id
     */
    public void genMachineFileBackDirs(Long id) throws IOException {
        Machine machine = this.getById(id);
        if(Func.isNotBlank(machine.getProgReceiveDir())){
            if(isLegalDirectoryPath(machine.getProgReceiveDir())) {
                Path path = Paths.get(machine.getProgReceiveDir());
                Files.createDirectories(path);
            }else{
                throw new RuntimeException("非法的目录格式");
            }
        }
    }
 
    /**
     * 判断是否是合法的目录格式
     * @param path
     * @return
     */
    static boolean isLegalDirectoryPath(String path) {
        // 基础检查
        if (path == null || path.trim().isEmpty()) {
            return false;
        }
 
        // 使用NIO检查路径格式
        Path nioPath;
        try {
            nioPath = Paths.get(path);
            if (!nioPath.normalize().toString().equals(path)) {
                return false; // 路径不规范
            }
        } catch (InvalidPathException ex) {
            return false;
        }
 
        return true;
 
    }
}