package com.ruoyi.web.controller.progress;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.system.domain.SysImplementationProgress;
import com.ruoyi.system.service.ISysImplementationProgressService;
import com.ruoyi.web.mapper.ImplementationProgressMapper;
import java.util.Base64;

/**
 * 项目实施进展Controller
 * 
 * @author ruoyi
 * @date 2026-02-26
 */
@Controller
@RequestMapping({"/system/projectProgress", "/project/progress"})
public class SysProjectProgressController extends BaseController
{
    private String prefix = "system/projectProgress";

    @Autowired
    private ImplementationProgressMapper implementationProgressMapper;
    
    @Autowired
    private ISysImplementationProgressService implementationProgressService;

    @GetMapping
    public String projectProgress(ModelMap mmap)
    {
        SysImplementationProgress projectProgress = implementationProgressMapper.selectSysImplementationProgressList(new SysImplementationProgress());
        mmap.put("projectProgress", projectProgress);
        return prefix + "/projectProgress";
    }

    @Anonymous
    @GetMapping("/get")
    @ResponseBody
    public AjaxResult getProjectProgressInfo()
    {
        SysImplementationProgress projectProgress = implementationProgressMapper.selectSysImplementationProgressList(new SysImplementationProgress());
        return AjaxResult.success(projectProgress);
    }

    @Anonymous
    @GetMapping("/list")
    @ResponseBody
    public AjaxResult listProgress(SysImplementationProgress progress)
    {
        SysImplementationProgress result = implementationProgressService.selectSysImplementationProgress();
        return AjaxResult.success(result);
    }

    @PreAuthorize("@ss.hasPermi('system:projectProgress:edit')")
    @PostMapping("/save")
    @ResponseBody
    public AjaxResult save(@RequestBody SysImplementationProgress projectProgress)
    {
        try {
            if (projectProgress.getContent() != null && !projectProgress.getContent().isEmpty()) {
                byte[] decodedBytes = Base64.getDecoder().decode(projectProgress.getContent());
                projectProgress.setContent(new String(decodedBytes, "UTF-8"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        projectProgress.setUpdateBy(getUsername());
        projectProgress.setUpdateTime(new java.util.Date());
        
        SysImplementationProgress existingProgress = implementationProgressMapper.selectSysImplementationProgressList(new SysImplementationProgress());
        
        if (existingProgress != null) {
            projectProgress.setId(existingProgress.getId());
            return toAjax(implementationProgressMapper.updateSysImplementationProgress(projectProgress));
        } else {
            projectProgress.setCreateBy(getUsername());
            projectProgress.setCreateTime(new java.util.Date());
            return toAjax(implementationProgressMapper.insertSysImplementationProgress(projectProgress));
        }
    }
}
