package com.hotwj.platform.noticeManagerment.systemNotification.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotwj.platform.driverManagement.driver.domain.HotDriver;
import com.hotwj.platform.driverManagement.driver.mapper.HotDriverMapper;
import com.hotwj.platform.noticeManagerment.systemNotification.domain.HotSystemNotification;
import com.hotwj.platform.noticeManagerment.systemNotification.domain.bo.HotSystemNotificationBo;
import com.hotwj.platform.noticeManagerment.systemNotification.domain.bo.HotSystemNotificationGroupBo;
import com.hotwj.platform.noticeManagerment.systemNotification.domain.vo.HotSystemNotificationVo;
import com.hotwj.platform.noticeManagerment.systemNotification.mapper.HotSystemNotificationMapper;
import com.hotwj.platform.noticeManagerment.systemNotification.service.IHotSystemNotificationService;
import com.hotwj.platform.resourceManagement.company.service.ISysUserLoginPortService;
import com.hotwj.platform.resourceManagement.companySafetyManager.domain.HotCompanySafetyManager;
import com.hotwj.platform.resourceManagement.companySafetyManager.mapper.HotCompanySafetyManagerMapper;
import com.hotwj.platform.resourceManagement.personnelConfig.domain.HotPersonnelConfig;
import com.hotwj.platform.resourceManagement.personnelConfig.mapper.HotPersonnelConfigMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.json.utils.JsonUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.sse.dto.SseMessageDto;
import org.dromara.common.sse.utils.SseMessageUtils;
import org.dromara.system.domain.vo.SysUserVo;
import org.dromara.system.service.ISysUserService;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 系统通知Service业务层处理
 *
 * @author shihongwei
 * @date 2026-01-07
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HotSystemNotificationServiceImpl implements IHotSystemNotificationService {

    private final HotSystemNotificationMapper baseMapper;
    private final HotDriverMapper driverMapper;
    private final HotCompanySafetyManagerMapper companySafetyManagerMapper;
    private final HotPersonnelConfigMapper personnelConfigMapper;
    private final ISysUserService sysUserService;

    /**
     * 查询系统通知
     *
     * @param id 主键
     * @return 系统通知
     */
    @Override
    public HotSystemNotificationVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询系统通知列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 系统通知分页列表
     */
    @Override
    public TableDataInfo<HotSystemNotificationVo> queryPageList(HotSystemNotificationBo bo, PageQuery pageQuery) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        assert loginUser != null;
        String businessUserId = loginUser.getBusinessUserId();
        if (StringUtils.equals(loginUser.getLoginPort(), ISysUserLoginPortService.DRIVER_PORT)) {
            bo.setReceiverId(businessUserId);
        }
        if (StringUtils.equals(loginUser.getLoginPort(), ISysUserLoginPortService.ENTERPRISE_PORT)) {
            bo.setCompanyId(loginUser.getCompanyId());
        }

        LambdaQueryWrapper<HotSystemNotification> lqw = buildQueryWrapper(bo);
        Page<HotSystemNotificationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<HotSystemNotificationVo> queryPageListByType(HotSystemNotificationBo bo, PageQuery pageQuery) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        assert loginUser != null;
        String businessUserId = loginUser.getBusinessUserId();
        if (StringUtils.equals(loginUser.getLoginPort(), ISysUserLoginPortService.DRIVER_PORT)) {
            bo.setReceiverId(businessUserId);
        }
        if (StringUtils.equals(loginUser.getLoginPort(), ISysUserLoginPortService.ENTERPRISE_PORT)) {
            bo.setCompanyId(loginUser.getCompanyId());
        }

        LambdaQueryWrapper<HotSystemNotification> lqw = buildQueryWrapper(bo);
        Page<HotSystemNotificationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的系统通知列表
     *
     * @param bo 查询条件
     * @return 系统通知列表
     */
    @Override
    public List<HotSystemNotificationVo> queryList(HotSystemNotificationBo bo) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        assert loginUser != null;
        String businessUserId = loginUser.getBusinessUserId();
        if (StringUtils.equals(loginUser.getLoginPort(), ISysUserLoginPortService.DRIVER_PORT)) {
            bo.setReceiverId(businessUserId);
        }
        if (StringUtils.equals(loginUser.getLoginPort(), ISysUserLoginPortService.ENTERPRISE_PORT)) {
            bo.setCompanyId(loginUser.getCompanyId());
        }
        LambdaQueryWrapper<HotSystemNotification> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HotSystemNotification> buildQueryWrapper(HotSystemNotificationBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HotSystemNotification> lqw = Wrappers.lambdaQuery();
        lqw.orderByDesc(HotSystemNotification::getSendTime);
        Object beginTime = params == null ? null : params.get("beginTime");
        Object endTime = params == null ? null : params.get("endTime");
        if (beginTime != null && endTime != null) {
            lqw.between(HotSystemNotification::getSendTime, beginTime, endTime);
        } else if (beginTime != null) {
            lqw.ge(HotSystemNotification::getSendTime, beginTime);
        } else if (endTime != null) {
            lqw.le(HotSystemNotification::getSendTime, endTime);
        }
        lqw.eq(StringUtils.isNotBlank(bo.getLevel()), HotSystemNotification::getLevel, bo.getLevel());
        lqw.eq(StringUtils.isNotBlank(bo.getSourceType()), HotSystemNotification::getSourceType, bo.getSourceType());
        if (StringUtils.isNotBlank(bo.getSenderType())) {
            lqw.in(HotSystemNotification::getSenderType, Arrays.asList(bo.getSenderType().split(",")));
        }
        lqw.eq(bo.getSenderId() != null, HotSystemNotification::getSenderId, bo.getSenderId());
        lqw.like(StringUtils.isNotBlank(bo.getSenderName()), HotSystemNotification::getSenderName, bo.getSenderName());
        lqw.like(StringUtils.isNotBlank(bo.getSenderMobile()), HotSystemNotification::getSenderMobile, bo.getSenderMobile());
        lqw.eq(bo.getSendTime() != null, HotSystemNotification::getSendTime, bo.getSendTime());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), HotSystemNotification::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getReceiverType()), HotSystemNotification::getReceiverType, bo.getReceiverType());
        lqw.eq(StringUtils.isNotBlank(bo.getReceiverId()), HotSystemNotification::getReceiverId, bo.getReceiverId());
        lqw.like(StringUtils.isNotBlank(bo.getReceiverName()), HotSystemNotification::getReceiverName, bo.getReceiverName());
        lqw.eq(bo.getCompanyId() != null, HotSystemNotification::getCompanyId, bo.getCompanyId());
        lqw.eq(bo.getReceiveTime() != null, HotSystemNotification::getReceiveTime, bo.getReceiveTime());
        lqw.like(StringUtils.isNotBlank(bo.getContent()), HotSystemNotification::getContent, bo.getContent());
        lqw.eq(bo.getIsDeleted() != null, HotSystemNotification::getIsDeleted, bo.getIsDeleted());
        return lqw;
    }

    /**
     * 新增系统通知
     *
     * @param bos 系统通知
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HotSystemNotificationGroupBo bos) {

        if ("GOVERNMENT".equals(bos.getSenderType())) {
            if (bos.getSenderId() != null) {
                HotPersonnelConfig config = personnelConfigMapper.selectById(bos.getSenderId());
                if (config != null) {
                    if (StringUtils.isBlank(bos.getSenderName())) {
                        bos.setSenderName(config.getStaffName());
                    }
                    bos.setSenderMobile(config.getMobile());
                }
            }
        } else if ("COMPANY".equals(bos.getSenderType())) {
            if (bos.getSenderId() != null) {
                HotCompanySafetyManager manager = companySafetyManagerMapper.selectById(bos.getSenderId());
                if (manager != null) {
                    if (StringUtils.isBlank(bos.getSenderName())) {
                        bos.setSenderName(manager.getName());
                    }
                    bos.setSenderMobile(manager.getPhone());
                }
            }
        }

        if ((bos.getReceiverIds() == null || bos.getReceiverIds().isEmpty()) && StringUtils.isEmpty(bos.getReceiverId())) {
            throw new IllegalArgumentException("接受人ID列表不能为空");
        }
        if (bos.getReceiverIds() != null && !bos.getReceiverIds().isEmpty()) {
            List<HotSystemNotification> entityList = new ArrayList<>();
            for (String receiverId : bos.getReceiverIds()) {
                HotSystemNotificationBo singleBo = new HotSystemNotificationBo();
                singleBo.setLevel(bos.getLevel());
                singleBo.setContent(bos.getContent());
                singleBo.setSourceType(bos.getSourceType());
                singleBo.setSenderType(bos.getSenderType());
                singleBo.setSenderId(bos.getSenderId());
                singleBo.setSenderName(bos.getSenderName());
                singleBo.setSenderMobile(bos.getSenderMobile());
                singleBo.setReceiverType(bos.getReceiverType());
                singleBo.setReceiverId(receiverId);

                if ("政府端".equals(singleBo.getReceiverType())) {
                    if (StringUtils.isNotBlank(singleBo.getReceiverId()) && (StringUtils.isBlank(singleBo.getReceiverName()) || singleBo.getCompanyId() == null)) {
                        try {
                            HotPersonnelConfig config = personnelConfigMapper.selectById(Long.valueOf(singleBo.getReceiverId()));
                            if (config != null) {
                                if (StringUtils.isBlank(singleBo.getReceiverName())) {
                                    singleBo.setReceiverName(config.getStaffName());
                                }
                                if (singleBo.getCompanyId() == null) {
                                    singleBo.setCompanyId(config.getUnitId());
                                }
                            }
                        } catch (NumberFormatException e) {
                            log.warn("政府端人员ID格式错误: {}", singleBo.getReceiverId());
                        }
                    }
                } else if ("管理员".equals(singleBo.getReceiverType())) {
                    if (StringUtils.isNotBlank(singleBo.getReceiverId()) && (StringUtils.isBlank(singleBo.getReceiverName()) || singleBo.getCompanyId() == null)) {
                        try {
                            Long id = Long.valueOf(singleBo.getReceiverId());
                            HotCompanySafetyManager manager = companySafetyManagerMapper.selectById(id);
                            if (manager != null) {
                                if (StringUtils.isBlank(singleBo.getReceiverName())) {
                                    singleBo.setReceiverName(manager.getName());
                                }
                                if (singleBo.getCompanyId() == null) {
                                    singleBo.setCompanyId(manager.getCompanyId());
                                }
                            }
                        } catch (NumberFormatException e) {
                            log.warn("企业人员ID格式错误: {}", singleBo.getReceiverId());
                        }
                    }
                } else if ("驾驶员".equals(singleBo.getReceiverType())) {
                    if (StringUtils.isNotBlank(singleBo.getReceiverId()) && (StringUtils.isBlank(singleBo.getReceiverName()) || singleBo.getCompanyId() == null)) {
                        HotDriver driver = driverMapper.selectById(singleBo.getReceiverId());
                        if (driver != null) {
                            if (StringUtils.isBlank(singleBo.getReceiverName())) {
                                singleBo.setReceiverName(driver.getName());
                            }
                            if (singleBo.getCompanyId() == null) {
                                singleBo.setCompanyId(driver.getCompanyId());
                            }
                        }
                    }
                } else if ("企业回复".equals(singleBo.getReceiverType())) {
                    if (StringUtils.isNotBlank(singleBo.getReceiverId()) && (StringUtils.isBlank(singleBo.getReceiverName()) || singleBo.getCompanyId() == null)) {
                        singleBo.setCompanyId(Long.valueOf(singleBo.getReceiverId()));
                    }
                    if (singleBo.getReceiverId().equals("1")) {
                        singleBo.setReceiverName("总部端");
                    } else {
                        singleBo.setReceiverName("政府端");
                    }
                } else if ("政府审核通过".equals(singleBo.getReceiverType())) {
                    singleBo.setCompanyId(1L);
                    singleBo.setReceiverName("总部端");
                } else {
                    if (StringUtils.isNotBlank(singleBo.getReceiverId()) && (StringUtils.isBlank(singleBo.getReceiverName()) || singleBo.getCompanyId() == null)) {
                        try {
                            Long id = Long.valueOf(singleBo.getReceiverId());
                            HotCompanySafetyManager manager = companySafetyManagerMapper.selectById(id);
                            if (manager != null) {
                                if (StringUtils.isBlank(singleBo.getReceiverName())) {
                                    singleBo.setReceiverName(manager.getName());
                                }
                                if (singleBo.getCompanyId() == null) {
                                    singleBo.setCompanyId(manager.getCompanyId());
                                }
                            } else {
                                HotDriver driver = driverMapper.selectById(singleBo.getReceiverId());
                                if (driver != null) {
                                    if (StringUtils.isBlank(singleBo.getReceiverName())) {
                                        singleBo.setReceiverName(driver.getName());
                                    }
                                    if (singleBo.getCompanyId() == null) {
                                        singleBo.setCompanyId(driver.getCompanyId());
                                    }
                                }
                            }
                        } catch (NumberFormatException e) {
                            log.warn("企业人员ID格式错误: {}", singleBo.getReceiverId());
                        }
                    }
                }

                if (singleBo.getSendTime() == null) {
                    singleBo.setSendTime(new Date());
                }
                if (StringUtils.isBlank(singleBo.getStatus())) {
                    singleBo.setStatus("未读");
                }

                HotSystemNotification entity = MapstructUtils.convert(singleBo, HotSystemNotification.class);
                validEntityBeforeSave(entity);
                entityList.add(entity);
            }
            boolean result = baseMapper.insertBatch(entityList);
            if (result) {
                entityList.forEach(entity -> sendSseMessage(entity, false));
            }
            return result;
        } else {
            String receiverId = bos.getReceiverId();
            HotSystemNotificationBo singleBo = new HotSystemNotificationBo();
            singleBo.setLevel(bos.getLevel());
            singleBo.setContent(bos.getContent());
            singleBo.setSourceType(bos.getSourceType());
            singleBo.setSenderType(bos.getSenderType());
            singleBo.setSenderId(bos.getSenderId());
            singleBo.setSenderName(bos.getSenderName());
            singleBo.setSenderMobile(bos.getSenderMobile());
            singleBo.setReceiverType(bos.getReceiverType());
            singleBo.setReceiverId(receiverId);

            if ("政府端".equals(singleBo.getReceiverType())) {
                if (StringUtils.isNotBlank(singleBo.getReceiverId()) && (StringUtils.isBlank(singleBo.getReceiverName()) || singleBo.getCompanyId() == null)) {
                    try {
                        HotPersonnelConfig config = personnelConfigMapper.selectById(Long.valueOf(singleBo.getReceiverId()));
                        if (config != null) {
                            if (StringUtils.isBlank(singleBo.getReceiverName())) {
                                singleBo.setReceiverName(config.getStaffName());
                            }
                            if (singleBo.getCompanyId() == null) {
                                singleBo.setCompanyId(config.getUnitId());
                            }
                        }
                    } catch (NumberFormatException e) {
                        log.warn("政府端人员ID格式错误: {}", singleBo.getReceiverId());
                    }
                }
            } else if ("管理员".equals(singleBo.getReceiverType())) {
                if (StringUtils.isNotBlank(singleBo.getReceiverId()) && (StringUtils.isBlank(singleBo.getReceiverName()) || singleBo.getCompanyId() == null)) {
                    try {
                        Long id = Long.valueOf(singleBo.getReceiverId());
                        HotCompanySafetyManager manager = companySafetyManagerMapper.selectById(id);
                        if (manager != null) {
                            if (StringUtils.isBlank(singleBo.getReceiverName())) {
                                singleBo.setReceiverName(manager.getName());
                            }
                            if (singleBo.getCompanyId() == null) {
                                singleBo.setCompanyId(manager.getCompanyId());
                            }
                        }
                    } catch (NumberFormatException e) {
                        log.warn("企业人员ID格式错误: {}", singleBo.getReceiverId());
                    }
                }
            } else if ("驾驶员".equals(singleBo.getReceiverType())) {
                if (StringUtils.isNotBlank(singleBo.getReceiverId()) && (StringUtils.isBlank(singleBo.getReceiverName()) || singleBo.getCompanyId() == null)) {
                    HotDriver driver = driverMapper.selectById(singleBo.getReceiverId());
                    if (driver != null) {
                        if (StringUtils.isBlank(singleBo.getReceiverName())) {
                            singleBo.setReceiverName(driver.getName());
                        }
                        if (singleBo.getCompanyId() == null) {
                            singleBo.setCompanyId(driver.getCompanyId());
                        }
                    }
                }
            } else {
                if (StringUtils.isNotBlank(singleBo.getReceiverId()) && (StringUtils.isBlank(singleBo.getReceiverName()) || singleBo.getCompanyId() == null)) {
                    try {
                        Long id = Long.valueOf(singleBo.getReceiverId());
                        HotCompanySafetyManager manager = companySafetyManagerMapper.selectById(id);
                        if (manager != null) {
                            if (StringUtils.isBlank(singleBo.getReceiverName())) {
                                singleBo.setReceiverName(manager.getName());
                            }
                            if (singleBo.getCompanyId() == null) {
                                singleBo.setCompanyId(manager.getCompanyId());
                            }
                            singleBo.setReceiverType("管理员");
                        }
                    } catch (NumberFormatException e) {
                        HotDriver driver = driverMapper.selectById(singleBo.getReceiverId());
                        if (driver != null) {
                            if (StringUtils.isBlank(singleBo.getReceiverName())) {
                                singleBo.setReceiverName(driver.getName());
                            }
                            if (singleBo.getCompanyId() == null) {
                                singleBo.setCompanyId(driver.getCompanyId());
                            }
                            singleBo.setReceiverType("驾驶员");
                        }
                    }
                }
            }

            if (singleBo.getSendTime() == null) {
                singleBo.setSendTime(new Date());
            }
            if (StringUtils.isBlank(singleBo.getStatus())) {
                singleBo.setStatus("未读");
            }

            HotSystemNotification entity = MapstructUtils.convert(singleBo, HotSystemNotification.class);
            validEntityBeforeSave(entity);
            int rows = baseMapper.insert(entity);
            if (rows > 0) {
                sendSseMessage(entity, false);
            }
            return rows > 0;
        }

    }

    /**
     * 发送SSE消息
     */
    private void sendSseMessage(HotSystemNotification entity, boolean isSilent) {
        try {
            Long userId = null;
            Set<Long> userIds = new LinkedHashSet<>();
            if ("政府端".equals(entity.getReceiverType())) {
                try {
                    HotPersonnelConfig config = personnelConfigMapper.selectById(Long.valueOf(entity.getReceiverId()));
                    if (config != null && StringUtils.isNotBlank(config.getMobile())) {
                        SysUserVo user = sysUserService.selectUserByPhonenumber(config.getMobile());
                        if (user != null) {
                            userId = user.getUserId();
                            userIds.add(userId);
                        }
                    }
                } catch (NumberFormatException e) {
                    log.warn("发送SSE消息失败，政府端人员ID格式错误: {}", entity.getReceiverId());
                }
            } else if ("管理员".equals(entity.getReceiverType())) {
                try {
                    HotCompanySafetyManager manager = companySafetyManagerMapper.selectById(Long.valueOf(entity.getReceiverId()));
                    if (manager != null) {
                        userId = manager.getUserId();
                        if (userId != null) {
                            userIds.add(userId);
                        }
                    }
                } catch (NumberFormatException e) {
                    log.warn("发送SSE消息失败，管理员ID格式错误: {}", entity.getReceiverId());
                }
            } else if ("驾驶员".equals(entity.getReceiverType())) {
                HotDriver driver = driverMapper.selectById(entity.getReceiverId());
                if (driver != null && StringUtils.isNotBlank(driver.getPhone())) {
                    SysUserVo user = sysUserService.selectUserByPhonenumber(driver.getPhone());
                    if (user != null) {
                        userId = user.getUserId();
                        userIds.add(userId);
                    }
                }
            } else if ("企业回复".equals(entity.getReceiverType()) || "政府审核通过".equals(entity.getReceiverType())) {
                try {
                    Long unitId = Long.valueOf(entity.getReceiverId());
                    List<HotPersonnelConfig> configs = personnelConfigMapper.selectList(
                            Wrappers.<HotPersonnelConfig>lambdaQuery()
                                    .eq(HotPersonnelConfig::getUnitId, unitId)
                                    .eq(HotPersonnelConfig::getIsEnabled, 1L)
                                    .eq(HotPersonnelConfig::getIsDeleted, 0L)
                    );
                    if (configs != null) {
                        for (HotPersonnelConfig config : configs) {
                            if (config == null || StringUtils.isBlank(config.getMobile())) {
                                continue;
                            }
                            SysUserVo user = sysUserService.selectUserByPhonenumber(config.getMobile());
                            if (user != null && user.getUserId() != null) {
                                userIds.add(user.getUserId());
                            }
                        }
                    }
                } catch (NumberFormatException e) {
                    log.warn("发送SSE消息失败，单位ID格式错误 receiverType={} receiverId={}", entity.getReceiverType(), entity.getReceiverId());
                }
            }

            if (!userIds.isEmpty()) {
                HotSystemNotificationVo vo = MapstructUtils.convert(entity, HotSystemNotificationVo.class);
                SseMessageDto dto = new SseMessageDto();
                dto.setUserIds(new ArrayList<>(userIds));
                if (isSilent) {
                    dto.setMessage(JsonUtils.toJsonString(Map.of("isSilent", true)));
                } else {
                    dto.setMessage(JsonUtils.toJsonString(vo));
                }
                SseMessageUtils.publishMessage(dto);
            }
        } catch (Exception e) {
            log.error("发送SSE消息失败", e);
        }
    }

    /**
     * 修改系统通知
     *
     * @param bo 系统通知
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HotSystemNotificationBo bo) {
        HotSystemNotification update = MapstructUtils.convert(bo, HotSystemNotification.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HotSystemNotification entity) {

        // 1. 参数判断
        if (StringUtils.isBlank(entity.getLevel())) {
            throw new IllegalArgumentException("等级不能为空");
        }
        if (StringUtils.isBlank(entity.getContent())) {
            throw new IllegalArgumentException("消息内容不能为空");
        }
        if (StringUtils.isBlank(entity.getSourceType())) {
            throw new IllegalArgumentException("来源类型不能为空");
        }
        if (StringUtils.isBlank(entity.getSenderType())) {
            throw new IllegalArgumentException("发送人类型不能为空");
        }
        if (StringUtils.isBlank(entity.getReceiverType())) {
            throw new IllegalArgumentException("接受人类型不能为空");
        }
        if (StringUtils.isBlank(entity.getReceiverId())) {
            throw new IllegalArgumentException("接受人ID不能为空");
        }
        if (entity.getCompanyId() == null) {
            throw new IllegalArgumentException("公司ID不能为空");
        }
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除系统通知信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }


    @Override
    public Boolean markRead(Long id) {
        LambdaUpdateWrapper<HotSystemNotification> uw = Wrappers.lambdaUpdate();
        uw.eq(HotSystemNotification::getId, id);
        uw.set(HotSystemNotification::getStatus, "已读");
        uw.set(HotSystemNotification::getReceiveTime, new Date());
        boolean success = baseMapper.update(null, uw) > 0;
        if (success) {
            HotSystemNotification entity = baseMapper.selectById(id);
            if (entity != null) {
                sendSseMessage(entity, true);
            }
        }
        return success;
    }
}
