package com.zxdmy.excite.system.service.impl.cloud.collection;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zxdmy.excite.common.entity.PageList;
import com.zxdmy.excite.common.exception.ServiceException;
import com.zxdmy.excite.system.entity.SysUser;
import com.zxdmy.excite.system.entity.cloud.account.CloudCompany;
import com.zxdmy.excite.system.entity.cloud.collection.CloudUserCollection;
import com.zxdmy.excite.system.entity.req.account.ReqCompany;
import com.zxdmy.excite.system.entity.req.collection.ReqCloudUserCollection;
import com.zxdmy.excite.system.entity.req.space.ReqCloudFile;
import com.zxdmy.excite.system.entity.vo.account.CompanyVo;
import com.zxdmy.excite.system.entity.vo.collection.CloudUserCollectionVo;
import com.zxdmy.excite.system.entity.vo.space.CloudFileVo;
import com.zxdmy.excite.system.mapper.SysUserMapper;
import com.zxdmy.excite.system.mapper.cloud.collection.CloudUserCollectionMapper;
import com.zxdmy.excite.system.service.cloud.collection.ICloudUserCollectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 收藏表 服务实现类
 * </p>
 *
 * @author 沈松
 * @since 2026-01-29
 */
@Service
@AllArgsConstructor
public class CloudUserCollectionServiceImpl extends ServiceImpl<CloudUserCollectionMapper, CloudUserCollection> implements ICloudUserCollectionService {
    CloudUserCollectionMapper cloudUserCollectionMapper;
    SysUserMapper sysUserMapper;
    @Override
    public PageList<CloudFileVo> queryByPage(Integer page, Integer limit, ReqCloudUserCollection data) {
        Integer pages = limit * (page - 1);
        ArrayList<CloudFileVo> list = cloudUserCollectionMapper.queryByPage(pages, limit,data);
        Integer count = cloudUserCollectionMapper.queryByPageCount(data);
        return new PageList<CloudFileVo>(list,page,limit,count);
    }

    @Override
    public CloudUserCollectionVo getByData(ReqCloudUserCollection data) {
        return cloudUserCollectionMapper.getData(data);
    }

    @Override
    public int saveData(ReqCloudUserCollection data) {
        LocalDateTime localDateTime = LocalDateTime.now();
        CloudUserCollection saveData = new CloudUserCollection();
        SysUser sysUser = sysUserMapper.selectById(StpUtil.getLoginIdAsInt());
        saveData.setFileId(Long.valueOf(data.getFileId()));
        saveData.setUserId(sysUser.getId());
        saveData.setCreateTime(localDateTime);
        if(cloudUserCollectionMapper.insert(saveData)==1){
            return 1;
        }
        return 0;
    }


    @Override
    public int deleteById(String id) {
        if (null == id) {
            throw new ServiceException("删除的ID不能为空！");
        }
        QueryWrapper<CloudUserCollection> wrapper = new QueryWrapper<>();
        wrapper.eq("file_id",id);
        wrapper.eq("user_id",StpUtil.getLoginIdAsInt());
        List<CloudUserCollection> list = cloudUserCollectionMapper.selectList(wrapper);
        if(list.size()>0){
            return cloudUserCollectionMapper.deleteById(list.get(0).getId());
        }
        return 0;
    }
}
