package com.template.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.template.mapper.SmartMenuMapper;
import com.template.model.pojo.SmartMenu;
import com.template.model.pojo.SmartMenu;
import com.template.model.result.PageUtils;
import com.template.services.SmartMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 应用管理 服务实现类
 * </p>
 *
 * @author ceshi
 * @since 2024-01-16
 */
@Service
public class SmartMenuServiceImpl extends ServiceImpl<SmartMenuMapper, SmartMenu> implements SmartMenuService {

    @Autowired
    private SmartMenuMapper smartMenuMapper;

    @Override
    public int insertSmartMenu(SmartMenu sm) {
        int result = smartMenuMapper.insert(sm);
        return result;
    }

    @Override
    public int updateSmartMenu(SmartMenu sm) {
        int result = smartMenuMapper.updateById(sm);
        return result;
    }

    @Override
    public PageUtils<SmartMenu> queryPageSmartMenus(int currentPage, int pageCount, String name) {
        Page<SmartMenu> page = new Page<>(currentPage, pageCount);
        QueryWrapper<SmartMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(name), "name", name);
        IPage<SmartMenu> result = smartMenuMapper.selectPage(page, queryWrapper);
        return new PageUtils<>(result);
    }

    @Override
    public int deleteSmartMenuById(int id) {
        int result = smartMenuMapper.deleteById(id);
        return result;
    }

    @Override
    public SmartMenu querySmartMenuByName(String name, String route) {
        QueryWrapper<SmartMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.hasText(name), "name", name)
                .eq(StringUtils.hasText(route), "route", route);
        SmartMenu result = smartMenuMapper.selectOne(queryWrapper);
        return result;
    }

    @Override
    public SmartMenu getSmartById(int id) {
        SmartMenu result = smartMenuMapper.selectById(id);
        return result;
    }

    @Override
    public SmartMenu getSmartMenu() {
        SmartMenu newOne = this.getOne(new LambdaQueryWrapper<SmartMenu>()
                .orderBy(true, false, SmartMenu::getCreateTime)
                .last("limit 1")
        );

        return newOne == null ? new SmartMenu() : newOne;
    }
}
