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.ArFileImageBo;
import com.ruoyi.biz.domain.vo.ArFileImageVo;
import com.ruoyi.biz.domain.ArFileImage;
import com.ruoyi.biz.mapper.ArFileImageMapper;
import com.ruoyi.biz.service.IArFileImageService;

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

/**
 * 相册Service业务层处理
 *
 * @author gwd
 * @date 2023-07-11
 */
@RequiredArgsConstructor
@Service
public class ArFileImageServiceImpl implements IArFileImageService {

    private final ArFileImageMapper baseMapper;

    /**
     * 查询相册
     */
    @Override
    public ArFileImageVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询相册列表
     */
    @Override
    public TableDataInfo<ArFileImageVo> queryPageList(ArFileImageBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ArFileImage> lqw = buildQueryWrapper(bo);
        Page<ArFileImageVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询相册列表
     */
    @Override
    public List<ArFileImageVo> queryList(ArFileImageBo bo) {
        LambdaQueryWrapper<ArFileImage> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

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

    /**
     * 新增相册
     */
    @Override
    public Boolean insertByBo(ArFileImageBo bo) {
        ArFileImage add = BeanUtil.toBean(bo, ArFileImage.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改相册
     */
    @Override
    public Boolean updateByBo(ArFileImageBo bo) {
        ArFileImage update = BeanUtil.toBean(bo, ArFileImage.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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