package com.ruoyi.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.biz.mapper.ArFileImageMapper;
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.ArFileIndexBo;
import com.ruoyi.biz.domain.vo.ArFileIndexVo;
import com.ruoyi.biz.domain.ArFileIndex;
import com.ruoyi.biz.mapper.ArFileIndexMapper;
import com.ruoyi.biz.service.IArFileIndexService;

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

/**
 * 展示图Service业务层处理
 *
 * @author gwd
 * @date 2023-11-10
 */
@RequiredArgsConstructor
@Service
public class ArFileIndexServiceImpl implements IArFileIndexService {

    private final ArFileIndexMapper baseMapper;

    private final ArFileImageMapper arFileImageMapper;

    /**
     * 查询展示图
     */
    @Override
    public ArFileIndexVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询展示图列表
     */
    @Override
    public TableDataInfo<ArFileIndexVo> queryPageList(ArFileIndexBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ArFileIndex> lqw = buildQueryWrapper(bo);
        Page<ArFileIndexVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询展示图列表
     */
    @Override
    public List<ArFileIndexVo> queryList(ArFileIndexBo bo) {
        LambdaQueryWrapper<ArFileIndex> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

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

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

    /**
     * 修改展示图
     */
    @Override
    public Boolean updateByBo(ArFileIndexBo bo) {
        ArFileIndex update = BeanUtil.toBean(bo, ArFileIndex.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    @Override
    public Boolean randomAdd(Integer count) {
        LambdaQueryWrapper<ArFileIndex> lqw = Wrappers.lambdaQuery();
        lqw.eq(ArFileIndex::getDelFlag, 0);
        baseMapper.delete(lqw);
        List<ArFileIndex> list = arFileImageMapper.queryArFileForIndexList(count);
        return baseMapper.insertBatch(list);
    }
}
