package org.dromara.system.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.system.domain.bo.TblDzbcBannerBo;
import org.dromara.system.domain.vo.TblDzbcBannerVo;
import org.dromara.system.domain.TblDzbcBanner;
import org.dromara.system.mapper.TblDzbcBannerMapper;
import org.dromara.system.service.ITblDzbcBannerService;

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

/**
 * 定制班车-小程序bannerService业务层处理
 *
 * @author Lion Li
 * @date 2025-02-12
 */
@RequiredArgsConstructor
@Service
public class TblDzbcBannerServiceImpl implements ITblDzbcBannerService {

    private final TblDzbcBannerMapper baseMapper;

    /**
     * 查询定制班车-小程序banner
     */
    @Override
    public TblDzbcBannerVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询定制班车-小程序banner列表
     */
    @Override
    public TableDataInfo<TblDzbcBannerVo> queryPageList(TblDzbcBannerBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TblDzbcBanner> lqw = buildQueryWrapper(bo);
        Page<TblDzbcBannerVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询定制班车-小程序banner列表
     */
    @Override
    public List<TblDzbcBannerVo> queryList(TblDzbcBannerBo bo) {
        LambdaQueryWrapper<TblDzbcBanner> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TblDzbcBanner> buildQueryWrapper(TblDzbcBannerBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TblDzbcBanner> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), TblDzbcBanner::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getAddr()), TblDzbcBanner::getAddr, bo.getAddr());
        lqw.eq(bo.getStatus() != null, TblDzbcBanner::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getExt1()), TblDzbcBanner::getExt1, bo.getExt1());
        lqw.eq(StringUtils.isNotBlank(bo.getExt2()), TblDzbcBanner::getExt2, bo.getExt2());
        return lqw;
    }

    /**
     * 新增定制班车-小程序banner
     */
    @Override
    public Boolean insertByBo(TblDzbcBannerBo bo) {
        TblDzbcBanner add = MapstructUtils.convert(bo, TblDzbcBanner.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改定制班车-小程序banner
     */
    @Override
    public Boolean updateByBo(TblDzbcBannerBo bo) {
        TblDzbcBanner update = MapstructUtils.convert(bo, TblDzbcBanner.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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