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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotwj.platform.noticeManagerment.noticeRecord.domain.HotNoticeRecord;
import com.hotwj.platform.noticeManagerment.noticeRecord.domain.bo.HotNoticeRecordBo;
import com.hotwj.platform.noticeManagerment.noticeRecord.domain.vo.HotNoticeRecordVo;
import com.hotwj.platform.noticeManagerment.noticeRecord.mapper.HotNoticeRecordMapper;
import com.hotwj.platform.noticeManagerment.noticeRecord.service.IHotNoticeRecordService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 通知记录Service业务层处理
 *
 * @author shihongwei
 * @date 2026-01-14
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HotNoticeRecordServiceImpl implements IHotNoticeRecordService {

    private final HotNoticeRecordMapper baseMapper;

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

    /**
     * 分页查询通知记录列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 通知记录分页列表
     */
    @Override
    public TableDataInfo<HotNoticeRecordVo> queryPageList(HotNoticeRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HotNoticeRecord> lqw = buildQueryWrapper(bo);
        Page<HotNoticeRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的通知记录列表
     *
     * @param bo 查询条件
     * @return 通知记录列表
     */
    @Override
    public List<HotNoticeRecordVo> queryList(HotNoticeRecordBo bo) {
        LambdaQueryWrapper<HotNoticeRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HotNoticeRecord> buildQueryWrapper(HotNoticeRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HotNoticeRecord> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HotNoticeRecord::getId);
        lqw.eq(StringUtils.isNotBlank(bo.getNotifyChannel()), HotNoticeRecord::getNotifyChannel, bo.getNotifyChannel());
        lqw.eq(StringUtils.isNotBlank(bo.getNoticeType()), HotNoticeRecord::getNoticeType, bo.getNoticeType());
        lqw.eq(StringUtils.isNotBlank(bo.getNoticeTarget()), HotNoticeRecord::getNoticeTarget, bo.getNoticeTarget());
        lqw.eq(StringUtils.isNotBlank(bo.getNoticeTargetType()), HotNoticeRecord::getNoticeTargetType, bo.getNoticeTargetType());
        lqw.eq(StringUtils.isNotBlank(bo.getNoticeMobile()), HotNoticeRecord::getNoticeMobile, bo.getNoticeMobile());
        lqw.eq(bo.getNoticeTime() != null, HotNoticeRecord::getNoticeTime, bo.getNoticeTime());
        lqw.eq(bo.getNoticeUserId() != null, HotNoticeRecord::getNoticeUserId, bo.getNoticeUserId());
        lqw.like(StringUtils.isNotBlank(bo.getNoticeUserName()), HotNoticeRecord::getNoticeUserName, bo.getNoticeUserName());
        lqw.eq(bo.getIsDeleted() != null, HotNoticeRecord::getIsDeleted, bo.getIsDeleted());
        return lqw;
    }

    /**
     * 新增通知记录
     *
     * @param bo 通知记录
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HotNoticeRecordBo bo) {
        HotNoticeRecord add = MapstructUtils.convert(bo, HotNoticeRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

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

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HotNoticeRecord entity) {
        //TODO 做一些数据校验,如唯一约束
    }

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