package com.hotwj.platform.noticeManagerment.adminNotice.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.adminNotice.domain.HotAdminNotice;
import com.hotwj.platform.noticeManagerment.adminNotice.domain.bo.HotAdminNoticeBo;
import com.hotwj.platform.noticeManagerment.adminNotice.domain.vo.HotAdminNoticeVo;
import com.hotwj.platform.noticeManagerment.adminNotice.mapper.HotAdminNoticeMapper;
import com.hotwj.platform.noticeManagerment.adminNotice.service.IHotAdminNoticeService;
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-13
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HotAdminNoticeServiceImpl implements IHotAdminNoticeService {

    private final HotAdminNoticeMapper baseMapper;

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

    /**
     * 分页查询行管通知列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 行管通知分页列表
     */
    @Override
    public TableDataInfo<HotAdminNoticeVo> queryPageList(HotAdminNoticeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HotAdminNotice> lqw = buildQueryWrapper(bo);
        Page<HotAdminNoticeVo> result = baseMapper.selectPageList(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 分页查询通知收到情况列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 通知收到情况分页列表
     */
    @Override
    public TableDataInfo<HotAdminNoticeVo> queryReceiptStatusList(HotAdminNoticeBo bo, PageQuery pageQuery) {
        Page<HotAdminNoticeVo> result = baseMapper.selectReceiptStatusList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的行管通知列表
     *
     * @param bo 查询条件
     * @return 行管通知列表
     */
    @Override
    public List<HotAdminNoticeVo> queryList(HotAdminNoticeBo bo) {
        LambdaQueryWrapper<HotAdminNotice> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HotAdminNotice> buildQueryWrapper(HotAdminNoticeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HotAdminNotice> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HotAdminNotice::getId);
        lqw.eq(bo.getCompanyId() != null, HotAdminNotice::getCompanyId, bo.getCompanyId());
        lqw.eq(bo.getNoticeId() != null, HotAdminNotice::getNoticeId, bo.getNoticeId());
        lqw.like(StringUtils.isNotBlank(bo.getFileName()), HotAdminNotice::getFileName, bo.getFileName());
        lqw.eq(bo.getNotifyType() != null, HotAdminNotice::getNotifyType, bo.getNotifyType());
        lqw.eq(StringUtils.isNotBlank(bo.getAttachmentUrl()), HotAdminNotice::getAttachmentUrl, bo.getAttachmentUrl());
        lqw.eq(bo.getIssueTime() != null, HotAdminNotice::getIssueTime, bo.getIssueTime());
        lqw.eq(bo.getIsReceived() != null, HotAdminNotice::getIsReceived, bo.getIsReceived());
        lqw.eq(bo.getReceiveTime() != null, HotAdminNotice::getReceiveTime, bo.getReceiveTime());
        lqw.eq(bo.getDeadlineTime() != null, HotAdminNotice::getDeadlineTime, bo.getDeadlineTime());
        lqw.eq(StringUtils.isNotBlank(bo.getReplyAttachmentUrl()), HotAdminNotice::getReplyAttachmentUrl, bo.getReplyAttachmentUrl());
        lqw.eq(bo.getIsDeleted() != null, HotAdminNotice::getIsDeleted, bo.getIsDeleted());
        lqw.eq(bo.getStatus() != null, HotAdminNotice::getStatus, bo.getStatus());
        return lqw;
    }

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

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

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

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