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.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; /** * 门禁设备服务实现 * * @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.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, 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); 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()); if (doorDeviceMapper.selectById(doorId) == null) { throw new ServiceException("门禁设备不存在"); } SysDoorDevice doorDevice = buildDoorDevice(request.getAreaId(), request.getDeviceName(), request.getBrand(), request.getDeviceId(), request.getRemark(), request.getEnabled(), doorId); 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 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; } }