package com.sqx.modules.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.common.utils.PageUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.shop.dao.SearchHistoryDao;
import com.sqx.modules.shop.entity.SearchHistory;
import com.sqx.modules.shop.service.SearchHistoryService;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class SearchHistoryServiceImpl extends ServiceImpl<SearchHistoryDao, SearchHistory> implements SearchHistoryService {

    @Override
    public void insertSearchHistory(Long userId, String impotr) {
        baseMapper.delete(new QueryWrapper<SearchHistory>().eq("user_id", userId).eq("message", impotr));
        SearchHistory searchHistory = new SearchHistory();
        searchHistory.setUserId(userId);
        searchHistory.setMessage(impotr);
        searchHistory.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        baseMapper.insert(searchHistory);
    }

    @Override
    public Result selectSearchHistory(Long userId, Integer page, Integer limit) {
        Page<SearchHistory> pages = new Page<>(page, limit);
        PageUtils pageUtils = new PageUtils(baseMapper.selectPage(pages, new QueryWrapper<SearchHistory>().eq("user_id", userId).orderByDesc("create_time")));
        return Result.success().put("data", pageUtils);
    }

    @Override
    public Result deleteSearchHistory(Long userId) {
        baseMapper.delete(new QueryWrapper<SearchHistory>().eq("user_id", userId));
        return Result.success();
    }
}
