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

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.collection.CloudUserCollection;
import com.zxdmy.excite.system.entity.cloud.lately.CloudUserLately;
import com.zxdmy.excite.system.entity.cloud.space.CloudFile;
import com.zxdmy.excite.system.entity.req.collection.ReqCloudUserCollection;
import com.zxdmy.excite.system.entity.req.lately.ReqCloudUserLately;
import com.zxdmy.excite.system.entity.req.trace.ReqCloudFileTrace;
import com.zxdmy.excite.system.entity.vo.collection.CloudUserCollectionVo;
import com.zxdmy.excite.system.entity.vo.lately.CloudUserLatelyVo;
import com.zxdmy.excite.system.entity.vo.space.CloudFileVo;
import com.zxdmy.excite.system.mapper.SysUserMapper;
import com.zxdmy.excite.system.mapper.cloud.lately.CloudUserLatelyMapper;
import com.zxdmy.excite.system.mapper.cloud.space.CloudFileMapper;
import com.zxdmy.excite.system.service.cloud.lately.ICloudUserLatelyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zxdmy.excite.system.service.cloud.trace.ICloudFileTraceService;
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 CloudUserLatelyServiceImpl extends ServiceImpl<CloudUserLatelyMapper, CloudUserLately> implements ICloudUserLatelyService {
    CloudUserLatelyMapper cloudUserLatelyMapper;
    SysUserMapper sysUserMapper;
    CloudFileMapper cloudFileMapper;
    ICloudFileTraceService iCloudFileTraceService;
    @Override
    public PageList<CloudFileVo> queryByPage(Integer page, Integer limit, ReqCloudUserLately data) {
        Integer pages = limit * (page - 1);
        ArrayList<CloudFileVo> list = cloudUserLatelyMapper.queryByPage(pages, limit,data);
        Integer count = cloudUserLatelyMapper.queryByPageCount(data);
        return new PageList<CloudFileVo>(list,page,limit,count);
    }

    @Override
    public CloudUserLatelyVo getByData(ReqCloudUserLately data) {
        return cloudUserLatelyMapper.getData(data);
    }

    @Override
    public int saveData(ReqCloudUserLately data) {
        LocalDateTime localDateTime = LocalDateTime.now();
        CloudUserLately saveData = new CloudUserLately();
        SysUser sysUser = sysUserMapper.selectById(StpUtil.getLoginIdAsInt());
        saveData.setFileId(Long.valueOf(data.getFileId()));
        saveData.setUserId(sysUser.getId());
        saveData.setCreateTime(localDateTime);
        saveData.setLatelyTime(localDateTime);
        if(cloudUserLatelyMapper.insert(saveData)==1){

            CloudFile cloudFile = cloudFileMapper.selectById(data.getFileId());

            ReqCloudFileTrace reqCloudFileTrace = new ReqCloudFileTrace();
            reqCloudFileTrace.setFileId(data.getFileId());
            reqCloudFileTrace.setTraceName("查看了 "+cloudFile.getFileName()+" 文件");
            iCloudFileTraceService.saveData(reqCloudFileTrace);


            return 1;
        }
        return 0;
    }


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