package org.jeecg.modules.system.app.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.system.app.entity.AppSearchHot;
import org.jeecg.modules.system.app.mapper.AppSearchHotMapper;
import org.jeecg.modules.system.app.service.IAppSearchHotService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class AppSearchHotServiceImpl extends ServiceImpl<AppSearchHotMapper, AppSearchHot> implements IAppSearchHotService {
    @Resource
    private AppSearchHotMapper appSearchHotMapper;
    @Override
    public IPage<AppSearchHot> getHotSearch(Page<AppSearchHot> page, String name) {
        if(null != name && !"".equals(name)){
            return appSearchHotMapper.selectPage(page, Wrappers.<AppSearchHot>lambdaQuery()
                    .like(AppSearchHot::getSearchContent, name).orderByDesc(AppSearchHot::getSort));
        }else {
            return appSearchHotMapper.selectPage(page,null);
        }
    }

    @Override
    public boolean addHotSearch(AppSearchHot appSearchHot) {
        if(null != appSearchHotMapper.selectOne(Wrappers.<AppSearchHot>lambdaQuery()
                .eq(AppSearchHot::getSearchContent, appSearchHot.getSearchContent()))){
            return appSearchHotMapper.insert(appSearchHot) > 0;
        }else {
            appSearchHot.setSearchCount(appSearchHot.getSearchCount() + 1);
            return appSearchHotMapper.updateById(appSearchHot) > 0;
        }

    }

    @Override
    public boolean deleteHotSearch(String id) {
        return appSearchHotMapper.deleteById(id) > 0;
    }

    @Override
    public boolean updateHotSearch(AppSearchHot appSearchHot) {
        return appSearchHotMapper.updateById(appSearchHot) > 0;
    }

    @Override
    public AppSearchHot getHotSearchById(String id) {
        return appSearchHotMapper.selectById(id);
    }
}
