package org.dromara.payment.sys.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.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.stereotype.Service;
import org.dromara.payment.sys.domain.bo.BusDownloadsBo;
import org.dromara.payment.sys.domain.vo.BusDownloadsVo;
import org.dromara.payment.sys.domain.BusDownloads;
import org.dromara.payment.sys.mapper.BusDownloadsMapper;
import org.dromara.payment.sys.service.IBusDownloadsService;

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

/**
 * 异步下载Service业务层处理
 *
 * @author LionLi
 * @date 2024-06-21
 */
@RequiredArgsConstructor
@Service
public class BusDownloadsServiceImpl implements IBusDownloadsService {

    private final BusDownloadsMapper baseMapper;

    /**
     * 查询异步下载
     */
    @Override
    public BusDownloadsVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询异步下载列表
     */
    @Override
    public TableDataInfo<BusDownloadsVo> queryPageList(BusDownloadsBo bo, PageQuery pageQuery) {
        bo.setCreateBy(LoginHelper.getUserId());
        QueryWrapper<BusDownloads> lqw = buildQueryWrapper(bo);
        Page<BusDownloadsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询异步下载列表
     */
    @Override
    public List<BusDownloadsVo> queryList(BusDownloadsBo bo) {
        QueryWrapper<BusDownloads> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private QueryWrapper<BusDownloads> buildQueryWrapper(BusDownloadsBo bo) {
        Map<String, Object> params = bo.getParams();
        QueryWrapper<BusDownloads> lqw = new QueryWrapper<>();
        lqw.eq(bo.getBusType() != null, "bus_type", bo.getBusType());
        lqw.eq(bo.getCreateBy() !=null,"create_by",bo.getCreateBy());
        lqw.eq(bo.getBusType() != null,"bus_type",bo.getBusType());
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), "url", bo.getUrl());
        lqw.orderByDesc("create_time");
        return lqw;
    }

    /**
     * 新增异步下载
     */
    @Override
    public Boolean insertByBo(BusDownloadsBo bo) {
        BusDownloads add = MapstructUtils.convert(bo, BusDownloads.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改异步下载
     */
    @Override
    public Boolean updateByBo(BusDownloadsBo bo) {
        BusDownloads update = MapstructUtils.convert(bo, BusDownloads.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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