package com.ktg.system.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ktg.common.core.text.Convert;
import com.ktg.common.utils.DateUtils;
import com.ktg.common.utils.StringUtils;
import com.ktg.system.domain.SysUploadFile;
import com.ktg.system.mapper.SysUploadFileMapper;
import com.ktg.system.service.ISysUploadFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 文件Service业务层处理
 *
 * @author cgj
 * @date 2024-10-22
 */
@Service
public class SysUploadFileServiceImpl extends ServiceImpl<SysUploadFileMapper, SysUploadFile> implements ISysUploadFileService {
    @Autowired
    private SysUploadFileMapper sysUploadFileMapper;

    /**
     * 查询文件
     *
     * @param id 文件主键
     * @return 文件
     */
    @Override
    public SysUploadFile selectSysUploadFileById(Long id) {
        return sysUploadFileMapper.selectSysUploadFileById(id);
    }

    /**
     * 查询文件列表
     *
     * @param sysUploadFile 文件
     * @return 文件
     */
    @Override
    public List<SysUploadFile> selectSysUploadFileList(SysUploadFile sysUploadFile) {
        return sysUploadFileMapper.selectSysUploadFileList(sysUploadFile);
    }

    /**
     * 新增文件
     *
     * @param sysUploadFile 文件
     * @return 结果
     */
    @Override
    public int insertSysUploadFile(SysUploadFile sysUploadFile) {
        sysUploadFile.setCreateTime(DateUtils.getNowDate());
        return sysUploadFileMapper.insertSysUploadFile(sysUploadFile);
    }

    /**
     * 修改文件
     *
     * @param sysUploadFile 文件
     * @return 结果
     */
    @Override
    public int updateSysUploadFile(SysUploadFile sysUploadFile) {
        sysUploadFile.setUpdateTime(DateUtils.getNowDate());
        return sysUploadFileMapper.updateSysUploadFile(sysUploadFile);
    }

    /**
     * 批量删除文件
     *
     * @param ids 需要删除的文件主键
     * @return 结果
     */
    @Override
    public int deleteSysUploadFileByIds(String ids) {
        Assert.notBlank(ids, "请选择需要删除的数据!");
        Long[] longIds = Convert.toLongArray(ids);
        return sysUploadFileMapper.deleteSysUploadFileByIds(longIds);
    }

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

    @Override
    public Boolean delFile(String url) {
        if (StringUtils.isNotBlank(url)) {
            SysUploadFile sysUploadFile = sysUploadFileMapper.selectOne(SysUploadFile::getUrl, url);
            if (sysUploadFile != null) {
                sysUploadFileMapper.delete(SysUploadFile::getUrl, url);
                File file = new File(sysUploadFile.getPath());
                if (file.delete()) {
                    System.out.println("文件已成功删除: " + file.getAbsolutePath());
                    return true;
                } else {
                    System.err.println("文件删除失败: " + file.getAbsolutePath());
                }
            }
        }
        return true;
    }

    @Override
    public Boolean delFileByPath(String urlPath) {
        if (StringUtils.isNotBlank(urlPath)) {
            // 指定要删除的文件路径
            File file = new File(urlPath);
            if (file.delete()) {
                System.out.println("文件已成功删除: " + file.getAbsolutePath());
                return true;
            } else {
                System.err.println("文件删除失败: " + file.getAbsolutePath());
            }
        }
        return true;
    }
}
