package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.SysImplementationProgress;
import com.ruoyi.system.mapper.SysImplementationProgressMapper;
import com.ruoyi.system.service.ISysImplementationProgressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Base64;

/**
 * 项目实施进展Service实现类
 * 
 * @author ruoyi
 * @date 2026-02-26
 */
@Service
public class SysImplementationProgressServiceImpl implements ISysImplementationProgressService
{
    @Autowired
    private SysImplementationProgressMapper sysImplementationProgressMapper;

    /**
     * 获取项目实施进展信息
     * 
     * @return 项目实施进展信息
     */
    @Override
    public SysImplementationProgress selectSysImplementationProgress()
    {
        SysImplementationProgress implementationProgress = sysImplementationProgressMapper.selectSysImplementationProgress();
        if (implementationProgress == null)
        {
            implementationProgress = new SysImplementationProgress();
        }
        return implementationProgress;
    }

    /**
     * 保存项目实施进展信息
     * 
     * @param implementationProgress 项目实施进展信息
     * @return 结果
     */
    @Override
    public int saveSysImplementationProgress(SysImplementationProgress implementationProgress)
    {
        try
        {
            if (implementationProgress.getContent() != null && !implementationProgress.getContent().isEmpty())
            {
                byte[] decodedBytes = Base64.getDecoder().decode(implementationProgress.getContent());
                implementationProgress.setContent(new String(decodedBytes, "UTF-8"));
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        
        SysImplementationProgress existingProgress = sysImplementationProgressMapper.selectSysImplementationProgress();
        if (existingProgress == null)
        {
            // 如果不存在，执行插入操作
            return sysImplementationProgressMapper.insertSysImplementationProgress(implementationProgress);
        }
        else
        {
            // 如果存在，执行更新操作
            implementationProgress.setId(existingProgress.getId());
            return sysImplementationProgressMapper.updateSysImplementationProgress(implementationProgress);
        }
    }
}