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.VsNavServiceToolbarBo;
import org.dromara.system.domain.vo.VsNavServiceToolbarVo;
import org.dromara.system.domain.VsNavServiceToolbar;
import org.dromara.system.mapper.VsNavServiceToolbarMapper;
import org.dromara.system.service.IVsNavServiceToolbarService;

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

/**
 * 服务区工具栏Service业务层处理
 *
 * @author system
 * @date 2025-01-20
 */
@RequiredArgsConstructor
@Service
public class VsNavServiceToolbarServiceImpl implements IVsNavServiceToolbarService {

    private final VsNavServiceToolbarMapper baseMapper;

    /**
     * 查询服务区工具栏
     */
    @Override
    public VsNavServiceToolbarVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询服务区工具栏列表
     */
    @Override
    public TableDataInfo<VsNavServiceToolbarVo> queryPageList(VsNavServiceToolbarBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<VsNavServiceToolbar> lqw = buildQueryWrapper(bo);
        Page<VsNavServiceToolbarVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询服务区工具栏列表
     */
    @Override
    public List<VsNavServiceToolbarVo> queryList(VsNavServiceToolbarBo bo) {
        LambdaQueryWrapper<VsNavServiceToolbar> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<VsNavServiceToolbar> buildQueryWrapper(VsNavServiceToolbarBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<VsNavServiceToolbar> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), VsNavServiceToolbar::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), VsNavServiceToolbar::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getPic()), VsNavServiceToolbar::getPic, bo.getPic());
        lqw.eq(bo.getJumpType() != null, VsNavServiceToolbar::getJumpType, bo.getJumpType());
        lqw.eq(StringUtils.isNotBlank(bo.getJumpPath()), VsNavServiceToolbar::getJumpPath, bo.getJumpPath());
        lqw.eq(bo.getCategoryId() != null, VsNavServiceToolbar::getCategoryId, bo.getCategoryId());
        lqw.eq(bo.getSortNum() != null, VsNavServiceToolbar::getSortNum, bo.getSortNum());
        lqw.eq(bo.getStatus() != null, VsNavServiceToolbar::getStatus, bo.getStatus());
        lqw.eq(bo.getShowFlag() != null, VsNavServiceToolbar::getShowFlag, bo.getShowFlag());
        return lqw;
    }

    /**
     * 新增服务区工具栏
     */
    @Override
    public Boolean insertByBo(VsNavServiceToolbarBo bo) {
        VsNavServiceToolbar add = MapstructUtils.convert(bo, VsNavServiceToolbar.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改服务区工具栏
     */
    @Override
    public Boolean updateByBo(VsNavServiceToolbarBo bo) {
        VsNavServiceToolbar update = MapstructUtils.convert(bo, VsNavServiceToolbar.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    /**
     * 根据分类ID查询工具栏列表
     */
    @Override
    public List<VsNavServiceToolbarVo> queryByCategoryId(Integer categoryId) {
        LambdaQueryWrapper<VsNavServiceToolbar> lqw = Wrappers.lambdaQuery();
        lqw.eq(VsNavServiceToolbar::getCategoryId, categoryId);
        lqw.eq(VsNavServiceToolbar::getStatus, 0); // 只查询正常状态
        lqw.eq(VsNavServiceToolbar::getShowFlag, 0); // 只查询显示状态
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 根据分类ID查询工具栏列表（按排序号排序）
     */
    @Override
    public List<VsNavServiceToolbarVo> queryByCategoryIdOrderBySort(Integer categoryId) {
        LambdaQueryWrapper<VsNavServiceToolbar> lqw = Wrappers.lambdaQuery();
        lqw.eq(VsNavServiceToolbar::getCategoryId, categoryId);
        lqw.eq(VsNavServiceToolbar::getStatus, 0); // 只查询正常状态
        lqw.eq(VsNavServiceToolbar::getShowFlag, 0); // 只查询显示状态
        lqw.orderByAsc(VsNavServiceToolbar::getSortNum); // 按排序号升序
        return baseMapper.selectVoList(lqw);
    }
}
