package shop.alien.store.service.impl;

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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import shop.alien.entity.store.RestRankingList;
import shop.alien.mapper.RestRankingListMapper;
import shop.alien.store.service.RestRankingListService;

import java.util.Date;

/**
 * @Author: fcw
 * @CreateTime: 2025-04-23
 * @Description: 店铺排行榜
 */
@Service
@RequiredArgsConstructor
public class RestRankingListServiceImpl extends ServiceImpl<RestRankingListMapper, RestRankingList> implements RestRankingListService {

    private final RestRankingListMapper restRankingListMapper;

    @Override
    public IPage<RestRankingList> getRestRankingList(Integer page, Integer size, String type) {
        QueryWrapper<RestRankingList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(!type.isEmpty(), "type", type);
        queryWrapper.orderByDesc("created_time");
        IPage<RestRankingList> storePage = new Page<>(page, size);
        return restRankingListMapper.selectPage(storePage, queryWrapper);
    }

    @Override
    public Integer addEvaluation(RestRankingList restRankingList) {
        return restRankingListMapper.insert(restRankingList);
    }

    @Override
    public int addOrUpdateEvaluation(RestRankingList restRankingList) {
        if (StringUtils.isEmpty(restRankingList.getId())) {
            Date nowDate = new Date(System.currentTimeMillis());
            restRankingList.setCreatedTime(nowDate);
            return restRankingListMapper.insert(restRankingList);
        } else {
            return restRankingListMapper.updateById(restRankingList);
        }
    }
}
