package com.ruoyi.system.service.impl; import com.ruoyi.common.basic.PageInfo; import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.utils.StringUtils; import com.ruoyi.system.domain.SysDoorDevice; import com.ruoyi.system.domain.SysVisitArea; import com.ruoyi.system.dto.SysDoorDeviceAddRequest; import com.ruoyi.system.dto.SysDoorDeviceDeleteRequest; import com.ruoyi.system.dto.SysDoorDeviceDetailRequest; import com.ruoyi.system.dto.SysDoorDeviceEditRequest; import com.ruoyi.system.dto.SysDoorDeviceLogoutRequest; import com.ruoyi.system.dto.SysDoorDevicePageRequest; import com.ruoyi.system.dto.SysDoorDeviceStatusRequest; import com.ruoyi.system.mapper.SysDoorDeviceMapper; import com.ruoyi.system.mapper.SysVisitAreaMapper; import com.ruoyi.system.service.ISysDoorDeviceService; import com.ruoyi.system.utils.AreaIdUtils; import com.ruoyi.system.utils.DoorDeviceCredentialCrypto; import com.ruoyi.system.vo.SysDoorDeviceVO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; import java.util.Date; import java.util.List; import java.nio.charset.Charset; /** * 门禁设备服务实现 * * @author ruoyi */ @Service public class SysDoorDeviceServiceImpl implements ISysDoorDeviceService { private static final String ONLINE_STATUS_UNACTIVATED = "UNACTIVATED"; @Autowired private SysDoorDeviceMapper doorDeviceMapper; @Autowired private SysVisitAreaMapper visitAreaMapper; @Override public PageInfo pageList(SysDoorDevicePageRequest request) { SysDoorDevicePageRequest query = request == null ? new SysDoorDevicePageRequest() : request; query.setAreaId(StringUtils.trim(query.getAreaId())); query.setDeviceName(StringUtils.trim(query.getDeviceName())); query.setDeviceId(StringUtils.trim(query.getDeviceId())); query.setOnlineStatus(StringUtils.trim(query.getOnlineStatus())); PageInfo pageInfo = new PageInfo(query.getPageNum(), query.getPageSize()); List list = doorDeviceMapper.pageList(query, AreaIdUtils.parseLocalAreaId(query.getAreaId()), pageInfo); pageInfo.setRecords(list); return pageInfo; } @Override public SysDoorDeviceVO detail(SysDoorDeviceDetailRequest request) { Long doorId = getRequiredDoorId(request == null ? null : request.getDoorId()); SysDoorDeviceVO detail = doorDeviceMapper.selectVoById(doorId); if (detail == null) { throw new ServiceException("门禁设备不存在"); } return detail; } @Override public int add(SysDoorDeviceAddRequest request, String username) { if (request == null) { throw new ServiceException("门禁设备内容不能为空"); } SysDoorDevice doorDevice = buildDoorDevice(request.getAreaId(), request.getDeviceName(), request.getBrand(), request.getDeviceId(), request.getRemark(), request.getEnabled(), null); fillConnectionSettings(doorDevice, request.getDevicePort(), request.getLoginUsername(), request.getLoginPassword(), request.getDoorNo(), request.getTimeSectionNo(), request.getCharset(), null, true); doorDevice.setOnlineStatus(ONLINE_STATUS_UNACTIVATED); doorDevice.setCreateBy(username); return doorDeviceMapper.insertDoorDevice(doorDevice); } @Override public int edit(SysDoorDeviceEditRequest request, String username) { if (request == null) { throw new ServiceException("门禁设备内容不能为空"); } Long doorId = getRequiredDoorId(request.getDoorId()); SysDoorDevice existing = doorDeviceMapper.selectById(doorId); if (existing == null) { throw new ServiceException("门禁设备不存在"); } SysDoorDevice doorDevice = buildDoorDevice(request.getAreaId(), request.getDeviceName(), request.getBrand(), request.getDeviceId(), request.getRemark(), request.getEnabled(), doorId); fillConnectionSettings(doorDevice, request.getDevicePort(), request.getLoginUsername(), request.getLoginPassword(), request.getDoorNo(), request.getTimeSectionNo(), request.getCharset(), existing.getLoginPasswordCipher(), StringUtils.isEmpty(existing.getLoginPasswordCipher())); doorDevice.setDoorId(doorId); doorDevice.setUpdateBy(username); return doorDeviceMapper.updateDoorDevice(doorDevice); } @Override public int changeStatus(SysDoorDeviceStatusRequest request, String username) { List doorIds = getRequiredDoorIds(request == null ? null : request.getDoorIds()); if (request.getEnabled() == null) { throw new ServiceException("启用状态不能为空"); } return doorDeviceMapper.updateEnabled(doorIds, request.getEnabled(), username); } @Override public int logout(SysDoorDeviceLogoutRequest request, String username) { List doorIds = getRequiredDoorIds(request == null ? null : request.getDoorIds()); return doorDeviceMapper.updateOnlineStatus(doorIds, ONLINE_STATUS_UNACTIVATED, username); } @Override public int delete(SysDoorDeviceDeleteRequest request, String username) { List doorIds = getRequiredDoorIds(request == null ? null : request.getDoorIds()); return doorDeviceMapper.deleteByIds(doorIds, username); } private SysDoorDevice buildDoorDevice(Long areaId, String deviceName, String brand, String deviceId, String remark, Boolean enabled, Long excludeDoorId) { Long requiredAreaId = getRequiredAreaId(areaId); SysVisitArea area = visitAreaMapper.selectById(requiredAreaId); if (area == null) { throw new ServiceException("所属区域不存在"); } String requiredDeviceName = getRequiredText(deviceName, "设备名称不能为空"); String requiredBrand = getRequiredText(brand, "品牌不能为空"); String requiredDeviceId = getRequiredText(deviceId, "设备ID不能为空"); if (doorDeviceMapper.checkDeviceIdUnique(requiredDeviceId, excludeDoorId) != null) { throw new ServiceException("设备ID已存在"); } SysDoorDevice doorDevice = new SysDoorDevice(); doorDevice.setAreaId(requiredAreaId); doorDevice.setDeviceName(requiredDeviceName); doorDevice.setBrand(requiredBrand); doorDevice.setDeviceId(requiredDeviceId); doorDevice.setRemark(StringUtils.trim(remark)); doorDevice.setEnabled(enabled == null ? Boolean.TRUE : enabled); return doorDevice; } private void fillConnectionSettings(SysDoorDevice doorDevice, Integer devicePort, String loginUsername, String loginPassword, Integer doorNo, Integer timeSectionNo, String charset, String existingPasswordCipher, boolean requirePassword) { int resolvedPort = devicePort == null ? 37777 : devicePort; if (resolvedPort <= 0 || resolvedPort > 65535) { throw new ServiceException("设备端口必须在1到65535之间"); } int resolvedDoorNo = doorNo == null ? 0 : doorNo; if (resolvedDoorNo < 0) { throw new ServiceException("门通道序号不能小于0"); } int resolvedTimeSectionNo = timeSectionNo == null ? 255 : timeSectionNo; if (resolvedTimeSectionNo < 0 || resolvedTimeSectionNo > 255) { throw new ServiceException("时间模板序号必须在0到255之间"); } String resolvedCharset = StringUtils.isEmpty(StringUtils.trim(charset)) ? "GBK" : StringUtils.trim(charset); try { Charset.forName(resolvedCharset); } catch (Exception e) { throw new ServiceException("设备姓名编码无效"); } String password = StringUtils.trim(loginPassword); if (requirePassword && StringUtils.isEmpty(password)) { throw new ServiceException("设备登录密码不能为空"); } doorDevice.setDevicePort(resolvedPort); doorDevice.setLoginUsername(StringUtils.isEmpty(StringUtils.trim(loginUsername)) ? "admin" : StringUtils.trim(loginUsername)); doorDevice.setLoginPasswordCipher(StringUtils.isEmpty(password) ? existingPasswordCipher : DoorDeviceCredentialCrypto.encrypt(password)); doorDevice.setDoorNo(resolvedDoorNo); doorDevice.setTimeSectionNo(resolvedTimeSectionNo); doorDevice.setCharset(resolvedCharset); } private Long getRequiredAreaId(Long areaId) { if (areaId == null || areaId <= 0) { throw new ServiceException("所属区域不能为空"); } return areaId; } private Long getRequiredDoorId(Long doorId) { if (doorId == null || doorId <= 0) { throw new ServiceException("门禁设备主键不能为空"); } return doorId; } private List getRequiredDoorIds(List doorIds) { if (CollectionUtils.isEmpty(doorIds)) { throw new ServiceException("门禁设备主键集合不能为空"); } for (Long doorId : doorIds) { getRequiredDoorId(doorId); } return doorIds; } private String getRequiredText(String value, String message) { String result = StringUtils.trim(value); if (StringUtils.isEmpty(result)) { throw new ServiceException(message); } return result; } }