Administrator
2023-08-21 53c50fadb458e7ed251eb942f865c06039fb5958
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
270
package com.ruoyi.alarmpoints.well.service.impl;
 
import java.util.List;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.alarmpoints.well.domain.ArdAlarmpointsWell;
import com.ruoyi.alarmpoints.well.domain.ArdAlarmpointsWellParam;
import com.ruoyi.alarmpoints.well.mapper.ArdAlarmpointsWellMapper;
import com.ruoyi.alarmpoints.well.service.IArdAlarmpointsWellService;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import lombok.ToString;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import javax.validation.Validator;
 
/**
 * 井管理Service业务层处理
 *
 * @author 刘苏义
 * @date 2023-03-07
 */
@Service
public class ArdAlarmpointsWellServiceImpl implements IArdAlarmpointsWellService {
    private static final Logger log = LoggerFactory.getLogger(ArdAlarmpointsWellServiceImpl.class);
    @Resource
    private ArdAlarmpointsWellMapper ardAlarmpointsWellMapper;
    @Autowired
    protected Validator validator;
    /**
     * 查询井管理
     *
     * @param id 井管理主键
     * @return 井管理
     */
    @Override
    public ArdAlarmpointsWell selectArdAlarmpointsWellById(String id) {
        return ardAlarmpointsWellMapper.selectArdAlarmpointsWellById(id);
    }
    /**
     * 查询井管理
     *
     * @param wellId 井管理主键
     * @return 井管理
     */
    @Override
    public ArdAlarmpointsWell selectArdAlarmpointsWellByWellId(String wellId) {
        return ardAlarmpointsWellMapper.selectArdAlarmpointsWellByWellId(wellId);
    }
 
    /**
     * 查询井管理列表
     *
     * @param ardAlarmpointsWell 井管理
     * @return 井管理
     */
    @Override
    @DataScope(deptAlias = "d",userAlias = "u")
    public List<ArdAlarmpointsWell> selectArdAlarmpointsWellList(ArdAlarmpointsWell ardAlarmpointsWell) {
        return ardAlarmpointsWellMapper.selectArdAlarmpointsWellList(ardAlarmpointsWell);
    }
 
    /**
     *
     * @param ardAlarmpointsWell 按井编号查询
     * @return
     */
    @Override
    @DataScope(deptAlias = "d",userAlias = "u")
    public List<ArdAlarmpointsWell> selectArdAlarmpointsWellByWellIdLike(ArdAlarmpointsWell ardAlarmpointsWell) {
        return ardAlarmpointsWellMapper.selectArdAlarmpointsWellByWellIdLike(ardAlarmpointsWell);
    }
 
    /**
     * 新增井管理
     *
     * @param ardAlarmpointsWell 井管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertArdAlarmpointsWell(ArdAlarmpointsWell ardAlarmpointsWell) {
        boolean wellIdExists = ardAlarmpointsWellMapper.checkWellIdExists(ardAlarmpointsWell.getWellId());
        if(wellIdExists)
        {
            throw new RuntimeException("井号已存在");
        }
        ardAlarmpointsWell.setId(IdUtils.simpleUUID());
        ardAlarmpointsWell.setUserId(SecurityUtils.getUserId());
        ardAlarmpointsWell.setCreateBy(SecurityUtils.getUsername());
        ardAlarmpointsWell.setCreateTime(DateUtils.getNowDate());
        return ardAlarmpointsWellMapper.insertArdAlarmpointsWell(ardAlarmpointsWell);
    }
 
    /**
     * 修改井管理
     *
     * @param ardAlarmpointsWell 井管理
     * @return 结果
     */
    @Override
    @Transactional
    public int updateArdAlarmpointsWell(ArdAlarmpointsWell ardAlarmpointsWell) {
        boolean wellIdExists = ardAlarmpointsWellMapper.checkWellIdExists(ardAlarmpointsWell.getWellId());
        if(wellIdExists)
        {
            throw new RuntimeException("井号已存在");
        }
        ardAlarmpointsWell.setUpdateBy(SecurityUtils.getUsername());
        ardAlarmpointsWell.setUpdateTime(DateUtils.getNowDate());
        return ardAlarmpointsWellMapper.updateArdAlarmpointsWell(ardAlarmpointsWell);
    }
 
    /**
     * 批量删除井管理
     *
     * @param ids 需要删除的井管理主键
     * @return 结果
     */
    @Override
    public int deleteArdAlarmpointsWellByIds(String[] ids) {
        return ardAlarmpointsWellMapper.deleteArdAlarmpointsWellByIds(ids);
    }
 
    /**
     * 删除井管理信息
     *
     * @param id 井管理主键
     * @return 结果
     */
    @Override
    public int deleteArdAlarmpointsWellById(String id) {
        return ardAlarmpointsWellMapper.deleteArdAlarmpointsWellById(id);
    }
 
