package com.ruoyi.project.system.controller;

import java.util.ArrayList;
import java.util.List;
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.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.domain.SysMessage;
import com.ruoyi.project.system.service.ISysMessageService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 系统消息Controller
 *
 * @author ruoyi
 * @date 2022-05-26
 */
@RestController
@RequestMapping("/system/message")
public class SysMessageController extends BaseController
{
    @Autowired
    private ISysMessageService sysMessageService;


    /**
     * 查询系统消息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysMessage sysMessage)
    {
        sysMessage.setUserId(getUserId());
        startPage();
        List<SysMessage> list = sysMessageService.selectSysMessageList(sysMessage);
        for (SysMessage s: list
        ) {
            SysMessage sm = new SysMessage();
            sm.setId(s.getId());
            sm.setIsRead(1l);
            sysMessageService.updateSysMessage(sm);
        }
        return getDataTable(list);
    }

    /**
     * 查询系统消息列表
     */
    @GetMapping("/unlist")
    public List<SysMessage> unlist(SysMessage sysMessage)
    {
        sysMessage.setIsRead(0l);
        sysMessage.setUserId(getUserId());
        List<SysMessage> list = sysMessageService.selectSysMessageList(sysMessage);

        return list;
    }

    /**
     * 导出系统消息列表
     */
    @Log(title = "系统消息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(SysMessage sysMessage)
    {
        List<SysMessage> list = sysMessageService.selectSysMessageList(sysMessage);
        ExcelUtil<SysMessage> util = new ExcelUtil<SysMessage>(SysMessage.class);
        return util.exportExcel(list, "message");
    }

    /**
     * 获取系统消息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sysMessageService.selectSysMessageById(id));
    }

    /**
     * 新增系统消息
     */
    @Log(title = "系统消息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysMessage sysMessage)
    {
        return toAjax(sysMessageService.insertSysMessage(sysMessage));
    }

    /**
     * 修改系统消息
     */
    @Log(title = "系统消息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysMessage sysMessage)
    {
        return toAjax(sysMessageService.updateSysMessage(sysMessage));
    }

    /**
     * 删除系统消息
     */
    @Log(title = "系统消息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysMessageService.deleteSysMessageByIds(ids));
    }
}
