package com.ruoyi.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.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 com.ruoyi.biz.domain.bo.ArFileBannerBo;
import com.ruoyi.biz.domain.vo.ArFileBannerVo;
import com.ruoyi.biz.domain.ArFileBanner;
import com.ruoyi.biz.mapper.ArFileBannerMapper;
import com.ruoyi.biz.service.IArFileBannerService;

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

/**
 * 轮播图Service业务层处理
 *
 * @author gwd
 * @date 2023-11-10
 */
@RequiredArgsConstructor
@Service
public class ArFileBannerServiceImpl implements IArFileBannerService {

    private final ArFileBannerMapper baseMapper;

    /**
     * 查询轮播图
     */
    @Override
    public ArFileBannerVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询轮播图列表
     */
    @Override
    public TableDataInfo<ArFileBannerVo> queryPageList(ArFileBannerBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ArFileBanner> lqw = buildQueryWrapper(bo);
        Page<ArFileBannerVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询轮播图列表
     */
    @Override
    public List<ArFileBannerVo> queryList(ArFileBannerBo bo) {
        LambdaQueryWrapper<ArFileBanner> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ArFileBanner> buildQueryWrapper(ArFileBannerBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ArFileBanner> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getOssId() != null, ArFileBanner::getOssId, bo.getOssId());
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), ArFileBanner::getUrl, bo.getUrl());
        return lqw;
    }

    /**
     * 新增轮播图
     */
    @Override
    public Boolean insertByBo(ArFileBannerBo bo) {
        ArFileBanner add = BeanUtil.toBean(bo, ArFileBanner.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改轮播图
     */
    @Override
    public Boolean updateByBo(ArFileBannerBo bo) {
        ArFileBanner update = BeanUtil.toBean(bo, ArFileBanner.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除轮播图
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