    @Override
    public String importUser(List<ArdAlarmpointsWell> ardAlarmpointsWellList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(ardAlarmpointsWellList) || ardAlarmpointsWellList.size() == 0) {
            throw new ServiceException("导入井数据不能为空!");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ArdAlarmpointsWell well : ardAlarmpointsWellList) {
            try {
                //获取当前登录用户id
                String userId = SecurityUtils.getUserId();
                well.setUserId(userId);
                // 验证是否存在这个用户
                ArdAlarmpointsWell u = ardAlarmpointsWellMapper.selectArdAlarmpointsWellByWellId(well.getWellId());
                if (StringUtils.isNull(u)) {
                    BeanValidators.validateWithException(validator, well);
                    well.setCreateBy(operName);
                    this.insertArdAlarmpointsWell(well);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、井号 " + well.getWellId() + " 导入成功");
                } else if (isUpdateSupport) {
                    BeanValidators.validateWithException(validator, well);
                    checkWellAllowed(well);
                    checkWellDataScope(well.getUserId());
                    well.setUpdateBy(operName);
                    this.updateArdAlarmpointsWell(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 well 井信息
     */
    @Override
    public void checkWellAllowed(ArdAlarmpointsWell well)
    {
        if (StringUtils.isNotNull(well.getId()))
        {
            throw new ServiceException("不允许操作井");
        }
    }
 
    /**
     * 校验用户是否有数据权限
     *
     * @param userId 用户id
     */
    @Override
    public void checkWellDataScope(String userId)
    {
        if (!SysUser.isAdmin(SecurityUtils.getUserId()))
        {
            ArdAlarmpointsWell well = new ArdAlarmpointsWell();
            well.setUserId(userId);
            List<ArdAlarmpointsWell> wells = SpringUtils.getAopProxy(this).selectArdAlarmpointsWellList(well);
            if (StringUtils.isEmpty(wells))
            {
                throw new ServiceException("没有权限访问井数据!");
            }
        }
    }
 
 
    @Override
    public List<ArdAlarmpointsWell> wellByDeptList(List<Long> deptList) {
        QueryWrapper<ArdAlarmpointsWell> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("dept_id",deptList);
        return ardAlarmpointsWellMapper.selectList(queryWrapper);
    }
 
    @Override
    public ArdAlarmpointsWell wellById(String id) {
        return ardAlarmpointsWellMapper.selectById(id);
    }
 
    @Override
    public List<ArdAlarmpointsWell> wellList(List<Long> deptList) {
        QueryWrapper<ArdAlarmpointsWell> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("dept_id",deptList);
        return ardAlarmpointsWellMapper.selectList(queryWrapper);
    }
 
    @Override
    public List<ArdAlarmpointsWell> conditionList(ArdAlarmpointsWellParam ardAlarmpointsWellParam){
        QueryWrapper<ArdAlarmpointsWell> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("dept_id",ardAlarmpointsWellParam.getDeptList())
        .eq(!StringUtils.isBlank(ardAlarmpointsWellParam.getWellId()),"well_id",ardAlarmpointsWellParam.getWellId())
        .eq(!StringUtils.isBlank(ardAlarmpointsWellParam.getWellNumber()),"well_number",ardAlarmpointsWellParam.getWellNumber())
        .eq(!StringUtils.isBlank(ardAlarmpointsWellParam.getOilProduction()),"oil_production",ardAlarmpointsWellParam.getOilProduction())
        .eq(!StringUtils.isBlank(ardAlarmpointsWellParam.getWellBlock()),"well_block",ardAlarmpointsWellParam.getWellBlock())
        .eq(!StringUtils.isBlank(ardAlarmpointsWellParam.getDisplacementMode()),"displacement_mode",ardAlarmpointsWellParam.getDisplacementMode())
        .eq(!StringUtils.isBlank(ardAlarmpointsWellParam.getSurroundingEnvironment()),"surrounding_environment",ardAlarmpointsWellParam.getSurroundingEnvironment())
        .eq(!StringUtils.isBlank(ardAlarmpointsWellParam.getWellType()),"well_type",ardAlarmpointsWellParam.getWellType())
        .eq(!StringUtils.isBlank(ardAlarmpointsWellParam.getInstalledLoad()),"installed_load",ardAlarmpointsWellParam.getInstalledLoad())
        .eq(!StringUtils.isBlank(ardAlarmpointsWellParam.getMeteringStation()),"metering_station",ardAlarmpointsWellParam.getMeteringStation())
        .eq(!StringUtils.isBlank(ardAlarmpointsWellParam.getTransferStation()),"transfer_station",ardAlarmpointsWellParam.getTransferStation())
        .eq(!StringUtils.isBlank(ardAlarmpointsWellParam.getDehydrationStation()),"dehydration_station",ardAlarmpointsWellParam.getDehydrationStation())
        .eq(!StringUtils.isBlank(ardAlarmpointsWellParam.getRunStatus()),"run_status",ardAlarmpointsWellParam.getRunStatus());
        return ardAlarmpointsWellMapper.selectList(queryWrapper);
    }
}