package org.dromara.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.domain.bo.SenMonitorPointMediaBo;
import org.dromara.domain.vo.SenMonitorPointMediaVo;
import org.dromara.domain.SenMonitorPointMedia;
import org.dromara.mapper.SenMonitorPointMediaMapper;
import org.dromara.service.ISenMonitorPointMediaService;

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

/**
 * 监控点位视频媒介Service业务层处理
 *
 * @author luogang
 * @date 2024-09-18
 */
@RequiredArgsConstructor
@Service
public class SenMonitorPointMediaServiceImpl implements ISenMonitorPointMediaService {

    private final SenMonitorPointMediaMapper baseMapper;

    /**
     * 查询监控点位视频媒介
     *
     * @param id 主键
     * @return 监控点位视频媒介
     */
    @Override
    public SenMonitorPointMediaVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询监控点位视频媒介列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 监控点位视频媒介分页列表
     */
    @Override
    public TableDataInfo<SenMonitorPointMediaVo> queryPageList(SenMonitorPointMediaBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SenMonitorPointMedia> lqw = buildQueryWrapper(bo);
        Page<SenMonitorPointMediaVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的监控点位视频媒介列表
     *
     * @param bo 查询条件
     * @return 监控点位视频媒介列表
     */
    @Override
    public List<SenMonitorPointMediaVo> queryList(SenMonitorPointMediaBo bo) {
        LambdaQueryWrapper<SenMonitorPointMedia> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SenMonitorPointMedia> buildQueryWrapper(SenMonitorPointMediaBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SenMonitorPointMedia> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getVideoType()), SenMonitorPointMedia::getVideoType, bo.getVideoType());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceNo()), SenMonitorPointMedia::getDeviceNo, bo.getDeviceNo());
        lqw.eq(StringUtils.isNotBlank(bo.getStreamUrl()), SenMonitorPointMedia::getStreamUrl, bo.getStreamUrl());
        lqw.like(StringUtils.isNotBlank(bo.getFileName()), SenMonitorPointMedia::getFileName, bo.getFileName());
        lqw.eq(StringUtils.isNotBlank(bo.getFilePath()), SenMonitorPointMedia::getFilePath, bo.getFilePath());
        lqw.eq(bo.getAdjustTime() != null, SenMonitorPointMedia::getAdjustTime, bo.getAdjustTime());
        return lqw;
    }

    /**
     * 新增监控点位视频媒介
     *
     * @param bo 监控点位视频媒介
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(SenMonitorPointMediaBo bo) {
        SenMonitorPointMedia add = MapstructUtils.convert(bo, SenMonitorPointMedia.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改监控点位视频媒介
     *
     * @param bo 监控点位视频媒介
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(SenMonitorPointMediaBo bo) {
        SenMonitorPointMedia update = MapstructUtils.convert(bo, SenMonitorPointMedia.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SenMonitorPointMedia entity){
        //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 SenMonitorPointMediaVo getPointMediaByPointId(SenMonitorPointMediaBo bo) {
        LambdaQueryWrapper<SenMonitorPointMedia> lqw = new LambdaQueryWrapper<>();
        return baseMapper.selectVoOne(lqw.eq(SenMonitorPointMedia::getPointId, bo.getPointId()));
    }
}
