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.SysProjectProgress;
import com.ruoyi.system.service.ISysProjectProgressService;
import com.ruoyi.web.mapper.ImplementationProgressMapper;
import java.util.Base64;
import java.util.List;

/**
 * 项目实施进展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 ISysProjectProgressService projectProgressService;

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

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

    @Anonymous
    @GetMapping("/list")
    @ResponseBody
    public AjaxResult listProgress(SysProjectProgress progress)
    {
        List<SysProjectProgress> list = projectProgressService.selectSysProjectProgressList(progress);
        return AjaxResult.success(list);
    }

    @PreAuthorize("@ss.hasPermi('system:projectProgress:edit')")
    @PostMapping("/save")
    @ResponseBody
    public AjaxResult save(@RequestBody SysProjectProgress 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());
        
        SysProjectProgress existingProgress = implementationProgressMapper.selectSysProjectProgressList(new SysProjectProgress());
        
        if (existingProgress != null) {
            projectProgress.setId(existingProgress.getId());
            return toAjax(implementationProgressMapper.updateSysProjectProgress(projectProgress));
        } else {
            projectProgress.setCreateBy(getUsername());
            projectProgress.setCreateTime(new java.util.Date());
            return toAjax(implementationProgressMapper.insertSysProjectProgress(projectProgress));
        }
    }
}
