张洋洋
2025-02-18 eaf2eddc15c08bc073c953630b727f6497dfee3a
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package com.se.simu.service;
 
import cn.hutool.core.bean.BeanUtil;
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.config.PropertiesConfig;
import com.se.simu.domain.dto.GeDb;
import com.se.simu.domain.po.DataPo;
import com.se.simu.domain.po.SimuPo;
import com.se.simu.domain.vo.CreateFilesSimuVo;
import com.se.simu.domain.vo.SimuVo;
import com.se.simu.helper.StringHelper;
import com.se.simu.helper.WebHelper;
import com.se.simu.mapper.SimuMapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.io.File;
import java.sql.Timestamp;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
/**
 * SIMU 文件服务
 *
 * @author xingjinshuang@smartearth.cn
 * @date 2024/12/24
 */
@Slf4j
@Service
 
public class SimuFilesService {
 
    @Resource
    SimuMapper simuMapper;
 
    @Resource
    PropertiesConfig config;
 
    @Resource
    GedbService gedbService;
 
    @Resource
    UwService uwService;
 
    @Resource
    ResultService resultService;
 
    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 (!StringHelper.isEmpty(vo.getName())) {
            wrapper.like("lower(name)", vo.getName().trim().toLowerCase());
        }
        if (!CollUtil.isEmpty(vo.getStatus())) {
            wrapper.in("status", vo.getStatus());
        }
 
        return wrapper;
    }
 
 
    private boolean delDir(String dir) {
        File file = new File(dir);
        if (!file.exists() || !file.isDirectory()) {
            return false;
        }
 
        return FileUtil.del(dir);
    }
 
 
    /**
     * "状态:
     * 0-创建仿真任务,
     * 1-连接GEDB库,
     * 2-下载空间数据,
     * 3-下载高程数据,
     * 4-生成降雨文件,
     * 5-生成配置文件,
     * 6-模拟内涝仿真,
     * 7-处理水位文件,
     * 8-处理排水文件,
     * 9-处理仿真结果,
     * 10-完成,-10-出错
     *
     * @param vo VO
     * @return boolean
     */
    public boolean createByfiles(CreateFilesSimuVo vo) {
        Date now = new Date();
        String date = StringHelper.YMDHMS2_FORMAT.format(now);
        if (StringHelper.isEmpty(vo.getName())) {
            vo.setName(date);
        }
        DataPo data = BeanUtil.copyProperties(vo, DataPo.class);
        data.setPath(date, date);
        initPath(data);
        SimuPo simu = new SimuPo(vo.getNum(), vo.getPid(), vo.getName(), JSONUtil.toJsonStr(data), 0, vo.getBak());
        simu.setServiceName(date);
        simu.setCreateTime(new Timestamp(now.getTime()));
        simu.setSemUrl(vo.getSemUrl());
        int rows = simuMapper.insert(simu);
        if (rows > 0) {
            asyncCall(simu);
        }
 
        return rows > 0;
    }
 
    private void initPath(DataPo data) {
        createDir(config.getInPath() + File.separator + data.getInPath());
        createDir(config.getOutPath() + File.separator + data.getOutPath());
    }
 
    private void createDir(String path) {
        File f = new File(path);
        if (f.exists() && f.isDirectory()) {
            FileUtil.del(f);
        }
        f.mkdirs();
    }
 
    private void asyncCall(SimuPo simu) {
        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.execute(new Runnable() {
            @Override
            @SneakyThrows
            public void run() {
                cope(simu);
            }
        });
        executor.shutdown();
    }
 
 
    /**
     * "状态:
     * 0-创建仿真任务,
     * 1-连接GEDB库,
     * 2-下载空间数据,
     * 3-下载高程数据,
     * 4-生成降雨文件,
     * 5-生成配置文件,
     * 6-模拟内涝仿真,
     * 7-处理水位文件,
     * 8-处理排水文件,
     * 9-处理仿真结果,
     * 10-完成,-10-出错
     *
     * @param simu 模拟
     */
 
    private void cope(SimuPo simu) {
        try {
            DataPo data = JSONUtil.toBean(simu.getData(), DataPo.class);
 
            update(simu, 1, null);
            String token = gedbService.getToken();
            GeDb db = gedbService.connectGedb(token, data);
            simu.setData(JSONUtil.toJsonStr(data));
 
            update(simu, 2, null);
            gedbService.copeVectors(token, data, db);
 
            update(simu, 3, null);
            gedbService.copeDem(token, data);
 
            update(simu, 4, null);
            uwService.createRainFile(data);
 
            update(simu, 5, null);
            uwService.createConfig(data);
 
            update(simu, 6, null);
            uwService.callExe(data);
 
            update(simu, 7, null);
            //uwService.copeWaterFiles();
 
            update(simu, 8, null);
            uwService.copeDrainFiles(data);
 
            update(simu, 9, null);
            resultService.process(data);
 
            update(simu, 10, "完成");
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            update(simu, -simu.getStatus(), ex.getMessage());
        }
    }
 
    private void update(SimuPo simu, int status, String rs) {
        simu.setStatus(status);
        if (null != rs) simu.setResult(rs);
        simu.setUpdateTime(WebHelper.getCurrentTimestamp());
 
        simuMapper.updateById(simu);
    }
}