package com.ruoyi.alarmpoints.well.service.impl; import java.util.ArrayList; import java.util.List; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.ruoyi.alarmpoints.well.domain.ArdAlarmpointsWell; import com.ruoyi.alarmpoints.well.domain.ArdAlarmpointsWellDeptVo; 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 com.ruoyi.utils.gis.GisUtil; 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 selectArdAlarmpointsWellList(ArdAlarmpointsWell ardAlarmpointsWell) { return ardAlarmpointsWellMapper.selectArdAlarmpointsWellList(ardAlarmpointsWell); } /** * @param ardAlarmpointsWell 按井编号查询 * @return */ @Override @DataScope(deptAlias = "d", userAlias = "u") public List selectArdAlarmpointsWellByWellIdLike(ArdAlarmpointsWell ardAlarmpointsWell) { return ardAlarmpointsWellMapper.selectArdAlarmpointsWellByWellIdLike(ardAlarmpointsWell); } /** * 新增井管理 * * @param ardAlarmpointsWell 井管理 * @return 结果 */ @Override @Transactional public int insertArdAlarmpointsWell(ArdAlarmpointsWell ardAlarmpointsWell) { boolean wellIdExists = checkWellIdExists(ardAlarmpointsWell); 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 = checkWellIdExists(ardAlarmpointsWell); 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 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("
" + successNum + "、井号 " + well.getWellId() + " 导入成功"); } else if (isUpdateSupport) { BeanValidators.validateWithException(validator, well); checkWellAllowed(well); checkWellDataScope(well.getUserId()); well.setUpdateBy(operName); this.updateArdAlarmpointsWell(well); successNum++; successMsg.append("
" + successNum + "、井号 " + well.getWellId() + " 更新成功"); } else { failureNum++; failureMsg.append("
" + failureNum + "、井号 " + well.getWellId() + " 已存在"); } } catch (Exception e) { failureNum++; String msg = "
" + 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 wells = SpringUtils.getAopProxy(this).selectArdAlarmpointsWellList(well); if (StringUtils.isEmpty(wells)) { throw new ServiceException("没有权限访问井数据!"); } } } /** * 核对井号是否唯一 * * @param well 需要核对的井 * @return 结果 */ @Override public Boolean checkWellIdExists(ArdAlarmpointsWell well) { // 校验:wellId String id = (well.getId() == null) ? "" : well.getId(); String wellId = well.getWellId(); ArdAlarmpointsWell ardAlarmpointsWell = ardAlarmpointsWellMapper.checkWellIdExists(wellId); String infoId = (ardAlarmpointsWell == null) ? "" : (ardAlarmpointsWell.getId()); if ((ardAlarmpointsWell != null) && !((infoId).equals(id))) { //已存在 return true; } return false; } @Override public List wellByDeptList(List deptList) { QueryWrapper 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 wellList(List deptList) { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.select("id", "well_id", "oil_production", "longitude", "latitude", "altitude").in("dept_id", deptList); return ardAlarmpointsWellMapper.selectList(queryWrapper); } @Override public PageInfo conditionList(ArdAlarmpointsWellParam ardAlarmpointsWellParam) { Integer pageNum = ardAlarmpointsWellParam.getPageNum(); Integer pageSize = ardAlarmpointsWellParam.getPageSize(); PageHelper.startPage(pageNum, pageSize); QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.in("dept_id", ardAlarmpointsWellParam.getDeptList()) .like(!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()); List list = ardAlarmpointsWellMapper.selectList(queryWrapper); return new PageInfo<>(list); } @Override public List wellListDept(List deptList) { return ardAlarmpointsWellMapper.wellListDept(deptList); } @Override public List getNearbyWellList(Long deptId, Double[] position, Integer range) { List nearbyWellList = new ArrayList<>(); try { ArdAlarmpointsWell ardAlarmpointsWell = new ArdAlarmpointsWell(); ardAlarmpointsWell.setDeptId(deptId); List ardAlarmpointsWells = ardAlarmpointsWellMapper.selectArdAlarmpointsWellList(ardAlarmpointsWell); if (ardAlarmpointsWells.size() > 0) { for (ArdAlarmpointsWell well : ardAlarmpointsWells) { Double longitude = well.getLongitude(); Double latitude = well.getLatitude(); if (longitude != null && latitude != null) { double distance = GisUtil.getDistance(position, new Double[]{longitude, latitude}); if (distance <= range) { nearbyWellList.add(well); } } } } } catch (Exception ex) { log.error("获取附近井异常:" + ex.getMessage()); } return nearbyWellList; } }