11
13693261870
2024-09-18 0849b4fd1ad5626710d0aebecd1f3e419573173a
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
package com.se.simu.service;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.se.simu.domain.po.DataPo;
import com.se.simu.domain.po.SimuPo;
import com.se.simu.domain.vo.CreateSimuVo;
import com.se.simu.domain.vo.SimuVo;
import com.se.simu.helper.StringHelper;
import com.se.simu.mapper.SimuMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
 
import javax.annotation.Resource;
import java.io.File;
import java.util.List;
 
/**
 * 仿真服务类
 *
 * @author WWW
 * @date   2024-09-18
 */
@Slf4j
@Service
@SuppressWarnings("ALL")
public class SimuService {
    @Value("${sys.path.in}")
    String inPath;
 
    @Value("${sys.path.out}")
    String outPath;
 
    @Resource
    SimuMapper simuMapper;
 
    public Integer getMaxId() {
        return simuMapper.selectMaxId();
    }
 
    public boolean create(CreateSimuVo vo) {
        return true;
    }
 
    public int del(List<Integer> ids) {
        List<SimuPo> list = simuMapper.selectBatchIds(ids);
        if (null != list && list.size() > 0) {
            for (SimuPo po : list) {
                try {
                    if (StringHelper.isEmpty(po.getData())) continue;
 
                    DataPo dp = JSONUtil.toBean(po.getData(), DataPo.class);
                    if (null == dp) continue;
 
                    delDir(inPath + File.separator + dp.getInPath());
                    delDir(outPath + File.separator + dp.getOutPath());
                } catch (Exception ex) {
                    log.error(ex.getMessage(), ex);
                }
            }
        }
 
        return simuMapper.deleteBatchIds(ids);
    }
 
    private boolean delDir(String dir) {
        File file = new File(dir);
        if (!file.exists() || !file.isDirectory()) {
            return false;
        }
 
        return FileUtil.del(dir);
    }
 
    public IPage<SimuPo> get(SimuVo vo) {
        QueryWrapper<SimuPo> wrapper = getPageWrapper(vo);
 
        Page<SimuPo> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        page.addOrder(OrderItem.desc("id"));
 
        IPage<SimuPo> paged = simuMapper.selectPage(page, wrapper);
 
        return paged;
    }
 
    private QueryWrapper<SimuPo> getPageWrapper(SimuVo vo) {
        QueryWrapper<SimuPo> wrapper = new QueryWrapper<>();
        if (null != vo.getId()) {
            wrapper.eq("id", vo.getId());
        }
        if (null != vo.getPid()) {
            wrapper.eq("pid", vo.getPid());
        }
        if (null != vo.getNum()) {
            wrapper.eq("num", vo.getNum());
        }
        if (!StringUtils.isEmpty(vo.getName())) {
            wrapper.like("lower(name)", vo.getName().trim().toLowerCase());
        }
        if (!CollUtil.isEmpty(vo.getStatus())) {
            wrapper.in("status", vo.getStatus());
        }
 
        return wrapper;
    }
}