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

import cn.dev33.satoken.stp.StpUtil;
import com.zxdmy.excite.common.entity.PageList;
import com.zxdmy.excite.system.entity.SysUser;
import com.zxdmy.excite.system.entity.cloud.lately.CloudUserLately;
import com.zxdmy.excite.system.entity.cloud.trace.CloudFileTrace;
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.space.CloudFileVo;
import com.zxdmy.excite.system.entity.vo.trace.CloudFileTraceVo;
import com.zxdmy.excite.system.mapper.SysUserMapper;
import com.zxdmy.excite.system.mapper.cloud.trace.CloudFileTraceMapper;
import com.zxdmy.excite.system.service.cloud.trace.ICloudFileTraceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 文件痕迹 服务实现类
 * </p>
 *
 * @author 沈松
 * @since 2026-01-29
 */
@Service
@AllArgsConstructor
public class CloudFileTraceServiceImpl extends ServiceImpl<CloudFileTraceMapper, CloudFileTrace> implements ICloudFileTraceService {
    CloudFileTraceMapper cloudFileTraceMapper;
    SysUserMapper sysUserMapper;

    @Override
    public PageList<CloudFileTraceVo> queryByPage(Integer page, Integer limit, ReqCloudFileTrace data) {
        Integer pages = limit * (page - 1);
        ArrayList<CloudFileTraceVo> list = cloudFileTraceMapper.queryByPage(pages, limit,data);
        Integer count = cloudFileTraceMapper.queryByPageCount(data);
        return new PageList<CloudFileTraceVo>(list,page,limit,count);
    }

    @Override
    public int saveData(ReqCloudFileTrace data) {
        LocalDateTime localDateTime = LocalDateTime.now();
        CloudFileTrace saveData = new CloudFileTrace();
        SysUser sysUser = sysUserMapper.selectById(StpUtil.getLoginIdAsInt());
        saveData.setTraceName(data.getTraceName());
        saveData.setFileId(Long.valueOf(data.getFileId()));
        saveData.setTraceCreatorId(sysUser.getId());
        saveData.setTraceCreatorName(sysUser.getUsername());
        saveData.setCreateTime(localDateTime);
        if(cloudFileTraceMapper.insert(saveData)==1){
            return 1;
        }
        return 0;
    }
}
