package org.xyzh.usercenter.service.impl;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xyzh.common.core.domain.ResultDomain;
import org.xyzh.common.dto.user.TbSysUser;
import org.xyzh.common.dto.usercenter.TbUserCollection;
import org.xyzh.system.utils.LoginUtil;
import org.xyzh.usercenter.mapper.UserCollectionMapper;
import org.xyzh.usercenter.service.UCUserCollectionService;

/**
 * @description 用户收藏服务实现类
 * @filename UCUserCollectionServiceImpl.java
 * @author yslg
 * @copyright xyzh
 * @since 2025-10-15
 */
@Service
public class UCUserCollectionServiceImpl implements UCUserCollectionService {

    private static final Logger logger = LoggerFactory.getLogger(UCUserCollectionServiceImpl.class);

    @Autowired
    private UserCollectionMapper userCollectionMapper;

    @Override
    public ResultDomain<TbUserCollection> addCollection(TbUserCollection userCollection) {
        TbSysUser user = LoginUtil.getCurrentUser();
        if (user == null) {
            ResultDomain<TbUserCollection> resultDomain = new ResultDomain<>();
            resultDomain.fail("请先登录");
            return resultDomain;
        }
        userCollection.setUserID(user.getID());
        userCollection.setCreateTime(new Date());
        int result = userCollectionMapper.insertUserCollection(userCollection);
        if (result > 0) {
            ResultDomain<TbUserCollection> resultDomain = new ResultDomain<>();
            resultDomain.success("添加收藏成功", userCollection);
            return resultDomain;
        } else {
            ResultDomain<TbUserCollection> resultDomain = new ResultDomain<>();
            resultDomain.fail("添加收藏失败");
            return resultDomain;
        }
    }

    @Override
    public ResultDomain<TbUserCollection> getCollectionDetail(String userID, Integer collectionType,
            String collectionID) {
        return null;
    }

    @Override
    public ResultDomain<TbUserCollection> getUserCollections(String userID, Integer collectionType) {
        TbUserCollection filter = new TbUserCollection();
        filter.setUserID(userID);
        filter.setCollectionType(collectionType);
        List<TbUserCollection> list = userCollectionMapper.selectUserCollections(filter);
        if (list != null && list.size() > 0) {
            ResultDomain<TbUserCollection> resultDomain = new ResultDomain<>();
            resultDomain.success("获取收藏列表成功", list);
            return resultDomain;
        } else {
            ResultDomain<TbUserCollection> resultDomain = new ResultDomain<>();
            resultDomain.fail("获取收藏列表失败");
            return resultDomain;
        }
    }

    @Override
    public ResultDomain<Boolean> isCollected(String userID, Integer collectionType, String collectionID) {
        TbUserCollection filter = new TbUserCollection();
        filter.setUserID(userID);
        filter.setCollectionType(collectionType);
        filter.setCollectionID(collectionID);
        long count = userCollectionMapper.countUserCollections(filter);
        if (count > 0) {
            ResultDomain<Boolean> resultDomain = new ResultDomain<>();
            resultDomain.success("已收藏", true);
            return resultDomain;
        } else {
            ResultDomain<Boolean> resultDomain = new ResultDomain<>();
            resultDomain.success("未收藏", false);
            return resultDomain;
        }
    }

    @Override
    public ResultDomain<Boolean> removeCollection(TbUserCollection userCollection) {
        int result = userCollectionMapper.deleteUserCollection(userCollection);
        if (result > 0) {
            ResultDomain<Boolean> resultDomain = new ResultDomain<>();
            resultDomain.success("删除收藏成功", true);
            return resultDomain;
        } else {
            ResultDomain<Boolean> resultDomain = new ResultDomain<>();
            resultDomain.fail("删除收藏失败");
            return resultDomain;
        }
    }

    @Override
    public ResultDomain<Integer> getCollectionCount(String userID, Integer collectionType, String collectionID) {
        TbUserCollection filter = new TbUserCollection();
        filter.setUserID(userID);
        filter.setCollectionType(collectionType);
        filter.setCollectionID(collectionID);
        long count = userCollectionMapper.countUserCollections(filter);
        if (count > 0) {
            ResultDomain<Integer> resultDomain = new ResultDomain<>();
            resultDomain.success("获取收藏数量成功", (int)count);
            return resultDomain;
        } else {
            ResultDomain<Integer> resultDomain = new ResultDomain<>();
            resultDomain.fail("获取收藏数量失败");
            return resultDomain;
        }
    }
}