package com.ruoyi.iit.service.impl;

import java.io.File;
import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.iit.domain.IitMyselfFile;
import com.ruoyi.iit.mapper.IitMyselfFileMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.token.TokenService;
import org.springframework.stereotype.Service;
import com.ruoyi.iit.mapper.IitFileInfoMapper;
import com.ruoyi.iit.domain.IitFileInfo;
import com.ruoyi.iit.service.IIitFileInfoService;

/**
 * 文件管理Service业务层处理
 *
 * @author Hao Li
 * @date 2025-04-11
 */
@Service
public class IitFileInfoServiceImpl implements IIitFileInfoService
{
    @Autowired
    private IitFileInfoMapper iitFileInfoMapper;

    @Autowired
    private IitMyselfFileMapper iitMyselfFileMapper;

    @Value("${ruoyi.profile}")
    private String profile;


    /**
     * 查询文件管理
     *
     * @param id 文件管理主键
     * @return 文件管理
     */
    @Override
    public IitFileInfo selectIitFileInfoById(Long id)
    {
        return iitFileInfoMapper.selectIitFileInfoById(id);
    }

    /**
     * 查询文件管理列表
     *
     * @param iitFileInfo 文件管理
     * @return 文件管理
     */
    @Override
    @DataScope(deptAlias = "u", userAlias = "u")
    public List<IitFileInfo> selectIitFileInfoList(IitFileInfo iitFileInfo)
    {
        return iitFileInfoMapper.selectIitFileInfoList(iitFileInfo);
    }

    @Override
    public List<IitFileInfo> selectMyselfList(IitFileInfo iitFileInfo) {
        // 获取当前登录用户的id
        Long userId = SecurityUtils.getUserId();
        iitFileInfo.setUserId(userId);
        return iitFileInfoMapper.selectMyselfList(iitFileInfo);
    }

    /**
     * 新增文件管理
     *
     * @param iitFileInfo 文件管理
     * @return 结果
     */
    @Override
    public int insertIitFileInfo(IitFileInfo iitFileInfo)
    {
        iitFileInfo.setCreateTime(DateUtils.getNowDate());
        // fileUrl : /profile/upload/2025/04/11/e1_代码项目交接.xlsx_20250411101820A001.xlsx
        String filePath = profile + iitFileInfo.getFileUrl().replace("/profile", "");
        // filePath : /home/inspur/iit/uploadPath/upload/2025/04/11/e1_代码项目交接.xlsx_20250411101820A001.xlsx
        iitFileInfo.setFilePath(filePath);
        String newFileName = filePath.substring(filePath.lastIndexOf("/") + 1);
        // newFileName : e1_代码项目交接.xlsx_20250411101820A001.xlsx
        String oldFileName = newFileName.substring(0, newFileName.lastIndexOf("_"));
        iitFileInfo.setFileName(oldFileName);
        // 获取文件大小
        iitFileInfo.setFileSize(getFileSize(filePath));
        // userId
        iitFileInfo.setUserId(SecurityUtils.getUserId());
        // deptId
        iitFileInfo.setDeptId(SecurityUtils.getDeptId());
        // userName
        iitFileInfo.setUserName(SecurityUtils.getLoginUser().getUser().getNickName());

        iitFileInfo.setPushTimes(0);

        // 文件新增以后，保存至我的文件表
        int insertResult = iitFileInfoMapper.insertIitFileInfo(iitFileInfo);
        IitMyselfFile iitMyselfFile = new IitMyselfFile();
        iitMyselfFile.setFileId(iitFileInfo.getId());
        iitMyselfFile.setUserId(SecurityUtils.getUserId());
        iitMyselfFile.setCreateTime(DateUtils.getNowDate());
        iitMyselfFileMapper.insertIitMyselfFile(iitMyselfFile);
        
        return insertResult;
    }

    private Long getFileSize(String filePath) {
        File file = new File(filePath);
        // 转换成KB
        return file.length() / 1024;
    }

    /**
     * 修改文件管理
     *
     * @param iitFileInfo 文件管理
     * @return 结果
     */
    @Override
    public int updateIitFileInfo(IitFileInfo iitFileInfo)
    {
        return iitFileInfoMapper.updateIitFileInfo(iitFileInfo);
    }

    /**
     * 批量删除文件管理
     *
     * @param ids 需要删除的文件管理主键
     * @return 结果
     */
    @Override
    public int deleteIitFileInfoByIds(Long[] ids)
    {
        return iitFileInfoMapper.deleteIitFileInfoByIds(ids);
    }

    /**
     * 删除文件管理信息
     *
     * @param id 文件管理主键
     * @return 结果
     */
    @Override
    public int deleteIitFileInfoById(Long id)
    {
        return iitFileInfoMapper.deleteIitFileInfoById(id);
    }
}
