package com.ruoyi.web.controller.progress;

import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysRealtimeNews;
import com.ruoyi.system.service.ISysRealtimeNewsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.core.page.TableDataInfo;

@RestController
@RequestMapping("/progress/realtimeNews")
public class SysRealtimeNewsController extends BaseController
{
    @Autowired
    private ISysRealtimeNewsService sysRealtimeNewsService;

    @PreAuthorize("@ss.hasPermi('progress:realtimeNews:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysRealtimeNews sysRealtimeNews)
    {
        startPage();
        List<SysRealtimeNews> list = sysRealtimeNewsService.selectSysRealtimeNewsList(sysRealtimeNews);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('progress:realtimeNews:export')")
    @Log(title = "实时动态", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysRealtimeNews sysRealtimeNews)
    {
        List<SysRealtimeNews> list = sysRealtimeNewsService.selectSysRealtimeNewsList(sysRealtimeNews);
        ExcelUtil<SysRealtimeNews> util = new ExcelUtil<SysRealtimeNews>(SysRealtimeNews.class);
        util.exportExcel(response, list, "实时动态数据");
    }

    @PreAuthorize("@ss.hasPermi('progress:realtimeNews:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysRealtimeNewsService.selectSysRealtimeNewsById(id));
    }

    @PreAuthorize("@ss.hasPermi('progress:realtimeNews:add')")
    @Log(title = "实时动态", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Map<String, Object> requestBody)
    {
        SysRealtimeNews sysRealtimeNews = new SysRealtimeNews();
        sysRealtimeNews.setTitle((String) requestBody.get("title"));
        sysRealtimeNews.setSummary((String) requestBody.get("summary"));
        sysRealtimeNews.setCoverImage((String) requestBody.get("coverImage"));
        sysRealtimeNews.setContentCn((String) requestBody.get("contentCn"));
        sysRealtimeNews.setContentEn((String) requestBody.get("contentEn"));
        sysRealtimeNews.setIsTop((String) requestBody.get("isTop"));
        sysRealtimeNews.setStatus((String) requestBody.get("status"));
        sysRealtimeNews.setRemark((String) requestBody.get("remark"));
        
        // 手动处理发布时间
        String publishTimeStr = (String) requestBody.get("publishTime");
        if (publishTimeStr != null) {
            try {
                Date publishTime = DateUtils.dateTime("yyyy-MM-dd HH:mm:ss", publishTimeStr);
                sysRealtimeNews.setPublishTime(publishTime);
            } catch (Exception e) {
                return AjaxResult.error("发布时间格式错误，请使用 yyyy-MM-dd HH:mm:ss 格式");
            }
        }
        
        return toAjax(sysRealtimeNewsService.insertSysRealtimeNews(sysRealtimeNews));
    }

    @PreAuthorize("@ss.hasPermi('progress:realtimeNews:edit')")
    @Log(title = "实时动态", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Map<String, Object> requestBody)
    {
        SysRealtimeNews sysRealtimeNews = new SysRealtimeNews();
        sysRealtimeNews.setId(((Number) requestBody.get("id")).longValue());
        sysRealtimeNews.setTitle((String) requestBody.get("title"));
        sysRealtimeNews.setSummary((String) requestBody.get("summary"));
        sysRealtimeNews.setCoverImage((String) requestBody.get("coverImage"));
        sysRealtimeNews.setContentCn((String) requestBody.get("contentCn"));
        sysRealtimeNews.setContentEn((String) requestBody.get("contentEn"));
        sysRealtimeNews.setIsTop((String) requestBody.get("isTop"));
        sysRealtimeNews.setStatus((String) requestBody.get("status"));
        sysRealtimeNews.setRemark((String) requestBody.get("remark"));
        
        // 手动处理发布时间
        String publishTimeStr = (String) requestBody.get("publishTime");
        if (publishTimeStr != null) {
            try {
                Date publishTime = DateUtils.dateTime("yyyy-MM-dd HH:mm:ss", publishTimeStr);
                sysRealtimeNews.setPublishTime(publishTime);
            } catch (Exception e) {
                return AjaxResult.error("发布时间格式错误，请使用 yyyy-MM-dd HH:mm:ss 格式");
            }
        }
        
        return toAjax(sysRealtimeNewsService.updateSysRealtimeNews(sysRealtimeNews));
    }

    @PreAuthorize("@ss.hasPermi('progress:realtimeNews:remove')")
    @Log(title = "实时动态", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysRealtimeNewsService.deleteSysRealtimeNewsByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('progress:realtimeNews:edit')")
    @Log(title = "实时动态", businessType = BusinessType.UPDATE)
    @PutMapping("/top/{id}/{isTop}")
    public AjaxResult updateTopStatus(@PathVariable Long id, @PathVariable String isTop)
    {
        return toAjax(sysRealtimeNewsService.updateTopStatus(id, isTop));
    }
}
