liusuyi
2026-05-14 9958bc1501d0daee954d9bba383475e55e24ebab
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package com.ard.work.point.well.service.impl;
 
import com.ard.common.core.constant.DeviceConstants;
import com.ard.common.core.constant.SecurityConstants;
import com.ard.common.core.constant.UserConstants;
import com.ard.common.core.domain.R;
import com.ard.common.core.exception.ServiceException;
import com.ard.common.core.utils.DateUtils;
import com.ard.common.core.utils.SpringUtils;
import com.ard.common.core.utils.StringUtils;
import com.ard.common.core.utils.bean.BeanValidators;
import com.ard.common.core.utils.uuid.IdUtils;
import com.ard.common.datascope.annotation.DataScope;
import com.ard.common.security.utils.SecurityUtils;
import com.ard.system.api.RemoteDeptService;
import com.ard.system.api.domain.SysDept;
import com.ard.system.api.domain.SysUser;
import com.ard.work.api.domian.ArdWell;
import com.ard.work.point.well.domain.ArdWellDeptVo;
import com.ard.work.point.well.domain.RtuDataYjEntity;
import com.ard.work.point.well.mapper.ArdWellMapper;
import com.ard.work.point.well.service.IArdWellService;
import com.ard.work.utils.data.Query;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import jakarta.validation.Validator;
import java.util.ArrayList;
import java.util.List;
 
import static com.ard.work.utils.gis.GisUtil.isInChina;
 
/**
 * 井管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-07
 */
@Slf4j
@Service
public class ArdWellServiceImpl implements IArdWellService {
 
    @Value("${orcl.orclUrl}")
    private String url;
 
    @Value("${orcl.orclUser}")
    private String user;
 
    @Value("${orcl.orclPwd}")
    private String pwd;
 
    @Value("${orcl.orclSql}")
    private String sql;
 
    @Value("${orcl.openClose}")
    private Boolean openClose;
 
    @Resource
    private ArdWellMapper ardWellMapper;
    @Resource
    protected Validator validator;
    @Resource
    private RemoteDeptService remoteDeptService;
 
    /**
     * 查询井管理
     *
     * @param id 井管理主键
     * @return 井管理
     */
    @Override
    public ArdWell selectArdWellById(String id) {
        ArdWell ardWell = ardWellMapper.selectArdWellById(id);
        R<SysDept> r = remoteDeptService.getDeptById(ardWell.getDeptId(), SecurityConstants.INNER);
        if (r.getCode() == 200) {
            ardWell.setDeptName(r.getData().getDeptName());
        }
 
        return ardWell;
    }
 
    /**
     * 查询井管理列表
     *
     * @param ardWell 井管理
     * @return 井管理
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<ArdWell> selectArdWellList(ArdWell ardWell) {
        return ardWellMapper.selectArdWellList(ardWell);
    }
 
    /**
     * 新增井管理
     *
     * @param ardWell 井管理
     * @return 结果
     */
    @Override
    public int insertArdWell(ArdWell ardWell) {
        ardWell.setId(IdUtils.simpleUUID());
        ardWell.setCreateTime(DateUtils.getNowDate());
        return ardWellMapper.insertArdWell(ardWell);
    }
 
    /**
     * 修改井管理
     *
     * @param ardWell 井管理
     * @return 结果
     */
    @Override
    public int updateArdWell(ArdWell ardWell) {
        ardWell.setUpdateTime(DateUtils.getNowDate());
        return ardWellMapper.updateArdWell(ardWell);
    }
 
    /**
     * 批量删除井管理
     *
     * @param ids 需要删除的井管理主键
     * @return 结果
     */
    @Override
    public int deleteArdWellByIds(String[] ids) {
        return ardWellMapper.deleteArdWellByIds(ids);
    }
 
    /**
     * 删除井管理信息
     *
     * @param id 井管理主键
     * @return 结果
     */
    @Override
    public int deleteArdWellById(String id) {
        return ardWellMapper.deleteArdWellById(id);
    }
 
    /**
     * 导入井管理数据
     *
     * @param ardWellList 井数据列表
     * @param isUpdateSupport 是否更新支持,如果已存在,则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importWell(List<ArdWell> ardWellList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(ardWellList) || ardWellList.size() == 0) {
            throw new ServiceException("导入井数据不能为空!");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ArdWell well : ardWellList) {
            try {
                //获取当前登录用户id
                Long userId = SecurityUtils.getUserId();
                well.setUserId(userId);
                // 验证是否存在这个井
                ArdWell w = ardWellMapper.selectArdWellByWellId(well.getWellId());
                // 验证井是否在国内
                if (!isInChina(well.getLongitude(), well.getLatitude())) {
                    throw new ServiceException(well.getWellId() + "经纬度不在中国范围内!");
                }
                if (StringUtils.isNull(w)) {
                    BeanValidators.validateWithException(validator, well);
                    well.setCreateBy(operName);
                    this.insertArdWell(well);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、井号 " + well.getWellId() + " 导入成功");
                } else if (isUpdateSupport) {
                    BeanValidators.validateWithException(validator, well);
                    checkWellDataScope(well.getUserId());
                    well.setUpdateBy(operName);
                    this.updateArdWell(well);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、井号 " + well.getWellId() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、井号 " + well.getWellId() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、井号 " + well.getWellId() + " 导入失败:";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉,导入失败!共 " + failureNum + " 条数据格式不正确,错误如下:");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您,数据已全部导入成功!共 " + successNum + " 条,数据如下:");
        }
        return successMsg.toString();
    }
 
 
    /**
     * 校验用户是否有数据权限
     *
     * @param userId 用户id
     */
    @Override
    public void checkWellDataScope(Long userId) {
        if (!UserConstants.isAdmin(SecurityUtils.getUserId())) {
            ArdWell well = new ArdWell();
            well.setUserId(userId);
            List<ArdWell> wells = SpringUtils.getAopProxy(this).selectArdWellList(well);
            if (StringUtils.isEmpty(wells)) {
                throw new ServiceException("没有权限访问井数据!");
            }
        }
    }
 
    /**
     * 根据deptId获取对应所有兴趣点数据
     *
     * @param deptList deptId
     * @return 结果
     */
    @Override
    public List<ArdWellDeptVo> wellListDept(List<Long> deptList) {
        return ardWellMapper.wellListDept(deptList);
    }
 
    /**
     * 根据wellId获取设备运行状态
     *
     * @param wellId wellId
     * @return 结果
     */
    @Override
    public List<RtuDataYjEntity> getRTUDataYJ8(String wellId) {
        if (!openClose) {
            // 目前解决,六厂,二厂,四厂。没有数据库的问题,让其返回空不影响下一步操作。
            return new ArrayList<>();
        }
        return Query.getRTUDataYJ8(wellId, url, user, pwd, sql);
    }
 
 
    /**
     * 校验井号是否唯一
     *
     * @param ardWell 井信息
     * @return 是否唯一
     */
    @Override
    public String checkWellIdUnique(ArdWell ardWell) {
        String wellId = ardWell.getWellId();
        ArdWell info = ardWellMapper.checkWellIdUnique(wellId);
        if (StringUtils.isNotNull(info) && !info.getId().equals(ardWell.getId())) {
            return DeviceConstants.NOT_UNIQUE;
        }
        return DeviceConstants.UNIQUE;
    }
    @Override
    public List<ArdWell> selectArdWellByUserId(Long userId){
        return ardWellMapper.selectArdWellByUserId(userId);
    }
}