package com.sundot.airport.system.service.impl;

import java.util.List;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sundot.airport.common.domain.BaseRead;
import com.sundot.airport.common.service.IBaseReadService;
import com.sundot.airport.common.utils.DateUtils;
import com.sundot.airport.system.mapper.BaseReadMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 已读Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-09
 */
@Service
public class BaseReadServiceImpl extends ServiceImpl<BaseReadMapper, BaseRead> implements IBaseReadService {
    @Autowired
    private BaseReadMapper baseReadMapper;

    /**
     * 查询已读
     *
     * @param id 已读主键
     * @return 已读
     */
    @Override
    public BaseRead selectBaseReadById(Long id) {
        return baseReadMapper.selectBaseReadById(id);
    }

    /**
     * 查询已读列表
     *
     * @param baseRead 已读
     * @return 已读
     */
    @Override
    public List<BaseRead> selectBaseReadList(BaseRead baseRead) {
        return baseReadMapper.selectBaseReadList(baseRead);
    }

    /**
     * 新增已读
     *
     * @param baseRead 已读
     * @return 结果
     */
    @Override
    public int insertBaseRead(BaseRead baseRead) {
        baseRead.setCreateTime(DateUtils.getNowDate());
        return baseReadMapper.insertBaseRead(baseRead);
    }

    /**
     * 修改已读
     *
     * @param baseRead 已读
     * @return 结果
     */
    @Override
    public int updateBaseRead(BaseRead baseRead) {
        baseRead.setUpdateTime(DateUtils.getNowDate());
        return baseReadMapper.updateBaseRead(baseRead);
    }

    /**
     * 批量删除已读
     *
     * @param ids 需要删除的已读主键
     * @return 结果
     */
    @Override
    public int deleteBaseReadByIds(Long[] ids) {
        return baseReadMapper.deleteBaseReadByIds(ids);
    }

    /**
     * 删除已读信息
     *
     * @param id 已读主键
     * @return 结果
     */
    @Override
    public int deleteBaseReadById(Long id) {
        return baseReadMapper.deleteBaseReadById(id);
    }

    /**
     * 新增或更新已读
     *
     * @param baseRead 已读
     * @return 结果
     */
    @Override
    public int insertOrUpdateBaseRead(BaseRead baseRead) {
        List<BaseRead> baseReadList = baseReadMapper.selectBaseReadList(baseRead);
        if (CollUtil.isNotEmpty(baseReadList)) {
            baseRead.setUpdateTime(DateUtils.getNowDate());
            return baseReadMapper.updateBaseRead(baseRead);
        } else {
            baseRead.setCreateTime(DateUtils.getNowDate());
            return baseReadMapper.insertBaseRead(baseRead);
        }
    }

    /**
     * 查询已读列表
     *
     * @param baseReadQuery 查询条件
     * @param formIdList    表单ID列表
     * @return 已读集合
     */
    @Override
    public List<BaseRead> selectBaseReadListByCondition(BaseRead baseReadQuery, List<Long> formIdList) {
        LambdaQueryWrapper<BaseRead> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseRead::getType, baseReadQuery.getType())
                .eq(BaseRead::getUserId, baseReadQuery.getUserId())
                .in(BaseRead::getFormId, formIdList);
        return baseReadMapper.selectList(queryWrapper);
    }
}
