package com.contract.review.service.impl;

import com.contract.review.entity.Contract;
import com.contract.review.entity.ContractRisk;
import com.contract.review.mapper.ContractMapper;
import com.contract.review.mapper.ContractRiskMapper;
import com.contract.review.service.ContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 合同服务实现
 */
@Service
@Transactional
public class ContractServiceImpl implements ContractService {
    
    @Autowired
    private ContractMapper contractMapper;
    
    @Autowired
    private ContractRiskMapper contractRiskMapper;
    
    @Override
    public List<Contract> getContractList(Map<String, Object> params) {
        return contractMapper.getContractList(params);
    }
    
    @Override
    public int getContractCount(Map<String, Object> params) {
        return contractMapper.getContractCount(params);
    }
    
    @Override
    public Contract getContractById(Long id) {
        return contractMapper.getContractById(id);
    }
    
    @Override
    public Long addContract(Contract contract) {
        contractMapper.addContract(contract);
        return contract.getId();
    }
    
    @Override
    public int updateContract(Contract contract) {
        return contractMapper.updateContract(contract);
    }
    
    @Override
    public int deleteContract(Long id) {
        // 先删除关联的风险记录
        contractRiskMapper.deleteRiskByContractId(id);
        // 再删除合同
        return contractMapper.deleteContract(id);
    }
    
    @Override
    public List<ContractRisk> getContractRiskList(Long contractId) {
        return contractRiskMapper.getRiskListByContractId(contractId);
    }
    
    @Override
    public Map<String, Object> getStatistics() {
        return contractMapper.getStatistics();
    }
    
    @Override
    public String getContractContent(Long contractId) {
        Contract contract = contractMapper.getContractById(contractId);
        if (contract == null || contract.getSourceFile() == null) {
            return null;
        }
        
        String sourceFile = contract.getSourceFile();
        try {
            File file = new File(sourceFile);
            if (!file.exists()) {
                return null;
            }
            
            // 直接返回文件信息，前端会通过文件流获取并解析内容
            StringBuilder content = new StringBuilder();
            content.append("# 合同内容\n\n");
            content.append("## 文件信息\n");
            content.append("- 文件名称: " + file.getName() + "\n");
            content.append("- 文件大小: " + file.length() + " 字节\n");
            content.append("- 文件类型: " + (sourceFile.endsWith(".doc") ? "Microsoft Word 97-2003 Document (.doc)" : "Microsoft Word Document (.docx)") + "\n");
            content.append("\n## 合同内容\n");
            content.append("### 合同内容正在加载中...\n");
            content.append("请稍候，系统正在解析合同文件内容。");
            
            return content.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return "# 文件读取失败\n\n" + e.getMessage();
        }
    }
    
    private String convertToMarkdown(String content) {
        // 简单的转换逻辑，将文本转换为Markdown格式
        if (content == null) {
            return "";
        }
        
        // 替换换行符为Markdown换行
        content = content.replaceAll("\\r\\n", "\\n");
        
        // 添加标题
        String markdown = "# 合同内容\\n\\n";
        
        // 添加内容
        markdown += content;
        
        return markdown;
    }
}
