package com.kym.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.kym.entity.Collect;
import com.kym.mapper.CollectMapper;
import com.kym.service.CollectService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 收藏表 服务实现类
 * </p>
 *
 * @author skyline
 * @since 2023-07-31
 */
@Service
public class CollectServiceImpl extends MPJBaseServiceImpl<CollectMapper, Collect> implements CollectService {


    /**
     * 添加/取消收藏
     *
     * @param stationId
     * @param status
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateCollect(String stationId, Integer status) {
        var userId = StpUtil.getLoginIdAsLong();

        switch (status) {
            case 1 -> // 收藏
            {
                var collect = new Collect();
                collect.setUserId(userId);
                collect.setStationId(stationId);
                collect.setStatus(status);
                var exist = lambdaQuery().eq(Collect::getUserId, userId).eq(Collect::getStationId, stationId).exists();
                if (!exist) {
                    save(collect);
                } else {
                    lambdaUpdate().set(Collect::getStatus, status).eq(Collect::getUserId, userId).eq(Collect::getStationId, stationId).update();
                }
            }
            case 0 -> // 取消收藏
                    lambdaUpdate().set(Collect::getStatus, status).eq(Collect::getUserId, userId).eq(Collect::getStationId, stationId).update();
        }

    }

    /**
     * 获取收藏列表，前端匹配站点详情数据
     *
     * @return
     */
    @Override
    public List<Collect> listCollect() {
        var userid = StpUtil.getLoginIdAsLong();
        return lambdaQuery().eq(Collect::getUserId, userid).list();
    }

}
