package com.ruoyi.system.service.impl;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.UUID;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.InvestigateDispositionTableMapper;
import com.ruoyi.system.domain.InvestigateDispositionTable;
import com.ruoyi.system.service.IInvestigateDispositionTableService;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

/**
 * 考察配置与考察主关联Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-10-10
 */
@Service
public class InvestigateDispositionTableServiceImpl implements IInvestigateDispositionTableService 
{
    @Autowired
    private InvestigateDispositionTableMapper investigateDispositionTableMapper;



    /**
     * 查询考察配置与考察主关联
     * 
     * @param investigateTableId 考察配置与考察主关联主键
     * @return 考察配置与考察主关联
     */
    @Override
    public InvestigateDispositionTable selectInvestigateDispositionTableByInvestigateTableId(Long investigateTableId)
    {
        return investigateDispositionTableMapper.selectInvestigateDispositionTableByInvestigateTableId(investigateTableId);
    }

    /**
     * 查询考察配置与考察主关联列表
     * 
     * @param investigateDispositionTable 考察配置与考察主关联
     * @return 考察配置与考察主关联
     */
    @Override
    public List<InvestigateDispositionTable> selectInvestigateDispositionTableList(InvestigateDispositionTable investigateDispositionTable)
    {
        return investigateDispositionTableMapper.selectInvestigateDispositionTableList(investigateDispositionTable);
    }

    /**
     * 新增考察配置与考察主关联
     * 
     * @param investigateDispositionTable 考察配置与考察主关联
     * @return 结果
     */
    @Override
    public int insertInvestigateDispositionTable(InvestigateDispositionTable investigateDispositionTable)
    {
        //获取HTML文件
        String content = investigateDispositionTable.getContent();
        content = " <meta charset=\"utf-8\">\n" +
                "<style>\n" +
                " body{margin: 0 !important;}\n" +
                "img{max-width: 100% !important;height: auto !important;}\n" +
                "</style>" + content;
        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            String fileNameHtml = UUID.randomUUID().toString();
            File html = File.createTempFile(fileNameHtml, ".html",new File(filePath));
            BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (html,true),StandardCharsets.UTF_8));
            writer.write(content);
            writer.close();
            InputStream inputStream = new FileInputStream(html);
            MultipartFile file = new org.springframework.mock.web.MockMultipartFile(html.getName(),html.getName(),"text/html",inputStream);

            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            investigateDispositionTable.setUrl(fileName);
            html.delete();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("文件生成成功！");
        return investigateDispositionTableMapper.insertInvestigateDispositionTable(investigateDispositionTable);
    }



    /**
     * 修改考察配置与考察主关联
     * 
     * @param investigateDispositionTable 考察配置与考察主关联
     * @return 结果
     */
    @Override
    public int updateInvestigateDispositionTable(InvestigateDispositionTable investigateDispositionTable)
    {

        String filePathOld = investigateDispositionTable.getUrl();
        //获取HTML文件
        String content = investigateDispositionTable.getContent();
        content = " <meta charset=\"utf-8\">\n" +
                "<style>\n" +
                " body{margin: 0 !important;}\n" +
                "img{max-width: 100% !important;height: auto !important;}\n" +
                "</style>" + content;
        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            String fileNameHtml = UUID.randomUUID().toString();
            File html = File.createTempFile(fileNameHtml, ".html",new File(filePath));
            BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (new FileOutputStream (html,true),StandardCharsets.UTF_8));
            writer.write(content);
            writer.close();
            InputStream inputStream = new FileInputStream(html);
            MultipartFile file = new org.springframework.mock.web.MockMultipartFile(html.getName(),html.getName(),"text/html",inputStream);

            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            investigateDispositionTable.setUrl(fileName);
            html.delete();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("文件生成成功！");
        return investigateDispositionTableMapper.updateInvestigateDispositionTable(investigateDispositionTable);
    }

    /**
     * 批量删除考察配置与考察主关联
     * 
     * @param investigateDispositionTable 需要删除的考察配置与考察主关联主键
     * @return 结果
     */
    @Override
    public int deleteInvestigateDispositionTableByInvestigateTableIds(InvestigateDispositionTable investigateDispositionTable)
    {
        return investigateDispositionTableMapper.deleteInvestigateDispositionTableByInvestigateTableIds(investigateDispositionTable);
    }

    /**
     * 删除考察配置与考察主关联信息
     * 
     * @param investigateTableId 考察配置与考察主关联主键
     * @return 结果
     */
    @Override
    public int deleteInvestigateDispositionTableByInvestigateTableId(Long investigateTableId)
    {
        return investigateDispositionTableMapper.deleteInvestigateDispositionTableByInvestigateTableId(investigateTableId);
    }
}
