package com.sandu.ximon.admin.service; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.github.pagehelper.PageHelper; import com.sandu.common.execption.BusinessException; import com.sandu.common.object.BaseConditionVO; import com.sandu.common.service.impl.BaseServiceImpl; import com.sandu.common.util.SpringContextHolder; import com.sandu.ximon.admin.param.InterphoneHostParam; import com.sandu.ximon.admin.security.SecurityUtils; import com.sandu.ximon.admin.utils.StoreOperationRecordsUtils; import com.sandu.ximon.dao.bo.InterphoneHostBo; import com.sandu.ximon.dao.domain.InterphoneHost; import com.sandu.ximon.dao.domain.InterphoneHostSubPole; import com.sandu.ximon.dao.enums.PoleBindingEnums; import com.sandu.ximon.dao.mapper.InterphoneHostMapper; import lombok.AllArgsConstructor; import org.springframework.beans.BeanUtils; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.List; @Service @AllArgsConstructor public class InterphoneHostService extends BaseServiceImpl { private final InterphoneHostMapper interphoneHostMapper; private final InterphoneHostSubService interphoneHostSubService; /** * 添加主机 */ public boolean addHost(InterphoneHostParam interphoneHostParam) { List list = list(Wrappers.lambdaQuery(InterphoneHost.class).eq(InterphoneHost::getHostMac, interphoneHostParam.getHostMac())); if (list.size() > 0) { throw new BusinessException("该设备已存在"); } InterphoneHost interphoneHost = new InterphoneHost(); BeanUtils.copyProperties(interphoneHostParam, interphoneHost); boolean save = save(interphoneHost); /** * 添加一键求助主机 日志记录开始 */ List listCode = new ArrayList<>(1); listCode.add(interphoneHost.getHostMac()); String content = "{ 设备id:" + interphoneHost.getHostId() + "设备code:" + interphoneHost.getHostMac() + "}"; StoreOperationRecordsUtils.storeOperationData(listCode, null, "添加一键求助主机", content); /** * 添加一键求助主机 日志记录结束 */ return save; } /** * 修改主机 */ @Transactional(rollbackFor = Exception.class) public boolean updateHost(Integer id, InterphoneHostParam interphoneHostParam) { InterphoneHost byId = getById(id); if (byId == null) { return false; } if (SecurityUtils.getClientId() != null) { boolean belong = SpringContextHolder.getBean(PoleBindingService.class).isBelong(byId.getHostMac(), PoleBindingEnums.FOR_HELP); if (!belong) { throw new BusinessException("该设备不属于您,不能修改设备信息"); } } InterphoneHost interphoneHost = new InterphoneHost(); interphoneHost.setHostId(id); BeanUtils.copyProperties(interphoneHostParam, interphoneHost); /** * 编辑一键求助主机 日志记录开始 */ List listCode = new ArrayList<>(1); listCode.add(interphoneHost.getHostMac()); String content = "{ 设备id:" + interphoneHost.getHostId() + "设备code:" + interphoneHost.getHostMac() + "}"; StoreOperationRecordsUtils.storeOperationData(listCode, null, "编辑一键求助主机", content); /** * 编辑一键求助主机 日志记录结束 */ boolean b = updateById(interphoneHost); List list = list(Wrappers.lambdaQuery(InterphoneHost.class).eq(InterphoneHost::getHostMac, interphoneHostParam.getHostMac())); if (list.size() > 1) { throw new BusinessException("请检查设备mac是否重复"); } return b; } /** * 删除主机 * * @param id * @return */ public boolean deleteHost(Integer id) { InterphoneHost byId = getById(id); if (byId == null) { throw new BusinessException("主机不存在!"); } InterphoneHostSubPole one = interphoneHostSubService.getOne(Wrappers.lambdaQuery(InterphoneHostSubPole.class).eq(InterphoneHostSubPole::getHostId, id)); if (one != null) { throw new BusinessException("该主机存在绑定关系,不能删除"); } /** * 删除一键求助主机 日志记录开始 */ List listCode = new ArrayList<>(1); listCode.add(byId.getHostMac()); String content = "{ 设备id:" + byId.getHostId() + "设备code:" + byId.getHostMac() + "}"; StoreOperationRecordsUtils.storeOperationData(listCode, null, "删除一键求助主机", content); /** * 删除一键求助主机 日志记录结束 */ return removeById(id); } /** * 查询主机 * * @param id * @return */ public InterphoneHost getHost(Integer id) { InterphoneHost byId = getById(id); if (byId == null) { return null; } return byId; } /** * 查询主机列表 */ public List getInterphoneHostList(BaseConditionVO baseConditionVO, String keyword) { if (baseConditionVO != null) { PageHelper.startPage(baseConditionVO.getPageNo(), baseConditionVO.getPageSize()); } List list; if (SecurityUtils.getClientId() == null) { list = interphoneHostMapper.getInterphoneHostList(keyword, null); } else { list = interphoneHostMapper.getInterphoneHostList(keyword, SecurityUtils.getUserId()); } return list; } }